📄 toolbartab.java
字号:
int lastColon = what.lastIndexOf(':'); String menuName = (lastColon < 0) ? "" : what.substring(0, lastColon); String commandName = what.substring(lastColon+1); EMenuItem mi = menuEntries.get(what); but = new ToolBar.EToolBarGeneralMenuButton(commandName, "ButtonUnknown", menuName, mi); knownButtons.put(what, but); } but.setIcon(icon, fileName); commandToIconMap.put(what, fileName); commandsTree.updateUI(); } } /** Build tree of menu commands */ private void buildCommandsTree() { DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(); menuEntries = new HashMap<String,EMenuItem>(); TopLevel top = TopLevel.getCurrentJFrame(); if (top == null || top.getEMenuBar() == null) return; // convert menuBar to tree for (EMenuItem menu: top.getEMenuBar().getItems()) { DefaultMutableTreeNode menuNode = new DefaultMutableTreeNode(new MenuTreeNode(menu, menu.getText())); rootNode.add(menuNode); addMenu(menuNode, (EMenu)menu, menu.getText()); } EMenu hiddenMenu = top.getEMenuBar().getHiddenMenu(); if (hiddenMenu != null) { DefaultMutableTreeNode menuNode = new DefaultMutableTreeNode(new MenuTreeNode(hiddenMenu, hiddenMenu.getText())); rootNode.add(menuNode); addMenu(menuNode, hiddenMenu, hiddenMenu.getText()); } commandsTree.setModel(new DefaultTreeModel(rootNode)); commandsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); commandsTree.setRootVisible(false); commandsTree.setShowsRootHandles(true); commandsTree.addTreeSelectionListener(this); } /** Adds menu items to parentNode, which represents Menu menu. */ private void addMenu(DefaultMutableTreeNode parentNode, EMenu menu, String menuName) { for (EMenuItem menuItem : menu.getItems()) { String menuItemName = menuName + ":" + menuItem.getText(); DefaultMutableTreeNode menuItemNode = new DefaultMutableTreeNode(new MenuTreeNode(menuItem, menuItemName)); parentNode.add(menuItemNode); if (menuItem instanceof EMenu) { addMenu(menuItemNode, (EMenu)menuItem, menuItemName); } else { menuEntries.put(menuItemName, menuItem); } } } /** * Class to encapsulate a tree node for displaying icons bound to commands. * The toString() method is overridden to show the key binding next to the * command name. This class encapsulates both JMenuItem and Menu, note * that both extend JMenuItem. */ private class MenuTreeNode { private EMenuItem menuItem; private String menuName; MenuTreeNode(EMenuItem menuItem, String menuName) { this.menuItem = menuItem; this.menuName = menuName; } public EMenuItem getMenuItem() { return menuItem; } /** * Convert to String to show on dialog tree */ public String toString() { if (menuItem != EMenuItem.SEPARATOR) { StringBuffer buf = new StringBuffer(menuItem.getDescription()); return buf.toString(); } return "---------------"; // separator } } private class MyRenderer extends DefaultTreeCellRenderer { public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); if (!(value instanceof DefaultMutableTreeNode)) return this; DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; if (node.getChildCount() > 0) { setIcon(menuIcon); } else { Object nodeInfo = node.getUserObject(); if (nodeInfo instanceof MenuTreeNode) { MenuTreeNode mtn = (MenuTreeNode)nodeInfo; if (mtn.menuItem == EMenuItem.SEPARATOR) { setIcon(null); } else { EToolBarButton but = knownButtons.get(mtn.menuName); if (but != null) setIcon(but.getIcon()); else { setIcon(ToolBar.getUnknownIcon()); } } } } return this; } } private class CommandTree extends JTree implements DragSourceListener { CommandTree() { setTransferHandler(new MyTransferHandler(getTransferHandler())); setDragEnabled(true); } public void dragEnter(DragSourceDragEvent e) {} public void dragOver(DragSourceDragEvent e) {} public void dragExit(DragSourceEvent e) {} public void dragDropEnd(DragSourceDropEvent e) {} public void dropActionChanged (DragSourceDragEvent e) {} /** * Class to start drag of a command in the tree. */ private class MyTransferHandler extends TransferHandler { private TransferHandler real; MyTransferHandler(TransferHandler real) { this.real = real; } protected Transferable createTransferable(JComponent c) { if (currentTreeNode == null) return null; Transferable transferable = new StringSelection("TOOLBARBUTTON " + currentTreeNode.menuName); return transferable; } public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) { return real.canImport(comp, transferFlavors); } public void exportAsDrag(JComponent comp, InputEvent e, int action) { real.exportAsDrag(comp, e, action); } protected void exportDone(JComponent source, Transferable data, int action) { } public void exportToClipboard(JComponent comp, Clipboard clip, int action) { real.exportToClipboard(comp, clip, action); } public int getSourceActions(JComponent c) { return real.getSourceActions(c); } public Icon getVisualRepresentation(Transferable t) { return real.getVisualRepresentation(t); } public boolean importData(JComponent comp, Transferable t) { return real.importData(comp, t); } } } /** * Class for defining toolbar entries that are draggable and droppable. */ private class DraggableToolbarEntry extends JLabel implements DragGestureListener, DragSourceListener { private DragSource dragSource; private int editIndex; private EToolBarButton toolbarButton; public DraggableToolbarEntry(int editIndex, EToolBarButton toolbarButton) { this.editIndex = editIndex; this.toolbarButton = toolbarButton; ImageIcon icon = (toolbarButton == null) ? separatorButton : toolbarButton.getIcon(); int width = icon.getIconWidth(); int height = icon.getIconHeight(); BufferedImage originalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = originalImage.getGraphics(); g.drawImage(icon.getImage(), 0, 0, null); Raster originalData = originalImage.getData(); int border = 4; BufferedImage biggerImage = new BufferedImage(width+border*2, height+border*2, BufferedImage.TYPE_INT_ARGB); WritableRaster biggerRaster = biggerImage.getRaster(); int [] sArray = new int[4]; for(int y=0; y<height; y++) { for(int x=0; x<width; x++) { originalData.getPixel(x, y, sArray); biggerRaster.setPixel(x+border, y+border, sArray); } } setIcon(new ImageIcon(biggerImage)); if (toolbarButton != null) setToolTipText(toolbarButton.getText()); dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, this); ToolBarDropTarget dropTarget = new ToolBarDropTarget(); new DropTarget(this, DnDConstants.ACTION_MOVE, dropTarget, true); } public void setIndex(int i) { editIndex = i; } public EToolBarButton getToolbarButton() { return toolbarButton; } public void dragGestureRecognized(DragGestureEvent e) { // make the Transferable Object Transferable transferable = new StringSelection("TOOLBARBUTTON #" + editIndex); // begin the drag dragSource.startDrag(e, DragSource.DefaultMoveDrop, transferable, this); } public void dragEnter(DragSourceDragEvent e) {} public void dragOver(DragSourceDragEvent e) {} public void dragExit(DragSourceEvent e) {} public void dragDropEnd(DragSourceDropEvent e) {} public void dropActionChanged (DragSourceDragEvent e) {} /** * Class for catching drags onto the toolbar. * These drags come from elsewhere in the toolbar (for rearrangement) * or from the toolbar preferences panel. */ private class ToolBarDropTarget implements DropTargetListener { private JLabel lastButtonDrawn = null; public void dragEnter(DropTargetDragEvent e) { dragAction(e); } public void dragOver(DropTargetDragEvent e) { if (dragAction(e)) return; DropTarget dt = (DropTarget)e.getSource(); if (dt.getComponent() instanceof JLabel) { JLabel but = (JLabel)dt.getComponent(); // erase former highlighting eraseDragImage(dt); // highlight what the drag is over Graphics2D g2 = (Graphics2D)but.getGraphics(); g2.setColor(Color.RED); int x = 0; if (e.getLocation().x > but.getWidth()/2) x = but.getWidth()-2; g2.drawRect(x, 0, 1, but.getHeight()); lastButtonDrawn = but; } } public void dropActionChanged(DropTargetDragEvent e) { dragAction(e); } public void dragExit(DropTargetEvent e) { eraseDragImage((DropTarget)e.getSource()); } public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(DnDConstants.ACTION_MOVE); // erase former highlighting eraseDragImage((DropTarget)dtde.getSource()); // get the files that were dragged String droppedButton = getDraggedObject(dtde.getTransferable(), dtde.getCurrentDataFlavors()); if (droppedButton != null) { // see what the drop is over DropTarget dt = (DropTarget)dtde.getSource(); Component but = dt.getComponent(); if (but != null) { // find index in menu int index = -1; for(int i=0; i<but.getParent().getComponentCount(); i++) if (but.getParent().getComponent(i) == but) { index = i; break; } boolean before = true; if (dtde.getLocation().x > but.getWidth()/2) before = false; if (index >= 0) droppedAt(index, droppedButton, before); dtde.dropComplete(true); return; } } dtde.dropComplete(false); } /** * Method to analyze the drag. * @param e * @return true if the drag is to be rejected. */ private boolean dragAction(DropTargetDragEvent e) { String droppedButton = getDraggedObject(e.getTransferable(), e.getCurrentDataFlavors()); if (droppedButton != null) { e.acceptDrag(e.getDropAction()); return false; } e.rejectDrag(); return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -