1 19 20 package org.netbeans.modules.ant.freeform.customcommands; 21 22 import java.awt.Component ; 23 import java.awt.event.ItemEvent ; 24 import java.awt.event.ItemListener ; 25 import javax.swing.Action ; 26 import javax.swing.DefaultComboBoxModel ; 27 import javax.swing.DefaultListCellRenderer ; 28 import javax.swing.JLabel ; 29 import javax.swing.JList ; 30 import javax.swing.JPanel ; 31 import javax.swing.ListCellRenderer ; 32 import javax.swing.event.DocumentEvent ; 33 import javax.swing.event.DocumentListener ; 34 import javax.swing.plaf.UIResource ; 35 import org.openide.ErrorManager; 36 import org.openide.awt.Mnemonics; 37 import org.openide.cookies.InstanceCookie; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.Repository; 40 import org.openide.loaders.DataFolder; 41 import org.openide.loaders.DataObject; 42 import org.openide.loaders.DataObjectNotFoundException; 43 import org.openide.util.NbBundle; 44 45 public final class NewCommandVisualPanel extends JPanel { 46 47 public NewCommandVisualPanel(final NewCommandWizardPanel master, String [] likelyCommandNames) { 48 initComponents(); 49 command.addItemListener(new ItemListener () { 50 public void itemStateChanged(ItemEvent e) { 51 master.fireChangeEvent(); 52 } 53 }); 54 displayName.getDocument().addDocumentListener(new DocumentListener () { 55 public void changedUpdate(DocumentEvent e) { 56 } 57 public void insertUpdate(DocumentEvent e) { 58 master.fireChangeEvent(); 59 } 60 public void removeUpdate(DocumentEvent e) { 61 master.fireChangeEvent(); 62 } 63 }); 64 command.setModel(new DefaultComboBoxModel (likelyCommandNames)); 65 updateMenus(); 66 } 67 68 private void updateMenus() { 69 DefaultComboBoxModel model = new DefaultComboBoxModel (); 70 DataFolder menuBarFolder = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("Menu")); DataObject[] kids = menuBarFolder.getChildren(); 72 for (int i = 0; i < kids.length; i++) { 73 if (!(kids[i] instanceof DataFolder)) { 74 continue; 75 } 76 model.addElement(kids[i].getPrimaryFile().getNameExt()); 77 DataObject[] kids2 = ((DataFolder) kids[i]).getChildren(); 78 for (int j = 0; j < kids2.length; j++) { 79 if (!(kids2[j] instanceof DataFolder)) { 80 continue; 81 } 82 model.addElement(kids[i].getPrimaryFile().getNameExt() + '/' + kids2[j].getPrimaryFile().getNameExt()); 83 } 84 } 85 menu.setModel(model); 86 menu.setSelectedItem("BuildProject"); menu.setRenderer(new MenuListCellRenderer()); 88 updatePositions(); 89 } 90 91 private void updatePositions() { 92 DefaultComboBoxModel model = new DefaultComboBoxModel (); 93 FileObject menuFolderFO = Repository.getDefault().getDefaultFileSystem().findResource("Menu/" + getMenu()); if (menuFolderFO != null) { 95 DataFolder menuFolder = DataFolder.findFolder(menuFolderFO); 96 DataObject[] kids = menuFolder.getChildren(); 97 for (int i = 0; i <= kids.length; i++) { 98 String before; 99 if (i > 0) { 100 before = findName(kids[i - 1]); 101 } else { 102 before = "START"; } 104 String after; 105 if (i < kids.length) { 106 after = findName(kids[i]); 107 } else { 108 after = "END"; } 110 model.addElement(before + " \u2194 " + after); } 112 } 113 position.setModel(model); 114 } 115 116 private static String findName(DataObject dataObject) { 117 InstanceCookie ic = (InstanceCookie) dataObject.getCookie(InstanceCookie.class); 118 if (ic != null) { 119 Object o; 120 try { 121 o = ic.instanceCreate(); 122 } catch (Exception e) { 123 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 124 o = null; 125 } 126 if (o instanceof Action ) { 127 return trimAmpersands((String ) ((Action ) o).getValue(Action.NAME)); 128 } 129 } 130 return trimAmpersands(dataObject.getNodeDelegate().getDisplayName()); 131 } 132 133 private static String trimAmpersands(String text) { 134 int amp = Mnemonics.findMnemonicAmpersand(text); 135 if (amp == -1) { 136 return text; 137 } else { 138 return text.substring(0, amp) + text.substring(amp + 1); 139 } 140 } 141 142 private static final class MenuListCellRenderer extends JLabel implements ListCellRenderer , UIResource { 143 144 public MenuListCellRenderer () { 145 setOpaque(true); 146 } 147 148 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 149 setName("ComboBox.listRenderer"); 152 String menu = (String ) value; 153 DataObject d; 154 try { 155 d = DataObject.find(Repository.getDefault().getDefaultFileSystem().findResource("Menu/" + menu)); } catch (DataObjectNotFoundException e) { 157 throw new AssertionError (e); 158 } 159 160 setText(findName(d)); 161 162 if ( isSelected ) { 163 setBackground(list.getSelectionBackground()); 164 setForeground(list.getSelectionForeground()); 165 } 166 else { 167 setBackground(list.getBackground()); 168 setForeground(list.getForeground()); 169 } 170 171 return this; 172 } 173 174 public String getName() { 176 String name = super.getName(); 177 return name == null ? "ComboBox.renderer" : name; } 179 180 } 181 182 public String getName() { 183 return NbBundle.getMessage(NewCommandVisualPanel.class, "NewCommandVisualPanel.name"); 184 } 185 186 String getCommand() { 187 return ((String ) command.getSelectedItem()).trim(); 188 } 189 190 String getDisplayName() { 191 return displayName.getText(); 192 } 193 194 String getMenu() { 195 return ((String ) menu.getSelectedItem()); 196 } 197 198 int getPosition() { 199 return position.getSelectedIndex(); 200 } 201 202 private void initComponents() { 204 commandLabel = new javax.swing.JLabel (); 205 command = new javax.swing.JComboBox (); 206 displayNameLabel = new javax.swing.JLabel (); 207 displayName = new javax.swing.JTextField (); 208 displayNameLabelSuffix = new javax.swing.JLabel (); 209 menuLabel = new javax.swing.JLabel (); 210 menu = new javax.swing.JComboBox (); 211 positionLabel = new javax.swing.JLabel (); 212 position = new javax.swing.JComboBox (); 213 messageLabel = new javax.swing.JLabel (); 214 215 commandLabel.setLabelFor(command); 216 org.openide.awt.Mnemonics.setLocalizedText(commandLabel, NbBundle.getMessage(NewCommandVisualPanel.class, "commandLabel")); 217 218 command.setEditable(true); 219 220 displayNameLabel.setLabelFor(displayName); 221 org.openide.awt.Mnemonics.setLocalizedText(displayNameLabel, NbBundle.getMessage(NewCommandVisualPanel.class, "displayNameLabel")); 222 223 org.openide.awt.Mnemonics.setLocalizedText(displayNameLabelSuffix, NbBundle.getMessage(NewCommandVisualPanel.class, "displayNameLabelSuffix")); 224 225 menuLabel.setLabelFor(menu); 226 org.openide.awt.Mnemonics.setLocalizedText(menuLabel, NbBundle.getMessage(NewCommandVisualPanel.class, "menuLabel")); 227 228 menu.addActionListener(new java.awt.event.ActionListener () { 229 public void actionPerformed(java.awt.event.ActionEvent evt) { 230 menuActionPerformed(evt); 231 } 232 }); 233 234 positionLabel.setLabelFor(position); 235 org.openide.awt.Mnemonics.setLocalizedText(positionLabel, NbBundle.getMessage(NewCommandVisualPanel.class, "positionLabel")); 236 237 org.openide.awt.Mnemonics.setLocalizedText(messageLabel, NbBundle.getMessage(NewCommandVisualPanel.class, "messageLabel")); 238 239 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 240 this.setLayout(layout); 241 layout.setHorizontalGroup( 242 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 243 .add(layout.createSequentialGroup() 244 .addContainerGap() 245 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 246 .add(layout.createSequentialGroup() 247 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 248 .add(displayNameLabel) 249 .add(commandLabel) 250 .add(menuLabel) 251 .add(positionLabel)) 252 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 253 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 254 .add(position, 0, 230, Short.MAX_VALUE) 255 .add(menu, 0, 230, Short.MAX_VALUE) 256 .add(command, 0, 230, Short.MAX_VALUE) 257 .add(displayName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)) 258 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 259 .add(displayNameLabelSuffix)) 260 .add(messageLabel)) 261 .addContainerGap()) 262 ); 263 layout.setVerticalGroup( 264 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 265 .add(layout.createSequentialGroup() 266 .addContainerGap() 267 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 268 .add(commandLabel) 269 .add(command, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 270 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 271 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 272 .add(displayNameLabel) 273 .add(displayNameLabelSuffix) 274 .add(displayName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 275 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 276 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 277 .add(menuLabel) 278 .add(menu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 279 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 280 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 281 .add(positionLabel) 282 .add(position, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 283 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 284 .add(messageLabel) 285 .addContainerGap(158, Short.MAX_VALUE)) 286 ); 287 } 289 private void menuActionPerformed(java.awt.event.ActionEvent evt) { updatePositions(); 291 } 293 294 private javax.swing.JComboBox command; 296 private javax.swing.JLabel commandLabel; 297 private javax.swing.JTextField displayName; 298 private javax.swing.JLabel displayNameLabel; 299 private javax.swing.JLabel displayNameLabelSuffix; 300 private javax.swing.JComboBox menu; 301 private javax.swing.JLabel menuLabel; 302 private javax.swing.JLabel messageLabel; 303 private javax.swing.JComboBox position; 304 private javax.swing.JLabel positionLabel; 305 307 } 308 | Popular Tags |