1 25 package org.ofbiz.pos.component; 26 27 import java.awt.Font ; 28 import java.awt.FontMetrics ; 29 import java.awt.Graphics ; 30 import javax.swing.border.Border ; 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 45 public class PosButtonWrapper { 46 47 public static final String module = PosButtonWrapper.class.getName(); 48 private static final String prefix = "<html><center><font color=\"${FCOLOR}\">"; 49 private static final String suffix = "</font></center></html>"; 50 51 private static final String disabledAll = "buttonDisabled"; 52 private static final String 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 origText = null; 60 protected String name = null; 61 62 public PosButtonWrapper(XButton button, String styleName) { 63 this.xbutton = button; 64 this.name = xbutton.getName(); 65 this.origText = xbutton.getText(); 66 67 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 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 try { 88 this.updateText(); 89 } catch (Throwable 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 if (UtilValidate.isEmpty(this.origText)) { 103 return; 104 } 105 106 StringBuffer newContent = new StringBuffer (); 107 XStyle style = null; 108 if (this.isEnabled) { 109 style = enabledStyle; 110 } else { 111 style = disabledStyle; 112 } 113 114 String fcolor = Integer.toHexString(style.getStyleAsColor(XStyle.COLOR_FORE).getRGB() & 0x00ffffff); 116 xbutton.setBackground(style.getStyleAsColor(XStyle.COLOR_BACK)); 117 118 if (fcolor.equals("0")) { 120 fcolor = "#000000"; 121 } else { 122 fcolor = "#" + fcolor; 123 } 124 125 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 newContent.append(prefix.replaceAll("\\$\\{FCOLOR\\}", fcolor)); 137 138 if (isBold) { 140 newContent.append("<b>"); 141 } 142 if (isItalic) { 143 newContent.append("<i>"); 144 } 145 146 newContent.append(wrapText(origText, "<BR>", 0)); 148 149 150 if (isItalic) { 152 newContent.append("</i>"); 153 } 154 if (isBold) { 155 newContent.append("</b>"); 156 } 157 158 newContent.append(suffix); 160 161 Font font = xbutton.getFont().deriveFont(Font.PLAIN); 163 xbutton.setFont(font); 164 165 xbutton.setRolloverEnabled(false); 167 xbutton.setText(newContent.toString()); 168 169 } 171 172 public String wrapText(String text, String newLine, int padding) { 173 if (UtilValidate.isEmpty(text)) { 175 return ""; 176 } 177 178 FontMetrics fm = xbutton.getFontMetrics(xbutton.getFont()); 179 Graphics g = xbutton.getGraphics(); 180 Border b = xbutton.getBorder(); 181 StringBuffer buf = new StringBuffer (); 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 int lineNumber = 0; 199 while (text.length() > 0) { 200 int thisPosition = this.getMaxStrIndex(fm, g, text, butWidth); 201 int space = -1; 202 String line = null; 203 204 if (thisPosition == text.length()) { 206 line = text; 207 buf.append(line); 208 text = new String (); 209 break; 210 } 211 212 line = text.substring(0, thisPosition); 213 space = line.lastIndexOf(32); 215 if (space == -1 && ((int)text.charAt(thisPosition)) != 32) { 219 buf.append(text); 220 break; 221 } else { 222 if (space != -1) { 223 thisPosition = space; 225 line = line.substring(0, thisPosition); 226 227 thisPosition++; 229 } 230 231 lineNumber++; 233 234 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 fm, Graphics g, String 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 |