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-2006 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:
042: package org.apache.tools.ant.module.bridge.impl;
043:
044: import java.awt.GridBagConstraints;
045: import java.awt.GridBagLayout;
046: import java.awt.Insets;
047: import javax.swing.JComboBox;
048: import javax.swing.JComponent;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.JTextField;
052: import org.apache.tools.ant.BuildException;
053: import org.apache.tools.ant.input.InputHandler;
054: import org.apache.tools.ant.input.InputRequest;
055: import org.apache.tools.ant.input.MultipleChoiceInputRequest;
056: import org.openide.DialogDescriptor;
057: import org.openide.DialogDisplayer;
058: import org.openide.NotifyDescriptor;
059: import org.openide.util.Exceptions;
060: import org.openide.util.HelpCtx;
061: import org.openide.util.NbBundle;
062:
063: /**
064: * @author David Konecny, Dusan Balek, Jesse Glick
065: */
066: final class NbInputHandler implements InputHandler {
067:
068: private JComboBox combo = null;
069: private JTextField input = null;
070: private final Runnable interestingOutputCallback;
071:
072: public NbInputHandler(Runnable interestingOutputCallback) {
073: this .interestingOutputCallback = interestingOutputCallback;
074: }
075:
076: public void handleInput(InputRequest request) throws BuildException {
077: interestingOutputCallback.run();
078:
079: // #30196 - for one Ant script containing several <input> tasks there will be created
080: // just one instance of the NbInputHandler. So it is necessary to cleanup the instance
081: // used by the previous <input> task first.
082: combo = null;
083: input = null;
084:
085: JPanel panel = createPanel(request);
086: DialogDescriptor dlg = new DialogDescriptor(panel,
087: NbBundle.getMessage(NbInputHandler.class,
088: "TITLE_input_handler")); //NOI18N
089: do {
090: DialogDisplayer.getDefault().createDialog(dlg).setVisible(
091: true);
092: if (dlg.getValue() != NotifyDescriptor.OK_OPTION) {
093: throw new BuildException(NbBundle.getMessage(
094: NbInputHandler.class, "MSG_input_aborted")); //NOI18N
095: }
096: String value;
097: if (combo != null) {
098: value = (String) combo.getSelectedItem();
099: } else {
100: value = input.getText();
101: }
102: request.setInput(value);
103: } while (!request.isInputValid());
104: }
105:
106: private JPanel createPanel(InputRequest request) {
107:
108: JPanel pane = new JPanel();
109: pane.setLayout(new GridBagLayout());
110:
111: JLabel jLabel1 = new JLabel(request.getPrompt());
112: GridBagConstraints gridBagConstraints = new GridBagConstraints();
113: gridBagConstraints.anchor = GridBagConstraints.WEST;
114: gridBagConstraints.insets = new Insets(12, 12, 11, 6);
115: pane.add(jLabel1, gridBagConstraints);
116:
117: JComponent comp = null;
118: if (request instanceof MultipleChoiceInputRequest) {
119: combo = new JComboBox(
120: ((MultipleChoiceInputRequest) request).getChoices());
121: if (defaultValue != null && defaultValue.length() > 0) {
122: combo.setSelectedItem(getDefaultValue(request));
123: }
124: comp = combo;
125: } else {
126: input = new JTextField(getDefaultValue(request), 25);
127: comp = input;
128: }
129:
130: comp.getAccessibleContext().setAccessibleDescription(
131: NbBundle.getMessage(NbInputHandler.class,
132: "ACSD_input_handler")); // NOI18N
133:
134: gridBagConstraints = new GridBagConstraints();
135: gridBagConstraints.gridx = 1;
136: gridBagConstraints.gridy = 0;
137: gridBagConstraints.fill = GridBagConstraints.BOTH;
138: gridBagConstraints.weightx = 1.0;
139: gridBagConstraints.insets = new Insets(12, 6, 11, 6);
140: pane.add(comp, gridBagConstraints);
141:
142: jLabel1.setLabelFor(comp);
143: if (request.getPrompt().length() > 0)
144: jLabel1.setDisplayedMnemonic(request.getPrompt().charAt(0));
145:
146: pane.getAccessibleContext().setAccessibleName(
147: NbBundle.getMessage(NbInputHandler.class,
148: "TITLE_input_handler")); // NOI18N
149: pane.getAccessibleContext().setAccessibleDescription(
150: NbBundle.getMessage(NbInputHandler.class,
151: "ACSD_input_handler")); // NOI18N
152:
153: HelpCtx.setHelpIDString(pane,
154: "org.apache.tools.ant.module.run.NBInputHandler"); // NOI18N
155:
156: return pane;
157: }
158:
159: private static String defaultValue;
160:
161: static void setDefaultValue(String d) {
162: defaultValue = d;
163: }
164:
165: private static String getDefaultValue(InputRequest req) {
166: try {
167: // Ant 1.7.0+
168: return (String) InputRequest.class.getMethod(
169: "getDefaultValue").invoke(req); // NOI18N
170: } catch (NoSuchMethodException e) {
171: // OK, Ant 1.6.5 or earlier
172: } catch (Exception e) {
173: Exceptions.printStackTrace(e);
174: }
175: return defaultValue;
176: }
177:
178: }
|