/** * Find the MWPane component to act upon. * * @param component Component. * @return MWPane component to act upon. */ protected final MWPane getMWPane(Component component) { if (component == null) { return null; } if (component instanceof MWPane) { return (MWPane) component; } if (component instanceof JMenuItem) { JMenuItem menuItem = (JMenuItem) component; return getMWPane(menuItem.getParent()); } if (component instanceof JPopupMenu) { JPopupMenu popup = (JPopupMenu) component; return getMWPane(popup.getInvoker()); } return null; }
/** * This method is not being used to paint menu item since 6.0 This code left for compatibility * only. Do not use or override it, this will not cause any visible effect. */ public static void paintMenuItem( Graphics g, JComponent c, Icon checkIcon, Icon arrowIcon, Color background, Color foreground, int defaultTextIconGap) { JMenuItem b = (JMenuItem) c; ButtonModel model = b.getModel(); Dimension size = b.getSize(); Insets i = c.getInsets(); Rectangle viewRect = new Rectangle(size); viewRect.x += i.left; viewRect.y += i.top; viewRect.width -= (i.right + viewRect.x); viewRect.height -= (i.bottom + viewRect.y); Rectangle iconRect = new Rectangle(); Rectangle textRect = new Rectangle(); Rectangle acceleratorRect = new Rectangle(); Rectangle checkRect = new Rectangle(); Rectangle arrowRect = new Rectangle(); Font holdf = g.getFont(); Font f = c.getFont(); g.setFont(f); FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f); FontMetrics fmAccel = SwingUtilities2.getFontMetrics(c, g, UIManager.getFont("MenuItem.acceleratorFont")); if (c.isOpaque()) { if (model.isArmed() || (c instanceof JMenu && model.isSelected())) { g.setColor(background); } else { g.setColor(c.getBackground()); } g.fillRect(0, 0, size.width, size.height); } // get Accelerator text KeyStroke accelerator = b.getAccelerator(); String acceleratorText = ""; if (accelerator != null) { int modifiers = accelerator.getModifiers(); if (modifiers > 0) { acceleratorText = KeyEvent.getKeyModifiersText(modifiers); acceleratorText += "+"; } acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode()); } // layout the text and icon String text = layoutMenuItem( c, fm, b.getText(), fmAccel, acceleratorText, b.getIcon(), checkIcon, arrowIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkRect, arrowRect, b.getText() == null ? 0 : defaultTextIconGap, defaultTextIconGap); // Paint the Check Color holdc = g.getColor(); if (checkIcon != null) { if (model.isArmed() || (c instanceof JMenu && model.isSelected())) g.setColor(foreground); checkIcon.paintIcon(c, g, checkRect.x, checkRect.y); g.setColor(holdc); } // Paint the Icon if (b.getIcon() != null) { Icon icon; if (!model.isEnabled()) { icon = (Icon) b.getDisabledIcon(); } else if (model.isPressed() && model.isArmed()) { icon = (Icon) b.getPressedIcon(); if (icon == null) { // Use default icon icon = (Icon) b.getIcon(); } } else { icon = (Icon) b.getIcon(); } if (icon != null) { icon.paintIcon(c, g, iconRect.x, iconRect.y); } } // Draw the Text if (text != null && !text.equals("")) { // Once BasicHTML becomes public, use BasicHTML.propertyKey // instead of the hardcoded string below! View v = (View) c.getClientProperty("html"); if (v != null) { v.paint(g, textRect); } else { int mnemIndex = b.getDisplayedMnemonicIndex(); if (!model.isEnabled()) { // *** paint the text disabled g.setColor(b.getBackground().brighter()); SwingUtilities2.drawStringUnderlineCharAt( b, g, text, mnemIndex, textRect.x, textRect.y + fmAccel.getAscent()); g.setColor(b.getBackground().darker()); SwingUtilities2.drawStringUnderlineCharAt( b, g, text, mnemIndex, textRect.x - 1, textRect.y + fmAccel.getAscent() - 1); } else { // *** paint the text normally if (model.isArmed() || (c instanceof JMenu && model.isSelected())) { g.setColor(foreground); } else { g.setColor(b.getForeground()); } SwingUtilities2.drawStringUnderlineCharAt( b, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); } } } // Draw the Accelerator Text if (acceleratorText != null && !acceleratorText.equals("")) { // Get the maxAccWidth from the parent to calculate the offset. int accOffset = 0; Container parent = b.getParent(); if (parent != null && parent instanceof JComponent) { JComponent p = (JComponent) parent; Integer maxValueInt = (Integer) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH); int maxValue = maxValueInt != null ? maxValueInt.intValue() : acceleratorRect.width; // Calculate the offset, with which the accelerator texts will be drawn with. accOffset = maxValue - acceleratorRect.width; } g.setFont(UIManager.getFont("MenuItem.acceleratorFont")); if (!model.isEnabled()) { // *** paint the acceleratorText disabled g.setColor(b.getBackground().brighter()); SwingUtilities2.drawString( c, g, acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fm.getAscent()); g.setColor(b.getBackground().darker()); SwingUtilities2.drawString( c, g, acceleratorText, acceleratorRect.x - accOffset - 1, acceleratorRect.y + fm.getAscent() - 1); } else { // *** paint the acceleratorText normally if (model.isArmed() || (c instanceof JMenu && model.isSelected())) { g.setColor(foreground); } else { g.setColor(b.getForeground()); } SwingUtilities2.drawString( c, g, acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent()); } } // Paint the Arrow if (arrowIcon != null) { if (model.isArmed() || (c instanceof JMenu && model.isSelected())) g.setColor(foreground); if (!(b.getParent() instanceof JMenuBar)) arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y); } g.setColor(holdc); g.setFont(holdf); }
public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("new")) { icons = new Hashtable[0]; fields = new Hashtable[0]; tabPane.removeAll(); repaint(); } else if (cmd.equals("open")) { JFileChooser chooser = new JFileChooser(); FileFilter filter = new FileFilter() { public boolean accept(File f) { return f.getAbsolutePath().endsWith(".template") || f.isDirectory(); } public String getDescription() { return "OME Notes Templates"; } }; chooser.setFileFilter(filter); int status = chooser.showOpenDialog(this); if (status == JFileChooser.APPROVE_OPTION) { String file = chooser.getSelectedFile().getAbsolutePath(); try { Template t = new Template(file); TemplateTab[] tabs = t.getTabs(); for (int i = 0; i < tabs.length; i++) { int rows = tabs[i].getRows(); int cols = tabs[i].getColumns(); if (cols == 0) cols = 1; if (rows == 0) rows = 1; addTab(tabs[i].getName(), rows, cols); tabPane.setSelectedIndex(i); for (int j = 0; j < tabs[i].getNumFields(); j++) { TemplateField f = tabs[i].getField(j); int x = f.getRow(); int y = f.getColumn(); if (x == -1) x = 1; if (y == -1) y = j + 1; Point p = new Point(x, y); DraggableIcon icon = (DraggableIcon) icons[i].get(p); icon.label = new JLabel(f.getName()); JPanel panel = new JPanel(); panel.add(f.getComponent()); icon.setPanel(panel); } } } catch (Exception exc) { error("Failed to parse template", exc); } tabPane.setSelectedIndex(0); } } else if (cmd.equals("save")) { // build up the template from the components TemplateTab[] tabs = new TemplateTab[tabPane.getTabCount()]; for (int i = 0; i < tabs.length; i++) { tabs[i] = new TemplateTab(); tabs[i].setName(tabPane.getTitleAt(i)); JComponent c = (JComponent) tabPane.getComponentAt(i); FormLayout layout = (FormLayout) c.getLayout(); tabs[i].setRows(layout.getRowCount()); tabs[i].setColumns(layout.getColumnCount()); Object[] keys = icons[i].keySet().toArray(); for (int j = 0; j < keys.length; j++) { Point p = (Point) keys[j]; DraggableIcon icon = (DraggableIcon) icons[i].get(p); TemplateField f = (TemplateField) fields[i].get(p); if (icon.image != null) { Component[] components = icon.image.getComponents(); JLabel label = icon.label; JComponent component = (JComponent) components[0]; f.setComponent(component); for (int k = 0; k < COMPONENTS.length; k++) { if (component.getClass().equals(COMPONENTS[k])) { f.setType(COMPONENT_TYPES[k]); break; } } f.setRow(p.y); f.setColumn(p.x); f.setDefaultValue(TemplateTools.getComponentValue(component)); tabs[i].addField(f); } } } Template t = new Template(tabs, null); // prompt for filename to save to if (currentFile == null) { JFileChooser chooser = new JFileChooser(); FileFilter filter = new FileFilter() { public boolean accept(File f) { return true; } public String getDescription() { return "All files"; } }; chooser.setFileFilter(filter); int status = chooser.showSaveDialog(this); if (status == JFileChooser.APPROVE_OPTION) { currentFile = chooser.getSelectedFile().getAbsolutePath(); if (currentFile == null) return; } } try { t.save(currentFile); } catch (IOException io) { error("Failed to save template", io); } } else if (cmd.equals("quit")) dispose(); else if (cmd.equals("add row")) addRow(); else if (cmd.equals("add col")) addColumn(); else if (cmd.equals("prompt tab")) { // prompt for tab name JPopupMenu menu = new JPopupMenu(); newTabName = new JTextField(); newTabName.setPreferredSize(new Dimension(200, 25)); menu.add(newTabName); JButton b = new JButton("OK"); b.addActionListener(this); b.setActionCommand("new tab"); menu.add(b); JComponent s = (JComponent) e.getSource(); menu.show(s, s.getX(), s.getY()); newTabName.grabFocus(); } else if (cmd.equals("new tab")) { newTabName.getParent().setVisible(false); addTab(newTabName.getText(), 2, 2); } else if (cmd.equals("setName")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); String text = ((JTextField) menu.getComponents()[0]).getText(); Point p = new Point(icon.gridx, icon.gridy); int ndx = tabPane.getSelectedIndex(); TemplateField f = (TemplateField) fields[ndx].get(p); f.setName(text); f.setNameMap(null); // set the name if (icon.label != null) icon.remove(icon.label); icon.remove(icon.image); icon.label = new JLabel(text); icon.add(icon.label); icon.add(icon.image); icon.getParent().repaint(); } else if (cmd.equals("changeName")) { // prompt for new field name JPopupMenu menu = new JPopupMenu(); JTextField field = new JTextField(); field.setPreferredSize(new Dimension(200, 25)); menu.add(field); JButton b = new JButton("OK"); b.addActionListener(this); b.setActionCommand("setName"); menu.add(b); menu.show(lastMenuComponent, lastMenuX, lastMenuY); field.grabFocus(); } else if (cmd.equals("nameMap")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); MappingWindow w = new MappingWindow(this, true); w.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("map")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); MappingWindow w = new MappingWindow(this, false); w.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("repeat")) { JMenuItem item = (JMenuItem) e.getSource(); DraggableIcon icon = (DraggableIcon) ((JPopupMenu) item.getParent()).getInvoker(); TemplateField f = getField(icon); if (item.getText().equals("Repeat this field")) { item.setText("Don't repeat this field"); f.setRepeated(true); } else { item.setText("Repeat this field"); f.setRepeated(false); } } else if (cmd.equals("removeField")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); menu.setVisible(false); int idx = tabPane.getSelectedIndex(); Object[] keys = icons[idx].keySet().toArray(); for (int i = 0; i < keys.length; i++) { if (icons[idx].get(keys[i]).equals(icon)) { icons[idx].remove(keys[i]); fields[idx].remove(keys[i]); break; } } icon.remove(icon.label); icon.remove(icon.image); tabPane.repaint(); } else if (cmd.startsWith("removeRow")) { int row = Integer.parseInt(cmd.substring(cmd.indexOf(":") + 1)); JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); menu.setVisible(false); JPanel pane = (JPanel) tabPane.getSelectedComponent(); FormLayout layout = (FormLayout) pane.getLayout(); int rows = layout.getRowCount(); int cols = layout.getColumnCount(); int idx = tabPane.getSelectedIndex(); for (int i = 0; i < cols; i++) { pane.remove((JComponent) icons[idx].get(new Point(i + 1, row + 1))); } rekey(row, -1); layout.removeRow(row + 1); tabPane.repaint(); } else if (cmd.startsWith("removeColumn")) { int col = Integer.parseInt(cmd.substring(cmd.indexOf(":") + 1)); JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); menu.setVisible(false); JPanel pane = (JPanel) tabPane.getSelectedComponent(); FormLayout layout = (FormLayout) pane.getLayout(); int rows = layout.getRowCount(); int cols = layout.getColumnCount(); int idx = tabPane.getSelectedIndex(); for (int i = 0; i < rows; i++) { pane.remove((JComponent) icons[idx].get(new Point(col + 1, i + 1))); } rekey(-1, col); layout.removeColumn(col + 1); tabPane.repaint(); } else if (cmd.equals("removeTab")) { int ndx = tabPane.getSelectedIndex(); tabPane.remove(ndx); Hashtable[] h = new Hashtable[icons.length - 1]; Hashtable[] f = new Hashtable[fields.length - 1]; System.arraycopy(icons, 0, h, 0, ndx); System.arraycopy(icons, ndx + 1, h, ndx, h.length - ndx); System.arraycopy(fields, 0, f, 0, ndx); System.arraycopy(fields, ndx + 1, f, ndx, f.length - ndx); icons = h; fields = f; } else if (cmd.equals("specifyChoices")) { JMenuItem item = (JMenuItem) e.getSource(); DraggableIcon icon = (DraggableIcon) ((JPopupMenu) item.getParent()).getInvoker(); TemplateField f = getField(icon); EnumWindow w = new EnumWindow(this, f.getEnums()); w.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("setThumbSource")) { JPopupMenu menu = new JPopupMenu(); ButtonGroup g = new ButtonGroup(); JRadioButton dataset = new JRadioButton("Use thumbnail from dataset"); dataset.setSelected(true); g.add(dataset); JRadioButton file = new JRadioButton("Use thumbnail from file:"); g.add(file); menu.add(dataset); menu.add(file); JTextField field = new JTextField(); field.setPreferredSize(new Dimension(200, 25)); menu.add(field); JButton b = new JButton("OK"); b.addActionListener(this); b.setActionCommand("applyThumbSource"); menu.add(b); menu.show(lastMenuComponent, lastMenuX, lastMenuY); } else if (cmd.equals("applyThumbSource")) { JPopupMenu menu = (JPopupMenu) ((JComponent) e.getSource()).getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); Component[] c = menu.getComponents(); JRadioButton dataset = (JRadioButton) c[0]; String text = null; if (!dataset.isSelected()) { JTextField t = (JTextField) c[2]; text = t.getText(); getField(icon).setValueMap(text); } menu.setVisible(false); if (text != null) { try { BufferedImageReader reader = new BufferedImageReader(); reader.setId(text); BufferedImage thumb = reader.openThumbImage(0); JLabel label = (JLabel) icon.image.getComponents()[0]; label.setIcon(new ImageIcon(thumb)); reader.close(); } catch (FormatException exc) { error("Failed to open thumbnail (" + text + ")", exc); } catch (IOException exc) { error("Failed to open thumbnail (" + text + ")", exc); } } } else if (cmd.equals("ok")) { // this event came from an instance of EnumWindow JPanel parent = (JPanel) ((JButton) e.getSource()).getParent(); EnumWindow menu = (EnumWindow) parent.getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); TemplateField f = getField(icon); menu.setVisible(false); String[] options = menu.getOptions(); f.setEnums(options); JComboBox box = (JComboBox) icon.image.getComponents()[0]; for (int i = 0; i < options.length; i++) box.addItem(options[i]); repaint(); } else if (cmd.equals("chooseMapping")) { // this event came from an instance of MappingWindow JTabbedPane parent = (JTabbedPane) ((JButton) e.getSource()).getParent(); MappingWindow menu = (MappingWindow) parent.getParent(); DraggableIcon icon = (DraggableIcon) menu.getInvoker(); TemplateField f = getField(icon); String omexmlMap = null; if (menu.nameMap) f.setNameMap(omexmlMap); else f.setValueMap(omexmlMap); menu.setVisible(false); } }
@Override protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) { JMenu menu = (JMenu) menuItem; ButtonModel buttonmodel = menu.getModel(); if (menu.isTopLevelMenu()) { this.selectionForeground = Colors.getBlack(); } else { this.selectionForeground = Colors.getWhite(); } int w = menu.getWidth(); int h = menu.getHeight(); Color oldColor = g.getColor(); if (!menu.isContentAreaFilled() || !menu.isOpaque()) { // do nothing } else { if (menu.isTopLevelMenu()) { if (buttonmodel.isSelected()) { CashedPainter.drawMenuBackground(menuItem, g, 0, 0, w, h); } else if (buttonmodel.isRollover() && buttonmodel.isEnabled()) { g.setColor(new ColorUIResource(252, 252, 252)); g.fillRect(1, 1, w - 2, h - 2); g.setColor(RapidLookTools.getColors().getToolbarButtonColors()[0]); g.drawLine(2, 0, w - 3, 0); g.drawLine(2, h - 1, w - 3, h - 1); g.drawLine(0, 2, 0, h - 3); g.drawLine(w - 1, 2, w - 1, h - 3); g.drawLine(1, 1, 1, 1); g.drawLine(w - 2, 1, w - 2, 1); g.drawLine(1, h - 2, 1, h - 2); g.drawLine(w - 2, h - 2, w - 2, h - 2); g.setColor(RapidLookTools.getColors().getToolbarButtonColors()[8]); g.drawLine(0, 1, 1, 0); g.drawLine(w - 1, 1, w - 2, 0); g.drawLine(1, h - 1, 0, h - 2); g.drawLine(w - 1, h - 2, w - 2, h - 1); g.setColor(RapidLookTools.getColors().getToolbarButtonColors()[7]); g.drawLine(1, 2, 2, 1); g.drawLine(w - 2, 2, w - 3, 1); g.setColor(RapidLookTools.getColors().getToolbarButtonColors()[6]); g.drawLine(2, h - 2, 1, h - 3); g.drawLine(w - 2, h - 3, w - 3, h - 2); g.setColor(new ColorUIResource(252, 252, 252)); g.drawLine(0, 0, 0, 0); g.drawLine(w - 1, 0, w - 1, 0); g.setColor(new ColorUIResource(232, 232, 232)); g.drawLine(0, h - 1, 0, h - 1); g.drawLine(w - 1, h - 1, w - 1, h - 1); } else { if (menuItem.getParent() instanceof JMenuBar) { ((MenuBarUI) ((JMenuBar) menuItem.getParent()).getUI()).update(g, menuItem); } } } else { if (!menuItem.getModel().isSelected()) { CashedPainter.drawMenuItemFading(menuItem, g); } else { RapidLookTools.drawMenuItemBackground(g, menuItem); } } } g.setColor(oldColor); }