KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > VersioningOutputTopComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
31 import java.util.*;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34
35 /**
36  * Top component of the Versioning Output view.
37  *
38  * @author Maros Sandor
39  */

40 public class VersioningOutputTopComponent extends TopComponent implements Externalizable, PropertyChangeListener JavaDoc {
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")); // NOI18N
52
setLayout(new BorderLayout JavaDoc());
53         getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_Title")); // NOI18N
54
updateName();
55         tabbedPane.addPropertyChangeListener(this);
56     }
57
58     public void propertyChange(PropertyChangeEvent JavaDoc 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"); // NOI18N
72
if (VersioningOutputTopComponent.instance == null) {
73                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException JavaDoc(
74                     "Can not find Versioning Output component")); // NOI18N
75
VersioningOutputTopComponent.instance = new VersioningOutputTopComponent();
76             }
77         }
78     
79         return VersioningOutputTopComponent.instance;
80     }
81
82     protected String JavaDoc preferredID() {
83         return "versioning_output"; // NOI18N
84
}
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 JavaDoc {
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 JavaDoc readResolve() {
103         return getInstance();
104     }
105
106     private void updateName() {
107         if (components.size() == 0) {
108             setName(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_Title")); // NOI18N
109
} else if (components.size() == 1) {
110             JComponent c = components.iterator().next();
111             setName(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_TitleOne", c.getName())); // NOI18N
112
} else {
113             setName(NbBundle.getMessage(VersioningOutputTopComponent.class, "CTL_VersioningOutput_Title")); // NOI18N
114
}
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 JavaDoc 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