下拉菜单按钮



import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import org.openide.util.Utilities;

/***/ /**
*JToggleButtonwithasmallarrowthatdisplayspopupmenuwhenclicked.
*
*
@authorS.Aubrecht
*
@since6.11
*/

class DropDownToggleButton extends JToggleButton ... {

privatebooleanmouseInButton=false;
privatebooleanmouseInArrowArea=false;

privateMap<String,Icon>regIcons=newHashMap<String,Icon>(5);
privateMap<String,Icon>arrowIcons=newHashMap<String,Icon>(5);

privatestaticfinalStringICON_NORMAL="normal";//NOI18N
privatestaticfinalStringICON_PRESSED="pressed";//NOI18N
privatestaticfinalStringICON_ROLLOVER="rollover";//NOI18N
privatestaticfinalStringICON_ROLLOVER_SELECTED="rolloverSelected";//NOI18N
privatestaticfinalStringICON_SELECTED="selected";//NOI18N
privatestaticfinalStringICON_DISABLED="disabled";//NOI18N
privatestaticfinalStringICON_DISABLED_SELECTED="disabledSelected";//NOI18N

privatestaticfinalStringICON_ROLLOVER_LINE="rolloverLine";//NOI18N
privatestaticfinalStringICON_ROLLOVER_SELECTED_LINE="rolloverSelectedLine";//NOI18N

privatePopupMenuListenermenuListener;

/***//**CreatesanewinstanceofDropDownToggleButton*/
publicDropDownToggleButton(Iconicon,JPopupMenupopup)...{
assertnull!=icon;

putClientProperty(DropDownButtonFactory.PROP_DROP_DOWN_MENU,popup);

setIcon(icon);

resetIcons();

addPropertyChangeListener(DropDownButtonFactory.PROP_DROP_DOWN_MENU,
newPropertyChangeListener()...{
publicvoidpropertyChange(PropertyChangeEvente)...{
resetIcons();
}

}
);

addMouseMotionListener(
newMouseMotionAdapter()...{
publicvoidmouseMoved(MouseEvente)...{
if(null!=getPopupMenu())...{
mouseInArrowArea
=isInArrowArea(e.getPoint());
updateRollover(_getRolloverIcon(),_getRolloverSelectedIcon());
}

}

}
);

addMouseListener(
newMouseAdapter()...{
privatebooleanpopupMenuOperation=false;

publicvoidmousePressed(MouseEvente)...{
popupMenuOperation
=false;
JPopupMenumenu
=getPopupMenu();
if(menu!=null&&getModel()instanceofModel)...{
Modelmodel
=(Model)getModel();
if(!model._isPressed())...{
if(isInArrowArea(e.getPoint())&&menu.getComponentCount()>0)...{
model._press();
menu.addPopupMenuListener(getMenuListener());
menu.show(DropDownToggleButton.
this,0,getHeight());
popupMenuOperation
=true;
}

}
else...{
model._release();
menu.removePopupMenuListener(getMenuListener());
popupMenuOperation
=true;
}

}

}


@Override
publicvoidmouseReleased(MouseEvente)...{
//Ifwedonesomethingwiththepopupmenu,weshouldconsume
//theevent,otherwisethebutton'sactionwillbetriggered.
if(popupMenuOperation)...{
popupMenuOperation
=false;
e.consume();
}

}


publicvoidmouseEntered(MouseEvente)...{
mouseInButton
=true;
if(hasPopupMenu())...{
mouseInArrowArea
=isInArrowArea(e.getPoint());
updateRollover(_getRolloverIcon(),_getRolloverSelectedIcon());
}

}


publicvoidmouseExited(MouseEvente)...{
mouseInButton
=false;
mouseInArrowArea
=false;
if(hasPopupMenu())...{
updateRollover(_getRolloverIcon(),_getRolloverSelectedIcon());
}

}

}
);

setModel(
newModel());
}


privatePopupMenuListenergetMenuListener()...{
if(null==menuListener)...{
menuListener
=newPopupMenuListener()...{
publicvoidpopupMenuWillBecomeVisible(PopupMenuEvente)...{
}


publicvoidpopupMenuWillBecomeInvisible(PopupMenuEvente)...{
//Ifinsidethebuttonletthebutton'smouselistener
//dealwiththestate.Thepopupmenuwillbehiddenand
//weshouldnotshowitagain.
if(!mouseInButton)...{
if(getModel()instanceofModel)...{
((Model)getModel())._release();
}

JPopupMenumenu
=getPopupMenu();
if(null!=menu)...{
menu.removePopupMenuListener(
this);
}

}

}


publicvoidpopupMenuCanceled(PopupMenuEvente)...{
}

}
;
}

returnmenuListener;
}


privatevoidupdateRollover(Iconrollover,IconrolloverSelected)...{
super.setRolloverIcon(rollover);
super.setRolloverSelectedIcon(rolloverSelected);
}


privatevoidresetIcons()...{
Iconicon
=regIcons.get(ICON_NORMAL);
if(null!=icon)
setIcon(icon);

icon
=regIcons.get(ICON_PRESSED);
if(null!=icon)
setPressedIcon(icon);

icon
=regIcons.get(ICON_ROLLOVER);
if(null!=icon)
setRolloverIcon(icon);

icon
=regIcons.get(ICON_ROLLOVER_SELECTED);
if(null!=icon)
setRolloverSelectedIcon(icon);

icon
=regIcons.get(ICON_SELECTED);
if(null!=icon)
setSelectedIcon(icon);

icon
=regIcons.get(ICON_DISABLED);
if(null!=icon)
setDisabledIcon(icon);

icon
=regIcons.get(ICON_DISABLED_SELECTED);
if(null!=icon)
setDisabledSelectedIcon(icon);
}


privateIcon_getRolloverIcon()...{
Iconicon
=null;
icon
=arrowIcons.get(mouseInArrowArea?ICON_ROLLOVER:ICON_ROLLOVER_LINE);
if(null==icon)...{
Iconorig
=regIcons.get(ICON_ROLLOVER);
if(null==orig)
orig
=regIcons.get(ICON_NORMAL);
icon
=newIconWithArrow(orig,!mouseInArrowArea);
arrowIcons.put(mouseInArrowArea
?ICON_ROLLOVER:ICON_ROLLOVER_LINE,icon);
}

returnicon;
}


privateIcon_getRolloverSelectedIcon()...{
Iconicon
=null;
t
分享到:
评论

你可能感兴趣的:(swing)