📄 toolbar.java
字号:
/* -*- tab-width: 4 -*- * * Electric(tm) VLSI Design System * * File: ToolBar.java * * Copyright (c) 2003 Sun Microsystems and Static Free Software * * Electric(tm) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Electric(tm) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */package com.sun.electric.tool.user.ui;import com.sun.electric.database.change.Undo;import com.sun.electric.database.geometry.Dimension2D;import com.sun.electric.database.hierarchy.Cell;import com.sun.electric.database.hierarchy.Library;import com.sun.electric.database.prototype.NodeProto;import com.sun.electric.database.text.Pref;import com.sun.electric.database.topology.NodeInst;import com.sun.electric.technology.PrimitiveNode;import com.sun.electric.tool.Client;import com.sun.electric.tool.user.CircuitChanges;import com.sun.electric.tool.user.Highlight2;import com.sun.electric.tool.user.Highlighter;import com.sun.electric.tool.user.Resources;import com.sun.electric.tool.user.User;import com.sun.electric.tool.user.UserInterfaceMain;import com.sun.electric.tool.user.dialogs.GetInfoText;import com.sun.electric.tool.user.dialogs.PreferencesFrame;import com.sun.electric.tool.user.menus.EMenu;import com.sun.electric.tool.user.menus.EMenuBar;import com.sun.electric.tool.user.menus.EMenuItem;import com.sun.electric.tool.user.menus.FileMenu;import com.sun.electric.tool.user.menus.MenuCommands;import java.awt.Component;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Image;import java.awt.Point;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.geom.Point2D;import java.awt.image.BufferedImage;import java.io.File;import java.util.ArrayList;import java.util.EventListener;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.imageio.ImageIO;import javax.swing.AbstractButton;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JCheckBoxMenuItem;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.JRadioButtonMenuItem;import javax.swing.JToggleButton;import javax.swing.JToolBar;import javax.swing.KeyStroke;/** * This class manages the Electric toolbar. */public class ToolBar extends JToolBar{ private static ImageIcon unknownIcon = Resources.getResource(ToolBar.class, "ButtonUnknown.gif"); private static Pref toolbarOrderPref = Pref.makeStringPref("ToolbarOrder", User.getUserTool().prefs, ""); private static Pref toolbarFilesPref = Pref.makeStringPref("ToolbarIconFiles", User.getUserTool().prefs, ""); private static EToolBarButton[] currentToolbarButtons; private static List<EToolBarButton> allButtons = new ArrayList<EToolBarButton>(); private static Map<String,String> commandToIconMap; /** * Method to create the toolbar. */ public static ToolBar createToolBar() { return new ToolBar(); } private ToolBar() { setFloatable(true); setRollover(true); redoToolbar(); setFocusable(false); } /** * Method to return the "factory default" set of toolbar buttons. * @return an array of default toolbar buttons. */ public static EToolBarButton[] getFactoryButtons() { // use built-in default EToolBarButton[] buttons = new EToolBarButton[] { openLibraryCommand, // File:Open Library... saveLibraryCommand, // File:Save All Libraries null, clickZoomWireCommand, // Edit:Modes:Edit:Click/Zoom/Wire panCommand, // Edit:Modes:Edit:Toggle Pan zoomCommand, // Edit:Modes:Edit:Toggle Zoom outlineCommand, // Edit:Modes:Edit:Toggle Outline Edit measureCommand, // Edit:Modes:Edit:Toggle Measure Distance null, gridLarger, gridSmaller, null, selectObjectsCommand, // Edit:Modes:Select:Select Objects selectAreaCommand, // Edit:Modes:Select:Select Area null, toggleSelectSpecialCommand, // Edit:Modes:Select:Toggle Special Select null, preferencesCommand, // File:Preferences... null, undoCommand, // Edit:Undo redoCommand, // Edit:Redo null, goBackButtonStatic, // Window:Go To Previous Focus goForwardButtonStatic, // Window:Go To Next Focus null, expandOneLevelCommand, // Cell:Expand Cell Instances:One Level Down unexpandOneLevelCommand // Cell:Unexpand Cell Instances:One Level Up }; return buttons; } /** * Method to return a list of all known toolbar buttons. * @return a list of all known toolbar buttons. */ public static List<EToolBarButton> getAllButtons() { if (allButtons.size() == 0) { allButtons.add(openLibraryCommand); allButtons.add(saveLibraryCommand); allButtons.add(clickZoomWireCommand); allButtons.add(panCommand); allButtons.add(zoomCommand); allButtons.add(outlineCommand); allButtons.add(measureCommand); allButtons.add(gridLarger); allButtons.add(gridSmaller); allButtons.add(selectObjectsCommand); allButtons.add(selectAreaCommand); allButtons.add(toggleSelectSpecialCommand); allButtons.add(preferencesCommand); allButtons.add(undoCommand); allButtons.add(redoCommand); allButtons.add(goBackButtonStatic); allButtons.add(goForwardButtonStatic); allButtons.add(expandOneLevelCommand); allButtons.add(unexpandOneLevelCommand); } return allButtons; } /** * Method to return all of the buttons in the toolbar. * @return an array of buttons in the toolbar. */ public static EToolBarButton[] getToolbarButtons() { if (currentToolbarButtons == null) currentToolbarButtons = getDefaultToolbarButtons(); return currentToolbarButtons; } /** * Method to return the icon to use when no icon can be found. * @return the default icon for the toolbar. */ public static ImageIcon getUnknownIcon() { return unknownIcon; } /** * Method to change the order of buttons in the toolbar. * This affects the current toolbar and is also saved for future * runs of Electric. * @param buttons the new order of buttons in the toolbar. */ public static void setToolbarButtons(EToolBarButton[] buttons) { currentToolbarButtons = buttons; // build a string describing the current buttons StringBuffer sb = new StringBuffer(); for(int i=0; i<buttons.length; i++) { if (i > 0) sb.append('|'); if (buttons[i] == null) continue; if (buttons[i].fullPathToIcon != null) { // button with user-specified icon String commandName = buttons[i].menuName+":"+buttons[i].getText(); sb.append("U="); sb.append(commandName); sb.append("=U="); sb.append(buttons[i].fullPathToIcon); } else { // built-in button if (buttons[i].iconName.equals("ButtonUnknown")) { sb.append("U="); sb.append(buttons[i].menuName+":"+buttons[i].getText()); } else { sb.append("B="); sb.append(buttons[i].iconName); } } } toolbarOrderPref.setString(sb.toString()); if (TopLevel.isMDIMode()) { ToolBar tb = TopLevel.getCurrentJFrame().getToolBar(); tb.redoToolbar(); } else { for(Iterator<WindowFrame> it = WindowFrame.getWindows(); it.hasNext(); ) { WindowFrame wf = it.next(); ToolBar tb = wf.getFrame().getToolBar(); tb.redoToolbar(); } } } /** * Method to convert an image file name into a proper-sized icon for the Toolbar. * @param fileName the path to the image file. * @return the toolbar icon (no more than 16 tall). */ public static ImageIcon getProperSizeIcon(String fileName) { String errorMessage = null; try { File f = new File(fileName); if (!f.exists()) errorMessage = "Missing file"; else { BufferedImage img = ImageIO.read(f); ImageIcon icon = new ImageIcon(img); // force it to be 16x16 or less int iconWidth = icon.getIconWidth(); int iconHeight = icon.getIconHeight(); int width = iconWidth; int height = iconHeight; if (height > 16) { double ratio = 16.0 / height; width = (int)(width * ratio); height = (int)(height * ratio); int iconWid = Math.max(width, 16); BufferedImage originalImage = new BufferedImage(iconWid, 16, BufferedImage.TYPE_INT_ARGB); Graphics g = originalImage.getGraphics(); int dx = (iconWid-width)/2; int dy = (16-height)/2; g.drawImage(icon.getImage(), dx, dy, dx+width, dy+height, 0, 0, iconWidth, iconHeight, null); icon = new ImageIcon(originalImage); } return icon; } } catch (Exception e) { errorMessage = e.getMessage(); } catch (OutOfMemoryError e) { errorMessage = "Out of Memory"; } System.out.println("Unable to read icon file: " + fileName + " (" + errorMessage + ")"); return null; } /** * Method to return a mapping from command names to disk files with their icons. * @return a mapping from command names to disk files with their icons. */ public static Map<String,String> getCommandToIconMap() { if (commandToIconMap == null) { commandToIconMap = new HashMap<String,String>(); String fileMap = toolbarFilesPref.getString(); String [] entries = fileMap.split("\t"); for(int i=0; i<entries.length; i++) { int barPos = entries[i].indexOf('|'); if (barPos < 0) continue; String commandName = entries[i].substring(0, barPos); String fileName = entries[i].substring(barPos+1); commandToIconMap.put(commandName, fileName); } } return commandToIconMap; } /** * Method to set a mapping from command names to disk files with their icons. * @param newMap a new mapping from command names to disk files with their icons. */ public static void setCommandToIconMap(Map<String,String> newMap) { commandToIconMap = newMap; StringBuffer sb = new StringBuffer(); for(String commandName : commandToIconMap.keySet()) { String fileName = commandToIconMap.get(commandName); if (sb.length() > 0) sb.append('\t'); sb.append(commandName); sb.append('|'); sb.append(fileName); } toolbarFilesPref.setString(sb.toString()); } /** * Method to get the default button order for the toolbar. * This considers saved orders from previous runs of Electric. * @return the default button order for the toolbar. */ private static EToolBarButton[] getDefaultToolbarButtons() { String prefOrder = toolbarOrderPref.getString(); boolean buttonError = false; if (prefOrder.length() > 0) { // preferences set List<EToolBarButton> knownButtons = getAllButtons(); EMenuBar.Instance bar = MenuCommands.menuBar().genInstance(null); String [] entries = prefOrder.split("\\|"); EToolBarButton[] buttons = new EToolBarButton[entries.length]; for(int i=0; i<entries.length; i++) { String entry = entries[i]; if (entry.length() == 0) continue; if (entry.startsWith("B=")) { // built-in button String buttonName = entry.substring(2); for(EToolBarButton known : knownButtons) { if (known.iconName.equals(buttonName)) { buttons[i] = known; break; } } if (buttons[i] == null) { System.out.println("WARNING: saved tool bar button '" + buttonName + "' is unknown"); buttonError = true; } } else if (entry.startsWith("U=")) { // user-defined button String fullCommandName = entry.substring(2); String iconFile = null; int endPath = entry.indexOf("=U="); if (endPath >= 0) { fullCommandName = entry.substring(2, endPath); iconFile = entry.substring(endPath+3); } ImageIcon icon = unknownIcon; if (iconFile != null) icon = getProperSizeIcon(iconFile); if (icon != null) { int lastColon = fullCommandName.lastIndexOf(':'); String menuName = (lastColon < 0) ? "" : fullCommandName.substring(0, lastColon); String commandName = fullCommandName.substring(lastColon+1); // scan all commands to find this one EMenuItem mi = null; for (EMenuItem menu: bar.getMenuBarGroup().getItems()) { mi = findMenuItem((EMenu)menu, menu.getText(), fullCommandName); if (mi != null) break; } if (mi != null) { buttons[i] = makeButtonFromMenuItem(mi, commandName, menuName); if (iconFile != null) buttons[i].setIcon(icon, iconFile); boolean alreadyKnown = false; for(EToolBarButton known : allButtons) { if (known.iconName != null && known.iconName.equals(fullCommandName)) { alreadyKnown = true; break; } } if (!alreadyKnown) allButtons.add(buttons[i]); } } } } if (!buttonError) return buttons; toolbarOrderPref.setString(""); System.out.println("WARNING: saved toolbar configuration has errors...using factory settings"); } // use built-in default return getFactoryButtons(); } private static EToolBarButton makeButtonFromMenuItem(EMenuItem mi, String command, String menu) { final EMenuItem item = mi; EToolBarButton but = new EToolBarButton(command, null, null, menu) { public void run() { item.run(); } }; return but; } private void redoToolbar() { removeAll(); EToolBarButton[] buttons = getToolbarButtons(); boolean placedGridDistance = false; for (int i=0; i<buttons.length; i++) { EToolBarButton b = buttons[i]; if (b == null) addSeparator(); else { // special case for buttons that are different in each toolbar if (b == goBackButtonStatic) b = goBackButton; if (b == goForwardButtonStatic) b = goForwardButton; AbstractButton j = b.genToolBarButton();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -