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.netbeans.modules.mobility.project.deployment;
043:
044: import java.awt.Component;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.Vector;
050: import javax.swing.DefaultComboBoxModel;
051: import javax.swing.JList;
052: import javax.swing.JPanel;
053: import javax.swing.ListCellRenderer;
054: import javax.swing.event.DocumentEvent;
055: import javax.swing.event.DocumentListener;
056: import org.netbeans.spi.mobility.deployment.DeploymentPlugin;
057: import org.openide.DialogDescriptor;
058: import org.openide.util.Lookup;
059: import org.openide.util.NbBundle;
060: import org.openide.util.Utilities;
061:
062: /**
063: *
064: * @author Adam Sotona
065: */
066: public class NewInstanceDialog extends JPanel implements
067: DocumentListener, ActionListener {
068:
069: private final MobilityDeploymentProperties props;
070: private DialogDescriptor dd;
071: private Collection<String> invalidNames = Collections.EMPTY_SET;
072:
073: /** Creates new form NewInstanceDialog */
074: public NewInstanceDialog(MobilityDeploymentProperties props,
075: DeploymentPlugin selected) {
076: this .props = props;
077: initComponents();
078: final ListCellRenderer r = jComboBoxType.getRenderer();
079: jComboBoxType.setRenderer(new ListCellRenderer() {
080: public Component getListCellRendererComponent(JList list,
081: Object value, int index, boolean isSelected,
082: boolean cellHasFocus) {
083: return r
084: .getListCellRendererComponent(
085: list,
086: value instanceof DeploymentPlugin ? ((DeploymentPlugin) value)
087: .getDeploymentMethodDisplayName()
088: : value, index, isSelected,
089: cellHasFocus);
090: }
091: });
092: Vector<DeploymentPlugin> v = new Vector();
093: for (DeploymentPlugin d : Lookup.getDefault().lookupAll(
094: DeploymentPlugin.class)) {
095: if (d.getGlobalPropertyDefaultValues().size() > 0)
096: v.add(d);
097: }
098: jComboBoxType.setModel(new DefaultComboBoxModel(v));
099: if (selected != null)
100: jComboBoxType.setSelectedItem(selected);
101: jComboBoxType.addActionListener(this );
102: jTextFieldName.getDocument().addDocumentListener(this );
103: }
104:
105: public void setDialogDescriptor(DialogDescriptor dd) {
106: this .dd = dd;
107: actionPerformed(null);
108: }
109:
110: /** This method is called from within the constructor to
111: * initialize the form.
112: * WARNING: Do NOT modify this code. The content of this method is
113: * always regenerated by the Form Editor.
114: */
115: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
116: private void initComponents() {
117:
118: jLabelType = new javax.swing.JLabel();
119: jComboBoxType = new javax.swing.JComboBox();
120: jLabelName = new javax.swing.JLabel();
121: jTextFieldName = new javax.swing.JTextField();
122: jLabelError = new javax.swing.JLabel();
123:
124: jLabelType.setLabelFor(jComboBoxType);
125: org.openide.awt.Mnemonics.setLocalizedText(jLabelType, NbBundle
126: .getMessage(NewInstanceDialog.class,
127: "NewInstanceDialog.jLabelType.text")); // NOI18N
128:
129: jLabelName.setLabelFor(jTextFieldName);
130: org.openide.awt.Mnemonics.setLocalizedText(jLabelName, NbBundle
131: .getMessage(NewInstanceDialog.class,
132: "NewInstanceDialog.jLabelName.text")); // NOI18N
133:
134: jLabelError.setForeground(new java.awt.Color(89, 79, 191));
135:
136: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
137: this );
138: this .setLayout(layout);
139: layout
140: .setHorizontalGroup(layout
141: .createParallelGroup(
142: org.jdesktop.layout.GroupLayout.LEADING)
143: .add(
144: layout
145: .createSequentialGroup()
146: .addContainerGap()
147: .add(
148: layout
149: .createParallelGroup(
150: org.jdesktop.layout.GroupLayout.LEADING)
151: .add(
152: org.jdesktop.layout.GroupLayout.TRAILING,
153: jLabelError,
154: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
155: 421,
156: Short.MAX_VALUE)
157: .add(
158: layout
159: .createSequentialGroup()
160: .add(
161: layout
162: .createParallelGroup(
163: org.jdesktop.layout.GroupLayout.LEADING,
164: false)
165: .add(
166: jLabelType,
167: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
168: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
169: Short.MAX_VALUE)
170: .add(
171: jLabelName,
172: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
173: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
174: Short.MAX_VALUE))
175: .addPreferredGap(
176: org.jdesktop.layout.LayoutStyle.RELATED)
177: .add(
178: layout
179: .createParallelGroup(
180: org.jdesktop.layout.GroupLayout.LEADING)
181: .add(
182: jTextFieldName,
183: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
184: 256,
185: Short.MAX_VALUE)
186: .add(
187: jComboBoxType,
188: 0,
189: 256,
190: Short.MAX_VALUE))))
191: .addContainerGap()));
192: layout
193: .setVerticalGroup(layout
194: .createParallelGroup(
195: org.jdesktop.layout.GroupLayout.LEADING)
196: .add(
197: layout
198: .createSequentialGroup()
199: .addContainerGap()
200: .add(
201: layout
202: .createParallelGroup(
203: org.jdesktop.layout.GroupLayout.BASELINE)
204: .add(
205: jComboBoxType,
206: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
207: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
208: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
209: .add(jLabelType))
210: .addPreferredGap(
211: org.jdesktop.layout.LayoutStyle.RELATED)
212: .add(
213: layout
214: .createParallelGroup(
215: org.jdesktop.layout.GroupLayout.BASELINE)
216: .add(jLabelName)
217: .add(
218: jTextFieldName,
219: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
220: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
221: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
222: .addPreferredGap(
223: org.jdesktop.layout.LayoutStyle.RELATED)
224: .add(jLabelError)
225: .addContainerGap(25,
226: Short.MAX_VALUE)));
227:
228: jLabelType.getAccessibleContext().setAccessibleName(
229: org.openide.util.NbBundle.getMessage(
230: NewInstanceDialog.class,
231: "ACCESSIBLE_NAME_jLabelType")); // NOI18N
232: jLabelType.getAccessibleContext().setAccessibleDescription(
233: org.openide.util.NbBundle.getMessage(
234: NewInstanceDialog.class,
235: "ACCESSIBLE_DESCRIPTION_jLabelType")); // NOI18N
236: jComboBoxType.getAccessibleContext().setAccessibleName(
237: org.openide.util.NbBundle.getMessage(
238: NewInstanceDialog.class,
239: "ACCESSIBLE_NAME_jComboBoxType")); // NOI18N
240: jComboBoxType.getAccessibleContext().setAccessibleDescription(
241: org.openide.util.NbBundle.getMessage(
242: NewInstanceDialog.class,
243: "ACCESSIBLE_DESCRIPTION_jComboBoxType")); // NOI18N
244: jLabelName.getAccessibleContext().setAccessibleName(
245: org.openide.util.NbBundle.getMessage(
246: NewInstanceDialog.class,
247: "ACCESSIBLE_NAME_jLabelName")); // NOI18N
248: jLabelName.getAccessibleContext().setAccessibleDescription(
249: org.openide.util.NbBundle.getMessage(
250: NewInstanceDialog.class,
251: "ACCESSIBLE_DESCRIPTION_jLabelName")); // NOI18N
252: jTextFieldName.getAccessibleContext().setAccessibleName(
253: org.openide.util.NbBundle.getMessage(
254: NewInstanceDialog.class,
255: "ACCESSIBLE_NAME_jTextFieldName")); // NOI18N
256: jTextFieldName.getAccessibleContext().setAccessibleDescription(
257: org.openide.util.NbBundle.getMessage(
258: NewInstanceDialog.class,
259: "ACCESSIBLE_DESCRIPTION_jLabelName")); // NOI18N
260: }// </editor-fold>//GEN-END:initComponents
261:
262: public void actionPerformed(ActionEvent e) {
263: DeploymentPlugin dp = getDeploymentPlugin();
264: invalidNames = dp == null ? Collections.EMPTY_SET : props
265: .getInstanceList(dp.getDeploymentMethodName());
266: changedUpdate(null);
267: }
268:
269: public void insertUpdate(DocumentEvent e) {
270: changedUpdate(e);
271: }
272:
273: public void removeUpdate(DocumentEvent e) {
274: changedUpdate(e);
275: }
276:
277: public void changedUpdate(DocumentEvent e) {
278: String name = getInstanceName();
279: if (invalidNames.contains(name)) {
280: jLabelError.setText(NbBundle.getMessage(
281: NewInstanceDialog.class, "ERR_InstanceExists")); // NOI18N
282: jLabelError.setVisible(true);
283: if (dd != null)
284: dd.setValid(false);
285: } else if (!Utilities.isJavaIdentifier(name)) {
286: jLabelError.setText(NbBundle.getMessage(
287: NewInstanceDialog.class, "ERR_InvalidName")); // NOI18N
288: jLabelError.setVisible(true);
289: if (dd != null)
290: dd.setValid(false);
291: } else {
292: jLabelError.setVisible(false);
293: if (dd != null)
294: dd.setValid(true);
295: }
296: }
297:
298: public DeploymentPlugin getDeploymentPlugin() {
299: return (DeploymentPlugin) jComboBoxType.getSelectedItem();
300: }
301:
302: public String getInstanceName() {
303: return jTextFieldName.getText();
304: }
305:
306: public void addNotify() {
307: super .addNotify();
308: jTextFieldName.requestFocusInWindow();
309: }
310:
311: // Variables declaration - do not modify//GEN-BEGIN:variables
312: private javax.swing.JComboBox jComboBoxType;
313: private javax.swing.JLabel jLabelError;
314: private javax.swing.JLabel jLabelName;
315: private javax.swing.JLabel jLabelType;
316: private javax.swing.JTextField jTextFieldName;
317: // End of variables declaration//GEN-END:variables
318:
319: }
|