KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.AbstractAction JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.ActionMap JavaDoc;
32 import javax.swing.InputMap JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.KeyStroke JavaDoc;
36 import javax.swing.event.DocumentEvent JavaDoc;
37 import javax.swing.event.ListSelectionEvent JavaDoc;
38 import javax.swing.event.ListSelectionListener JavaDoc;
39 import javax.swing.text.BadLocationException JavaDoc;
40 import javax.swing.text.Style JavaDoc;
41 import javax.swing.text.StyleConstants JavaDoc;
42 import javax.swing.text.StyledDocument JavaDoc;
43 import org.netbeans.modules.apisupport.project.Util;
44 import org.netbeans.modules.apisupport.project.ui.UIUtil;
45 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
46 import org.openide.ErrorManager;
47 import org.openide.awt.HtmlBrowser;
48 import org.openide.util.Mutex;
49 import org.openide.util.NbBundle;
50 import org.openide.util.RequestProcessor;
51
52 /**
53  * Represents panel for adding new dependency for a module. Shown after
54  * <em>Add</em> button on the <code>CustomizerLibraries</code> panel has been
55  * pushed.
56  *
57  * @author Martin Krauskopf, Jesse Glick
58  */

59 public final class AddModulePanel extends JPanel JavaDoc {
60     
61     private static final String JavaDoc FILTER_DESCRIPTION = getMessage("LBL_FilterDescription");
62     
63     private CustomizerComponentFactory.DependencyListModel universeModules;
64     private RequestProcessor.Task filterTask;
65     private AddModuleFilter filterer;
66     private URL JavaDoc currectJavadoc;
67     
68     private final SingleModuleProperties props;
69     
70     public AddModulePanel(final SingleModuleProperties props) {
71         this.props = props;
72         initComponents();
73         initAccessibility();
74         filterValue.setText(FILTER_DESCRIPTION);
75         fillUpUniverseModules();
76         moduleList.setCellRenderer(CustomizerComponentFactory.getDependencyCellRenderer(false));
77         moduleList.addListSelectionListener(new ListSelectionListener JavaDoc() {
78             public void valueChanged(ListSelectionEvent JavaDoc e) {
79                 showDescription();
80                 currectJavadoc = null;
81                 ModuleDependency[] deps = getSelectedDependencies();
82                 if (deps.length == 1) {
83                     NbPlatform platform = props.getActivePlatform();
84                     if (platform == null) { // NetBeans.org module
85
currectJavadoc = Util.findJavadocForNetBeansOrgModules(deps[0]);
86                     } else {
87                         currectJavadoc = Util.findJavadoc(deps[0], platform);
88                     }
89                 }
90                 showJavadocButton.setEnabled(currectJavadoc != null);
91             }
92         });
93         filterValue.getDocument().addDocumentListener(new UIUtil.DocumentAdapter() {
94             public void insertUpdate(DocumentEvent JavaDoc e) {
95                 if (!FILTER_DESCRIPTION.equals(filterValue.getText())) {
96                     search();
97                 }
98             }
99         });
100         // Make basic navigation commands from the list work from the text field.
101
String JavaDoc[][] listNavCommands = {
102             { "selectPreviousRow", "selectPreviousRow" }, // NOI18N
103
{ "selectNextRow", "selectNextRow" }, // NOI18N
104
{ "selectFirstRow", "selectFirstRow" }, // NOI18N
105
{ "selectLastRow", "selectLastRow" }, // NOI18N
106
{ "scrollUp", "scrollUp" }, // NOI18N
107
{ "scrollDown", "scrollDown" }, // NOI18N
108
};
109         String JavaDoc[][] areaNavCommands = {
110             { "selection-page-up", "page-up" },// NOI18N
111
{ "selection-page-down", "page-down" },// NOI18N
112
{ "selection-up", "caret-up" },// NOI18N
113
{ "selection-down", "caret-down" },// NOI18N
114
};
115         exchangeCommands(listNavCommands, moduleList, filterValue);
116         exchangeCommands(areaNavCommands, descValue, filterValue);
117     }
118     
119     private static void exchangeCommands(String JavaDoc[][] commandsToExchange,
120             final JComponent JavaDoc target, final JComponent JavaDoc source) {
121         InputMap JavaDoc targetBindings = target.getInputMap();
122         KeyStroke JavaDoc[] targetBindingKeys = targetBindings.allKeys();
123         ActionMap JavaDoc targetActions = target.getActionMap();
124         InputMap JavaDoc sourceBindings = source.getInputMap();
125         ActionMap JavaDoc sourceActions = source.getActionMap();
126         for (int i = 0; i < commandsToExchange.length; i++) {
127             String JavaDoc commandFrom = commandsToExchange[i][0];
128             String JavaDoc commandTo = commandsToExchange[i][1];
129             final Action JavaDoc orig = targetActions.get(commandTo);
130             if (orig == null) {
131                 continue;
132             }
133             sourceActions.put(commandTo, new AbstractAction JavaDoc() {
134                 public void actionPerformed(ActionEvent JavaDoc e) {
135                     orig.actionPerformed(new ActionEvent JavaDoc(target, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers()));
136                 }
137             });
138             for (int j = 0; j < targetBindingKeys.length; j++) {
139                 if (targetBindings.get(targetBindingKeys[j]).equals(commandFrom)) {
140                     sourceBindings.put(targetBindingKeys[j], commandTo);
141                 }
142             }
143         }
144     }
145     
146     private void fillUpUniverseModules() {
147         filterValue.setEnabled(false);
148         moduleList.setEnabled(false);
149         showNonAPIModules.setEnabled(false);
150         final String JavaDoc lastFilter = filterValue.getText();
151         filterValue.setText(CustomizerComponentFactory.WAIT_VALUE);
152         moduleList.setModel(CustomizerComponentFactory.createListWaitModel());
153         final boolean nonApiDeps = showNonAPIModules.isSelected();
154         ModuleProperties.RP.post(new Runnable JavaDoc() {
155             public void run() {
156                 final Set JavaDoc universeDeps = props.getUniverseDependencies(true, !nonApiDeps);
157                 EventQueue.invokeLater(new Runnable JavaDoc() {
158                     public void run() {
159                         universeModules = CustomizerComponentFactory.createSortedDependencyListModel(universeDeps);
160                         filterer = null;
161                         moduleList.setModel(universeModules);
162                         moduleList.setEnabled(true);
163                         filterValue.setEnabled(true);
164                         showNonAPIModules.setEnabled(true);
165                         filterValue.setText(lastFilter);
166                         if (!FILTER_DESCRIPTION.equals(lastFilter)) {
167                             search();
168                         } else {
169                             filterValue.selectAll();
170                         }
171                         filterValue.requestFocusInWindow();
172                     }
173                 });
174             }
175         });
176     }
177     
178     private void showDescription() {
179         StyledDocument JavaDoc doc = descValue.getStyledDocument();
180         try {
181             doc.remove(0, doc.getLength());
182             ModuleDependency[] deps = getSelectedDependencies();
183             if (deps.length != 1) {
184                 return;
185             }
186             String JavaDoc longDesc = deps[0].getModuleEntry().getLongDescription();
187             if (longDesc != null) {
188                 doc.insertString(0, longDesc, null);
189             }
190             String JavaDoc filterText = filterValue.getText().trim();
191             if (filterText.length() != 0 && !FILTER_DESCRIPTION.equals(filterText)) {
192                 doc.insertString(doc.getLength(), "\n\n", null); // NOI18N
193
Style JavaDoc bold = doc.addStyle(null, null);
194                 bold.addAttribute(StyleConstants.Bold, Boolean.TRUE);
195                 doc.insertString(doc.getLength(), getMessage("TEXT_matching_filter_contents"), bold);
196                 doc.insertString(doc.getLength(), "\n", null); // NOI18N
197
if (filterText.length() > 0) {
198                     String JavaDoc filterTextLC = filterText.toLowerCase(Locale.US);
199                     Style JavaDoc match = doc.addStyle(null, null);
200                     match.addAttribute(StyleConstants.Background, new Color JavaDoc(246, 248, 139));
201                     Set JavaDoc<String JavaDoc> matches = filterer.getMatchesFor(filterText, deps[0]);
202                     Iterator JavaDoc it = matches.iterator();
203                     boolean isEven = false;
204                     Style JavaDoc even = doc.addStyle(null, null);
205                     even.addAttribute(StyleConstants.Background, new Color JavaDoc(235, 235, 235));
206                     while (it.hasNext()) {
207                         String JavaDoc hit = (String JavaDoc) it.next();
208                         int loc = doc.getLength();
209                         doc.insertString(loc, hit, (isEven ? even : null));
210                         int start = hit.toLowerCase(Locale.US).indexOf(filterTextLC);
211                         while (start != -1) {
212                             doc.setCharacterAttributes(loc + start, filterTextLC.length(), match, true);
213                             start = hit.toLowerCase(Locale.US).indexOf(filterTextLC, start + 1);
214                         }
215                         doc.insertString(doc.getLength(), "\n", (isEven ? even : null)); // NOI18N
216
isEven ^= true;
217                     }
218                 } else {
219                     Style JavaDoc italics = doc.addStyle(null, null);
220                     italics.addAttribute(StyleConstants.Italic, Boolean.TRUE);
221                     doc.insertString(doc.getLength(), getMessage("TEXT_no_filter_specified"), italics);
222                 }
223             }
224             descValue.setCaretPosition(0);
225         } catch (BadLocationException JavaDoc e) {
226             Util.err.notify(ErrorManager.INFORMATIONAL, e);
227         }
228     }
229     
230     public ModuleDependency[] getSelectedDependencies() {
231         ModuleDependency[] deps;
232         if (CustomizerComponentFactory.isWaitModel(moduleList.getModel())) {
233             deps = new ModuleDependency[0];
234         } else {
235             Object JavaDoc[] objects = moduleList.getSelectedValues();
236             deps = new ModuleDependency[objects.length];
237             System.arraycopy(objects, 0, deps, 0, objects.length);
238         }
239         return deps;
240     }
241     
242     private void search() {
243         if (filterTask != null) {
244             filterTask.cancel();
245             filterTask = null;
246         }
247         final String JavaDoc text = filterValue.getText();
248         if (text.length() == 0) {
249             moduleList.setModel(universeModules);
250             moduleList.setSelectedIndex(0);
251             moduleList.ensureIndexIsVisible(0);
252         } else {
253             final Runnable JavaDoc compute = new Runnable JavaDoc() {
254                 public void run() {
255                     final Set JavaDoc<ModuleDependency> matches = filterer.getMatches(text);
256                     filterTask = null;
257                     Mutex.EVENT.readAccess(new Runnable JavaDoc() {
258                         public void run() {
259                             // XXX would be better to have more fine-grained control over the thread
260
if (!text.equals(filterValue.getText())) {
261                                 return; // no longer valid, don't apply
262
}
263                             moduleList.setModel(CustomizerComponentFactory.createDependencyListModel(matches));
264                             int index = matches.isEmpty() ? -1 : 0;
265                             moduleList.setSelectedIndex(index);
266                             moduleList.ensureIndexIsVisible(index);
267                         }
268                     });
269                 }
270             };
271             if (filterer == null) {
272                 // Slow to create it, so show Please wait...
273
moduleList.setModel(CustomizerComponentFactory.createListWaitModel());
274                 filterTask = RequestProcessor.getDefault().post(new Runnable JavaDoc() {
275                     public void run() {
276                         if (filterer == null) {
277                             filterer = new AddModuleFilter(universeModules.getDependencies(), props.getCodeNameBase());
278                             compute.run();
279                         }
280                     }
281                 });
282             } else {
283                 // Pretty fast once we have it, so do right now and avoid flickering.
284
compute.run();
285             }
286         }
287     }
288     
289     private void initAccessibility() {
290         this.getAccessibleContext().setAccessibleDescription(getMessage("ACS_AddModuleDependency"));
291         filterValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_LBL_Filter"));
292         moduleList.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_ModuleList"));
293         moduleSP.getVerticalScrollBar().getAccessibleContext().setAccessibleName(getMessage("ACS_CTL_ModuleListVerticalScroll"));
294         moduleSP.getVerticalScrollBar().getAccessibleContext().setAccessibleDescription(getMessage("ACSD_CTL_ModuleListVerticalScroll"));
295         moduleSP.getHorizontalScrollBar().getAccessibleContext().setAccessibleName(getMessage("ACS_CTL_ModuleListHorizontalScroll"));
296         moduleSP.getHorizontalScrollBar().getAccessibleContext().setAccessibleDescription(getMessage("ACSD_CTL_ModuleListHorizontalScroll"));
297         showNonAPIModules.getAccessibleContext().setAccessibleDescription(getMessage("ACSD_ShowNonApiModules"));
298     }
299     
300     private static String JavaDoc getMessage(String JavaDoc key) {
301         return NbBundle.getMessage(AddModulePanel.class, key);
302     }
303     
304     /** This method is called from within the constructor to
305      * initialize the form.
306      * WARNING: Do NOT modify this code. The content of this method is
307      * always regenerated by the Form Editor.
308      */

309     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
310
private void initComponents() {
311         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
312
313         moduleLabel = new javax.swing.JLabel JavaDoc();
314         moduleSP = new javax.swing.JScrollPane JavaDoc();
315         moduleList = new javax.swing.JList JavaDoc();
316         descLabel = new javax.swing.JLabel JavaDoc();
317         filter = new javax.swing.JLabel JavaDoc();
318         filterValue = new javax.swing.JTextField JavaDoc();
319         descValueSP = new javax.swing.JScrollPane JavaDoc();
320         descValue = new javax.swing.JTextPane JavaDoc();
321         showJavadocButton = new javax.swing.JButton JavaDoc();
322         showNonAPIModules = new javax.swing.JCheckBox JavaDoc();
323
324         setLayout(new java.awt.GridBagLayout JavaDoc());
325
326         setBorder(javax.swing.BorderFactory.createEmptyBorder(6, 6, 6, 6));
327         setPreferredSize(new java.awt.Dimension JavaDoc(400, 400));
328         moduleLabel.setLabelFor(moduleList);
329         org.openide.awt.Mnemonics.setLocalizedText(moduleLabel, org.openide.util.NbBundle.getMessage(AddModulePanel.class, "LBL_Module"));
330         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
331         gridBagConstraints.gridx = 0;
332         gridBagConstraints.gridy = 2;
333         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
334         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
335         gridBagConstraints.insets = new java.awt.Insets JavaDoc(24, 0, 0, 0);
336         add(moduleLabel, gridBagConstraints);
337
338         moduleSP.setViewportView(moduleList);
339
340         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
341         gridBagConstraints.gridx = 0;
342         gridBagConstraints.gridy = 3;
343         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
344         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
345         gridBagConstraints.weighty = 1.0;
346         add(moduleSP, gridBagConstraints);
347
348         descLabel.setLabelFor(descValue);
349         org.openide.awt.Mnemonics.setLocalizedText(descLabel, org.openide.util.NbBundle.getMessage(AddModulePanel.class, "LBL_Description"));
350         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
351         gridBagConstraints.gridx = 0;
352         gridBagConstraints.gridy = 4;
353         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
354         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
355         gridBagConstraints.insets = new java.awt.Insets JavaDoc(24, 0, 0, 0);
356         add(descLabel, gridBagConstraints);
357
358         filter.setLabelFor(filterValue);
359         org.openide.awt.Mnemonics.setLocalizedText(filter, org.openide.util.NbBundle.getMessage(AddModulePanel.class, "LBL_Filter"));
360         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
361         gridBagConstraints.gridx = 0;
362         gridBagConstraints.gridy = 0;
363         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
364         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
365         add(filter, gridBagConstraints);
366
367         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
368         gridBagConstraints.gridx = 1;
369         gridBagConstraints.gridy = 0;
370         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
371         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
372         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
373         gridBagConstraints.weightx = 1.0;
374         add(filterValue, gridBagConstraints);
375
376         descValue.setEditable(false);
377         descValue.setPreferredSize(new java.awt.Dimension JavaDoc(6, 100));
378         descValueSP.setViewportView(descValue);
379
380         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
381         gridBagConstraints.gridx = 0;
382         gridBagConstraints.gridy = 5;
383         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
384         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
385         gridBagConstraints.weighty = 1.0;
386         add(descValueSP, gridBagConstraints);
387
388         org.openide.awt.Mnemonics.setLocalizedText(showJavadocButton, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("CTL_ShowJavadoc"));
389         showJavadocButton.setEnabled(false);
390         showJavadocButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
391             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
392                 showJavadoc(evt);
393             }
394         });
395
396         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
397         gridBagConstraints.gridx = 0;
398         gridBagConstraints.gridy = 6;
399         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
400         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
401         gridBagConstraints.insets = new java.awt.Insets JavaDoc(24, 0, 0, 0);
402         add(showJavadocButton, gridBagConstraints);
403
404         org.openide.awt.Mnemonics.setLocalizedText(showNonAPIModules, org.openide.util.NbBundle.getMessage(AddModulePanel.class, "CTL_ShowNonAPIModules"));
405         showNonAPIModules.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
406         showNonAPIModules.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
407         showNonAPIModules.addActionListener(new java.awt.event.ActionListener JavaDoc() {
408             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
409                 showNonAPIModulesActionPerformed(evt);
410             }
411         });
412
413         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
414         gridBagConstraints.gridx = 0;
415         gridBagConstraints.gridy = 1;
416         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
417         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
418         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
419         add(showNonAPIModules, gridBagConstraints);
420
421     }// </editor-fold>//GEN-END:initComponents
422

423     private void showNonAPIModulesActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_showNonAPIModulesActionPerformed
424
fillUpUniverseModules();
425     }//GEN-LAST:event_showNonAPIModulesActionPerformed
426

427     private void showJavadoc(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_showJavadoc
428
HtmlBrowser.URLDisplayer.getDefault().showURL(currectJavadoc);
429     }//GEN-LAST:event_showJavadoc
430

431     
432     // Variables declaration - do not modify//GEN-BEGIN:variables
433
private javax.swing.JLabel JavaDoc descLabel;
434     private javax.swing.JTextPane JavaDoc descValue;
435     private javax.swing.JScrollPane JavaDoc descValueSP;
436     private javax.swing.JLabel JavaDoc filter;
437     javax.swing.JTextField JavaDoc filterValue;
438     private javax.swing.JLabel JavaDoc moduleLabel;
439     javax.swing.JList JavaDoc moduleList;
440     private javax.swing.JScrollPane JavaDoc moduleSP;
441     private javax.swing.JButton JavaDoc showJavadocButton;
442     private javax.swing.JCheckBox JavaDoc showNonAPIModules;
443     // End of variables declaration//GEN-END:variables
444

445 }
446
Popular Tags