KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > syncview > CvsSynchronizeTopComponent


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.syncview;
21
22 import org.openide.windows.TopComponent;
23 import org.openide.windows.WindowManager;
24 import org.openide.util.NbBundle;
25 import org.openide.util.HelpCtx;
26 import org.openide.ErrorManager;
27 import org.netbeans.modules.versioning.system.cvss.util.Context;
28
29 import java.awt.*;
30 import java.io.*;
31
32 /**
33  * Top component of the Versioning view.
34  *
35  * @author Maros Sandor
36  */

37 public class CvsSynchronizeTopComponent extends TopComponent implements Externalizable {
38
39     private static final long serialVersionUID = 1L;
40     
41     private SynchronizePanel syncPanel;
42     private Context context;
43     private String JavaDoc contentTitle;
44     private String JavaDoc branchTitle;
45     private long lastUpdateTimestamp;
46     
47     private static CvsSynchronizeTopComponent instance;
48
49     public CvsSynchronizeTopComponent() {
50         setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title"));
51         setIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/versioning-view.png", true)); // NOI18N
52
setLayout(new BorderLayout());
53         getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "ACSD_Synchronize_TopComponent"));
54         syncPanel = new SynchronizePanel(this);
55         add(syncPanel);
56         setFocusable(true);
57     }
58
59     public HelpCtx getHelpCtx() {
60         return new HelpCtx(getClass());
61     }
62
63     protected void componentActivated() {
64         updateTitle();
65         syncPanel.focus();
66     }
67
68     public void writeExternal(ObjectOutput out) throws IOException {
69         super.writeExternal(out);
70         out.writeObject(context);
71         out.writeObject(contentTitle);
72         out.writeLong(lastUpdateTimestamp);
73     }
74
75     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
76         super.readExternal(in);
77         context = (Context) in.readObject();
78         contentTitle = (String JavaDoc) in.readObject();
79         lastUpdateTimestamp = in.readLong();
80         syncPanel.deserialize();
81     }
82
83     protected void componentOpened() {
84         super.componentOpened();
85         refreshContent();
86     }
87
88     protected void componentClosed() {
89         super.componentClosed();
90     }
91
92     private void refreshContent() {
93         if (syncPanel == null) return; // the component is not showing => nothing to refresh
94
updateTitle();
95         syncPanel.setContext(context);
96     }
97
98     /**
99      * Sets the 'content' portion of Versioning component title.
100      * Title pattern: Versioning[ - contentTitle[ - branchTitle]] (10 minutes ago)
101      *
102      * @param contentTitle a new content title, e.g. "2 projects"
103      */

104     public void setContentTitle(String JavaDoc contentTitle) {
105         this.contentTitle = contentTitle;
106         updateTitle();
107     }
108
109     /**
110      * Sets the 'branch' portion of Versioning component title.
111      * Title pattern: Versioning[ - contentTitle[ - branchTitle]] (10 minutes ago)
112      *
113      * @param branchTitle a new content title, e.g. "release40" branch
114      */

115     void setBranchTitle(String JavaDoc branchTitle) {
116         this.branchTitle = branchTitle;
117         updateTitle();
118     }
119     
120     public void contentRefreshed() {
121         lastUpdateTimestamp = System.currentTimeMillis();
122         updateTitle();
123     }
124     
125     private void updateTitle() {
126         String JavaDoc age = computeAge(System.currentTimeMillis() - lastUpdateTimestamp);
127         if (contentTitle == null) {
128             setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title")); //NOI18N
129
} else {
130             if (branchTitle == null) {
131                 setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_MultiTitle", contentTitle, age));
132             } else {
133                 setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title_ContentBranch", contentTitle, branchTitle, age));
134             }
135         }
136     }
137
138     String JavaDoc getContentTitle() {
139         return contentTitle;
140     }
141
142     private String JavaDoc computeAge(long l) {
143         if (lastUpdateTimestamp == 0) {
144             return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeUnknown");
145         } else if (l < 1000) { // 1000 equals 1 second
146
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeCurrent");
147         } else if (l < 2000) { // age between 1 and 2 seconds
148
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneSecond");
149         } else if (l < 60000) { // 60000 equals 1 minute
150
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeSeconds", Long.toString(l / 1000));
151         } else if (l < 120000) { // age between 1 and 2 minutes
152
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneMinute");
153         } else if (l < 3600000) { // 3600000 equals 1 hour
154
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeMinutes", Long.toString(l / 60000));
155         } else if (l < 7200000) { // age between 1 and 2 hours
156
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneHour");
157         } else if (l < 86400000) { // 86400000 equals 1 day
158
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeHours", Long.toString(l / 3600000));
159         } else if (l < 172800000) { // age between 1 and 2 days
160
return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeOneDay");
161         } else {
162             return NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_AgeDays", Long.toString(l / 86400000));
163         }
164     }
165
166     public static synchronized CvsSynchronizeTopComponent getInstance() {
167         if (instance == null) {
168             instance = (CvsSynchronizeTopComponent) WindowManager.getDefault().findTopComponent("synchronize"); // NOI18N
169
if (instance == null) {
170                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException JavaDoc(
171                     "Can not find Versioning component")); // NOI18N
172
instance = new CvsSynchronizeTopComponent();
173             }
174         }
175     
176         return instance;
177     }
178
179     public Object JavaDoc readResolve() {
180         return getInstance();
181     }
182
183     /**
184      * Programmatically invokes the Refresh action.
185      */

186     public void performRefreshAction() {
187         syncPanel.performRefreshAction();
188     }
189
190     public static final class ReadResolver implements java.io.Serializable JavaDoc {
191         public Object JavaDoc readResolve() {
192             if(instance == null) {
193                 instance = new CvsSynchronizeTopComponent();
194             }
195             return instance;
196         }
197     }
198
199     /**
200      * Sets files/folders the user wants to synchronize.
201      * They are typically activated (selected) nodes.
202      * It cancels refresh task serving previous context.
203      *
204      * @param ctx new context of the Versioning view.
205      * <code>null</code> for preparation phase then it must
206      * be followed by real context on preparation phase termination.
207      */

208     public void setContext(Context ctx) {
209         syncPanel.cancelRefresh();
210         if (ctx == null) {
211             setName(NbBundle.getMessage(CvsSynchronizeTopComponent.class, "BK1001"));
212             setEnabled(false);
213             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
214         } else {
215             setEnabled(true);
216             setCursor(Cursor.getDefaultCursor());
217             context = ctx;
218             setBranchTitle(null);
219             refreshContent();
220         }
221         setToolTipText(getContextFilesList(ctx, NbBundle.getMessage(CvsSynchronizeTopComponent.class, "CTL_Synchronize_TopComponent_Title")));
222     }
223
224     private String JavaDoc getContextFilesList(Context ctx, String JavaDoc def) {
225         if (ctx == null || ctx.getFiles().length == 0) return def;
226         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(200);
227         sb.append("<html>"); // NOI18N
228
for (File file : ctx.getFiles()) {
229             sb.append(file.getAbsolutePath());
230             sb.append("<br>"); // NOI18N
231
}
232         sb.delete(sb.length() - 4, Integer.MAX_VALUE);
233         return sb.toString();
234     }
235     
236     /** Tests whether it shows some content. */
237     public boolean hasContext() {
238         return context != null && context.getFiles().length > 0;
239     }
240
241     protected String JavaDoc preferredID() {
242         return "synchronize"; // NOI18N
243
}
244
245     public int getPersistenceType() {
246         return TopComponent.PERSISTENCE_ALWAYS;
247     }
248 }
249
Popular Tags