1 25 package org.ofbiz.pos.screen; 26 27 import java.awt.Component ; 28 import java.awt.Container ; 29 import java.awt.Dimension ; 30 import java.awt.Frame ; 31 import java.awt.Point ; 32 import java.awt.Window ; 33 import java.awt.event.ActionEvent ; 34 import java.awt.event.ActionListener ; 35 import java.awt.event.WindowEvent ; 36 import java.awt.event.WindowListener ; 37 import java.awt.event.FocusListener ; 38 import java.awt.event.FocusEvent ; 39 import java.awt.event.ComponentListener ; 40 import java.awt.event.ComponentEvent ; 41 import javax.swing.JButton ; 42 import javax.swing.JDialog ; 43 import javax.swing.JRootPane ; 44 45 import net.xoetrope.swing.XButton; 46 import net.xoetrope.swing.XTextArea; 47 import net.xoetrope.xui.XPage; 48 import net.xoetrope.xui.XProjectManager; 49 50 import org.ofbiz.base.util.Debug; 51 import org.ofbiz.base.util.cache.UtilCache; 52 53 59 public class PosDialog { 60 61 public static final String module = PosDialog.class.getName(); 62 protected static UtilCache instances = new UtilCache("pos.Dialogs", 0, 0); 63 64 protected final Frame clientFrame = XProjectManager.getCurrentProject().getAppFrame(); 65 protected final Window appWindow = XProjectManager.getCurrentProject().getAppWindow(); 66 67 protected DialogCallback cb = null; 68 protected Component parent = null; 69 70 protected JDialog dialog = null; 71 protected XTextArea output = null; 72 protected XButton closeBtn = null; 73 protected XPage page = null; 74 protected boolean modal = true; 75 protected int padding = 0; 76 77 public static PosDialog getInstance(XPage page) { 78 return getInstance(page, true, 0); 79 } 80 81 public static PosDialog getInstance(XPage page, boolean modal, int padding) { 82 PosDialog dialog = (PosDialog) instances.get(page); 83 if (dialog == null) { 84 synchronized(PosDialog.class) { 85 dialog = (PosDialog) instances.get(page); 86 87 if (dialog == null) { 88 dialog = new PosDialog(page, modal, padding); 89 instances.put(page, dialog); 90 } 91 } 92 } 93 94 dialog.modal = modal; 95 dialog.padding = padding; 96 dialog.pack(); 97 return dialog; 98 } 99 100 protected PosDialog(XPage page, boolean modal, int padding) { 101 this.page = page; 102 this.modal = modal; 103 this.padding = padding; 104 this.configure(); 105 } 106 107 protected void configure() { 108 this.dialog = new JDialog (clientFrame, "Alert", modal); 110 dialog.setUndecorated(true); 111 dialog.setResizable(false); 112 dialog.setSize(page.getSize()); 113 dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 114 115 this.output = (XTextArea) page.findComponent("dialog_output"); 117 if (this.output != null) { 118 this.output.setWrapStyleWord(true); 119 this.output.setLineWrap(true); 120 this.output.setEditable(false); 121 } 122 123 Component [] coms = page.getComponents(); 125 for (int i = 0; i < coms.length; i++) { 126 dialog.getContentPane().add(coms[i]); 127 coms[i].setVisible(true); 128 } 129 130 this.setCloseBtn(dialog); 132 133 this.pack(); 135 136 Dimension wSize = dialog.getSize(); 138 dialog.setLocation(appWindow.getLocation().x + (appWindow.getSize().width / 2 - wSize.width / 2), 139 appWindow.getLocation().y + (appWindow.getSize().height / 2 - wSize.height / 2)); 140 141 final PosDialog thisPosDialog = this; 143 dialog.addComponentListener(new ComponentListener () { 144 145 public void componentResized(ComponentEvent event) { 146 this.reset(); 147 } 148 149 public void componentMoved(ComponentEvent event) { 150 this.reset(); 151 } 152 153 public void componentShown(ComponentEvent event) { 154 this.reset(); 155 } 156 157 public void componentHidden(ComponentEvent event) { 158 this.reset(); 159 } 160 161 public void reset() { 162 if (dialog.isEnabled()) { 163 thisPosDialog.checkSize(); 164 Dimension wSize = dialog.getSize(); 165 dialog.setLocation(appWindow.getLocation().x + (appWindow.getSize().width / 2 - wSize.width / 2), 166 appWindow.getLocation().y + (appWindow.getSize().height / 2 - wSize.height / 2)); 167 dialog.requestFocus(); 168 } 169 } 170 }); 171 172 dialog.addWindowListener(new WindowListener () { 174 public void windowClosing(WindowEvent e) { 175 } 176 177 public void windowActivated(WindowEvent e) { 178 } 179 180 public void windowDeactivated(WindowEvent e) { 181 this.reset(); 182 } 183 184 public void windowClosed(WindowEvent e) { 185 } 186 187 public void windowOpened(WindowEvent e) { 188 } 189 190 public void windowDeiconified(WindowEvent e) { 191 } 192 193 public void windowIconified(WindowEvent e) { 194 } 195 196 public void reset() { 197 if (dialog.isEnabled()) { 199 dialog.requestFocus(); 200 } 201 } 202 }); 203 204 dialog.addFocusListener(new FocusListener () { 206 207 public void focusGained(FocusEvent event) { 208 } 209 210 public void focusLost(FocusEvent event) { 211 if (dialog.isEnabled()) { 212 Component focused = event.getOppositeComponent(); 213 if (focused == null || !"closeBtn".equals(focused.getName())) { 214 dialog.requestFocus(); 215 } 216 } 217 } 218 }); 219 } 220 221 public void showDialog(Container parent, DialogCallback cb, String text) { 222 this.parent = parent; 223 this.cb = cb; 224 if (text != null) { 225 this.setText(text); 226 } 227 228 appWindow.setFocusable(false); 230 parent.setFocusable(false); 231 232 dialog.setFocusable(true); 233 dialog.setEnabled(true); 234 dialog.setVisible(true); 235 dialog.requestFocus(); 236 dialog.repaint(); 237 } 238 239 public void setText(String text) { 240 if (this.output != null) { 241 this.output.setText(text); 242 } else if (this.closeBtn != null) { 243 this.closeBtn.setText("<html><center>" + text + "</center></html>"); 244 } else { 245 Debug.log("PosDialog output edit box is NULL!", module); 246 } 247 } 248 249 public String getName() { 250 return page.getName(); 251 } 252 253 protected void close() { 254 dialog.setEnabled(false); 256 dialog.setVisible(false); 257 dialog.setFocusable(false); 258 259 appWindow.setFocusable(true); 261 appWindow.requestFocus(); 262 parent.setFocusable(true); 263 parent.requestFocus(); 264 265 if (cb != null) { 267 cb.receiveDialogCb(this); 268 } 269 } 270 271 private void setCloseBtn(Container con) { 272 Component [] coms = con.getComponents(); 273 for (int i = 0; i < coms.length; i++) { 274 if (coms[i].getName() != null && "closeBtn".equals(coms[i].getName())) { 275 if (coms[i] instanceof XButton) { 276 this.closeBtn = (XButton) coms[i]; 277 JButton b = (JButton ) coms[i]; 278 b.addActionListener(new ActionListener () { 279 public void actionPerformed(ActionEvent event) { 280 dialog.setEnabled(false); 281 close(); 282 } 283 }); 284 } else { 285 Debug.logWarning("Found component with name 'closeBtn' but was not an instance of JButton", module); 286 } 287 } else if (coms[i] instanceof Container ) { 288 setCloseBtn((Container ) coms[i]); 289 } else { 290 coms[i].requestFocus(); 291 } 292 } 293 } 294 295 private void pack() { 296 dialog.pack(); 297 298 Dimension pageSize = page.getSize(); 299 if (pageSize.getHeight() > 0 || pageSize.getWidth() > 0) { 300 dialog.setSize(page.getSize()); 301 } else { 302 Container contentPane = dialog.getContentPane(); 303 Point size = this.getMaxCoordinates(contentPane); 304 this.setSize(size.x + 2 * padding + 2, size.y + 2 * padding + 4); 305 } 306 } 307 308 private void checkSize() { 309 Dimension wSize = dialog.getSize(); 310 311 Container contentPane = dialog.getContentPane(); 312 Point size = this.getMaxCoordinates(contentPane); 313 size.x += 2 * padding + 2; 314 size.y += 2 * padding + 4 + 2; 315 if ( size.x != wSize.width || size.y != wSize.height ) { 316 this.pack(); 317 } 318 } 319 320 private void setSize(int width, int height) { 321 dialog.getContentPane().setBounds(padding, padding, width - (padding * 2), height - (padding * 2)); 322 dialog.setSize(width, height + 2); 323 } 324 325 private Point getMaxCoordinates(Container cont) { 326 Point pt = cont.getLocation(); 327 328 int maxX = pt.x; 329 int maxY = pt.y; 330 int numChildren = cont.getComponentCount(); 331 332 for (int i = 0; i < numChildren; i++) { 333 Component comp = cont.getComponent(i); 334 Dimension size = comp.getSize(); 335 Point p = comp.getLocation(); 336 maxX = Math.max(pt.x + p.x + size.width, maxX); 337 maxY = Math.max(pt.y + p.y + size.height, maxY); 338 if (comp instanceof Container ) { 339 Point childDim = this.getMaxCoordinates((Container ) comp); 340 maxX = Math.max(childDim.x, maxX); 341 maxY = Math.max(childDim.y, maxY); 342 } 343 } 344 345 return new Point (maxX, maxY); 346 } 347 348 } 349 | Popular Tags |