2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org
.netbeans
.modules
.git
.ui
.status
;
44 import java
.awt
.BorderLayout
;
45 import java
.awt
.Cursor
;
47 import java
.io
.Serializable
;
48 import java
.util
.logging
.Level
;
49 import javax
.swing
.SwingUtilities
;
50 import org
.netbeans
.modules
.git
.Git
;
51 import org
.netbeans
.modules
.git
.util
.GitUtils
;
52 import org
.netbeans
.modules
.versioning
.spi
.VCSContext
;
53 import org
.openide
.util
.HelpCtx
;
54 import org
.openide
.util
.NbBundle
;
55 import org
.openide
.windows
.TopComponent
;
56 import org
.openide
.windows
.WindowManager
;
59 * Top component of the Versioning view.
61 * @author Maros Sandor
63 public class GitVersioningTopComponent
extends TopComponent
{
65 private static final long serialVersionUID
= 1L;
67 private VersioningPanel syncPanel
;
68 private VCSContext context
;
69 private String contentTitle
;
70 private String branchTitle
;
71 private long lastUpdateTimestamp
;
72 private static final String PREFERRED_ID
= "gitversioning"; // NOI18N
74 private static GitVersioningTopComponent instance
;
76 public GitVersioningTopComponent() {
77 putClientProperty("SlidingName", NbBundle
.getMessage(GitVersioningTopComponent
.class, "CTL_Versioning_TopComponent_Title")); //NOI18N
79 setName(NbBundle
.getMessage(GitVersioningTopComponent
.class, "CTL_Versioning_TopComponent_Title")); // NOI18N
80 setIcon(org
.openide
.util
.Utilities
.loadImage("org/netbeans/modules/git/resources/icons/versioning-view.png")); // NOI18N
81 setLayout(new BorderLayout());
82 getAccessibleContext().setAccessibleDescription(NbBundle
.getMessage(GitVersioningTopComponent
.class, "CTL_Versioning_TopComponent_Title")); // NOI18N
83 syncPanel
= new VersioningPanel(this);
88 public HelpCtx
getHelpCtx() {
89 return new HelpCtx(getClass());
93 protected void componentActivated() {
99 protected void componentOpened() {
100 super.componentOpened();
105 protected void componentClosed() {
106 super.componentClosed();
109 private void refreshContent() {
110 if (syncPanel
== null) return; // the component is not showing => nothing to refresh
112 syncPanel
.setContext(context
);
116 * Sets the 'content' portion of Versioning component title.
117 * Title pattern: Versioning[ - contentTitle[ - branchTitle]] (10 minutes ago)
119 * @param contentTitle a new content title, e.g. "2 projects" // NOI18N
121 public void setContentTitle(String contentTitle
) {
122 this.contentTitle
= contentTitle
;
127 * Sets the 'branch' portion of Versioning component title.
128 * Title pattern: Versioning[ - contentTitle[ - branchTitle]] (10 minutes ago)
130 * @param branchTitle a new content title, e.g. "release40" branch // NOI18N
132 void setBranchTitle(String branchTitle
) {
133 this.branchTitle
= branchTitle
;
137 public void contentRefreshed() {
138 lastUpdateTimestamp
= System
.currentTimeMillis();
142 private void updateTitle() {
143 SwingUtilities
.invokeLater(new Runnable (){
146 if (contentTitle
== null) {
147 setName(NbBundle
.getMessage(GitVersioningTopComponent
.class, "CTL_Versioning_TopComponent_Title")); // NOI18N
149 File baseFile
= GitUtils
.getRootFile(context
);
151 if(baseFile
!= null){
152 name
= baseFile
.getName();
155 if (branchTitle
== null) {
156 setName(NbBundle
.getMessage(GitVersioningTopComponent
.class,
157 "CTL_Versioning_TopComponent_MultiTitle",
158 contentTitle
, name
.equals(contentTitle
)?
"": "[" + name
+ "]")); // NOI18N
160 setName(NbBundle
.getMessage(GitVersioningTopComponent
.class,
161 "CTL_Versioning_TopComponent_Title_ContentBranch",
162 contentTitle
, name
.equals(contentTitle
)?
"": "[" + name
+ "] ", branchTitle
)); // NOI18N
169 String
getContentTitle() {
174 * Gets default instance. Do not use directly: reserved for *.settings files only,
175 * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
176 * To obtain the singleton instance, use {@link findInstance}.
178 public static synchronized GitVersioningTopComponent
getDefault() {
179 if (instance
== null) {
180 instance
= new GitVersioningTopComponent();
186 * Obtain the GitVersioningTopComponent instance. Never call {@link #getDefault} directly!
188 public static synchronized GitVersioningTopComponent
findInstance() {
189 TopComponent win
= WindowManager
.getDefault().findTopComponent(PREFERRED_ID
);
191 Git
.LOG
.log(Level
.FINE
, "Cannot find " + PREFERRED_ID
+ " component. It will not be located properly in the window system."); // NOI18N
194 if (win
instanceof GitVersioningTopComponent
) {
195 return (GitVersioningTopComponent
)win
;
197 Git
.LOG
.log(Level
.FINE
,
198 "There seem to be multiple components with the '" + PREFERRED_ID
+ // NOI18N
199 "' ID. That is a potential source of errors and unexpected behavior."); // NOI18N
204 public int getPersistenceType() {
205 return TopComponent
.PERSISTENCE_ALWAYS
;
208 /** replaces this in object stream */
210 public Object
writeReplace() {
211 return new ResolvableHelper();
215 protected String
preferredID() {
219 final static class ResolvableHelper
implements Serializable
{
220 private static final long serialVersionUID
= 1L;
221 public Object
readResolve() {
222 return GitVersioningTopComponent
.getDefault();
227 * Programmatically invokes the Refresh action.
229 public void performRefreshAction() {
230 syncPanel
.performRefreshAction();
234 * Sets files/folders the user wants to synchronize. They are typically activated (selected) nodes.
236 * @param ctx new context of the Versioning view
238 public void setContext(VCSContext ctx
) {
239 syncPanel
.cancelRefresh();
242 setName(NbBundle
.getMessage(GitVersioningTopComponent
.class, "MSG_Preparing")); // NOI18N
244 setCursor(Cursor
.getPredefinedCursor(Cursor
.WAIT_CURSOR
));
247 setCursor(Cursor
.getDefaultCursor());
249 syncPanel
.setContext(ctx
);
250 setBranchTitle(NbBundle
.getMessage(GitVersioningTopComponent
.class, "CTL_VersioningView_UnnamedBranchTitle")); // NOI18N
253 setToolTipText(getContextFilesList(ctx
, NbBundle
.getMessage(GitVersioningTopComponent
.class, "CTL_Versioning_TopComponent_Title"))); // NOI18N
256 private String
getContextFilesList(VCSContext ctx
, String def
) {
257 if (ctx
== null || ctx
.getRootFiles().size() == 0) return def
;
258 StringBuffer sb
= new StringBuffer(200);
259 sb
.append("<html>"); // NOI18N
260 for (File file
: ctx
.getRootFiles()) {
261 sb
.append(file
.getAbsolutePath());
262 sb
.append("<br>"); // NOI18N
264 sb
.delete(sb
.length() - 4, Integer
.MAX_VALUE
);
265 return sb
.toString();
268 /** Tests whether it shows some content. */
269 public boolean hasContext() {
270 return context
!= null && context
.getRootFiles().size() > 0;