Cleanup code whitespace
[nbgit.git] / src / org / nbgit / ui / properties / GitProperties.java
blob8308ea3020e1d7969bad6f9c664046c6c1226212
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.nbgit.ui.properties;
44 import java.awt.Font;
45 import java.io.File;
46 import java.io.IOException;
47 import java.util.Enumeration;
48 import java.util.Properties;
49 import javax.swing.ListSelectionModel;
50 import javax.swing.event.ListSelectionEvent;
51 import javax.swing.event.ListSelectionListener;
52 import org.nbgit.Git;
53 import org.nbgit.GitModuleConfig;
54 import org.nbgit.GitProgressSupport;
55 import org.openide.util.Exceptions;
56 import org.openide.util.RequestProcessor;
57 import org.spearce.jgit.lib.Repository;
58 import org.spearce.jgit.lib.RepositoryConfig;
60 /**
62 * @author Padraig O'Briain
64 public class GitProperties implements ListSelectionListener {
66 public static final String GITPROPNAME_USER_EMAIL = "email"; // NOI18N
67 public static final String GITPROPNAME_USER_NAME = "name"; // NOI18N
68 public static final String GITPROPNAME_DEFAULT_PULL = "default-pull"; // NOI18N
69 private PropertiesPanel panel;
70 private File root;
71 private PropertiesTable propTable;
72 private GitProgressSupport support;
73 private File loadedValueFile;
74 private Font fontTextArea;
76 /** Creates a new instance of GitProperties */
77 public GitProperties(PropertiesPanel panel, PropertiesTable propTable, File root)
79 this.panel = panel;
80 this.propTable = propTable;
81 this.root = root;
82 propTable.getTable().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
83 propTable.getTable().getSelectionModel().addListSelectionListener(this);
85 refreshProperties();
88 public PropertiesPanel getPropertiesPanel()
90 return panel;
93 public void setPropertiesPanel(PropertiesPanel panel)
95 this.panel = panel;
98 public File getRoot()
100 return root;
103 public void setRoot(File root)
105 this.root = root;
108 protected String getPropertyValue()
110 return panel.txtAreaValue.getText();
113 protected void refreshProperties()
115 RequestProcessor rp = Git.getInstance().getRequestProcessor(root.getAbsolutePath());
116 try {
117 support = new GitProgressSupport() {
119 protected void perform()
121 Properties props = GitModuleConfig.getDefault().getProperties(root);
122 GitPropertiesNode[] gitProps = new GitPropertiesNode[props.size()];
123 int i = 0;
125 for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
126 String name = (String) e.nextElement();
127 String tmp = props.getProperty(name);
128 String value = tmp != null ? tmp : ""; // NOI18N
129 gitProps[i] = new GitPropertiesNode(name, value);
130 i++;
132 propTable.setNodes(gitProps);
135 support.start(rp, root.getAbsolutePath(), org.openide.util.NbBundle.getMessage(GitProperties.class, "LBL_Properties_Progress")); // NOI18N
136 } finally {
137 support = null;
141 public void setProperties()
143 RequestProcessor rp = Git.getInstance().getRequestProcessor(root.getAbsolutePath());
144 try {
145 support = new GitProgressSupport() {
147 protected void perform()
149 Repository repo = Git.getInstance().getRepository(root);
150 if (repo == null) {
151 return;
153 RepositoryConfig config = repo.getConfig();
154 boolean save = false;
156 GitModuleConfig.getDefault().clearProperties(root, "paths"); // NOI18N
157 GitModuleConfig.getDefault().removeProperty(root, "user", GITPROPNAME_USER_EMAIL); // NOI18N
158 GitModuleConfig.getDefault().removeProperty(root, "user", GITPROPNAME_USER_NAME); // NOI18N
160 GitPropertiesNode[] gitPropertiesNodes = propTable.getNodes();
161 for (int i = 0; i < gitPropertiesNodes.length; i++) {
162 String name = gitPropertiesNodes[i].getName();
163 String value = gitPropertiesNodes[i].getValue().trim();
164 if (value.length() == 0) {
165 continue;
167 if (name.equals("user.name")) {
168 config.setString("user", null, "name", value);
169 save = true;
172 if (name.equals("user.email")) {
173 config.setString("user", null, "email", value);
174 save = true;
178 //GitRepositoryContextCache.resetPullDefault();
179 //GitRepositoryContextCache.resetPushDefault();
180 try {
181 config.save();
182 } catch (IOException ex) {
183 Exceptions.printStackTrace(ex);
187 support.start(rp, root.getAbsolutePath(), org.openide.util.NbBundle.getMessage(GitProperties.class, "LBL_Properties_Progress")); // NOI18N
188 } finally {
189 support = null;
192 private int lastIndex = -1;
194 public void updateLastSelection()
196 GitPropertiesNode[] gitPropertiesNodes = propTable.getNodes();
197 if (lastIndex >= 0) {
198 gitPropertiesNodes[lastIndex].setValue(getPropertyValue());
202 public void valueChanged(ListSelectionEvent e)
204 int index = propTable.getTable().getSelectedRow();
205 if (index < 0) {
206 lastIndex = -1;
207 return;
209 GitPropertiesNode[] gitPropertiesNodes = propTable.getNodes();
210 if (lastIndex >= 0) {
211 gitPropertiesNodes[lastIndex].setValue(getPropertyValue());
213 panel.txtAreaValue.setText(gitPropertiesNodes[index].getValue());
214 lastIndex = index;