📄 layersmenu.java
字号:
Component[] components = getMenuComponents(); if (components.length > 0) { Debug.message("layersmenu", "LayersMenu.removeAll(): purging menu"); } for (int i = 0; i < components.length; i++) { if (components[i] instanceof LayerCheckBoxMenuItem) { ((LayerCheckBoxMenuItem) components[i]).cleanup(); } } super.removeAll(); } /** * Update the layer names. */ public synchronized void updateLayerLabels() { if (layerHandler != null) { setLayers(layerHandler.getLayers()); } } /** * CheckBoxMenuItem that encapsulates a Layer. */ class LayerCheckBoxMenuItem extends JCheckBoxMenuItem implements ActionListener, ComponentListener { /** The layer that the button triggers. */ Layer layer; /** * Construct the menu item, connected to the given layer. */ LayerCheckBoxMenuItem(Layer aLayer) { if (aLayer == null) { throw new IllegalArgumentException("null Layer"); } layer = aLayer; this.setText(layer.getName()); setState(layer.isVisible()); this.addActionListener(this); layer.addComponentListener(this); } /** Get the layer for this checkbox. */ public Layer getLayer() { return layer; } /** * Disconnect all the listeners from the layer, clean up other * references. */ public void cleanup() { layer.removeComponentListener(this); this.removeActionListener(this); layer = null; } /** * If this widget is being used for bringing up palettes, bring up the * layer's palette. */ protected void showPalette() { layer.showPalette(); } /** * If this widget is being used for bringing up palettes, hide the * layer's palette. */ protected void hidePalette() { layer.hidePalette(); } /** This menu item listens to the status of its layer. */ public void componentResized(ComponentEvent e) {} /** This menu item listens to the status of its layer. */ public void componentMoved(ComponentEvent e) {} /** * This menu item listens to the status of its layer. If the layer * becomes visible, it makes the check box enabled. */ public void componentShown(ComponentEvent e) { if (e.getComponent() == layer) { if (getState() != true && menuType == LAYERS_ON_OFF) { setState(true); if (Debug.debugging("layersmenu")) { Debug.output("layersmenu.LCBMI: layer " + layer.getName() + " is now visible."); } } } else if (e.getComponent() == layer.getPalette() && menuType == PALETTES_ON_OFF) { setState(true); } } /** * This menu item listens to the status of its layer. If the layer * becomes invisible, it disables the check box. */ public void componentHidden(ComponentEvent e) { if (e.getComponent() == layer) { if (getState() != false && menuType == LAYERS_ON_OFF) { setState(false); if (Debug.debugging("layersmenu")) { Debug.output("layersmenu.LCBMI: layer " + layer.getName() + " is now hidden."); } } } else if (e.getComponent() == layer.getPalette() && menuType == PALETTES_ON_OFF) { setState(false); } } /** * Triggered by the menu item check button. * * @param e ActionEvent fired by the button. */ public void actionPerformed(ActionEvent e) { if (!this.equals(e.getSource())) { Debug.error("Wiring is hopelessly wrong in LayersMenu"); } switch (menuType) { case LAYERS_ON_OFF: layerHandler.turnLayerOn(getState(), layer); break; case PALETTES_ON_OFF: if (getState()) showPalette(); else hidePalette(); break; default: System.err.println("LayersMenu: unknown menuType!"); } } } /** * Given a LayersPanel, set up a JButton to add to the end of the LayersMenu * that will trigger the action listener of the LayersPanel. * * @param lp the LayersPanel to ask for an ActionListener from. */ public void setupEditLayersButton(LayersPanel lp) { // initalize the Edit Layers... button. JMenuItem button = new JMenuItem(i18n.get(LayersMenu.class, "editLayersButtonTitle", editLayersButtonTitle)); button.setActionCommand("edit"); button.addActionListener(lp.getActionListener()); setEdit(button); setLayersPanel(lp); } /** * Constructs the menu item that will bring up the LayerAddPanel. */ public void setupLayerAddButton(final LayerAddPanel menu) { final LayerAddPanel lap = menu; // JMenuItem button = new JMenuItem(addLayersButtonTitle); JMenuItem button = new JMenuItem(i18n.get(LayersMenu.class, "addLayersButtonTitle", addLayersButtonTitle)); button.setActionCommand("add"); button.addActionListener(lap); setAdd(button); add(button); } /** * Called when the BeanContext membership changes with object from the * BeanContext. This lets this object hook up with what it needs. It expects * to find only one LayerHandler and LayersPanel in the BeanContext. If * another LayerHandler/LayersPanel is somehow added to the BeanContext, * however, it will drop the connection to the component it is set up to * listen to, and rewire itself to reflect the status of the last version of * the LayerHandler/LayersPanel found. * * @param someObj Object received in the BeanContext or * BeanContextMembershipEvent. */ public void findAndInit(Object someObj) { if (someObj instanceof LayerHandler) { // do the initializing that need to be done here Debug.message("bc", "LayersMenu found a LayerHandler"); setLayerHandler((LayerHandler) someObj); } else if (someObj instanceof LayersPanel) { setupEditLayersButton((LayersPanel) someObj); } else if (someObj instanceof LayerAddPanel) { // if a LayerAddPanel is present, do the necessary things setupLayerAddButton((LayerAddPanel) someObj); } } /** * Called when objects are removed from the BeanContext. Should be used to * check for relevant objects that need to be disconnected from this object. * This method does check to see if a LayerHandler or LayersPanel is being * removed, that it is the same object currently being used by this * LayersMenu. * * @param someObj the object being removed from the BeanContext */ public void findAndUndo(Object someObj) { if (someObj instanceof LayerHandler) { LayerHandler lh = (LayerHandler) someObj; // Need to check to see if this layerhandler is the // same as the one we have !!!! if (lh != getLayerHandler()) { Debug.message("bc", "LayersMenu asked to remove LayerHandler that is not the same as what is currently held - ignoring request."); return; } // do the initializing that need to be done here Debug.message("bc", "LayersMenu.childrenRemoved: removing LayerHandler"); setLayerHandler(null); setEdit(null); } if (someObj instanceof LayersPanel) { LayersPanel lp = (LayersPanel) someObj; // There's a problem here. We can't tell if the // LayersPanel being removed is the owner of the // action listener used by the edit button. If two // LayersPanels have been added to the BeanContext, // and we're now listening to the second one, then if // the first one is removed, we are forced here to // disconnect from the second and valid one. Looks // like we need to maintain a handle on the // LayersPanel being triggered. if (lp != getLayersPanel()) { Debug.message("bc", "LayersMenu asked to remove LayersPanel that is not the same as what is currently held - ignoring request."); return; } // do the initializing that need to be done here Debug.message("bc", "LayersMenu.childrenRemoved: removing LayersPanel"); setLayersPanel(null); setEdit(null); } } /** * Report a vetoable property update to any registered listeners. If anyone * vetos the change, then fire a new event reverting everyone to the old * value and then rethrow the PropertyVetoException. * <P> * * No event is fired if old and new are equal and non-null. * <P> * * @param name The programmatic name of the property that is about to change * * @param oldValue The old value of the property * @param newValue - The new value of the property * * @throws PropertyVetoException if the recipient wishes the property change * to be rolled back. */ public void fireVetoableChange(String name, Object oldValue, Object newValue) throws PropertyVetoException { super.fireVetoableChange(name, oldValue, newValue); beanContextChildSupport.fireVetoableChange(name, oldValue, newValue); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -