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

Quick Search    Search Deep

Source code: iiuf/xmillum/displayable/Polygon.java


1   /* (C) 2002, DIUF, http://www.unifr.ch/diuf
2    *
3    * This program is free software; you can redistribute it and/or modify it
4    * under the terms of the GNU General Public License as published by the
5    * Free Software Foundation; either version 2 of the License, or (at your
6    * option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful, but
9    * WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11   * Public License for more details.
12   *
13   * You should have received a copy of the GNU General Public License along
14   * with this program; if not, write to the Free Software Foundation, Inc.,
15   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  package iiuf.xmillum.displayable;
19  
20  import iiuf.xmillum.ActionHandler;
21  import iiuf.xmillum.ActionHandlerFactory;
22  import iiuf.xmillum.BrowserContext;
23  import iiuf.xmillum.Displayable;
24  import iiuf.xmillum.DisplayableAppearance;
25  import iiuf.xmillum.DisplayableClass;
26  import iiuf.xmillum.FlagManager;
27  import iiuf.xmillum.Parameter;
28  import iiuf.xmillum.ParameterException;
29  import iiuf.xmillum.Style;
30  
31  import iiuf.dom.DOMUtils;
32  
33  import java.awt.AlphaComposite;
34  import java.awt.Composite;
35  import java.awt.Color;
36  import java.awt.Graphics2D;
37  import java.awt.Point;
38  import java.awt.Rectangle;
39  import java.awt.event.MouseEvent;
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.Map;
43  import javax.swing.JComponent;
44  
45  import org.w3c.dom.Element;
46  import org.w3c.dom.NodeList;
47  
48  /**
49   * Polygon
50   *
51   * Represents a polygon shown by xmillum.
52   *
53   * <p>ActionHandlers:
54   * <ul>
55   *   <li>over: triggered when the mouse is over this object
56   *   <li>click1: mouse button 1 clicked
57   *   <li>click2: mouse button 2 clicked
58   *   <li>click3: mouse button 3 clicked
59   *   <li>press1: mouse button 1 press & hold
60   *   <li>press2: mouse button 2 press & hold
61   *   <li>press3: mouse button 3 press & hold
62   * </ul>
63   *
64   * @author $Author: ohitz $
65   * @version $Revision: 1.1 $
66   */
67  public class Polygon extends DisplayableClass {
68   
69    static Map parameters = new HashMap();
70  
71    /**
72     * Set up the parameter handling functions.
73     */
74    static {
75      parameters.put("style", new Parameter() {
76    public void setParam(BrowserContext c, Object o, String v) throws ParameterException {
77      Polygon b = (Polygon) o;
78      b.style = c.styleRegistry.getStyle(v);
79    }
80        });
81      parameters.put("click1", new Parameter() {
82    public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
83      Polygon b = (Polygon) o;
84      b.click1Handler = v;
85      b.click1HandlerOpt = opt;
86    }
87        });
88      parameters.put("click2", new Parameter() {
89    public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
90      Polygon b = (Polygon) o;
91      b.click2Handler = v;
92      b.click2HandlerOpt = opt;
93    }
94        });
95      parameters.put("click3", new Parameter() {
96    public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
97      Polygon b = (Polygon) o;
98      b.click3Handler = v;
99      b.click3HandlerOpt = opt;
100   }
101       });
102     parameters.put("press1", new Parameter() {
103   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
104     Polygon b = (Polygon) o;
105     b.press1Handler = v;
106     b.press1HandlerOpt = opt;
107   }
108       });
109     parameters.put("press2", new Parameter() {
110   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
111     Polygon b = (Polygon) o;
112     b.press2Handler = v;
113     b.press2HandlerOpt = opt;
114   }
115       });
116     parameters.put("press3", new Parameter() {
117   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
118     Polygon b = (Polygon) o;
119     b.press3Handler = v;
120     b.press3HandlerOpt = opt;
121   }
122       });
123     parameters.put("over", new Parameter() {
124   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
125     Polygon b = (Polygon) o;
126     b.overHandler = v;
127     b.overHandlerOpt = opt;
128   }
129       });
130   }
131 
132   /** Style */
133   Style  style;
134 
135   /** Mouse button 1 clicked */
136   String click1Handler = null;
137   String click1HandlerOpt = null;
138 
139   /** Mouse button 2 clicked */
140   String click2Handler = null;
141   String click2HandlerOpt = null;
142 
143   /** Mouse button 3 clicked */
144   String click3Handler = null;
145   String click3HandlerOpt = null;
146 
147   /** Mouse button 1 pressed */
148   String press1Handler = null;
149   String press1HandlerOpt = null;
150 
151   /** Mouse button 2 pressed */
152   String press2Handler = null;
153   String press2HandlerOpt = null;
154 
155   /** Mouse button 3 pressed */
156   String press3Handler = null;
157   String press3HandlerOpt = null;
158 
159   /** Mouse button over the object */
160   String overHandler   = null;
161   String overHandlerOpt = null;
162 
163   /** Holds the current browser context */
164   BrowserContext context;
165 
166   /**
167    * Initializes this class of Displayable.
168    */
169   public void initialize(BrowserContext c, Element e) {
170     context = c;
171     Parameter.setParameters(c, e, this, parameters);
172   }
173 
174   /**
175    * Returns a Polygon displayable.
176    */
177   public Displayable getDisplayable(Element element) {
178     Point minPoint = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
179     Point maxPoint = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
180 
181     // Create the new polygon
182     DisplayablePolygon d = new DisplayablePolygon(element);
183     ArrayList points = new ArrayList();
184     
185     NodeList pointElements = DOMUtils.getChildsByTagName(element, "point");
186     for (int i = 0; i < pointElements.getLength(); i++) {
187       Element p    = (Element) pointElements.item(i);
188       try {
189   Point point = new Point(Integer.parseInt(p.getAttribute("x")),
190         Integer.parseInt(p.getAttribute("y")));
191   points.add(point);
192 
193   minPoint.x = Math.min(minPoint.x, point.x);
194   minPoint.y = Math.min(minPoint.y, point.y);
195   maxPoint.x = Math.max(maxPoint.x, point.x);
196   maxPoint.y = Math.max(maxPoint.y, point.y);
197       } catch (NumberFormatException e) {
198       }
199     }
200     d.points = points;
201     d.bounds = new Rectangle(minPoint.x, minPoint.y, maxPoint.x-minPoint.x, maxPoint.y-minPoint.y);
202 
203     return d;
204   }
205 
206   /**
207    * Represents a displayable polygon.
208    */
209   private class DisplayablePolygon extends Displayable {
210     ArrayList points;
211     int[] xpoints;
212     int[] ypoints;
213 
214     public DisplayablePolygon(Element e) {
215       super(e);
216     }
217 
218     int[][] scaledPoints;
219     double  scale = 0.0;
220 
221     int[][] getPoints(double s) {
222       if (scaledPoints == null || scale != s) {
223   scaledPoints = new int[2][points.size()];
224   scale = s;
225 
226   xpoints = new int[points.size()];
227   ypoints = new int[points.size()];
228   for (int i = 0; i < points.size(); i++) {
229     scaledPoints[0][i] = (int) ((double) ((Point) points.get(i)).x * scale);
230     scaledPoints[1][i] = (int) ((double) ((Point) points.get(i)).y * scale);
231   }
232       }
233       return scaledPoints;
234     }
235     
236     public Rectangle getBounds(double scale) {
237       return new Rectangle((int) (scale * bounds.x),
238          (int) (scale * bounds.y),
239          (int) Math.max(1, Math.ceil(scale * bounds.width)),
240          (int) Math.max(1, Math.ceil(scale * bounds.height)));
241     }
242 
243     public void paintObject(Graphics2D g, double scale) {
244       if (style != null) {
245   style.setStyle(g);
246       }
247       Style[] styles = context.flagger.getStyles(element);
248       for (int i = 0; i < styles.length; i++) {
249   styles[i].setStyle(g);
250       }
251 
252       int[][] p = getPoints(scale);
253 
254       if ((style != null) && style.isFilled()) {
255   g.fillPolygon(p[0], p[1], p[0].length);
256       } else {
257   g.drawPolygon(p[0], p[1], p[0].length);
258       }
259     }
260 
261     public boolean mouseMovedAction(MouseEvent event) {     
262       if (overHandler != null) {
263   context.actionFactory.handleAction(overHandler, overHandlerOpt, this, context);
264   return true;
265       }
266       return false;
267     }
268     
269     public boolean mouseClickedAction(MouseEvent event) {
270       if ((click1Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON1_MASK))) {
271   context.actionFactory.handleAction(click1Handler, click1HandlerOpt, this, context);
272   return true;
273       }
274       if ((click2Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON2_MASK))) {
275   context.actionFactory.handleAction(click2Handler, click2HandlerOpt, this, context);
276   return true;
277       }
278       if ((click3Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON3_MASK))) {
279   context.actionFactory.handleAction(click3Handler, click3HandlerOpt, this, context);
280   return true;
281       }
282       return super.mouseClickedAction(event);
283     }
284 
285     public boolean mousePressedAction(MouseEvent event) {
286       if ((press1Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON1_MASK))) {
287   context.actionFactory.handleAction(press1Handler, press1HandlerOpt, this, context);
288   return true;
289       }
290       if ((press2Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON2_MASK))) {
291   context.actionFactory.handleAction(press2Handler, press2HandlerOpt, this, context);
292   return true;
293       }
294       if ((press3Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON3_MASK))) {
295   context.actionFactory.handleAction(press3Handler, press3HandlerOpt, this, context);
296   return true;
297       }
298       return false;
299     }
300 
301     public boolean mouseDraggedAction(MouseEvent event) {
302       return false;
303     }
304   }
305 }