001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings.dialogs;
022:
023: import java.awt.Container;
024: import java.awt.Frame;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.GridLayout;
028: import java.awt.Insets;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.KeyEvent;
032: import java.awt.event.WindowEvent;
033: import com.javujavu.javux.wings.ShortcutAdapter;
034: import com.javujavu.javux.wings.WingButton;
035: import com.javujavu.javux.wings.WingComponent;
036: import com.javujavu.javux.wings.WingDialog;
037: import com.javujavu.javux.wings.WingLabel;
038: import com.javujavu.javux.wings.WingPanel;
039: import com.javujavu.javux.wings.WingScrollPane;
040: import com.javujavu.javux.wings.WingSkin;
041:
042: /**
043: * This class creates, displays, and operates a message box.
044: * The message box contains an application-defined message and title,
045: * plus any combination of push buttons
046: * <br>
047: * <b>This class is thread safe.</b>
048: **/
049: public class WingMessageBox extends WingDialog implements
050: ActionListener {
051: public static final int OKYES = 1;
052: public static final int NO = 2;
053: public static final int CANCEL = 3;
054:
055: protected WingButton bYes;
056: protected WingButton bNo;
057: protected WingButton bCancel;
058: protected ShortcutAdapter sCancel;
059: public int result;
060:
061: public WingMessageBox(Frame owner, String title, Object content,
062: Object labelOKYes, Object labelNo, Object labelCancel) {
063: this (null, owner, title, content, labelOKYes, labelNo,
064: labelCancel, 5, true, GridBagConstraints.HORIZONTAL,
065: GridBagConstraints.BOTH, WingLabel.LEFT);
066: }
067:
068: public WingMessageBox(String styleId, Frame owner, String title,
069: Object content, Object labelOKYes, Object labelNo,
070: Object labelCancel, int gap, boolean scrollContent,
071: int fill, int scrollFill, int labelAlignment) {
072: super (owner, true);
073: getRootPane().setStyleId(styleId);
074:
075: WingPanel pButtons = new WingPanel(new GridLayout(1, 0, gap,
076: gap));
077: pButtons.setStyleId(styleId);
078: if (labelOKYes != null) {
079: pButtons.add(bYes = new WingButton(labelOKYes));
080: bYes.setStyleId(WingSkin.catKeys(styleId, "yes"));
081: bYes.setShortcut(KeyEvent.VK_ENTER, 0);
082: }
083: if (labelNo != null) {
084: pButtons.add(bNo = new WingButton(labelNo));
085: bNo.setStyleId(WingSkin.catKeys(styleId, "no"));
086: if (labelCancel == null)
087: bNo.setShortcut(KeyEvent.VK_ESCAPE, 0);
088: }
089: if (labelCancel != null) {
090: pButtons.add(bCancel = new WingButton(labelCancel));
091: bCancel.setStyleId(WingSkin.catKeys(styleId, "cancel"));
092: bCancel.setShortcut(KeyEvent.VK_ESCAPE, 0);
093: }
094: if (labelNo == null && labelCancel == null) {
095: getRootPane().addShortcut(
096: sCancel = new ShortcutAdapter(KeyEvent.VK_ESCAPE,
097: 0, this ));
098: }
099:
100: WingPanel pContent = new WingPanel(new GridBagLayout());
101: pContent.setStyleId(styleId);
102: GridBagConstraints c = new GridBagConstraints();
103:
104: double weightx = (fill == GridBagConstraints.HORIZONTAL || fill == GridBagConstraints.BOTH) ? 1.0
105: : 0.0;
106: double weighty = (fill == GridBagConstraints.VERTICAL || fill == GridBagConstraints.BOTH) ? 1.0
107: : 0.0;
108: double scrollWeightx = (scrollFill == GridBagConstraints.HORIZONTAL || scrollFill == GridBagConstraints.BOTH) ? 1.0
109: : 0.0;
110: double scrollWeighty = (scrollFill == GridBagConstraints.VERTICAL || scrollFill == GridBagConstraints.BOTH) ? 1.0
111: : 0.0;
112: c.gridy = 0;
113: int i = 0;
114: Object[] rows = (content instanceof Object[]) ? (Object[]) content
115: : null;
116: while (true) {
117: Object row = (rows != null) ? rows[i] : content;
118: Object[] cols = (row instanceof Object[]) ? (Object[]) row
119: : null;
120:
121: int j = 0;
122: c.gridx = 0;
123: while (true) {
124: c.insets = new Insets((i == 0) ? gap : 0,
125: (j == 0) ? gap : 0, gap, gap);
126: c.gridwidth = (cols != null && j < cols.length - 1) ? 1
127: : GridBagConstraints.REMAINDER;
128: Object item = (cols != null) ? cols[j] : row;
129:
130: if (item instanceof WingScrollPane) {
131: c.fill = scrollFill;
132: c.weightx = scrollWeightx;
133: c.weighty = scrollWeighty;
134: } else {
135: c.fill = fill;
136: c.weightx = weightx;
137: c.weighty = weighty;
138: }
139: if (item instanceof WingComponent) {
140: pContent.add((WingComponent) item, c);
141: } else {
142: WingLabel l = new WingLabel(item, labelAlignment);
143: l.setStyleId(styleId);
144: pContent.add(l, c);
145: }
146:
147: c.gridx++;
148: j++;
149: if (cols == null || j == cols.length)
150: break;
151: }
152: c.gridy++;
153: i++;
154: if (rows == null || i == rows.length)
155: break;
156: }
157:
158: WingPanel p = new WingPanel(new GridBagLayout());
159: p.setStyleId(styleId);
160: c = new GridBagConstraints();
161: c.fill = GridBagConstraints.BOTH;
162: c.weightx = 1.0;
163: c.weighty = 1.0;
164: c.gridwidth = GridBagConstraints.REMAINDER;
165: if (scrollContent) {
166: p.add(new WingScrollPane(pContent, false), c);
167: } else {
168: p.add(pContent, c);
169: }
170: int buttonCount = pButtons.getComponentCount();
171: c.fill = GridBagConstraints.NONE;
172: c.weightx = 0.0;
173: c.weighty = 0.0;
174: c.ipadx = buttonCount * 2 * gap;
175: c.anchor = (buttonCount > 1) ? GridBagConstraints.EAST
176: : GridBagConstraints.CENTER;
177: c.insets = new Insets(gap, gap, gap, gap);
178: p.add(pButtons, c);
179:
180: setContentPane(p);
181: setTitle(title);
182: p.addActionListener(this );
183: enableEvents(WindowEvent.WINDOW_EVENT_MASK);
184: }
185:
186: public int showMessage(Container origin) {
187: return showMessage(origin, 0, 0);
188: }
189:
190: public int showMessage(Container origin, int minWidth, int minHeight) {
191: super .packAndCenter(origin, minWidth, minHeight);
192: setVisible(true);
193: return result;
194: }
195:
196: public void actionPerformed(ActionEvent e) {
197: Object src = e.getSource();
198: if (src == bYes)
199: accept();
200: else if (src == bNo)
201: close(NO);
202: else if (src == bCancel || src == sCancel)
203: close(CANCEL);
204: }
205:
206: protected void processWindowEvent(WindowEvent e) {
207: if (e.getID() == WindowEvent.WINDOW_CLOSING)
208: close(CANCEL);
209: super .processWindowEvent(e);
210: }
211:
212: public void accept() {
213: close(OKYES);
214: }
215:
216: public void close(int result) {
217: this .result = result;
218: setVisible(false);
219: dispose();
220: }
221: }
|