1 19 20 package org.netbeans.core.windows.view.ui.toolbars; 21 22 import java.util.logging.Logger ; 23 import org.netbeans.core.NbPlaces; 24 import org.openide.awt.JPopupMenuPlus; 25 import org.openide.awt.Toolbar; 26 import org.openide.awt.ToolbarPool; 27 import org.openide.filesystems.FileLock; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileSystem; 30 import org.openide.loaders.XMLDataObject; 31 import org.openide.util.NbBundle; 32 import org.openide.util.Utilities; 33 import org.xml.sax.*; 34 35 import javax.swing.*; 36 import java.awt.*; 37 import java.awt.event.ActionEvent ; 38 import java.awt.event.ActionListener ; 39 import java.beans.PropertyChangeEvent ; 40 import java.beans.PropertyChangeListener ; 41 import java.beans.PropertyChangeSupport ; 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.io.OutputStream ; 45 import java.io.OutputStreamWriter ; 46 import java.io.Writer ; 47 import java.util.*; 48 import java.util.logging.Level ; 49 import org.netbeans.core.IDESettings; 50 59 public final class ToolbarConfiguration extends Object 60 implements ToolbarPool.Configuration, PropertyChangeListener { 61 62 protected static final String TOOLBAR_DTD_WEB = 63 "http://www.netbeans.org/dtds/toolbar.dtd"; 65 protected static final String TOOLBAR_DTD_PUBLIC_ID = 66 "-//NetBeans IDE//DTD toolbar//EN"; 68 protected static final Class TOOLBAR_PROCESSOR_CLASS = ToolbarProcessor.class; 69 70 protected static final String TOOLBAR_ICON_BASE = 71 "/org/netbeans/core/windows/toolbars/xmlToolbars"; 73 74 private static Logger ERR = Logger.getLogger("org.netbeans.core.windows.toolbars"); 76 77 private volatile long lastReload; 78 79 80 protected static final String EXT_XML = "xml"; 84 85 protected static final String TAG_CONFIG = "Configuration"; 87 protected static final String TAG_ROW = "Row"; 89 protected static final String TAG_TOOLBAR = "Toolbar"; 91 protected static final String ATT_TOOLBAR_NAME = "name"; 93 protected static final String ATT_TOOLBAR_POSITION = "position"; 95 protected static final String ATT_TOOLBAR_VISIBLE = "visible"; 97 98 private static JPanel toolbarPanel; 99 100 private static WeakHashMap<ToolbarConfiguration, String > confs2Names = 101 new WeakHashMap<ToolbarConfiguration, String >(10); 102 103 104 private ToolbarLayout toolbarLayout; 105 106 private ToolbarDnDListener toolbarListener; 107 108 109 private WeakHashMap<String , ToolbarConstraints> allToolbars; 110 111 private Vector<ToolbarRow> toolbarRows; 112 113 private HashMap<ToolbarConstraints,Integer > invisibleToolbars; 114 115 117 private static JMenu toolbarMenu; 118 119 122 private WeakHashMap<String , ToolbarConstraints> waitingToolbars; 123 124 private String configName; 125 126 private String configDisplayName; 127 128 private int prefWidth; 129 132 private final ThreadLocal <Boolean > WRITE_IN_PROGRESS = new ThreadLocal <Boolean > (); 133 134 136 139 public ToolbarConfiguration (String name, String displayName) { 140 configName = name; 141 configDisplayName = displayName; 142 if (configDisplayName.endsWith(".xml")) { 144 configDisplayName = configDisplayName.substring(0, configDisplayName.length() - ".xml".length()); 145 } 146 initInstance (); 147 confs2Names.put(this, name); 149 } 150 151 155 public ToolbarConfiguration(XMLDataObject xml) throws IOException { 156 this(xml.getNodeDelegate().getName(), xml.getNodeDelegate().getDisplayName()); 157 readConfig(xml); 158 } 159 160 private void readConfig(XMLDataObject xml) throws IOException { 161 Parser parser = xml.createParser(); 162 163 164 ToolbarParser handler = new ToolbarParser(); 165 parser.setEntityResolver(handler); 166 parser.setDocumentHandler(handler); 167 168 InputStream is = null; 169 try { 170 is = xml.getPrimaryFile().getInputStream(); 171 parser.parse(new InputSource(is)); 172 } catch (Exception saxe) { 173 throw (IOException ) new IOException (saxe.toString()).initCause(saxe); 174 } finally { 175 try { 176 if (is != null) { 177 is.close(); 178 } 179 } catch (IOException exc) { 180 Logger.getLogger(ToolbarConfiguration.class.getName()).log(Level.WARNING, null, exc); 181 } 182 } 183 checkToolbarRows(); 184 } 185 186 private class ToolbarParser extends HandlerBase implements EntityResolver { 187 private ToolbarRow currentRow = null; 188 private int toolbarIndex = 0; 189 190 public void startElement(String name, AttributeList amap) throws SAXException { 191 if (TAG_ROW.equals(name)) { 192 toolbarIndex = 0; 193 currentRow = new ToolbarRow(ToolbarConfiguration.this); 194 addRow(currentRow); 195 } 196 else if (currentRow != null && TAG_TOOLBAR.equals(name)) { 197 String tbname = amap.getValue(ATT_TOOLBAR_NAME); 198 if (tbname == null || tbname.equals("")) return; 200 201 String posStr = amap.getValue(ATT_TOOLBAR_POSITION); 202 Integer pos = null; 203 if (posStr != null) 204 pos = new Integer (posStr); 205 206 String visStr = amap.getValue(ATT_TOOLBAR_VISIBLE); 207 Boolean vis; 208 if (visStr != null) 209 vis = Boolean.valueOf(visStr); 210 else 211 vis = Boolean.TRUE; 212 213 addToolbar(currentRow, checkToolbarConstraints (tbname, pos, vis, toolbarIndex++)); 214 } 215 } 216 217 public void endElement(String name) throws SAXException { 218 if (TAG_ROW.equals(name)) { 219 currentRow = null; 220 } 221 } 222 223 public InputSource resolveEntity(String pubid, String sysid) { 224 return new InputSource(new java.io.ByteArrayInputStream (new byte[0])); 225 } 226 }; 227 229 private void initInstance () { 230 allToolbars = new WeakHashMap<String , ToolbarConstraints>(); 231 waitingToolbars = new WeakHashMap<String , ToolbarConstraints>(); 232 toolbarRows = new Vector<ToolbarRow>(); 233 invisibleToolbars = new HashMap<ToolbarConstraints, Integer >(); 234 toolbarListener = new ToolbarDnDListener (this); 235 } 236 237 238 static final String getBundleString (String bundleStr) { 239 return NbBundle.getMessage(ToolbarConfiguration.class, bundleStr); 240 } 241 242 245 public static final ToolbarConfiguration findConfiguration (String name) { 246 Map.Entry curEntry = null; 247 for (Iterator iter = confs2Names.entrySet().iterator(); iter.hasNext(); ) { 248 curEntry = (Map.Entry)iter.next(); 249 if (name.equals((String )curEntry.getValue())) { 250 return (ToolbarConfiguration)curEntry.getKey(); 251 } 252 } 253 return null; 255 } 256 257 265 void addToolbar (ToolbarRow row, ToolbarConstraints tc) { 266 if (tc == null) 267 return; 268 269 if (tc.isVisible()) 270 row.addToolbar (tc); 271 else { 272 int rI; 273 if (row == null) 274 rI = toolbarRows.size(); 275 else 276 rI = toolbarRows.indexOf (row); 277 invisibleToolbars.put (tc, Integer.valueOf(rI)); 278 } 279 allToolbars.put (tc.getName(), tc); 280 } 281 282 287 ToolbarConstraints removeToolbar (String name) { 288 ToolbarConstraints tc = allToolbars.remove (name); 289 if (tc.destroy()) 290 checkToolbarRows(); 291 return tc; 292 } 293 294 297 void addRow (ToolbarRow row) { 298 addRow (row, toolbarRows.size()); 299 } 300 301 305 void addRow (ToolbarRow row, int index) { 306 307 ToolbarRow prev = null; 308 ToolbarRow next = null; 309 int rowCount = toolbarRows.size(); 310 if( index > 0 && index <= rowCount ) 311 prev = toolbarRows.elementAt( index - 1 ); 312 if( index >= 0 && index < rowCount ) 313 next = toolbarRows.elementAt (index); 314 315 if (prev != null) 316 prev.setNextRow (row); 317 row.setPrevRow (prev); 318 row.setNextRow (next); 319 if (next != null) 320 next.setPrevRow (row); 321 322 toolbarRows.insertElementAt (row, index); 323 updateBounds (row); 324 } 325 326 329 void removeRow (ToolbarRow row) { 330 331 ToolbarRow prev = row.getPrevRow(); 332 ToolbarRow next = row.getNextRow(); 333 if (prev != null) { 334 prev.setNextRow (next); 335 } 336 if (next != null) { 337 next.setPrevRow (prev); 338 } 339 340 toolbarRows.removeElement (row); 341 updateBounds (next); 342 revalidateWindow(); 343 } 344 345 348 void updateBounds (ToolbarRow row) { 349 while (row != null) { 350 row.updateBounds(); 351 row = row.getNextRow(); 352 } 353 } 354 355 private static final ToolbarPool toolbarPool () { 356 return ToolbarPool.getDefault (); 357 } 358 359 362 void revalidateWindow () { 363 toolbarPanel().revalidate(); 365 } 372 373 382 386 int rowIndex (ToolbarRow row) { 387 return toolbarRows.indexOf (row); 388 } 389 390 392 void updatePrefWidth () { 393 prefWidth = 0; 394 for (ToolbarRow tr: toolbarRows) { 395 prefWidth = Math.max (prefWidth, tr.getPrefWidth()); 396 } 397 } 398 399 402 int getPrefWidth () { 403 return prefWidth; 404 } 405 406 410 int getPrefHeight () { 411 if (getRowCount() == 0) return 0; 412 ToolbarRow lastRow = (ToolbarRow)toolbarRows.lastElement(); 413 return getRowVertLocation(lastRow) + lastRow.getPreferredHeight(); 414 } 415 416 418 void checkToolbarRows () { 419 Object [] rows = toolbarRows.toArray(); 420 ToolbarRow row; 421 422 for (int i = rows.length - 1; i >= 0; i--) { 423 row = (ToolbarRow)rows[i]; 424 if (row.isEmpty()) 425 removeRow (row); 426 } 427 } 428 429 432 int getRowCount () { 433 return toolbarRows.size(); 434 } 435 436 440 ToolbarConstraints getToolbarConstraints (String name) { 441 return allToolbars.get (name); 442 } 443 444 454 ToolbarConstraints checkToolbarConstraints (String name, Integer position, Boolean visible, int toolbarIndex) { 455 ToolbarConstraints tc = allToolbars.get (name); 456 if (tc == null) 457 tc = new ToolbarConstraints (this, name, position, visible, toolbarIndex); 458 else 459 tc.checkNextPosition (position, visible); 460 return tc; 461 } 462 463 469 boolean checkConfigurationOver () { 470 boolean change = false; 471 String name; 472 String [] waNas = waitingToolbars.keySet().toArray(new String [0]); 473 String [] names = allToolbars.keySet().toArray(new String [0]); 474 475 476 for (int i = 0; i < waNas.length; i++) { 477 name = waNas[i]; 478 if (toolbarPool ().findToolbar (name) != null) { 480 ToolbarConstraints tc = (ToolbarConstraints)waitingToolbars.remove (name); 481 482 allToolbars.put (name, tc); 483 addVisible (tc); 484 change = true; 485 } 486 } 487 488 489 for (int i = 0; i < names.length; i++) { 490 name = names[i]; 491 if (toolbarPool ().findToolbar (name) == null) { 492 ToolbarConstraints tc = removeToolbar (name); 493 waitingToolbars.put (name, tc); 494 invisibleToolbars.put (tc, Integer.valueOf(tc.rowIndex())); 495 change = true; 496 } 497 } 498 if (change || Utilities.arrayHashCode(toolbarPool().getConfigurations()) != lastConfigurationHash) { 499 rebuildMenu(); 500 } 501 return change; 502 } 503 504 void refresh() { 505 rebuildPanel(); 506 rebuildMenu(); 507 } 508 509 private int lastConfigurationHash = -1; 510 private void rebuildMenu() { 511 if (toolbarMenu != null) { 512 toolbarMenu.removeAll(); 513 fillToolbarsMenu(toolbarMenu); 514 revalidateWindow(); 515 } 516 } 517 518 521 private void removeVisible (ToolbarConstraints tc) { 522 invisibleToolbars.put (tc, Integer.valueOf (tc.rowIndex())); 523 if (tc.destroy()) 524 checkToolbarRows(); 525 tc.setVisible (false); 526 527 } 529 530 533 private void addVisible (ToolbarConstraints tc) { 534 int rC = toolbarRows.size(); 535 int pos = invisibleToolbars.remove (tc).intValue(); 536 tc.setVisible (true); 537 for (int i = pos; i < pos + tc.getRowCount(); i++) { 538 getRow (i).addToolbar (tc, tc.getPosition()); 539 } 540 541 if (rC != toolbarRows.size()) 542 revalidateWindow(); 543 } 545 546 551 ToolbarRow getRow (int rI) { 552 ToolbarRow row; 553 int s = toolbarRows.size(); 554 if (rI < 0) { 555 row = new ToolbarRow (this); 556 addRow (row, 0); 557 } else if (rI >= s) { 558 row = new ToolbarRow (this); 559 addRow (row); 560 } else { 561 row = (ToolbarRow)toolbarRows.elementAt (rI); 562 } 563 return row; 564 } 565 566 569 ToolbarRow createLastRow () { 570 return getRow (toolbarRows.size()); 571 } 572 573 579 void reactivatePanel (boolean someBarRemoved, boolean writeAtAll) { 580 toolbarPanel().removeAll(); 581 prefWidth = 0; 582 583 Toolbar tbs[] = toolbarPool ().getToolbars(); 584 Toolbar tb; 585 ToolbarConstraints tc; 586 String name; 587 ToolbarRow lastRow = null; 588 589 for (int i = 0; i < tbs.length; i++) { 590 tb = tbs[i]; 591 name = tb.getName(); 592 tc = (ToolbarConstraints)allToolbars.get (name); 593 if (tc == null) { 594 if (lastRow == null) { 595 if( toolbarRows.isEmpty() ) 596 lastRow = createLastRow(); 597 else 598 lastRow = getRow( toolbarRows.size()-1 ); 599 } 600 tc = new ToolbarConstraints (this, name, null, Boolean.TRUE); 601 addToolbar (lastRow, tc); 602 } 603 toolbarPanel().add (tb, tc); 604 } 605 606 revalidateWindow(); 607 608 } 609 610 613 private void rebuildPanel () { 614 toolbarPanel().removeAll(); 615 prefWidth = 0; 616 617 Toolbar tbs[] = toolbarPool ().getToolbars(); 618 Toolbar tb; 619 ToolbarConstraints tc; 620 String name; 621 ToolbarRow newRow = null; 622 boolean smallToolbarIcons = (ToolbarPool.getDefault().getPreferredIconSize() == 16); 623 for (int i = 0; i < tbs.length; i++) { 624 tb = tbs[i]; 625 name = tb.getName(); 626 Component [] comps = tb.getComponents(); 627 for (int j = 0; j < comps.length; j++) { 628 if (comps[j] instanceof JComponent) { 629 if (smallToolbarIcons) { 630 ((JComponent) comps[j]).putClientProperty("PreferredIconSize",null); } else { 632 ((JComponent) comps[j]).putClientProperty("PreferredIconSize",Integer.valueOf(24)); } 634 } 635 } 636 tc = (ToolbarConstraints)allToolbars.get (name); 637 if (tc == null) { 638 if (newRow == null) { 639 newRow = createLastRow(); 640 } 641 tc = new ToolbarConstraints (this, name, null, Boolean.TRUE); 642 addToolbar (newRow, tc); 643 } 644 toolbarPanel().add (tb, tc); 645 } 646 revalidateWindow(); 647 } 648 649 652 boolean isImportantActivateComponent () { 653 Object [] names = allToolbars.keySet().toArray(); 654 Toolbar[] toolbars = toolbarPool ().getToolbars(); 655 656 657 if (names.length != toolbars.length) 658 return true; 659 660 661 if (! configName.equals (toolbarPool ().getConfiguration())) 662 return true; 663 664 return false; 665 } 666 667 669 void reflectChanges () { 670 try { 671 writeDocument(); 672 } catch (IOException e) { } 673 } 674 675 678 682 public Component activate () { 683 return activate (isImportantActivateComponent (), true); 684 } 685 686 687 691 private Component activate (boolean isImportant, boolean writeAtAll) { 692 toolbarPool().setToolbarsListener (toolbarListener); 693 694 boolean someBarRemoved = checkConfigurationOver(); 695 696 if (isImportant || someBarRemoved) { 697 toolbarLayout = new ToolbarLayout (this); 698 toolbarPanel().setLayout (toolbarLayout); 699 reactivatePanel (someBarRemoved, writeAtAll); 700 rebuildMenu(); 701 } 702 703 return toolbarPanel(); 704 } 705 706 709 public String getName () { 710 return configName; 711 } 712 713 public String getDisplayName () { 714 return configDisplayName; 715 } 716 717 723 public JPopupMenu getContextMenu () { 724 JPopupMenu menu = new JPopupMenuPlus(); 725 fillToolbarsMenu(menu); 726 return menu; 727 } 728 729 731 public JMenu getToolbarsMenu (JMenu menu) { 732 fillToolbarsMenu(menu); 733 toolbarMenu = menu; 734 return menu; 735 } 736 737 public static void resetToolbarIconSize() { 738 ToolbarPool.getDefault().setPreferredIconSize(24); 739 String name = ToolbarPool.getDefault().getConfiguration(); 741 ToolbarConfiguration tbConf = findConfiguration(name); 742 if (tbConf != null) { 743 tbConf.rebuildPanel(); 744 tbConf.rebuildMenu(); 745 } 746 } 747 748 749 private void fillToolbarsMenu (JComponent menu) { 750 lastConfigurationHash = Utilities.arrayHashCode(ToolbarPool.getDefault().getConfigurations()); 751 Iterator it = Arrays.asList (ToolbarPool.getDefault ().getToolbars ()).iterator (); 753 while (it.hasNext()) { 754 final Toolbar tb = (Toolbar)it.next(); 755 final String tbName = tb.getName(); 756 ToolbarConstraints tc = 757 (ToolbarConstraints)allToolbars.get (tb.getName()); 758 766 if (tc == null || tb == null) { 767 checkConfigurationOver(); 770 } 771 772 773 if (tc != null && tb != null) { 774 JCheckBoxMenuItem mi = new JCheckBoxMenuItem ( 776 tb.getDisplayName(), tc.isVisible() 777 ); 778 mi.putClientProperty("ToolbarName", tbName); mi.addActionListener (new ActionListener () { 780 public void actionPerformed (ActionEvent ae) { 781 ToolbarConstraints tc = (ToolbarConstraints)allToolbars.get (tbName ); 786 setToolbarVisible(tb, !tc.isVisible()); 787 } 788 }); 789 menu.add (mi); 790 } 791 } 792 menu.add (new JPopupMenu.Separator()); 793 794 boolean smallToolbarIcons = (ToolbarPool.getDefault().getPreferredIconSize() == 16); 796 797 JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem ( 798 getBundleString("PROP_smallToolbarIcons"), smallToolbarIcons 799 ); 800 cbmi.addActionListener (new ActionListener () { 801 public void actionPerformed (ActionEvent ev) { 802 if (ev.getSource() instanceof JCheckBoxMenuItem) { 803 JCheckBoxMenuItem cb = (JCheckBoxMenuItem) ev.getSource(); 804 boolean state = cb.getState(); 805 if (state) { 806 ToolbarPool.getDefault().setPreferredIconSize(16); 807 } else { 808 ToolbarPool.getDefault().setPreferredIconSize(24); 809 } 810 String name = ToolbarPool.getDefault().getConfiguration(); 813 ToolbarConfiguration tbConf = findConfiguration(name); 814 if (tbConf != null) { 815 tbConf.rebuildPanel(); 816 } 817 rebuildMenu(); 818 } 819 } 820 }); 821 menu.add (cbmi); 822 823 menu.add( new JPopupMenu.Separator() ); 824 825 JMenuItem menuItem = new JMenuItem( new ResetToolbarsAction() ); 826 menu.add( menuItem ); 827 828 menuItem = new JMenuItem(getBundleString( "CTL_CustomizeToolbars" ) ); 829 menuItem.addActionListener(new ActionListener () { 830 public void actionPerformed (ActionEvent event) { 831 ConfigureToolbarPanel.showConfigureDialog(); 832 } 833 }); 834 menu.add( menuItem ); 835 836 } 838 842 public void setToolbarVisible(Toolbar tb, boolean b) { 843 ToolbarConstraints tc = getToolbarConstraints(tb.getName()); 844 if (b) { 845 addVisible(tc); 846 } else { 847 removeVisible(tc); 848 } 849 if (toolbarMenu != null) { 850 Component[] elements = toolbarMenu.getMenuComponents(); 854 for (int i = 0; i < elements.length; i++) { 855 JComponent component = (JComponent)elements[i]; 856 String tcmenu = (String )component.getClientProperty("ToolbarName"); if (tcmenu != null && tcmenu.equals(tb.getName())) { 858 ((JCheckBoxMenuItem)component).setSelected(b); 859 break; 860 } 861 } 862 } 863 tb.setVisible(b); 864 reflectChanges(); 865 firePropertyChange(); 866 } 867 868 872 public boolean isToolbarVisible(Toolbar tb) { 873 ToolbarConstraints tc = getToolbarConstraints(tb.getName()); 874 return tc.isVisible(); 875 } 876 877 PropertyChangeSupport pcs; 878 879 public void addPropertyChangeListener(PropertyChangeListener l) { 880 if (pcs == null) { 881 pcs = new PropertyChangeSupport (this); 882 } 883 pcs.addPropertyChangeListener(l); 884 } 885 886 public void removePropertyChangeListener(PropertyChangeListener l) { 887 if (pcs != null) { 888 pcs.removePropertyChangeListener(l); 889 } 890 } 891 892 private void firePropertyChange() { 893 if (pcs != null) { 894 pcs.firePropertyChange("constraints", null, null); } 896 } 897 898 900 901 public void writeDocument () throws IOException { 902 writeDocument (configName); 903 } 904 905 908 private void writeDocument (final String cn) throws IOException { 909 ERR.fine("writeDocument: " + cn); WritableToolbarConfiguration wtc = new WritableToolbarConfiguration (toolbarRows, invisibleToolbars); 911 final StringBuffer sb = new StringBuffer ("<?xml version=\"1.0\"?>\n\n"); sb.append ("<!DOCTYPE ").append (TAG_CONFIG).append (" PUBLIC \""). append (TOOLBAR_DTD_PUBLIC_ID).append ("\" \"").append (TOOLBAR_DTD_WEB).append ("\">\n\n").append (wtc.toString()); 915 final FileObject tbFO = NbPlaces.getDefault().toolbars().getPrimaryFile(); 916 final FileSystem tbFS = tbFO.getFileSystem(); 917 918 Boolean prev = WRITE_IN_PROGRESS.get (); 919 try { 920 WRITE_IN_PROGRESS.set (Boolean.TRUE); 921 tbFS.runAtomicAction (new FileSystem.AtomicAction () { 922 public void run () throws IOException { 923 FileLock lock = null; 924 OutputStream os = null; 925 FileObject xmlFO = tbFO.getFileObject(cn, EXT_XML); 926 if (xmlFO == null) 927 xmlFO = tbFO.createData (cn, EXT_XML); 928 try { 929 lock = xmlFO.lock (); 930 os = xmlFO.getOutputStream (lock); 931 932 Writer writer = new OutputStreamWriter (os, "UTF-8"); writer.write(sb.toString()); 934 writer.close(); 935 } finally { 936 lastReload = System.currentTimeMillis (); 937 ERR.fine("Setting last reload: " + lastReload); 939 if (os != null) 940 os.close (); 941 if (lock != null) 942 lock.releaseLock (); 943 } 944 } 945 }); 946 } finally { 947 WRITE_IN_PROGRESS.set (prev); 948 } 949 ERR.fine("writeDocument finished"); } 951 952 953 private static final synchronized JPanel toolbarPanel () { 954 if (toolbarPanel == null) { 955 toolbarPanel = new JPanel(); 956 toolbarPanel.setLayout(new FlowLayout (FlowLayout.LEFT)); 957 } 958 return toolbarPanel; 959 } 960 961 963 public void propertyChange(PropertyChangeEvent ev) { 964 if (!XMLDataObject.PROP_DOCUMENT.equals(ev.getPropertyName ())) { 965 return; 967 } 968 if (Boolean.TRUE.equals (WRITE_IN_PROGRESS.get ())) { 969 return; 970 } 971 972 updateConfiguration((XMLDataObject)ev.getSource()); 973 } 974 975 978 void updateConfiguration(final XMLDataObject xmlDataObject) { 979 long mod = xmlDataObject.getPrimaryFile ().lastModified().getTime (); 980 ERR.fine("Checking modified: " + lastReload); 987 988 SwingUtilities.invokeLater(new Runnable () { 995 996 public void run() { 997 try { 998 initInstance(); 999 readConfig(xmlDataObject); 1000 checkConfigurationOver(); 1001 if (configName.equals(toolbarPool().getConfiguration())) { 1002 ERR.fine("Activating the configuration"); 1003 activate(true, false); 1007 } 1008 } 1009 catch (IOException ex) { 1010 Logger.getLogger(ToolbarConfiguration.class.getName()).log(Level.WARNING, null, ex); 1011 } 1012 } 1013 }); 1014 } 1015 1016 1018 int getRowVertLocation (ToolbarRow row) { 1019 int index = rowIndex(row); 1020 int vertLocation = index * ToolbarLayout.VGAP; 1021 Iterator iter = toolbarRows.iterator(); 1022 for (int i = 0; i < index; i++) { 1023 vertLocation += ((ToolbarRow)iter.next()).getPreferredHeight(); 1024 } 1025 return vertLocation; 1026 } 1027 1028 static class WritableToolbarConfiguration { 1030 1031 Vector<ToolbarRow.WritableToolbarRow> rows; 1032 1033 1037 public WritableToolbarConfiguration (Vector<ToolbarRow> rs, Map iv) { 1038 initRows (rs); 1039 initInvisible (iv); 1040 removeEmptyRows(); 1041 } 1042 1043 1046 void initRows (Vector<ToolbarRow> rs) { 1047 rows = new Vector<ToolbarRow.WritableToolbarRow>(); 1048 for (ToolbarRow r: rs) { 1049 rows.addElement (new ToolbarRow.WritableToolbarRow (r)); 1050 } 1051 } 1052 1053 1056 void initInvisible (Map iv) { 1057 Iterator it = iv.keySet().iterator(); 1058 ToolbarConstraints tc; 1059 int row; 1060 while (it.hasNext()) { 1061 tc = (ToolbarConstraints)it.next(); 1062 row = ((Integer )iv.get (tc)).intValue(); 1063 for (int i = row; i < row + tc.getRowCount(); i++) { 1064 getRow (i).addToolbar (tc); 1065 } 1066 } 1067 } 1068 1069 1070 void removeEmptyRows () { 1071 ToolbarRow.WritableToolbarRow row; 1072 for (int i = rows.size() - 1; i >= 0; i--) { 1073 row = (ToolbarRow.WritableToolbarRow)rows.elementAt (i); 1074 if (row.isEmpty()) 1075 rows.removeElement (row); 1076 } 1077 } 1078 1079 1083 ToolbarRow.WritableToolbarRow getRow (int r) { 1084 try { 1085 return rows.elementAt (r); 1086 } catch (ArrayIndexOutOfBoundsException e) { 1087 rows.addElement (new ToolbarRow.WritableToolbarRow ()); 1088 return getRow (r); 1089 } 1090 } 1091 1092 1093 public String toString () { 1094 StringBuffer sb = new StringBuffer (); 1095 1096 sb.append ("<").append (TAG_CONFIG).append (">\n"); Iterator it = rows.iterator(); 1098 while (it.hasNext()) { 1099 sb.append (it.next().toString()); 1100 } 1101 sb.append ("</").append (TAG_CONFIG).append (">\n"); 1103 return sb.toString(); 1104 } 1105 } } | Popular Tags |