Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / options / GitExtProperties.java
bloba34afe68f8fc12b4e15516c5b356bb92669c1298
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.options;
44 import java.awt.EventQueue;
45 import java.awt.Font;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.MouseAdapter;
49 import java.awt.event.MouseEvent;
50 import java.io.File;
51 import java.util.ArrayList;
52 import java.util.List;
53 import java.util.Properties;
54 import java.util.Vector;
55 import javax.swing.ComboBoxModel;
56 import javax.swing.DefaultComboBoxModel;
57 import javax.swing.JTextField;
58 import javax.swing.event.DocumentEvent;
59 import javax.swing.event.DocumentListener;
60 import javax.swing.text.Document;
61 import org.netbeans.modules.git.Git;
62 import org.netbeans.modules.git.GitModuleConfig;
63 import org.netbeans.modules.git.GitProgressSupport;
64 import org.netbeans.modules.git.ui.properties.GitPropertiesNode;
65 import org.netbeans.modules.git.ui.properties.PropertiesPanel;
66 import org.openide.util.RequestProcessor;
68 public class GitExtProperties implements ActionListener, DocumentListener {
70 private PropertiesPanel panel;
71 private File root;
72 private PropertiesTable propTable;
73 private GitProgressSupport support;
74 private File loadedValueFile;
75 private Font fontTextArea;
77 /** Creates a new instance of HgExtProperties */
78 public GitExtProperties(PropertiesPanel panel, PropertiesTable propTable, File root) {
79 this.panel = panel;
80 this.propTable = propTable;
81 this.root = root;
82 panel.getTxtAreaValue().getDocument().addDocumentListener(this);
83 ((JTextField) panel.getComboName().getEditor().getEditorComponent()).getDocument().addDocumentListener(this);
84 propTable.getTable().addMouseListener(new TableMouseListener());
85 panel.getBtnAdd().addActionListener(this);
86 panel.getBtnRemove().addActionListener(this);
87 panel.getComboName().setEditable(true);
88 panel.getBtnAdd().setEnabled(false);
89 initPropertyNameCbx();
90 refreshProperties();
93 public PropertiesPanel getPropertiesPanel() {
94 return panel;
97 public void setPropertiesPanel(PropertiesPanel panel) {
98 this.panel = panel;
101 public File getRoot() {
102 return root;
105 public void setRoot(File root) {
106 this.root = root;
109 public void actionPerformed(ActionEvent event) {
110 Object source = event.getSource();
112 if (source.equals(panel.getBtnAdd())) {
113 addProperty();
116 if (source.equals(panel.getBtnRemove())) {
117 removeProperties();
122 protected void initPropertyNameCbx() {
123 List<String> lstName = new ArrayList<String>(8);
125 ComboBoxModel comboModel = new DefaultComboBoxModel(new Vector<String>(lstName));
126 panel.getComboName().setModel(comboModel);
127 panel.getComboName().getEditor().setItem(""); // NOI18N
130 protected String getPropertyValue() {
131 return panel.getTxtAreaValue().getText();
134 protected String getPropertyName() {
135 Object selectedItem = panel.getComboName().getSelectedObjects()[0];
136 if (selectedItem != null) {
137 return panel.getComboName().getEditor().getItem().toString().trim();
138 } else {
139 return selectedItem.toString().trim();
143 protected void refreshProperties() {
144 RequestProcessor rp = Git.getInstance().getRequestProcessor();
145 try {
146 support = new GitProgressSupport() {
147 protected void perform() {
148 Properties props = GitModuleConfig.getDefault().getProperties(root, "extensions"); // NOI18N
149 GitPropertiesNode[] hgProps = new HgPropertiesNode[props.size()];
150 int i = 0;
152 for (Enumeration e = props.propertyNames(); e.hasMoreElements() ; ) {
153 String name = (String) e.nextElement();
154 String tmp = props.getProperty(name);
155 String value = tmp != null ? tmp : ""; // NOI18N
156 hgProps[i] = new HgPropertiesNode(name, value);
157 i++;
159 propTable.setNodes(hgProps);
162 support.start(rp, null, org.openide.util.NbBundle.getMessage(HgExtProperties.class, "LBL_Properties_Progress")); // NOI18N
163 } finally {
164 support = null;
168 private boolean addProperty(String name, String value) {
169 GitPropertiesNode[] hgPropertiesNodes = propTable.getNodes();
170 for (int i = 0; i < hgPropertiesNodes.length; i++) {
171 String hgPropertyName = hgPropertiesNodes[propTable.getModelIndex(i)].getName();
172 if (hgPropertyName.equals(name)) {
173 hgPropertiesNodes[propTable.getModelIndex(i)].setValue(value);
174 propTable.setNodes(hgPropertiesNodes);
175 return true;
178 GitPropertiesNode[] hgProps = new HgPropertiesNode[hgPropertiesNodes.length + 1];
179 for (int i = 0; i < hgPropertiesNodes.length; i++) {
180 hgProps[i] = hgPropertiesNodes[i];
182 hgProps[hgPropertiesNodes.length] = new GitPropertiesNode(name, value);
183 propTable.setNodes(hgProps);
184 return true;
187 public void addProperty() {
188 if (addProperty(getPropertyName(), getPropertyValue())) {
189 panel.getComboName().getEditor().setItem(""); // NOI18N
190 panel.getTxtAreaValue().setText(""); // NOI18N
194 public void setProperties() {
195 RequestProcessor rp = Git.getInstance().getRequestProcessor();
196 try {
197 support = new GitProgressSupport() {
198 protected void perform() {
199 GitModuleConfig.getDefault().clearProperties(root, "extensions"); // NOI18N
200 GitPropertiesNode[] hgPropertiesNodes = propTable.getNodes();
201 for (int i = 0; i < hgPropertiesNodes.length; i++) {
202 String hgPropertyName = hgPropertiesNodes[propTable.getModelIndex(i)].getName();
203 String hgPropertyValue = hgPropertiesNodes[propTable.getModelIndex(i)].getValue();
204 HgModuleConfig.getDefault().setProperty(root, "extensions", hgPropertyName, hgPropertyValue, true); // NOI18N
208 support.start(rp, null, org.openide.util.NbBundle.getMessage(HgExtProperties.class, "LBL_Properties_Progress")); // NOI18N
209 } finally {
210 support = null;
214 public void removeProperties() {
215 final int[] rows = propTable.getSelectedItems();
216 // No rows selected
217 if (rows.length == 0) return;
218 GitPropertiesNode[] hgPropertiesNodes = propTable.getNodes();
219 GitPropertiesNode[] hgProps = new GitPropertiesNode[hgPropertiesNodes.length - rows.length];
220 int j = 0;
221 int k = 0;
222 for (int i = 0; i < hgPropertiesNodes.length; i++) {
223 if (i != rows[j]) {
224 hgProps[k++] = hgPropertiesNodes[i];
225 } else {
226 if (j < rows.length - 1) j++;
229 propTable.setNodes(hgProps);
232 public void insertUpdate(DocumentEvent event) {
233 validateUserInput(event);
236 public void removeUpdate(DocumentEvent event) {
237 validateUserInput(event);
240 public void changedUpdate(DocumentEvent event) {
241 validateUserInput(event);
244 private void validateUserInput(DocumentEvent event) {
246 Document doc = event.getDocument();
247 String name = panel.getComboName().getEditor().getItem().toString().trim();
248 String value = panel.getTxtAreaValue().getText().trim();
250 if (name.length() == 0 || name.indexOf(" ") > 0) { // NOI18N
251 panel.getBtnAdd().setEnabled(false);
252 } else {
253 panel.getBtnAdd().setEnabled(true);
257 public class TableMouseListener extends MouseAdapter {
259 @Override
260 public void mouseClicked(MouseEvent event) {
261 //super.mouseClicked(arg0);
262 if (event.getClickCount() == 2) {
263 int[] rows = propTable.getSelectedItems();
264 GitPropertiesNode[] hgPropertiesNodes = propTable.getNodes();
265 if (hgPropertiesNodes == null)
266 return;
267 final String hgPropertyName = hgPropertiesNodes[propTable.getModelIndex(rows[0])].getName();
268 final String hgPropertyValue = hgPropertiesNodes[propTable.getModelIndex(rows[0])].getValue();
269 EventQueue.invokeLater(new Runnable() {
270 public void run() {
271 panel.getComboName().getEditor().setItem(hgPropertyName);
272 panel.getTxtAreaValue().setText(hgPropertyValue);