Source code: com/memoire/dja/DjaObject.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:25 $
3 * @statut unstable
4 * @file DjaObject.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.io.*;
24 import java.util.*;
25 import javax.swing.*;
26 import javax.swing.border.*;
27
28 public abstract class DjaObject
29 implements DjaOwner, DjaOptions, Cloneable, Serializable
30 {
31 private boolean selected_;
32 private DjaColor foreground_;
33 private DjaColor background_;
34 private DjaColor color_;
35 private DjaFont font_;
36 private DjaText[] texts_;
37
38 private FuHashtablePublic properties_;
39
40 private transient DjaOwner owner_;
41 private transient FuHashtablePublic datas_;
42
43 public DjaObject()
44 {
45 properties_=new FuHashtablePublic(11);
46 texts_ =new DjaText[0];
47
48 setSelected (false);
49 setForeground(Color.black);
50 setBackground(Color.white);
51 setFont (defaultPlainFont);
52 setColor (Color.black);
53 }
54
55 public DjaOwner getOwner()
56 { return owner_; }
57
58 void setOwner(DjaOwner _owner)
59 { owner_=_owner; }
60
61 public DjaGrid getGrid()
62 {
63 DjaGrid r=null;
64
65 DjaOwner o=getOwner();
66 while(o!=null)
67 {
68 if(o instanceof DjaGrid)
69 { r=(DjaGrid)o; break; }
70 o=o.getOwner();
71 }
72
73 return r;
74 }
75
76 public void fireGridEvent(DjaOwner _object,int _id)
77 {
78 DjaGrid grid=getGrid();
79 if(grid!=null) grid.fireGridEvent(_object,_id);
80 }
81
82 public Object clone() throws CloneNotSupportedException
83 {
84 DjaObject r=(DjaObject)super.clone();
85
86 properties_=(FuHashtablePublic)properties_.clone();
87
88 if(datas_!=null) datas_=(FuHashtablePublic)datas_.clone();
89
90 DjaText[] t=r.getTextArray();
91 DjaText[] u=new DjaText[t.length];
92 for(int i=0;i<t.length;i++)
93 {
94 u[i]=(DjaText)t[i].clone();
95 u[i].setParent(r);
96 }
97 r.setTextArray(u);
98
99 return r;
100 }
101
102 public Icon getIcon()
103 {
104 String s=getClass().getName().toLowerCase();
105 int i=s.indexOf("dja");
106 if(i>=0) s="dja-"+s.substring(i+3);
107 return DjaResource.DJA.getIcon(s,16);
108 }
109
110 public String getProperty(String _key)
111 {
112 return (String)properties_.get(_key);
113 }
114
115 public void putProperty(String _key, String _value)
116 {
117 if(_value!=null) properties_.put(_key,_value);
118 else removeProperty(_key);
119 }
120
121 public void removeProperty(String _key)
122 {
123 properties_.remove(_key);
124 }
125
126 public Object getData(String _key)
127 {
128 return datas_==null ? null : datas_.get(_key);
129 }
130
131 public void putData(String _key, Object _value)
132 {
133 if(datas_==null) datas_=new FuHashtablePublic(11);
134 datas_.put(_key,_value);
135 }
136
137 public void removeData(String _key)
138 {
139 if(datas_!=null) datas_.remove(_key);
140 }
141
142 // Saver
143 Enumeration getPropertyKeys()
144 { return properties_.keys(); }
145
146 public void beforeSaving() { }
147 public void afterLoading() { }
148
149 public abstract Rectangle getBounds();
150 public abstract void setX (int _x);
151 public abstract void setY (int _y);
152 public abstract void setWidth (int _w);
153 public abstract void setHeight(int _h);
154
155 public boolean contains(int _x, int _y)
156 { return getBounds().contains(_x,_y); }
157
158 public int getX()
159 { return getBounds().x; }
160
161 public int getY()
162 { return getBounds().y; }
163
164 public int getWidth()
165 { return getBounds().width; }
166
167 public Rectangle getExtendedBounds()
168 {
169 return getExtendedBounds(getBounds());
170 }
171
172 protected Rectangle getExtendedBounds(Rectangle _r)
173 {
174 if(isSelected()) _r.grow(deltaX,deltaY);
175 if(this instanceof DjaLink) _r.grow(11,11);
176
177 /*
178 {
179 DjaAnchor[] anchors=getAnchors();
180 for(int i=0;i<anchors.length;i++)
181 anchors[i].paint(_g);
182 }
183
184 {
185 DjaAttach[] attachs=getAttachs();
186 for(int i=0;i<attachs.length;i++)
187 attachs[i].paint(_g);
188 }
189
190 {
191 DjaHandle[] handles=getHandles();
192 for(int i=0;i<handles.length;i++)
193 handles[i].paint(_g);
194 }
195
196 {
197 DjaControl[] controls=getControls();
198 for(int i=0;i<controls.length;i++)
199 controls[i].paint(_g);
200 }
201 */
202
203 {
204 DjaText[] texts=getTexts();
205 for(int i=0;i<texts.length;i++)
206 _r=_r.union(texts[i].getBounds());
207 }
208
209 _r.grow(3,3);
210
211 String epaisseur=getProperty("epaisseur");
212 if(epaisseur!=null)
213 {
214 int e=Integer.parseInt(epaisseur);
215 _r.grow(e,e);
216 }
217
218 return _r;
219 }
220
221 public int getHeight()
222 { return getBounds().height; }
223
224 public boolean isSelected()
225 { return selected_; }
226
227 public void setSelected(boolean _selected)
228 {
229 if(selected_!=_selected)
230 {
231 selected_=_selected;
232 fireGridEvent(this,selected_
233 ? DjaGridEvent.SELECTED
234 : DjaGridEvent.UNSELECTED);
235 }
236 }
237
238 public Color getForeground()
239 {
240 Color r=null;
241
242 if(foreground_!=null)
243 {
244 if(selected_)
245 r=BuLib.mixColors(foreground_.getColor(),selectionForeground);
246 else
247 r=foreground_.getColor();
248 }
249
250 return r;
251 }
252
253 public void setForeground(Color _foreground)
254 {
255 if(_foreground!=null)
256 foreground_=new DjaColor(_foreground);
257 else
258 foreground_=null;
259 }
260
261 public Color getBackground()
262 {
263 Color r=null;
264
265 if(background_!=null)
266 {
267 if(selected_)
268 r=BuLib.mixColors(background_.getColor(),selectionBackground);
269 else
270 r=background_.getColor();
271 }
272
273 return r;
274 }
275
276 public void setBackground(Color _background)
277 {
278 if(_background!=null)
279 background_=new DjaColor(_background);
280 else
281 background_=null;
282 }
283
284 public DjaAnchor[] getAnchors()
285 {
286 return new DjaAnchor[0];
287 }
288
289 public DjaAttach[] getAttachs()
290 {
291 return new DjaAttach[0];
292 }
293
294 public DjaHandle[] getHandles()
295 {
296 int x=getX();
297 int y=getY();
298 int w=getWidth();
299 int h=getHeight();
300
301 DjaHandle[] r=new DjaHandle[8];
302 r[0]=new DjaHandle(this,NORTH ,x+w/2 ,y -deltaY);
303 r[2]=new DjaHandle(this,EAST ,x+w-1+deltaX,y+h/2 );
304 r[4]=new DjaHandle(this,SOUTH ,x+w/2 ,y+h-1+deltaY);
305 r[6]=new DjaHandle(this,WEST ,x -deltaX,y+h/2 );
306 r[1]=new DjaHandle(this,NORTH_EAST,x+w-1+deltaX,y -deltaY);
307 r[3]=new DjaHandle(this,SOUTH_EAST,x+w-1+deltaX,y+h-1+deltaY);
308 r[5]=new DjaHandle(this,SOUTH_WEST,x -deltaX,y+h-1+deltaY);
309 r[7]=new DjaHandle(this,NORTH_WEST,x -deltaX,y -deltaY);
310
311 return r;
312 }
313
314 public DjaControl[] getControls()
315 {
316 return new DjaControl[0];
317 }
318
319 public DjaLink[] getBeginConnections()
320 {
321 DjaGrid grid=getGrid();
322 if(grid==null) return new DjaLink[0];
323
324 DjaVector v=new DjaVector();
325 for(Enumeration e=grid.enumerate(DjaLink.class);
326 e.hasMoreElements(); )
327 {
328 DjaLink l=(DjaLink)e.nextElement();
329 if(l.getBeginObject()==this)
330 v.addElement(l);
331 }
332
333 int t=v.size();
334 DjaLink[] r=new DjaLink[t];
335 for(int i=0; i<t; i++) r[i]=(DjaLink)v.elementAt(i);
336 return r;
337 }
338
339 public DjaLink[] getEndConnections()
340 {
341 DjaGrid grid=getGrid();
342 if(grid==null) return new DjaLink[0];
343
344 DjaVector v=new DjaVector();
345 for(Enumeration e=grid.enumerate(DjaLink.class);
346 e.hasMoreElements(); )
347 {
348 DjaLink l=(DjaLink)e.nextElement();
349 if(l.getEndObject()==this)
350 v.addElement(l);
351 }
352
353 int t=v.size();
354 DjaLink[] r=new DjaLink[t];
355 for(int i=0; i<t; i++) r[i]=(DjaLink)v.elementAt(i);
356 return r;
357 }
358
359 public DjaText[] getTexts()
360 {
361 DjaText[] r=texts_;
362 if(r==null) r=new DjaText[0];
363 return r;
364 }
365
366 protected final DjaText[] getTextArray()
367 {
368 return texts_;
369 }
370
371 protected final void setTextArray(DjaText[] _texts)
372 {
373 texts_=_texts;
374 }
375
376 public final synchronized void addText(DjaText _text)
377 {
378 if(texts_==null)
379 {
380 _text.setNum(0);
381 texts_=new DjaText[] { _text };
382 }
383 else
384 {
385 int l=texts_.length;
386 int n=_text.getNum();
387
388 if((n>=0)&&(n<l))
389 texts_[n]=_text;
390 else
391 {
392 DjaText[] a=new DjaText[l+1];
393 for(int i=0;i<l;i++) a[i]=texts_[i];
394 _text.setNum(l);
395 a[l]=_text;
396 texts_=a;
397 }
398 }
399 }
400
401 public void addText(String _text)
402 {
403 DjaText t=new DjaText(this,-1,_text);
404 t.setPosition(ABSOLUTE);
405 addText(t);
406 }
407
408 /**
409 * Notify when a text was modified
410 */
411 public void textChanged(DjaText _text) { }
412
413 // Simplify text
414
415 /**
416 * @deprecated use getText(0)
417 */
418 public /*final*/ String getText()
419 {
420 return getText(0);
421 }
422
423 public String getText(int _i)
424 {
425 String r=null;
426 if(texts_.length>_i) texts_[_i].getText();
427 return r;
428 }
429
430 /**
431 * @deprecated use setText(0)
432 */
433 public /*final*/ void setText(String _text)
434 {
435 setText(_text,0);
436 }
437
438 public void setText(String _text, int _i)
439 {
440 if("".equals(_text)) _text=null;
441 if(texts_.length>_i) texts_[_i].setText(_text);
442 }
443
444 public Font getFont()
445 {
446 Font r=null;
447 if(font_!=null) r=font_.getFont();
448 if(r==null) r=defaultPlainFont;
449 return r;
450 }
451
452 public void setFont(Font _font)
453 {
454 font_=new DjaFont(_font);
455 }
456
457 public Color getColor()
458 {
459 Color r=null;
460 if(color_!=null) r=color_.getColor();
461 return r;
462 }
463
464 public void setColor(Color _color)
465 {
466 if(_color!=null)
467 color_=new DjaColor(_color);
468 else
469 color_=null;
470 }
471
472 /**
473 * @deprecated
474 */
475 public Color getTextColor()
476 {
477 Color r=null;
478 /*
479 if(selected_) r=selectionForeground;
480 else r=textcolor_;
481 */
482 if(texts_.length>0) r=texts_[0].getColor();
483 return r;
484 }
485
486 /**
487 * @deprecated
488 */
489 public void setTextColor(Color _color)
490 {
491 if(texts_.length>0) texts_[0].setColor(_color);
492 }
493
494 /**
495 * @deprecated
496 */
497 public int getTextPosition()
498 {
499 int r=CENTER;
500 if(texts_.length>0) r=texts_[0].getPosition();
501 return r;
502 }
503
504 /**
505 * @deprecated
506 */
507 public void setTextPosition(int _pos)
508 {
509 if(texts_.length>0) texts_[0].setPosition(_pos);
510 }
511
512 /**
513 * @deprecated
514 */
515 public boolean isTextMultiline()
516 {
517 boolean r=false;
518 if(texts_.length>0) texts_[0].isMultiline();
519 return r;
520 }
521
522 /**
523 * @deprecated
524 */
525 protected void setTextBounds(Rectangle _r) { }
526 /*
527 private Rectangle textBounds_=null;
528
529 public boolean textContains(int _x, int _y)
530 {
531 Rectangle tb=getTextBounds();
532 return (tb==null ? false : tb.contains(_x,_y));
533 }
534
535 protected Rectangle getTextBounds() { return textBounds_; }
536 protected void setTextBounds(Rectangle _r) { textBounds_=_r; }
537 */
538
539 public final void paint(Graphics _g)
540 {
541 paintObject(_g);
542 paintText(_g);
543 paintTexts(_g);
544 }
545
546 public void paintObject(Graphics _g)
547 {
548 }
549
550 /**
551 * @deprecated
552 */
553 public void paintText(Graphics _g)
554 {
555 }
556
557 public void paintInteractive(Graphics _g)
558 {
559 if(isSelected())
560 {
561 DjaText[] texts=getTexts();
562 for(int k=0;k<texts.length;k++)
563 {
564 boolean moveable=texts[k].isMoveable();
565 boolean selected=texts[k].isSelected();
566
567 Rectangle r=texts[k].getBounds();
568
569 int x=r.x;
570 int y=r.y;
571 int w=r.width;
572 int h=r.height;
573
574 _g.setColor(selected
575 ? selectionZone
576 : texts[k].getColor());
577
578 for(int i=1;i<=w;i+=4)
579 {
580 if(moveable) _g.drawLine(x+i,y-1,x+i+1,y-1);
581 _g.drawLine(x+i,y+h+1,x+i+1,y+h+1);
582 }
583
584 if(moveable)
585 for(int j=1;j<=h;j+=4)
586 {
587 _g.drawLine(x-1 ,y+j,x-1 ,y+j+1);
588 _g.drawLine(x+w+1,y+j,x+w+1,y+j+1);
589 }
590
591 if(selected)
592 {
593 _g.drawLine(x,y,x+5,y );
594 _g.drawLine(x+5,y,x,y+5);
595 _g.drawLine(x,y+5,x,y );
596 }
597 }
598 }
599 }
600
601 public void paintAnchors(Graphics _g)
602 {
603 DjaAnchor[] anchors=getAnchors();
604 for(int i=0;i<anchors.length;i++)
605 anchors[i].paint(_g);
606 }
607
608 public void paintAttachs(Graphics _g)
609 {
610 DjaAttach[] attachs=getAttachs();
611 for(int i=0;i<attachs.length;i++)
612 attachs[i].paint(_g);
613 }
614
615 public void paintHandles(Graphics _g)
616 {
617 DjaHandle[] handles=getHandles();
618 for(int i=0;i<handles.length;i++)
619 handles[i].paint(_g);
620 }
621
622 public void paintControls(Graphics _g)
623 {
624 DjaControl[] controls=getControls();
625 for(int i=0;i<controls.length;i++)
626 controls[i].paint(_g);
627 }
628
629 public void paintTexts(Graphics _g)
630 {
631 DjaText[] texts=getTexts();
632 for(int i=0;i<texts.length;i++)
633 texts[i].paint(_g);
634 }
635
636 public String toString()
637 {
638 String r=getClass().getName();
639 int i=r.lastIndexOf('.');
640 if(i>=0) r=r.substring(i+1);
641 if(r.startsWith("Dja")) r=r.substring(3);
642 return r;
643 }
644 }