KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > status > SvnVersioningTopComponent


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.subversion.ui.status;
21
22 import javax.swing.SwingUtilities JavaDoc;
23 import org.openide.util.*;
24 import org.openide.windows.TopComponent;
25 import org.openide.windows.WindowManager;
26 import org.openide.util.NbBundle;
27 import org.openide.util.HelpCtx;
28 import org.openide.ErrorManager;
29 import org.netbeans.modules.subversion.util.Context;
30
31 import java.awt.BorderLayout JavaDoc;
32 import java.awt.Cursor JavaDoc;
33 import java.io.*;
34
35 /**
36  * Top component of the Versioning view.
37  *
38  * @author Maros Sandor
39  */

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

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

117     void setBranchTitle(String JavaDoc branchTitle) {
118         this.branchTitle = branchTitle;
119         updateTitle();
120     }
121     
122     public void contentRefreshed() {
123         lastUpdateTimestamp = System.currentTimeMillis();
124         updateTitle();
125     }
126     
127     private void updateTitle() {
128         final String JavaDoc age = computeAge(System.currentTimeMillis() - lastUpdateTimestamp);
129         SwingUtilities.invokeLater(new Runnable JavaDoc (){
130             public void run() {
131                 if (contentTitle == null) {
132                     setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title")); // NOI18N
133
} else {
134                     if (branchTitle == null) {
135                         setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_MultiTitle", contentTitle, age)); // NOI18N
136
} else {
137                         setName(NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title_ContentBranch", contentTitle, branchTitle, age)); // NOI18N
138
}
139                 }
140             }
141         });
142     }
143
144     String JavaDoc getContentTitle() {
145         return contentTitle;
146     }
147
148     private String JavaDoc computeAge(long l) {
149         if (lastUpdateTimestamp == 0) {
150             return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeUnknown"); // NOI18N
151
} else if (l < 1000) { // 1000 equals 1 second
152
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeCurrent"); // NOI18N
153
} else if (l < 2000) { // age between 1 and 2 seconds
154
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneSecond"); // NOI18N
155
} else if (l < 60000) { // 60000 equals 1 minute
156
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeSeconds", Long.toString(l / 1000)); // NOI18N
157
} else if (l < 120000) { // age between 1 and 2 minutes
158
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneMinute"); // NOI18N
159
} else if (l < 3600000) { // 3600000 equals 1 hour
160
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeMinutes", Long.toString(l / 60000)); // NOI18N
161
} else if (l < 7200000) { // age between 1 and 2 hours
162
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneHour"); // NOI18N
163
} else if (l < 86400000) { // 86400000 equals 1 day
164
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeHours", Long.toString(l / 3600000)); // NOI18N
165
} else if (l < 172800000) { // age between 1 and 2 days
166
return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeOneDay"); // NOI18N
167
} else {
168             return NbBundle.getMessage(SvnVersioningTopComponent.class, "CTL_Versioning_TopComponent_AgeDays", Long.toString(l / 86400000)); // NOI18N
169
}
170     }
171
172     public static synchronized SvnVersioningTopComponent getInstance() {
173         if (instance == null) {
174             instance = (SvnVersioningTopComponent) WindowManager.getDefault().findTopComponent("svnversioning"); // NOI18N
175
if (instance == null) {
176                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException JavaDoc(
177                     "Can not find Versioning component")); // NOI18N
178
instance = new SvnVersioningTopComponent();
179             }
180         }
181     
182         return instance;
183     }
184
185     public Object JavaDoc readResolve() {
186         return getInstance();
187     }
188
189     /**
190      * Programmatically invokes the Refresh action.
191      */

192     public RequestProcessor.Task performRefreshAction() {
193         return syncPanel.performRefreshAction();
194     }
195
196     /**
197      * Sets files/folders the user wants to synchronize. They are typically activated (selected) nodes.
198      *
199      * @param ctx new context of the Versioning view
200      */

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