1 19 20 package org.netbeans.modules.apisupport.project.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.EventQueue ; 24 import java.awt.Window ; 25 import javax.swing.DefaultComboBoxModel ; 26 import javax.swing.JPanel ; 27 import javax.swing.SwingUtilities ; 28 import javax.swing.event.DocumentEvent ; 29 import javax.swing.text.JTextComponent ; 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 39 public class AddFriendPanel extends JPanel { 40 41 static final String VALID_PROPERTY = "isPanelValid"; 43 boolean valid = false; 44 45 46 public AddFriendPanel(final SingleModuleProperties props) { 47 initComponents(); 48 friends.setPrototypeDisplayValue("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"); Component editorComp = friends.getEditor().getEditorComponent(); 51 if (editorComp instanceof JTextComponent ) { 52 ((JTextComponent ) editorComp).getDocument().addDocumentListener(new UIUtil.DocumentAdapter() { 53 public void insertUpdate(DocumentEvent 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 () { 62 public void run() { 63 final String [] friendCNBs = props.getAvailableFriends(); 64 EventQueue.invokeLater(new Runnable () { 65 public void run() { 66 DefaultComboBoxModel model = new DefaultComboBoxModel (); 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 friends.setPrototypeDisplayValue(null); 75 Window 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 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(""); } else if (!Util.isValidJavaFQN(cnb)) { 92 setErrorMessage(NbBundle.getMessage(AddFriendPanel.class, "MSG_FriendIsNotValidCNB")); 93 } else { 94 setErrorMessage(null); 95 } 96 } 97 98 String getFriendCNB() { 99 return friends.getEditor().getItem().toString().trim(); 100 } 101 102 private void setErrorMessage(String 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 116 private void initComponents() { 118 java.awt.GridBagConstraints gridBagConstraints; 119 120 friendsTxt = new javax.swing.JLabel (); 121 friends = new javax.swing.JComboBox (); 122 errorMessage = new javax.swing.JLabel (); 123 124 setLayout(new java.awt.GridBagLayout ()); 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 (); 130 gridBagConstraints.gridx = 0; 131 gridBagConstraints.gridy = 0; 132 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 133 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 134 add(friendsTxt, gridBagConstraints); 135 136 friends.setEditable(true); 137 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (); 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 (6, 0, 6, 0); 151 add(errorMessage, gridBagConstraints); 152 153 } 154 156 157 public javax.swing.JLabel errorMessage; 159 public javax.swing.JComboBox friends; 160 public javax.swing.JLabel friendsTxt; 161 163 } 164 | Popular Tags |