KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > graph > view > BasicGraphView


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.graph.view;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.graph.model.Display;
29 import org.objectweb.fractal.gui.graph.model.DisplayListener;
30 import org.objectweb.fractal.gui.graph.model.GraphModel;
31 import org.objectweb.fractal.gui.graph.model.GraphModelListener;
32 import org.objectweb.fractal.gui.graph.model.Rect;
33 import org.objectweb.fractal.gui.graph.model.Tools;
34 import org.objectweb.fractal.gui.model.ClientInterface;
35 import org.objectweb.fractal.gui.model.Component;
36 import org.objectweb.fractal.gui.model.Configuration;
37 import org.objectweb.fractal.gui.model.ConfigurationListener;
38 import org.objectweb.fractal.gui.model.Interface;
39 import org.objectweb.fractal.gui.model.ServerInterface;
40 import org.objectweb.fractal.gui.selection.model.Selection;
41 import org.objectweb.fractal.gui.selection.model.SelectionListener;
42 import org.objectweb.fractal.gui.admin.model.AdminModel;
43 import org.objectweb.fractal.gui.admin.model.AdminModelListener;
44 import org.objectweb.fractal.swing.JPanelImpl;
45
46 import cwi.SVGGraphics.SVGGraphics;
47
48 import java.awt.AWTEvent JavaDoc;
49 import java.awt.Color JavaDoc;
50 import java.awt.Font JavaDoc;
51 import java.awt.Graphics JavaDoc;
52 import java.awt.Graphics2D JavaDoc;
53 import java.awt.Image JavaDoc;
54 import java.awt.Point JavaDoc;
55 import java.awt.Rectangle JavaDoc;
56 import java.awt.RenderingHints JavaDoc;
57 import java.awt.Shape JavaDoc;
58 import java.awt.print.Printable JavaDoc;
59 import java.awt.print.PageFormat JavaDoc;
60 import java.awt.print.Book JavaDoc;
61 import java.awt.print.PrinterJob JavaDoc;
62 import java.awt.print.PrinterException JavaDoc;
63 import java.awt.event.ComponentAdapter JavaDoc;
64 import java.awt.event.ComponentEvent JavaDoc;
65 import java.awt.event.MouseEvent JavaDoc;
66 import java.util.List JavaDoc;
67 import java.util.GregorianCalendar JavaDoc;
68 import java.util.Calendar JavaDoc;
69 import java.util.Map JavaDoc;
70 import java.util.HashMap JavaDoc;
71 import java.util.Iterator JavaDoc;
72
73 /**
74  * A configuration view that displays configurations as graph of boxes and
75  * lines, representing components and bindings. This component does not draw the
76  * components and bindings itself: it uses auxiliary component renderer and
77  * binding renderer components for that. In fact this component just calls these
78  * components for each direct and indirect sub component of the configuration's
79  * root component. The mouse events are not handled by this component: they are
80  * just dispatched to the listeners of this component.
81  */

82
83 public class BasicGraphView extends JPanelImpl implements
84   DisplayListener,
85   AdminModelListener,
86   GraphModelListener,
87   ConfigurationListener,
88   SelectionListener,
89   Printer,
90   Printable JavaDoc,
91   BindingController
92 {
93
94   /**
95    * A mandatory client interface bound to a {@link Configuration configuration}
96    * model.
97    */

98
99   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
100
101   /**
102    * A mandatory client interface bound to a {@link GraphModel graph} model.
103    */

104
105   public final static String JavaDoc GRAPH_BINDING = "graph";
106
107   /**
108    * An optional client interface bound to a {@link Selection selection} model.
109    */

110
111   public final static String JavaDoc SELECTION_BINDING = "selection";
112
113   /**
114    * An optional client interface bound to a {@link Display display} model.
115    */

116
117   public final static String JavaDoc DISPLAY_BINDING = "display";
118
119   /**
120    * An optional client interface bound to an {@link AdminModel admin} model.
121    */

122   public final static String JavaDoc ADMIN_BINDING = "admin";
123   
124   /**
125    * An optional client interface boudn to a {@link Tools tools} model.
126    */

127
128   public final static String JavaDoc TOOLS_BINDING = "tools";
129
130   /**
131    * A mandatory client interface bound to a {@link ComponentRenderer}.
132    */

133
134   public final static String JavaDoc COMPONENT_RENDERER_BINDING = "component-renderer";
135
136   /**
137    * A mandatory client interface bound to a {@link BindingRenderer}.
138    */

139
140   public final static String JavaDoc BINDING_RENDERER_BINDING = "binding-renderer";
141
142   /**
143    * A collection client interface bound to the {@link GraphViewListener
144    * listeners} of this view component.
145    */

146
147   public final static String JavaDoc GRAPH_VIEW_LISTENERS_BINDING =
148     "graph-view-listeners";
149
150   /**
151    * The configuration client interface.
152    */

153
154   private Configuration configuration;
155
156   /**
157    * The graph model client interface.
158    */

159
160   private GraphModel graphModel;
161
162   /**
163    * The selection client interface.
164    */

165
166   private Selection selection;
167
168   /**
169    * The display client interface.
170    */

171
172   private Display display;
173
174   /**
175    * The admin model client interface.
176    */

177   
178   private AdminModel admin;
179   
180   /**
181    * The tools client interface.
182    */

183
184   private Tools tools;
185
186   /**
187    * The component renderer client interface.
188    */

189
190   private ComponentRenderer cRenderer;
191
192   /**
193    * The binding renderer client interface.
194    */

195
196   private BindingRenderer bRenderer;
197
198   /**
199    * The listeners component interface.
200    */

201
202   private Map JavaDoc graphViewListeners;
203
204   private int w, h, xo, yo;
205
206   private int wp, hp, xp, yp;
207
208   private boolean printInProgress = false;
209
210   private Component ctp; // Component To Print
211

212   private Image JavaDoc offScreen;
213
214   private boolean dirty;
215
216   private Point JavaDoc bindPoint;
217
218   private int typePrint;
219
220   static String JavaDoc UN = new String JavaDoc (System.getProperty("user.name"));
221
222   static final String JavaDoc [] mois = {
223     "Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.",
224     "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."};
225
226   /**
227    * Constructs a new {@link BasicGraphView} component.
228    */

229
230   public BasicGraphView () {
231     graphViewListeners = new HashMap JavaDoc();
232     enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
233     addComponentListener(
234       new ComponentAdapter JavaDoc() {
235         public void componentResized (final ComponentEvent JavaDoc e) {
236           w = e.getComponent().getSize().width;
237           h = e.getComponent().getSize().height;
238           xo = 0;
239           yo = 0;
240           if (w > 0 && h > 0) {
241             if (display != null) {
242 // display.setScreenSize(new Rectangle(0, 0, w, h));
243
display.setScreenSize(new Rectangle JavaDoc(xo, yo, w, h));
244             }
245             offScreen = createOffscreenImage(w, h);
246             redraw();
247           }
248         }
249       });
250   }
251
252   // -------------------------------------------------------------------------
253
// Implementation of the UserBindingController interface
254
// -------------------------------------------------------------------------
255

256   public String JavaDoc[] listFc () {
257     int size = graphViewListeners.size();
258     String JavaDoc[] names = new String JavaDoc[size + 8];
259     graphViewListeners.keySet().toArray(names);
260     names[size] = COMPONENT_RENDERER_BINDING;
261     names[size + 1] = BINDING_RENDERER_BINDING;
262     names[size + 2] = DISPLAY_BINDING;
263     names[size + 3] = TOOLS_BINDING;
264     names[size + 4] = SELECTION_BINDING;
265     names[size + 5] = CONFIGURATION_BINDING;
266     names[size + 6] = GRAPH_BINDING;
267     names[size + 7] = ADMIN_BINDING;
268     return names;
269   }
270
271   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
272     if (COMPONENT_RENDERER_BINDING.equals(clientItfName)) {
273       return cRenderer;
274     } else if (BINDING_RENDERER_BINDING.equals(clientItfName)) {
275       return bRenderer;
276     } else if (DISPLAY_BINDING.equals(clientItfName)) {
277       return display;
278     } else if (ADMIN_BINDING.equals(clientItfName)) {
279       return admin;
280     } else if (TOOLS_BINDING.equals(clientItfName)) {
281       return tools;
282     } else if (SELECTION_BINDING.equals(clientItfName)) {
283       return selection;
284     } else if (CONFIGURATION_BINDING.equals(clientItfName)) {
285       return configuration;
286     } else if (GRAPH_BINDING.equals(clientItfName)) {
287       return graphModel;
288     } else if (clientItfName.startsWith(GRAPH_VIEW_LISTENERS_BINDING)) {
289       return graphViewListeners.get(clientItfName);
290     }
291     return null;
292   }
293
294   public void bindFc (
295     final String JavaDoc clientItfName,
296     final Object JavaDoc serverItf)
297   {
298     if (COMPONENT_RENDERER_BINDING.equals(clientItfName)) {
299       cRenderer = (ComponentRenderer)serverItf;
300     } else if (BINDING_RENDERER_BINDING.equals(clientItfName)) {
301       bRenderer = (BindingRenderer)serverItf;
302     } else if (DISPLAY_BINDING.equals(clientItfName)) {
303       display = (Display)serverItf;
304     } else if (ADMIN_BINDING.equals(clientItfName)) {
305       admin = (AdminModel)serverItf;
306     } else if (TOOLS_BINDING.equals(clientItfName)) {
307       tools = (Tools)serverItf;
308     } else if (SELECTION_BINDING.equals(clientItfName)) {
309       selection = (Selection)serverItf;
310     } else if (CONFIGURATION_BINDING.equals(clientItfName)) {
311       configuration = (Configuration)serverItf;
312     } else if (GRAPH_BINDING.equals(clientItfName)) {
313       graphModel = (GraphModel)serverItf;
314     } else if (clientItfName.startsWith(GRAPH_VIEW_LISTENERS_BINDING)) {
315       graphViewListeners.put(clientItfName, serverItf);
316     }
317   }
318
319   public void unbindFc (final String JavaDoc clientItfName) {
320     if (COMPONENT_RENDERER_BINDING.equals(clientItfName)) {
321       cRenderer = null;
322     } else if (BINDING_RENDERER_BINDING.equals(clientItfName)) {
323       bRenderer = null;
324     } else if (DISPLAY_BINDING.equals(clientItfName)) {
325       display = null;
326     } else if (ADMIN_BINDING.equals(clientItfName)) {
327       admin = null;
328     } else if (TOOLS_BINDING.equals(clientItfName)) {
329       tools = null;
330     } else if (SELECTION_BINDING.equals(clientItfName)) {
331       selection = null;
332     } else if (CONFIGURATION_BINDING.equals(clientItfName)) {
333       configuration = null;
334     } else if (GRAPH_BINDING.equals(clientItfName)) {
335       graphModel = null;
336     } else if (clientItfName.startsWith(GRAPH_VIEW_LISTENERS_BINDING)) {
337       graphViewListeners.remove(clientItfName);
338     }
339   }
340
341   // -------------------------------------------------------------------------
342
// Implementation of the DisplayListener interface
343
// -------------------------------------------------------------------------
344

345   public void displayedAreaChanged (final Rect oldValue) {
346     redraw();
347   }
348
349   public void antialiasingChanged () {
350     redraw();
351   }
352
353   public void maxDepthChanged () {
354     redraw();
355   }
356
357   public void itfNameDisplayModeChanged () {
358     redraw();
359   }
360
361   // -------------------------------------------------------------------------
362
// Implementation of the AdminModelListener interface
363
// -------------------------------------------------------------------------
364

365   public void componentCreated (Component model) {
366     redraw();
367   }
368
369   public void componentDeleted (Component model) {
370     redraw();
371   }
372
373   public void componentStarted (Component model) {
374     redraw();
375   }
376
377   public void componentStopped (Component model) {
378     redraw();
379   }
380
381   // -------------------------------------------------------------------------
382
// Implementation of the SelectionListener interface
383
// -------------------------------------------------------------------------
384

385   public void selectionChanged () {
386     redraw();
387   }
388
389   // -------------------------------------------------------------------------
390
// Implementation of the Printer interface
391
// -------------------------------------------------------------------------
392

393   public void setType (int ntype) {
394     typePrint = ntype;
395   }
396
397   public boolean print (final boolean ask, Component c) {
398
399     if (typePrint == Printer.TYPE_EXPORT) {
400       SVGGraphics svg = SVGGraphics.create (this);
401       if (svg == null) return false;
402       drawShapes(svg, c);
403       svg.close();
404       return true;
405     }
406
407     PrinterJob JavaDoc job = PrinterJob.getPrinterJob();
408     Book JavaDoc bk = new Book JavaDoc();
409     if (ask) bk.append (new PaintCover(), job.defaultPage());
410     ctp = c;
411
412     // Create a page format
413
PageFormat JavaDoc page = job.defaultPage();
414     page.setOrientation(PageFormat.LANDSCAPE);
415     xp = (int)page.getImageableX();
416     yp = (int)page.getImageableY()+30;
417     wp = w - 2*xp;
418     hp = h - 2*yp;
419 // wp = (int)page.getWidth() - 2*xp; résultat hideux !
420
// hp = (int)page.getHeight() - 2*yp;
421

422     bk.append (this, page);
423     if (c.getMasterComponent() != null) {
424       c = c.getMasterComponent();
425     }
426     if (selection != null) {
427       selection.selectComponent(c);
428     }
429     configuration.setRootComponent(c);
430
431     // Pass the book to the PrinterJob
432
job.setPageable(bk);
433
434     // Put up the dialog box
435
if (ask) {
436       if (job.printDialog()) imprime (job);
437       else return false;
438     } else {
439       imprime (job);
440     }
441     return true;
442   }
443
444   private void imprime (final PrinterJob JavaDoc job) {
445     try {
446
447       int oldxo = xo;
448       int oldyo = yo;
449       int oldw = w;
450       int oldh = h;
451       xo = xp; yo = yp; w = wp; h = hp;
452
453       printInProgress = true;
454       job.print();
455
456       xo = oldxo;
457       yo = oldyo;
458       w = oldw;
459       h = oldh;
460
461       printInProgress = false;
462     } catch (Exception JavaDoc exc) {
463       exc.printStackTrace(); /* Handle Exception */
464     }
465   }
466
467   // -------------------------------------------------------------------------
468
// Implementation of the Printable interface
469
// -------------------------------------------------------------------------
470

471   public int print (final Graphics g, final PageFormat JavaDoc pf, final int pageIndex)
472     throws PrinterException JavaDoc
473   {
474     drawShapes((Graphics2D JavaDoc) g, ctp);
475     return Printable.PAGE_EXISTS;
476   }
477
478   // -------------------------------------------------------------------------
479
// Implementation of the ConfigurationListener interface
480
// -------------------------------------------------------------------------
481

482   public void changeCountChanged (Component component, long changeCount) {
483     // does nothing
484
}
485
486   public void rootComponentChanged (final Component oldValue) {
487     Iterator JavaDoc i = graphViewListeners.values().iterator();
488     while (i.hasNext()) {
489       GraphViewListener l = (GraphViewListener)i.next();
490       l.viewChanged();
491     }
492     redraw();
493   }
494
495   public void nameChanged (final Component component, final String JavaDoc oldValue) {
496     redraw();
497   }
498
499   public void typeChanged (final Component component, final String JavaDoc oldValue) {
500     redraw();
501   }
502
503   public void implementationChanged (
504     final Component component,
505     final String JavaDoc oldValue)
506   {
507     redraw();
508   }
509
510   public void interfaceNameChanged (
511     final Interface i,
512     final String JavaDoc oldValue)
513   {
514     redraw();
515   }
516
517   public void interfaceSignatureChanged (
518     final Interface i,
519     final String JavaDoc oldValue)
520   {
521     redraw();
522   }
523
524   public void interfaceContingencyChanged (
525     final Interface i,
526     final boolean oldValue)
527   {
528     redraw();
529   }
530
531   public void interfaceCardinalityChanged (
532     final Interface i,
533     final boolean oldValue)
534   {
535     redraw();
536   }
537
538   public void clientInterfaceAdded (
539     final Component component,
540     final ClientInterface i,
541     final int index)
542   {
543     redraw();
544   }
545
546   public void clientInterfaceRemoved (
547     final Component component,
548     final ClientInterface i,
549     final int index)
550   {
551     redraw();
552   }
553
554   public void serverInterfaceAdded (
555     final Component component,
556     final ServerInterface i,
557     final int index)
558   {
559     redraw();
560   }
561
562   public void serverInterfaceRemoved (
563     final Component component,
564     final ServerInterface i,
565     final int index)
566   {
567     redraw();
568   }
569
570   public void interfaceBound (
571     final ClientInterface citf,
572     final ServerInterface sitf) {
573    redraw();
574   }
575
576   public void interfaceRebound (
577     final ClientInterface citf,
578     final ServerInterface oldSitf)
579   {
580     redraw();
581   }
582
583   public void interfaceUnbound (
584     final ClientInterface citf,
585     final ServerInterface sitf)
586   {
587     redraw();
588   }
589
590   public void attributeControllerChanged (
591     final Component component,
592     final String JavaDoc oldValue)
593   {
594     redraw();
595   }
596
597   public void attributeChanged (
598     final Component component,
599     final String JavaDoc attributeName,
600     final String JavaDoc oldValue)
601   {
602     redraw();
603   }
604
605   public void templateControllerDescriptorChanged (
606     final Component component,
607     final String JavaDoc oldValue)
608   {
609     redraw();
610   }
611
612   public void componentControllerDescriptorChanged (
613     final Component component,
614     final String JavaDoc oldValue)
615   {
616     redraw();
617   }
618
619   public void subComponentAdded (
620     final Component parent,
621     final Component child,
622     final int index)
623   {
624     redraw();
625   }
626
627   public void subComponentRemoved (
628     final Component parent,
629     final Component child,
630     final int index)
631   {
632     redraw();
633   }
634
635   // -------------------------------------------------------------------------
636
// Implementation of the GraphModelListener interface
637
// -------------------------------------------------------------------------
638

639   public void componentColorChanged (
640     final Component component,
641     final Color JavaDoc oldColor)
642   {
643     if (configuration.getRootComponent().contains(component)) {
644       redraw();
645     }
646   }
647
648   public void componentPositionChanged (
649     final Component component,
650     final Rect oldValue)
651   {
652     if (configuration.getRootComponent().contains(component)) {
653       redraw();
654     }
655   }
656
657   // -------------------------------------------------------------------------
658
// Overriden JPanel methods
659
// -------------------------------------------------------------------------
660

661   protected void processMouseEvent (final MouseEvent JavaDoc e) {
662     if (configuration == null) {
663       return;
664     }
665     int x = e.getX();
666     int y = e.getY();
667     ComponentPart p = getComponentPart(x, y);
668
669     GraphViewListener l;
670     Iterator JavaDoc i = graphViewListeners.values().iterator();
671
672     boolean needRedraw = tools != null && tools.getTool() == Tools.BIND;
673
674     switch (e.getID()) {
675       case MouseEvent.MOUSE_PRESSED:
676         while (i.hasNext()) {
677           l = (GraphViewListener)i.next();
678           l.mousePressed(e, p);
679         }
680         break;
681
682       case MouseEvent.MOUSE_RELEASED:
683         while (i.hasNext()) {
684           l = (GraphViewListener)i.next();
685           l.mouseReleased(e, p);
686         }
687         break;
688
689       case MouseEvent.MOUSE_CLICKED:
690         while (i.hasNext()) {
691           l = (GraphViewListener)i.next();
692           l.mouseClicked(e, p);
693         }
694         break;
695
696       case MouseEvent.MOUSE_ENTERED:
697         while (i.hasNext()) {
698           l = (GraphViewListener)i.next();
699           l.mouseEntered(e);
700         }
701         break;
702
703       case MouseEvent.MOUSE_EXITED:
704         while (i.hasNext()) {
705           l = (GraphViewListener)i.next();
706           l.mouseExited(e);
707         }
708         break;
709
710       default:
711         break;
712     }
713
714     if (needRedraw) {
715       redraw();
716     }
717     super.processMouseEvent(e);
718   }
719
720   protected void processMouseMotionEvent (final MouseEvent JavaDoc e) {
721     if (configuration == null) {
722       return;
723     }
724
725     boolean needRedraw = tools != null && tools.getTool() == Tools.BIND;
726
727     Iterator JavaDoc i = graphViewListeners.values().iterator();
728     GraphViewListener l;
729     bindPoint = e.getPoint();
730
731     switch (e.getID()) {
732       case MouseEvent.MOUSE_DRAGGED:
733         while (i.hasNext()) {
734           l = (GraphViewListener)i.next();
735           l.mouseDragged(e);
736         }
737         break;
738
739       case MouseEvent.MOUSE_MOVED:
740         int x = e.getX();
741         int y = e.getY();
742         ComponentPart p = getComponentPart(x, y);
743         while (i.hasNext()) {
744           l = (GraphViewListener)i.next();
745           l.mouseMoved(e, p);
746         }
747         break;
748
749       default:
750         break;
751     }
752
753     if (needRedraw) {
754       redraw();
755     }
756
757     super.processMouseMotionEvent(e);
758   }
759
760   // ----- paint
761

762   public void paint (final Graphics g) {
763     if (offScreen != null) {
764       if (printInProgress) {
765         g.drawImage(offScreen, xo, yo, w, h, this);
766       } else {
767         g.drawImage(offScreen, xo, yo, w, h, this);
768       }
769     }
770   }
771
772   public void repaint () {
773     paint(getGraphics());
774   }
775
776   public void setVisible (final boolean visible) {
777     super.setVisible(visible);
778     if (visible && dirty) {
779       redraw();
780     }
781   }
782
783   // -------------------------------------------------------------------------
784
// Other methods
785
// -------------------------------------------------------------------------
786

787   Image JavaDoc createOffscreenImage (final int w, final int h) {
788     return createImage(w, h);
789   }
790
791   private ComponentPart getComponentPart (final int x, final int y) {
792     Component root = configuration.getRootComponent();
793     Rectangle JavaDoc r = new Rectangle JavaDoc(xo, yo, w, h);
794     if (display != null) {
795       r = getRectangle(r, display.getDisplayedArea());
796     } else {
797       r = getRectangle(r, new Rect(0.1, 0.1, 0.9, 0.9));
798     }
799
800     int depth = display == null ? 0 : display.getMaxDepth();
801     ComponentPart part = getComponentPart(root, r, x, y, depth);
802     if (part == null) {
803       part = new ComponentPart(null, null, 0, new Rectangle JavaDoc(xo, yo, w, h));
804     }
805     return part;
806   }
807
808   private ComponentPart getComponentPart (
809     final Component c,
810     final Rectangle JavaDoc r,
811     final int x,
812     final int y,
813     final int depth)
814   {
815     if (r.width <= 0 || r.height <= 0) {
816       return null;
817     }
818     if (!r.intersects(new Rectangle JavaDoc(xo, yo, w, h))) {
819       return null;
820     }
821
822     // tests the component itself
823
ComponentPart part = cRenderer.getComponentPart(c, r, depth > 0, x, y);
824
825     if (part != null && part.getPart() == ComponentPart.CONTENT) {
826       if (depth > 0 && c.isComposite()) {
827         // tests the sub-components, if any
828
List JavaDoc subComponents = c.getSubComponents();
829         for (int i = 0; i < subComponents.size(); ++i) {
830           Component subC = (Component)subComponents.get(i);
831           Rectangle JavaDoc subR = cRenderer.getSubComponentArea(c, r);
832           subR = getRectangle(subR, graphModel.getComponentPosition(subC));
833           ComponentPart subPart = getComponentPart(subC, subR, x, y, depth - 1);
834           if (subPart != null) {
835             return subPart;
836           }
837         }
838       }
839     }
840     return part;
841   }
842
843   synchronized void redraw () {
844     if (offScreen == null || configuration == null) {
845       return;
846     }
847     if (!isVisible()) {
848       dirty = true;
849       return;
850     }
851     dirty = false;
852
853     final Graphics2D JavaDoc g = (Graphics2D JavaDoc)offScreen.getGraphics();
854     g.setRenderingHint (
855       RenderingHints.KEY_ANTIALIASING,
856       display != null && display.isAntialiasing()
857       ? RenderingHints.VALUE_ANTIALIAS_ON
858       : RenderingHints.VALUE_ANTIALIAS_OFF);
859
860     g.setColor(Color.white);
861     g.fillRect(0, 0, w, h);
862
863     final Component root = configuration.getRootComponent();
864     Rectangle JavaDoc r = new Rectangle JavaDoc(xo, yo, w, h);
865     if (display != null) {
866       r = getRectangle(r, display.getDisplayedArea());
867     } else {
868       r = getRectangle(r, new Rect(0.1, 0.1, 0.9, 0.9));
869     }
870     drawComponent(g, root, r, display == null ? 0 : display.getMaxDepth());
871     repaint();
872   }
873
874
875   synchronized void drawShapes (final Graphics2D JavaDoc g, final Component c) {
876     if (offScreen == null || configuration == null) {
877       return;
878     }
879     if (!isVisible()) {
880       dirty = true;
881       return;
882     }
883     dirty = false;
884     g.setRenderingHint(
885       RenderingHints.KEY_ANTIALIASING,
886       display != null && display.isAntialiasing()
887       ? RenderingHints.VALUE_ANTIALIAS_ON
888       : RenderingHints.VALUE_ANTIALIAS_OFF);
889
890     g.setColor(Color.white);
891     g.fillRect(0, 0, w, h);
892
893 // Component root = configuration.getRootComponent();
894
Rectangle JavaDoc r = new Rectangle JavaDoc(xo, yo, w, h);
895     if (display != null) {
896       r = getRectangle(r, display.getDisplayedArea());
897     } else {
898       r = getRectangle(r, new Rect(0.1, 0.1, 0.9, 0.9));
899     }
900     drawComponent(g, c, r, display == null ? 0 : display.getMaxDepth());
901     paint (g);
902   }
903
904
905   private void drawComponent (
906     final Graphics g,
907     final Component c,
908     final Rectangle JavaDoc r,
909     final int depth)
910   {
911     if (r.width <= 0 || r.height <= 0) {
912       return;
913     }
914     if (!r.intersects(new Rectangle JavaDoc(xo, yo, w, h))) {
915       return;
916     }
917     boolean expanded = depth > 0 && c.isComposite();
918
919     int instance = ComponentRenderer.NO_INSTANCE;
920     if (admin != null) {
921       if (admin.getInstance(c) != null) {
922         if (admin.isStarted(c)) {
923           instance = ComponentRenderer.STARTED;
924         } else {
925           instance = ComponentRenderer.STOPPED;
926         }
927       }
928     }
929     
930     // draws the component itself
931
Color JavaDoc color = graphModel.getComponentColor(c);
932     if (display == null)
933       cRenderer.drawComponent(g, c, selection, r, color, expanded, 0, instance);
934     else cRenderer.drawComponent
935       (g, c, selection, r, color, expanded, display.getItfNameDisplayMode(), instance);
936
937     if (expanded) {
938       // draws its sub-components, if any
939
Shape JavaDoc clip = g.getClip();
940       Rectangle JavaDoc s = cRenderer.getSubComponentArea(c, r);
941       g.clipRect(s.x, s.y, s.width, s.height);
942
943       List JavaDoc subComponents = c.getSubComponents();
944       for (int i = subComponents.size() - 1; i >= 0; --i) {
945         Component subC = (Component)subComponents.get(i);
946         Rectangle JavaDoc subR = getRectangle(s, graphModel.getComponentPosition(subC));
947         drawComponent(g, subC, subR, depth - 1);
948       }
949
950       for (int i = subComponents.size() - 1; i >= 0; --i) {
951         Component subC = (Component)subComponents.get(i);
952         Rectangle JavaDoc subR = getRectangle(s, graphModel.getComponentPosition(subC));
953         drawExternalBindings(g, c, r, subC, subR, i);
954       }
955       // draws the bindings of the internal client interfaces
956
drawInternalBindings(g, c, r);
957       g.setClip(clip);
958     }
959   }
960
961   private void drawExternalBindings (
962     final Graphics g,
963     final Component p,
964     final Rectangle JavaDoc pr,
965     final Component c,
966     final Rectangle JavaDoc r,
967     final int rg)
968   {
969     List JavaDoc l = c.getClientInterfaces();
970     for (int i = 0; i < l.size(); ++i) {
971       ClientInterface citf = (ClientInterface)l.get(i);
972       if (citf.getBinding() != null) {
973         ServerInterface sitf = citf.getBinding().getServerInterface();
974         Component server = sitf.getOwner();
975         Point JavaDoc cp = cRenderer.getInterfacePosition(c, r, citf);
976
977         Rectangle JavaDoc t;
978         if (server == p) {
979           t = pr;
980         } else if (server.getParent() != c.getParent()) {
981           continue;
982         } else {
983           Rectangle JavaDoc s = cRenderer.getSubComponentArea(p, pr);
984           t = getRectangle(s, graphModel.getComponentPosition(server));
985         }
986         Point JavaDoc sp = cRenderer.getInterfacePosition(server, t, sitf);
987         bRenderer.drawBinding(g, r, t, cp, sp, citf.getBinding(), rg+i);
988       }
989
990       if (tools != null &&
991           tools.getTool() == Tools.BIND &&
992           tools.getBindInterface() == citf)
993       {
994         Point JavaDoc cp = cRenderer.getInterfacePosition(c, r, citf);
995         bRenderer.drawBinding(g, r, null, cp, bindPoint, citf.getBinding(), rg+i);
996       }
997     }
998   }
999
1000
1001  private void drawInternalBindings (
1002    final Graphics g,
1003    final Component c,
1004    final Rectangle JavaDoc r)
1005  {
1006    if (!c.isComposite()) {
1007      return;
1008    }
1009
1010    List JavaDoc l = c.getServerInterfaces();
1011    for (int i = 0; i < l.size(); ++i) {
1012      Interface itf = (Interface)l.get(i);
1013
1014      ClientInterface citf = (ClientInterface)itf.getComplementaryInterface();
1015      if (citf.getBinding() != null) {
1016        Point JavaDoc sp;
1017        Rectangle JavaDoc t;
1018        Point JavaDoc cp = cRenderer.getInterfacePosition(c, r, citf);
1019        ServerInterface sitf = citf.getBinding().getServerInterface();
1020        Component server = sitf.getOwner();
1021
1022        if (server != c) {
1023          Rectangle JavaDoc s = cRenderer.getSubComponentArea(c, r);
1024          t = getRectangle(s, graphModel.getComponentPosition(server));
1025          sp = cRenderer.getInterfacePosition(server, t, sitf);
1026        } else {
1027          t = r;
1028          sp = cRenderer.getInterfacePosition(server, r, sitf);
1029        }
1030        bRenderer.drawBinding(g, r, t, cp, sp, citf.getBinding(), i);
1031      }
1032
1033      if (tools != null &&
1034          tools.getTool() == Tools.BIND &&
1035          tools.getBindInterface() == citf)
1036      {
1037        Point JavaDoc cp = cRenderer.getInterfacePosition(c, r, citf);
1038        bRenderer.drawBinding(g, r, null, cp, bindPoint, citf.getBinding(), i);
1039      }
1040    }
1041  }
1042
1043  private Rectangle JavaDoc getRectangle (final Rectangle JavaDoc r, final Rect rect) {
1044    int h0 = r.x + (int)(r.width * rect.x0);
1045    int v0 = r.y + (int)(r.height * rect.y0);
1046    int h1 = r.x + (int)(r.width * rect.x1);
1047    int v1 = r.y + (int)(r.height * rect.y1);
1048    return new Rectangle JavaDoc(h0, v0, h1 - h0, v1 - v0);
1049  }
1050
1051  class PaintCover implements Printable JavaDoc
1052  {
1053    private Component c;
1054
1055    public int print(Graphics g, PageFormat JavaDoc pf, int pageIndex)
1056      throws PrinterException JavaDoc
1057    {
1058      g.setColor(Color.black);
1059      g.setFont(new Font JavaDoc("MonoSpaced", Font.PLAIN, 14));
1060      GregorianCalendar JavaDoc grcl = new GregorianCalendar JavaDoc ();
1061
1062      g.drawString("Printing by FractalGUI", 100, 100);
1063      g.drawString("for : "+UN, 120, 120);
1064      g.drawString("Component : '"+configuration.getRootComponent().getName()
1065          +"'", 120, 140);
1066      g.drawString("Date : "+mois[grcl.get(Calendar.MONTH)]+", "
1067          +grcl.get(Calendar.DAY_OF_MONTH)+"th "+grcl.get(Calendar.YEAR)
1068          , 120, 160);
1069      g.drawString("Time : "+grcl.get(Calendar.HOUR_OF_DAY)
1070          +"H"+grcl.get(Calendar.MINUTE)
1071          , 120, 180);
1072      return Printable.PAGE_EXISTS;
1073    }
1074  }
1075}
1076
Popular Tags