KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > bridge > impl > NbInputHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.tools.ant.module.bridge.impl;
21
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import javax.swing.JComboBox JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JTextField JavaDoc;
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.input.InputHandler;
32 import org.apache.tools.ant.input.InputRequest;
33 import org.apache.tools.ant.input.MultipleChoiceInputRequest;
34 import org.openide.DialogDescriptor;
35 import org.openide.DialogDisplayer;
36 import org.openide.NotifyDescriptor;
37 import org.openide.util.Exceptions;
38 import org.openide.util.HelpCtx;
39 import org.openide.util.NbBundle;
40
41 /**
42  * @author David Konecny, Dusan Balek, Jesse Glick
43  */

44 final class NbInputHandler implements InputHandler {
45
46     private JComboBox JavaDoc combo = null;
47     private JTextField JavaDoc input = null;
48     private final Runnable JavaDoc interestingOutputCallback;
49
50     public NbInputHandler(Runnable JavaDoc interestingOutputCallback) {
51         this.interestingOutputCallback = interestingOutputCallback;
52     }
53
54     public void handleInput(InputRequest request) throws BuildException {
55         interestingOutputCallback.run();
56
57         // #30196 - for one Ant script containing several <input> tasks there will be created
58
// just one instance of the NbInputHandler. So it is necessary to cleanup the instance
59
// used by the previous <input> task first.
60
combo = null;
61         input = null;
62
63         JPanel JavaDoc panel = createPanel(request);
64         DialogDescriptor dlg = new DialogDescriptor(panel,
65         NbBundle.getMessage(NbInputHandler.class, "TITLE_input_handler")); //NOI18N
66
do {
67             DialogDisplayer.getDefault().createDialog(dlg).setVisible(true);
68             if (dlg.getValue() != NotifyDescriptor.OK_OPTION) {
69                 throw new BuildException(NbBundle.getMessage(NbInputHandler.class, "MSG_input_aborted")); //NOI18N
70
}
71             String JavaDoc value;
72             if (combo != null) {
73                 value = (String JavaDoc) combo.getSelectedItem();
74             } else {
75                 value = input.getText();
76             }
77             request.setInput(value);
78         } while (!request.isInputValid());
79     }
80
81     private JPanel JavaDoc createPanel(InputRequest request) {
82
83         JPanel JavaDoc pane = new JPanel JavaDoc();
84         pane.setLayout(new GridBagLayout JavaDoc());
85
86         JLabel JavaDoc jLabel1 = new JLabel JavaDoc(request.getPrompt());
87         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
88         gridBagConstraints.anchor = GridBagConstraints.WEST;
89         gridBagConstraints.insets = new Insets JavaDoc(12, 12, 11, 6);
90         pane.add(jLabel1, gridBagConstraints);
91
92         JComponent JavaDoc comp = null;
93         if (request instanceof MultipleChoiceInputRequest) {
94             combo = new JComboBox JavaDoc(((MultipleChoiceInputRequest)request).getChoices());
95             if (defaultValue != null && defaultValue.length() > 0) {
96                 combo.setSelectedItem(getDefaultValue(request));
97             }
98             comp = combo;
99         } else {
100             input = new JTextField JavaDoc(getDefaultValue(request), 25);
101             comp = input;
102         }
103
104         comp.getAccessibleContext().setAccessibleDescription(
105         NbBundle.getMessage(NbInputHandler.class, "ACSD_input_handler")); // NOI18N
106

107         gridBagConstraints = new GridBagConstraints JavaDoc();
108         gridBagConstraints.gridx = 1;
109         gridBagConstraints.gridy = 0;
110         gridBagConstraints.fill = GridBagConstraints.BOTH;
111         gridBagConstraints.weightx = 1.0;
112         gridBagConstraints.insets = new Insets JavaDoc(12, 6, 11, 6);
113         pane.add(comp, gridBagConstraints);
114
115         jLabel1.setLabelFor(comp);
116         if (request.getPrompt().length() > 0)
117             jLabel1.setDisplayedMnemonic(request.getPrompt().charAt(0));
118
119         pane.getAccessibleContext().setAccessibleName(
120         NbBundle.getMessage(NbInputHandler.class, "TITLE_input_handler")); // NOI18N
121
pane.getAccessibleContext().setAccessibleDescription(
122         NbBundle.getMessage(NbInputHandler.class, "ACSD_input_handler")); // NOI18N
123

124         HelpCtx.setHelpIDString(pane, "org.apache.tools.ant.module.run.NBInputHandler"); // NOI18N
125

126         return pane;
127     }
128
129     private static String JavaDoc defaultValue;
130     static void setDefaultValue(String JavaDoc d) {
131         defaultValue = d;
132     }
133     private static String JavaDoc getDefaultValue(InputRequest req) {
134         try {
135             // Ant 1.7.0+
136
return (String JavaDoc) InputRequest.class.getMethod("getDefaultValue").invoke(req); // NOI18N
137
} catch (NoSuchMethodException JavaDoc e) {
138             // OK, Ant 1.6.5 or earlier
139
} catch (Exception JavaDoc e) {
140             Exceptions.printStackTrace(e);
141         }
142         return defaultValue;
143     }
144
145 }
146
Popular Tags