Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/trapezium/chisel/ChiselWorkspace.java


1   /*  ChiselWorkspace
2    *
3    */
4   
5   package com.trapezium.chisel;
6   
7   import java.awt.*;
8   import java.awt.event.*;
9   import java.util.*;
10  
11  import com.trapezium.factory.FactoryResponseListener;
12  import com.trapezium.factory.FactoryData;
13  import com.trapezium.chisel.gui.ScrollablePane;
14  import com.trapezium.chisel.gui.WorkspaceListener;
15  import com.trapezium.chisel.gui.ChiselAWTViewer;
16  
17  
18  /** A ChiselWorkspace holds an arbitrary number of ChiselViewers */
19  public class ChiselWorkspace extends ChiselPane implements FactoryResponseListener {
20  
21      ViewerVector viewers;
22      int selectedViewer;
23  
24      int xOpen = 10;
25      int yOpen = 10;
26      int dxOpen = 16;
27      int dyOpen = 28;
28      int widthOpen = 320;
29      int heightOpen = 400;
30  
31      // listen for window events
32      WorkspaceListener workspaceListener;
33      // listener to chain to
34      WorkspaceListener nextListener = null;
35  
36      // the interface to the container
37      Workspace workspace;
38  
39      ChiselTableStack chiselTableStack;
40      public ChiselWorkspace(ChiselTableStack tables) {
41          super();
42          this.chiselTableStack = tables;
43  
44          // get the workspace interface.  This doesn't work with
45          // JFC.
46          workspace = new WorkspacePane();
47          setComponent((Component) workspace);
48  
49          viewers = new ViewerVector();
50          getComponent().setBackground(DEFAULT_WORKSPACECOLOR);
51          selectedViewer = -1;
52          workspaceListener = new ChiselWorkspaceListener();
53      }
54  
55      public void setErrorMarks( ProcessedFile pf, BitSet errorMarks, BitSet warningMarks, BitSet nonconformanceMarks ) {
56          ProcessedFileViewer pfv = getViewer( pf );
57          if ( pfv != null ) {
58              Component component = pfv.getComponent();
59              if ( component instanceof ChiselAWTViewer ) {
60                  ((ChiselAWTViewer)component).setErrorMarks( errorMarks, warningMarks, nonconformanceMarks );
61  //            pfv.setErrorMarks( errorMarks, warningMarks, nonconformanceMarks );
62              }
63          }
64      }
65  
66      public void paste() {
67          if ( workspace != null ) {
68              workspace.paste();
69          }
70      }
71  
72      public void cut() {
73          if ( workspace != null ) {
74              workspace.cut();
75          }
76      }
77  
78      public void copy() {
79          if ( workspace != null ) {
80              workspace.copy();
81          }
82      }
83  
84      public void undo() {
85          if ( workspace != null ) {
86              workspace.undo();
87          }
88      }
89  
90      public void redo() {
91          if ( workspace != null ) {
92              workspace.redo();
93          }
94      }
95  
96      public void nextError() {
97          if ( workspace != null ) {
98              workspace.nextError();
99          }
100     }
101 
102     public void prevError() {
103         if ( workspace != null ) {
104             workspace.prevError();
105         }
106     }
107 
108     public void setSelection(ProcessedFile file) {
109         setSelection(viewers.findComponentFor(file));
110     }
111 
112     public void setSelection(String url) {
113         setSelection(viewers.findComponentFor(url));
114     }
115 
116     public void setSelection(Component c) {
117         int index = viewers.findIndexFor(c);
118         if (index != selectedViewer) {
119             Component old;
120             if (selectedViewer >= 0 && selectedViewer < viewers.size()) {
121                 old = ((ProcessedFileViewer) viewers.elementAt(selectedViewer)).getComponent();
122             } else {
123                 old = null;
124             }
125             if (old != null && old != c && old instanceof ChiselAWTViewer) {
126                 ((ChiselAWTViewer)old).setActivated(false);
127             }
128             workspace.setSelection(c);
129             if (c != null && c != old && c instanceof ChiselAWTViewer) {
130                 ((ChiselAWTViewer)c).setActivated(true);
131             }
132             selectedViewer = index;
133         }
134     }
135 
136     public Component getSelection() {
137         return workspace.getSelection(); //selectedViewer >= 0 ? ((ProcessedFileViewer) viewers.elementAt(selectedViewer)).getComponent() : null;
138     }
139 
140     public void close(Component c) {
141         int index = viewers.findIndexFor( c );
142         viewers.removeElementAt(index);
143         workspace.close(c);
144         if (index == selectedViewer) {
145             Component sel = workspace.getSelection();
146             selectedViewer = viewers.findIndexFor(sel);
147             if ( sel != null && sel instanceof ChiselAWTViewer ) {
148                 ((ChiselAWTViewer)sel).setActivated(true);
149             }
150             chiselTableStack.close();
151 //            chiselTableStack.updateHeaderLine();
152         } 
153     }
154 
155     public void moveToFront(Component c) {
156         workspace.moveToFront(c);
157     }
158 
159     public void moveToBack(Component c) {
160         workspace.moveToBack(c);
161     }
162 
163 
164     class WorkspacePane extends ScrollablePane {
165 
166         public WorkspacePane() {
167             super();
168             setLayout(null);
169             setScrollMode(DYNAMIC);
170         }
171 
172         public void doLayout() {
173             super.doLayout();
174             boolean max = false;
175             Dimension size = getSize();
176             Enumeration enum = viewers.elements();
177             while (enum.hasMoreElements()) {
178                 ProcessedFileViewer v = (ProcessedFileViewer) enum.nextElement();
179                 Component c = v.getComponent();
180                 if (c instanceof ChiselAWTViewer && ((ChiselAWTViewer) c).isMaximized()) {
181                     ((ChiselAWTViewer) c).maximize(true);
182                 }
183             }
184         }
185 
186         // override to disable scroll when a child is maximized
187         public void setScrollValues() {
188             boolean enable = true;
189             if (getComponentCount() > 0 && viewers != null) {
190                 Enumeration enum = viewers.elements();
191                 while (enum.hasMoreElements()) {
192                     ProcessedFileViewer v = (ProcessedFileViewer) enum.nextElement();
193                     Component c = v.getComponent();
194                     if (c instanceof ChiselAWTViewer && ((ChiselAWTViewer) c).isMaximized()) {
195                         enable = false;
196                         break;
197                     }
198                 }
199 
200             } else {
201                 enable = false;
202             }
203             enableScroll(enable, enable);
204             super.setScrollValues();
205         }
206     }
207 
208     class ChiselWorkspaceListener implements WorkspaceListener {
209         public void windowOpened(WindowEvent evt) {
210             //System.out.println("CWL gets windowOpened");
211             if (nextListener != null) {
212                 //System.out.println("OK!!!");
213                 nextListener.windowOpened(evt);
214             }
215         }
216         public void windowClosing(WindowEvent evt) {
217             //System.out.println("CWL gets windowClosing");
218             if (nextListener != null) {
219                 //System.out.println("OK!!!");
220                 nextListener.windowClosing(evt);
221             }
222         }
223         public void windowClosed(WindowEvent evt) {
224             //System.out.println("CWL gets windowClosed");
225             close(evt.getComponent());
226             if (nextListener != null) {
227                 nextListener.windowClosed(evt);
228             }
229         }
230         public void windowIconified(WindowEvent evt) {
231             //System.out.println("CWL gets windowIconified");
232             if (nextListener != null) {
233                 nextListener.windowIconified(evt);
234             }
235         }
236         public void windowDeiconified(WindowEvent evt) {
237             //System.out.println("CWL gets windowDeiconified");
238             if (nextListener != null) {
239                 nextListener.windowDeiconified(evt);
240             }
241         }
242         public void windowActivated(WindowEvent evt) {
243             //System.out.println("CWL gets windowActivated");
244             if (nextListener != null) {
245                 nextListener.windowActivated(evt);
246             }
247         }
248         public void windowDeactivated(WindowEvent evt) {
249             //System.out.println("CWL gets windowDeactivated");
250             if (nextListener != null) {
251                 nextListener.windowDeactivated(evt);
252             }
253         }
254         public void windowMaximized(WindowEvent evt) {
255             //System.out.println("CWL gets windowMaximized");
256             if (nextListener != null) {
257                 nextListener.windowMaximized(evt);
258             }
259         }
260         public void windowDemaximized(WindowEvent evt) {
261             //System.out.println("CWL gets windowDemaximized");
262             if (nextListener != null) {
263                 nextListener.windowDemaximized(evt);
264             }
265         }
266     }
267 
268 
269     public synchronized void addWorkspaceListener(WorkspaceListener l) {
270         //workspaceListener = AWTEventMulticaster.add(workspaceListener, l);
271         nextListener = l;
272     }
273 
274     /** determine if a file is currently being viewed */
275     public boolean contains(ProcessedFile data) {
276         Enumeration enum = viewers.elements();
277         while (enum.hasMoreElements()) {
278             ProcessedFileViewer v = (ProcessedFileViewer) enum.nextElement();
279             if (v.getProcessedFile() == data) {
280                 return true;
281             }
282         }
283         return false;
284   }
285 
286     public void open(ProcessedFile data) {
287         ProcessedFileViewer v = new ChiselViewer();
288         v.setProcessedFile(data);
289         add(v);
290         //data.setDoneListener( this );
291         setSelection(v.getComponent());
292     }
293 
294 
295     protected void setNextViewerBounds(Component viewer) {
296         Dimension size = getComponent().getSize();
297         if (xOpen > size.width - dxOpen) {
298            xOpen = 10;
299         }
300         if (yOpen > size.height - dyOpen) {
301            yOpen = 10;
302         }
303         viewer.setBounds(xOpen, yOpen, widthOpen, heightOpen);
304         xOpen += dxOpen;
305         yOpen += dyOpen;
306     }
307 
308 
309   public void add(ProcessedFileViewer viewer) {
310       //selectedViewer = viewers.size();
311         viewers.addElement(viewer);
312       Container container = getContainer();
313       if (container != null) {
314           Component viewerComponent = viewer.getComponent();
315             setNextViewerBounds(viewerComponent);
316           container.add(viewerComponent);
317           //viewerComponent.addMouseListener(clicker);
318 
319             if (workspaceListener != null && viewerComponent instanceof ChiselAWTViewer) {
320                 ((ChiselAWTViewer) viewerComponent).addWorkspaceListener(workspaceListener);
321             }
322 
323             // without this for some reason the left and right edges don't show up
324           viewerComponent.repaint();
325       }
326     }
327 
328   public void remove(ProcessedFileViewer viewer) {
329         viewers.removeElement(viewer);
330       Container container = getContainer();
331       if (container != null) {
332           container.remove(viewer.getComponent());
333       }
334     }
335 
336     /** update the display if the object belongs to this viewer or is null */
337     public void fileUpdated(ProcessedFile data) {
338         Enumeration enum = viewers.elements();
339         while (enum.hasMoreElements()) {
340             ProcessedFileViewer v = (ProcessedFileViewer) enum.nextElement();
341             v.fileUpdated(data);
342         }
343     }
344 
345     /** load an object into the viewer */
346     public void done(FactoryData data) {
347 //        System.out.println("WORKSPACE DONE!");
348         Enumeration enum = viewers.elements();
349         while (enum.hasMoreElements()) {
350             ProcessedFileViewer v = (ProcessedFileViewer) enum.nextElement();
351             if (v.getProcessedFile().getId() == data.getId()) {
352                 v.fileDone(v.getProcessedFile());
353 //                System.out.println("WS calls v.fileDone!");
354             }
355         }
356   }
357 
358   public ProcessedFileViewer getViewer( ProcessedFile pf ) {
359       Enumeration enum = viewers.elements();
360       while ( enum.hasMoreElements() ) {
361           ProcessedFileViewer v = (ProcessedFileViewer) enum.nextElement();
362           if ( v.getProcessedFile().getId() == pf.getId() ) {
363               return( v );
364           }
365       }
366       return( null );
367   }
368 
369   public void update( FactoryData result ) {}
370   public void setNumberOfLines( int n ) {}
371   public void setPolygonCount( int n ) {}
372   public void setAction( String action ) {}
373   public void setText( String text ) {}
374   public void setFactoryData( FactoryData fd ) {}
375   public void setLinePercent( int n ) {}
376 
377   /** save the current document.  Prompts for filename if the document was not loaded
378         from disk or previously saved. */
379     public void save() {
380     }
381 
382     /** remove the current document from the viewer and display an empty document */
383     public void empty() {
384     }
385 
386     /** get the ProcessedFile being viewed */
387     public ProcessedFile getProcessedFile() {
388         if (selectedViewer >= 0) {
389             return ((ProcessedFileViewer) viewers.elementAt(selectedViewer)).getProcessedFile();
390         } else {
391             return null;
392         }
393     }
394 
395     /** set the ProcessedFile being viewed */
396     public void setProcessedFile(ProcessedFile data) {
397         if (selectedViewer >= 0) {
398             ((ProcessedFileViewer) viewers.elementAt(selectedViewer)).setProcessedFile(data);
399         }
400     }
401 
402     /** get the ProcessedFile being viewed */
403     public ProcessedFileViewer getActiveViewer() {
404         if (selectedViewer >= 0) {
405             return (ProcessedFileViewer) viewers.elementAt(selectedViewer);
406         } else {
407             return null;
408         }
409     }
410 
411 
412     /** The default layout behavior for a workspace leaves all the windows where they are. */
413     public void layoutContainer(Container target) {
414     }
415 
416 
417     /** dump the current document to the console */
418     public void dump() {
419         System.out.println("ChiselWorkspace containing " + viewers.size() + " viewers, #" + selectedViewer + " selected.");
420     }
421 }
422 
423 class ViewerVector extends Vector {
424     public boolean containsViewer(ProcessedFileViewer viewer) {
425         if (contains(viewer)) {
426             return true;
427         } else if (viewer != viewer.getComponent() && contains(viewer.getComponent())) {
428             return true;
429         } else {
430             return false;
431         }
432     }
433     public void removeViewer(ProcessedFileViewer viewer) {
434         Component comp = viewer.getComponent();
435         if (viewer == comp) {
436             viewer = findViewerFor(comp);
437         }
438         removeElement(viewer);
439     }
440 
441     public int findIndexFor(Component comp) {
442         int num = size();
443         for (int index = 0; index < num; index++) {
444             ProcessedFileViewer viewer = (ProcessedFileViewer) elementAt(index);
445             if (viewer.getComponent() == comp) {
446                 return index;
447             }
448         }
449         return -1;
450     }
451 
452     public ProcessedFileViewer findViewerFor(Component comp) {
453         Enumeration enum = elements();
454         while (enum.hasMoreElements()) {
455             ProcessedFileViewer viewer = (ProcessedFileViewer) enum.nextElement();
456             if (viewer.getComponent() == comp) {
457                 return viewer;
458             }
459         }
460         return null;
461     }
462     public Component findComponentFor(ProcessedFile data) {
463         Enumeration enum = elements();
464         while (enum.hasMoreElements()) {
465             ProcessedFileViewer viewer = (ProcessedFileViewer) enum.nextElement();
466             if (viewer != null && viewer.getProcessedFile() == data) {
467                 return viewer.getComponent();
468             }
469         }
470         return null;
471     }
472 
473     /** return the first component viewing a particular file, or null if
474       * not found.
475       */
476     public Component findComponentFor(String url) {
477         Enumeration enum = elements();
478         while (enum.hasMoreElements()) {
479             ProcessedFileViewer viewer = (ProcessedFileViewer) enum.nextElement();
480             ProcessedFile file = viewer.getProcessedFile();
481             if (file != null && file.getUrl().equals(url)) {
482                 return viewer.getComponent();
483             }
484         }
485         return null;
486     }
487 }
488