KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > client > grapheditor > WFMenuBar


1 package hero.client.grapheditor;
2
3 /**
4  * WFMenuBar.java
5  *
6  *
7  * Created: Fri Aug 09 16:01:06 2002
8  *
9  * @version 1.0
10  */

11
12 import java.awt.Color JavaDoc;
13 import java.awt.FileDialog JavaDoc;
14 import java.awt.event.ActionEvent JavaDoc;
15 import java.awt.event.ActionListener JavaDoc;
16 import java.awt.event.KeyEvent JavaDoc;
17 import java.awt.image.BufferedImage JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import javax.imageio.ImageIO JavaDoc;
23 import javax.swing.AbstractAction JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JCheckBoxMenuItem JavaDoc;
26 import javax.swing.JMenu JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import javax.swing.JOptionPane JavaDoc;
29 import javax.swing.KeyStroke JavaDoc;
30
31 public class WFMenuBar extends javax.swing.JMenuBar JavaDoc {
32 static java.util.ResourceBundle JavaDoc resource = java.util.ResourceBundle.getBundle("resources.Traduction")/*#BundleType=List*/;
33     Frame frame;
34
35     public WFMenuBar(final Frame frame, final WFManager manager) {
36         this.frame = frame;
37         boolean buttonState = manager.getPersistence().hasPermission();
38         ClassLoader JavaDoc cl = frame.getClass().getClassLoader();
39
40         JMenu JavaDoc menu;
41         JMenuItem JavaDoc mi;
42
43         menu = new JMenu JavaDoc(resource.getString("wfmenubar.file")); // File
44
menu.setBackground(new Color JavaDoc(153, 153, 255));
45         add(menu);
46
47         // New
48
mi = new JMenuItem JavaDoc(new ImageIcon JavaDoc(cl.getResource("images/new.png")));
49         mi.setText(resource.getString("wfmenubar.new"));
50         mi.setBackground(new Color JavaDoc(153, 153, 255));
51         mi.setAccelerator(
52             KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
53         mi.addActionListener(new ActionListener JavaDoc() {
54             public void actionPerformed(ActionEvent JavaDoc e) {
55                 manager.newProject();
56             }
57         });
58         menu.add(mi);
59
60         // Clone
61
mi = new JMenuItem JavaDoc(new ImageIcon JavaDoc(cl.getResource("images/clone.gif")));
62         mi.setText(resource.getString("wfmenubar.clone"));
63         mi.setBackground(new Color JavaDoc(153, 153, 255));
64         mi.setAccelerator(
65             KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
66         mi.addActionListener(new ActionListener JavaDoc() {
67             public void actionPerformed(ActionEvent JavaDoc e) {
68                 manager.cloneProject();
69             }
70         });
71         menu.add(mi);
72
73         // Open
74
mi = new JMenuItem JavaDoc(new ImageIcon JavaDoc(cl.getResource("images/open.png")));
75         mi.setText(resource.getString("wfmenubar.open"));
76         mi.setBackground(new Color JavaDoc(153, 153, 255));
77         mi.setAccelerator(
78             KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
79         mi.addActionListener(new ActionListener JavaDoc() {
80             public void actionPerformed(ActionEvent JavaDoc e) {
81                 manager.openProject();
82             }
83         });
84         menu.add(mi);
85
86         menu.addSeparator();
87
88         JMenu JavaDoc export = new JMenu JavaDoc(resource.getString("wfmenubar.export"));
89         export.setBackground(new Color JavaDoc(153, 153, 255));
90         export.setIcon(new ImageIcon JavaDoc(cl.getResource("images/export.gif")));
91         // ImageIO Writers
92
AbstractAction JavaDoc a;
93
94         String JavaDoc[] s = javax.imageio.ImageIO.getWriterFormatNames();
95         for (int i = 0; i < s.length; i++) {
96             final String JavaDoc format = s[i];
97             if (format.toUpperCase().equals(format)) {
98                 a = new AbstractAction JavaDoc(format) {
99                     public void actionPerformed(ActionEvent JavaDoc e) {
100                         String JavaDoc name =
101                             saveDialog(resource.getString("wfmenubar.saveas") + format.toUpperCase());
102                         if (manager.getGraph().getModel().getRootCount() > 0) {
103                             if (name != null) {
104                                 try {
105                                     BufferedImage JavaDoc im = manager.getGraphImage();
106                                     if (im != null) {
107                                         File JavaDoc f = new File JavaDoc(name);
108                                         ImageIO.write(im, format, f);
109                                         JOptionPane.showMessageDialog(
110                                             null,
111                                             resource.getString("wfmenubar.successexport"));
112                                     } else
113                                         error("");
114                                 } catch (IOException JavaDoc ex) {
115                                     error(ex.toString());
116                                 }
117                             }
118                         } else
119                             error("");
120                     }
121                 };
122                 JMenuItem JavaDoc imageFormat = new JMenuItem JavaDoc(a);
123                 imageFormat.setBackground(new Color JavaDoc(153, 153, 255));
124                 export.add(imageFormat);
125             }
126         }
127
128         menu.addSeparator();
129
130         AbstractAction JavaDoc b = new AbstractAction JavaDoc("VRML") {
131             public void actionPerformed(ActionEvent JavaDoc e) {
132                 String JavaDoc name = saveDialog(resource.getString("wfmenubar.saveasvrm"));
133                 if (manager.getGraph().getModel().getRootCount() > 0) {
134                     if (name != null) {
135                         try {
136                             String JavaDoc data = manager.getGraphVRML();
137                             if (data != null) {
138                                 File JavaDoc f = new File JavaDoc(name + ".wrl");
139                                 FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(f);
140                                 fout.write(data.getBytes());
141                                 fout.close();
142                                 JOptionPane.showMessageDialog(
143                                     null,
144                                     resource.getString("wfmenubar.successexport"));
145                             } else
146                                 error("");
147                         } catch (IOException JavaDoc ex) {
148                             error(ex.toString());
149                         }
150                     }
151                 } else
152                     error("");
153             }
154         };
155         JMenuItem JavaDoc vrmlFormat = new JMenuItem JavaDoc(b);
156         vrmlFormat.setBackground(new Color JavaDoc(153, 153, 255));
157         export.add(vrmlFormat);
158
159         menu.add(export);
160
161         menu.addSeparator();
162
163
164         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.exit")); // - Exit
165
mi.setBackground(new Color JavaDoc(153, 153, 255));
166         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/exit.png")));
167         mi.addActionListener(new ActionListener JavaDoc() {
168             public void actionPerformed(ActionEvent JavaDoc e) {
169                 manager.exit();
170             }
171         });
172         menu.add(mi);
173
174         menu = new JMenu JavaDoc(resource.getString("wfmenubar.edit")); //Edit
175
menu.setBackground(new Color JavaDoc(153, 153, 255));
176         add(menu);
177
178         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.copy")); // - Copy
179
mi.setBackground(new Color JavaDoc(153, 153, 255));
180         // mi.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C, ActionEvent.CTRL_MASK));
181
mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Copy.gif")));
182         mi.setEnabled(buttonState);
183         mi.addActionListener(new ActionListener JavaDoc() {
184             public void actionPerformed(ActionEvent JavaDoc e) {
185                 manager.copySelection();
186             }
187         });
188         menu.add(mi);
189         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.paste")); // - Paste
190
mi.setBackground(new Color JavaDoc(153, 153, 255));
191         // mi.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_V, ActionEvent.CTRL_MASK));
192
mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Paste.gif")));
193         mi.setEnabled(buttonState);
194         mi.addActionListener(new ActionListener JavaDoc() {
195             public void actionPerformed(ActionEvent JavaDoc e) {
196                 manager.pasteSelection();
197             }
198         });
199         menu.add(mi);
200
201         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.delete")); // - Delete
202
mi.setBackground(new Color JavaDoc(153, 153, 255));
203         mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
204         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Delete.gif")));
205         mi.setEnabled(buttonState);
206         mi.addActionListener(new ActionListener JavaDoc() {
207             public void actionPerformed(ActionEvent JavaDoc e) {
208                 manager.deleteSelection();
209             }
210         });
211         menu.add(mi);
212
213         menu.addSeparator();
214
215         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.selectall"), 'A'); // - Select all
216
mi.setBackground(new Color JavaDoc(153, 153, 255));
217         mi.setAccelerator(
218             KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
219         mi.addActionListener(new ActionListener JavaDoc() {
220             public void actionPerformed(ActionEvent JavaDoc e) {
221                 manager.selectAll();
222             }
223         });
224         menu.add(mi);
225
226         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.deselectall"), 'D'); // - Deselect all
227
mi.setBackground(new Color JavaDoc(153, 153, 255));
228
229         mi.setAccelerator(
230             KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
231         mi.addActionListener(new ActionListener JavaDoc() {
232             public void actionPerformed(ActionEvent JavaDoc e) {
233                 manager.clearSelection();
234             }
235         });
236         menu.add(mi);
237
238         menu = new JMenu JavaDoc(resource.getString("wfmenubar.format")); //Format
239
menu.setBackground(new Color JavaDoc(153, 153, 255));
240         add(menu);
241
242         final JCheckBoxMenuItem JavaDoc menuItem =
243             new JCheckBoxMenuItem JavaDoc(resource.getString("wfmenubar.autolay"));
244         menu.add(menuItem);
245         menu.setBackground(new Color JavaDoc(153, 153, 255));
246         menuItem.setState(true);
247         menuItem.setBackground(new Color JavaDoc(153, 153, 255));
248         menuItem.setAccelerator(
249             KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
250         menuItem.addActionListener(new ActionListener JavaDoc() {
251             public void actionPerformed(ActionEvent JavaDoc e) {
252                 manager.automaticLayout();
253             }
254         });
255         menu.add(menuItem);
256
257         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.circlelay"));
258         mi.setBackground(new Color JavaDoc(153, 153, 255));
259         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Arrange.gif")));
260         mi.addActionListener(new ActionListener JavaDoc() {
261             public void actionPerformed(ActionEvent JavaDoc e) {
262                 manager.setLayout1();
263             }
264         });
265
266         menu.add(mi);
267
268         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.expandlay"));
269         mi.setBackground(new Color JavaDoc(153, 153, 255));
270         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Layout.gif")));
271         mi.addActionListener(new ActionListener JavaDoc() {
272             public void actionPerformed(ActionEvent JavaDoc e) {
273                 manager.setLayout2();
274             }
275         });
276
277         menu.add(mi);
278
279         menu = new JMenu JavaDoc(resource.getString("wfmenubar.user")); //Users
280
menu.setEnabled(buttonState);
281         menu.setBackground(new Color JavaDoc(153, 153, 255));
282         add(menu);
283         mi = (JMenuItem JavaDoc) menu.add(new JMenuItem JavaDoc(resource.getString("wfmenubar.newuser")));
284         mi.setBackground(new Color JavaDoc(153, 153, 255));
285         mi.setAccelerator(
286             KeyStroke.getKeyStroke(KeyEvent.VK_U, ActionEvent.CTRL_MASK));
287         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/User.gif")));
288         mi.setEnabled(buttonState);
289         mi.addActionListener(new ActionListener JavaDoc() {
290             public void actionPerformed(ActionEvent JavaDoc e) {
291                 manager.newUser();
292             }
293         });
294         menu.add(mi);
295
296         mi = (JMenuItem JavaDoc) menu.add(new JMenuItem JavaDoc(resource.getString("wfmenubar.projuser")));
297         mi.setBackground(new Color JavaDoc(153, 153, 255));
298         mi.setAccelerator(
299             KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
300         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/User2.gif")));
301         mi.setEnabled(buttonState);
302         mi.addActionListener(new ActionListener JavaDoc() {
303             public void actionPerformed(ActionEvent JavaDoc e) {
304                 manager.usersInProject();
305             }
306         });
307         menu.add(mi);
308
309         menu.addSeparator();
310
311         mi = (JMenuItem JavaDoc) menu.add(new JMenuItem JavaDoc(resource.getString("wfmenubar.addrole")));
312         mi.setBackground(new Color JavaDoc(153, 153, 255));
313         mi.setAccelerator(
314             KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
315         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Role2.gif")));
316         mi.setEnabled(buttonState);
317         mi.addActionListener(new ActionListener JavaDoc() {
318             public void actionPerformed(ActionEvent JavaDoc e) {
319                 manager.newRole();
320             }
321         });
322         menu.add(mi);
323         mi = (JMenuItem JavaDoc) menu.add(new JMenuItem JavaDoc(resource.getString("wfmenubar.addrole2")));
324         mi.setBackground(new Color JavaDoc(153, 153, 255));
325         mi.setAccelerator(
326             KeyStroke.getKeyStroke(KeyEvent.VK_J, ActionEvent.CTRL_MASK));
327         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Role.gif")));
328         mi.setEnabled(buttonState);
329         mi.addActionListener(new ActionListener JavaDoc() {
330             public void actionPerformed(ActionEvent JavaDoc e) {
331                 manager.addUserToRole();
332             }
333         });
334         menu.add(mi);
335
336         mi = (JMenuItem JavaDoc) menu.add(new JMenuItem JavaDoc(resource.getString("wfmenubar.addmapper")));
337         mi.setBackground(new Color JavaDoc(153, 153, 255));
338         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/rolemapper.gif")));
339         mi.setEnabled(buttonState);
340         mi.addActionListener(new ActionListener JavaDoc() {
341             public void actionPerformed(ActionEvent JavaDoc e) {
342                 manager.addMapperToRole();
343             }
344         });
345         menu.add(mi);
346
347         menu = new JMenu JavaDoc(resource.getString("wfmenubar.zoom")); //Zoom
348
menu.setBackground(new Color JavaDoc(153, 153, 255));
349         add(menu);
350
351         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.fit"));
352         mi.setBackground(new Color JavaDoc(153, 153, 255));
353         mi.setAccelerator(
354             KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
355         mi.addActionListener(new ActionListener JavaDoc() {
356             public void actionPerformed(ActionEvent JavaDoc e) {
357                 manager.fitWindow();
358             }
359         });
360         menu.add(mi);
361
362         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.zoom100"));
363         mi.setBackground(new Color JavaDoc(153, 153, 255));
364         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/Zoom100.gif")));
365         mi.setAccelerator(
366             KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
367         mi.addActionListener(new ActionListener JavaDoc() {
368             public void actionPerformed(ActionEvent JavaDoc e) {
369                 manager.zoom(100);
370             }
371         });
372         menu.add(mi);
373
374         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.zoomin"));
375         mi.setBackground(new Color JavaDoc(153, 153, 255));
376         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/ZoomIn.gif")));
377         mi.setAccelerator(
378             KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
379         mi.addActionListener(new ActionListener JavaDoc() {
380             public void actionPerformed(ActionEvent JavaDoc e) {
381                 manager.zoomIn();
382             }
383         });
384         menu.add(mi);
385
386         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.zoomout"));
387         mi.setBackground(new Color JavaDoc(153, 153, 255));
388         mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/ZoomOut.gif")));
389         mi.setAccelerator(
390             KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
391         mi.addActionListener(new ActionListener JavaDoc() {
392             public void actionPerformed(ActionEvent JavaDoc e) {
393                 manager.zoomOut();
394             }
395         });
396         menu.add(mi);
397         /*
398             menu = new JMenu("Help"); //HELP
399             add(menu);
400         */

401
402         menu = new JMenu JavaDoc(resource.getString("wfmenubar.info")); //Info
403
menu.setBackground(new Color JavaDoc(153, 153, 255));
404         add(menu);
405
406         mi = new JMenuItem JavaDoc(resource.getString("wfmenubar.about")); // - Copy
407
mi.setBackground(new Color JavaDoc(153, 153, 255));
408         // mi.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C, ActionEvent.CTRL_MASK));
409
mi.setIcon(new ImageIcon JavaDoc(cl.getResource("images/toolbar-info.png")));
410         mi.addActionListener(new ActionListener JavaDoc() {
411             public void actionPerformed(ActionEvent JavaDoc e) {
412                 manager.Info();
413             }
414         });
415         menu.add(mi);
416
417     }
418
419     /* Show a dialog with the given error message. */
420     public void error(String JavaDoc message) {
421         JOptionPane.showMessageDialog(this,message,resource.getString("wfmenubar.info"),JOptionPane.ERROR_MESSAGE);
422     }
423     /* Show a file save dialog and return the filename. */
424     public String JavaDoc saveDialog(String JavaDoc message) {
425         return dialog(message, FileDialog.SAVE);
426     }
427
428     /* Show a file load dialog and return the filename. */
429     public String JavaDoc loadDialog(String JavaDoc message) {
430         return dialog(message, FileDialog.LOAD);
431     }
432
433     /* Open a dialog and return the filename. Returns null if cancelled. */
434     protected String JavaDoc dialog(String JavaDoc message, int mode) {
435         FileDialog JavaDoc f = new FileDialog JavaDoc(frame, message, mode);
436         f.show();
437         if (f.getFile() != null)
438             return f.getDirectory() + f.getFile();
439         else
440             return null;
441     }
442
443 }
444
Popular Tags