📄 toolbar.java
字号:
add(j); j.setFocusable(false); if (!placedGridDistance && (b == gridSmaller || b == gridLarger)) { placedGridDistance = true; rewriteGridDistance(); if (!currentGridAmountInited) { currentGridAmountInited = true; currentGridAmount.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { chooseGridAmount(); } }); } add(currentGridAmount); } } } updateUI(); } private void chooseGridAmount() { JPopupMenu gridMenu = new JPopupMenu("Grid Spacing"); JMenuItem menuItem = new JMenuItem("Grid Alignment 1 (largest)"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { changeGridSize(0); } }); gridMenu.add(menuItem); menuItem = new JMenuItem("Grid Alignment 2"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { changeGridSize(1); } }); gridMenu.add(menuItem); menuItem = new JMenuItem("Grid Alignment 3"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { changeGridSize(2); } }); gridMenu.add(menuItem); menuItem = new JMenuItem("Grid Alignment 4"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { changeGridSize(3); } }); gridMenu.add(menuItem); menuItem = new JMenuItem("Grid Alignment 5 (smallest)"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { changeGridSize(4); } }); gridMenu.add(menuItem); gridMenu.addSeparator(); menuItem = new JMenuItem("Grid Preferences..."); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { PreferencesFrame.preferencesCommand("Grid", "Display"); } }); gridMenu.add(menuItem); Point pt = currentGridAmount.getLocation(); gridMenu.show(this, pt.x, pt.y); } private static void rewriteGridDistance() { Dimension2D val = User.getAlignmentToGrid(); String valStr = " " + val.getWidth(); if (val.getWidth() != val.getHeight()) valStr += "/" + val.getHeight(); valStr += " "; if (TopLevel.isMDIMode()) { TopLevel tl = TopLevel.getCurrentJFrame(); if (tl != null) { ToolBar tb = tl.getToolBar(); tb.currentGridAmount.setText(valStr); } } else { for(Iterator<WindowFrame> it = WindowFrame.getWindows(); it.hasNext(); ) { WindowFrame wf = it.next(); ToolBar tb = wf.getFrame().getToolBar(); tb.currentGridAmount.setText(valStr); } } } /** * Method to find the EMenuItem associated with a full command name. */ private static EMenuItem findMenuItem(EMenu menu, String menuName, String command) { for (EMenuItem menuItem : menu.getItems()) { String menuItemName = menuName + ":" + menuItem.getText(); if (menuItem instanceof EMenu) { EMenuItem found = findMenuItem((EMenu)menuItem, menuItemName, command); if (found != null) return found; } else { if (menuItemName.equals(command)) return menuItem; } } return null; } // --------------------------- class EToolBarBuitton --------------------------------------------------------- /** * Generic tool bar button. */ public abstract static class EToolBarButton extends EMenuItem { /** * Default icon for tool bar button instance. */ private ImageIcon defaultIcon; private String iconName; private String menuName; private String fullPathToIcon; /** * @param text the menu item's displayed text. An "_" in the string * indicates the location of the "mnemonic" key for that entry. * @param accelerator the shortcut key, or null if none specified. * @param iconName filename without extension of default icon. */ EToolBarButton(String text, KeyStroke accelerator, String iconName, String menuName) { super(text, accelerator); this.iconName = iconName; this.menuName = menuName; if (iconName == null) this.defaultIcon = unknownIcon; else this.defaultIcon = Resources.getResource(ToolBar.class, iconName + ".gif"); } /** * @param text the menu item's displayed text. An "_" in the string * indicates the location of the "mnemonic" key for that entry. * @param acceleratorChar the shortcut char. * @param iconName filename without extension of default icon. */ EToolBarButton(String text, char acceleratorChar, String iconName, String menuName) { super(text, acceleratorChar); this.iconName = iconName; this.menuName = menuName; if (iconName == null) this.defaultIcon = unknownIcon; else this.defaultIcon = Resources.getResource(ToolBar.class, iconName + ".gif"); } /** * Method to return the name of the menu entry associated with this button. * @return the name of the menu entry associated with this button. */ public String getMenuName() { return menuName; } /** * Method to return the icon associated with this button. * @return the icon associated with this button. */ public ImageIcon getIcon() { return defaultIcon; } /** * Method to set the icon associated with this button. * @param i the new icon associated with this button. * @param path the full path to the icon file. */ public void setIcon(ImageIcon i, String path) { defaultIcon = i; fullPathToIcon = path; } /** * Generates tool bar button item by this this generic EToolBarButton * @return generated instance. */ public AbstractButton genToolBarButton() { AbstractButton b = createToolBarButton(); b.setToolTipText(getToolTipText()); b.setIcon(defaultIcon); b.addActionListener(this); updateToolBarButton(b); return b; } AbstractButton createToolBarButton() { return new JButton(); } /** * Updates appearance of tool bar button instance after change of state. */ void updateToolBarButton(AbstractButton item) { item.setEnabled(isEnabled()); item.setSelected(isSelected()); item.setToolTipText(getToolTipText()); } @Override protected void registerItem() { super.registerItem(); registerUpdatable(); } @Override protected void updateButtons() { updateToolBarButtons(); } } /** * Generic tool bar radio button. */ public static class EToolBarGeneralMenuButton extends EToolBarButton { private EMenuItem item; public EToolBarGeneralMenuButton(String text, String iconName, String menuName, EMenuItem item) { super(text, null, iconName, menuName); this.item = item; } public void run() { item.run(); } } /** * Generic tool bar radio button. */ private abstract static class EToolBarRadioButton extends EToolBarButton { EToolBarRadioButton(String text, KeyStroke accelerator, String iconName, String menuName) { super(text, accelerator, iconName, menuName); } EToolBarRadioButton(String text, char acceleratorChar, String iconName, String menuName) { super(text, acceleratorChar, iconName, menuName); } @Override protected JMenuItem createMenuItem() { if (Client.isOSMac()) return new JMenuItem(); return new JRadioButtonMenuItem(); } @Override JToggleButton createToolBarButton() { return new JToggleButton(); } } // --------------------------- Load/Save Library --------------------------------------------------------- public static final EToolBarButton openLibraryCommand = new EToolBarButton("_Open Library...", 'O', "ButtonOpenLibrary", "File") { @Override public void run() { FileMenu.openLibraryCommand(); } }; public static final EToolBarButton saveLibraryCommand = new EToolBarButton("Sa_ve Library", null, "ButtonSaveLibrary", "File") { @Override public boolean isEnabled() { return Library.getCurrent() != null; } @Override public void run() { FileMenu.saveLibraryCommand(Library.getCurrent()); } }; public static void setSaveLibraryButton() { updateToolBarButtons(); } // --------------------------- CursorMode staff --------------------------------------------------------- private static CursorMode curMode = CursorMode.CLICKZOOMWIRE; /** * CursorMode is a typesafe enum class that describes the current editing mode (select, zoom, etc). */ public static enum CursorMode { /** Describes ClickZoomWire mode (does everything). */ CLICKZOOMWIRE,// /** Describes Selection mode (click and drag). */ SELECT("Toggle Select"),// /** Describes wiring mode (creating arcs). */ WIRE("Toggle Wiring"), /** Describes Panning mode (move window contents). */ PAN, /** Describes Zoom mode (scale window contents). */ ZOOM, /** Describes Outline edit mode. */ OUTLINE, /** Describes Measure mode. */ MEASURE; } static final Cursor zoomCursor = readCursor("CursorZoom.gif", 6, 6); static final Cursor zoomOutCursor = readCursor("CursorZoomOut.gif", 6, 6); static final Cursor panCursor = readCursor("CursorPan.gif", 8, 8); static final Cursor wiringCursor = readCursor("CursorWiring.gif", 0, 0); static final Cursor outlineCursor = readCursor("CursorOutline.gif", 0, 8); static final Cursor measureCursor = readCursor("CursorMeasure.gif", 0, 0); public static Cursor readCursor(String cursorName, int hotX, int hotY) { ImageIcon imageIcon = Resources.getResource(ToolBar.class, cursorName); Image image = imageIcon.getImage(); int width = image.getWidth(null); int height = image.getHeight(null); Dimension bestSize = Toolkit.getDefaultToolkit().getBestCursorSize(width, height); int bestWidth = (int)bestSize.getWidth(); int bestHeight = (int)bestSize.getHeight(); if (bestWidth != 0 && bestHeight != 0) { if (bestWidth != width || bestHeight != height) { if (bestWidth > width && bestHeight > height) { // want a larger cursor, so just pad this one Image newImage = new BufferedImage(bestWidth, bestHeight, BufferedImage.TYPE_INT_ARGB); Graphics g = newImage.getGraphics(); g.drawImage(image, (bestWidth-width)/2, (bestHeight-height)/2, null); image = newImage; hotX += (bestWidth-width)/2; hotY += (bestHeight-height)/2; } else { // want a smaller cursor, so scale this one image = image.getScaledInstance(bestWidth, bestHeight, 0); hotX = hotX * bestWidth / width; hotY = hotY * bestHeight / height; } } } Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(hotX, hotY), cursorName); return cursor; } /** * Method to tell which cursor mode is in effect. * @return the current mode (select, pan, zoom, outline, measure). */ public static CursorMode getCursorMode() { return curMode; } private static EventListener lastListener = null; private static void setCursorMode(CursorMode cm) { switch (cm) { case CLICKZOOMWIRE: checkLeavingOutlineMode(); WindowFrame.setListener(ClickZoomWireListener.theOne); TopLevel.setCurrentCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); curMode = CursorMode.CLICKZOOMWIRE; lastListener = null; break; case PAN: if (WindowFrame.getListener() == ZoomAndPanListener.theOne && curMode == CursorMode.PAN) { // if there was a special mode, revert to it if (lastListener != null && lastListener != ClickZoomWireListener.theOne && lastListener != OutlineListener.theOne && lastListener != MeasureListener.theOne) { WindowFrame.setListener(lastListener); curMode = CursorMode.CLICKZOOMWIRE; lastListener = null; TopLevel.setCurrentCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); return; } // switch back to click zoom wire listener setCursorMode(CursorMode.CLICKZOOMWIRE); return; } lastListener = WindowFrame.getListener(); WindowFrame.setListener(ZoomAndPanListener.theOne); //makeCursors(); TopLevel.setCurrentCursor(panCursor); curMode = CursorMode.PAN; break; case ZOOM: if (WindowFrame.getListener() == ZoomAndPanListener.theOne && curMode == CursorMode.ZOOM) { // if there was a special mode, revert to it if (lastListener != null && lastListener != ClickZoomWireListener.theOne && lastListener != OutlineListener.theOne && lastListener != MeasureListener.theOne) { WindowFrame.setListener(lastListener); curMode = CursorMode.CLICKZOOMWIRE; lastListener = null; TopLevel.setCurrentCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); return; } // switch back to click zoom wire listener setCursorMode(CursorMode.CLICKZOOMWIRE); return; } lastListener = WindowFrame.getListener(); checkLeavingOutlineMode(); WindowFrame.setListener(ZoomAndPanListener.theOne); TopLevel.setCurrentCursor(zoomCursor); curMode = CursorMode.ZOOM; break; case OUTLINE: lastListener = null; if (WindowFrame.getListener() == OutlineListener.theOne) { // switch back to click zoom wire listener setCursorMode(CursorMode.CLICKZOOMWIRE); return; } EditWindow wnd = EditWindow.needCurrent(); if (wnd == null) return; Highlighter highlighter = wnd.getHighlighter(); CursorMode oldMode = curMode; NodeInst ni = (NodeInst)highlighter.getOneElectricObject(NodeInst.class); if (ni == null) { if (oldMode == CursorMode.OUTLINE) setCursorMode(CursorMode.CLICKZOOMWIRE); else setCursorMode(oldMode); return; } NodeProto np = ni.getProto(); if (ni.isCellInstance() || !((PrimitiveNode)np).isHoldsOutline()) { System.out.println("Sorry, " + np + " does not hold outline information"); if (oldMode == CursorMode.OUTLINE) setCursorMode(CursorMode.CLICKZOOMWIRE); else setCursorMode(oldMode); return; } if (WindowFrame.getListener() != OutlineListener.theOne) OutlineListener.theOne.setNode(ni); WindowFrame.setListener(OutlineListener.theOne); TopLevel.setCurrentCursor(outlineCursor); curMode = CursorMode.OUTLINE; break; case MEASURE: lastListener = null; if (WindowFrame.getListener() == MeasureListener.theOne) { // switch back to click zoom wire listener setCursorMode(CursorMode.CLICKZOOMWIRE); return; } checkLeavingOutlineMode(); MeasureListener.theOne.reset(); WindowFrame.setListener(MeasureListener.theOne); TopLevel.setCurrentCursor(measureCursor); curMode = CursorMode.MEASURE; break; } } private static void checkLeavingOutlineMode() { // if exiting outline-edit mode, turn off special display if (WindowFrame.getListener() == OutlineListener.theOne && curMode == CursorMode.OUTLINE) { EditWindow wnd = EditWindow.needCurrent(); if (wnd != null) { Highlighter highlighter = wnd.getHighlighter(); NodeInst ni = (NodeInst)highlighter.getOneElectricObject(NodeInst.class); if (ni != null) { Highlight2 high = highlighter.getOneHighlight(); if (high != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -