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.apisupport.project.ui.customizer;
043:
044: import java.awt.Component;
045: import java.awt.EventQueue;
046: import java.awt.Window;
047: import javax.swing.DefaultComboBoxModel;
048: import javax.swing.JPanel;
049: import javax.swing.SwingUtilities;
050: import javax.swing.event.DocumentEvent;
051: import javax.swing.text.JTextComponent;
052: import org.netbeans.modules.apisupport.project.Util;
053: import org.netbeans.modules.apisupport.project.ui.UIUtil;
054: import org.openide.util.NbBundle;
055:
056: /**
057: * Panel for choosing a <em>friend</em>.
058: *
059: * @author Martin Krauskopf
060: */
061: public class AddFriendPanel extends JPanel {
062:
063: static final String VALID_PROPERTY = "isPanelValid"; // NOI18N
064:
065: boolean valid = false;
066:
067: /** Creates new form AddFriendPanel */
068: public AddFriendPanel(final SingleModuleProperties props) {
069: initComponents();
070: // helps prevents flickering
071: friends
072: .setPrototypeDisplayValue("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"); // NOI18N
073: Component editorComp = friends.getEditor().getEditorComponent();
074: if (editorComp instanceof JTextComponent) {
075: ((JTextComponent) editorComp).getDocument()
076: .addDocumentListener(new UIUtil.DocumentAdapter() {
077: public void insertUpdate(DocumentEvent e) {
078: checkValidity();
079: }
080: });
081: }
082: friends.setEnabled(false);
083: friends.setModel(CustomizerComponentFactory
084: .createComboWaitModel());
085: friends.setSelectedItem(CustomizerComponentFactory.WAIT_VALUE);
086: ModuleProperties.RP.post(new Runnable() {
087: public void run() {
088: final String[] friendCNBs = props.getAvailableFriends();
089: EventQueue.invokeLater(new Runnable() {
090: public void run() {
091: DefaultComboBoxModel model = new DefaultComboBoxModel();
092: for (int i = 0; i < friendCNBs.length; i++) {
093: model.addElement(friendCNBs[i]);
094: }
095: friends.setModel(model);
096: friends.setEnabled(true);
097: checkValidity();
098: // data are loaded lets LayoutManager do its work
099: friends.setPrototypeDisplayValue(null);
100: Window w = SwingUtilities
101: .getWindowAncestor(AddFriendPanel.this );
102: if (w != null
103: && w.getWidth() < w.getPreferredSize()
104: .getWidth()) {
105: w.pack();
106: }
107: }
108: });
109: }
110: });
111: }
112:
113: private void checkValidity() {
114: String cnb = getFriendCNB();
115: if (cnb.length() == 0) {
116: setErrorMessage(NbBundle.getMessage(AddFriendPanel.class,
117: "MSG_FriendMayNotBeBlank"));
118: } else if (CustomizerComponentFactory.WAIT_VALUE == friends
119: .getSelectedItem()) {
120: setErrorMessage(""); // do not show errormessage during "Please wait..." state
121: } else if (!Util.isValidJavaFQN(cnb)) {
122: setErrorMessage(NbBundle.getMessage(AddFriendPanel.class,
123: "MSG_FriendIsNotValidCNB"));
124: } else {
125: setErrorMessage(null);
126: }
127: }
128:
129: String getFriendCNB() {
130: return friends.getEditor().getItem().toString().trim();
131: }
132:
133: private void setErrorMessage(String errMessage) {
134: this .errorMessage
135: .setText(errMessage == null ? " " : errMessage);
136: boolean valid = errMessage == null;
137: if (this .valid != valid) {
138: this .valid = valid;
139: firePropertyChange(AddFriendPanel.VALID_PROPERTY, !valid,
140: valid);
141: }
142: }
143:
144: /** This method is called from within the constructor to
145: * initialize the form.
146: * WARNING: Do NOT modify this code. The content of this method is
147: * always regenerated by the Form Editor.
148: */
149: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
150: private void initComponents() {
151: java.awt.GridBagConstraints gridBagConstraints;
152:
153: friendsTxt = new javax.swing.JLabel();
154: friends = new javax.swing.JComboBox();
155: errorMessage = new javax.swing.JLabel();
156:
157: setLayout(new java.awt.GridBagLayout());
158:
159: setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
160: 10, 10));
161: friendsTxt.setLabelFor(friends);
162: org.openide.awt.Mnemonics.setLocalizedText(friendsTxt,
163: org.openide.util.NbBundle.getMessage(
164: AddFriendPanel.class, "LBL_FriendModule"));
165: gridBagConstraints = new java.awt.GridBagConstraints();
166: gridBagConstraints.gridx = 0;
167: gridBagConstraints.gridy = 0;
168: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
169: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
170: add(friendsTxt, gridBagConstraints);
171:
172: friends.setEditable(true);
173: gridBagConstraints = new java.awt.GridBagConstraints();
174: gridBagConstraints.gridx = 1;
175: gridBagConstraints.gridy = 0;
176: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
177: gridBagConstraints.weightx = 1.0;
178: add(friends, gridBagConstraints);
179:
180: errorMessage.setForeground(javax.swing.UIManager.getDefaults()
181: .getColor("nb.errorForeground"));
182: gridBagConstraints = new java.awt.GridBagConstraints();
183: gridBagConstraints.gridx = 0;
184: gridBagConstraints.gridy = 1;
185: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
186: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
187: gridBagConstraints.insets = new java.awt.Insets(6, 0, 6, 0);
188: add(errorMessage, gridBagConstraints);
189:
190: }
191:
192: // </editor-fold>//GEN-END:initComponents
193:
194: // Variables declaration - do not modify//GEN-BEGIN:variables
195: public javax.swing.JLabel errorMessage;
196: public javax.swing.JComboBox friends;
197: public javax.swing.JLabel friendsTxt;
198: // End of variables declaration//GEN-END:variables
199:
200: }
|