1 19 20 package org.openide.loaders; 21 22 import java.awt.Component ; 23 import java.util.LinkedList ; 24 import java.util.ResourceBundle ; 25 import javax.swing.JComponent ; 26 import javax.swing.SwingUtilities ; 27 import javax.swing.UIManager ; 28 import javax.swing.border.Border ; 29 import javax.swing.border.EtchedBorder ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import javax.swing.tree.TreeSelectionModel ; 33 import org.openide.awt.HtmlBrowser; 34 import org.openide.awt.Mnemonics; 35 import org.openide.explorer.ExplorerManager; 36 import org.openide.explorer.view.BeanTreeView; 37 import org.openide.explorer.view.NodeTreeModel; 38 import org.openide.explorer.view.Visualizer; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.Repository; 41 import org.openide.nodes.Children; 42 import org.openide.nodes.FilterNode; 43 import org.openide.nodes.Node; 44 import org.openide.util.AsyncGUIJob; 45 import org.openide.util.NbBundle; 46 import org.openide.util.RequestProcessor; 47 import org.openide.util.Utilities; 48 49 53 final class TemplateWizard1 extends javax.swing.JPanel implements DataFilter, 54 ExplorerManager.Provider, java.beans.PropertyChangeListener , AsyncGUIJob { 55 57 private static final String PROP_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; 60 private static final String PROP_CONTENT_DATA = "WizardPanel_contentData"; 62 private ChangeListener listener; 63 64 private DataObject template; 65 66 private DataFolder templatesRoot; 67 68 private ExplorerManager manager; 69 70 72 private static final class InitData { 73 HtmlBrowser browser; 74 String noDescMsg; 75 Border noDescBorder; 76 }; 78 79 private InitData initData; 80 81 82 public TemplateWizard1 () { 83 initComponents (); 84 85 treeView = new TemplatesTreeView(); 86 treeView.setDefaultActionAllowed(false); 87 treeView.setPopupAllowed(false); 88 treeView.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 89 java.awt.GridBagConstraints gridBagConstraints1 = new java.awt.GridBagConstraints (); 90 gridBagConstraints1.gridx = 0; 91 gridBagConstraints1.gridy = 1; 92 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 93 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 11, 0); 94 gridBagConstraints1.weightx = 1.0; 95 gridBagConstraints1.weighty = 1.0; 96 add(treeView, gridBagConstraints1); 97 98 ResourceBundle bundle = org.openide.util.NbBundle.getBundle(TemplateWizard1.class); 99 100 setName (bundle.getString("LAB_TemplateChooserPanelName")); 101 102 putClientProperty(PROP_CONTENT_SELECTED_INDEX, 0); 103 putClientProperty(PROP_CONTENT_DATA, new String [] {getName(), "..."}); 105 putClientProperty("LAB_SelectTemplateBorder", bundle.getString("LAB_SelectTemplateBorder")); 108 putClientProperty("LAB_TemplateDescriptionBorder", bundle.getString("LAB_TemplateDescriptionBorder")); 110 putClientProperty("ACSD_TemplatesTree", bundle.getString("ACSD_TemplatesTree")); 112 putClientProperty("ACSD_TemplateWizard1", bundle.getString("ACSD_TemplateWizard1")); 114 116 updateRootNode (null); 117 118 templatesLabel.setLabelFor(treeView); 119 120 noBrowser.setText(bundle.getString("MSG_InitDescription")); 121 java.awt.CardLayout card = (java.awt.CardLayout )browserPanel.getLayout(); 122 card.show (browserPanel, "noBrowser"); Utilities.attachInitJob(this, this); 125 } 126 127 public void addNotify() { 128 Mnemonics.setLocalizedText(templatesLabel, 131 (String )getClientProperty("LAB_SelectTemplateBorder") ); 133 Mnemonics.setLocalizedText(browserLabel, 134 (String )getClientProperty("LAB_TemplateDescriptionBorder") ); 136 137 treeView.getAccessibleContext().setAccessibleDescription( 138 (String )getClientProperty("ACSD_TemplatesTree") ); 140 getAccessibleContext().setAccessibleDescription( 141 (String )getClientProperty("ACSD_TemplateWizard1") ); 143 144 super.addNotify(); 145 } 146 147 148 public ExplorerManager getExplorerManager() { 149 if (manager == null) { 150 manager = new ExplorerManager(); 151 manager.addPropertyChangeListener(this); 152 } 153 return manager; 154 } 155 156 157 @SuppressWarnings ("deprecation") 158 public boolean requestDefaultFocus() { 159 return treeView.requestDefaultFocus(); 160 } 161 162 163 public java.awt.Dimension getPreferredSize() { 164 return TemplateWizard.PREF_DIM; 165 } 166 167 170 private void updateRootNode (DataFolder root) { 171 if (root == null) { 172 FileObject fo = Repository.getDefault ().getDefaultFileSystem ().findResource ("/Templates"); if (fo != null && fo.isFolder ()) 174 root = DataFolder.findFolder (fo); 175 } 176 177 if (root == null || root.equals(templatesRoot)) 178 return; 179 180 templatesRoot = root; 181 182 Children ch = new DataShadowFilterChildren(root.getNodeDelegate()); 183 getExplorerManager().setRootContext(new DataShadowFilterNode (root.getNodeDelegate(), ch, root.getNodeDelegate().getDisplayName ())); 184 } 185 186 private class DataShadowFilterChildren extends FilterNode.Children { 187 188 public DataShadowFilterChildren (Node or) { 189 super (or); 190 } 191 192 194 @Override 195 protected Node[] createNodes(Node key) { 196 Node n = key; 197 String nodeName = n.getDisplayName(); 198 199 DataObject obj = null; 200 DataShadow shadow = n.getCookie(DataShadow.class); 201 if (shadow != null) { 202 DataNode dn = new DataNode(shadow, Children.LEAF); 205 nodeName = dn.getDisplayName(); 206 obj = shadow.getOriginal(); 207 n = obj.getNodeDelegate(); 208 } 209 210 if (obj == null) 211 obj = n.getCookie(DataObject.class); 212 213 if (obj != null) { 214 if (obj.isTemplate ()) { 215 return new Node[] { new DataShadowFilterNode (n, Children.LEAF, nodeName) }; 217 } 218 219 if (acceptDataObject (obj)) { 220 return new Node[] { new DataShadowFilterNode (n, new DataShadowFilterChildren (n), nodeName) }; 222 } 223 } 224 return new Node[] {}; 225 } 226 227 } 228 229 230 private static class DataShadowFilterNode extends FilterNode { 231 232 private String name; 233 234 public DataShadowFilterNode (Node or, org.openide.nodes.Children children, String name) { 235 super (or, children); 236 this.name = name; 237 disableDelegation(FilterNode.DELEGATE_SET_DISPLAY_NAME); 238 } 239 240 public String getDisplayName() { 241 return name; 242 } 243 244 public boolean canRename () { 246 return false; 247 } 248 249 } 250 251 252 255 private void updateDescription (DataObject obj) { 256 java.net.URL url = null; 257 if (obj != null) { 258 url = TemplateWizard.getDescription (obj); 259 } 260 java.awt.CardLayout card = (java.awt.CardLayout )browserPanel.getLayout(); 261 if (url != null && 262 getExplorerManager().getSelectedNodes().length != 0) { 263 if (browser != null) { 264 browser.setURL(url); 265 if (!browser.isVisible()) { 266 card.show (browserPanel, "browser"); } 268 } 269 } else { 270 card.show (browserPanel, "noBrowser"); } 272 } 273 274 279 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 281 282 browserPanel = new javax.swing.JPanel (); 283 noBrowser = new javax.swing.JLabel (); 284 templatesLabel = new javax.swing.JLabel (); 285 browserLabel = new javax.swing.JLabel (); 286 287 setLayout(new java.awt.GridBagLayout ()); 288 289 setPreferredSize(new java.awt.Dimension (0, 0)); 290 browserPanel.setLayout(new java.awt.CardLayout ()); 291 292 noBrowser.setBackground(javax.swing.UIManager.getDefaults().getColor("EditorPane.background")); 294 noBrowser.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 295 noBrowser.setMinimumSize(new java.awt.Dimension (0, 25)); 298 noBrowser.setOpaque(true); 299 browserPanel.add(noBrowser, "noBrowser"); 300 301 gridBagConstraints = new java.awt.GridBagConstraints (); 302 gridBagConstraints.gridx = 0; 303 gridBagConstraints.gridy = 3; 304 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 305 gridBagConstraints.weighty = 0.5; 306 add(browserPanel, gridBagConstraints); 307 308 gridBagConstraints = new java.awt.GridBagConstraints (); 309 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 310 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 311 add(templatesLabel, gridBagConstraints); 312 313 gridBagConstraints = new java.awt.GridBagConstraints (); 314 gridBagConstraints.gridx = 0; 315 gridBagConstraints.gridy = 2; 316 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 317 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 318 add(browserLabel, gridBagConstraints); 319 320 } 322 323 private void nameFocusGained (java.awt.event.FocusEvent evt) { } private void templatesTreeValueChanged (javax.swing.event.TreeSelectionEvent evt) { } 328 329 public void propertyChange(java.beans.PropertyChangeEvent evt) { 330 if (evt.getPropertyName() == ExplorerManager.PROP_SELECTED_NODES && listener != null) { 331 listener.stateChanged (new ChangeEvent (this)); 332 333 updateDescription (template); 334 } 335 } 336 337 338 private void packagesListValueChanged (javax.swing.event.ListSelectionEvent evt) { } 341 342 private javax.swing.JLabel browserLabel; 344 private javax.swing.JPanel browserPanel; 345 private javax.swing.JLabel noBrowser; 346 private javax.swing.JLabel templatesLabel; 347 private TemplatesTreeView treeView; 349 private HtmlBrowser browser; 350 351 356 public boolean acceptDataObject(DataObject obj) { 357 if (obj instanceof DataFolder) { 358 Object o = obj.getPrimaryFile ().getAttribute ("simple"); return o == null || Boolean.TRUE.equals (o); 360 } else { 361 return obj.isTemplate(); 362 } 363 } 364 365 368 public void construct() { 369 initData = new InitData(); 370 initData.browser = new HtmlBrowser(false, false); 371 initData.browser.setName("browser"); 372 initData.noDescMsg = NbBundle.getBundle(TemplateWizard1.class). 373 getString("MSG_NoDescription"); 374 initData.noDescBorder = new EtchedBorder (); 375 376 379 Component comp = initData.browser.getBrowserComponent(); 380 if (! (comp instanceof javax.swing.JEditorPane )) 381 return; 382 383 javax.swing.text.EditorKit kit = ((javax.swing.JEditorPane ) comp).getEditorKitForContentType("text/html"); if (! (kit instanceof javax.swing.text.html.HTMLEditorKit )) 385 return; 386 387 javax.swing.text.html.HTMLEditorKit htmlkit = (javax.swing.text.html.HTMLEditorKit ) kit; 388 389 394 if (htmlkit.getStyleSheet().getStyleSheets() != null) 395 return; 396 397 javax.swing.text.html.StyleSheet css = new javax.swing.text.html.StyleSheet (); 398 java.awt.Font f = new javax.swing.JTextArea ().getFont(); 399 css.addRule(new StringBuffer ("body { font-size: ").append(f.getSize()) .append("; font-family: ").append(f.getName()).append("; }").toString()); css.addStyleSheet(htmlkit.getStyleSheet()); 402 htmlkit.setStyleSheet(css); 403 } 404 405 407 public void finished() { 408 browser = initData.browser; 409 browserLabel.setLabelFor(browser); 410 browser.getAccessibleContext().setAccessibleName(browserLabel.getText()); 411 browserPanel.add(browser, "browser"); 412 updateDescription(template); 413 noBrowser.setText(initData.noDescMsg); 416 noBrowser.setBorder(initData.noDescBorder); 417 initData = null; 419 } 420 421 427 void implReadSettings (Object settings) { 428 TemplateWizard wizard = (TemplateWizard)settings; 429 wizard.setTitle(org.openide.util.NbBundle.getBundle(TemplateWizard.class).getString("CTL_TemplateTitle")); 430 updateRootNode (wizard.getTemplatesFolder ()); 431 432 template = wizard.getTemplate (); 433 if (template != null && !template.isValid() ) { 434 template = null; 435 } 436 437 DataObject obj = template; 440 DataObject stop = wizard.getTemplatesFolder (); 441 final LinkedList <String > names = new LinkedList <String >(); 442 for (;;) { 443 if (obj == null) { 444 break; 446 } 447 448 if (obj == stop) { 449 break; 451 } 452 453 String key = obj.getNodeDelegate().getName (); 454 names.addFirst(key); 455 obj = obj.getFolder(); 456 } 457 458 RequestProcessor.getDefault ().post (new Runnable () { 459 private Node selection; 460 461 public void run () { 462 if (selection == null) { 463 Node node = getExplorerManager().getRootContext(); 465 for (String name : names) { 466 node = node.getChildren ().findChild (name); 467 if (node == null) { 468 node = getExplorerManager().getRootContext(); 470 break; 471 } 472 } 473 474 selection = node; 475 476 SwingUtilities.invokeLater (this); 478 } else { 479 try { 481 getExplorerManager().setSelectedNodes(new Node[] {selection}); 482 } catch (java.beans.PropertyVetoException evt) { 483 } 485 } 486 } 487 }, 300, Thread.MIN_PRIORITY); 488 } 489 490 500 void implStoreSettings (Object settings) { 501 if (template != null) { 502 TemplateWizard wizard = (TemplateWizard)settings; 503 if (wizard.getTemplate() != template) { 504 Component c = wizard.targetChooser().getComponent(); 506 if (c instanceof JComponent ) { 507 ((JComponent )c).putClientProperty(PROP_CONTENT_DATA, new String [] { c.getName() }); 508 ((JComponent )c).putClientProperty(PROP_CONTENT_SELECTED_INDEX, new Integer (0)); 509 } 510 } else { 511 Component c = wizard.targetChooser().getComponent(); 513 if (c instanceof JComponent ) { 514 if (((JComponent )c).getClientProperty (PROP_CONTENT_DATA) == null) { 515 ((JComponent )c).putClientProperty(PROP_CONTENT_DATA, new String [] { c.getName() }); 516 ((JComponent )c).putClientProperty(PROP_CONTENT_SELECTED_INDEX, new Integer (0)); 517 } 518 } 519 } 520 wizard.setTemplateImpl (template, false); 521 } 522 } 523 524 529 boolean implIsValid () { 530 boolean enable = false; 531 Node[] n = getExplorerManager().getSelectedNodes(); 532 if (n.length == 1) { 533 template = n[0].getCookie(DataObject.class); 534 enable = template != null && template.isTemplate(); 535 } 536 return enable; 537 } 538 539 543 void addChangeListener (ChangeListener l) { 544 if (listener != null) throw new IllegalStateException (); 545 listener = l; 546 } 547 548 551 void removeChangeListener (ChangeListener l) { 552 listener = null; 553 } 554 555 556 558 private static final class TemplatesModel extends NodeTreeModel { 559 TemplatesModel() {} 560 561 public int getChildCount (Object o) { 562 Node n = Visualizer.findNode(o); 563 DataObject obj = n.getCookie(DataObject.class); 564 565 return obj == null || obj.isTemplate () ? 0 : super.getChildCount (o); 566 } 567 568 public boolean isLeaf (Object o) { 569 Node n = Visualizer.findNode(o); 570 DataObject obj = n.getCookie(DataObject.class); 571 572 return obj == null || obj.isTemplate (); 573 } 574 } 575 576 578 private static final class TemplatesTreeView extends BeanTreeView { 579 TemplatesTreeView() { 580 tree.setEditable(false); 581 setBorder((Border )UIManager.get("Nb.ScrollPane.border")); } 584 585 protected NodeTreeModel createModel() { 586 return new TemplatesModel(); 587 } 588 } 589 } 590 | Popular Tags |