1 19 package org.netbeans.modules.versioning; 20 21 import org.openide.windows.TopComponent; 22 import org.openide.windows.WindowManager; 23 import org.openide.util.NbBundle; 24 import org.openide.util.HelpCtx; 25 import org.openide.ErrorManager; 26 import org.openide.awt.TabbedPaneFactory; 27 28 import javax.swing.*; 29 import java.io.*; 30 import java.awt.BorderLayout ; 31 import java.util.*; 32 import java.beans.PropertyChangeListener ; 33 import java.beans.PropertyChangeEvent ; 34 35 40 public class VersioningOutputTopComponent extends TopComponent implements Externalizable, PropertyChangeListener { 41 42 private static final long serialVersionUID = 1L; 43 44 private static VersioningOutputTopComponent instance; 45 46 private Set<JComponent> components = new HashSet<JComponent>(1); 47 48 private JTabbedPane tabbedPane = TabbedPaneFactory.createCloseButtonTabbedPane(); 49 50 public VersioningOutputTopComponent() { 51 setIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/versioning/resources/window-versioning.png")); setLayout(new BorderLayout ()); 53 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_Title")); updateName(); 55 tabbedPane.addPropertyChangeListener(this); 56 } 57 58 public void propertyChange(PropertyChangeEvent evt) { 59 if (TabbedPaneFactory.PROP_CLOSE.equals(evt.getPropertyName())) { 60 JComponent c = (JComponent) evt.getNewValue(); 61 removeComponent(c); 62 } 63 } 64 65 public HelpCtx getHelpCtx() { 66 return new HelpCtx(getClass()); 67 } 68 69 public static synchronized VersioningOutputTopComponent getInstance() { 70 if (VersioningOutputTopComponent.instance == null) { 71 VersioningOutputTopComponent.instance = (VersioningOutputTopComponent) WindowManager.getDefault().findTopComponent("versioning_output"); if (VersioningOutputTopComponent.instance == null) { 73 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ( 74 "Can not find Versioning Output component")); VersioningOutputTopComponent.instance = new VersioningOutputTopComponent(); 76 } 77 } 78 79 return VersioningOutputTopComponent.instance; 80 } 81 82 protected String preferredID() { 83 return "versioning_output"; } 85 86 public int getPersistenceType() { 87 return TopComponent.PERSISTENCE_ALWAYS; 88 } 89 90 public void writeExternal(ObjectOutput out) throws IOException { 91 super.writeExternal(out); 92 } 93 94 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 95 super.readExternal(in); 96 components = new HashSet<JComponent>(1); 97 tabbedPane = TabbedPaneFactory.createCloseButtonTabbedPane(); 98 tabbedPane.addPropertyChangeListener(this); 99 updateName(); 100 } 101 102 public Object readResolve() { 103 return getInstance(); 104 } 105 106 private void updateName() { 107 if (components.size() == 0) { 108 setName(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_Title")); } else if (components.size() == 1) { 110 JComponent c = components.iterator().next(); 111 setName(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_TitleOne", c.getName())); } else { 113 setName(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_Title")); } 115 } 116 117 private void removeComponent(JComponent c) { 118 assert SwingUtilities.isEventDispatchThread(); 119 assert components.remove(c); 120 if (components.size() == 0) { 121 removeAll(); 122 } else if (components.size() == 1) { 123 tabbedPane.removeAll(); 124 removeAll(); 125 add(components.iterator().next()); 126 } else { 127 tabbedPane.remove(c); 128 } 129 updateName(); 130 revalidate(); 131 } 132 133 void addComponent(String key, JComponent c) { 134 assert SwingUtilities.isEventDispatchThread(); 135 for (JComponent existing : components) { 136 if (existing.getClientProperty(VersioningOutputTopComponent.class).equals(key)) { 137 removeComponent(existing); 138 break; 139 } 140 } 141 if (!components.add(c)) return; 142 c.putClientProperty(VersioningOutputTopComponent.class, key); 143 if (components.size() == 1) { 144 assert getComponentCount() == 0; 145 add(c); 146 } else if (components.size() == 2) { 147 assert getComponentCount() == 1; 148 removeAll(); 149 Iterator<JComponent> it = components.iterator(); 150 tabbedPane.add(it.next()); 151 tabbedPane.add(it.next()); 152 tabbedPane.setSelectedComponent(c); 153 add(tabbedPane); 154 } else { 155 assert getComponentCount() == 1; 156 tabbedPane.add(c); 157 tabbedPane.setSelectedComponent(c); 158 } 159 updateName(); 160 revalidate(); 161 } 162 } 163 | Popular Tags |