KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > catalog > ModuleSelectionPanel


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.autoupdate.catalog;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Cursor JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyVetoException JavaDoc;
31 import java.io.CharConversionException JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import javax.swing.ActionMap JavaDoc;
35 import javax.swing.JScrollPane JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import javax.swing.UIManager JavaDoc;
38 import javax.swing.border.Border JavaDoc;
39 import javax.swing.event.ChangeEvent JavaDoc;
40 import javax.swing.event.ChangeListener JavaDoc;
41 import javax.swing.text.html.HTMLEditorKit JavaDoc;
42 import javax.swing.text.html.StyleSheet JavaDoc;
43 import org.netbeans.api.progress.ProgressHandle;
44 import org.netbeans.api.progress.ProgressHandleFactory;
45 import org.openide.DialogDescriptor;
46 import org.openide.DialogDisplayer;
47 import org.openide.awt.StatusDisplayer;
48 import org.openide.explorer.ExplorerManager;
49 import org.openide.explorer.ExplorerUtils;
50 import org.openide.explorer.view.TreeTableView;
51 import org.openide.nodes.Node;
52 import org.openide.nodes.PropertySupport;
53 import org.openide.util.AsyncGUIJob;
54 import org.openide.util.NbBundle;
55 import org.openide.util.Utilities;
56 import org.openide.windows.TopComponent;
57 import org.openide.xml.XMLUtil;
58
59 /** Module selection panel allows user to enable/disable modules in Module Catalog
60  *
61  * @author cledantec, jrojcek, Jesse Glick, Jiri Rechtacek
62  */

63 public class ModuleSelectionPanel extends javax.swing.JPanel JavaDoc
64                                   implements PropertyChangeListener JavaDoc {
65
66     private TreeTableView treeTableView;
67     private static final Logger JavaDoc err = Logger.getLogger("org.netbeans.modules.autoupdate.catalog.ModuleSelectionPanel"); // NOI18N
68

69     /** default size values */
70     private static final int DEF_TREE_WIDTH = 420;
71     private static final int DEF_0_COL_WIDTH = 60;
72     private static final int DEF_1_COL_WIDTH = 150;
73     private static final int DEF_HEIGHT = 350;
74     
75     private static ModuleDeleter deleter;
76     
77     private Cursor JavaDoc cursor = null;
78     static private ModuleSelectionPanel panel = null;
79     
80     static public ModuleSelectionPanel getGUI (boolean create) {
81         assert create || panel != null;
82         if (panel == null && create) {
83             panel = new ModuleSelectionPanel ();
84         }
85         return panel;
86     }
87
88     private ModuleSelectionPanel () {
89         initComponents();
90         treeTableView = new TreeTableView ();
91         treeTableView.setRootVisible(false);
92         ExplorerPanel explorerPanel = new ExplorerPanel ();
93     explorerPanel.getAccessibleContext ().setAccessibleName (
94         NbBundle.getBundle (ModuleSelectionPanel.class).getString ("ACN_ModuleSelectionPanel_ExplorerPanel")); // NOI18N
95
explorerPanel.getAccessibleContext ().setAccessibleDescription (
96         NbBundle.getBundle (ModuleSelectionPanel.class).getString ("ACD_ModuleSelectionPanel_ExplorerPanel")); // NOI18N
97
explorerPanel.setLayout (new BorderLayout JavaDoc ());
98         explorerPanel.add (treeTableView, BorderLayout.CENTER);
99         modulesPane.add (explorerPanel, BorderLayout.CENTER);
100         manager = explorerPanel.getExplorerManager();
101
102         //Fix for NPE on WinXP L&F - either may be null - Tim
103
Font JavaDoc f = UIManager.getFont("controlFont"); // NOI18N
104
Integer JavaDoc i = (Integer JavaDoc) UIManager.get("nbDefaultFontSize"); // NOI18N
105
if (i == null) {
106             i = new Integer JavaDoc(11); //fudge the default if not present
107
}
108         if (f == null) {
109             f = getFont();
110         }
111
112         Node.Property [] properties = new Node.Property [] {
113                 new PropertySupport.ReadWrite (
114                     "enabled", // NOI18N
115
Boolean.TYPE,
116                     org.openide.util.NbBundle.getMessage (ModuleNode.class, "PROP_modules_enabled"),
117                     org.openide.util.NbBundle.getMessage (ModuleNode.class, "HINT_modules_enabled")
118                 ) {
119                     public Object JavaDoc getValue () {
120                         return null;
121                     }
122
123                     public void setValue (Object JavaDoc o) {
124                     }
125                 },
126                 new PropertySupport.ReadOnly (
127                     "specificationVersion", // NOI18N
128
String JavaDoc.class,
129                     org.openide.util.NbBundle.getMessage (ModuleNode.class, "PROP_modules_specversion"),
130                     org.openide.util.NbBundle.getMessage (ModuleNode.class, "HINT_modules_specversion")
131                 ) {
132                     public Object JavaDoc getValue () {
133                         return null;
134                     }
135                 }
136             };
137
138         treeTableView.setProperties (properties);
139         
140         // perform additional preferred size computations for larger fonts
141
if (f.getSize() > i.intValue()) { // NOI18N
142
sizeTTVCarefully();
143         } else {
144             // direct sizing for default situation
145
treeTableView.setPreferredSize(
146                 new Dimension JavaDoc (DEF_1_COL_WIDTH + DEF_0_COL_WIDTH + DEF_TREE_WIDTH,
147                                DEF_HEIGHT)
148             );
149             treeTableView.setTreePreferredWidth(DEF_TREE_WIDTH);
150             treeTableView.setTableColumnPreferredWidth(0, DEF_0_COL_WIDTH);
151             treeTableView.setTableColumnPreferredWidth(1, DEF_1_COL_WIDTH);
152         }
153         treeTableView.setPopupAllowed(true);
154         treeTableView.setDefaultActionAllowed(false);
155
156         // install proper border
157
treeTableView.setBorder((Border JavaDoc)UIManager.get("Nb.ScrollPane.border")); // NOI18N
158
treeTableView.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
159
160         initAccessibility();
161         
162         Utilities.attachInitJob (this, new WarmupJob ());
163         setWaitingState (true, false);
164     }
165     
166     private boolean isWaiting = false;
167     private Dialog JavaDoc d = null;
168     
169     public void setWaitingState (boolean wait, final boolean showProgress) {
170         if (isWaiting == wait) {
171             return ;
172         }
173         isWaiting = wait;
174         SwingUtilities.invokeLater (new Runnable JavaDoc () {
175             public void run () {
176                 doSetWaitingState (isWaiting, showProgress);
177             }
178         });
179     }
180     
181     private ChangeListener JavaDoc statusTextListener = null;
182     
183     private void doSetWaitingState (boolean wait, boolean showProgress) {
184         assert SwingUtilities.isEventDispatchThread ();
185         if (! this.isVisible ()) return ;
186         err.log(Level.FINE,
187                 "Set waiting state on ModuleSelectionPanel to (wait:" + wait +
188                 ", showProgress: " + showProgress + ")");
189         uninstallButton.setEnabled (! wait);
190         if (wait) {
191             if (cursor == null) cursor = getCursor();
192             setCursor (Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR));
193             if (showProgress) {
194                 if (d == null) {
195                     final ProgressHandle handle = ProgressHandleFactory.createHandle ("modules-update");
196                     final ModuleUpdaterProgress progressPanel = new ModuleUpdaterProgress (handle);
197                     DialogDescriptor dd = new DialogDescriptor (progressPanel,
198                                             NbBundle.getMessage (ModuleUpdaterProgress.class, "CTL_ModuleUpdaterProgress_Title"), // NOI18N
199
true, // modal
200
new Object JavaDoc [0],
201                                             null,
202                                             DialogDescriptor.DEFAULT_ALIGN,
203                                             null,
204                                             null,
205                                             true);
206                     statusTextListener = new ChangeListener JavaDoc () {
207                         public void stateChanged (ChangeEvent JavaDoc e) {
208                             handle.progress (StatusDisplayer.getDefault ().getStatusText ());
209                         }
210                     };
211                     StatusDisplayer.getDefault ().addChangeListener (statusTextListener);
212                     d = DialogDisplayer.getDefault ().createDialog (dd);
213                     handle.start ();
214                     d.setVisible (true);
215                 }
216             }
217         } else {
218             if (cursor != null) {
219                 setCursor (cursor);
220                 cursor = null;
221             } else {
222                 setCursor (Cursor.getDefaultCursor ());
223             }
224             if (d != null && d.isVisible ()) {
225                 d.setVisible (false);
226                 d.dispose ();
227                 d = null;
228                 this.requestFocus ();
229             }
230             if (statusTextListener != null) {
231                 StatusDisplayer.getDefault ().removeChangeListener (statusTextListener);
232             }
233         }
234     }
235     
236     public boolean isWaitingState () {
237         return isWaiting;
238     }
239     
240     private void prepareComponents () {
241         this.uninstallButton.setEnabled (false);
242         HTMLEditorKit JavaDoc htmlkit = new HTMLEditorKit JavaDoc ();
243         // override the Swing default CSS to make the HTMLEditorKit use the
244
// same font as the rest of the UI.
245

246         // XXX the style sheet is shared by all HTMLEditorKits. We must
247
// detect if it has been tweaked by ourselves or someone else
248
// (code completion javadoc popup for example) and avoid doing the
249
// same thing again
250

251         StyleSheet JavaDoc css = htmlkit.getStyleSheet ();
252
253         if (css.getStyleSheets() == null) {
254             StyleSheet JavaDoc css2 = new StyleSheet JavaDoc();
255             Font JavaDoc f = treeTableView.getFont ();
256             int size = treeTableView.getFont ().getSize ();
257             css2.addRule(new StringBuffer JavaDoc("body { font-size: ").append(size) // NOI18N
258
.append("; font-family: ").append(f.getName()).append("; }").toString()); // NOI18N
259
css2.addStyleSheet(css);
260             htmlkit.setStyleSheet(css2);
261         }
262
263         this.description.setEditorKit (htmlkit);
264         
265         // #66654: use a full set of properties for modules at top level
266
ModuleNode node = new ModuleNode();
267         manager.setRootContext(node);
268
269         manager.addPropertyChangeListener (this);
270
271         Node[] kids = node.getChildren().getNodes(true);
272         if (kids.length > 0) {
273             try {
274                 manager.setSelectedNodes(new Node[] {kids[0]});
275             } catch (PropertyVetoException JavaDoc ex) {
276                 assert false : ex;
277             }
278         }
279     }
280     
281     /** Computes and sets right preferred sizes of TTV columns.
282      * Sizes of columns are computed as maximum of default values and
283      * header text length. Size of tree is aproximate, grows linearly with
284      * font size.
285      */

286     private void sizeTTVCarefully () {
287         Font JavaDoc headerFont = (Font JavaDoc)UIManager.getDefaults().get("TableHeader.font"); // NOI18N
288
Font JavaDoc tableFont = (Font JavaDoc)UIManager.getDefaults().get("Table.font"); // NOI18N
289
FontMetrics JavaDoc headerFm = getFontMetrics(headerFont);
290         
291         int enabledColWidth = Math.max(DEF_0_COL_WIDTH, headerFm.stringWidth(
292             NbBundle.getMessage (ModuleNode.class, "PROP_modules_enabled")) + 20
293         );
294         int specColWidth = Math.max(DEF_1_COL_WIDTH, headerFm.stringWidth(
295             NbBundle.getMessage (ModuleNode.class, "PROP_modules_specversion")) + 20
296         );
297         int defFontSize = UIManager.getDefaults().getInt("nbDefaultFontSize");
298         int treeWidth = DEF_TREE_WIDTH + 10 * (tableFont.getSize() - defFontSize);
299         
300         treeTableView.setPreferredSize(
301             new Dimension JavaDoc (treeWidth + enabledColWidth + specColWidth,
302                            DEF_HEIGHT + 10 * (tableFont.getSize() - defFontSize))
303         );
304         treeTableView.setTreePreferredWidth(treeWidth);
305         treeTableView.setTableColumnPreferredWidth(0, enabledColWidth);
306         treeTableView.setTableColumnPreferredWidth(1, specColWidth);
307     }
308
309     /** This method is called from within the constructor to
310      * initialize the form.
311      * WARNING: Do NOT modify this code. The content of this method is
312      * always regenerated by the Form Editor.
313      */

314     private void initComponents() {//GEN-BEGIN:initComponents
315
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
316
317         modulesLabel = new javax.swing.JLabel JavaDoc();
318         uninstallButton = new javax.swing.JButton JavaDoc();
319         descriptionLabel = new javax.swing.JLabel JavaDoc();
320         descriptionPane = new javax.swing.JScrollPane JavaDoc();
321         description = new javax.swing.JEditorPane JavaDoc();
322         modulesPane = new javax.swing.JPanel JavaDoc();
323
324         setLayout(new java.awt.GridBagLayout JavaDoc());
325
326         setName(NbBundle.getMessage(ModuleSelectionPanel.class, "LBL_ModuleSelectionPanel_Name"));
327         modulesLabel.setLabelFor(modulesPane);
328         org.openide.awt.Mnemonics.setLocalizedText(modulesLabel, org.openide.util.NbBundle.getBundle(ModuleSelectionPanel.class).getString("ModuleSelectionPanel_ModuleLabel_text"));
329         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
330         gridBagConstraints.gridx = 0;
331         gridBagConstraints.gridy = 0;
332         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
333         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
334         gridBagConstraints.insets = new java.awt.Insets JavaDoc(7, 11, 0, 0);
335         add(modulesLabel, gridBagConstraints);
336
337         org.openide.awt.Mnemonics.setLocalizedText(uninstallButton, org.openide.util.NbBundle.getMessage(ModuleSelectionPanel.class, "BTN_ModuleSelectionPanel_Uninstall"));
338         uninstallButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
339             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
340                 uninstallButtonActionPerformed(evt);
341             }
342         });
343
344         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
345         gridBagConstraints.gridx = 1;
346         gridBagConstraints.gridy = 1;
347         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
348         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
349         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
350         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
351         gridBagConstraints.weightx = 0.15;
352         gridBagConstraints.weighty = 1.0;
353         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 8, 0, 12);
354         add(uninstallButton, gridBagConstraints);
355
356         descriptionLabel.setLabelFor(descriptionPane);
357         org.openide.awt.Mnemonics.setLocalizedText(descriptionLabel, org.openide.util.NbBundle.getMessage(ModuleSelectionPanel.class, "LBL_ModuleSelectionPanel_descriptionLabel"));
358         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
359         gridBagConstraints.gridx = 0;
360         gridBagConstraints.gridy = 2;
361         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
362         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
363         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 11, 0, 0);
364         add(descriptionLabel, gridBagConstraints);
365
366         descriptionPane.setMinimumSize(new java.awt.Dimension JavaDoc(400, 70));
367         descriptionPane.setPreferredSize(new java.awt.Dimension JavaDoc(400, 70));
368         description.setEditable(false);
369         descriptionPane.setViewportView(description);
370
371         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
372         gridBagConstraints.gridx = 0;
373         gridBagConstraints.gridy = 3;
374         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
375         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
376         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
377         gridBagConstraints.weightx = 0.85;
378         gridBagConstraints.weighty = 0.2;
379         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 11, 0, 0);
380         add(descriptionPane, gridBagConstraints);
381
382         modulesPane.setLayout(new java.awt.BorderLayout JavaDoc());
383
384         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
385         gridBagConstraints.gridx = 0;
386         gridBagConstraints.gridy = 1;
387         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
388         gridBagConstraints.weightx = 0.85;
389         gridBagConstraints.weighty = 0.8;
390         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 11, 0, 0);
391         add(modulesPane, gridBagConstraints);
392
393     }//GEN-END:initComponents
394

395     private void uninstallButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_uninstallButtonActionPerformed
396
ModuleNodeUtils.doUninstall (manager.getSelectedNodes ());
397     }//GEN-LAST:event_uninstallButtonActionPerformed
398

399     private void prevButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_prevButtonActionPerformed
400
// Add your handling code here:
401
}//GEN-LAST:event_prevButtonActionPerformed
402

403     // Variables declaration - do not modify//GEN-BEGIN:variables
404
private javax.swing.JEditorPane JavaDoc description;
405     private javax.swing.JLabel JavaDoc descriptionLabel;
406     private javax.swing.JScrollPane JavaDoc descriptionPane;
407     private javax.swing.JLabel JavaDoc modulesLabel;
408     private javax.swing.JPanel JavaDoc modulesPane;
409     private javax.swing.JButton JavaDoc uninstallButton;
410     // End of variables declaration//GEN-END:variables
411

412     private ExplorerManager manager;
413     
414     private static class ExplorerPanel extends TopComponent implements ExplorerManager.Provider {
415         private ExplorerManager mgr;
416         public ExplorerPanel () {
417             this.mgr = new ExplorerManager ();
418             ActionMap JavaDoc map = this.getActionMap ();
419             map.put ("delete", ExplorerUtils.actionDelete(mgr, false));
420             associateLookup (ExplorerUtils.createLookup (mgr, map));
421         }
422         
423         public ExplorerManager getExplorerManager () {
424             return mgr;
425         }
426         
427         protected void componentActivated() {
428             ExplorerUtils.activateActions (mgr, true);
429         }
430         
431         protected void componentDeactivated() {
432             ExplorerUtils.activateActions (mgr, false);
433         }
434     }
435     
436     /** Handling of property changes in node structure
437      */

438     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
439         if ((evt.getSource() == manager)
440             && (manager.PROP_SELECTED_NODES.equals(evt.getPropertyName()) || manager.PROP_NODE_CHANGE.equals (evt.getPropertyName ()))) {
441             
442             final Node[] nodes = manager.getSelectedNodes();
443             String JavaDoc text = ""; // NOI18N
444

445             if (nodes.length == 1) {
446                 ModuleBean bean = (ModuleBean) nodes[0].getLookup().lookup(ModuleBean.class);
447                 if (bean != null) {
448                     try {
449                         text = "<b>" + XMLUtil.toElementContent(bean.getModule().getDisplayName()) + "</b>"; // NOI18N
450
String JavaDoc longDesc = bean.getLongDescription();
451                         if (longDesc != null) {
452                             text += "<br>" + XMLUtil.toElementContent(longDesc); // NOI18N
453
}
454                     } catch (CharConversionException JavaDoc e) {
455                         err.log(Level.WARNING, null, e);
456                     }
457                 }
458             }
459             
460             description.setText(text);
461             description.setCaretPosition (0);
462             
463             SwingUtilities.invokeLater (new Runnable JavaDoc () {
464                 public void run () {
465                     treeTableView.requestFocus ();
466                     // bugfix #61904: set enabled in following AWT queue
467
uninstallButton.setEnabled (! isWaitingState () && ModuleNodeUtils.canUninstall (nodes));
468                 }
469             });
470         }
471     }
472     
473     /** Initialize accesibility
474      */

475     public void initAccessibility(){
476
477         java.util.ResourceBundle JavaDoc b = NbBundle.getBundle(this.getClass());
478         
479         this.getAccessibleContext().setAccessibleDescription(b.getString("ACSD_ModuleSelectionPanel")); // NOI18N
480
}
481     
482     // impl of AsyncGUIJob - perf. issue 61987
483
private class WarmupJob implements AsyncGUIJob {
484
485         public void construct () {
486             prepareComponents ();
487         }
488         
489         public void finished () {
490             setWaitingState (false, false);
491             treeTableView.requestFocus();
492         }
493
494     }
495
496 }
497
Popular Tags