@Override
public void mouseReleased ( final MouseEvent e )
{
final WebSelectablePanel wsp = WebSelectablePanel.this;
final ComponentPanelLayout cpl = getContainerLayout ();
if ( SwingUtilities.isLeftMouseButton ( e ) && dragged )
{
// Stop drag
dragged = false;
// Update if needed
if ( getY () - startY == 0 || cpl.getComponents ().size () <= 1 )
{
// Ignore drag if there is only 1 component or change is zero
cpl.setComponentShift ( wsp, null );
}
else
{
// Dragged panel index and middle
final int oldIndex = cpl.indexOf ( wsp );
final int middle = getMiddleY ( wsp );
// Searching for insert index
int insertIndex = 0;
for ( final Component component : cpl.getComponents () )
{
if ( component != wsp && middle > getMiddleY ( component ) )
{
insertIndex = cpl.indexOf ( component ) + 1;
}
}
// Fix index
if ( insertIndex > oldIndex )
{
insertIndex--;
}
// Resetting shift
cpl.setComponentShift ( wsp, null );
// Changing panel location if it has actually changed
if ( oldIndex != insertIndex )
{
// Updating panel indices
cpl.removeLayoutComponent ( wsp );
cpl.insertLayoutComponent ( insertIndex, wsp );
updateAllBorders ();
// Informing all listeners
fireComponentOrderChanged ( wsp.getComponent (), oldIndex, insertIndex );
}