1 6 package org.netbeans.editor.example; 7 8 import java.util.*; 9 10 import java.net.URL ; 11 import java.io.*; 12 import java.awt.Component ; 13 import java.awt.Dimension ; 14 import java.awt.MenuItem ; 15 import java.awt.event.*; 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 20 import javax.swing.*; 21 import javax.swing.text.Document ; 22 import javax.swing.text.EditorKit ; 23 import javax.swing.text.JTextComponent ; 24 import javax.swing.event.DocumentListener ; 25 import javax.swing.event.DocumentEvent ; 26 import javax.swing.undo.UndoManager ; 27 import javax.swing.filechooser.FileFilter ; 28 import javax.swing.filechooser.FileView ; 29 import org.netbeans.editor.*; 30 import org.netbeans.editor.ext.*; 31 32 33 38 public class Editor extends javax.swing.JFrame { 39 40 private static final File distributionDirectory; 41 42 static { 43 URL url = Editor.class.getProtectionDomain().getCodeSource().getLocation(); 44 String protocol = url.getProtocol(); 45 File file = new File(url.getFile()); 46 if (!file.isDirectory()) file = file.getParentFile(); 47 distributionDirectory = file; 48 } 49 50 51 private static final String FILE = "file"; 53 private static final String CREATED = "created"; 55 private static final String MODIFIED = "modified"; 57 private ResourceBundle settings = ResourceBundle.getBundle( "settings" ); 59 private JFileChooser fileChooser; 60 61 private boolean createBackups; 62 private boolean safeSave; 63 64 private int fileCounter = -1; 65 Map com2text = new HashMap(); 66 67 private Impl impl = new Impl("org.netbeans.editor.Bundle"); 69 private class Impl extends FileView implements WindowListener, 70 ActionListener, LocaleSupport.Localizer { 71 72 private ResourceBundle bundle; 73 74 public Impl( String bundleName ) { 75 bundle = ResourceBundle.getBundle( bundleName ); 76 } 77 78 public String getName( File f ) { return null; } 80 public String getDescription( File f ) { return null; } 81 public String getTypeDescription( File f ) { return null; } 82 public Boolean isTraversable( File f ) { return null; } 83 public Icon getIcon( File f ) { 84 if( f.isDirectory() ) return null; 85 KitInfo ki = KitInfo.getKitInfoForFile( f ); 86 return ki == null ? null : ki.getIcon(); 87 } 88 89 public String getString( String key ) { 91 return bundle.getString( key ); 92 } 93 94 public void windowActivated(WindowEvent evt) {} 96 public void windowClosed(WindowEvent evt) {} 97 public void windowDeactivated(WindowEvent evt) {} 98 public void windowDeiconified(WindowEvent evt) {} 99 public void windowIconified(WindowEvent evt) {} 100 public void windowOpened(WindowEvent evt) {} 101 public void windowClosing(java.awt.event.WindowEvent evt) { 102 doExit(); 103 } 104 105 public void actionPerformed(java.awt.event.ActionEvent evt) { 107 Object src = evt.getSource(); 108 109 if (!handleOpenRecent(src)) { 110 if (src == openItem) { 111 fileChooser.setMultiSelectionEnabled(true); 112 int returnVal = fileChooser.showOpenDialog(Editor.this); 113 if (returnVal == JFileChooser.APPROVE_OPTION) { 114 File[] files = fileChooser.getSelectedFiles(); 115 for (int i = 0; i < files.length; i++) openFile(files[i], i == 0); 116 } 117 fileChooser.setMultiSelectionEnabled(false); 118 } else if (src == closeItem) { 119 Component editor = tabPane.getSelectedComponent(); 120 if (checkClose(editor)) { 121 doCloseEditor(editor); 122 } 123 } else if (src == saveItem) { 124 saveFile(tabPane.getSelectedComponent()); 125 } else if (src == saveAsItem) { 126 saveAs(tabPane.getSelectedComponent()); 127 } else if (src == saveAllItem) { 128 int index = tabPane.getSelectedIndex(); 129 for (int i = 0; i < tabPane.getComponentCount(); i++) { 130 saveFile(tabPane.getComponentAt(i)); 131 } 132 tabPane.setSelectedIndex(index); 133 } else if (src == exitItem) { 134 doExit(); 135 } else if (src instanceof JMenuItem) { 136 Object ki = ((JMenuItem) src).getClientProperty("kitInfo"); 138 if (ki instanceof KitInfo) { 139 createNewFile((KitInfo) ki); 140 } 141 } 142 } 143 } 144 } 145 146 public Editor() { 147 super( "NetBeans Editor" ); LocaleSupport.addLocalizer(impl); 149 150 Settings.addInitializer(new BaseSettingsInitializer(), Settings.CORE_LEVEL); 152 Settings.addInitializer(new ExtSettingsInitializer(), Settings.CORE_LEVEL); 153 Settings.reset(); 154 155 initComponents (); 157 openItem.addActionListener(impl); 158 closeItem.addActionListener(impl); 159 saveItem.addActionListener(impl); 160 saveAsItem.addActionListener(impl); 161 saveAllItem.addActionListener(impl); 162 exitItem.addActionListener(impl); 163 addWindowListener(impl); 164 165 readSettings(); 167 168 setLocation( 150, 150 ); 170 pack (); 171 172 fileToMenu = new HashMap(); 173 menuToFile = new HashMap(); 174 recentFiles = new Vector(); 175 maxRecent = 4; 176 177 createBackups = false; 178 safeSave = true; 179 } 180 181 public Dimension getPreferredSize() { 182 Dimension size = new Dimension ( 640,480 ); 183 return size; 184 } 185 186 191 private void initComponents() { tabPane = new javax.swing.JTabbedPane (); 193 menuBar = new javax.swing.JMenuBar (); 194 fileMenu = new javax.swing.JMenu (); 195 newMenu = new javax.swing.JMenu (); 196 openItem = new javax.swing.JMenuItem (); 197 closeItem = new javax.swing.JMenuItem (); 198 sep1 = new javax.swing.JSeparator (); 199 saveItem = new javax.swing.JMenuItem (); 200 saveAsItem = new javax.swing.JMenuItem (); 201 saveAllItem = new javax.swing.JMenuItem (); 202 sep2 = new javax.swing.JSeparator (); 203 exitItem = new javax.swing.JMenuItem (); 204 205 getContentPane().setLayout(new java.awt.GridLayout (1, 1)); 206 207 setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); 208 getContentPane().add(tabPane); 209 210 fileMenu.setMnemonic(KeyEvent.VK_F); 211 fileMenu.setText("File"); newMenu.setMnemonic(KeyEvent.VK_N); 213 newMenu.setText("New..."); fileMenu.add(newMenu); 215 openItem.setMnemonic(KeyEvent.VK_O); 216 openItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK)); 217 openItem.setText("Open File..."); fileMenu.add(openItem); 219 closeItem.setMnemonic(KeyEvent.VK_C); 220 closeItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.CTRL_MASK)); 221 closeItem.setText("Close"); fileMenu.add(closeItem); 223 fileMenu.add(sep1); 224 saveItem.setMnemonic(KeyEvent.VK_S); 225 saveItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK)); 226 saveItem.setText("Save"); fileMenu.add(saveItem); 228 saveAsItem.setMnemonic(KeyEvent.VK_A); 229 saveAsItem.setText("Save As..."); fileMenu.add(saveAsItem); 231 saveAllItem.setMnemonic(KeyEvent.VK_L); 232 saveAllItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK)); 233 saveAllItem.setText("Save All"); fileMenu.add(saveAllItem); 235 fileMenu.add(sep2); 236 exitItem.setMnemonic(KeyEvent.VK_E); 237 exitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK)); 238 exitItem.setText("Exit"); fileMenu.add(exitItem); 240 menuBar.add(fileMenu); 241 setJMenuBar(menuBar); 242 243 } 245 private boolean saveFile( Component comp, File file, boolean checkOverwrite ) { 246 if( comp == null ) return false; 247 tabPane.setSelectedComponent( comp ); 248 JTextComponent edit = (JTextComponent )com2text.get( comp ); 249 Document doc = edit.getDocument(); 250 251 if( checkOverwrite && file.exists() ) { 252 tabPane.setSelectedComponent( comp ); 253 int choice = JOptionPane.showOptionDialog(this, 254 "File " + file.getName() + " already exists, overwrite?", "File exists", JOptionPane.YES_NO_CANCEL_OPTION, 257 JOptionPane.QUESTION_MESSAGE, 258 null, null, null ); 262 if( choice != 0 ) return false; 263 } 264 265 File safeSaveFile = new File(file.getAbsolutePath() + "~~"); File backupFile = new File(file.getAbsolutePath() + "~"); 268 if (safeSave || createBackups) { 269 file.renameTo(safeSaveFile); 270 } 271 272 FileWriter output = null; 273 274 try { 275 output = new FileWriter( file ); 276 edit.write( output ); 277 278 if (createBackups) { 279 safeSaveFile.renameTo(backupFile); 280 } else { 281 if (safeSave) { 282 safeSaveFile.delete(); 283 } 284 } 285 } catch( IOException exc ) { 286 JOptionPane.showMessageDialog( this, "Can't write to file '" + file.getName() + "'.", "Error", JOptionPane.ERROR_MESSAGE ); 289 if (safeSave) 290 safeSaveFile.renameTo(file); 291 292 return false; 293 } finally { 294 if (output != null) { 295 try { 296 output.close(); 297 } catch (IOException e) { 298 e.printStackTrace(System.err); 299 } 300 } 301 } 302 303 doc.putProperty( MODIFIED, Boolean.FALSE ); 304 doc.putProperty( CREATED, Boolean.FALSE ); 305 doc.putProperty( FILE, file ); 306 doc.addDocumentListener( new MarkingDocumentListener( comp ) ); 307 308 int index = tabPane.indexOfComponent( comp ); 309 tabPane.setTitleAt( index, file.getName() ); 310 311 return true; 312 } 313 314 private boolean saveFile( Component comp ) { 315 if( comp == null ) return false; 316 JTextComponent edit = (JTextComponent )com2text.get( comp ); 317 Document doc = edit.getDocument(); 318 File file = (File)doc.getProperty( FILE ); 319 boolean created = ((Boolean )doc.getProperty( CREATED )).booleanValue(); 320 321 return saveFile( comp, file, created ); 322 } 323 324 private boolean saveAs( Component comp ) { 325 if( comp == null ) return false; 326 JTextComponent edit = (JTextComponent )com2text.get( comp ); 327 File file = (File)edit.getDocument().getProperty( FILE ); 328 329 fileChooser.setCurrentDirectory( file.getParentFile() ); 330 fileChooser.setSelectedFile( file ); 331 KitInfo fileInfo = KitInfo.getKitInfoOrDefault( file ); 332 333 if( fileInfo != null ) fileChooser.setFileFilter( fileInfo ); 334 335 if( fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 337 return saveFile( comp, fileChooser.getSelectedFile(), true ); 338 else 339 return false; } 341 342 private void openFile( File file, boolean focus ) { 343 KitInfo info = KitInfo.getKitInfoOrDefault( file ); 344 345 final JEditorPane pane = new JEditorPane( info.getType(), "" ); 346 try { 347 pane.read( new FileInputStream( file ), file.getCanonicalPath() ); 348 } catch( IOException exc ) { 349 JOptionPane.showMessageDialog( this, "Can't read from file '" + file.getName() + "'.", "Error", JOptionPane.ERROR_MESSAGE ); return; 352 } 353 addEditorPane( pane, info.getIcon(), file, false, focus ); 354 355 removeFromRecent(file.getAbsolutePath()); 356 } 357 358 private void doExit() { 359 boolean exit = true; 360 int components = tabPane.getComponentCount(); 361 362 for (int cntr = 0; cntr < components; cntr++) { 363 Component editor = tabPane.getComponentAt( cntr ); 364 365 if( ! checkClose( editor ) ) { 366 exit = false; 367 return; 368 } 369 } 370 371 if (!exit) { 372 System.err.println("keeping"); 373 return; 374 } 375 376 writeUserConfiguration(); 377 378 while( tabPane.getComponentCount() > 0 ) { 379 Component editor = tabPane.getComponentAt( 0 ); 380 381 if ((editor != null) && (com2text.get(editor) != null)) 382 doCloseEditor(editor); 383 } 384 385 if( exit ) System.exit (0); 386 } 387 388 private void doCloseEditor(Component editor) { 389 JTextComponent editorPane = (JTextComponent ) com2text.get(editor); 390 if (editorPane != null) { 391 File file = (File) editorPane.getDocument().getProperty(FILE); 392 393 addToRecent(file.getAbsolutePath()); 394 } 395 396 tabPane.remove( editor ); 397 com2text.remove( editor ); 398 } 399 400 private boolean checkClose( Component comp ) { 401 if( comp == null ) return false; 402 JTextComponent edit = (JTextComponent )com2text.get( comp ); 403 Document doc = edit.getDocument(); 404 405 Object mod = doc.getProperty( MODIFIED ); 406 if( mod == null || ! ((Boolean )mod).booleanValue()) return true; 407 408 tabPane.setSelectedComponent( comp ); 409 File file = (File)doc.getProperty( FILE ); 410 411 for( ;; ) { 412 int choice = JOptionPane.showOptionDialog(this, 413 "File " + file.getName() + " was modified, save it?", "File modified", JOptionPane.YES_NO_CANCEL_OPTION, 416 JOptionPane.QUESTION_MESSAGE, 417 null, new String [] { "Save", "Save As...", "Discard", "Cancel" }, "Cancel" ); 421 422 switch( choice ) { 423 case JOptionPane.CLOSED_OPTION: 424 case 4: 425 return false; case 1: 427 if( !saveAs( comp ) ) continue; return true; 429 case 0: 430 if( !saveFile( comp ) ) continue; case 2: 432 return true; } 434 return false; 435 } 436 } 437 438 private void addEditorPane( JEditorPane pane, Icon icon, File file, boolean created, boolean focus ) { 439 final JComponent c = (pane.getUI() instanceof BaseTextUI) ? 440 Utilities.getEditorUI(pane).getExtComponent() : new JScrollPane( pane ); 441 Document doc = pane.getDocument(); 442 443 doc.addDocumentListener( new MarkingDocumentListener( c ) ); 444 doc.putProperty( FILE, file ); 445 doc.putProperty( CREATED, created ? Boolean.TRUE : Boolean.FALSE ); 446 447 UndoManager um = new UndoManager (); 448 doc.addUndoableEditListener( um ); 449 doc.putProperty( BaseDocument.UNDO_MANAGER_PROP, um ); 450 451 com2text.put( c, pane ); 452 tabPane.addTab( file.getName(), icon, c, file.getAbsolutePath() ); 453 if (focus) { 454 tabPane.setSelectedComponent( c ); 455 pane.requestFocus(); 456 } 457 } 458 459 460 private void createNewFile( KitInfo info ) { 461 final String fileName = ((++fileCounter == 0 ) ? 462 "unnamed" : ("unnamed" + fileCounter)) + info.getDefaultExtension(); final File file = new File( fileName ).getAbsoluteFile(); 465 466 final JEditorPane pane = new JEditorPane( info.getType(), "" ); 467 URL template = info.getTemplate(); 468 if( template != null ) { 469 try { 470 pane.read( template.openStream(), file.getCanonicalPath() ); 471 } catch( IOException e ) { 472 JOptionPane.showMessageDialog( this, "Can't read template", "Error", JOptionPane.ERROR_MESSAGE ); } 474 } 475 addEditorPane( pane, info.getIcon(), file, true, true ); 476 } 477 478 479 480 public static File getDistributionDirectory() { 481 return distributionDirectory; 482 } 483 484 487 public static void main (String args[]) { 488 if (!getDistributionDirectory().canRead()) { 489 System.err.println("Fatal error while startup - can read from distribution directory."); 490 System.exit(0); 491 } 492 493 Editor editor = new Editor (); 494 495 editor.show (); 496 497 editor.readUserConfiguration(); 498 499 for( int i = 0; i < args.length; i++ ) { 500 String fileName = args[i]; 501 editor.openFile( new File( fileName ), i == 0 ); 502 } 503 } 504 505 private Map fileToMenu; 506 private Map menuToFile; 507 private Vector recentFiles; 508 private int maxRecent; 509 private JSeparator recentSeparator; 510 private int separatorIndex; 511 512 private String [] getOpenedFiles() { 513 ArrayList opened = new ArrayList (); 514 515 int components = tabPane.getComponentCount(); 516 517 for (int cntr = 0; cntr < components; cntr++) { 518 Component editorComponent = tabPane.getComponentAt( cntr ); 519 520 JTextComponent editor = (JTextComponent ) com2text.get(editorComponent); 521 522 if (editor == null) { 523 continue; 524 } 525 526 Document doc = editor.getDocument(); 527 File file = (File) doc.getProperty(FILE); 528 529 if (file != null) { 530 opened.add(file.getAbsolutePath()); 531 } 532 } 533 534 return (String []) opened.toArray(new String [opened.size()]); 535 } 536 537 private int findInRecent(String fileToFind) { 538 for (int cntr = 0; cntr < recentFiles.size(); cntr++) { 539 String file = (String ) recentFiles.get(cntr); 540 541 if (fileToFind.equals(file)) 542 return cntr; 543 } 544 545 return -1; 546 } 547 548 private boolean handleOpenRecent(Object source) { 549 String fileName = (String ) menuToFile.get(source); 550 551 if (fileName == null) 552 return false; 553 554 openFile(new File(fileName), true); 555 556 return true; 557 } 558 559 private String generateMenuItemName(int index, String file) { 560 return "" + index + ". " + file; } 562 563 private void addToRecent(String fileToAdd) { 564 removeFromRecent(fileToAdd); 566 567 if (recentFiles.size() >= maxRecent) { 568 while (recentFiles.size() >= maxRecent) { 569 removeFromRecent(recentFiles.size() - 1); 570 } 571 } 572 573 recentFiles.add(0, fileToAdd); 574 575 JMenuItem newItem = new JMenuItem(generateMenuItemName(1, fileToAdd)); 576 577 if (recentFiles.size() == 1) { 578 recentSeparator = new JSeparator(); 579 fileMenu.add(recentSeparator); 580 separatorIndex = fileMenu.getMenuComponentCount(); 581 } 582 583 newItem.addActionListener(impl); 584 585 fileMenu.insert(newItem, separatorIndex); 586 fileToMenu.put(fileToAdd, newItem); 587 menuToFile.put(newItem, fileToAdd); 588 589 correctItemNumbers(); 590 } 591 592 private void correctItemNumbers() { 593 for (int cntr = 0; cntr < recentFiles.size(); cntr++) { 594 JMenuItem item = (JMenuItem ) fileToMenu.get(recentFiles.get(cntr)); 595 596 item.setText(generateMenuItemName(cntr + 1, (String ) recentFiles.get(cntr))); 597 } 598 } 599 600 private void removeFromRecent(String fileToRemove) { 601 int position = findInRecent(fileToRemove); 602 603 if (position != (-1)) 604 removeFromRecent(position); 605 } 606 607 private void removeFromRecent(int indexToRemove) { 608 String file = (String ) recentFiles.get(indexToRemove); 609 610 recentFiles.remove(indexToRemove); 611 612 JMenuItem fileItem = (JMenuItem) fileToMenu.get(file); 613 614 fileMenu.remove(fileItem); 615 616 fileToMenu.remove(file); 617 menuToFile.remove(fileItem); 618 619 correctItemNumbers(); 620 621 if (recentFiles.size() == 0) { 622 fileMenu.remove(recentSeparator); 623 recentSeparator = null; 624 separatorIndex = -1; 625 } 626 } 627 628 private String [] readStrings(ResourceBundle bundle, String prefix) { 629 int count = 0; 630 boolean finish = false; 631 ArrayList result = new ArrayList (); 632 633 while (!finish) { 634 try { 635 String current = bundle.getString(prefix + "_" + count); 636 637 result.add(current); 638 count++; 639 } catch (MissingResourceException e) { 640 finish = true; 641 } 642 } 643 644 return (String []) result.toArray(new String [result.size()]); 645 } 646 647 private void readUserConfiguration(ResourceBundle bundle) { 648 String [] openedFiles = readStrings(bundle, "Open-File"); String [] recentFiles = readStrings(bundle, "Recent-File"); String recentFilesMaxCount = bundle.getString("Max-Recent-Files"); 651 String safeSaveString = bundle.getString("Safe-Save"); 652 String createBackupsString = bundle.getString("Create-Backups"); 653 654 this.maxRecent = Integer.parseInt(recentFilesMaxCount); 655 this.safeSave = Boolean.valueOf(safeSaveString).booleanValue(); 656 this.createBackups = Boolean.valueOf(createBackupsString).booleanValue(); 657 658 for (int cntr = recentFiles.length; cntr > 0; cntr--) { 659 addToRecent(recentFiles[cntr - 1]); 660 } 661 662 for (int cntr = 0; cntr < openedFiles.length; cntr++) { 663 openFile(new File(openedFiles[cntr]), false); 664 } 665 } 666 667 private void writeUserConfiguration(PrintWriter output) { 668 output.println("Max-Recent-Files=" + maxRecent); output.println("Safe-Save=" + safeSave); output.println("Create-Backups=" + createBackups); 672 for (int cntr = 0; cntr < recentFiles.size(); cntr++) { 673 output.println("Recent-File_" + cntr + "=" + recentFiles.get(cntr)); } 675 String [] openFiles = getOpenedFiles(); 676 677 for (int cntr = 0; cntr < openFiles.length; cntr++) { 678 output.println("Open-File_" + cntr + "=" + openFiles[cntr]); } 680 } 681 682 private File getConfigurationFileName() { 683 File homedir = new File( System.getProperty( "user.home" ) ).getAbsoluteFile(); 684 File configurationFile = new File(homedir, ".nb-editor"); 686 return configurationFile; 687 } 688 689 private void writeUserConfiguration() { 690 File configurationFile = getConfigurationFileName(); 691 File configurationFileBackup = new File(configurationFile.getAbsolutePath() + "~"); boolean backup = false; 693 694 if (configurationFile.exists()) { 695 backup = true; 696 configurationFile.renameTo(configurationFileBackup); 697 } 698 699 PrintWriter output = null; 700 try { 701 output = new PrintWriter(new FileWriter(configurationFile)); 702 703 writeUserConfiguration(output); 704 705 if (backup) { 706 if (!output.checkError()) { 707 configurationFileBackup.delete(); 708 } else { 709 configurationFileBackup.renameTo(configurationFile); 711 } 712 } 713 } catch (IOException e) { 714 e.printStackTrace(); 715 } finally { 716 if (output != null) { 717 output.close(); 718 } 719 } 720 } 721 722 private void readUserConfiguration() { 723 File configurationFileName = getConfigurationFileName(); 724 InputStream in = null; 725 try { 726 in = new FileInputStream(configurationFileName); 727 readUserConfiguration(new PropertyResourceBundle(in)); 728 } catch (FileNotFoundException e) { 729 try { 732 System.err.println("User configuration not found in \"" + configurationFileName.getCanonicalPath() + "\"."); 733 } catch (IOException f) { 734 } 735 } catch (IOException e) { 736 e.printStackTrace(); 737 } finally { 738 if (in != null) { 739 try { 740 in.close(); 741 } catch (IOException e) { 742 e.printStackTrace(); 743 } 744 } 745 } 746 747 } 748 749 private javax.swing.JSeparator sep2; 751 private javax.swing.JSeparator sep1; 752 private javax.swing.JMenu newMenu; 753 private javax.swing.JMenuItem saveAllItem; 754 private javax.swing.JMenuItem closeItem; 755 private javax.swing.JMenuBar menuBar; 756 private javax.swing.JMenuItem exitItem; 757 private javax.swing.JTabbedPane tabPane; 758 private javax.swing.JMenuItem saveAsItem; 759 private javax.swing.JMenu fileMenu; 760 private javax.swing.JMenuItem openItem; 761 private javax.swing.JMenuItem saveItem; 762 764 765 private void readSettings() throws MissingResourceException { 766 File currentPath = new File( System.getProperty( "user.dir" ) ).getAbsoluteFile(); 767 fileChooser = new JFileChooser( currentPath ); 768 769 fileChooser.setFileView(impl); 770 771 String kits = settings.getString( "InstalledEditors" ); 772 String defaultKit = settings.getString( "DefaultEditor" ); 773 774 StringTokenizer st = new StringTokenizer( kits, "," ); while( st.hasMoreTokens() ) { 776 String kitName = st.nextToken(); 777 String contentType = settings.getString( kitName + "_ContentType" ); 779 String extList = settings.getString( kitName + "_ExtensionList" ); 780 String menuTitle = settings.getString( kitName + "_NewMenuTitle" ); 781 char menuMnemonic = settings.getString( kitName + "_NewMenuMnemonic" ).charAt( 0 ); 782 String templateURL = settings.getString( kitName + "_Template" ); 783 String iconName = settings.getString( kitName + "_Icon" ); 784 String filterTitle = settings.getString( kitName + "_FileFilterTitle" ); 785 String kit = settings.getString( kitName + "_KitClass" ); 786 787 Class kitClass; 789 try { 790 kitClass = Class.forName( kit ); 791 } catch( ClassNotFoundException exc ) { throw new MissingResourceException( "Missing class", kit, "KitClass" ); } 794 795 Icon icon = null; 797 ClassLoader loader = kitClass.getClassLoader(); 798 if( loader == null ) loader = ClassLoader.getSystemClassLoader(); 799 URL resource = loader.getResource( iconName ); 800 if( resource == null ) resource = ClassLoader.getSystemResource( iconName ); 801 if( resource != null ) icon = new ImageIcon( resource ); 802 803 URL template = loader.getResource( templateURL ); 805 if( resource == null ) template = ClassLoader.getSystemResource( templateURL ); 806 807 List l = new ArrayList ( 5 ); 809 StringTokenizer extST = new StringTokenizer( extList, "," ); while( extST.hasMoreTokens() ) l.add( extST.nextToken() ); 811 812 KitInfo ki = new KitInfo( contentType, l, template, icon, filterTitle, kitClass, loader, defaultKit.equals( kitName ) ); 814 815 JMenuItem item = new JMenuItem( menuTitle, icon ); 817 item.setMnemonic( menuMnemonic ); 818 item.putClientProperty( "kitInfo", ki ); item.addActionListener( impl ); 820 newMenu.add( item ); 821 822 fileChooser.addChoosableFileFilter( ki ); 824 } 825 826 828 fileChooser.addChoosableFileFilter( new FileFilter () { 829 public String getDescription() { 830 return "All recognized files"; } 832 833 public boolean accept( File f ) { 834 return f.isDirectory() || KitInfo.getKitInfoForFile( f ) != null; 835 } 836 }); 837 838 if( KitInfo.getDefault() == null ) throw new MissingResourceException( "Missing default kit definition", defaultKit, "DefaultEditor" ); } 840 841 private static final class KitInfo extends FileFilter { 842 843 private static List kits = new ArrayList (); 844 private static KitInfo defaultKitInfo; 845 846 public static List getKitList() { 847 return new ArrayList ( kits ); 848 } 849 850 public static KitInfo getDefault() { 851 return defaultKitInfo; 852 } 853 854 public static KitInfo getKitInfoOrDefault( File f ) { 855 KitInfo ki = getKitInfoForFile( f ); 856 return ki == null ? defaultKitInfo : ki; 857 } 858 859 public static KitInfo getKitInfoForFile( File f ) { 860 for( int i = 0; i < kits.size(); i++ ) { 861 if( ((KitInfo)kits.get(i)).accept( f ) ) 862 return (KitInfo)kits.get(i); 863 } 864 return null; 865 } 866 867 private String type; 868 private String [] extensions; 869 private URL template; 870 private Icon icon; 871 private Class kitClass; 872 private String description; 873 874 public KitInfo( String type, List exts, URL template, Icon icon, String description, Class kitClass, ClassLoader loader, boolean isDefault ) { 875 this.type = type; 877 this.extensions = (String [])exts.toArray( new String [0] ); 878 this.template = template; 879 this.icon = icon; 880 this.description = description; 881 this.kitClass = kitClass; 882 883 JEditorPane.registerEditorKitForContentType( type, kitClass.getName(), loader ); 885 kits.add( this ); 886 if( isDefault ) defaultKitInfo = this; 887 } 888 889 890 891 public String getType() { 892 return type; 893 } 894 895 public String getDefaultExtension() { 896 return extensions[0]; 897 } 898 899 public URL getTemplate() { 900 return template; 901 } 902 903 public Icon getIcon() { 904 return icon; 905 } 906 907 public Class getKitClass() { 908 return kitClass; 909 } 910 911 public String getDescription() { 912 return description; 913 } 914 915 public boolean accept( File f ) { 916 if( f.isDirectory() ) return true; 917 String fileName = f.getName(); 918 for( int i=0; i<extensions.length; i++ ) { 919 if( fileName.endsWith( extensions[i] ) ) return true; 920 } 921 return false; 922 } 923 } 924 925 930 private class MarkingDocumentListener implements DocumentListener { 931 private Component comp; 932 933 public MarkingDocumentListener( Component comp ) { 934 this.comp = comp; 935 } 936 937 private void markChanged( DocumentEvent evt ) { 938 Document doc = evt.getDocument(); 939 doc.putProperty( MODIFIED, Boolean.TRUE ); 940 941 File file = (File)doc.getProperty( FILE ); 942 int index = tabPane.indexOfComponent( comp ); 943 944 tabPane.setTitleAt( index, file.getName() + '*' ); 945 946 doc.removeDocumentListener( this ); 947 } 948 949 public void changedUpdate( DocumentEvent e ) { 950 } 951 952 public void insertUpdate( DocumentEvent evt ) { 953 markChanged( evt ); 954 } 955 956 public void removeUpdate( DocumentEvent evt ) { 957 markChanged( evt ); 958 } 959 } 960 } 961 | Popular Tags |