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

Quick Search    Search Deep

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


1   /* (C) 2000-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 java.awt.AlphaComposite;
21  import java.awt.Composite;
22  import java.awt.Color;
23  import java.awt.Graphics2D;
24  import java.awt.Point;
25  import java.awt.Rectangle;
26  import java.awt.event.MouseEvent;
27  import java.util.HashMap;
28  import java.util.Map;
29  import javax.swing.JComponent;
30  
31  import org.w3c.dom.Element;
32  import org.w3c.dom.NodeList;
33  
34  import iiuf.xmillum.ActionHandler;
35  import iiuf.xmillum.ActionHandlerFactory;
36  import iiuf.xmillum.BrowserContext;
37  import iiuf.xmillum.Displayable;
38  import iiuf.xmillum.DisplayableAppearance;
39  import iiuf.xmillum.DisplayableClass;
40  import iiuf.xmillum.FlagManager;
41  import iiuf.xmillum.Parameter;
42  import iiuf.xmillum.ParameterException;
43  import iiuf.xmillum.Style;
44  
45  import iiuf.dom.DOMUtils;
46  
47  /**
48   * Block
49   *
50   * Represents a rectangular block shown by xmillum.
51   *
52   * <p>ActionHandlers:
53   * <ul>
54   *   <li>over: triggered when the mouse is over this object
55   *   <li>click1: mouse button 1 clicked
56   *   <li>click2: mouse button 2 clicked
57   *   <li>click3: mouse button 3 clicked
58   *   <li>press1: mouse button 1 press & hold
59   *   <li>press2: mouse button 2 press & hold
60   *   <li>press3: mouse button 3 press & hold
61   * </ul>
62   *
63   * @author $Author: ohitz $
64   * @version $Revision: 1.1 $
65   */
66  public class Block extends DisplayableClass {
67   
68    static Map parameters = new HashMap();
69  
70    /**
71     * Set up the parameter handling functions.
72     */
73    static {
74      parameters.put("style", new Parameter() {
75    public void setParam(BrowserContext c, Object o, String v) throws ParameterException {
76      Block b = (Block) o;
77      b.style = c.styleRegistry.getStyle(v);
78    }
79        });
80      parameters.put("click1", new Parameter() {
81    public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
82      Block b = (Block) o;
83      b.click1Handler = v;
84      b.click1HandlerOpt = opt;
85    }
86        });
87      parameters.put("click2", new Parameter() {
88    public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
89      Block b = (Block) o;
90      b.click2Handler = v;
91      b.click2HandlerOpt = opt;
92    }
93        });
94      parameters.put("click3", new Parameter() {
95    public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
96      Block b = (Block) o;
97      b.click3Handler = v;
98      b.click3HandlerOpt = opt;
99    }
100       });
101     parameters.put("press1", new Parameter() {
102   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
103     Block b = (Block) o;
104     b.press1Handler = v;
105     b.press1HandlerOpt = opt;
106   }
107       });
108     parameters.put("press2", new Parameter() {
109   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
110     Block b = (Block) o;
111     b.press2Handler = v;
112     b.press2HandlerOpt = opt;
113   }
114       });
115     parameters.put("press3", new Parameter() {
116   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
117     Block b = (Block) o;
118     b.press3Handler = v;
119     b.press3HandlerOpt = opt;
120   }
121       });
122     parameters.put("over", new Parameter() {
123   public void setParam(BrowserContext c, Object o, String v, String opt) throws ParameterException {
124     Block b = (Block) o;
125     b.overHandler = v;
126     b.overHandlerOpt = opt;
127   }
128       });
129   }
130 
131   /** Style */
132   Style  style;
133 
134   /** Mouse button 1 clicked */
135   String click1Handler = null;
136   String click1HandlerOpt = null;
137 
138   /** Mouse button 2 clicked */
139   String click2Handler = null;
140   String click2HandlerOpt = null;
141 
142   /** Mouse button 3 clicked */
143   String click3Handler = null;
144   String click3HandlerOpt = null;
145 
146   /** Mouse button 1 pressed */
147   String press1Handler = null;
148   String press1HandlerOpt = null;
149 
150   /** Mouse button 2 pressed */
151   String press2Handler = null;
152   String press2HandlerOpt = null;
153 
154   /** Mouse button 3 pressed */
155   String press3Handler = null;
156   String press3HandlerOpt = null;
157 
158   /** Mouse button over the object */
159   String overHandler   = null;
160   String overHandlerOpt = null;
161 
162   /** Holds the current browser context */
163   BrowserContext context;
164 
165   /**
166    * Initializes this class of Displayable.
167    */
168   public void initialize(BrowserContext c, Element e) {
169     context = c;
170     Parameter.setParameters(c, e, this, parameters);
171   }
172 
173   /**
174    * Returns a Block displayable.
175    */
176   public Displayable getDisplayable(Element element) {
177     // Create the new block
178     DisplayableBlock d = new DisplayableBlock(element);
179     try {
180       d.bounds = new Rectangle(Integer.parseInt(element.getAttribute("x")),
181              Integer.parseInt(element.getAttribute("y")),
182              Integer.parseInt(element.getAttribute("w")),
183              Integer.parseInt(element.getAttribute("h")));
184     } catch (NumberFormatException e) {
185     }
186 
187     // Recursively build tree
188     d.childs = getChilds(element, context);
189     return d;
190   }
191 
192   /**
193    * Represents a displayable block.
194    */
195   private class DisplayableBlock extends Displayable {
196     public DisplayableBlock(Element e) {
197       super(e);
198     }
199 
200     public Rectangle getBounds(double scale) {
201       if (bounds != null) {
202   return new Rectangle((int) (scale * bounds.x),
203            (int) (scale * bounds.y),
204            Math.max(1, (int) Math.ceil(scale * bounds.width)),
205            Math.max(1, (int) Math.ceil(scale * bounds.height)));
206       } else {
207   return new Rectangle();
208       }
209     }
210 
211     public void paintObject(Graphics2D g, double scale) {
212       Rectangle b = getBounds(scale);
213 
214       if (style != null) {
215   style.setStyle(g);
216       }
217       Style[] styles = context.flagger.getStyles(element);
218       for (int i = 0; i < styles.length; i++) {
219   styles[i].setStyle(g);
220       }
221 
222       if ((style != null) && style.isFilled()) {
223   g.fill(b);
224       } else {
225   g.draw(b);
226       }
227     }
228 
229     /**
230      * Mouse moved - calls the "over" handler
231      *
232      * @param context Current browser context
233      * @param event Mouse event
234      */
235     public boolean mouseMovedAction(MouseEvent event) {     
236       if (overHandler != null) {
237   context.actionFactory.handleAction(overHandler, overHandlerOpt, this, context);
238   return true;
239       }
240       return false;
241     }
242 
243     /**
244      * Mouse clicked - calls the "clickX" handler
245      *
246      * @param context Current browser context
247      * @param event Mouse event
248      */    
249     public boolean mouseClickedAction(MouseEvent event) {
250       if ((click1Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON1_MASK))) {
251   context.actionFactory.handleAction(click1Handler, click1HandlerOpt, this, context);
252   return true;
253       }
254       if ((click2Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON2_MASK))) {
255   context.actionFactory.handleAction(click2Handler, click2HandlerOpt, this, context);
256   return true;
257       }
258       if ((click3Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON3_MASK))) {
259   context.actionFactory.handleAction(click3Handler, click3HandlerOpt, this, context);
260   return true;
261       }
262       return super.mouseClickedAction(event);
263     }
264 
265     /**
266      * Mouse pressed - calls the "pressX" handler
267      *
268      * @param context Current browser context
269      * @param event Mouse event
270      */
271     public boolean mousePressedAction(MouseEvent event) {
272       if ((press1Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON1_MASK))) {
273   context.actionFactory.handleAction(press1Handler, press1HandlerOpt, this, context);
274   return true;
275       }
276       if ((press2Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON2_MASK))) {
277   context.actionFactory.handleAction(press2Handler, press2HandlerOpt, this, context);
278   return true;
279       }
280       if ((press3Handler != null) && (0 != (event.getModifiers() & MouseEvent.BUTTON3_MASK))) {
281   context.actionFactory.handleAction(press3Handler, press3HandlerOpt, this, context);
282   return true;
283       }
284       return false;
285     }
286   }
287 }