001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.pos.screen;
019:
020: import java.awt.Component;
021: import java.awt.Container;
022: import java.awt.Dimension;
023: import java.awt.Frame;
024: import java.awt.Point;
025: import java.awt.Window;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.WindowEvent;
029: import java.awt.event.WindowListener;
030: import java.awt.event.FocusListener;
031: import java.awt.event.FocusEvent;
032: import java.awt.event.ComponentListener;
033: import java.awt.event.ComponentEvent;
034: import javax.swing.JButton;
035: import javax.swing.JDialog;
036: import javax.swing.JRootPane;
037: import javax.swing.SwingUtilities;
038:
039: import net.xoetrope.swing.XButton;
040: import net.xoetrope.swing.XTextArea;
041: import net.xoetrope.xui.XPage;
042: import net.xoetrope.xui.XProjectManager;
043:
044: import org.ofbiz.base.util.Debug;
045: import org.ofbiz.base.util.cache.UtilCache;
046:
047: public class PosDialog {
048:
049: public static final String module = PosDialog.class.getName();
050: protected static UtilCache instances = new UtilCache("pos.Dialogs",
051: 0, 0);
052:
053: protected final Frame clientFrame = XProjectManager
054: .getCurrentProject().getAppFrame();
055: protected final Window appWindow = XProjectManager
056: .getCurrentProject().getAppWindow();
057:
058: protected DialogCallback cb = null;
059: protected Component parent = null;
060:
061: protected JDialog dialog = null;
062: protected XTextArea output = null;
063: protected XButton closeBtn = null;
064: protected XPage page = null;
065: protected boolean modal = true;
066: protected int padding = 0;
067: protected boolean posDialogVisible = false;
068:
069: public static PosDialog getInstance(XPage page) {
070: return getInstance(page, true, 0);
071: }
072:
073: public static PosDialog getInstance(XPage page, boolean modal,
074: int padding) {
075: PosDialog dialog = (PosDialog) instances.get(page);
076: if (dialog == null) {
077: synchronized (PosDialog.class) {
078: dialog = (PosDialog) instances.get(page);
079:
080: if (dialog == null) {
081: dialog = new PosDialog(page, modal, padding);
082: instances.put(page, dialog);
083: }
084: }
085: }
086:
087: dialog.modal = modal;
088: dialog.padding = padding;
089: dialog.pack();
090: return dialog;
091: }
092:
093: protected PosDialog(XPage page, boolean modal, int padding) {
094: this .page = page;
095: this .modal = modal;
096: this .padding = padding;
097: this .configure();
098: }
099:
100: protected void configure() {
101: // create the new dialog box
102: this .dialog = new JDialog(clientFrame, "Alert", modal);
103: dialog.setUndecorated(true);
104: dialog.setResizable(false);
105: dialog.setSize(page.getSize());
106: dialog.getRootPane().setWindowDecorationStyle(
107: JRootPane.PLAIN_DIALOG);
108:
109: // find the output edit object
110: this .output = (XTextArea) page.findComponent("dialog_output");
111: if (this .output != null) {
112: this .output.setWrapStyleWord(true);
113: this .output.setLineWrap(true);
114: this .output.setEditable(false);
115: }
116:
117: // set the page pieces
118: Component[] coms = page.getComponents();
119: for (int i = 0; i < coms.length; i++) {
120: dialog.getContentPane().add(coms[i]);
121: coms[i].setVisible(true);
122: }
123:
124: // set the close button
125: this .setCloseBtn(dialog);
126:
127: // fix the layout and size
128: this .pack();
129:
130: // adjust the dialog location
131: Dimension wSize = dialog.getSize();
132: dialog
133: .setLocation(
134: appWindow.getLocation().x
135: + (appWindow.getSize().width / 2 - wSize.width / 2),
136: appWindow.getLocation().y
137: + (appWindow.getSize().height / 2 - wSize.height / 2));
138:
139: // set the component listener
140: final PosDialog this PosDialog = this ;
141: dialog.addComponentListener(new ComponentListener() {
142:
143: public void componentResized(ComponentEvent event) {
144: this .reset();
145: }
146:
147: public void componentMoved(ComponentEvent event) {
148: this .reset();
149: }
150:
151: public void componentShown(ComponentEvent event) {
152: this .reset();
153: }
154:
155: public void componentHidden(ComponentEvent event) {
156: this .reset();
157: }
158:
159: public void reset() {
160: if (dialog.isEnabled()) {
161: this PosDialog.checkSize();
162: Dimension wSize = dialog.getSize();
163: dialog
164: .setLocation(
165: appWindow.getLocation().x
166: + (appWindow.getSize().width / 2 - wSize.width / 2),
167: appWindow.getLocation().y
168: + (appWindow.getSize().height / 2 - wSize.height / 2));
169: dialog.requestFocus();
170: }
171: }
172: });
173:
174: // set the window listener
175: dialog.addWindowListener(new WindowListener() {
176: public void windowClosing(WindowEvent e) {
177: close();
178: }
179:
180: public void windowActivated(WindowEvent e) {
181: }
182:
183: public void windowDeactivated(WindowEvent e) {
184: this .reset();
185: }
186:
187: public void windowClosed(WindowEvent e) {
188: }
189:
190: public void windowOpened(WindowEvent e) {
191: }
192:
193: public void windowDeiconified(WindowEvent e) {
194: }
195:
196: public void windowIconified(WindowEvent e) {
197: }
198:
199: public void reset() {
200: // always keep focus if we are enabled
201: if (dialog.isEnabled()) {
202: dialog.requestFocus();
203: }
204: }
205: });
206:
207: // set the focus listener
208: dialog.addFocusListener(new FocusListener() {
209:
210: public void focusGained(FocusEvent event) {
211: }
212:
213: public void focusLost(FocusEvent event) {
214: if (dialog.isEnabled()) {
215: Component focused = event.getOppositeComponent();
216: if (focused == null
217: || !"closeBtn".equals(focused.getName())) {
218: dialog.requestFocus();
219: }
220: }
221: }
222: });
223: }
224:
225: public void showDialog(Container parent, DialogCallback cb,
226: String text) {
227: this .parent = parent;
228: this .cb = cb;
229: if (text != null) {
230: this .setText(text);
231: }
232:
233: // don't allow the main window to take focus
234: appWindow.setFocusable(false);
235: parent.setFocusable(false);
236:
237: dialog.setFocusable(true);
238: dialog.setEnabled(true);
239: dialog.requestFocus();
240: dialog.repaint();
241: dialog.pack();
242: posDialogSetVisible(true);
243: }
244:
245: public void setText(String text) {
246: if (this .output != null) {
247: this .output.setText(text);
248: } else if (this .closeBtn != null) {
249: this .closeBtn.setText("<html><center>" + text
250: + "</center></html>");
251: } else {
252: Debug.log("PosDialog output edit box is NULL!", module);
253: }
254: }
255:
256: public String getName() {
257: return page.getName();
258: }
259:
260: protected void close() {
261: // close down the dialog
262: dialog.setEnabled(false);
263: dialog.setVisible(false);
264: dialog.setFocusable(false);
265:
266: // refocus the parent window
267: appWindow.setFocusable(true);
268: appWindow.requestFocus();
269: parent.setFocusable(true);
270: parent.requestFocus();
271:
272: // callback the parent
273: if (cb != null) {
274: cb.receiveDialogCb(this );
275: }
276: }
277:
278: private void setCloseBtn(Container con) {
279: Component[] coms = con.getComponents();
280: for (int i = 0; i < coms.length; i++) {
281: if (coms[i].getName() != null
282: && "closeBtn".equals(coms[i].getName())) {
283: if (coms[i] instanceof XButton) {
284: this .closeBtn = (XButton) coms[i];
285: JButton b = (JButton) coms[i];
286: b.addActionListener(new ActionListener() {
287: public void actionPerformed(ActionEvent event) {
288: dialog.setEnabled(false);
289: close();
290: }
291: });
292: } else {
293: Debug
294: .logWarning(
295: "Found component with name 'closeBtn' but was not an instance of JButton",
296: module);
297: }
298: } else if (coms[i] instanceof Container) {
299: setCloseBtn((Container) coms[i]);
300: } else {
301: coms[i].requestFocus();
302: }
303: }
304: }
305:
306: private void pack() {
307: dialog.pack();
308:
309: Dimension pageSize = page.getSize();
310: if (pageSize.getHeight() > 0 || pageSize.getWidth() > 0) {
311: dialog.setSize(page.getSize());
312: } else {
313: Container contentPane = dialog.getContentPane();
314: Point size = this .getMaxCoordinates(contentPane);
315: this .setSize(size.x + 2 * padding + 2, size.y + 2 * padding
316: + 4);
317: }
318: }
319:
320: private void checkSize() {
321: Dimension wSize = dialog.getSize();
322:
323: Container contentPane = dialog.getContentPane();
324: Point size = this .getMaxCoordinates(contentPane);
325: size.x += 2 * padding + 2;
326: size.y += 2 * padding + 4 + 2;
327: if (size.x != wSize.width || size.y != wSize.height) {
328: this .pack();
329: }
330: }
331:
332: private void setSize(int width, int height) {
333: dialog.getContentPane().setBounds(padding, padding,
334: width - (padding * 2), height - (padding * 2));
335: dialog.setSize(width, height + 2);
336: }
337:
338: private Point getMaxCoordinates(Container cont) {
339: Point pt = cont.getLocation();
340:
341: int maxX = pt.x;
342: int maxY = pt.y;
343: int numChildren = cont.getComponentCount();
344:
345: for (int i = 0; i < numChildren; i++) {
346: Component comp = cont.getComponent(i);
347: Dimension size = comp.getSize();
348: Point p = comp.getLocation();
349: maxX = Math.max(pt.x + p.x + size.width, maxX);
350: maxY = Math.max(pt.y + p.y + size.height, maxY);
351: if (comp instanceof Container) {
352: Point childDim = this
353: .getMaxCoordinates((Container) comp);
354: maxX = Math.max(childDim.x, maxX);
355: maxY = Math.max(childDim.y, maxY);
356: }
357: }
358:
359: return new Point(maxX, maxY);
360: }
361:
362: public void posDialogSetVisible(boolean visible) {
363: posDialogVisible = visible;
364: SwingUtilities.invokeLater(new Runnable() {
365: public void run() {
366: dialog.setVisible(posDialogVisible);
367: }
368: });
369: }
370:
371: }
|