Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/memoire/dja/DjaGroup.java


1   /**
2    * @modification $Date: 2003/01/23 09:59:29 $
3    * @statut       unstable
4    * @file         DjaGroup.java
5    * @version      0.36
6    * @author       Guillaume Desnoix
7    * @email        guillaume@desnoix.com
8    * @license      GNU General Public License 2 (GPL2)
9    * @copyright    1998-2001 Guillaume Desnoix
10   */
11  
12  package com.memoire.dja;
13  
14  import com.memoire.fu.*;
15  import com.memoire.dnd.*;
16  import com.memoire.bu.*;
17  import com.memoire.dja.*;
18  import com.memoire.mst.*;
19  
20  
21  import java.awt.*;
22  import java.awt.event.*;
23  import java.util.*;
24  import javax.swing.*;
25  import javax.swing.border.*;
26  
27  public class DjaGroup
28         extends DjaForm
29  {
30    public DjaGroup(String _text)
31    {
32      super(_text,NORTH);
33  
34      objects_=new DjaVector();
35      setForeground(null);
36      setBackground(null);
37    }
38  
39    public DjaGroup()
40    {
41      this(null);
42    }
43  
44    private DjaVector objects_;
45    public  DjaVector getObjects() { return objects_; }
46  
47    public void addText(String _text)
48    {
49      DjaText t=new DjaText(this,-1,_text);
50      int p=CENTER;
51      switch(getTexts().length)
52      {
53      case 0: p=NORTH;  break;
54      case 1: p=SOUTH;  break;
55      case 2: p=WEST;   break;
56      case 3: p=EAST;   break;
57      }
58      t.setPosition(p);
59      addText(t);
60    }
61  
62    public int getX()
63    {
64      int xmin=Integer.MAX_VALUE;
65  
66      if(objects_!=null)
67      for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
68      {
69        DjaObject o=(DjaObject)e.nextElement();
70        xmin=Math.min(xmin,o.getX());
71      }
72  
73      if(xmin==Integer.MAX_VALUE) xmin=0;
74  
75      return xmin;
76    }
77  
78    public int getY()
79    {
80      int ymin=Integer.MAX_VALUE;
81  
82      if(objects_!=null)
83      for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
84      {
85        DjaObject o=(DjaObject)e.nextElement();
86        ymin=Math.min(ymin,o.getY());
87      }
88  
89      if(ymin==Integer.MAX_VALUE) ymin=0;
90  
91      return ymin;
92    }
93  
94    public int getWidth()
95    {
96      int xmax=-Integer.MAX_VALUE;
97  
98      if(objects_!=null)
99      for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
100     {
101       DjaObject o=(DjaObject)e.nextElement();
102       xmax=Math.max(xmax,o.getX()+o.getWidth() );
103     }
104 
105     if(xmax==-Integer.MAX_VALUE) xmax=2*deltaX;
106     else                         xmax-=getX();
107 
108     return xmax;
109   }
110 
111   public int getHeight()
112   {
113     int ymax=-Integer.MAX_VALUE;
114 
115     if(objects_!=null)
116     for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
117     {
118       DjaObject o=(DjaObject)e.nextElement();
119       ymax=Math.max(ymax,o.getY()+o.getHeight());
120     }
121 
122     if(ymax==-Integer.MAX_VALUE) ymax=2*deltaY;
123     else                         ymax-=getY();
124 
125     return ymax;
126   }
127 
128   public void setX(int _x)
129   {
130     int dx=getX()-_x;
131 
132     if(objects_!=null)
133     for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
134     {
135       DjaObject o=(DjaObject)e.nextElement();
136       //if(o instanceof DjaForm)
137       //((DjaForm) o).setX(o.getX()-dx);
138       o.setX(o.getX()-dx);
139     }
140 
141     super.setX(_x);
142   }
143 
144   public void setY(int _y)
145   {
146     int dy=getY()-_y;
147 
148     if(objects_!=null)
149     for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
150     {
151       DjaObject o=(DjaObject)e.nextElement();
152       //if(o instanceof DjaForm)
153       //((DjaForm) o).setY(o.getY()-dy);
154       o.setY(o.getY()-dy);
155     }
156 
157     super.setY(_y);
158   }
159 
160   public void setForeground(Color _foreground)
161   {
162     if(objects_!=null)
163     for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
164     {
165       DjaObject o=(DjaObject)e.nextElement();
166       o.setForeground(_foreground);
167     }
168   }
169 
170   public void setBackground(Color _background)
171   {
172     if(objects_!=null)
173     for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
174     {
175       DjaObject o=(DjaObject)e.nextElement();
176       o.setBackground(_background);
177     }
178   }
179 
180   public DjaAnchor[] getAnchors()
181   {
182     if(objects_==null) return super.getAnchors();
183 
184     int x=getX();
185     int y=getY();
186     int w=getWidth();
187     int h=getHeight();
188 
189     DjaVector v=new DjaVector();
190     int       n=0;
191 
192     for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
193     {
194       DjaObject   o=(DjaObject)e.nextElement();
195       DjaAnchor[] a=o.getAnchors();
196 
197       for(int j=0;j<a.length;j++)
198       {
199   v.addElement(a[j]);
200 
201   /*
202   int xf=a[j].getX();
203   int yf=a[j].getY();
204 
205        if(y    ==yf) v.addElement(new DjaAnchor(this,n++,NORTH,xf,yf));
206   else if(x    ==xf) v.addElement(new DjaAnchor(this,n++,WEST ,xf,yf));
207   else if(y+h-1==yf) v.addElement(new DjaAnchor(this,n++,SOUTH ,xf,yf));
208   else if(x+w-1==xf) v.addElement(new DjaAnchor(this,n++,EAST ,xf,yf));
209   */
210       }
211     }
212 
213     DjaAnchor[] r=new DjaAnchor[v.size()];
214     for(int i=0;i<r.length;i++)
215       r[i]=(DjaAnchor)v.elementAt(i);
216 
217     return r;
218   }
219 
220   public DjaHandle[] getHandles()
221   {
222     return new DjaHandle[0];
223   }
224 
225   public void add(DjaObject _object)
226   {
227     add(_object,false);
228   }
229 
230   public void add(DjaObject _object, boolean _quiet)
231   {
232     if(objects_!=null)
233     {
234       if(_object.getOwner()!=null)
235   throw new RuntimeException("belong to "+_object.getOwner());
236       _object.setSelected(false);
237       objects_.addElement(_object);
238       _object.setOwner(this);
239 
240       if(!_quiet) fireGridEvent(_object,DjaGridEvent.ADDED);
241     }
242   }
243 
244   public void remove(DjaObject _object)
245   {
246     remove(_object,false);
247   }
248 
249   public void remove(DjaObject _object, boolean _quiet)
250   {
251     if(objects_!=null)
252     {
253       if(_object.getOwner()!=this)
254   throw new RuntimeException("doesn't belong to "+this);
255       objects_.removeElement(_object);
256       _object.setOwner(null);
257 
258       if(!_quiet) fireGridEvent(_object,DjaGridEvent.REMOVED);
259     }
260   }
261 
262   public void paintObject(Graphics _g)
263   {
264     if(objects_!=null)
265     {
266   Rectangle clip=_g.getClipBounds();
267 
268   for(Enumeration e=objects_.reverseElements(); e.hasMoreElements(); )
269   {
270       DjaObject o=(DjaObject)e.nextElement();
271       if(!clip.intersects(o.getBounds())) continue;
272       o.paint(_g);
273   }
274     }
275    
276     super.paintObject(_g);
277   }
278 
279   public void paintInteractive(Graphics _g)
280   {
281     if(objects_!=null)
282     for(Enumeration e=objects_.elements(); e.hasMoreElements(); )
283     {
284       DjaObject o=(DjaObject)e.nextElement();
285       o.paintInteractive(_g);
286     }
287 
288     int x=getX();
289     int y=getY();
290     int w=getWidth();
291     int h=getHeight();
292 
293     putProperty("epaisseur","3");
294     putProperty("trace","3.5");
295     _g.setColor(isSelected() ? selectionForeground : Color.gray);
296     _g.drawRect(x-2,y-2,w+3,h+3);
297     DjaGraphics.drawRect(_g,x-2,y-2,w+3,h+3,
298        DjaGraphics.getBresenhamParams(this));
299 
300     super.paintInteractive(_g);
301   }
302 }