KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > screen > PosDialog


1 /*
2  * $Id: PosDialog.java 6465 2006-01-04 06:42:36Z 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.screen;
26
27 import java.awt.Component JavaDoc;
28 import java.awt.Container JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.awt.Frame JavaDoc;
31 import java.awt.Point JavaDoc;
32 import java.awt.Window JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.awt.event.WindowEvent JavaDoc;
36 import java.awt.event.WindowListener JavaDoc;
37 import java.awt.event.FocusListener JavaDoc;
38 import java.awt.event.FocusEvent JavaDoc;
39 import java.awt.event.ComponentListener JavaDoc;
40 import java.awt.event.ComponentEvent JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JDialog JavaDoc;
43 import javax.swing.JRootPane JavaDoc;
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 /**
54  *
55  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
56  * @version $Rev: 6465 $
57  * @since 3.2
58  */

59 public class PosDialog {
60
61     public static final String JavaDoc module = PosDialog.class.getName();
62     protected static UtilCache instances = new UtilCache("pos.Dialogs", 0, 0);
63
64     protected final Frame JavaDoc clientFrame = XProjectManager.getCurrentProject().getAppFrame();
65     protected final Window JavaDoc appWindow = XProjectManager.getCurrentProject().getAppWindow();
66
67     protected DialogCallback cb = null;
68     protected Component JavaDoc parent = null;
69
70     protected JDialog JavaDoc 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         // create the new dialog box
109
this.dialog = new JDialog JavaDoc(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         // find the output edit object
116
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         // set the page pieces
124
Component JavaDoc[] 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         // set the close button
131
this.setCloseBtn(dialog);
132
133         // fix the layout and size
134
this.pack();
135
136         // adjust the dialog location
137
Dimension JavaDoc 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         // set the component listener
142
final PosDialog thisPosDialog = this;
143         dialog.addComponentListener(new ComponentListener JavaDoc() {
144
145             public void componentResized(ComponentEvent JavaDoc event) {
146                 this.reset();
147             }
148
149             public void componentMoved(ComponentEvent JavaDoc event) {
150                 this.reset();
151             }
152
153             public void componentShown(ComponentEvent JavaDoc event) {
154                 this.reset();
155             }
156
157             public void componentHidden(ComponentEvent JavaDoc event) {
158                 this.reset();
159             }
160
161             public void reset() {
162                 if (dialog.isEnabled()) {
163                     thisPosDialog.checkSize();
164                     Dimension JavaDoc 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         // set the window listener
173
dialog.addWindowListener(new WindowListener JavaDoc() {
174             public void windowClosing(WindowEvent JavaDoc e) {
175             }
176
177             public void windowActivated(WindowEvent JavaDoc e) {
178             }
179
180             public void windowDeactivated(WindowEvent JavaDoc e) {
181                 this.reset();
182             }
183
184             public void windowClosed(WindowEvent JavaDoc e) {
185             }
186
187             public void windowOpened(WindowEvent JavaDoc e) {
188             }
189
190             public void windowDeiconified(WindowEvent JavaDoc e) {
191             }
192
193             public void windowIconified(WindowEvent JavaDoc e) {
194             }
195
196             public void reset() {
197                 // always keep focus if we are enabled
198
if (dialog.isEnabled()) {
199                     dialog.requestFocus();
200                 }
201             }
202         });
203
204         // set the focus listener
205
dialog.addFocusListener(new FocusListener JavaDoc() {
206
207             public void focusGained(FocusEvent JavaDoc event) {
208             }
209
210             public void focusLost(FocusEvent JavaDoc event) {
211                 if (dialog.isEnabled()) {
212                     Component JavaDoc 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 JavaDoc parent, DialogCallback cb, String JavaDoc text) {
222         this.parent = parent;
223         this.cb = cb;
224         if (text != null) {
225             this.setText(text);
226         }
227
228         // don't allow the main window to take focus
229
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 JavaDoc 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 JavaDoc getName() {
250         return page.getName();
251     }
252
253     protected void close() {
254         // close down the dialog
255
dialog.setEnabled(false);
256         dialog.setVisible(false);
257         dialog.setFocusable(false);
258
259         // refocus the parent window
260
appWindow.setFocusable(true);
261         appWindow.requestFocus();
262         parent.setFocusable(true);
263         parent.requestFocus();
264
265         // callback the parent
266
if (cb != null) {
267             cb.receiveDialogCb(this);
268         }
269     }
270
271     private void setCloseBtn(Container JavaDoc con) {
272         Component JavaDoc[] 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 JavaDoc b = (JButton JavaDoc) coms[i];
278                     b.addActionListener(new ActionListener JavaDoc() {
279                         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc) {
288                 setCloseBtn((Container JavaDoc) coms[i]);
289             } else {
290                 coms[i].requestFocus();
291             }
292         }
293     }
294
295     private void pack() {
296         dialog.pack();
297
298         Dimension JavaDoc pageSize = page.getSize();
299         if (pageSize.getHeight() > 0 || pageSize.getWidth() > 0) {
300             dialog.setSize(page.getSize());
301         } else {
302             Container JavaDoc contentPane = dialog.getContentPane();
303             Point JavaDoc 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 JavaDoc wSize = dialog.getSize();
310
311         Container JavaDoc contentPane = dialog.getContentPane();
312         Point JavaDoc 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 JavaDoc getMaxCoordinates(Container JavaDoc cont) {
326         Point JavaDoc 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 JavaDoc comp = cont.getComponent(i);
334             Dimension JavaDoc size = comp.getSize();
335             Point JavaDoc 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 JavaDoc) {
339                 Point JavaDoc childDim = this.getMaxCoordinates((Container JavaDoc) comp);
340                 maxX = Math.max(childDim.x, maxX);
341                 maxY = Math.max(childDim.y, maxY);
342             }
343         }
344
345         return new Point JavaDoc(maxX, maxY);
346     }
347
348 }
349
Popular Tags