1 19 20 package org.netbeans.core.windows.view.ui; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dialog ; 24 import java.awt.Dimension ; 25 import java.awt.Image ; 26 import java.awt.Window ; 27 import java.awt.event.ActionEvent ; 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyVetoException ; 31 import java.io.IOException ; 32 import java.text.Collator ; 33 import java.util.ArrayList ; 34 import java.util.Arrays ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 import javax.swing.Action ; 40 import javax.swing.JPanel ; 41 import javax.swing.SwingUtilities ; 42 import javax.swing.UIManager ; 43 import javax.swing.border.Border ; 44 import org.netbeans.core.windows.Constants; 45 import org.netbeans.core.windows.ModeImpl; 46 import org.netbeans.core.windows.WindowManagerImpl; 47 import org.openide.DialogDescriptor; 48 import org.openide.DialogDisplayer; 49 import org.openide.awt.Mnemonics; 50 import org.openide.cookies.SaveCookie; 51 import org.openide.explorer.ExplorerManager; 52 import org.openide.explorer.view.ListView; 53 import org.openide.nodes.AbstractNode; 54 import org.openide.nodes.Children; 55 import org.openide.nodes.Node; 56 import org.openide.util.HelpCtx; 57 import org.openide.util.Lookup; 58 import org.openide.util.NbBundle; 59 import org.openide.util.Utilities; 60 import org.openide.util.WeakListeners; 61 import org.openide.windows.TopComponent; 62 63 68 public class DocumentsDlg extends JPanel implements PropertyChangeListener , ExplorerManager.Provider { 69 70 private static DocumentsDlg defaultInstance; 71 72 private final ExplorerManager explorer = new ExplorerManager(); 73 74 75 private DocumentsDlg () { 76 initComponents(); 77 78 Mnemonics.setLocalizedText(jButtonActivate, NbBundle.getMessage(DocumentsDlg.class, "LBL_Activate")); 80 Mnemonics.setLocalizedText(jButtonClose, NbBundle.getMessage(DocumentsDlg.class, "LBL_CloseDocuments")); 81 Mnemonics.setLocalizedText(jButtonSave, NbBundle.getMessage(DocumentsDlg.class, "LBL_SaveDocuments")); 82 Mnemonics.setLocalizedText(explorerLabel, NbBundle.getMessage(DocumentsDlg.class, "LBL_Documents")); 83 Mnemonics.setLocalizedText(descriptionLabel, NbBundle.getMessage(DocumentsDlg.class, "LBL_Description")); 84 85 Mnemonics.setLocalizedText(closeButton, NbBundle.getMessage(DocumentsDlg.class, "LBL_Close")); 86 87 explorerLabel.setLabelFor(listView); 89 descriptionLabel.setLabelFor(descriptionArea); 90 91 jButtonActivate.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentsDlg.class, "ACSD_Activate")); 93 jButtonClose.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentsDlg.class, "ACSD_CloseDocuments")); 94 jButtonSave.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentsDlg.class, "ACSD_SaveDocuments")); 95 closeButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentsDlg.class, "ACSD_Close")); 96 descriptionArea.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentsDlg.class, "ACSD_DescriptionArea")); 97 } 98 99 100 private static DocumentsDlg getDefault() { 101 if(defaultInstance == null) { 102 defaultInstance = new DocumentsDlg(); 103 } 104 return defaultInstance; 105 } 106 107 108 public HelpCtx getHelpCtx() { 109 return new HelpCtx(DocumentsDlg.class); 111 } 112 113 118 private void initComponents() { 120 java.awt.GridBagConstraints gridBagConstraints; 121 122 explorerLabel = new javax.swing.JLabel (); 123 descriptionLabel = new javax.swing.JLabel (); 124 explorerPanel = createListView(); 125 jScrollPane1 = new javax.swing.JScrollPane (); 126 descriptionArea = new javax.swing.JTextArea (); 127 jButtonActivate = new javax.swing.JButton (); 128 jButtonClose = new javax.swing.JButton (); 129 jButtonSave = new javax.swing.JButton (); 130 closeButton = new javax.swing.JButton (); 131 132 FormListener formListener = new FormListener(); 133 134 setLayout(new java.awt.GridBagLayout ()); 135 136 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11)); 137 gridBagConstraints = new java.awt.GridBagConstraints (); 138 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 139 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 140 add(explorerLabel, gridBagConstraints); 141 142 gridBagConstraints = new java.awt.GridBagConstraints (); 143 gridBagConstraints.gridx = 0; 144 gridBagConstraints.gridy = 5; 145 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 146 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 147 add(descriptionLabel, gridBagConstraints); 148 149 gridBagConstraints = new java.awt.GridBagConstraints (); 150 gridBagConstraints.gridx = 0; 151 gridBagConstraints.gridy = 1; 152 gridBagConstraints.gridheight = 4; 153 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 154 gridBagConstraints.weightx = 1.0; 155 gridBagConstraints.weighty = 1.0; 156 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 157 add(explorerPanel, gridBagConstraints); 158 159 jScrollPane1.setPreferredSize(new java.awt.Dimension (3, 60)); 160 descriptionArea.setEditable(false); 161 jScrollPane1.setViewportView(descriptionArea); 162 163 gridBagConstraints = new java.awt.GridBagConstraints (); 164 gridBagConstraints.gridx = 0; 165 gridBagConstraints.gridy = 6; 166 gridBagConstraints.gridheight = 3; 167 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 168 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH; 169 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 11); 170 add(jScrollPane1, gridBagConstraints); 171 172 jButtonActivate.addActionListener(formListener); 173 174 gridBagConstraints = new java.awt.GridBagConstraints (); 175 gridBagConstraints.gridx = 1; 176 gridBagConstraints.gridy = 1; 177 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 178 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 179 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 180 add(jButtonActivate, gridBagConstraints); 181 182 jButtonClose.addActionListener(formListener); 183 184 gridBagConstraints = new java.awt.GridBagConstraints (); 185 gridBagConstraints.gridx = 1; 186 gridBagConstraints.gridy = 2; 187 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 188 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 189 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 190 add(jButtonClose, gridBagConstraints); 191 192 jButtonSave.addActionListener(formListener); 193 194 gridBagConstraints = new java.awt.GridBagConstraints (); 195 gridBagConstraints.gridx = 1; 196 gridBagConstraints.gridy = 3; 197 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 198 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 199 gridBagConstraints.weighty = 1.0; 200 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 201 add(jButtonSave, gridBagConstraints); 202 203 closeButton.addActionListener(formListener); 204 205 gridBagConstraints = new java.awt.GridBagConstraints (); 206 gridBagConstraints.gridx = 1; 207 gridBagConstraints.gridy = 7; 208 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 209 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH; 210 gridBagConstraints.weighty = 0.1; 211 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 212 add(closeButton, gridBagConstraints); 213 214 } 215 216 218 private class FormListener implements java.awt.event.ActionListener { 219 public void actionPerformed(java.awt.event.ActionEvent evt) { 220 if (evt.getSource() == jButtonActivate) { 221 DocumentsDlg.this.activate(evt); 222 } 223 else if (evt.getSource() == jButtonClose) { 224 DocumentsDlg.this.closeDocuments(evt); 225 } 226 else if (evt.getSource() == jButtonSave) { 227 DocumentsDlg.this.saveDocuments(evt); 228 } 229 else if (evt.getSource() == closeButton) { 230 DocumentsDlg.this.closeButtonActionPerformed(evt); 231 } 232 } 233 } 235 private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) { closeDialog(); 237 } 239 private void saveDocuments(java.awt.event.ActionEvent evt) { Node[] selNodes = explorer.getSelectedNodes(); 242 if (selNodes.length == 0) { 243 return; 244 } 245 for (int i = 0; i < selNodes.length; i++) { 246 TopComponent tc = ((TopComponentNode) selNodes[i]).getTopComponent(); 247 Lookup l = tc.getLookup(); 248 SaveCookie sc = (SaveCookie) l.lookup(SaveCookie.class); 249 if (sc != null) { 250 try { 251 sc.save(); 252 } catch (IOException exc) { 253 Logger.getAnonymousLogger().log(Level.WARNING, 254 "[WinSys.DocumentsDlg.saveDocuments]" + " Warning: Cannot save content of TopComponent: [" + WindowManagerImpl.getInstance().getTopComponentDisplayName(tc) + "]" + " [" + tc.getClass().getName() + "]", exc); } 259 ((TopComponentNode) selNodes[i]).refresh(); 262 } 263 } 264 jButtonSave.setEnabled(false); 265 } 267 private void closeDocuments(java.awt.event.ActionEvent evt) { Node[] selNodes = explorer.getSelectedNodes(); 270 if (selNodes.length == 0) { 271 return; 272 } 273 for (int i = 0; i < selNodes.length; i++) { 274 TopComponent tc = ((TopComponentNode) selNodes[i]).getTopComponent(); 275 tc.close(); 276 } 277 278 List <TopComponent> tcList = getOpenedDocuments(); 279 List <TopComponentNode> tcNodes = new ArrayList <TopComponentNode> (tcList.size()); 280 for (TopComponent tc : tcList) { 281 tcNodes.add(new TopComponentNode(tc)); 282 } 283 284 if(tcList.isEmpty()) { 285 closeDialog(); 287 } else { 288 java.util.Collections.sort(tcNodes); 290 Children.Array nodeArray = new Children.Array(); 291 nodeArray.add((TopComponentNode[])tcNodes.toArray(new TopComponentNode[0])); 292 Node root = new AbstractNode(nodeArray); 293 explorer.setRootContext(root); 294 try { 296 explorer.setSelectedNodes(new Node[] {root.getChildren().getNodes()[0]}); 297 } catch (PropertyVetoException exc) { 298 } 300 listView.requestFocusInWindow(); 301 } 303 } 305 private void activate(java.awt.event.ActionEvent evt) { Node[] selNodes = explorer.getSelectedNodes(); 308 if (selNodes.length == 0) { 309 return; 310 } 311 312 closeDialog(); 313 314 final TopComponent tc = ((TopComponentNode) selNodes[0]).getTopComponent(); 315 SwingUtilities.invokeLater(new Runnable () { 318 public void run() { 319 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 321 ModeImpl mode = (ModeImpl)wm.findMode(tc); 322 if(mode != null && mode != wm.getCurrentMaximizedMode()) { 323 wm.switchMaximizedMode(null); 324 } 325 tc.requestActive(); 326 } 327 }); 328 } 330 private void closeDialog() { 331 Window w = SwingUtilities.getWindowAncestor(this); 332 w.setVisible(false); 333 w.dispose(); 334 } 335 336 public void addNotify () { 337 explorer.addPropertyChangeListener(this); 338 jButtonActivate.setEnabled(false); 339 jButtonClose.setEnabled(false); 340 jButtonSave.setEnabled(false); 341 super.addNotify(); 342 } 343 344 public void removeNotify () { 345 super.removeNotify(); 346 explorer.removePropertyChangeListener(this); 347 } 348 349 350 public static void showDocumentsDialog() { 351 DocumentsDlg documentsPanel = getDefault(); 352 DialogDescriptor dlgDesc = new DialogDescriptor( 353 documentsPanel, 354 NbBundle.getMessage(DocumentsDlg.class, "CTL_DocumentsTitle"), 355 true, new Object [0], 357 getDefault().jButtonActivate, 359 DialogDescriptor.DEFAULT_ALIGN, 360 null, 361 null 362 ); 363 Dialog dlg = DialogDisplayer.getDefault().createDialog(dlgDesc); 364 dlg.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentsDlg.class, "ACSD_DocumentsDialog")); 365 getDefault().updateNodes(); 366 dlg.setVisible(true); 367 getDefault().clearNodes(); 368 } 369 370 375 public static boolean isEmpty () { 376 for(Object elem : WindowManagerImpl.getInstance().getModes()) { 377 ModeImpl mode = (ModeImpl)elem; 378 if(mode.getKind() == Constants.MODE_KIND_EDITOR && 379 !mode.getOpenedTopComponents().isEmpty()) { 380 return false; 381 } 382 } 383 return true; 384 } 385 386 private JPanel createListView () { 387 JPanel panel = new JPanel (); 388 panel.setPreferredSize(new Dimension (375, 232)); 392 panel.setLayout(new BorderLayout ()); 393 listView = new ListView(); 394 listView.setBorder((Border )UIManager.get("Nb.ScrollPane.border")); listView.setPopupAllowed(false); 397 listView.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentsDlg.class, "ACSD_ListView")); 398 panel.add(listView, BorderLayout.CENTER); 400 return panel; 401 } 402 403 private void updateNodes() { 404 List <TopComponent> tcList = getOpenedDocuments(); 406 TopComponent activeTC = TopComponent.getRegistry().getActivated(); 407 TopComponentNode[] tcNodes = new TopComponentNode[tcList.size()]; 408 TopComponentNode toSelect = null; 409 for (int i = 0; i < tcNodes.length; i++) { 410 TopComponent tc = tcList.get(i); 411 tcNodes[i] = new TopComponentNode(tc); 412 if( tc == activeTC ) { 413 toSelect = tcNodes[i]; 414 } 415 } 416 Arrays.sort(tcNodes); 417 418 Children.Array nodeArray = new Children.Array(); 419 nodeArray.add(tcNodes); 420 Node root = new AbstractNode(nodeArray); 421 explorer.setRootContext(root); 422 listView.requestFocus(); 424 if (tcNodes.length > 0) { 426 try { 427 if( null == toSelect ) 428 toSelect = tcNodes[0]; 429 explorer.setSelectedNodes(new Node[] {toSelect} ); 430 } catch (PropertyVetoException exc) { 431 } 433 } 434 } 435 436 private void clearNodes() { 437 explorer.setRootContext(Node.EMPTY); 438 } 439 440 private static List <TopComponent> getOpenedDocuments() { 441 List <TopComponent> documents = new ArrayList <TopComponent> (); 442 for(Object elem : WindowManagerImpl.getInstance().getModes()) { 443 ModeImpl mode = (ModeImpl)elem; 444 if(mode.getKind() == Constants.MODE_KIND_EDITOR) { 445 documents.addAll(mode.getOpenedTopComponents()); 446 } 447 } 448 449 return documents; 450 } 451 452 public void propertyChange(PropertyChangeEvent evt) { 453 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 454 Node [] selNodes = (Node []) evt.getNewValue(); 455 if (selNodes.length == 1) { 457 jButtonActivate.setEnabled(true); 458 } else { 459 jButtonActivate.setEnabled(false); 460 } 461 if (selNodes.length > 0) { 463 jButtonClose.setEnabled(true); 464 } else { 465 jButtonClose.setEnabled(false); 466 } 467 boolean enableSave = false; 470 for (int i = 0; i < selNodes.length; i++) { 471 TopComponent tc = ((TopComponentNode) selNodes[i]).getTopComponent(); 472 Lookup l = tc.getLookup(); 473 SaveCookie sc = (SaveCookie) l.lookup(SaveCookie.class); 474 if (sc != null) { 475 enableSave = true; 476 break; 477 } 478 } 479 jButtonSave.setEnabled(enableSave); 480 481 if(selNodes != null && selNodes.length == 1) { 483 descriptionArea.setText(((TopComponentNode)selNodes[0]).getDescription()); 484 } else { 485 descriptionArea.setText(null); 486 } 487 } 488 } 489 490 private javax.swing.JButton closeButton; 492 private javax.swing.JTextArea descriptionArea; 493 private javax.swing.JLabel descriptionLabel; 494 private javax.swing.JLabel explorerLabel; 495 private javax.swing.JPanel explorerPanel; 496 private javax.swing.JButton jButtonActivate; 497 private javax.swing.JButton jButtonClose; 498 private javax.swing.JButton jButtonSave; 499 private javax.swing.JScrollPane jScrollPane1; 500 private ListView listView; 502 503 public ExplorerManager getExplorerManager() { 504 return explorer; 505 } 506 507 private static final Collator COLLATOR = Collator.getInstance(); 508 509 510 private class TopComponentNode extends AbstractNode 511 implements Comparable <TopComponentNode>, Action , PropertyChangeListener { 512 513 private TopComponent tc; 514 515 public TopComponentNode (TopComponent tc) { 516 super(Children.LEAF); 517 this.tc = tc; 518 tc.addPropertyChangeListener(WeakListeners.propertyChange(this, tc)); 519 } 520 521 public String getName() { 522 return tc.getName(); 524 } 525 public String getDisplayName() { 526 String result = tc.getDisplayName(); 528 return result != null ? result : tc.getName(); 529 } 530 531 public String getHtmlDisplayName() { 532 return tc.getHtmlDisplayName(); 533 } 534 535 public Image getIcon (int type) { 536 Image image = tc.getIcon(); 537 return image == null ? Utilities.loadImage("org/openide/resources/actions/empty.gif") : image; } 539 540 public String getDescription() { 541 return tc.getToolTipText(); 542 } 543 544 public TopComponent getTopComponent () { 545 return tc; 546 } 547 548 549 void refresh () { 550 fireNameChange(null, null); 551 } 552 553 public int compareTo(TopComponentNode tcn) { 554 String displayName1 = getDisplayName(); 555 String displayName2 = tcn.getDisplayName(); 556 557 if(displayName1 == null) { 558 return displayName2 == null ? 0 : -1; 559 } else { 560 return displayName2 == null ? 1 : COLLATOR.compare(displayName1, displayName2); 561 } 562 } 563 564 566 public Action getPreferredAction() { 567 return this; 568 } 569 570 574 public void actionPerformed(ActionEvent evt) { 575 activate(evt); 576 } 577 578 public boolean isEnabled() { 579 return true; 580 } 581 582 public void putValue(String key, Object value) { 583 } 585 586 public void setEnabled(boolean b) { 587 } 589 590 public void propertyChange(PropertyChangeEvent evt) { 591 fireDisplayNameChange(null, null); 592 } 593 594 } 595 } 596 | Popular Tags |