001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.bpel.editors.api.utils;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.FlowLayout;
047: import java.awt.Frame;
048: import java.awt.GridBagConstraints;
049: import java.awt.GridBagLayout;
050: import java.awt.Insets;
051: import java.awt.Rectangle;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.awt.event.KeyEvent;
055: import java.util.List;
056:
057: import javax.swing.AbstractAction;
058: import javax.swing.Action;
059: import javax.swing.InputMap;
060: import javax.swing.JComponent;
061: import javax.swing.KeyStroke;
062:
063: import javax.swing.JButton;
064: import javax.swing.JCheckBox;
065: import javax.swing.JComboBox;
066: import javax.swing.JComponent;
067: import javax.swing.JDialog;
068: import javax.swing.JLabel;
069: import javax.swing.JPanel;
070: import javax.swing.JTextField;
071:
072: import org.openide.util.Utilities;
073:
074: import static org.netbeans.modules.soa.ui.util.UI.*;
075:
076: /**
077: * @author Vladimir Yaroslavskiy
078: * @version 2007.11.27
079: */
080: public final class DurationDialog extends JDialog {
081:
082: public DurationDialog() {
083: super ((Frame) null, i18n(DurationDialog.class, "LBL_Duration"),
084: true); // NOI18N
085: a11y(this , i18n(DurationDialog.class, "ACS_Duration")); // NOI18N
086: setLayout(new BorderLayout());
087: add(getResizable(createPanel()), BorderLayout.CENTER);
088: add(createButtonPanel(), BorderLayout.SOUTH);
089:
090: String cancel = "cancel"; // NOI18N
091: KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
092: getRootPane().getInputMap(
093: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(key,
094: cancel);
095:
096: Action action = new AbstractAction() {
097: public void actionPerformed(ActionEvent event) {
098: dispose();
099: }
100: };
101: getRootPane().getActionMap().put(cancel, action);
102:
103: pack();
104:
105: Rectangle r = Utilities.getUsableScreenBounds();
106: int maxW = (r.width * 9) / 10;
107: int maxH = (r.height * 9) / 10;
108: Dimension d = getPreferredSize();
109: d.width = Math.min(d.width, maxW);
110: d.height = Math.min(d.height, maxH);
111: setBounds(Utilities.findCenterBounds(d));
112: }
113:
114: private JPanel createButtonPanel() {
115: JPanel panel = new JPanel();
116: panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
117: JButton button;
118:
119: button = createButton("LBL_OK", "ACS_OK"); // NOI18N
120: getRootPane().setDefaultButton(button);
121: button.addActionListener(new ActionListener() {
122: public void actionPerformed(ActionEvent event) {
123: if (check()) {
124: dispose();
125: }
126: }
127: });
128: panel.add(button);
129:
130: button = createButton("LBL_Cancel", "ACS_Cancel"); // NOI18N
131: button.addActionListener(new ActionListener() {
132: public void actionPerformed(ActionEvent event) {
133: dispose();
134: }
135: });
136: panel.add(button);
137:
138: return panel;
139: }
140:
141: private JButton createButton(String key, String a11y) {
142: JButton button = new JButton(i18n(DurationDialog.class, key));
143: a11y(button, i18n(DurationDialog.class, a11y));
144: return button;
145: }
146:
147: private JPanel createPanel() {
148: JPanel panel = new JPanel(new GridBagLayout());
149: GridBagConstraints c = new GridBagConstraints();
150: c.gridy = 0;
151:
152: myYear = createField("LBL_Year", "ACS_Year", c, panel); // NOI18N
153: myMonth = createField("LBL_Month", "ACS_Month", c, panel); // NOI18N
154: myDay = createField("LBL_Day", "ACS_Day", c, panel); // NOI18N
155:
156: c.gridy++;
157:
158: myHour = createField("LBL_Hour", "ACS_Hour", c, panel); // NOI18N
159: myMinute = createField("LBL_Minute", "ACS_Minute", c, panel); // NOI18N
160: mySecond = createField("LBL_Second", "ACS_Second", c, panel); // NOI18N
161:
162: return panel;
163: }
164:
165: private JTextField createField(String key, String a11y,
166: GridBagConstraints c, JPanel panel) {
167: c.weightx = 0.0;
168: c.fill = GridBagConstraints.NONE;
169: c.insets = new Insets(10, 10, 0, 0);
170: c.anchor = GridBagConstraints.EAST;
171: JLabel label = createLabel(i18n(DurationDialog.class, key));
172: panel.add(label, c);
173:
174: c.weightx = 1.0;
175: c.fill = GridBagConstraints.HORIZONTAL;
176: c.insets = new Insets(10, 10, 0, 0);
177: c.anchor = GridBagConstraints.WEST;
178: JTextField field = new JTextField(DEFAULT_VALUE);
179: a11y(field, i18n(DurationDialog.class, a11y));
180: setWidth(field, TEXT_WIDTH);
181: panel.add(field, c);
182: label.setLabelFor(field);
183:
184: return field;
185: }
186:
187: public String getDuration() {
188: return myDuration;
189: }
190:
191: private boolean check() {
192: int year = getInt(myYear.getText());
193: int month = getInt(myMonth.getText());
194: int day = getInt(myDay.getText());
195: int hour = getInt(myHour.getText());
196: int minute = getInt(myMinute.getText());
197: int second = getInt(mySecond.getText());
198:
199: if (check(year, "ERR_invalid_year", myYear) && // NOI18N
200: check(month, "ERR_invalid_month", myMonth) && // NOI18N
201: check(day, "ERR_invalid_day", myDay) && // NOI18N
202: check(hour, "ERR_invalid_hour", myHour) && // NOI18N
203: check(minute, "ERR_invalid_minute", myMinute) && // NOI18N
204: check(second, "ERR_invalid_second", mySecond)) // NOI18N
205: {
206: myDuration = TimeEventUtil.getContent(true, year, month,
207: day, hour, minute, second);
208: return true;
209: } else {
210: myDuration = null;
211: return false;
212: }
213: }
214:
215: private boolean check(int value, String key, JTextField field) {
216: if (value >= 0) {
217: return true;
218: }
219: printError(i18n(DurationDialog.class, key, field.getText()));
220: field.requestFocus();
221: field.selectAll();
222:
223: return false;
224: }
225:
226: private JTextField myYear;
227: private JTextField myMonth;
228: private JTextField myDay;
229: private JTextField myHour;
230: private JTextField myMinute;
231: private JTextField mySecond;
232: private String myDuration;
233:
234: private static final int TEXT_WIDTH = 60;
235: private static final String DEFAULT_VALUE = "0"; // NOI18N
236: }
|