1 4 package org.oddjob.designer.view; 5 6 import java.awt.KeyboardFocusManager ; 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.ActionListener ; 9 import java.awt.event.KeyEvent ; 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 import java.util.Observable ; 13 import java.util.Observer ; 14 15 import javax.swing.Action ; 16 import javax.swing.JComponent ; 17 import javax.swing.JMenu ; 18 import javax.swing.JMenuBar ; 19 import javax.swing.JMenuItem ; 20 import javax.swing.JPopupMenu ; 21 import javax.swing.JSeparator ; 22 import javax.swing.KeyStroke ; 23 import javax.swing.TransferHandler ; 24 25 import org.oddjob.designer.actions.DeleteAction; 26 import org.oddjob.designer.actions.ViewComponentsAction; 27 import org.oddjob.designer.actions.ViewXMLAction; 28 import org.oddjob.designer.components.UnknownDC; 29 import org.oddjob.designer.factory.SimpleHierarchy; 30 import org.oddjob.designer.model.DesignComponent; 31 import org.oddjob.designer.model.DesignTreeNode; 32 import org.oddjob.designer.model.DesignerModel; 33 import org.oddjob.designer.model.StructuralDesignComponent; 34 35 41 42 public class DesignerMenuBar extends JMenuBar implements Observer { 43 44 45 private JMenu fileMenu; 46 47 48 private JMenu design; 49 50 51 private JPopupMenu popupMenu; 52 53 54 private DesignerModel model; 55 56 60 public DesignerMenuBar() { 61 fileMenu = new JMenu ("File"); 62 fileMenu.setMnemonic(KeyEvent.VK_F); 63 add(fileMenu); 64 65 JMenuItem menuItem = null; 66 JMenuBar menuBar = new JMenuBar (); 67 JMenu editMenu = new JMenu ("Edit"); 68 editMenu.setMnemonic(KeyEvent.VK_E); 69 TransferActionListener actionListener = new TransferActionListener(); 70 71 menuItem = new JMenuItem ("Cut"); 72 menuItem.setActionCommand((String ) TransferHandler.getCutAction() 73 .getValue(Action.NAME)); 74 menuItem.addActionListener(actionListener); 75 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, 76 ActionEvent.CTRL_MASK)); 77 menuItem.setMnemonic(KeyEvent.VK_T); 78 editMenu.add(menuItem); 79 menuItem = new JMenuItem ("Copy"); 80 menuItem.setActionCommand((String ) TransferHandler.getCopyAction() 81 .getValue(Action.NAME)); 82 menuItem.addActionListener(actionListener); 83 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 84 ActionEvent.CTRL_MASK)); 85 menuItem.setMnemonic(KeyEvent.VK_C); 86 editMenu.add(menuItem); 87 menuItem = new JMenuItem ("Paste"); 88 menuItem.setActionCommand((String ) TransferHandler.getPasteAction() 89 .getValue(Action.NAME)); 90 menuItem.addActionListener(actionListener); 91 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 92 ActionEvent.CTRL_MASK)); 93 menuItem.setMnemonic(KeyEvent.VK_P); 94 editMenu.add(menuItem); 95 96 add(editMenu); 97 } 98 99 104 public JMenu getFileMenu() { 105 return fileMenu; 106 } 107 108 114 void setDesign(JMenu design) { 115 if (this.design != null) { 116 remove(this.design); 117 } 118 this.design = design; 119 if (design != null) { 120 add(design); 121 } 122 validate(); 123 repaint(); 124 } 125 126 131 public void update(Observable o, Object arg) { 132 DesignerModel model = (DesignerModel) o; 133 DesignTreeNode node = model.getCurrentSelection(); 134 135 if (node == null) { 136 setDesign(null); 137 popupMenu = null; 138 return; 139 } 140 141 DesignComponent dc = node.getDesignComponent(); 142 143 JMenu menu = new JMenu ("Design"); 144 JMenu popup = new JMenu (); 145 146 SimpleHierarchy componentActions = dc.availableActions(); 147 FormFactory.createComponentMenu(menu, componentActions); 148 FormFactory.createComponentMenu(popup, componentActions); 149 150 if (node.getParent() != null) { 151 Action delete = new DeleteAction( 152 (StructuralDesignComponent) ((DesignTreeNode) node 153 .getParent()).getDesignComponent(), dc); 154 menu.add(new JMenuItem (delete)); 155 popup.add(new JMenuItem (delete)); 156 } 157 158 if (node.getParent() != null) { 159 menu.add(new JSeparator ()); 160 if (node.getDesignComponent() instanceof UnknownDC) { 161 Action a = new ViewComponentsAction(model); 162 menu.add(new JMenuItem (a)); 163 popup.add(new JMenuItem (a)); 164 } else { 165 Action a = new ViewXMLAction(model); 166 menu.add(new JMenuItem (a)); 167 popup.add(new JMenuItem (a)); 168 } 169 } 170 popupMenu = popup.getPopupMenu(); 171 setDesign(menu); 172 } 173 174 179 public void setDesignerModel(DesignerModel model) { 180 if (this.model != null) { 181 this.model.deleteObserver(this); 182 } 183 this.model = model; 184 setDesign(null); 185 if (model != null) { 186 model.addObserver(this); 187 } 188 } 189 190 public JPopupMenu getPopupMenu() { 191 return popupMenu; 192 } 193 194 public class TransferActionListener implements ActionListener , 195 PropertyChangeListener { 196 private JComponent focusOwner = null; 197 198 public TransferActionListener() { 199 KeyboardFocusManager manager = KeyboardFocusManager 200 .getCurrentKeyboardFocusManager(); 201 manager.addPropertyChangeListener("permanentFocusOwner", this); 202 } 203 204 public void propertyChange(PropertyChangeEvent e) { 205 Object o = e.getNewValue(); 206 if (o instanceof JComponent ) { 207 focusOwner = (JComponent ) o; 208 } else { 209 focusOwner = null; 210 } 211 } 212 213 public void actionPerformed(ActionEvent e) { 214 if (focusOwner == null) 215 return; 216 String action = (String ) e.getActionCommand(); 217 Action a = focusOwner.getActionMap().get(action); 218 if (a != null) { 219 a.actionPerformed(new ActionEvent (focusOwner, 220 ActionEvent.ACTION_PERFORMED, null)); 221 } 222 } 223 } 224 } 225 | Popular Tags |