KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > DesignerMenuBar


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.view;
5
6 import java.awt.KeyboardFocusManager JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.awt.event.KeyEvent JavaDoc;
10 import java.beans.PropertyChangeEvent JavaDoc;
11 import java.beans.PropertyChangeListener JavaDoc;
12 import java.util.Observable JavaDoc;
13 import java.util.Observer JavaDoc;
14
15 import javax.swing.Action JavaDoc;
16 import javax.swing.JComponent JavaDoc;
17 import javax.swing.JMenu JavaDoc;
18 import javax.swing.JMenuBar JavaDoc;
19 import javax.swing.JMenuItem JavaDoc;
20 import javax.swing.JPopupMenu JavaDoc;
21 import javax.swing.JSeparator JavaDoc;
22 import javax.swing.KeyStroke JavaDoc;
23 import javax.swing.TransferHandler JavaDoc;
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 /**
36  * A menu bar for OddjobDesigner. The menu bar tracks selection changes in the
37  * model and enables/disables menus appropriately.
38  *
39  * @author Rob Gordon
40  */

41
42 public class DesignerMenuBar extends JMenuBar JavaDoc implements Observer JavaDoc {
43
44     /** The file menu */
45     private JMenu JavaDoc fileMenu;
46
47     /** The design menu */
48     private JMenu JavaDoc design;
49
50     /** The popup menu */
51     private JPopupMenu JavaDoc popupMenu;
52
53     /** The model */
54     private DesignerModel model;
55
56     /**
57      * Constructor.
58      *
59      */

60     public DesignerMenuBar() {
61         fileMenu = new JMenu JavaDoc("File");
62         fileMenu.setMnemonic(KeyEvent.VK_F);
63         add(fileMenu);
64
65         JMenuItem JavaDoc menuItem = null;
66         JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
67         JMenu JavaDoc editMenu = new JMenu JavaDoc("Edit");
68         editMenu.setMnemonic(KeyEvent.VK_E);
69         TransferActionListener actionListener = new TransferActionListener();
70
71         menuItem = new JMenuItem JavaDoc("Cut");
72         menuItem.setActionCommand((String JavaDoc) 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 JavaDoc("Copy");
80         menuItem.setActionCommand((String JavaDoc) 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 JavaDoc("Paste");
88         menuItem.setActionCommand((String JavaDoc) 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     /**
100      * Get the file menu. This can be used to add to the file menu.
101      *
102      * @return The file menu.
103      */

104     public JMenu JavaDoc getFileMenu() {
105         return fileMenu;
106     }
107
108     /**
109      * Change the design menu.
110      *
111      * @param design
112      * The menu.
113      */

114     void setDesign(JMenu JavaDoc 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     /*
127      * (non-Javadoc)
128      *
129      * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
130      */

131     public void update(Observable JavaDoc o, Object JavaDoc 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 JavaDoc menu = new JMenu JavaDoc("Design");
144         JMenu JavaDoc popup = new JMenu JavaDoc();
145
146         SimpleHierarchy componentActions = dc.availableActions();
147         FormFactory.createComponentMenu(menu, componentActions);
148         FormFactory.createComponentMenu(popup, componentActions);
149
150         if (node.getParent() != null) {
151             Action JavaDoc delete = new DeleteAction(
152                     (StructuralDesignComponent) ((DesignTreeNode) node
153                             .getParent()).getDesignComponent(), dc);
154             menu.add(new JMenuItem JavaDoc(delete));
155             popup.add(new JMenuItem JavaDoc(delete));
156         }
157
158         if (node.getParent() != null) {
159             menu.add(new JSeparator JavaDoc());
160             if (node.getDesignComponent() instanceof UnknownDC) {
161                 Action JavaDoc a = new ViewComponentsAction(model);
162                 menu.add(new JMenuItem JavaDoc(a));
163                 popup.add(new JMenuItem JavaDoc(a));
164             } else {
165                 Action JavaDoc a = new ViewXMLAction(model);
166                 menu.add(new JMenuItem JavaDoc(a));
167                 popup.add(new JMenuItem JavaDoc(a));
168             }
169         }
170         popupMenu = popup.getPopupMenu();
171         setDesign(menu);
172     }
173
174     /**
175      * Set the DesignerModel.
176      *
177      * @param model The new model. May be null.
178      */

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 JavaDoc getPopupMenu() {
191         return popupMenu;
192     }
193
194     public class TransferActionListener implements ActionListener JavaDoc,
195             PropertyChangeListener JavaDoc {
196         private JComponent JavaDoc focusOwner = null;
197
198         public TransferActionListener() {
199             KeyboardFocusManager JavaDoc manager = KeyboardFocusManager
200                     .getCurrentKeyboardFocusManager();
201             manager.addPropertyChangeListener("permanentFocusOwner", this);
202         }
203
204         public void propertyChange(PropertyChangeEvent JavaDoc e) {
205             Object JavaDoc o = e.getNewValue();
206             if (o instanceof JComponent JavaDoc) {
207                 focusOwner = (JComponent JavaDoc) o;
208             } else {
209                 focusOwner = null;
210             }
211         }
212
213         public void actionPerformed(ActionEvent JavaDoc e) {
214             if (focusOwner == null)
215                 return;
216             String JavaDoc action = (String JavaDoc) e.getActionCommand();
217             Action JavaDoc a = focusOwner.getActionMap().get(action);
218             if (a != null) {
219                 a.actionPerformed(new ActionEvent JavaDoc(focusOwner,
220                         ActionEvent.ACTION_PERFORMED, null));
221             }
222         }
223     }
224 }
225
Popular Tags