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 camera3d.*; 23 import javax.swing.*; 24 import java.awt.event.*; 25 26 /* 27 * Title: Câmera Virtual - LIVES 28 * Description: Câmera Virtual para Controle via LabVIEW 29 * Copyright: Copyright (c) 2001 30 * Company: Centro de Educação em Informática - SENAC - SP 31 */ 32 33 /*** 34 * This class is the popup Menu that appears over the Viewport. It allows controlling 35 * whether the viewport window is shown inside or outside the main application window. 36 * It also keeps some controls for mouse manipulation of scene objects. 37 * 38 * @author Fábio Roberto de Miranda, Carlos da Silva dos Santos 39 * @version 1.0 40 */ 41 class ViewportContextMenu implements ActionListener { 42 43 44 /*** Index of absolute item in checkItem array*/ 45 private static final int ABS_ITEM = 0; 46 47 /*** Index of relative item in checkItem array*/ 48 private static final int RELAT_ITEM = 1; 49 50 /*** Index of translation item in checkItem array*/ 51 private static final int TRANS_ITEM = 2; 52 53 /*** Index of rotation item in checkItem array*/ 54 private static final int ROT_ITEM = 3; 55 56 /*** Index of scale item in checkItem array*/ 57 private static final int SCALE_ITEM = 4; 58 59 /*** Index of x axis item in checkItem array*/ 60 private static final int X_ITEM = 5; 61 62 /*** Index of y axis item in checkItem array*/ 63 private static final int Y_ITEM = 6; 64 65 /*** Index of z axis item in checkItem array*/ 66 private static final int Z_ITEM = 7; 67 68 /*** Index of XY plane item in checkItem array*/ 69 private static final int XY_ITEM = 8; 70 71 /*** Index of YZ plane item in checkItem array*/ 72 private static final int YZ_ITEM = 9; 73 74 /*** Index of ZX item in checkItem array*/ 75 private static final int ZX_ITEM = 10; 76 77 /*** Index of XYZ in checkItem array*/ 78 private static final int XYZ_ITEM = 11; 79 80 private TransformType type; 81 private TransformMode mode; 82 private TransformScope scope; 83 84 /* the popup menu itself. */ 85 private JPopupMenu popup = new JPopupMenu(); 86 87 /*** Menu item used to change internal/external status of Viewport frame. */ 88 private JMenuItem frameStateItem; 89 90 private JCheckBoxMenuItem[] checkItem; 91 private JCheckBoxMenuItem dummyAction = new JCheckBoxMenuItem(); 92 93 /*** Menu for selecting relative/absolute modes. */ 94 private JMenu modeMenu = new JMenu("Mode"); 95 /*** Menu for choosing type of action (Translation, Rotation, Scaling, etc ). */ 96 private JMenu actionMenu = new JMenu("Action"); 97 /*** Menu for choosing axis affected by action. */ 98 private JMenu axisMenu = new JMenu("Axis"); 99 100 private ButtonGroup modeGroup = new ButtonGroup(); 101 private ButtonGroup actionGroup = new ButtonGroup(); 102 private ButtonGroup axisGroup = new ButtonGroup(); 103 104 105 // 106 /* 107 * MouseListener is used to process mouse events that come from the Canvas3D. 108 */ 109 private MouseListener mouseListener; 110 111 private KeybListener keyboardListener; 112 113 private ViewportFrame viewportFrame; 114 115 private ViewportsEventHandler viewportsEventHandler; 116 117 /*** 118 * Creates context menu, setting the current ViewportFrame. The newly created context 119 * menu is registered as a listener to mouse events generated by the Canvas3D object 120 * contained by vpFrame. 121 * @param vpFrame ViewportFrame whose Canvas3D events will be sent to this context menu. 122 */ 123 ViewportContextMenu(ViewportFrame vpFrame) { 124 this(); 125 setViewportFrame(vpFrame); 126 } 127 128 129 /*** 130 * Detaches this context menu from its ViewportFrame, so Canvas3D events are no longer 131 * sent to this object. 132 */ 133 void detachFromViewportFrame(){ 134 if (viewportFrame != null){ 135 viewportFrame.getViewport().getCanvas3D().removeMouseListener(mouseListener); 136 } 137 } 138 139 /*** 140 * Sets the ViewportFrame whose Canvas events will be sent to this context menu. 141 */ 142 void setViewportFrame(ViewportFrame vpFrame){ 143 detachFromViewportFrame(); // added 08/04/2003 144 this.viewportFrame = vpFrame; 145 146 // Adds listeners 147 vpFrame.getViewport().getCanvas3D().addMouseListener(mouseListener); 148 149 if (viewportFrame.isInternalFrame()){ 150 frameStateItem.setIcon(new ImageIcon(ViewportContextMenu.class.getResource("icons/frameout.gif"))); 151 frameStateItem.setText("Bring the window outside"); 152 } else { 153 frameStateItem.setIcon(new ImageIcon(ViewportContextMenu.class.getResource("icons/framein.gif"))); 154 frameStateItem.setText("Bring the window inside"); 155 } 156 } 157 158 159 ViewportContextMenu() { 160 mouseListener = new PopupListener(); 161 keyboardListener = new KeybListener(); 162 frameStateItem = new JMenuItem(); 163 164 popup.setLightWeightPopupEnabled(false); 165 popup.setDefaultLightWeightPopupEnabled(false); // Added 06/2002 166 ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);// Added 06/2002 167 popup.add(frameStateItem); 168 frameStateItem.addActionListener(this); 169 popup.addSeparator(); 170 171 checkItem = new JCheckBoxMenuItem[12]; 172 173 // Global coord 174 checkItem[ABS_ITEM] = new JCheckBoxMenuItem("Absolute"); 175 // Local coord 176 checkItem[RELAT_ITEM] = new JCheckBoxMenuItem("Relative"); 177 178 modeMenu.add(checkItem[ABS_ITEM]); 179 modeMenu.add(checkItem[RELAT_ITEM]); 180 modeGroup.add(checkItem[ABS_ITEM]); 181 modeGroup.add(checkItem[RELAT_ITEM]); 182 183 // Translation 184 checkItem[TRANS_ITEM] = new JCheckBoxMenuItem("Translation"); 185 // Rotation 186 checkItem[ROT_ITEM] = new JCheckBoxMenuItem("Rotation"); 187 // Scaling 188 checkItem[SCALE_ITEM] = new JCheckBoxMenuItem("Scaling"); 189 190 popup.add(checkItem[TRANS_ITEM]); 191 popup.add(checkItem[ROT_ITEM]); 192 popup.add(checkItem[SCALE_ITEM]); 193 194 popup.addSeparator(); 195 196 actionGroup.add(checkItem[TRANS_ITEM]); 197 actionGroup.add(checkItem[ROT_ITEM]); 198 actionGroup.add(checkItem[SCALE_ITEM]); 199 actionGroup.add(dummyAction); 200 201 // X 202 checkItem[X_ITEM] = new JCheckBoxMenuItem("X"); 203 // Y 204 checkItem[Y_ITEM] = new JCheckBoxMenuItem("Y"); 205 // Z 206 checkItem[Z_ITEM] = new JCheckBoxMenuItem("Z"); 207 // XY 208 checkItem[XY_ITEM] = new JCheckBoxMenuItem("XY"); 209 // YZ 210 checkItem[YZ_ITEM] = new JCheckBoxMenuItem("YZ"); 211 // ZX 212 checkItem[ZX_ITEM] = new JCheckBoxMenuItem("ZX"); 213 // XYZ 214 checkItem[XYZ_ITEM] = new JCheckBoxMenuItem("XYZ"); 215 216 axisMenu.add(checkItem[X_ITEM]); 217 axisMenu.add(checkItem[Y_ITEM]); 218 axisMenu.add(checkItem[Z_ITEM]); 219 axisMenu.add(checkItem[XY_ITEM]); 220 axisMenu.add(checkItem[YZ_ITEM]); 221 axisMenu.add(checkItem[ZX_ITEM]); 222 axisMenu.add(checkItem[XYZ_ITEM]); 223 224 axisGroup.add(checkItem[X_ITEM]); 225 axisGroup.add(checkItem[Y_ITEM]); 226 axisGroup.add(checkItem[Z_ITEM]); 227 axisGroup.add(checkItem[XY_ITEM]); 228 axisGroup.add(checkItem[YZ_ITEM]); 229 axisGroup.add(checkItem[ZX_ITEM]); 230 axisGroup.add(checkItem[XYZ_ITEM]); 231 232 233 for (int i = 0; i < checkItem.length; i++) { 234 checkItem[i].addActionListener(this); 235 } 236 237 popup.add(modeMenu); 238 popup.add(axisMenu); 239 240 /* 241 * Added in 24-5-2002 to circumvent null pData bug (4298592 in Sun's Bug Parade) 242 */ 243 modeMenu.enableInputMethods(false); 244 axisMenu.enableInputMethods(false); 245 246 } 247 248 /* Code pasted from Java's PopupDemo.java */ 249 class PopupListener extends MouseAdapter implements MouseMotionListener { 250 public void mousePressed(MouseEvent e) { 251 //System.out.println("Mouse listened to"); 252 maybeShowPopup(e); 253 } 254 255 public void mouseReleased(MouseEvent e) { 256 maybeShowPopup(e); 257 } 258 259 private void maybeShowPopup(MouseEvent e) { 260 if (e.isPopupTrigger()) { 261 popup.show(e.getComponent(), 262 e.getX(), e.getY()); 263 } 264 } 265 266 public void mouseEntered(MouseEvent e){ 267 debugln("Mouse entered"); 268 VcView view = viewportFrame.getViewport().getView(); 269 debugln("Camera name:"+view.getLabel()); 270 /* 271 if(viewportsEventHandler!=null){ 272 viewportsEventHandler.setEnteredViewport(viewportFrame.getViewport()); 273 }*/ 274 // Commented out because it was annoying - 22/04/2002 275 //viewportFrame.getViewport().getCanvas3D().requestFocus(); 276 } 277 278 public void mouseExited(MouseEvent e){ 279 //debugln("Mouse exited"); 280 } 281 282 // MouseMotionListener methods 283 public void mouseMoved(MouseEvent e){ 284 //debugln("Mouse moved to "+e.getX()+","+e.getY()); 285 } 286 287 public void mouseDragged(MouseEvent e){ 288 //debugln("Mouse Dragged to "+e.getX()+","+e.getY()); 289 } 290 291 } 292 293 294 class KeybListener extends KeyAdapter{ 295 public void keyPressed(KeyEvent k){ 296 debugln("Key Pressed: "+KeyEvent.getKeyText(k.getKeyCode())); 297 } 298 299 public void keyReleased(KeyEvent k){ 300 debugln("Key Released: "+KeyEvent.getKeyText(k.getKeyCode())); 301 } 302 } 303 304 public void actionPerformed(ActionEvent event){ 305 Object source = event.getSource(); 306 if (source==frameStateItem){ 307 toggleFrameState(); 308 } else if (source== checkItem[ABS_ITEM]){ 309 viewportsEventHandler.setSelectedManipulationMode(TransformMode.ABSOLUTE); 310 } else if (source== checkItem[RELAT_ITEM]){ 311 viewportsEventHandler.setSelectedManipulationMode(TransformMode.RELATIVE); 312 } else if (source== checkItem[TRANS_ITEM]){ 313 viewportsEventHandler.setSelectedManipulationAction(TransformType.TRANSLATION); 314 } else if (source== checkItem[ROT_ITEM]){ 315 viewportsEventHandler.setSelectedManipulationAction(TransformType.ROTATION); 316 } else if (source== checkItem[SCALE_ITEM]){ 317 viewportsEventHandler.setSelectedManipulationAction(TransformType.SCALING); 318 // scaling with mouse can only be performed in relative mode 319 viewportsEventHandler.setSelectedManipulationMode(TransformMode.RELATIVE); 320 } else if (source== checkItem[X_ITEM]){ 321 viewportsEventHandler.setSelectedManipulationScope(TransformScope.X); 322 debugln("VPCtxMenu: X"); 323 } else if (source== checkItem[Y_ITEM]){ 324 viewportsEventHandler.setSelectedManipulationScope(TransformScope.Y); 325 debugln("VPCtxMenu: Y"); 326 } else if (source== checkItem[Z_ITEM]){ 327 viewportsEventHandler.setSelectedManipulationScope(TransformScope.Z); 328 debugln("VPCtxMenu: Z"); 329 } else if (source== checkItem[XY_ITEM]){ 330 viewportsEventHandler.setSelectedManipulationScope(TransformScope.XY); 331 } else if (source== checkItem[YZ_ITEM]){ 332 viewportsEventHandler.setSelectedManipulationScope(TransformScope.YZ); 333 } else if (source== checkItem[ZX_ITEM]){ 334 viewportsEventHandler.setSelectedManipulationScope(TransformScope.ZX); 335 } else if (source== checkItem[XYZ_ITEM]){ 336 viewportsEventHandler.setSelectedManipulationScope(TransformScope.XYZ); 337 } 338 339 } 340 341 void setViewportEventsHandler(ViewportsEventHandler handler){ 342 this.viewportsEventHandler = handler; 343 } 344 345 void debugln(String s){ 346 if (GUIControl.debugflag) 347 System.out.println(s); 348 } 349 350 void setIndications(TransformType type, TransformMode mode, 351 TransformScope scope, boolean viewSelected){ 352 this.type = type; 353 this.mode = mode; 354 this.scope = scope; 355 checkConsistency(viewSelected); 356 handleTransformType(viewSelected); 357 if(this.mode == TransformMode.ABSOLUTE){ 358 checkItem[ABS_ITEM].setState(true); 359 }else if(this.mode == TransformMode.RELATIVE){ 360 checkItem[RELAT_ITEM].setState(true); 361 } 362 handleTransformScope(); 363 } 364 365 /*** 366 * Toggles internal/external state of viewportFrame. 367 */ 368 private void toggleFrameState(){ 369 if (viewportFrame.isInternalFrame()){ 370 viewportFrame.changeToJFrame(); 371 frameStateItem.setText("Take this window inside"); 372 } else { 373 viewportFrame.changeToJInternalFrame(); 374 frameStateItem.setText("Take this window outside"); 375 } 376 } 377 378 /*** 379 * Assures that settings of mode, type and scope are consistent. 380 */ 381 private void checkConsistency(boolean viewSelected){ 382 // view scaling must be uniform 383 if( viewSelected && this.type == TransformType.SCALING){ 384 this.mode = TransformMode.RELATIVE; 385 this.scope = TransformScope.XYZ; 386 } 387 // rotation scope must be an axis 388 if(this.type == TransformType.ROTATION && !scope.isAxis()){ 389 this.scope = TransformScope.X; 390 } 391 // translation scope should not be XYZ 392 if(this.type == TransformType.TRANSLATION && scope == TransformScope.XYZ ){ 393 this.scope = TransformScope.X; 394 } 395 } 396 397 /*** 398 * Enables/disables check items that correspond to axis. 399 * @param enable enables axis items if true, disables them if false. 400 */ 401 private void setAxisEnable(boolean enable){ 402 checkItem[X_ITEM].setEnabled(enable); 403 checkItem[Y_ITEM].setEnabled(enable); 404 checkItem[Z_ITEM].setEnabled(enable); 405 } 406 407 /*** 408 * Enables/disables check items that correspond to planes. 409 * @param enable enables plane items if true, disables them if false. 410 */ 411 private void setPlaneEnable(boolean enable){ 412 checkItem[XY_ITEM].setEnabled(enable); 413 checkItem[YZ_ITEM].setEnabled(enable); 414 checkItem[ZX_ITEM].setEnabled(enable); 415 } 416 417 /*** 418 * Enables/disables XYZ item. 419 * @param enable enables XYZ item if true, disables it if false. 420 */ 421 private void setXYZEnable(boolean enable){ 422 // if must disable XYZ item and it is already selected 423 // change selection to X item 424 if(!enable && checkItem[XYZ_ITEM].isSelected()){ 425 checkItem[X_ITEM].setSelected(true); 426 } 427 checkItem[XYZ_ITEM].setEnabled(enable); 428 } 429 430 private void handleTransformType(boolean viewSelected){ 431 // enables absolute transformations - we will disable 432 // it later, if transformation type is scaling 433 checkItem[ABS_ITEM].setEnabled(true); 434 if(this.type == TransformType.TRANSLATION){ 435 // Translation item 436 checkItem[TRANS_ITEM].setState(true); 437 // Enable absolute/relative control 438 modeMenu.setEnabled(true); 439 // enable translations relative to axis and plane 440 setAxisEnable(true); 441 setPlaneEnable(true); 442 // disables simultaneous translation in the 3 axii 443 setXYZEnable(false); 444 }else if(this.type == TransformType.ROTATION){ 445 // Rotation item 446 checkItem[ROT_ITEM].setState(true); 447 // Enable absolute/relative control 448 modeMenu.setEnabled(true); 449 setAxisEnable(true); 450 // cannot perform rotation relative to a plane 451 setPlaneEnable(false); 452 setXYZEnable(false); 453 }else if(this.type == TransformType.SCALING){ 454 // disables non-uniform scaling 455 if(viewSelected){ 456 setAxisEnable(false); 457 setPlaneEnable(false); 458 } 459 // enables uniform scaling selection 460 setXYZEnable(true); 461 // All scaling performed with mouse is relative, i.e. incremental 462 checkItem[ABS_ITEM].setEnabled(false); 463 }else if(type == TransformType.NONE){ 464 // Unselect all actions 465 dummyAction.setSelected(true); 466 // In case they are disabled, reenables checkBoxMenuItems 467 for (int i = 0; i < checkItem.length; i++) { 468 checkItem[i].setEnabled(true); 469 } 470 } 471 } 472 473 private void handleTransformScope(){ 474 if(this.scope == TransformScope.X){ 475 checkItem[X_ITEM].setState(true); 476 } 477 else if(this.scope == TransformScope.Y){ 478 checkItem[Y_ITEM].setState(true); 479 } 480 else if(this.scope == TransformScope.Z){ 481 checkItem[Z_ITEM].setState(true); 482 } 483 else if(this.scope == TransformScope.XY){ 484 checkItem[XY_ITEM].setState(true); 485 } 486 else if(this.scope == TransformScope.YZ){ 487 checkItem[YZ_ITEM].setState(true); 488 } 489 else if(this.scope == TransformScope.ZX){ 490 checkItem[ZX_ITEM].setState(true); 491 } 492 else if(this.scope == TransformScope.XYZ){ 493 checkItem[XYZ_ITEM].setState(true); 494 } 495 } 496 497 } 498

This page was automatically generated by Maven