KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > InstanceFigure


1 /*
2   Copyright (C) 2002 Renaud Pawlak
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA. */

18
19 package org.objectweb.jac.ide.diagrams;
20
21 import CH.ifa.draw.framework.*;
22 import CH.ifa.draw.standard.*;
23 import org.objectweb.jac.core.Wrappee;
24 import org.objectweb.jac.core.Wrapping;
25 import org.objectweb.jac.ide.Instance;
26 import org.objectweb.jac.ide.ModelElement;
27 import org.objectweb.jac.ide.Package;
28 import org.objectweb.jac.util.Log;
29 import java.awt.Color JavaDoc;
30 import java.awt.Dimension JavaDoc;
31 import java.awt.Font JavaDoc;
32 import java.awt.Graphics JavaDoc;
33 import java.awt.Point JavaDoc;
34 import java.awt.Rectangle JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 public class InstanceFigure extends CompositeFigure
38     implements ModelElementFigure
39 {
40     private static final int BORDER = 3;
41     private Rectangle JavaDoc fDisplayBox;
42    
43     Instance substance;
44
45     /*
46       public void refreshView() {
47       if( DiagramView.init ) return;
48       DiagramView.init = true;
49       initInstance();
50       layout();
51       changed();
52       DiagramView.refreshFigure(this);
53       //editor().validate();
54       DiagramView.init = false;
55       }
56     */

57
58     public void close() {}
59
60     /**
61      * Get the value of substance.
62      * @return value of substance.
63      */

64     public ModelElement getSubstance() {
65         return substance;
66     }
67    
68     /**
69      * Set the value of substance.
70      * @param v Value to assign to substance.
71      */

72     public void setSubstance(Instance v) {
73         this.substance = v;
74     }
75
76     public String JavaDoc getType() {
77         return substance.getType().getName();
78     }
79    
80     public InstanceFigure() {
81         initialize();
82     }
83
84     Package JavaDoc containerPackage;
85    
86     /**
87      * Get the value of containerPackage.
88      * @return value of containerPackage.
89      */

90     public Package JavaDoc getContainerPackage() {
91         return containerPackage;
92     }
93    
94     /**
95      * Set the value of containerPackage.
96      * @param v Value to assign to containerPackage.
97      */

98     public void setContainerPackage(Package JavaDoc v) {
99         this.containerPackage = v;
100     }
101       
102     void initInstance() {
103         if (substance == null) {
104             Log.warning("UNRESOLVED INSTANCE!");
105         } else {
106             Log.trace("figures","init class "+substance);
107             initTitle();
108             Wrapping.invokeRoleMethod((Wrappee)substance,"addView",new Object JavaDoc[]{this});
109         }
110     }
111
112     void initTitle() {
113         InstanceNameFigure nf = (InstanceNameFigure)figures().nextFigure();
114         nf.setSubstance(substance);
115         nf.setText(substance.toString());
116     }
117
118     protected void basicMoveBy(int x, int y) {
119         fDisplayBox.translate(x, y);
120         super.basicMoveBy(x, y);
121     }
122
123     public Rectangle JavaDoc displayBox() {
124         return new Rectangle JavaDoc(
125             fDisplayBox.x,
126             fDisplayBox.y,
127             fDisplayBox.width,
128             fDisplayBox.height);
129     }
130
131     public void basicDisplayBox(Point JavaDoc origin, Point JavaDoc corner) {
132         fDisplayBox = new Rectangle JavaDoc(origin);
133         fDisplayBox.add(corner);
134         layout();
135     }
136
137     protected void drawBorder(Graphics JavaDoc g) {
138       
139         Rectangle JavaDoc r = displayBox();
140
141         g.setColor(Color.white);
142         g.fillRoundRect(r.x, r.y, r.width, r.height,7,7);
143       
144         g.setColor(Color.black);
145         g.drawRoundRect(r.x, r.y, r.width, r.height,7,7);
146       
147         //Figure f = figureAt(0);
148
//Rectangle rf = f.displayBox();
149
}
150
151     public void draw(Graphics JavaDoc g) {
152         drawBorder(g);
153         super.draw(g);
154     }
155
156     public Vector JavaDoc handles() {
157         Vector JavaDoc handles = new Vector JavaDoc();
158         handles.addElement(new NullHandle(this, RelativeLocator.northWest()));
159         handles.addElement(new NullHandle(this, RelativeLocator.northEast()));
160         handles.addElement(new NullHandle(this, RelativeLocator.southWest()));
161         handles.addElement(new NullHandle(this, RelativeLocator.southEast()));
162         return handles;
163     }
164
165     public String JavaDoc getName() {
166         return ((TextFigure)figures().nextFigure()).getText();
167     }
168
169     void initName() {
170         InstanceNameFigure name=new InstanceNameFigure();
171         name.setText("NewInstance");
172         add(name);
173     }
174
175     private void initialize() {
176       
177         fDisplayBox = new Rectangle JavaDoc(0, 0, 0, 0);
178       
179         Font JavaDoc f = new Font JavaDoc("Helvetica", Font.PLAIN, 12);
180         Font JavaDoc fb = new Font JavaDoc("Helvetica", Font.BOLD, 12);
181       
182         initName();
183
184     }
185    
186     public void layout() {
187         Point JavaDoc partOrigin = new Point JavaDoc(fDisplayBox.x, fDisplayBox.y);
188         partOrigin.translate(BORDER, BORDER);
189         Dimension JavaDoc extent = new Dimension JavaDoc(0, 0);
190
191
192         Dimension JavaDoc partExtent = figureAt(0).size();
193         Point JavaDoc corner = new Point JavaDoc(
194             partOrigin.x+partExtent.width,
195             partOrigin.y+partExtent.height);
196         figureAt(0).basicDisplayBox(partOrigin, corner);
197          
198         extent.width = partExtent.width;
199         extent.height = partExtent.height;
200         fDisplayBox.width = extent.width + 2*BORDER;
201         fDisplayBox.height = extent.height + 2*BORDER;
202
203     }
204
205     private boolean needsLayout() {
206         /* Dimension extent = new Dimension(0, 0);
207
208                 FigureEnumeration k = figures();
209                 while (k.hasMoreElements()) {
210                 Figure f = k.nextFigure();
211                 extent.width = Math.max(extent.width, f.size().width);
212                 }
213                 int newExtent = extent.width + 2*BORDER;
214                 return newExtent != fDisplayBox.width;*/

215         return true;
216     }
217
218     public void update(FigureChangeEvent e) {
219         /*if (e.getFigure() == figureAt(1)) {
220           // duration has changed
221           //updateDurations();
222           }*/

223         if (needsLayout()) {
224             layout();
225             changed();
226         }
227     }
228
229     public void figureChanged(FigureChangeEvent e) {
230         update(e);
231     }
232
233     public void figureRemoved(FigureChangeEvent e) {
234         update(e);
235     }
236
237 }
238
Popular Tags