KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > toolbar > ToolButton


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.toolbar;
14
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.w3c.dom.Element JavaDoc;
19
20 import com.tonbeller.wcf.controller.RequestContext;
21 import com.tonbeller.wcf.controller.RequestListener;
22 import com.tonbeller.wcf.utils.DomUtils;
23
24 /**
25  * @author av
26  */

27 public class ToolButton extends ToolBarComponentSupport implements RequestListener {
28   private String JavaDoc tooltip;
29   String JavaDoc upImg;
30   String JavaDoc downImg;
31   boolean pressed;
32   String JavaDoc radioGroup;
33
34   ToolBar toolBar;
35   ToolButtonModel model;
36
37   String JavaDoc requestId;
38
39   public ToolButton(ToolButtonModel model, String JavaDoc upImg, String JavaDoc downImg) {
40     this.model = model;
41     this.upImg = upImg;
42     this.downImg = downImg;
43   }
44
45   /**
46    * Returns the pressed.
47    * @return boolean
48    */

49   public boolean isPressed() {
50     return pressed;
51   }
52
53   /**
54    * Returns the radioGroup.
55    * @return String
56    */

57   public String JavaDoc getRadioGroup() {
58     return radioGroup;
59   }
60
61   /**
62    * Sets the radioGroup.
63    * @param radioGroup The radioGroup to set
64    */

65   public void setRadioGroup(String JavaDoc radioGroup) {
66     this.radioGroup = radioGroup;
67   }
68
69   /**
70    * Returns the downImg.
71    * @return String
72    */

73   public String JavaDoc getDownImg() {
74     return downImg;
75   }
76
77   /**
78    * Returns the upImg.
79    * @return String
80    */

81   public String JavaDoc getUpImg() {
82     return upImg;
83   }
84
85   /**
86    * Sets the downImg.
87    * @param downImg The downImg to set
88    */

89   public void setDownImg(String JavaDoc downImg) {
90     this.downImg = downImg;
91   }
92
93   /**
94    * Sets the upImg.
95    * @param upImg The upImg to set
96    */

97   public void setUpImg(String JavaDoc upImg) {
98     this.upImg = upImg;
99   }
100
101   public void initialize(RequestContext context, ToolBar owner) {
102     this.toolBar = (ToolBar) owner;
103     // stay compatible - many tests have to be rewritten to use the local names
104
// so if no buttonIdPrefix is set, we still use the plain button ids
105
if (toolBar.isGlobalButtonIds())
106       this.requestId = getId();
107     else
108       this.requestId = toolBar.getId() + "." + getId();
109     owner.getDispatcher().addRequestListener(requestId, null, this);
110   }
111
112   public void render(RequestContext context, Element JavaDoc parent) throws Exception JavaDoc {
113     // synchronize with model
114
pressed = model.isPressed(context);
115
116     Element JavaDoc elem = DomUtils.appendElement(parent, "tool-button");
117     elem.setAttribute("id", requestId);
118     if (pressed)
119       elem.setAttribute("img", downImg);
120     else
121       elem.setAttribute("img", upImg);
122     elem.setAttribute("title", toolBar.getTooltip(tooltip));
123   }
124
125   public void request(RequestContext context) throws Exception JavaDoc {
126     List JavaDoc list = toolBar.getRadioGroup(this);
127     if (list != null) {
128       for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
129         ToolButton btn = (ToolButton) it.next();
130         if (btn != this)
131           btn.model.setPressed(context, false);
132       }
133     }
134
135     // flip the button
136
pressed = model.isPressed(context);
137     pressed = !pressed;
138     model.setPressed(context, pressed);
139     // sync with model
140
pressed = model.isPressed(context);
141   }
142
143   /**
144    * @param tooltip
145    */

146   public void setTooltip(String JavaDoc tooltip) {
147     this.tooltip = tooltip;
148   }
149
150   public boolean isSeparator() {
151     return false;
152   }
153
154   public void setImg(String JavaDoc img) {
155     setUpImg(img + "-up");
156     setDownImg(img + "-down");
157   }
158
159 }
Popular Tags