001    /*
002      Copyright (C) 2002 Renaud Pawlak
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      USA. */
018    
019    package org.objectweb.jac.ide.diagrams;
020    
021    import CH.ifa.draw.framework.*;
022    import CH.ifa.draw.standard.*;
023    import org.objectweb.jac.core.Wrappee;
024    import org.objectweb.jac.core.Wrapping;
025    import org.objectweb.jac.ide.Instance;
026    import org.objectweb.jac.ide.ModelElement;
027    import org.objectweb.jac.ide.Package;
028    import org.objectweb.jac.util.Log;
029    import java.awt.Color;
030    import java.awt.Dimension;
031    import java.awt.Font;
032    import java.awt.Graphics;
033    import java.awt.Point;
034    import java.awt.Rectangle;
035    import java.util.Vector;
036    
037    public class InstanceFigure extends CompositeFigure 
038        implements ModelElementFigure 
039    {
040        private static final int BORDER = 3;
041        private Rectangle fDisplayBox;
042       
043        Instance substance;
044    
045        /*
046          public void refreshView() {
047          if( DiagramView.init ) return;
048          DiagramView.init = true;
049          initInstance();
050          layout();
051          changed();
052          DiagramView.refreshFigure(this);
053          //editor().validate();
054          DiagramView.init = false;
055          }
056        */
057    
058        public void close() {}
059    
060        /**
061         * Get the value of substance.
062         * @return value of substance.
063         */
064        public ModelElement getSubstance() {
065            return substance;
066        }
067       
068        /**
069         * Set the value of substance.
070         * @param v  Value to assign to substance.
071         */
072        public void setSubstance(Instance  v) {
073            this.substance = v;
074        }
075    
076        public String getType() {
077            return substance.getType().getName();
078        }
079       
080        public InstanceFigure() {
081            initialize();
082        }
083    
084        Package containerPackage;
085       
086        /**
087         * Get the value of containerPackage.
088         * @return value of containerPackage.
089         */
090        public Package getContainerPackage() {
091            return containerPackage;
092        }
093       
094        /**
095         * Set the value of containerPackage.
096         * @param v  Value to assign to containerPackage.
097         */
098        public void setContainerPackage(Package  v) {
099            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[]{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 displayBox() {
124            return new Rectangle(
125                fDisplayBox.x,
126                fDisplayBox.y,
127                fDisplayBox.width,
128                fDisplayBox.height);
129        }
130    
131        public void basicDisplayBox(Point origin, Point corner) {
132            fDisplayBox = new Rectangle(origin);
133            fDisplayBox.add(corner);
134            layout();
135        }
136    
137        protected void drawBorder(Graphics g) {
138          
139            Rectangle 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 g) {
152            drawBorder(g);
153            super.draw(g);
154        }
155    
156        public Vector handles() {
157            Vector handles = new Vector();
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 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(0, 0, 0, 0);
178          
179            Font f = new Font("Helvetica", Font.PLAIN, 12);
180            Font fb = new Font("Helvetica", Font.BOLD, 12);
181          
182            initName();
183    
184        }
185       
186        public void layout() {
187            Point partOrigin = new Point(fDisplayBox.x, fDisplayBox.y);
188            partOrigin.translate(BORDER, BORDER);
189            Dimension extent = new Dimension(0, 0);
190    
191    
192            Dimension partExtent = figureAt(0).size();
193            Point corner = new Point(
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    }