KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > component > PosButtonWrapper


1 /*
2  * $Id: PosButtonWrapper.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.pos.component;
26
27 import java.awt.Font JavaDoc;
28 import java.awt.FontMetrics JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import javax.swing.border.Border JavaDoc;
31
32 import net.xoetrope.swing.XButton;
33 import net.xoetrope.xui.XProjectManager;
34 import net.xoetrope.xui.style.XStyle;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilValidate;
38
39 /**
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 3.1
44  */

45 public class PosButtonWrapper {
46
47     public static final String JavaDoc module = PosButtonWrapper.class.getName();
48     private static final String JavaDoc prefix = "<html><center><font color=\"${FCOLOR}\">";
49     private static final String JavaDoc suffix = "</font></center></html>";
50
51     private static final String JavaDoc disabledAll = "buttonDisabled";
52     private static final String JavaDoc enabledMenu = "posButton";
53
54     protected XStyle disabledStyle = null;
55     protected XStyle enabledStyle = null;
56     protected XButton xbutton = null;
57
58     protected boolean isEnabled = true;
59     protected String JavaDoc origText = null;
60     protected String JavaDoc name = null;
61
62     public PosButtonWrapper(XButton button, String JavaDoc styleName) {
63         this.xbutton = button;
64         this.name = xbutton.getName();
65         this.origText = xbutton.getText();
66
67         // load the disabled style
68
this.disabledStyle = XProjectManager.getStyleManager().getStyle(disabledAll);
69         if (this.disabledStyle == null) {
70             Debug.logError("ERROR: The disabled button style \"buttonDisabled\" was not found!", module);
71         }
72
73         // load the enabled style
74
if (styleName != null) {
75             this.enabledStyle = XProjectManager.getStyleManager().getStyle(styleName);
76             if (this.enabledStyle == null) {
77                 Debug.logError("ERROR: The enabled button style \""+ styleName + "\" was not found!", module);
78             }
79         } else {
80             this.enabledStyle = XProjectManager.getStyleManager().getStyle(enabledMenu);
81             if (this.enabledStyle == null) {
82                 Debug.logError("ERROR: The enabled button style \""+ enabledMenu + "\" was not found!", module);
83             }
84         }
85
86         // wrap the text in HTML and set colors
87
try {
88             this.updateText();
89         } catch (Throwable JavaDoc t) {
90             Debug.logError(t, module);
91         }
92     }
93
94     public void setEnabled(boolean enable) {
95         this.isEnabled = enable;
96         this.updateText();
97         xbutton.setEnabled(enable);
98     }
99
100     public void updateText() {
101         // no text to output; nothing to do
102
if (UtilValidate.isEmpty(this.origText)) {
103             return;
104         }
105
106         StringBuffer JavaDoc newContent = new StringBuffer JavaDoc();
107         XStyle style = null;
108         if (this.isEnabled) {
109             style = enabledStyle;
110         } else {
111             style = disabledStyle;
112         }
113
114         // get the hex color for the current style
115
String JavaDoc fcolor = Integer.toHexString(style.getStyleAsColor(XStyle.COLOR_FORE).getRGB() & 0x00ffffff);
116         xbutton.setBackground(style.getStyleAsColor(XStyle.COLOR_BACK));
117
118         // add the # for the HTML color
119
if (fcolor.equals("0")) {
120             fcolor = "#000000";
121         } else {
122             fcolor = "#" + fcolor;
123         }
124
125         // get the additional styles
126
boolean isItalic = false;
127         boolean isBold = false;
128         if (style.getStyleAsInt(XStyle.FONT_WEIGHT) > 0) {
129             isBold = true;
130         }
131         if (style.getStyleAsInt(XStyle.FONT_ITALIC) > 0) {
132             isItalic = true;
133         }
134
135         // open with prefix (opening tags)
136
newContent.append(prefix.replaceAll("\\$\\{FCOLOR\\}", fcolor));
137
138         // add in additional styles
139
if (isBold) {
140             newContent.append("<b>");
141         }
142         if (isItalic) {
143             newContent.append("<i>");
144         }
145
146         // the actual wrapped text
147
newContent.append(wrapText(origText, "<BR>", 0));
148
149
150         // close the additional styles
151
if (isItalic) {
152             newContent.append("</i>");
153         }
154         if (isBold) {
155             newContent.append("</b>");
156         }
157
158         // append the suffix (closing tags)
159
newContent.append(suffix);
160
161         // set the button font
162
Font JavaDoc font = xbutton.getFont().deriveFont(Font.PLAIN);
163         xbutton.setFont(font);
164
165         // update the button text
166
xbutton.setRolloverEnabled(false);
167         xbutton.setText(newContent.toString());
168
169         //Debug.log("Button [" + name + "] = " + xbutton.getText(), module);
170
}
171
172     public String JavaDoc wrapText(String JavaDoc text, String JavaDoc newLine, int padding) {
173         // nothing to do
174
if (UtilValidate.isEmpty(text)) {
175             return "";
176         }
177
178         FontMetrics JavaDoc fm = xbutton.getFontMetrics(xbutton.getFont());
179         Graphics JavaDoc g = xbutton.getGraphics();
180         Border JavaDoc b = xbutton.getBorder();
181         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
182
183         int leftBorder = b.getBorderInsets(xbutton).left;
184         int rightBorder = b.getBorderInsets(xbutton).right;
185         int topBorder = b.getBorderInsets(xbutton).top;
186         int bottomBorder = b.getBorderInsets(xbutton).bottom;
187
188         int padWidth = ((int) (fm.getStringBounds((new char[] {(char) 32}), 0, 1, g).getWidth()) * padding);
189         int butWidth = (((xbutton.getSize().width) - leftBorder - rightBorder) - padWidth);
190         int maxLines = ((xbutton.getSize().height - topBorder - bottomBorder) / fm.getHeight());
191         int strWidth = (int) fm.getStringBounds(text, g).getWidth();
192
193         if (strWidth <= butWidth) {
194             return text;
195         }
196
197         // if not we need to wrap the text
198
int lineNumber = 0;
199         while (text.length() > 0) {
200             int thisPosition = this.getMaxStrIndex(fm, g, text, butWidth);
201             int space = -1;
202             String JavaDoc line = null;
203
204             // the end of the string has been reached
205
if (thisPosition == text.length()) {
206                 line = text;
207                 buf.append(line);
208                 text = new String JavaDoc();
209                 break;
210             }
211
212             line = text.substring(0, thisPosition);
213             space = line.lastIndexOf(32); // last space
214

215             // we can only wrap if :
216
// 1) we have a space available
217
// 2) the next character is a space
218
if (space == -1 && ((int)text.charAt(thisPosition)) != 32) {
219                 buf.append(text);
220                 break;
221             } else {
222                 if (space != -1) {
223                     // we found a space; use that location to wrap
224
thisPosition = space;
225                     line = line.substring(0, thisPosition);
226
227                     // move forward one to trim off the space
228
thisPosition++;
229                 }
230
231                 // increment the line counter; we've added a line
232
lineNumber++;
233
234                 // make sure we don't have too many lines;
235
// if so, trim it down ...
236
if (lineNumber >= maxLines) {
237                     int dotWidth = (int) fm.getStringBounds("...", g).getWidth();
238                     int maxLineIdx = this.getMaxStrIndex(fm, g, text, (butWidth - dotWidth));
239                     line = text.substring(0, maxLineIdx);
240                     buf.append(line);
241                     buf.append("...");
242                     break;
243                 } else {
244                     text = text.substring(thisPosition);
245                     buf.append(line);
246                     buf.append(newLine);
247                 }
248             }
249         }
250
251         return buf.toString();
252     }
253
254     private int getMaxStrIndex(FontMetrics JavaDoc fm, Graphics JavaDoc g, String JavaDoc str, int width) {
255         for (int i = str.length(); i > 0; i--) {
256             double widthTest = fm.getStringBounds(str.substring(0, i), g).getWidth();
257             if (widthTest < width) {
258                 return i;
259             }
260         }
261         return str.length();
262     }
263 }
264
Popular Tags