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