1 19 20 package org.netbeans.modules.j2ee.blueprints.ui; 21 22 import java.awt.Component ; 23 import java.awt.Cursor ; 24 import java.awt.Dimension ; 25 import java.awt.Font ; 26 import java.awt.FontMetrics ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.awt.geom.Rectangle2D ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.File ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.Hashtable ; 38 import java.util.Iterator ; 39 import java.util.Map ; 40 import java.util.ResourceBundle ; 41 import java.util.Set ; 42 import javax.swing.ComboBoxModel ; 43 import javax.swing.DefaultComboBoxModel ; 44 import javax.swing.DefaultListCellRenderer ; 45 import javax.swing.ImageIcon ; 46 import javax.swing.JLabel ; 47 import javax.swing.JList ; 48 import javax.swing.JOptionPane ; 49 import javax.swing.JScrollBar ; 50 import javax.swing.JScrollPane ; 51 import javax.swing.SwingUtilities ; 52 import javax.swing.border.EmptyBorder ; 53 import javax.swing.text.BadLocationException ; 54 import javax.swing.text.html.HTMLEditorKit ; 55 import org.netbeans.modules.j2ee.blueprints.catalog.SolutionsCatalog; 56 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbcatalog; 57 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbcategory; 58 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbexample; 59 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbsolution; 60 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbwriteup; 61 import org.openide.ErrorManager; 62 import org.openide.filesystems.FileUtil; 63 import org.openide.util.NbBundle; 64 import org.openide.filesystems.FileObject; 65 import org.openide.loaders.DataObject; 66 import org.openide.filesystems.Repository; 67 import org.openide.cookies.InstanceCookie; 68 import javax.swing.Action ; 69 70 71 82 public class BluePrintsPanel extends javax.swing.JPanel { 83 84 85 public BluePrintsPanel() { 86 initComponents(); 87 initTabs(); 88 initComboBox(); 89 } 90 91 96 private void initComponents() { 98 java.awt.GridBagConstraints gridBagConstraints; 99 100 jPopupMenu1 = new javax.swing.JPopupMenu (); 101 titlePanel = new javax.swing.JPanel (); 102 sunLogoLbl = new javax.swing.JLabel (); 103 titleSubPnl = new javax.swing.JPanel (); 104 titleLbl = new AntialiasedJLabel(); 105 titleSubLbl = new AntialiasedJLabel(); 106 toolbarPanel = new javax.swing.JPanel (); 107 entryCbx = new javax.swing.JComboBox (); 108 tabbedPnl = new javax.swing.JTabbedPane (); 109 categoryPnl = new CategoryTab(this); 110 solutionPnl = new SolutionTab(this); 111 designPnl = new DesignTab(this); 112 examplePnl = new ExampleTab(this); 113 homePnl = new HomeTab(this); 114 115 setLayout(new java.awt.GridBagLayout ()); 116 117 setBackground(new java.awt.Color (255, 255, 255)); 118 titlePanel.setLayout(new java.awt.GridBagLayout ()); 119 120 titlePanel.setOpaque(false); 121 sunLogoLbl.setBackground(new java.awt.Color (89, 79, 191)); 122 sunLogoLbl.setIcon(new javax.swing.ImageIcon (getClass().getResource("/org/netbeans/modules/j2ee/blueprints/ui/resources/logo_sun.gif"))); 123 sunLogoLbl.setOpaque(true); 124 gridBagConstraints = new java.awt.GridBagConstraints (); 125 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 126 titlePanel.add(sunLogoLbl, gridBagConstraints); 127 128 titleSubPnl.setLayout(new java.awt.GridBagLayout ()); 129 130 titleLbl.setBackground(new java.awt.Color (251, 226, 73)); 131 titleLbl.setFont(new java.awt.Font ("Dialog", 1, 24)); 132 titleLbl.setForeground(new java.awt.Color (89, 79, 191)); 133 titleLbl.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("NB_title")); 134 titleLbl.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 12, 1, 1)); 135 titleLbl.setOpaque(true); 136 gridBagConstraints = new java.awt.GridBagConstraints (); 137 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 138 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 139 gridBagConstraints.weightx = 1.0; 140 titleSubPnl.add(titleLbl, gridBagConstraints); 141 142 titleSubLbl.setBackground(new java.awt.Color (251, 226, 73)); 143 titleSubLbl.setForeground(new java.awt.Color (89, 79, 191)); 144 titleSubLbl.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("NB_subtitle")); 145 titleSubLbl.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 12, 1, 1)); 146 titleSubLbl.setOpaque(true); 147 gridBagConstraints = new java.awt.GridBagConstraints (); 148 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 149 gridBagConstraints.weightx = 1.0; 150 titleSubPnl.add(titleSubLbl, gridBagConstraints); 151 152 gridBagConstraints = new java.awt.GridBagConstraints (); 153 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 154 gridBagConstraints.weightx = 1.0; 155 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 156 titlePanel.add(titleSubPnl, gridBagConstraints); 157 158 gridBagConstraints = new java.awt.GridBagConstraints (); 159 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 160 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 161 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 162 gridBagConstraints.weightx = 1.0; 163 gridBagConstraints.insets = new java.awt.Insets (12, 12, 7, 12); 164 add(titlePanel, gridBagConstraints); 165 166 toolbarPanel.setLayout(new java.awt.GridBagLayout ()); 167 168 toolbarPanel.setOpaque(false); 169 entryCbx.setMaximumRowCount(16); 170 entryCbx.addItemListener(new java.awt.event.ItemListener () { 171 public void itemStateChanged(java.awt.event.ItemEvent evt) { 172 entryCbxItemStateChanged(evt); 173 } 174 }); 175 176 gridBagConstraints = new java.awt.GridBagConstraints (); 177 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 178 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 179 gridBagConstraints.weightx = 1.0; 180 gridBagConstraints.insets = new java.awt.Insets (0, 5, 0, 5); 181 toolbarPanel.add(entryCbx, gridBagConstraints); 182 183 gridBagConstraints = new java.awt.GridBagConstraints (); 184 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 185 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 186 gridBagConstraints.insets = new java.awt.Insets (0, 7, 1, 7); 187 add(toolbarPanel, gridBagConstraints); 188 189 tabbedPnl.addTab(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("categoryPnl"), categoryPnl); 190 191 tabbedPnl.addTab(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("solutionPnl"), solutionPnl); 192 193 tabbedPnl.addTab(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("designPnl"), designPnl); 194 195 tabbedPnl.addTab(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("examplePnl"), examplePnl); 196 197 tabbedPnl.addTab(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("homePnl"), homePnl); 198 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 201 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 202 gridBagConstraints.weightx = 1.0; 203 gridBagConstraints.weighty = 1.0; 204 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 12); 205 add(tabbedPnl, gridBagConstraints); 206 207 } 209 private void entryCbxItemStateChanged(java.awt.event.ItemEvent evt) { if(!navigating) { 211 if(lastSelectedIndex != entryCbx.getSelectedIndex()) { 212 lastSelectedIndex = entryCbx.getSelectedIndex(); 215 selectNewEntry(); 216 } 217 } 218 } 220 221 private javax.swing.JPanel categoryPnl; 223 private javax.swing.JPanel designPnl; 224 private javax.swing.JComboBox entryCbx; 225 private javax.swing.JPanel examplePnl; 226 private javax.swing.JPanel homePnl; 227 private javax.swing.JPopupMenu jPopupMenu1; 228 private javax.swing.JPanel solutionPnl; 229 private javax.swing.JLabel sunLogoLbl; 230 private javax.swing.JTabbedPane tabbedPnl; 231 private javax.swing.JLabel titleLbl; 232 private javax.swing.JPanel titlePanel; 233 private javax.swing.JLabel titleSubLbl; 234 private javax.swing.JPanel titleSubPnl; 235 private javax.swing.JPanel toolbarPanel; 236 238 private static final String UI_RESOURCES_URL = 239 "/org/netbeans/modules/j2ee/blueprints/ui/resources"; private static final String ICON_CATEGORY = 241 UI_RESOURCES_URL + "/category.gif"; private static final String ICON_ARTICLE = 243 UI_RESOURCES_URL + "/article.gif"; private static ResourceBundle bundle = ResourceBundle.getBundle( 245 "org/netbeans/modules/j2ee/blueprints/ui/Bundle"); static final String CATALOG_RESOURCES_URL = 247 "/org/netbeans/modules/j2ee/blueprints/catalog/resources"; 249 public static final String TAB_CATEGORY = "categoryPnl"; public static final String TAB_SOLUTION = "solutionPnl"; public static final String TAB_DESIGN = "designPnl"; public static final String TAB_EXAMPLE = "examplePnl"; public static final String TAB_HOME = "homePnl"; public final HashMap TABS = new HashMap (); 255 256 private SolutionsCatalog solutionsCatalog = SolutionsCatalog.getInstance(); 257 private BrowseHistory history = new BrowseHistory(); 258 private BrowseHistoryToken historyToken = new BrowseHistoryToken(); 259 260 private boolean navigating = false; 262 private int lastSelectedIndex = -1; 263 264 public Nbcategory getSelectedCategory() { 265 Object entry = entryCbx.getSelectedItem(); 266 if(!(entry instanceof Nbcategory)) { 267 int index = entryCbx.getSelectedIndex(); 269 do { 270 index--; 271 entry = entryCbx.getItemAt(index); 272 } while((index > 0) && !(entry instanceof Nbcategory)); 273 } 274 return (Nbcategory)entry; 275 } 276 277 public Nbsolution getSelectedArticle() { 278 Object entry = entryCbx.getSelectedItem(); 279 return (entry instanceof Nbsolution) ? (Nbsolution)entry : null; 280 } 281 282 public String getExampleId() { 283 String result = null; 284 Nbsolution solution = getSelectedArticle(); 285 if(solution != null) { 286 result = solution.getExampleId(); 287 } 288 return result; 289 } 290 291 private void initTabs() { 292 TABS.put(TAB_CATEGORY, categoryPnl); 293 TABS.put(TAB_SOLUTION, solutionPnl); 294 TABS.put(TAB_DESIGN, designPnl); 295 TABS.put(TAB_EXAMPLE, examplePnl); 296 TABS.put(TAB_HOME, homePnl); 297 tabbedPnl.removeAll(); 298 } 299 300 private void initComboBox() { 301 entryCbx.setRenderer(new EntryListCellRenderer()); 303 entryCbx.setModel(new EntryComboBoxModel()); 304 entryCbx.setSelectedIndex(0); 305 history.clear(); 306 updateTabs(); 307 historyToken = createBrowseHistoryToken(); 309 } 310 311 private void selectNewEntry() { 312 historyToken.setScrollPosition(getScrollPosition()); 315 historyToken.setTab(getSelectedTabName()); 316 history.pushBackStack(historyToken); 317 318 updateTabs(); 319 321 historyToken = createBrowseHistoryToken(); 323 } 324 325 private void goForward() { 326 if(!history.isForwardStackEmpty()) { 327 navigateTo(history.forward(createBrowseHistoryToken())); 328 } 330 } 331 332 private void goBack() { 333 if(!history.isBackStackEmpty()) { 334 navigateTo(history.back(createBrowseHistoryToken())); 335 } 337 } 338 339 private BrowseHistoryToken createBrowseHistoryToken() { 340 String category = getSelectedCategory().getId(); 341 Nbsolution solution = getSelectedArticle(); 342 String article = (solution == null) ? null : solution.getId(); 343 String tab = getSelectedTabName(); 344 int scrollPosition = getScrollPosition(); 345 return new BrowseHistoryToken(category, article, tab, scrollPosition); 346 } 347 348 private String getSelectedTabName() { 349 Component selectedTab = tabbedPnl.getSelectedComponent(); 350 String result = null; 351 Set entries = TABS.entrySet(); 352 Iterator iter = entries.iterator(); 353 while(iter.hasNext()) { 354 Map.Entry entry = (Map.Entry )iter.next(); 355 if(entry.getValue() == selectedTab) { 356 result = (String )entry.getKey(); 357 } 358 } 359 return result; 360 } 361 362 private int getScrollPosition() { 363 int result = 0; 364 Component selectedTab = tabbedPnl.getSelectedComponent(); 365 if(selectedTab != null) { 366 result = ((BluePrintsTabPanel)selectedTab).getScrollPosition(); 367 } 368 return result; 369 } 370 371 private void scrollTo(int position) { 372 Component selectedTab = tabbedPnl.getSelectedComponent(); 373 if(selectedTab != null) { 374 ((BluePrintsTabPanel)selectedTab).setScrollPosition(position); 375 } 376 } 377 378 private void navigateTo(BrowseHistoryToken token) { 379 if(token != null) { 380 String category = token.getCategory(); 381 String article = token.getArticle(); 382 navigateTo(category, article); 383 updateTabs(); 384 String tab = token.getTab(); 385 selectTab(tab); 386 int scrollPosition = token.getScrollPosition(); 387 scrollTo(scrollPosition); 388 historyToken = createBrowseHistoryToken(); 389 } 390 } 391 392 private void navigateTo(String category, String article) { 393 navigating = true; if(category != null) { 395 boolean foundCategory = false; 396 int count = entryCbx.getItemCount(); 397 for(int i = 0; i < count; i++) { 398 Object entry = entryCbx.getItemAt(i); 399 if(entry instanceof Nbcategory) { 400 Nbcategory c = (Nbcategory)entry; 401 if(c.getId().equals(category)) { 402 foundCategory = true; 403 if(article == null) { 404 entryCbx.setSelectedIndex(i); 406 break; 407 } 408 } 409 else { 410 if(foundCategory) { 411 break; 413 } 414 } 415 } 416 else { 417 if(foundCategory) { 418 Nbsolution s = (Nbsolution)entryCbx.getItemAt(i); 419 if(s.getId().equals(article)) { 420 entryCbx.setSelectedIndex(i); 422 break; 423 } 424 } 425 } 426 } 427 } 428 navigating = false; 429 } 430 431 435 436 private void showHome() { 437 tabbedPnl.removeAll(); 438 tabbedPnl.addTab(bundle.getString(TAB_HOME), 439 (Component )TABS.get(TAB_HOME)); 440 } 441 442 private void updateTabs() { 443 Nbcategory category = getSelectedCategory(); 444 Nbsolution solution = getSelectedArticle(); 445 446 if(solution == null) { 449 if(category.getId().equals("HOME")) { showHome(); 451 } else { 452 tabbedPnl.removeAll(); 453 tabbedPnl.addTab(bundle.getString(TAB_CATEGORY), 454 (Component )TABS.get(TAB_CATEGORY)); 455 } 456 } 457 else { 458 tabbedPnl.removeAll(); 459 tabbedPnl.addTab(bundle.getString(TAB_SOLUTION), 460 (Component )TABS.get(TAB_SOLUTION)); 461 tabbedPnl.addTab(bundle.getString(TAB_DESIGN), 462 (Component )TABS.get(TAB_DESIGN)); 463 tabbedPnl.addTab(bundle.getString(TAB_EXAMPLE), 464 (Component )TABS.get(TAB_EXAMPLE)); 465 } 466 467 updateCategoryTab(); 468 updateSolutionTab(); 469 updateDesignTab(); 470 updateExampleTab(); 471 updateHomeTab(); 472 } 473 474 private void selectTab(String tab) { 475 tabbedPnl.setSelectedComponent((Component )TABS.get(tab)); 476 } 477 478 private void updateCategoryTab() { 479 ((BluePrintsTabPanel)categoryPnl).updateTab(); 480 } 481 482 private void updateSolutionTab() { 483 ((BluePrintsTabPanel)solutionPnl).updateTab(); 484 } 485 486 private void updateHomeTab() { 487 ((BluePrintsTabPanel)homePnl).updateTab(); 488 } 489 490 private void updateDesignTab() { 491 Nbcategory category = getSelectedCategory(); 492 Nbsolution solution = getSelectedArticle(); 493 if(solution != null) { 494 Nbwriteup writeup = solution.getNbwriteup(); 495 if(writeup.getDesigndocPath() == null || (writeup.getDesigndocPath().trim().equals(""))) { tabbedPnl.remove(designPnl); 497 } 498 } 499 ((BluePrintsTabPanel)designPnl).updateTab(); 500 } 501 502 public void updateExampleTab() { 503 Nbcategory category = getSelectedCategory(); 504 Nbsolution solution = getSelectedArticle(); 505 if(solution != null) { 506 String exampleIds = solution.getExampleId(); 507 if(exampleIds.trim().equals("")) { tabbedPnl.remove(examplePnl); 509 } 510 } 511 ((BluePrintsTabPanel)examplePnl).updateTab(); 512 } 513 514 518 private class EntryComboBoxModel 519 extends DefaultComboBoxModel 520 { 521 522 private ArrayList entries = new ArrayList (); 523 private boolean firstTime = true; 524 525 public EntryComboBoxModel() { 526 Nbcategory homeCategory = new Nbcategory(); 528 homeCategory.setId("HOME"); homeCategory.setDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("homeCatDesc")); 530 homeCategory.setCategoryName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("homeCatName")); 531 entries.add(homeCategory); 532 533 Nbcatalog nbcatalog = solutionsCatalog.getCatalogXml(); 535 populateEntries(nbcatalog); 536 } 537 538 public int getSize() { 539 return entries.size(); 540 } 541 542 public Object getElementAt(int index) { 543 return entries.get(index); 544 } 545 546 private void populateEntries(Nbcatalog nbcatalog){ 547 Nbcategory[] cats = nbcatalog.getNbcategory(); 548 for(int categoryNum = 0; categoryNum < cats.length; categoryNum++) { 549 Nbcategory category = cats[categoryNum]; 550 if(category.getShowSpec().equals("true") && firstTime){ 551 Nbcategory enclCat = new Nbcategory(); 552 String specname = category.getSpec(); 553 enclCat.setId(specname); 554 enclCat.setDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/BundleBpcatalog").getString(getCatKeyName(specname))); 555 enclCat.setCategoryName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/BundleBpcatalog").getString(getCatKeyDesc(specname))); 556 entries.add(enclCat); 557 firstTime = false; 558 } 559 entries.add(category); 560 561 Nbsolution[] solutions = category.getNbsolution(); 562 for(int solutionNum=0; solutionNum < solutions.length; solutionNum++){ 563 entries.add(solutions[solutionNum]); 564 } 565 } 566 } 567 568 } 569 570 576 private static class EntryListCellRenderer 577 extends DefaultListCellRenderer 578 { 579 private ImageIcon categoryIcon; 580 private ImageIcon articleIcon; 581 582 public EntryListCellRenderer() { 583 this.categoryIcon = new ImageIcon (getClass().getResource( 584 ICON_CATEGORY)); 585 this.articleIcon = new ImageIcon (getClass().getResource( 586 ICON_ARTICLE)); 587 } 588 589 public Component getListCellRendererComponent(JList list, 590 Object value, int index, boolean isSelected, boolean cellHasFocus) 591 { 592 Component result = super.getListCellRendererComponent(list, value, 593 index, isSelected, cellHasFocus); 594 JLabel label = (JLabel )result; 595 ResourceBundle bundleBpcatalog = java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/BundleBpcatalog"); 596 String catName=null; 597 String fullName=null; 598 if(value instanceof Nbcategory) { 599 Nbcategory category = (Nbcategory)value; 600 label.setIcon(categoryIcon); 601 602 try { 603 catName = bundleBpcatalog.getString(getCatKeyName(category.getId())); 605 } catch (Exception e) { 606 catName = category.getCategoryName().trim(); 607 } 608 label.setText(catName); 609 } 610 else if(value instanceof Nbsolution) { 611 Nbsolution solution = (Nbsolution)value; 612 label.setIcon(articleIcon); 613 String solKey = "solution/" + solution.getId().trim() + "/full-name"; 615 try { 616 fullName = bundleBpcatalog.getString(solKey); 617 } catch (Exception e) { 618 fullName = solution.getFullName().trim(); 619 } 620 label.setText(fullName); 621 label.setBorder(new EmptyBorder (0, 19, 0, 0)); 623 } 624 return result; 625 } 626 } 627 628 private static String getCatKeyName(String catName){ 629 String nameKey = "category/" + catName.trim() + "/category-name"; return nameKey; 631 } 632 633 private static String getCatKeyDesc(String catDesc){ 634 String descKey = "category/" + catDesc.trim() + "/description"; return descKey; 636 } 637 638 } 639 640 | Popular Tags |