001    /*
002      Copyright (C) 2002 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017    */
018    
019    package org.objectweb.jac.ide.diagrams;
020    
021    import CH.ifa.draw.framework.Figure;
022    import org.objectweb.jac.ide.ModelElement;
023    import org.objectweb.jac.ide.RelationLink;
024    import org.objectweb.jac.ide.RelationRole;
025    import org.objectweb.jac.util.Log;
026    
027    /**
028     * Text attached to relations (relation name, cardinalities and role
029     * names)
030     */
031    
032    public class AttachedTextFigure extends TextFigure 
033        implements ModelElementFigure 
034    {
035    
036        public final static int NAME = 0;
037        public final static int START_CARDINALITY = 1;
038        public final static int END_CARDINALITY = 2;
039        public final static int START_ROLE = 3;
040        public final static int END_ROLE = 4;
041    
042        public void close() {}
043    
044        public static AttachedTextFigure createName(org.objectweb.jac.ide.LinkFigure lf) {
045            return new AttachedTextFigure(lf,NAME);
046        }
047    
048        public static AttachedTextFigure createStartCardinality(org.objectweb.jac.ide.LinkFigure lf) {
049            return new AttachedTextFigure(lf,START_CARDINALITY);
050        }
051    
052        public static AttachedTextFigure createEndCardinality(org.objectweb.jac.ide.LinkFigure lf) {
053            return new AttachedTextFigure(lf,END_CARDINALITY);
054        }
055    
056        public static AttachedTextFigure createStartRole(org.objectweb.jac.ide.LinkFigure lf) {
057            return new AttachedTextFigure(lf,START_ROLE);
058        }
059    
060        public static AttachedTextFigure createEndRole(org.objectweb.jac.ide.LinkFigure lf) {
061            return new AttachedTextFigure(lf,END_ROLE);
062        }
063    
064        org.objectweb.jac.ide.LinkFigure linkFig;
065    
066        public ModelElement getSubstance() {
067            return linkFig.getElement();
068        }
069    
070        public AttachedTextFigure() {}
071    
072        public AttachedTextFigure(org.objectweb.jac.ide.LinkFigure linkFig,int type) {
073            this.linkFig = linkFig;
074            this.type = type;
075        }
076    
077        public void setText(String s) {
078            if (s==null) 
079                return;
080            super.setText(s);
081            if (DiagramView.init) 
082                return;
083            RelationLink substance = (RelationLink)linkFig.getLink();
084            Log.trace("diagram","setText "+s+", type="+type+
085                      ", substance="+substance);
086            if (substance != null) {
087                RelationRole start = substance.startRole();
088                RelationRole end = substance.endRole();
089                switch (type) {
090                    case NAME:
091                        substance.setName(s);
092                        break;
093                    case END_ROLE:
094                        end.setName(s);
095                        break;
096                    case END_CARDINALITY:
097                        end.setCardinality(s);
098                        break;
099                    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    }