Renamed constants, local variables and member variables using "hg" to "git".
[nbgit.git] / src / org / netbeans / modules / git / ui / status / GitVersioningTopComponent.java
blobaee512892541159930df7b680ab7afcd01ce9b52
1 /*
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]"
24 * Contributor(s):
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;
46 import java.io.File;
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;
58 /**
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);
84 add(syncPanel);
87 @Override
88 public HelpCtx getHelpCtx() {
89 return new HelpCtx(getClass());
92 @Override
93 protected void componentActivated() {
94 updateTitle();
95 syncPanel.focus();
98 @Override
99 protected void componentOpened() {
100 super.componentOpened();
101 refreshContent();
104 @Override
105 protected void componentClosed() {
106 super.componentClosed();
109 private void refreshContent() {
110 if (syncPanel == null) return; // the component is not showing => nothing to refresh
111 updateTitle();
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;
123 updateTitle();
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;
134 updateTitle();
137 public void contentRefreshed() {
138 lastUpdateTimestamp = System.currentTimeMillis();
139 updateTitle();
142 private void updateTitle() {
143 SwingUtilities.invokeLater(new Runnable (){
144 public void run() {
146 if (contentTitle == null) {
147 setName(NbBundle.getMessage(GitVersioningTopComponent.class, "CTL_Versioning_TopComponent_Title")); // NOI18N
148 } else {
149 File baseFile = GitUtils.getRootFile(context);
150 String name = "";
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
159 } else {
160 setName(NbBundle.getMessage(GitVersioningTopComponent.class,
161 "CTL_Versioning_TopComponent_Title_ContentBranch",
162 contentTitle, name.equals(contentTitle)? "": "[" + name + "] ", branchTitle)); // NOI18N
169 String getContentTitle() {
170 return contentTitle;
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();
182 return instance;
186 * Obtain the GitVersioningTopComponent instance. Never call {@link #getDefault} directly!
188 public static synchronized GitVersioningTopComponent findInstance() {
189 TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
190 if (win == null) {
191 Git.LOG.log(Level.FINE, "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system."); // NOI18N
192 return getDefault();
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
200 return getDefault();
203 @Override
204 public int getPersistenceType() {
205 return TopComponent.PERSISTENCE_ALWAYS;
208 /** replaces this in object stream */
209 @Override
210 public Object writeReplace() {
211 return new ResolvableHelper();
214 @Override
215 protected String preferredID() {
216 return PREFERRED_ID;
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();
241 if (ctx == null) {
242 setName(NbBundle.getMessage(GitVersioningTopComponent.class, "MSG_Preparing")); // NOI18N
243 setEnabled(false);
244 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
245 } else {
246 setEnabled(true);
247 setCursor(Cursor.getDefaultCursor());
248 context = ctx;
249 syncPanel.setContext(ctx);
250 setBranchTitle(NbBundle.getMessage(GitVersioningTopComponent.class, "CTL_VersioningView_UnnamedBranchTitle")); // NOI18N
251 refreshContent();
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;