KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2002 Renaud Pawlak <renaud@aopsys.com>
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 */

18
19 package org.objectweb.jac.ide.diagrams;
20
21 import CH.ifa.draw.framework.Figure;
22 import org.objectweb.jac.ide.ModelElement;
23 import org.objectweb.jac.ide.RelationLink;
24 import org.objectweb.jac.ide.RelationRole;
25 import org.objectweb.jac.util.Log;
26
27 /**
28  * Text attached to relations (relation name, cardinalities and role
29  * names)
30  */

31
32 public class AttachedTextFigure extends TextFigure
33     implements ModelElementFigure
34 {
35
36     public final static int NAME = 0;
37     public final static int START_CARDINALITY = 1;
38     public final static int END_CARDINALITY = 2;
39     public final static int START_ROLE = 3;
40     public final static int END_ROLE = 4;
41
42     public void close() {}
43
44     public static AttachedTextFigure createName(org.objectweb.jac.ide.LinkFigure lf) {
45         return new AttachedTextFigure(lf,NAME);
46     }
47
48     public static AttachedTextFigure createStartCardinality(org.objectweb.jac.ide.LinkFigure lf) {
49         return new AttachedTextFigure(lf,START_CARDINALITY);
50     }
51
52     public static AttachedTextFigure createEndCardinality(org.objectweb.jac.ide.LinkFigure lf) {
53         return new AttachedTextFigure(lf,END_CARDINALITY);
54     }
55
56     public static AttachedTextFigure createStartRole(org.objectweb.jac.ide.LinkFigure lf) {
57         return new AttachedTextFigure(lf,START_ROLE);
58     }
59
60     public static AttachedTextFigure createEndRole(org.objectweb.jac.ide.LinkFigure lf) {
61         return new AttachedTextFigure(lf,END_ROLE);
62     }
63
64     org.objectweb.jac.ide.LinkFigure linkFig;
65
66     public ModelElement getSubstance() {
67         return linkFig.getElement();
68     }
69
70     public AttachedTextFigure() {}
71
72     public AttachedTextFigure(org.objectweb.jac.ide.LinkFigure linkFig,int type) {
73         this.linkFig = linkFig;
74         this.type = type;
75     }
76
77     public void setText(String JavaDoc s) {
78         if (s==null)
79             return;
80         super.setText(s);
81         if (DiagramView.init)
82             return;
83         RelationLink substance = (RelationLink)linkFig.getLink();
84         Log.trace("diagram","setText "+s+", type="+type+
85                   ", substance="+substance);
86         if (substance != null) {
87             RelationRole start = substance.startRole();
88             RelationRole end = substance.endRole();
89             switch (type) {
90                 case NAME:
91                     substance.setName(s);
92                     break;
93                 case END_ROLE:
94                     end.setName(s);
95                     break;
96                 case END_CARDINALITY:
97                     end.setCardinality(s);
98                     break;
99                 case START_ROLE:
100                     start.setName(s);
101                     break;
102                 case START_CARDINALITY:
103                     start.setCardinality(s);
104                     break;
105             }
106         }
107     }
108
109     public void refresh() {
110         RelationLink roledLink = (RelationLink)linkFig.getLink();
111         if (roledLink != null) {
112             RelationRole start = roledLink.startRole();
113             RelationRole end = roledLink.endRole();
114             int orientation = roledLink.getOrientation();
115             switch (type) {
116                 case NAME:
117                     if (roledLink.getName()!=null)
118                         super.setText(roledLink.getName());
119                     break;
120                 case END_ROLE:
121                     if (end.getName()!=null &&
122                         orientation!=RelationLink.ORIENTATION_STRAIGHT)
123                         super.setText(end.getName());
124                     else
125                         super.setText("");
126                     break;
127                 case END_CARDINALITY:
128                     if (end.getCardinality()!=null &&
129                         orientation!=RelationLink.ORIENTATION_STRAIGHT)
130                         super.setText(end.getCardinality());
131                     else
132                         super.setText("");
133                     break;
134                 case START_ROLE:
135                     if (start.getName()!=null &&
136                         orientation!=RelationLink.ORIENTATION_REVERSE)
137                         super.setText(start.getName());
138                     else
139                         super.setText("");
140                     break;
141                 case START_CARDINALITY:
142                     if (start.getCardinality()!=null &&
143                         orientation!=RelationLink.ORIENTATION_REVERSE)
144                         super.setText(start.getCardinality());
145                     else
146                         super.setText("");
147                     break;
148             }
149         }
150     }
151
152     protected void basicMoveBy(int dx, int dy) {
153         if(!DiagramView.init) {
154             Log.trace("diagram",2,this+".basicMoveBy "+dx+","+dy);
155             //new Exception().printStackTrace();
156
switch(type) {
157                 case NAME:
158                     linkFig.translateName(dx,dy);
159                     break;
160                 case START_ROLE:
161                     linkFig.translateStartRole(dx,dy);
162                     break;
163                 case START_CARDINALITY:
164                     linkFig.translateStartCardinality(dx,dy);
165                     break;
166                 case END_ROLE:
167                     linkFig.translateEndRole(dx,dy);
168                     break;
169                 case END_CARDINALITY:
170                     linkFig.translateEndCardinality(dx,dy);
171                     break;
172             }
173         }
174         super.basicMoveBy(dx, dy);
175     }
176
177     int type = -1;
178     
179     /**
180      * Get the value of type.
181      * @return value of type.
182      */

183     public int getType() {
184         return type;
185     }
186    
187     /**
188      * Set the value of type.
189      * @param v Value to assign to type.
190      */

191     public void setType(int v) {
192         this.type = v;
193     }
194
195     LinkFigure connectedLink;
196    
197     /**
198      * Get the value of connectedLink.
199      * @return value of connectedLink.
200      */

201     public LinkFigure getConnectedLink() {
202         return connectedLink;
203     }
204
205     /**
206      * Set the value of connectedLink.
207      * @param v Value to assign to connectedLink.
208      */

209     public void setConnectedLink(LinkFigure v) {
210         this.connectedLink = v;
211     }
212
213     public void connect(Figure f) {
214         Log.trace("diagram","connecting text figure, type= "+type);
215         super.connect(f);
216         ((LinkFigure)f).addAttachedTextFigure(this);
217         connectedLink = (LinkFigure)f;
218     }
219
220 }
221
Popular Tags