KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > diagrammer > PaintPanel


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.diagrammer;
31
32 import java.awt.BasicStroke JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.awt.Graphics2D JavaDoc;
37 import java.awt.Rectangle JavaDoc;
38 import java.awt.Stroke JavaDoc;
39 import java.awt.geom.AffineTransform JavaDoc;
40 import java.awt.image.BufferedImage JavaDoc;
41 import java.awt.print.PageFormat JavaDoc;
42 import java.awt.print.Printable JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.HashMap JavaDoc;
45
46 import javax.swing.JOptionPane JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48
49 import com.genimen.djeneric.language.Messages;
50 import com.genimen.djeneric.repository.DjExtent;
51 import com.genimen.djeneric.repository.DjPersistenceManager;
52 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
53 import com.genimen.djeneric.ui.Util;
54
55 public class PaintPanel extends JPanel JavaDoc implements Printable JavaDoc
56 {
57   private static final long serialVersionUID = 1L;
58   final static int MIN_OBJECT_WIDTH = 80;
59   final static int MIN_OBJECT_HEIGHT = 20;
60   Graphics JavaDoc _g = null;
61   BufferedImage JavaDoc _bi = null;
62   Dimension JavaDoc _dim = new Dimension JavaDoc(1, 1);
63   DjPersistenceManager _mgr = null;
64   ArrayList JavaDoc _extentViewers = new ArrayList JavaDoc();
65   public final static int SHOW_CLASS = 0;
66   public final static int SHOW_ALIAS = 1;
67   public final static int SHOW_EXTENT = 2;
68   public final static int SHOW_QUALIFIED = 3;
69   public final static int PRINT_MARGIN = 0;
70   private int _viewMode = SHOW_CLASS;
71   int _selectionBoxX1 = -1;
72   int _selectionBoxY1 = -1;
73   int _selectionBoxX2 = -1;
74   int _selectionBoxY2 = -1;
75   int _relationLineX1;
76   int _relationLineY1;
77   int _relationLineX2;
78   int _relationLineY2;
79   private double _scale = 1.0;
80   int _scrollOffsetX = 0;
81   int _scrollOffsetY = 0;
82
83   public PaintPanel()
84   {
85     setDoubleBuffered(false);
86   }
87
88   public void setViewMode(int viewMode)
89   {
90     _viewMode = viewMode;
91   }
92
93   public int getViewMode()
94   {
95     return _viewMode;
96   }
97
98   public void setRelationLine(ExtentViewer master, int mouseX, int mouseY)
99   {
100     _relationLineX1 = master.getX() + master.getWidth() / 2;
101     _relationLineY1 = master.getY() + master.getHeight() / 2;
102     _relationLineX2 = mouseX;
103     _relationLineY2 = mouseY;
104   }
105
106   public void clearRelationLine()
107   {
108     _relationLineX1 = -1;
109   }
110
111   public void setScale(double s)
112   {
113     _scale = s;
114   }
115
116   public Dimension JavaDoc getDiagramSizeUnScaled()
117   {
118     int maxY = 0;
119     int maxX = 0;
120     int minY = 0;
121     int minX = 0;
122     for (int i = 0; i < getExtentViewerCount(); i++)
123     {
124       ExtentViewer tv = getExtentViewer(i);
125       if (tv.getX() < minX)
126       {
127         minX = tv.getX();
128       }
129       if (tv.getY() < minY)
130       {
131         minY = tv.getY();
132       }
133       if (tv.getX2() > maxX)
134       {
135         maxX = tv.getX2();
136       }
137       if (tv.getY2() > maxY)
138       {
139         maxY = tv.getY2();
140       }
141     }
142     if (minX < 0)
143     {
144       minX *= -1;
145     }
146     else
147     {
148       minX = 0;
149     }
150     if (minY < 0)
151     {
152       minY *= -1;
153     }
154     else
155     {
156       minY = 0;
157     }
158     for (int i = 0; i < getExtentViewerCount(); i++)
159     {
160       ExtentViewer tv = getExtentViewer(i);
161       tv.setX(tv.getX() + minX);
162       tv.setY(tv.getY() + minY);
163       if (tv.getWidth() < MIN_OBJECT_WIDTH)
164       {
165         tv.setWidth(MIN_OBJECT_WIDTH);
166       }
167       if (tv.getHeight() < MIN_OBJECT_HEIGHT)
168       {
169         tv.setHeight(MIN_OBJECT_HEIGHT);
170       }
171     }
172     for (int i = 0; i < getExtentViewerCount(); i++)
173     {
174       ExtentViewer tv = getExtentViewer(i);
175       if (tv.getX2() + 20 > maxX)
176       {
177         maxX = tv.getX2() + 20;
178       }
179       if (tv.getY2() + 20 > maxY)
180       {
181         maxY = tv.getY2() + 20;
182       }
183     }
184     return new Dimension JavaDoc(maxX, maxY);
185   }
186
187   public Dimension JavaDoc getDiagramSizeScaled()
188   {
189     Dimension JavaDoc dim = getDiagramSizeUnScaled();
190     return new Dimension JavaDoc(scaleUp(dim.width), scaleUp(dim.height));
191   }
192
193   public double getScale()
194   {
195     return _scale;
196   }
197
198   public int scaleUp(int x)
199   {
200     return (int) (x * _scale);
201   }
202
203   public int scaleDown(int x)
204   {
205     return (int) (x / _scale);
206   }
207
208   public int getRealX(int x)
209   {
210     x = (int) (x / _scale);
211     return x - _scrollOffsetX;
212   }
213
214   public int getRealY(int y)
215   {
216     y = (int) (y / _scale);
217     return y - _scrollOffsetY;
218   }
219
220   public ExtentViewer getExtentViewerAt(int x, int y)
221   {
222     for (int i = 0; i < getExtentViewers().size(); i++)
223     {
224       ExtentViewer tv = getExtentViewer(i);
225       if (tv.inBounds(x, y))
226       {
227         return (tv);
228       }
229     }
230     return (null);
231   }
232
233   public void setSelectionBox(int x1, int y1, int x2, int y2)
234   {
235     if (x2 < x1)
236     {
237       int t = x1;
238       x1 = x2;
239       x2 = t;
240     }
241     if (y2 < y1)
242     {
243       int t = y1;
244       y1 = y2;
245       y2 = t;
246     }
247     _selectionBoxX1 = x1;
248     _selectionBoxY1 = y1;
249     _selectionBoxX2 = x2;
250     _selectionBoxY2 = y2;
251   }
252
253   public void clearSelectionBox()
254   {
255     _selectionBoxX1 = -1;
256   }
257
258   public void setManager(DjPersistenceManager mgr)
259   {
260     _mgr = mgr;
261   }
262
263   public void synchronize()
264   {
265     ArrayList JavaDoc all = getExtentViewers();
266
267     for (int i = 0; i < all.size(); i++)
268     {
269       ExtentViewer vw = (ExtentViewer) all.get(i);
270
271       DjExtent ext = null;
272       try
273       {
274         ext = _mgr.getExtentByInternalCode(vw.getExtent().getAlias());
275       }
276       catch (ObjectNotDefinedException onde)
277       {
278         // silent ignore
279
}
280
281       if (ext == null) try
282       {
283         ext = _mgr.getExtent(vw.getExtent().getName());
284       }
285       catch (ObjectNotDefinedException onde)
286       {
287         // silent ignore
288
}
289
290       if (ext == null) try
291       {
292         ext = _mgr.getExtentByObjectType(vw.getExtent().getQualifiedObjectType());
293       }
294       catch (ObjectNotDefinedException onde)
295       {
296         // silent ignore
297
}
298
299       if (ext == null) removeExtentViewer(vw);
300       else vw.setExtent(ext);
301     }
302   }
303
304   public void addExtentViewer(ExtentViewer viewer)
305   {
306     if (!_extentViewers.contains(viewer))
307     {
308       _extentViewers.add(viewer);
309       viewer.setPaintPanel(this);
310     }
311   }
312
313   public ArrayList JavaDoc getExtentViewers()
314   {
315     return _extentViewers;
316   }
317
318   public int getExtentViewerCount()
319   {
320     return _extentViewers.size();
321   }
322
323   public ExtentViewer getExtentViewer(int idx)
324   {
325     return (ExtentViewer) _extentViewers.get(idx);
326   }
327
328   public ExtentViewer getExtentViewer(DjExtent tab)
329   {
330     for (int i = 0; i < getExtentViewerCount(); i++)
331     {
332       if (getExtentViewer(i).getExtent() == tab) return getExtentViewer(i);
333     }
334     return null;
335   }
336
337   public void deselectExtentViewers()
338   {
339     for (int i = 0; i < _extentViewers.size(); i++)
340     {
341       ExtentViewer tv = getExtentViewer(i);
342       tv.setSelected(false);
343     }
344   }
345
346   public ExtentViewer setSelectedExtentViewer(ExtentViewer sel)
347   {
348     if (sel != null)
349     {
350       for (int i = 0; i < _extentViewers.size(); i++)
351       {
352         ExtentViewer tv = getExtentViewer(i);
353         if (tv._extent == sel._extent)
354         {
355           tv.setSelected(true);
356           moveToFront(tv);
357           return tv;
358         }
359       }
360     }
361     return null;
362   }
363
364   private boolean inRange(int p, int x1, int x2)
365   {
366     return p >= x1 && p <= x2;
367   }
368
369   public void selectUsingSelectionBox()
370   {
371     if (_selectionBoxX1 == -1) return;
372     for (int i = 0; i < _extentViewers.size(); i++)
373     {
374       ExtentViewer tv = getExtentViewer(i);
375       if (inRange(tv.getX(), _selectionBoxX1, _selectionBoxX2) && inRange(tv.getX2(), _selectionBoxX1, _selectionBoxX2)
376           && inRange(tv.getY(), _selectionBoxY1, _selectionBoxY2)
377           && inRange(tv.getY2(), _selectionBoxY1, _selectionBoxY2))
378       {
379         tv.setSelected(true);
380       }
381     }
382   }
383
384   public void moveToFront(ExtentViewer tv)
385   {
386     _extentViewers.remove(tv);
387     _extentViewers.add(0, tv);
388   }
389
390   public ExtentViewer[] getSelectedExtentViewers()
391   {
392     ArrayList JavaDoc result = new ArrayList JavaDoc();
393     for (int i = 0; i < _extentViewers.size(); i++)
394     {
395       if (getExtentViewer(i).isSelected()) result.add(getExtentViewer(i));
396     }
397     return (ExtentViewer[]) result.toArray(new ExtentViewer[0]);
398   }
399
400   public DjExtent[] getSelectedExtents()
401   {
402     ExtentViewer[] vws = getSelectedExtentViewers();
403     DjExtent[] result = new DjExtent[vws.length];
404     for (int i = 0; i < vws.length; i++)
405       result[i] = vws[i].getExtent();
406     return result;
407   }
408
409   public void removeExtentViewer(ExtentViewer v)
410   {
411     _extentViewers.remove(v);
412   }
413
414   public void clear()
415   {
416     _extentViewers.clear();
417   }
418
419   private double getBorderX()
420   {
421     int result = 0;
422     for (int i = 0; i < _extentViewers.size(); i++)
423     {
424       ExtentViewer vw = (ExtentViewer) _extentViewers.get(i);
425       if (result < vw.getX2() + PRINT_MARGIN) result = vw.getX2() + PRINT_MARGIN;
426     }
427     return result;
428   }
429
430   private double getBorderY()
431   {
432     int result = 0;
433     for (int i = 0; i < _extentViewers.size(); i++)
434     {
435       ExtentViewer vw = (ExtentViewer) _extentViewers.get(i);
436       if (result < vw.getY2() + PRINT_MARGIN) result = vw.getY2() + PRINT_MARGIN;
437     }
438     return result;
439   }
440
441   public BufferedImage JavaDoc getBufferedImage()
442   {
443     return _bi;
444   }
445
446   public int getScrollOffsetx()
447   {
448     return _scrollOffsetX;
449   }
450
451   public void setScrollOffsetX(int scrollOffsetx)
452   {
453     _scrollOffsetX = scrollOffsetx;
454   }
455
456   public int getScrollOffsetY()
457   {
458     return _scrollOffsetY;
459   }
460
461   public void setScrollOffsetY(int scrollOffsetY)
462   {
463     _scrollOffsetY = scrollOffsetY;
464   }
465
466   public void paint(Graphics JavaDoc destGraphics)
467   {
468     paint(destGraphics, _scale, _scale);
469   }
470
471   public BufferedImage JavaDoc getDiagramImage()
472   {
473     Dimension JavaDoc dim = getDiagramSizeUnScaled();
474     BufferedImage JavaDoc bi = (BufferedImage JavaDoc) createImage(dim.width, dim.height);
475     Graphics2D JavaDoc g2D = bi.createGraphics();
476     g2D.setColor(Color.white);
477     g2D.fillRect(0, 0, dim.width, dim.height);
478     AffineTransform JavaDoc afx = g2D.getTransform();
479     HashMap JavaDoc horzMastersDone = new HashMap JavaDoc();
480     HashMap JavaDoc vertMastersDone = new HashMap JavaDoc();
481     for (int i = _extentViewers.size() - 1; i >= 0; i--)
482     {
483       ((ExtentViewer) (_extentViewers.get(i))).paint(g2D, afx, horzMastersDone, vertMastersDone, getViewMode());
484     }
485     for (int i = _extentViewers.size() - 1; i >= 0; i--)
486     {
487       ((ExtentViewer) (_extentViewers.get(i))).paintLines(g2D, afx, horzMastersDone, vertMastersDone);
488     }
489     return bi;
490   }
491
492   public void paint(Graphics JavaDoc destGraphics, double scaleX, double scaleY)
493   {
494     Dimension JavaDoc sz = getSize();
495     if ((sz.getWidth() <= 0) || sz.getHeight() <= 0) return;
496     if (!_dim.equals(sz))
497     {
498       try
499       {
500         _dim = sz;
501         _bi = (BufferedImage JavaDoc) createImage(_dim.width, _dim.height);
502         _g = _bi.createGraphics();
503       }
504       catch (OutOfMemoryError JavaDoc ooe)
505       {
506         JOptionPane.showMessageDialog(Util.findFrame(this), Messages.getString("ModelEditor.OutOfMemory"), Messages
507             .getString("ModelEditor.OutOfMemoryTitle"), JOptionPane.ERROR_MESSAGE);
508       }
509     }
510     Graphics2D JavaDoc g2D = (Graphics2D JavaDoc) _g;
511
512     // Clears the rectangle that was previously drawn.
513
g2D.setColor(Color.white);
514     g2D.fillRect(0, 0, _dim.width, _dim.height);
515     AffineTransform JavaDoc orgAfx = g2D.getTransform();
516     if (scaleX != 1.0 || scaleY != 1.0)
517     {
518       g2D.scale(scaleX, scaleY);
519     }
520     g2D.translate(_scrollOffsetX, _scrollOffsetY);
521     AffineTransform JavaDoc afx = g2D.getTransform();
522     HashMap JavaDoc horzMastersDone = new HashMap JavaDoc();
523     HashMap JavaDoc vertMastersDone = new HashMap JavaDoc();
524     for (int i = _extentViewers.size() - 1; i >= 0; i--)
525     {
526       ExtentViewer vw = ((ExtentViewer) (_extentViewers.get(i)));
527       vw.paintShadow(g2D);
528     }
529     for (int i = _extentViewers.size() - 1; i >= 0; i--)
530     {
531       ExtentViewer vw = ((ExtentViewer) (_extentViewers.get(i)));
532       vw.paint(g2D, afx, horzMastersDone, vertMastersDone, getViewMode());
533     }
534     for (int i = _extentViewers.size() - 1; i >= 0; i--)
535     {
536       ((ExtentViewer) (_extentViewers.get(i))).paintLines(g2D, afx, horzMastersDone, vertMastersDone);
537     }
538     if (_selectionBoxX1 != -1)
539     {
540       Rectangle JavaDoc rect = new Rectangle JavaDoc();
541       rect.setFrame(_selectionBoxX1, _selectionBoxY1, _selectionBoxX2 - _selectionBoxX1, _selectionBoxY2
542                                                                                          - _selectionBoxY1);
543       g2D.setColor(Color.DARK_GRAY);
544       Stroke JavaDoc orgStroke = g2D.getStroke();
545       BasicStroke JavaDoc dotted = new BasicStroke JavaDoc(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 0,
546           new float[]{3, 3, 3, 3}, 0);
547       g2D.setStroke(dotted);
548       g2D.draw(rect);
549       g2D.setStroke(orgStroke);
550     }
551     if (_relationLineX1 != -1)
552     {
553       g2D.setTransform(afx);
554       g2D.setColor(Color.lightGray);
555       g2D.drawLine(_relationLineX1, _relationLineY1, _relationLineX2, _relationLineY2);
556     }
557     g2D.setTransform(orgAfx);
558     // Draws the buffered image to the screen.
559
destGraphics.drawImage(_bi, 0, 0, this);
560   }
561
562   public int print(Graphics JavaDoc graphics, PageFormat JavaDoc pageFormat, int pageIndex)
563   {
564     if (pageIndex != 0) return Printable.NO_SUCH_PAGE;
565     Graphics2D JavaDoc g2D = (Graphics2D JavaDoc) graphics;
566     double wPage = 0;
567     double hPage = 0;
568     if (pageFormat.getOrientation() == PageFormat.PORTRAIT)
569     {
570       wPage = pageFormat.getImageableWidth();
571       hPage = pageFormat.getImageableHeight();
572     }
573     else
574     {
575       wPage = pageFormat.getImageableWidth();
576       hPage = pageFormat.getImageableHeight();
577     }
578     double scaleX = wPage / getBorderX();
579     double scaleY = hPage / getBorderY();
580     if (scaleX > 1.0 && scaleY > 1.0)
581     {
582       // do not magnify, only scale down.
583
scaleX = 1.0;
584       scaleY = 1.0;
585     }
586     if (scaleY < scaleX) scaleX = scaleY;
587     g2D.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
588     g2D.scale(scaleX, scaleX);
589     AffineTransform JavaDoc afx = g2D.getTransform();
590     HashMap JavaDoc horzMastersDone = new HashMap JavaDoc();
591     HashMap JavaDoc vertMastersDone = new HashMap JavaDoc();
592
593     for (int i = _extentViewers.size() - 1; i >= 0; i--)
594     {
595       ExtentViewer vw = ((ExtentViewer) (_extentViewers.get(i)));
596       vw.paintShadow(g2D);
597     }
598     for (int i = _extentViewers.size() - 1; i >= 0; i--)
599     {
600       ((ExtentViewer) (_extentViewers.get(i))).paint(g2D, afx, horzMastersDone, vertMastersDone, getViewMode());
601     }
602     for (int i = _extentViewers.size() - 1; i >= 0; i--)
603     {
604       ((ExtentViewer) (_extentViewers.get(i))).paintLines(g2D, afx, horzMastersDone, vertMastersDone);
605     }
606     return Printable.PAGE_EXISTS;
607   }
608 }
Popular Tags