View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, Joćo E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d.gui; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import javax.swing.*; 25 import camera3d.*; 26 import java.io.*; 27 import java.util.*; 28 29 /*** 30 * VcApplication main window. 31 * 32 * @author Fabio Roberto de Miranda 33 */ 34 public class VcFrame extends JFrame { 35 private boolean beanMode = false; 36 37 /*** Object to which are sent users requests. */ 38 private GUIControl guiControl; 39 40 private JFileChooser fileDialog; 41 private JPanel contentPane; 42 private JMenuBar jMenuBar1 = new JMenuBar(); 43 private JMenu jMenuFile = new JMenu(); 44 private JMenuItem fileExitMenuItem = new JMenuItem(); 45 private JMenu jMenuHelp = new JMenu(); 46 private JMenuItem jMenuHelpAbout = new JMenuItem(); 47 private JToolBar jToolBar = new JToolBar(); 48 private JButton openBtn = new JButton(); 49 private JButton importBtn = new JButton(); 50 private JButton saveBtn = new JButton(); 51 private ImageIcon image1; 52 private ImageIcon image2; 53 private ImageIcon image3; 54 private ImageIcon image4; 55 private ImageIcon camImage; 56 private ImageIcon viewportImage; 57 private ImageIcon undoImage; 58 private ImageIcon deleteImage; 59 private ImageIcon gridImage; 60 private ImageIcon lightImage; 61 private ImageIcon frameImage; 62 private ImageIcon measurerImage; 63 private ImageIcon dumpImage; 64 /* Used to generate actions (such as deletion) that affect selected objects. */ 65 private SelectionList selectionList; 66 private JLabel statusBar = new JLabel(); 67 private BorderLayout borderLayout1 = new BorderLayout(); 68 private JButton addCamBtn = new JButton(); 69 private JButton undoBtn = new JButton(); 70 private JButton addViewBtn = new JButton(); 71 private JButton deleteBtn = new JButton(); 72 private JMenuItem openMenuItem = new JMenuItem(); 73 private JMenuItem importMenuItem = new JMenuItem(); 74 private JMenuItem saveMenuItem = new JMenuItem(); 75 private JMenu editMenuItem = new JMenu(); 76 private JMenuItem undoMenuItem = new JMenuItem(); 77 private JPanel leftPanel = new JPanel(); 78 79 private FrameSwitcherPane editionSwitcherPane = new FrameSwitcherPane(); 80 private EditionPane editionPane = new EditionPane(); 81 82 private FrameSwitcherPane selectionSwitcherPane = new FrameSwitcherPane(); 83 private JScrollPane jScrollPane1 = new JScrollPane(); 84 85 86 private SelectionListPane selectionListPane = new SelectionListPane(); 87 private GridBagLayout gridBagLayout1 = new GridBagLayout(); 88 private JDesktopPane desktopPane = new JDesktopPane(); 89 private JButton gridBtn = new JButton(); 90 private JButton addLightBtn = new JButton(); 91 private LightChooserDialog lightDialog; 92 private JMenuItem jMenuItem2 = new JMenuItem(); 93 private JMenuItem jMenuItem3 = new JMenuItem(); 94 private JMenu debugMenuItem = new JMenu(); 95 private JMenuItem debugEnableMenuItem = new JMenuItem(); 96 private JMenuItem debugDisableMenuItem = new JMenuItem(); 97 private JMenu jMenu1 = new JMenu(); 98 private JMenuItem takeOutMenuItem = new JMenuItem(); 99 private JMenuItem bringInMenuItem = new JMenuItem(); 100 private JMenuItem minAllMenItem = new JMenuItem(); 101 private JButton measurerBtn = new JButton(); 102 private JButton dumpBtn = new JButton(); 103 104 105 /***Constructs the frame.*/ 106 public VcFrame() { 107 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 108 try { 109 jbInit(); 110 } 111 catch(Exception e) { 112 e.printStackTrace(); 113 } 114 fileDialog = new JFileChooser(); 115 this.setIconImage((new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/frameicon.gif"))).getImage()); 116 117 editionSwitcherPane.setTitle("Edition Window"); 118 selectionSwitcherPane.setTitle("Selection Window"); 119 } 120 121 /***Component initialization*/ 122 private void jbInit() throws Exception { 123 lightDialog = new LightChooserDialog(this); 124 image1 = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/openFile.gif")); 125 image2 = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/closeFile.gif")); 126 image3 = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/help.gif")); 127 image4 = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/importFile.gif")); 128 camImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/camera.gif")); 129 viewportImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/vport.gif")); 130 deleteImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/delete.gif")); 131 undoImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/undo.gif")); 132 gridImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/grid.gif")); 133 lightImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/light.gif")); 134 frameImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/frame.gif")); 135 measurerImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/measurericon.gif")); 136 dumpImage = new ImageIcon(camera3d.gui.VcFrame.class.getResource("icons/dumpicon.gif")); 137 138 139 //setIconImage(Toolkit.getDefaultToolkit().createImage(VcFrame.class.getResource("[Your Icon]"))); 140 contentPane = (JPanel) this.getContentPane(); 141 contentPane.setLayout(borderLayout1); 142 this.setSize(new Dimension(590, 420)); 143 this.setTitle("Virtual Camera"); 144 statusBar.setText(" "); 145 jMenuFile.setText("File"); 146 fileExitMenuItem.setText("Exit"); 147 fileExitMenuItem.addActionListener(new ActionListener() { 148 public void actionPerformed(ActionEvent e) { 149 fileExitMenuItem_actionPerformed(e); 150 } 151 }); 152 jMenuHelp.setText("Help"); 153 jMenuHelpAbout.setText("About"); 154 jMenuHelpAbout.addActionListener(new ActionListener() { 155 public void actionPerformed(ActionEvent e) { 156 jMenuHelpAbout_actionPerformed(e); 157 } 158 }); 159 openBtn.setIcon(image1); 160 openBtn.addActionListener(new java.awt.event.ActionListener() { 161 public void actionPerformed(ActionEvent e) { 162 openBtn_actionPerformed(e); 163 } 164 }); 165 openBtn.setToolTipText("Open File"); 166 167 importBtn.setIcon(image4); 168 importBtn.addActionListener(new java.awt.event.ActionListener() { 169 public void actionPerformed(ActionEvent e) { 170 importBtn_actionPerformed(e); 171 } 172 }); 173 importBtn.setToolTipText("Import File"); 174 175 saveBtn.setIcon(image2); 176 saveBtn.addActionListener(new java.awt.event.ActionListener() { 177 public void actionPerformed(ActionEvent e) { 178 saveBtn_actionPerformed(e); 179 } 180 }); 181 saveBtn.setEnabled(true); 182 saveBtn.setToolTipText("Close File"); 183 addCamBtn.setToolTipText("add new camera"); 184 addCamBtn.setIcon(camImage); 185 addCamBtn.addActionListener(new java.awt.event.ActionListener() { 186 public void actionPerformed(ActionEvent e) { 187 addCamBtn_actionPerformed(e); 188 } 189 }); 190 undoBtn.setIcon(undoImage); 191 undoBtn.setEnabled(false); 192 undoBtn.setToolTipText("undo last operation"); 193 addViewBtn.setToolTipText("add viewport"); 194 addViewBtn.setIcon(viewportImage); 195 addViewBtn.addActionListener(new java.awt.event.ActionListener() { 196 public void actionPerformed(ActionEvent e) { 197 addViewBtn_actionPerformed(e); 198 } 199 }); 200 deleteBtn.setIcon(deleteImage); 201 deleteBtn.addActionListener(new java.awt.event.ActionListener() { 202 public void actionPerformed(ActionEvent e) { 203 deleteBtn_actionPerformed(e); 204 } 205 }); 206 deleteBtn.setToolTipText("delete selected object"); 207 openMenuItem.setText("Open"); 208 openMenuItem.addActionListener(new java.awt.event.ActionListener() { 209 public void actionPerformed(ActionEvent e) { 210 openMenuItem_actionPerformed(e); 211 } 212 }); 213 importMenuItem.setText("Import File"); 214 importMenuItem.addActionListener(new java.awt.event.ActionListener() { 215 public void actionPerformed(ActionEvent e) { 216 importMenuItem_actionPerformed(e); 217 } 218 }); 219 saveMenuItem.setText("Save File"); 220 saveMenuItem.addActionListener(new java.awt.event.ActionListener() { 221 public void actionPerformed(ActionEvent e) { 222 saveMenuItem_actionPerformed(e); 223 } 224 }); 225 226 227 editMenuItem.setRolloverSelectedIcon(image3); 228 editMenuItem.setText("Edit"); 229 undoMenuItem.setEnabled(false); 230 undoMenuItem.setText("Undo"); 231 leftPanel.setMinimumSize(new Dimension(200, 262)); 232 leftPanel.setPreferredSize(new Dimension(250, 200)); 233 leftPanel.setLayout(gridBagLayout1); 234 gridBtn.setToolTipText("toggle helping grid"); 235 gridBtn.setIcon(gridImage); 236 gridBtn.addActionListener(new java.awt.event.ActionListener() { 237 public void actionPerformed(ActionEvent e) { 238 gridBtn_actionPerformed(e); 239 } 240 }); 241 addLightBtn.addActionListener(new java.awt.event.ActionListener() { 242 public void actionPerformed(ActionEvent e) { 243 addLightBtn_actionPerformed(e); 244 } 245 }); 246 addLightBtn.setIcon(lightImage); 247 addLightBtn.setToolTipText("add a ligth to the scene"); 248 addLightBtn.setActionCommand("new light"); 249 jMenuItem2.setEnabled(false); 250 jMenuItem2.setText("User manual"); 251 jMenuItem3.setEnabled(false); 252 jMenuItem3.setText("ActiveX Component Reference"); 253 debugMenuItem.setText("debug mode"); 254 debugEnableMenuItem.setText("Enable"); 255 debugEnableMenuItem.addActionListener(new java.awt.event.ActionListener() { 256 public void actionPerformed(ActionEvent e) { 257 debugEnableMenuItem_actionPerformed(e); 258 } 259 }); 260 debugDisableMenuItem.setText("Disable"); 261 debugDisableMenuItem.addActionListener(new java.awt.event.ActionListener() { 262 public void actionPerformed(ActionEvent e) { 263 debugDisableMenuItem_actionPerformed(e); 264 } 265 }); 266 jMenu1.setText("Window"); 267 takeOutMenuItem.setText("Take outside"); 268 269 takeOutMenuItem.addActionListener(new java.awt.event.ActionListener() { 270 public void actionPerformed(ActionEvent e) { 271 takeOutMenuItem_actionPerformed(e); 272 } 273 }); 274 bringInMenuItem.setText("Bring inside"); 275 minAllMenItem.setText("Minimize all"); 276 measurerBtn.addActionListener(new java.awt.event.ActionListener() { 277 public void actionPerformed(ActionEvent e) { 278 measurerBtn_actionPerformed(e); 279 } 280 }); 281 measurerBtn.setIcon(measurerImage); 282 measurerBtn.setToolTipText("Creates measurer"); 283 dumpBtn.setToolTipText("Dumps object edges to file"); 284 dumpBtn.setIcon(dumpImage); 285 dumpBtn.addActionListener(new java.awt.event.ActionListener() { 286 public void actionPerformed(ActionEvent e) { 287 dumpBtn_actionPerformed(e); 288 } 289 }); 290 291 jToolBar.add(openBtn); 292 jToolBar.add(importBtn); 293 jToolBar.add(saveBtn); 294 jToolBar.addSeparator(new Dimension(40, 5)); 295 jToolBar.add(undoBtn, null); 296 jToolBar.add(deleteBtn, null); 297 jToolBar.add(addViewBtn, null); 298 jToolBar.add(addCamBtn, null); 299 jToolBar.add(gridBtn, null); 300 jToolBar.add(addLightBtn, null); 301 jToolBar.add(measurerBtn, null); 302 jToolBar.add(dumpBtn, null); 303 304 305 jMenuFile.add(openMenuItem); 306 jMenuFile.add(importMenuItem); 307 jMenuFile.add(saveMenuItem); 308 jMenuFile.addSeparator(); 309 jMenuFile.add(fileExitMenuItem); 310 jMenuHelp.add(jMenuHelpAbout); 311 jMenuHelp.add(jMenuItem2); 312 jMenuHelp.add(jMenuItem3); 313 jMenuBar1.add(jMenuFile); 314 jMenuBar1.add(editMenuItem); 315 jMenuBar1.add(jMenuHelp); 316 jMenuBar1.add(jMenu1); 317 this.setJMenuBar(jMenuBar1); 318 contentPane.add(jToolBar, BorderLayout.NORTH); 319 contentPane.add(statusBar, BorderLayout.SOUTH); 320 contentPane.add(leftPanel, BorderLayout.EAST); 321 desktopPane.setDragMode(JDesktopPane.LIVE_DRAG_MODE); 322 contentPane.add(desktopPane, BorderLayout.CENTER); 323 324 325 leftPanel.add(editionSwitcherPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0 326 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 3, 1, 2), 67, 108)); 327 editionSwitcherPane.add(editionPane, BorderLayout.CENTER); 328 329 selectionSwitcherPane.add(jScrollPane1, BorderLayout.CENTER); 330 leftPanel.add(selectionSwitcherPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0 331 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 3, 0, 2), 12, 108)); 332 333 jScrollPane1.getViewport().add(selectionListPane, null); 334 335 336 editMenuItem.add(undoMenuItem); 337 editMenuItem.add(debugMenuItem); 338 debugMenuItem.add(debugEnableMenuItem); 339 debugMenuItem.add(debugDisableMenuItem); 340 jMenu1.add(takeOutMenuItem); 341 jMenu1.add(bringInMenuItem); 342 jMenu1.add(minAllMenItem); 343 344 } 345 346 /***File | Exit action performed*/ 347 public void fileExitMenuItem_actionPerformed(ActionEvent e) { 348 if (beanMode){ 349 this.dispose(); 350 } else { 351 System.exit(0); 352 } 353 } 354 355 356 /***Help | About action performed.*/ 357 public void jMenuHelpAbout_actionPerformed(ActionEvent e) { 358 VcFrame_AboutBox dlg = new VcFrame_AboutBox(this); 359 Dimension dlgSize = dlg.getPreferredSize(); 360 Dimension frmSize = getSize(); 361 Point loc = getLocation(); 362 dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); 363 dlg.setModal(true); 364 dlg.show(); 365 } 366 367 /***Overridden so we can exit when window is closed.*/ 368 protected void processWindowEvent(WindowEvent e) { 369 super.processWindowEvent(e); 370 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 371 fileExitMenuItem_actionPerformed(null); 372 } 373 } 374 375 public SelectionListPane getSelectionListPane(){ 376 return this.selectionListPane; 377 } 378 379 public EditionPane getEditionPane(){ 380 return this.editionPane; 381 } 382 383 public JDesktopPane getDesktopPane(){ 384 return this.desktopPane; 385 } 386 387 // changed to handle both save and open situations 388 /*** 389 * Shows dialog that asks user to select a file selected for opening or saving. 390 * The name of the file is returned at end of execution. 391 * @param open true if file is to be selected for opening, false if file is to be 392 * selected for saving. 393 * @return name of the file. 394 */ 395 public String getFilename(boolean open){ 396 397 int returnVal; 398 if(open) returnVal = fileDialog.showOpenDialog(this); 399 else returnVal = fileDialog.showSaveDialog(this); 400 401 String name = null; 402 403 if (returnVal == JFileChooser.APPROVE_OPTION) { 404 File file = fileDialog.getSelectedFile(); 405 //this is where a real application would open the file. 406 name = file.getAbsolutePath(); 407 } else { 408 debugln("Command cancelled by user."); 409 } 410 debugln("File chosen: "+name); 411 return name; 412 } 413 414 /*** 415 * Prints a message meant for debugging. 416 * @param s message to be printed. 417 */ 418 void debugln(String s){ 419 if (GUIControl.debugflag){ 420 System.out.println(s); 421 } 422 } 423 424 /*** 425 * Sets the GUIControl for this object. That involves setting the SelectionPane 426 * and EditionPane for guiControl and adding it to the group of SelectionChangeListeners. 427 */ 428 public void setGUIControl(GUIControl guiControl){ 429 this.guiControl = guiControl; 430 if (guiControl.getDebugFlag()){ 431 debugEnableMenuItem.setEnabled(false); 432 debugDisableMenuItem.setEnabled(true); 433 } else { 434 debugEnableMenuItem.setEnabled(true); 435 debugDisableMenuItem.setEnabled(false); 436 } 437 selectionListPane.setGUIControl(guiControl); 438 editionPane.setGUIControl(guiControl); 439 if(guiControl!=null){ 440 if(this.selectionList!=null) this.selectionList.addSelectionChangeListener(guiControl); 441 } 442 /* Following two lines added in order to allow saving/retrieving the state 443 of both FrameSwitcherPanes */ 444 guiControl.setSelectionPane(selectionSwitcherPane); 445 guiControl.setEditionPane(editionSwitcherPane); 446 } 447 448 void openBtn_actionPerformed(ActionEvent e) { 449 //added 16/08/2002 450 fileDialog.resetChoosableFileFilters(); 451 CustomFileFilter filter = new CustomFileFilter(); 452 filter.addExtension("xml"); 453 filter.setDescription("Environment File"); 454 fileDialog.setFileFilter(filter); 455 // 456 String s = this.getFilename(true); 457 if (s!=null){ 458 if (!s.equals("")){ 459 guiControl.loadFile(s); 460 } 461 } 462 } 463 464 void importBtn_actionPerformed(ActionEvent e) { 465 //added 16/08/2002 466 fileDialog.resetChoosableFileFilters(); 467 CustomFileFilter filter = new CustomFileFilter(); 468 filter.addExtension("wrl"); 469 filter.setDescription("VRML2.0 File"); 470 fileDialog.setFileFilter(filter); 471 // 472 String s = this.getFilename(true); 473 if (s!=null){ 474 if (!s.equals("")){ 475 guiControl.importFile(s); 476 } 477 } 478 } 479 480 // added 27/06/2002 - Carlos 481 void saveBtn_actionPerformed(ActionEvent e) { 482 //added 16/08/2002 483 fileDialog.resetChoosableFileFilters(); 484 CustomFileFilter filter = new CustomFileFilter(); 485 filter.addExtension("xml"); 486 filter.setDescription("Environment File"); 487 fileDialog.setFileFilter(filter); 488 // 489 String s = this.getFilename(false); 490 if (s!=null){ 491 if (!s.equals("")){ 492 guiControl.saveFile(s); 493 } 494 } 495 } 496 497 498 void addCamBtn_actionPerformed(ActionEvent e) { 499 guiControl.addView("Camera"+VcView.getInstanceCounter()); 500 } 501 502 void addViewBtn_actionPerformed(ActionEvent e) { 503 guiControl.addViewport(); 504 } 505 506 void gridBtn_actionPerformed(ActionEvent e) { 507 guiControl.toggleHelpingGrid(); 508 } 509 void addLightBtn_actionPerformed(ActionEvent e) { 510 if (e.getActionCommand().equals("new light")){ 511 lightDialog.showDialog(); 512 if (lightDialog.getUserAction()==LightChooserDialog.OK_SELECTED){ 513 int lightType = lightDialog.getLightSelection(); 514 String name = lightDialog.getLightName(); 515 guiControl.addLight(lightType, name); 516 517 } 518 } 519 520 } 521 522 /*** 523 * Sets the SelectionList for this object. The current GUIControl object 524 * is added to the group of listeners to list. 525 */ 526 public void setSelectionList(SelectionList list){ 527 this.selectionList = list; 528 editionPane.setSelectionList(list); 529 selectionListPane.setSelectionList(list); 530 531 if(guiControl!=null){ 532 if(this.selectionList!=null) this.selectionList.addSelectionChangeListener(guiControl); 533 } 534 } 535 536 537 void deleteBtn_actionPerformed(ActionEvent e) { 538 java.util.List selectedObjects = selectionList.getSelectedObjects(); 539 Iterator iter = selectedObjects.iterator(); 540 while (iter.hasNext()){ 541 Object obj = iter.next(); 542 if (obj instanceof VcObject){ 543 VcObject vcObj = (VcObject)obj; 544 guiControl.removeFromScene(vcObj); 545 } 546 } 547 } 548 549 /*** 550 * Sets flag that signals whether application is running in bean mode. 551 */ 552 public void setBeanMode(boolean b){ 553 this.beanMode = b; 554 } 555 556 void debugEnableMenuItem_actionPerformed(ActionEvent e) { 557 // Enable debug mode 558 debugEnableMenuItem.setEnabled(false); 559 debugDisableMenuItem.setEnabled(true); 560 guiControl.setDebugFlag(true); 561 } 562 563 void debugDisableMenuItem_actionPerformed(ActionEvent e) { 564 // disable debug mode 565 debugDisableMenuItem.setEnabled(false); 566 debugEnableMenuItem.setEnabled(true); 567 guiControl.setDebugFlag(false); 568 } 569 570 void openMenuItem_actionPerformed(ActionEvent e) { 571 openBtn_actionPerformed(e); 572 } 573 574 void importMenuItem_actionPerformed(ActionEvent e) { 575 importBtn_actionPerformed(e); 576 } 577 578 void saveMenuItem_actionPerformed(ActionEvent e) { 579 saveBtn_actionPerformed(e); 580 } 581 582 void frameBtn_actionPerformed(ActionEvent e) { 583 java.util.List selectedObjects = selectionList.getSelectedObjects(); 584 Iterator iter = selectedObjects.iterator(); 585 while (iter.hasNext()){ 586 Object obj = iter.next(); 587 if (obj instanceof VcObject){ 588 VcObject vcObj = (VcObject)obj; 589 if (vcObj instanceof VcView){ 590 VcView view = (VcView)vcObj; 591 guiControl.frameObjectsInView(view); 592 } 593 } 594 } 595 596 } 597 598 void takeOutMenuItem_actionPerformed(ActionEvent e) { 599 600 } 601 602 void measurerBtn_actionPerformed(ActionEvent e) { 603 guiControl.addDistanceMeasurer(); 604 } 605 606 void dumpBtn_actionPerformed(ActionEvent e) { 607 guiControl.dumpContentEdgesToFile(); 608 } 609 610 611 }

This page was automatically generated by Maven