KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > properties > SvnProperties


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.properties;
20
21 import java.awt.Dialog JavaDoc;
22 import java.awt.EventQueue JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.MouseAdapter JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Vector JavaDoc;
36 import javax.swing.ComboBoxModel JavaDoc;
37 import javax.swing.DefaultComboBoxModel JavaDoc;
38 import javax.swing.JFileChooser JavaDoc;
39 import javax.swing.JTextField JavaDoc;
40 import javax.swing.event.DocumentEvent JavaDoc;
41 import javax.swing.event.DocumentListener JavaDoc;
42 import javax.swing.text.Document JavaDoc;
43 import org.netbeans.modules.subversion.Subversion;
44 import org.netbeans.modules.subversion.client.SvnClient;
45 import org.netbeans.modules.subversion.client.SvnProgressSupport;
46 import org.netbeans.modules.subversion.util.SvnUtils;
47 import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
48 import org.netbeans.modules.versioning.util.Utils;
49 import org.openide.DialogDescriptor;
50 import org.openide.DialogDisplayer;
51 import org.openide.ErrorManager;
52 import org.openide.util.NbBundle;
53 import org.openide.util.RequestProcessor;
54 import org.tigris.subversion.svnclientadapter.ISVNProperty;
55 import org.tigris.subversion.svnclientadapter.SVNClientException;
56 import org.tigris.subversion.svnclientadapter.SVNUrl;
57
58 /**
59  *
60  * @author Peter Pis
61  */

62 public class SvnProperties implements ActionListener JavaDoc, DocumentListener JavaDoc {
63     
64     private PropertiesPanel panel;
65     private File JavaDoc root;
66     private PropertiesTable propTable;
67     private SvnProgressSupport support;
68     private boolean loadedFromFile;
69     private File JavaDoc loadedValueFile;
70     private Font JavaDoc fontTextArea;
71     
72     /** Creates a ew instance of SvnProperties */
73     public SvnProperties(PropertiesPanel panel, PropertiesTable propTable, File JavaDoc root) {
74         this.panel = panel;
75         this.propTable = propTable;
76         this.root = root;
77         panel.txtAreaValue.getDocument().addDocumentListener(this);
78         ((JTextField JavaDoc) panel.comboName.getEditor().getEditorComponent()).getDocument().addDocumentListener(this);
79         propTable.getTable().addMouseListener(new TableMouseListener());
80         panel.btnRefresh.addActionListener(this);
81         panel.btnAdd.addActionListener(this);
82         panel.btnRemove.addActionListener(this);
83         panel.btnBrowse.addActionListener(this);
84         panel.comboName.setEditable(true);
85         if (!root.isDirectory()) {
86             panel.cbxRecursively.setEnabled(false);
87         }
88         setLoadedValueFile(null);
89         initPropertyNameCbx();
90         setLoadedFromFile(false);
91         refreshProperties();
92         
93     }
94     
95     public PropertiesPanel getPropertiesPanel() {
96         return panel;
97     }
98     
99     public void setPropertiesPanel(PropertiesPanel panel) {
100         this.panel = panel;
101     }
102     
103     public File JavaDoc getRoot() {
104         return root;
105     }
106     
107     public void setRoot(File JavaDoc root) {
108         this.root = root;
109     }
110     
111     public void setLoadedValueFile(File JavaDoc file) {
112         this.loadedValueFile = file;
113     }
114     
115     public File JavaDoc getLoadedValueFile() {
116         return loadedValueFile;
117     }
118     
119     public void actionPerformed(ActionEvent JavaDoc event) {
120         Object JavaDoc source = event.getSource();
121         
122         if (source.equals(panel.btnRefresh)) {
123             refreshProperties();
124         }
125         
126         if (source.equals(panel.btnAdd)) {
127             setProperties();
128         }
129         
130         if (source.equals(panel.btnRemove)) {
131             removeProperties();
132         }
133         
134         if (source.equals(panel.btnBrowse)) {
135             loadFromFile();
136         }
137     }
138     
139     protected void initPropertyNameCbx() {
140         List JavaDoc<String JavaDoc> lstName = new ArrayList JavaDoc<String JavaDoc>(8);
141         if (panel.comboName.isEditable()) {
142             if (root.isDirectory()) {
143                 lstName.add("svn:ignore");
144                 lstName.add("svn:externals");
145             } else {
146                 lstName.add("svn:eol-style");
147                 lstName.add("svn:executable");
148                 lstName.add("svn:keywords");
149                 lstName.add("svn:needs-lock");
150                 lstName.add("svn:mime-type");
151             }
152             
153         }
154         ComboBoxModel JavaDoc comboModel = new DefaultComboBoxModel JavaDoc(new Vector JavaDoc(lstName));
155         panel.comboName.setModel(comboModel);
156         panel.comboName.getEditor().setItem("");
157     }
158     
159     protected String JavaDoc getPropertyValue() {
160         return panel.txtAreaValue.getText();
161     }
162     
163     protected String JavaDoc getPropertyName() {
164         Object JavaDoc selectedItem = panel.comboName.getSelectedObjects()[0];
165         if (selectedItem != null) {
166             return panel.comboName.getEditor().getItem().toString().trim();
167         } else {
168             return selectedItem.toString().trim();
169         }
170     }
171     
172     public boolean isLoadedFromFile() {
173         return loadedFromFile;
174     }
175     
176     public void setLoadedFromFile(boolean value) {
177         this.loadedFromFile = value;
178     }
179     
180     public void handleBinaryFile(File JavaDoc source) {
181         setLoadedValueFile(source);
182         StringBuffer JavaDoc txtValue = new StringBuffer JavaDoc();
183         txtValue.append(NbBundle.getMessage(SvnProperties.class, "Binary_Content"));
184         txtValue.append("\n");
185         try {
186             txtValue.append(source.getCanonicalPath());
187         } catch (IOException JavaDoc ex) {
188             ErrorManager.getDefault().notify(ex);
189         }
190         fontTextArea = panel.txtAreaValue.getFont();
191         panel.txtAreaValue.setFont(new Font JavaDoc("Monospaced", Font.ITALIC, 12));
192         panel.txtAreaValue.setText(txtValue.toString());
193         setLoadedFromFile(true);
194     }
195     
196     public void loadFromFile() {
197         final JFileChooser JavaDoc chooser = new AccessibleJFileChooser(NbBundle.getMessage(SvnProperties.class, "ACSD_Properties"));
198         chooser.setDialogTitle(NbBundle.getMessage(SvnProperties.class, "CTL_Load_Value_Title"));
199         chooser.setMultiSelectionEnabled(false);
200         javax.swing.filechooser.FileFilter JavaDoc[] fileFilters = chooser.getChoosableFileFilters();
201         for (int i = 0; i < fileFilters.length; i++) {
202             javax.swing.filechooser.FileFilter JavaDoc fileFilter = fileFilters[i];
203             chooser.removeChoosableFileFilter(fileFilter);
204         }
205         
206         chooser.setCurrentDirectory(root.getParentFile()); // NOI18N
207
chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter JavaDoc() {
208             public boolean accept(File JavaDoc f) {
209                 return f.exists();
210             }
211             public String JavaDoc getDescription() {
212                 return "";
213             }
214         });
215         
216         chooser.setDialogType(JFileChooser.OPEN_DIALOG);
217         chooser.setApproveButtonMnemonic(NbBundle.getMessage(SvnProperties.class, "MNE_LoadValue").charAt(0));
218         chooser.setApproveButtonText(NbBundle.getMessage(SvnProperties.class, "CTL_LoadValue"));
219         DialogDescriptor dd = new DialogDescriptor(chooser, NbBundle.getMessage(SvnProperties.class, "CTL_Load_Value_Title"));
220         dd.setOptions(new Object JavaDoc[0]);
221         final Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dd);
222         
223         chooser.addActionListener(new ActionListener JavaDoc() {
224             public void actionPerformed(ActionEvent JavaDoc e) {
225                 String JavaDoc state = e.getActionCommand();
226                 if (state.equals(JFileChooser.APPROVE_SELECTION)) {
227                     File JavaDoc source = chooser.getSelectedFile();
228                   
229                     if (SvnUtils.isFileContentBinary(source)) {
230                         handleBinaryFile(source);
231                     } else {
232                         if (source.canRead()) {
233                             StringWriter JavaDoc sw = new StringWriter JavaDoc();
234                             try {
235                                 Utils.copyStreamsCloseAll(sw, new FileReader JavaDoc(source));
236                                 panel.txtAreaValue.setText(sw.toString());
237                             } catch (IOException JavaDoc ex) {
238                                 ErrorManager.getDefault().notify(ex);
239                             }
240                         }
241                     }
242                 }
243                 dialog.dispose();
244             }
245         });
246         dialog.setVisible(true);
247
248     }
249   
250     protected void refreshProperties() {
251         final SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(root);
252               
253         RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repositoryUrl);
254         try {
255             support = new SvnProgressSupport() {
256                 SvnClient client;
257                 ISVNProperty[] isvnProps;
258                 protected void perform() {
259                     try {
260                         client = Subversion.getInstance().getClient(repositoryUrl);
261                     } catch (SVNClientException ex) {
262                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not hapen
263
return;
264                     }
265                     
266                     try {
267                         isvnProps = client.getProperties(root);
268                     } catch (SVNClientException ex) {
269                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
270                         return;
271                     }
272                     EventQueue.invokeLater(new Runnable JavaDoc() {
273                         public void run() {
274                             SvnPropertiesNode[] svnProps = new SvnPropertiesNode[isvnProps.length];
275                             for (int i = 0; i < isvnProps.length; i++) {
276                                 if (isvnProps[i] == null) {
277                                     //ErrorManager.getDefault().notify();
278
return;
279                                 }
280                                 String JavaDoc name = isvnProps[i].getName();
281                                 String JavaDoc value;
282                                 if (SvnUtils.isBinary(isvnProps[i].getData())) {
283                                     value = org.openide.util.NbBundle.getMessage(SvnProperties.class, "Binary_Content");
284                                 } else {
285                                     String JavaDoc tmp = isvnProps[i].getValue();
286                                     value = tmp != null ? tmp : "";
287                                 }
288                                 svnProps[i] = new SvnPropertiesNode(name, value);
289                             }
290                             propTable.setNodes(svnProps);
291                         }
292                     });
293                 }
294             };
295             support.start(rp, repositoryUrl, org.openide.util.NbBundle.getMessage(SvnProperties.class, "LBL_Properties_Progress"));
296         } finally {
297             support = null;
298         }
299     }
300     
301     public void setProperties() {
302         final SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(root);
303               
304         RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repositoryUrl);
305         try {
306             support = new SvnProgressSupport() {
307                 SvnClient client;
308                 ISVNProperty[] isvnProps;
309                 protected void perform() {
310                     try {
311                         client = Subversion.getInstance().getClient(repositoryUrl);
312                     } catch (SVNClientException ex) {
313                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not hapen
314
return;
315                     }
316                     
317                     try {
318                         if (isLoadedFromFile()) {
319                             try {
320                                 client.propertySet(root, getPropertyName(), getLoadedValueFile(), panel.cbxRecursively.isSelected());
321                             } catch (IOException JavaDoc ex) {
322                                 ErrorManager.getDefault().notify(ex);
323                                 return;
324                             }
325                         } else {
326                             client.propertySet(root, getPropertyName(), getPropertyValue(), panel.cbxRecursively.isSelected());
327                         }
328                     } catch (SVNClientException ex) {
329                         ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
330                         return;
331                     }
332                     EventQueue.invokeLater(new Runnable JavaDoc() {
333                         public void run() {
334                             panel.comboName.getEditor().setItem("");
335                             panel.txtAreaValue.setText("");
336                         }
337                     });
338                 }
339             };
340             support.start(rp, repositoryUrl, org.openide.util.NbBundle.getMessage(SvnProperties.class, "LBL_Properties_Progress"));
341         } finally {
342             support = null;
343         }
344         refreshProperties();
345     }
346     
347     public void removeProperties() {
348         final SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(root);
349         final int[] rows = propTable.getSelectedItems();
350         RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repositoryUrl);
351         try {
352             support = new SvnProgressSupport() {
353                 SvnClient client;
354                 protected void perform() {
355                     try {
356                         client = Subversion.getInstance().getClient(repositoryUrl);
357                     } catch (SVNClientException ex) {
358                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not hapen
359
return;
360                     }
361                     
362                     try {
363                         boolean recursively = panel.cbxRecursively.isSelected();
364                         for (int i = rows.length - 1; i >= 0; i--) {
365                             SvnPropertiesNode[] svnPropertiesNodes = propTable.getNodes();
366                             List JavaDoc<SvnPropertiesNode> lstSvnPropertiesNodes = Arrays.asList(svnPropertiesNodes);
367                             String JavaDoc svnPropertyName = svnPropertiesNodes[propTable.getModelIndex(rows[i])].getName();
368                             client.propertyDel(root, svnPropertyName, recursively);
369                             try {
370                                 lstSvnPropertiesNodes.remove(svnPropertiesNodes[propTable.getModelIndex(rows[i])]);
371                             } catch (UnsupportedOperationException JavaDoc e) {
372                             }
373
374                             propTable.setNodes((SvnPropertiesNode[]) lstSvnPropertiesNodes.toArray());
375                             //refreshProperties();
376
}
377                     } catch (SVNClientException ex) {
378                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
379                         return;
380                     }
381                 }
382             };
383             support.start(rp, repositoryUrl, org.openide.util.NbBundle.getMessage(SvnProperties.class, "LBL_Properties_Progress"));
384         } finally {
385             support = null;
386         }
387         refreshProperties();
388     }
389     
390     public void insertUpdate(DocumentEvent JavaDoc event) {
391         validateUserInput(event);
392     }
393
394     public void removeUpdate(DocumentEvent JavaDoc event) {
395         validateUserInput(event);
396     }
397
398     public void changedUpdate(DocumentEvent JavaDoc event) {
399         validateUserInput(event);
400     }
401     
402     private void validateUserInput(DocumentEvent JavaDoc event) {
403         
404         Document JavaDoc doc = event.getDocument();
405         synchronized (panel.txtAreaValue) {
406             if (doc.equals(panel.txtAreaValue.getDocument())) {
407                 if (isLoadedFromFile()) {
408                    EventQueue.invokeLater(new Runnable JavaDoc() {
409                         public void run() {
410                             panel.txtAreaValue.setFont(fontTextArea);
411                             panel.txtAreaValue.setText("");
412                         }
413                    });
414                    setLoadedFromFile(false);
415                 }
416             }
417         }
418         String JavaDoc name = panel.comboName.getEditor().getItem().toString().trim();
419         String JavaDoc value = panel.txtAreaValue.getText().trim();
420         
421         if (name.length() == 0 || value.length() == 0 || name.indexOf(" ") > 0) {
422             panel.btnAdd.setEnabled(false);
423         } else {
424             panel.btnAdd.setEnabled(true);
425         }
426     }
427     
428     public class TableMouseListener extends MouseAdapter JavaDoc {
429         
430         @Override JavaDoc
431         public void mouseClicked(MouseEvent JavaDoc event) {
432             //super.mouseClicked(arg0);
433
if (event.getClickCount() == 2) {
434                 int[] rows = propTable.getSelectedItems();
435                 SvnPropertiesNode[] svnPropertiesNodes = propTable.getNodes();
436                 if (svnPropertiesNodes == null)
437                     return;
438                 final String JavaDoc svnPropertyName = svnPropertiesNodes[propTable.getModelIndex(rows[0])].getName();
439                 final String JavaDoc svnPropertyValue = svnPropertiesNodes[propTable.getModelIndex(rows[0])].getValue();
440                 EventQueue.invokeLater(new Runnable JavaDoc() {
441                     public void run() {
442                         panel.comboName.getEditor().setItem(svnPropertyName);
443                         panel.txtAreaValue.setText(svnPropertyValue);
444                     }
445                 });
446             }
447         }
448 }
449 }
Popular Tags