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
23 import javax.swing.*;
24 import camera3d.event.*;
25 import camera3d.*;
26 import java.util.*;
27 import javax.vecmath.*;
28
29 /***
30 * This class manages all ViewportInternalCombos present at the application.
31 * One of its responsibilities is notifying the ViewportInternalCombos of
32 * changes in the number of VcViews of the scene.
33 * @author Fábio Roberto de Miranda, Carlos da Silva dos Santos
34 */
35 public final class ViewportWindowManager implements SceneObjectsChangeListener, VcObjectChangeListener {
36
37 private boolean debugflag = true;
38
39 private J3DBase base;
40 private JDesktopPane desktop;
41 private GUIControl guiControl;
42 private Vector vcViews = new Vector(20, 10);
43 private ViewportsEventHandler viewportsEventHandler;
44
45 private SelectionList selectionList;
46
47 private ViewportCombo[] vpCombo = new ViewportCombo[20];
48 private int axisLength = 5; // Width pixels are drawn in pixels
49
50
51 public ViewportWindowManager(JDesktopPane desktop, J3DBase base, GUIControl guiControl) {
52 this.desktop = desktop;
53 this.base = base;
54 this.guiControl = guiControl;
55 guiControl.setViewportManager(this);
56 vcViews.addAll(base.getViewList());
57 Iterator iter = vcViews.iterator();
58 VcView tempView;
59 while(iter.hasNext()){
60 tempView = (VcView)iter.next();
61 tempView.addChangeListener(this);
62 }
63 //this.sceneObjectsChanged(base); // Gets list of updated views for the first time
64 //viewportsEventHandler = new ViewportsEventHandler(guiControl);
65 }
66
67 public ViewportWindowManager(JDesktopPane desktop, J3DBase base, GUIControl guiControl, SelectionList list) {
68 this(desktop, base, guiControl);
69 this.setSelectionList(list);
70 }
71
72
73 public void setViewportsEventHandler(ViewportsEventHandler handler){
74 this.viewportsEventHandler = handler;
75 handler.setViewportWindowManager(this);
76 }
77
78 /***
79 * Changes the view shown by a certain viewport.
80 * @param viewportIndex index of viewport whose view will be changed.
81 * @param view new view to be shown by viewport. Must be non null.
82 * @throws IndexOutOfBoundsException if viewportIndex is invalid.
83 */
84 public void setViewInViewport(int viewportIndex, VcView view){
85 if (view == null){
86 throw new NullPointerException("VcView passed to setViewInViewport was null");
87 }
88 if(viewportIndex>=vpCombo.length){
89 throw new IndexOutOfBoundsException("Number of Viewports: "+ vpCombo.length +
90 "; Viewport index: " + viewportIndex);
91 }
92 if(vpCombo[viewportIndex]==null) this.createViewportInIndex(viewportIndex,view);
93 else vpCombo[viewportIndex].getViewport().setView(view);
94 vpCombo[viewportIndex].setVisible(true);
95 }
96
97 /***
98 * Moves input Viewport to front.
99 */
100 public void moveViewportToFront(Viewport viewport){
101 boolean found=false;
102 int i;
103 for(i=0;i<vpCombo.length;i++){
104 if(viewport==vpCombo[i].getViewport()) {
105 found = true;
106 break;
107 }
108 }
109 if(found)vpCombo[i].toFront();
110 }
111
112 /***
113 * Adds a new viewport window to workspace.
114 * @param view VcView that will be shown by new viewport window.
115 */
116 public void addViewport(VcView view){
117 VcView vcView = view;
118 if (vcView == null){
119 debugln("Viewport: View passed as argument to addViewport is null.");
120 vcView = base.getDefaultView();
121 }
122 // finds first empty slot
123 for (int i=0; i < vpCombo.length; i++) {
124 if (vpCombo[i] == null){
125 this.createViewportInIndex(i,vcView);
126 break;
127 }
128 }
129 setAxisPoints(null, null, null, null);
130 }
131
132
133 /***
134 * Method that effectively creates new Viewports.
135 * @param index position of new viewport in vpCombo array.
136 * @param vcView view that will be shown by new Viewport.
137 */
138 private void createViewportInIndex(int index, VcView vcView) {
139 //debugln("creating viewport");
140 //debugln("index: "+index);
141 //debugln("view: "+vcView.getLabel());
142 vpCombo[index] = new ViewportCombo(vcView, guiControl);
143 vpCombo[index].updateListOfViews(vcViews);
144 vpCombo[index].setVisible(true);
145 vpCombo[index].setViewportsEventHandler(viewportsEventHandler);
146 vpCombo[index].getViewport().setSelectionList(selectionList);
147 desktop.add(vpCombo[index].getJInternalFrame());
148 if (base == null){
149 //System.out.println("J3DBase in ViewportManager is null.");
150 throw new NullPointerException("J3DBase in ViewportManager is null.");
151 }
152 /*
153 if (vpCombo[index].getViewport() == null){
154 System.out.println("Viewport in vpCombo["+index+"] is null.");
155 }*/
156 vpCombo[index].getViewport().setJ3DRootBGForPicking(base.getJ3DRootBranchGroup());
157 }
158
159
160
161 /***
162 * Returns the number of active viewports
163 */
164 public int getNumberOfViewports(){
165 int count = 0;
166 for (int i=0; i < vpCombo.length; i++) {
167 if (vpCombo[i] != null){
168 count++;
169 }
170 }
171 return count;
172 }
173
174
175 /***
176 * Returns a list of active ViewportFrames
177 */
178 public List getListOfFrames(){
179 List list = new Vector();
180 for (int i=0; i < vpCombo.length; i++) {
181 if (vpCombo[i] != null){
182 list.add(vpCombo[i]);
183 }
184 }
185 return list;
186 }
187
188
189
190
191
192 public void setViewportLocation(int viewportIndex, boolean internal,
193 int x, int y, int width, int height){
194 if(viewportIndex>= vpCombo.length) return;
195 if(vpCombo[viewportIndex]==null) createViewportInIndex(viewportIndex,base.getDefaultView());
196
197 vpCombo[viewportIndex].setInternalFrame(internal);
198 vpCombo[viewportIndex].setPosition(x,y);
199 vpCombo[viewportIndex].setSize(width,height);
200 vpCombo[viewportIndex].setVisible(true);
201 }
202
203 /***
204 * Method from SceneObjectsChangeListener interface. ViewportWindowManager will
205 * only care about addition/removal of VcView objects. Then it will propagate
206 * the changes to the viewports.
207 *
208 * @param event signals changes in the scene graph.
209 */
210 public void sceneObjectsChanged(SceneObjectsChangedEvent event){
211 if(event instanceof ObjectAddedEvent){
212 Object obj = ((ObjectAddedEvent)event).getObject();
213 // only VcViews will interest us now.
214 if(!(obj instanceof VcView)) return;
215 /* Checks whether the new view is already in the view list.
216 This is necessary as the list of views belonging to viewportCommandPane
217 points to the same object as vcViews. Thus, it might be that the new view
218 has already been added to the list inside viewportCommandPane.
219 */
220 if(!vcViews.contains(obj)){
221 vcViews.add(obj);
222 }
223 //
224 ((VcView)obj).addChangeListener(this);
225 for (int i=0; i<vpCombo.length; i++){
226 if (vpCombo[i] != null){
227 // vpCombo[i].updateListOfViews(vcViews);
228 vpCombo[i].addView((VcView)obj);
229 }
230 }
231 }
232 if(event instanceof ObjectRemovedEvent){
233 //debugln("__Removed object");
234 //this.printListOfViews();
235 Object obj = ((ObjectRemovedEvent)event).getObject();
236 if(!(obj instanceof VcView)) return;
237 //debugln("____it is a view");
238 vcViews.remove(obj);
239 ((VcView)obj).removeChangeListener(this);
240 //this.printListOfViews();
241 for (int i=0; i<vpCombo.length; i++){
242 if (vpCombo[i] != null){
243 //vpCombo[i].updateListOfViews(vcViews);
244 vpCombo[i].removeView((VcView)obj);
245 }
246 }
247 }
248 }
249
250 /***
251 * Method from VcObjectChangeListener interface. Necessary to filter changes
252 * made to VcView objects and propagate them to viewport windows.
253 *
254 * @param event Signals changes made to objects in the scene graph.
255 */
256 public void vcObjectChanged(VcObjectEvent event){
257 // we will only care about changes made to VcView objects.
258 if(!(event.getSource() instanceof VcView))return;
259 VcView tempView = (VcView)event.getSource();
260 if(event instanceof VcLabelChangedEvent){
261 // handles changes of camera label
262 VcLabelChangedEvent lce = (VcLabelChangedEvent) event;
263 String newLabel = lce.getNewLabel();
264 String oldLabel = lce.getOldLabel();
265 ViewportCombo tempCombo;
266 for (int i=0; i < vpCombo.length; i++){
267 tempCombo = vpCombo[i];
268 if (tempCombo != null){
269 // if the view shown by the combo is the view which changed label, we need
270 // to update the title of the frame.
271 if(tempCombo.getViewport().getView()==tempView) tempCombo.setTitle(newLabel);
272 // changes the label in each ViewportCommandPane
273 vpCombo[i].getViewportCommandPane().replaceLabel(newLabel,oldLabel);
274 }
275 }
276 }
277 }
278
279
280 public void printListOfViews(){
281 //List tempList = base.getViewList();
282 Iterator iter = vcViews.iterator();
283 debugln("Printing list of views");
284 while (iter.hasNext()){
285 VcView view = (VcView)iter.next();
286 debugln(view.getLabel());
287 }
288 }
289
290 void debugln(String s){
291 if (GUIControl.debugflag){
292 System.out.println(s);
293 }
294 }
295
296 /***
297 * Returns a reference to a viewport given its name. If there is no viewport whose
298 * name coincides with the input parameter the method returns null;
299 *
300 * @param viewName name of the view to be retrieved.
301 * @return a reference to the viewport if it exists, null otherwise.
302 */
303 public Viewport getViewportByLabel(String viewName){
304 Viewport tempViewport;
305 for (int i=0; i<vpCombo.length; i++){
306 tempViewport = vpCombo[i].getViewport();
307 VcView tempView = tempViewport.getView();
308 if (tempView.getLabel().equals(viewName)){
309 return tempViewport;
310 }
311 }
312 // If we arrive at this point and still haven't found the viewport whose
313 // view is named viewName we return a null
314 return null;
315 }
316
317
318 public void enableMessageInAllViewports(String s){
319 Viewport tempViewport;
320 for (int i=0; i<vpCombo.length; i++){
321 if (vpCombo[i] != null){
322 tempViewport = vpCombo[i].getViewport();
323 if (tempViewport!=null){
324 tempViewport.enableMessage(s);
325 }
326 }
327 }
328 }
329
330 public void setCursorInViewports(TransformType type){
331 Viewport tempViewport;
332 for (int i=0; i<vpCombo.length; i++){
333 if (vpCombo[i] != null){
334 tempViewport = vpCombo[i].getViewport();
335 if (tempViewport!=null){
336 tempViewport.getCanvas3D().setCursor(CursorManager.getCursorManager().getTransformationCursor(type));
337 }
338 }
339 }
340 }
341
342 public void disableMessageInAllViewports(){
343 Viewport tempViewport;
344 for (int i=0; i<vpCombo.length; i++){
345 if (vpCombo[i] != null){
346 tempViewport = vpCombo[i].getViewport();
347 if (tempViewport!=null){
348 tempViewport.disableMessage();
349 }
350 }
351 }
352
353 }
354
355
356 /*
357 * Sometimes, when objects are being edited, it is useful to display the
358 * relevant axes on the screen.
359 * This method allows us to specify a center point, and 3 additional points
360 * to allow the definition of 3 axis
361 */
362 void setAxisPoints(Point3d center, Point3d x, Point3d y, Point3d z){
363 // Tells each visible Canvas3D (i.e. Viewport) of the axis points
364
365
366 // BEGINNING OF DUMMY TEST CODE
367 /*
368 Point3d tempCenter = new Point3d(1.0, 1.0, 1.0);
369 Point3d tempX = new Point3d(10.0, 1.0, 1.0);
370 Point3d tempY = new Point3d(1.0, 10.0, 1.0);
371 Point3d tempZ = new Point3d(1.0, 1.0, 10.0);
372 for (int i=0; i < vpCombo.length; i++){
373 if (vpCombo[i] != null){
374 Viewport vp = vpCombo[i].getViewport();
375 if (vp != null){
376 //vpCombo[i].getViewport().setAxisPoints(tempCenter, tempX, tempY, tempZ);
377 }
378 }
379 }*/
380 // END OF DUMMY TEST CODE
381
382 }
383
384
385 /*
386 * Allow the user to specify the length of axes drawn on screen
387 */
388 void setDrawnAxisLength(int length){
389 this.axisLength = length;
390 // Tells each of the Viewports of the new axis length
391 }
392
393 /*
394 * Enables/disables the displaying of xyz axis on Viewports
395 */
396 void setAxisVisible(boolean visible){
397
398 }
399
400 /*
401 * Enables/disables picking on Viewports
402 */
403 public void setPickingInViewports(boolean pickEnable){
404 for (int i=0; i < vpCombo.length; i++){
405 if (vpCombo[i] != null){
406 Viewport vp = vpCombo[i].getViewport();
407 if (vp != null){
408 vp.setPickingEnable(pickEnable);
409 }
410 }
411 }
412 }
413
414 public void setSelectionList(SelectionList list){
415 this.selectionList = list;
416 }
417
418 /***
419 * Sets the type of transformation displayed by all ViewportContextMenus.
420 * @param type type of the selected operation.
421 * @param mode mode of the selected operation.
422 * @param scope scope affected by selected operation
423 * @param viewSelected tells the context menus that a view has been selected,
424 * in which case non uniform scaling must be disabled.
425 */
426 void setTransformationTypeInContextMenus(TransformType type, TransformMode mode,
427 TransformScope axis, boolean viewSelected){
428 for (int i=0; i < vpCombo.length; i++){
429 if (vpCombo[i] != null){
430 ViewportContextMenu contextMenu = vpCombo[i].getViewportContextMenu();
431 contextMenu.setIndications(type, mode, axis, viewSelected);
432 }
433 }
434 }
435
436 }
This page was automatically generated by Maven