KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > customcommands > NewCommandVisualPanel


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.ant.freeform.customcommands;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ItemEvent JavaDoc;
24 import java.awt.event.ItemListener JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.DefaultComboBoxModel JavaDoc;
27 import javax.swing.DefaultListCellRenderer JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JList JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.ListCellRenderer JavaDoc;
32 import javax.swing.event.DocumentEvent JavaDoc;
33 import javax.swing.event.DocumentListener JavaDoc;
34 import javax.swing.plaf.UIResource JavaDoc;
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 JavaDoc {
46     
47     public NewCommandVisualPanel(final NewCommandWizardPanel master, String JavaDoc[] likelyCommandNames) {
48         initComponents();
49         command.addItemListener(new ItemListener JavaDoc() {
50             public void itemStateChanged(ItemEvent JavaDoc e) {
51                 master.fireChangeEvent();
52             }
53         });
54         displayName.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
55             public void changedUpdate(DocumentEvent JavaDoc e) {
56             }
57             public void insertUpdate(DocumentEvent JavaDoc e) {
58                 master.fireChangeEvent();
59             }
60             public void removeUpdate(DocumentEvent JavaDoc e) {
61                 master.fireChangeEvent();
62             }
63         });
64         command.setModel(new DefaultComboBoxModel JavaDoc(likelyCommandNames));
65         updateMenus();
66     }
67     
68     private void updateMenus() {
69         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc();
70         DataFolder menuBarFolder = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("Menu")); // NOI18N
71
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"); // NOI18N
87
menu.setRenderer(new MenuListCellRenderer());
88         updatePositions();
89     }
90     
91     private void updatePositions() {
92         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc();
93         FileObject menuFolderFO = Repository.getDefault().getDefaultFileSystem().findResource("Menu/" + getMenu()); // NOI18N
94
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 JavaDoc before;
99                 if (i > 0) {
100                     before = findName(kids[i - 1]);
101                 } else {
102                     before = "START"; // XXX I18N
103
}
104                 String JavaDoc after;
105                 if (i < kids.length) {
106                     after = findName(kids[i]);
107                 } else {
108                     after = "END"; // XXX I18N
109
}
110                 model.addElement(before + " \u2194 " + after); // XXX I18N
111
}
112         }
113         position.setModel(model);
114     }
115     
116     private static String JavaDoc findName(DataObject dataObject) {
117         InstanceCookie ic = (InstanceCookie) dataObject.getCookie(InstanceCookie.class);
118         if (ic != null) {
119             Object JavaDoc o;
120             try {
121                 o = ic.instanceCreate();
122             } catch (Exception JavaDoc e) {
123                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
124                 o = null;
125             }
126             if (o instanceof Action JavaDoc) {
127                 return trimAmpersands((String JavaDoc) ((Action JavaDoc) o).getValue(Action.NAME));
128             }
129         }
130         return trimAmpersands(dataObject.getNodeDelegate().getDisplayName());
131     }
132     
133     private static String JavaDoc trimAmpersands(String JavaDoc 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 JavaDoc implements ListCellRenderer JavaDoc, UIResource JavaDoc {
143         
144         public MenuListCellRenderer () {
145             setOpaque(true);
146         }
147         
148         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
149             // #93658: GTK needs name to render cell renderer "natively"
150
setName("ComboBox.listRenderer"); // NOI18N
151

152             String JavaDoc menu = (String JavaDoc) value;
153             DataObject d;
154             try {
155                 d = DataObject.find(Repository.getDefault().getDefaultFileSystem().findResource("Menu/" + menu)); // NOI18N
156
} catch (DataObjectNotFoundException e) {
157                 throw new AssertionError JavaDoc(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         // #93658: GTK needs name to render cell renderer "natively"
175
public String JavaDoc getName() {
176             String JavaDoc name = super.getName();
177             return name == null ? "ComboBox.renderer" : name; // NOI18N
178
}
179         
180     }
181
182     public String JavaDoc getName() {
183         return NbBundle.getMessage(NewCommandVisualPanel.class, "NewCommandVisualPanel.name");
184     }
185     
186     String JavaDoc getCommand() {
187         return ((String JavaDoc) command.getSelectedItem()).trim();
188     }
189     
190     String JavaDoc getDisplayName() {
191         return displayName.getText();
192     }
193     
194     String JavaDoc getMenu() {
195         return ((String JavaDoc) menu.getSelectedItem());
196     }
197     
198     int getPosition() {
199         return position.getSelectedIndex();
200     }
201     
202     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
203
private void initComponents() {
204         commandLabel = new javax.swing.JLabel JavaDoc();
205         command = new javax.swing.JComboBox JavaDoc();
206         displayNameLabel = new javax.swing.JLabel JavaDoc();
207         displayName = new javax.swing.JTextField JavaDoc();
208         displayNameLabelSuffix = new javax.swing.JLabel JavaDoc();
209         menuLabel = new javax.swing.JLabel JavaDoc();
210         menu = new javax.swing.JComboBox JavaDoc();
211         positionLabel = new javax.swing.JLabel JavaDoc();
212         position = new javax.swing.JComboBox JavaDoc();
213         messageLabel = new javax.swing.JLabel JavaDoc();
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 JavaDoc() {
229             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
288

289     private void menuActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_menuActionPerformed
290
updatePositions();
291     }//GEN-LAST:event_menuActionPerformed
292

293     
294     // Variables declaration - do not modify//GEN-BEGIN:variables
295
private javax.swing.JComboBox JavaDoc command;
296     private javax.swing.JLabel JavaDoc commandLabel;
297     private javax.swing.JTextField JavaDoc displayName;
298     private javax.swing.JLabel JavaDoc displayNameLabel;
299     private javax.swing.JLabel JavaDoc displayNameLabelSuffix;
300     private javax.swing.JComboBox JavaDoc menu;
301     private javax.swing.JLabel JavaDoc menuLabel;
302     private javax.swing.JLabel JavaDoc messageLabel;
303     private javax.swing.JComboBox JavaDoc position;
304     private javax.swing.JLabel JavaDoc positionLabel;
305     // End of variables declaration//GEN-END:variables
306

307 }
308
Popular Tags