KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > AddFriendPanel


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.netbeans.modules.apisupport.project.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.awt.Window JavaDoc;
25 import javax.swing.DefaultComboBoxModel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
30 import org.netbeans.modules.apisupport.project.Util;
31 import org.netbeans.modules.apisupport.project.ui.UIUtil;
32 import org.openide.util.NbBundle;
33
34 /**
35  * Panel for choosing a <em>friend</em>.
36  *
37  * @author Martin Krauskopf
38  */

39 public class AddFriendPanel extends JPanel JavaDoc {
40     
41     static final String JavaDoc VALID_PROPERTY = "isPanelValid"; // NOI18N
42

43     boolean valid = false;
44     
45     /** Creates new form AddFriendPanel */
46     public AddFriendPanel(final SingleModuleProperties props) {
47         initComponents();
48         // helps prevents flickering
49
friends.setPrototypeDisplayValue("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"); // NOI18N
50
Component JavaDoc editorComp = friends.getEditor().getEditorComponent();
51         if (editorComp instanceof JTextComponent JavaDoc) {
52             ((JTextComponent JavaDoc) editorComp).getDocument().addDocumentListener(new UIUtil.DocumentAdapter() {
53                 public void insertUpdate(DocumentEvent JavaDoc e) {
54                     checkValidity();
55                 }
56             });
57         }
58         friends.setEnabled(false);
59         friends.setModel(CustomizerComponentFactory.createComboWaitModel());
60         friends.setSelectedItem(CustomizerComponentFactory.WAIT_VALUE);
61         ModuleProperties.RP.post(new Runnable JavaDoc() {
62             public void run() {
63                 final String JavaDoc[] friendCNBs = props.getAvailableFriends();
64                 EventQueue.invokeLater(new Runnable JavaDoc() {
65                     public void run() {
66                         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc();
67                         for (int i = 0; i < friendCNBs.length; i++) {
68                             model.addElement(friendCNBs[i]);
69                         }
70                         friends.setModel(model);
71                         friends.setEnabled(true);
72                         checkValidity();
73                         // data are loaded lets LayoutManager do its work
74
friends.setPrototypeDisplayValue(null);
75                         Window JavaDoc w = SwingUtilities.getWindowAncestor(AddFriendPanel.this);
76                         if (w != null && w.getWidth() < w.getPreferredSize().getWidth()) {
77                             w.pack();
78                         }
79                     }
80                 });
81             }
82         });
83     }
84     
85     private void checkValidity() {
86         String JavaDoc cnb = getFriendCNB();
87         if (cnb.length() == 0) {
88             setErrorMessage(NbBundle.getMessage(AddFriendPanel.class, "MSG_FriendMayNotBeBlank"));
89         } else if (CustomizerComponentFactory.WAIT_VALUE == friends.getSelectedItem()) {
90             setErrorMessage(""); // do not show errormessage during "Please wait..." state
91
} else if (!Util.isValidJavaFQN(cnb)) {
92             setErrorMessage(NbBundle.getMessage(AddFriendPanel.class, "MSG_FriendIsNotValidCNB"));
93         } else {
94             setErrorMessage(null);
95         }
96     }
97     
98     String JavaDoc getFriendCNB() {
99         return friends.getEditor().getItem().toString().trim();
100     }
101     
102     private void setErrorMessage(String JavaDoc errMessage) {
103         this.errorMessage.setText(errMessage == null ? " " : errMessage);
104         boolean valid = errMessage == null;
105         if (this.valid != valid) {
106             this.valid = valid;
107             firePropertyChange(AddFriendPanel.VALID_PROPERTY, !valid, valid);
108         }
109     }
110     
111     /** This method is called from within the constructor to
112      * initialize the form.
113      * WARNING: Do NOT modify this code. The content of this method is
114      * always regenerated by the Form Editor.
115      */

116     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
117
private void initComponents() {
118         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
119
120         friendsTxt = new javax.swing.JLabel JavaDoc();
121         friends = new javax.swing.JComboBox JavaDoc();
122         errorMessage = new javax.swing.JLabel JavaDoc();
123
124         setLayout(new java.awt.GridBagLayout JavaDoc());
125
126         setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
127         friendsTxt.setLabelFor(friends);
128         org.openide.awt.Mnemonics.setLocalizedText(friendsTxt, org.openide.util.NbBundle.getMessage(AddFriendPanel.class, "LBL_FriendModule"));
129         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
130         gridBagConstraints.gridx = 0;
131         gridBagConstraints.gridy = 0;
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
133         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
134         add(friendsTxt, gridBagConstraints);
135
136         friends.setEditable(true);
137         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
138         gridBagConstraints.gridx = 1;
139         gridBagConstraints.gridy = 0;
140         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
141         gridBagConstraints.weightx = 1.0;
142         add(friends, gridBagConstraints);
143
144         errorMessage.setForeground(javax.swing.UIManager.getDefaults().getColor("nb.errorForeground"));
145         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
146         gridBagConstraints.gridx = 0;
147         gridBagConstraints.gridy = 1;
148         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
149         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
150         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 6, 0);
151         add(errorMessage, gridBagConstraints);
152
153     }
154     // </editor-fold>//GEN-END:initComponents
155

156     
157     // Variables declaration - do not modify//GEN-BEGIN:variables
158
public javax.swing.JLabel JavaDoc errorMessage;
159     public javax.swing.JComboBox JavaDoc friends;
160     public javax.swing.JLabel JavaDoc friendsTxt;
161     // End of variables declaration//GEN-END:variables
162

163 }
164
Popular Tags