001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mercurial.options;
042:
043: import java.awt.Dialog;
044: import java.awt.EventQueue;
045: import java.awt.Font;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.awt.event.MouseAdapter;
049: import java.awt.event.MouseEvent;
050: import java.io.File;
051: import java.io.FileReader;
052: import java.io.IOException;
053: import java.io.StringWriter;
054: import java.util.ArrayList;
055: import java.util.Arrays;
056: import java.util.List;
057: import java.util.Vector;
058: import java.util.Properties;
059: import java.util.Enumeration;
060: import javax.swing.JOptionPane;
061: import javax.swing.ComboBoxModel;
062: import javax.swing.DefaultComboBoxModel;
063: import javax.swing.JFileChooser;
064: import javax.swing.JTextField;
065: import javax.swing.event.DocumentEvent;
066: import javax.swing.event.DocumentListener;
067: import javax.swing.text.Document;
068: import org.netbeans.modules.mercurial.Mercurial;
069: import org.netbeans.modules.mercurial.HgProgressSupport;
070: import org.netbeans.modules.mercurial.HgModuleConfig;
071: import org.netbeans.modules.mercurial.options.PropertiesPanel;
072: import org.netbeans.modules.mercurial.options.PropertiesTable;
073: import org.netbeans.modules.mercurial.ui.properties.HgPropertiesNode;
074: import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
075: import org.netbeans.modules.versioning.util.Utils;
076: import org.openide.DialogDescriptor;
077: import org.openide.DialogDisplayer;
078: import org.openide.ErrorManager;
079: import org.openide.util.NbBundle;
080: import org.openide.util.RequestProcessor;
081: import org.openide.NotifyDescriptor;
082:
083: /**
084: *
085: * @author Padraig O'Briain
086: */
087: public class HgExtProperties implements ActionListener,
088: DocumentListener {
089:
090: private PropertiesPanel panel;
091: private File root;
092: private PropertiesTable propTable;
093: private HgProgressSupport support;
094: private File loadedValueFile;
095: private Font fontTextArea;
096:
097: /** Creates a new instance of HgExtProperties */
098: public HgExtProperties(PropertiesPanel panel,
099: PropertiesTable propTable, File root) {
100: this .panel = panel;
101: this .propTable = propTable;
102: this .root = root;
103: panel.getTxtAreaValue().getDocument().addDocumentListener(this );
104: ((JTextField) panel.getComboName().getEditor()
105: .getEditorComponent()).getDocument()
106: .addDocumentListener(this );
107: propTable.getTable().addMouseListener(new TableMouseListener());
108: panel.getBtnAdd().addActionListener(this );
109: panel.getBtnRemove().addActionListener(this );
110: panel.getComboName().setEditable(true);
111: panel.getBtnAdd().setEnabled(false);
112: initPropertyNameCbx();
113: refreshProperties();
114: }
115:
116: public PropertiesPanel getPropertiesPanel() {
117: return panel;
118: }
119:
120: public void setPropertiesPanel(PropertiesPanel panel) {
121: this .panel = panel;
122: }
123:
124: public File getRoot() {
125: return root;
126: }
127:
128: public void setRoot(File root) {
129: this .root = root;
130: }
131:
132: public void actionPerformed(ActionEvent event) {
133: Object source = event.getSource();
134:
135: if (source.equals(panel.getBtnAdd())) {
136: addProperty();
137: }
138:
139: if (source.equals(panel.getBtnRemove())) {
140: removeProperties();
141: }
142:
143: }
144:
145: protected void initPropertyNameCbx() {
146: List<String> lstName = new ArrayList<String>(8);
147:
148: ComboBoxModel comboModel = new DefaultComboBoxModel(
149: new Vector<String>(lstName));
150: panel.getComboName().setModel(comboModel);
151: panel.getComboName().getEditor().setItem(""); // NOI18N
152: }
153:
154: protected String getPropertyValue() {
155: return panel.getTxtAreaValue().getText();
156: }
157:
158: protected String getPropertyName() {
159: Object selectedItem = panel.getComboName().getSelectedObjects()[0];
160: if (selectedItem != null) {
161: return panel.getComboName().getEditor().getItem()
162: .toString().trim();
163: } else {
164: return selectedItem.toString().trim();
165: }
166: }
167:
168: protected void refreshProperties() {
169: RequestProcessor rp = Mercurial.getInstance()
170: .getRequestProcessor();
171: try {
172: support = new HgProgressSupport() {
173: protected void perform() {
174: Properties props = HgModuleConfig.getDefault()
175: .getProperties(root, "extensions"); // NOI18N
176: HgPropertiesNode[] hgProps = new HgPropertiesNode[props
177: .size()];
178: int i = 0;
179:
180: for (Enumeration e = props.propertyNames(); e
181: .hasMoreElements();) {
182: String name = (String) e.nextElement();
183: String tmp = props.getProperty(name);
184: String value = tmp != null ? tmp : ""; // NOI18N
185: hgProps[i] = new HgPropertiesNode(name, value);
186: i++;
187: }
188: propTable.setNodes(hgProps);
189: }
190: };
191: support.start(rp, null, org.openide.util.NbBundle
192: .getMessage(HgExtProperties.class,
193: "LBL_Properties_Progress")); // NOI18N
194: } finally {
195: support = null;
196: }
197: }
198:
199: private boolean addProperty(String name, String value) {
200: HgPropertiesNode[] hgPropertiesNodes = propTable.getNodes();
201: for (int i = 0; i < hgPropertiesNodes.length; i++) {
202: String hgPropertyName = hgPropertiesNodes[propTable
203: .getModelIndex(i)].getName();
204: if (hgPropertyName.equals(name)) {
205: hgPropertiesNodes[propTable.getModelIndex(i)]
206: .setValue(value);
207: propTable.setNodes(hgPropertiesNodes);
208: return true;
209: }
210: }
211: HgPropertiesNode[] hgProps = new HgPropertiesNode[hgPropertiesNodes.length + 1];
212: for (int i = 0; i < hgPropertiesNodes.length; i++) {
213: hgProps[i] = hgPropertiesNodes[i];
214: }
215: hgProps[hgPropertiesNodes.length] = new HgPropertiesNode(name,
216: value);
217: propTable.setNodes(hgProps);
218: return true;
219: }
220:
221: public void addProperty() {
222: if (addProperty(getPropertyName(), getPropertyValue())) {
223: panel.getComboName().getEditor().setItem(""); // NOI18N
224: panel.getTxtAreaValue().setText(""); // NOI18N
225: }
226: }
227:
228: public void setProperties() {
229: RequestProcessor rp = Mercurial.getInstance()
230: .getRequestProcessor();
231: try {
232: support = new HgProgressSupport() {
233: protected void perform() {
234: HgModuleConfig.getDefault().clearProperties(root,
235: "extensions"); // NOI18N
236: HgPropertiesNode[] hgPropertiesNodes = propTable
237: .getNodes();
238: for (int i = 0; i < hgPropertiesNodes.length; i++) {
239: String hgPropertyName = hgPropertiesNodes[propTable
240: .getModelIndex(i)].getName();
241: String hgPropertyValue = hgPropertiesNodes[propTable
242: .getModelIndex(i)].getValue();
243: HgModuleConfig.getDefault().setProperty(root,
244: "extensions", hgPropertyName,
245: hgPropertyValue, true); // NOI18N
246: }
247: }
248: };
249: support.start(rp, null, org.openide.util.NbBundle
250: .getMessage(HgExtProperties.class,
251: "LBL_Properties_Progress")); // NOI18N
252: } finally {
253: support = null;
254: }
255: }
256:
257: public void removeProperties() {
258: final int[] rows = propTable.getSelectedItems();
259: // No rows selected
260: if (rows.length == 0)
261: return;
262: HgPropertiesNode[] hgPropertiesNodes = propTable.getNodes();
263: HgPropertiesNode[] hgProps = new HgPropertiesNode[hgPropertiesNodes.length
264: - rows.length];
265: int j = 0;
266: int k = 0;
267: for (int i = 0; i < hgPropertiesNodes.length; i++) {
268: if (i != rows[j]) {
269: hgProps[k++] = hgPropertiesNodes[i];
270: } else {
271: if (j < rows.length - 1)
272: j++;
273: }
274: }
275: propTable.setNodes(hgProps);
276: }
277:
278: public void insertUpdate(DocumentEvent event) {
279: validateUserInput(event);
280: }
281:
282: public void removeUpdate(DocumentEvent event) {
283: validateUserInput(event);
284: }
285:
286: public void changedUpdate(DocumentEvent event) {
287: validateUserInput(event);
288: }
289:
290: private void validateUserInput(DocumentEvent event) {
291:
292: Document doc = event.getDocument();
293: String name = panel.getComboName().getEditor().getItem()
294: .toString().trim();
295: String value = panel.getTxtAreaValue().getText().trim();
296:
297: if (name.length() == 0 || name.indexOf(" ") > 0) { // NOI18N
298: panel.getBtnAdd().setEnabled(false);
299: } else {
300: panel.getBtnAdd().setEnabled(true);
301: }
302: }
303:
304: public class TableMouseListener extends MouseAdapter {
305:
306: @Override
307: public void mouseClicked(MouseEvent event) {
308: //super.mouseClicked(arg0);
309: if (event.getClickCount() == 2) {
310: int[] rows = propTable.getSelectedItems();
311: HgPropertiesNode[] hgPropertiesNodes = propTable
312: .getNodes();
313: if (hgPropertiesNodes == null)
314: return;
315: final String hgPropertyName = hgPropertiesNodes[propTable
316: .getModelIndex(rows[0])].getName();
317: final String hgPropertyValue = hgPropertiesNodes[propTable
318: .getModelIndex(rows[0])].getValue();
319: EventQueue.invokeLater(new Runnable() {
320: public void run() {
321: panel.getComboName().getEditor().setItem(
322: hgPropertyName);
323: panel.getTxtAreaValue()
324: .setText(hgPropertyValue);
325: }
326: });
327: }
328: }
329: }
330: }
|