1 19 package org.netbeans.modules.subversion.ui.properties; 20 21 import java.awt.Dialog ; 22 import java.awt.EventQueue ; 23 import java.awt.Font ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.MouseAdapter ; 27 import java.awt.event.MouseEvent ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 import java.io.StringWriter ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.List ; 35 import java.util.Vector ; 36 import javax.swing.ComboBoxModel ; 37 import javax.swing.DefaultComboBoxModel ; 38 import javax.swing.JFileChooser ; 39 import javax.swing.JTextField ; 40 import javax.swing.event.DocumentEvent ; 41 import javax.swing.event.DocumentListener ; 42 import javax.swing.text.Document ; 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 62 public class SvnProperties implements ActionListener , DocumentListener { 63 64 private PropertiesPanel panel; 65 private File root; 66 private PropertiesTable propTable; 67 private SvnProgressSupport support; 68 private boolean loadedFromFile; 69 private File loadedValueFile; 70 private Font fontTextArea; 71 72 73 public SvnProperties(PropertiesPanel panel, PropertiesTable propTable, File root) { 74 this.panel = panel; 75 this.propTable = propTable; 76 this.root = root; 77 panel.txtAreaValue.getDocument().addDocumentListener(this); 78 ((JTextField ) 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 getRoot() { 104 return root; 105 } 106 107 public void setRoot(File root) { 108 this.root = root; 109 } 110 111 public void setLoadedValueFile(File file) { 112 this.loadedValueFile = file; 113 } 114 115 public File getLoadedValueFile() { 116 return loadedValueFile; 117 } 118 119 public void actionPerformed(ActionEvent event) { 120 Object 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 <String > lstName = new ArrayList <String >(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 comboModel = new DefaultComboBoxModel (new Vector (lstName)); 155 panel.comboName.setModel(comboModel); 156 panel.comboName.getEditor().setItem(""); 157 } 158 159 protected String getPropertyValue() { 160 return panel.txtAreaValue.getText(); 161 } 162 163 protected String getPropertyName() { 164 Object 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 source) { 181 setLoadedValueFile(source); 182 StringBuffer txtValue = new StringBuffer (); 183 txtValue.append(NbBundle.getMessage(SvnProperties.class, "Binary_Content")); 184 txtValue.append("\n"); 185 try { 186 txtValue.append(source.getCanonicalPath()); 187 } catch (IOException ex) { 188 ErrorManager.getDefault().notify(ex); 189 } 190 fontTextArea = panel.txtAreaValue.getFont(); 191 panel.txtAreaValue.setFont(new Font ("Monospaced", Font.ITALIC, 12)); 192 panel.txtAreaValue.setText(txtValue.toString()); 193 setLoadedFromFile(true); 194 } 195 196 public void loadFromFile() { 197 final JFileChooser 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 [] fileFilters = chooser.getChoosableFileFilters(); 201 for (int i = 0; i < fileFilters.length; i++) { 202 javax.swing.filechooser.FileFilter fileFilter = fileFilters[i]; 203 chooser.removeChoosableFileFilter(fileFilter); 204 } 205 206 chooser.setCurrentDirectory(root.getParentFile()); chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter () { 208 public boolean accept(File f) { 209 return f.exists(); 210 } 211 public String 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 [0]); 221 final Dialog dialog = DialogDisplayer.getDefault().createDialog(dd); 222 223 chooser.addActionListener(new ActionListener () { 224 public void actionPerformed(ActionEvent e) { 225 String state = e.getActionCommand(); 226 if (state.equals(JFileChooser.APPROVE_SELECTION)) { 227 File source = chooser.getSelectedFile(); 228 229 if (SvnUtils.isFileContentBinary(source)) { 230 handleBinaryFile(source); 231 } else { 232 if (source.canRead()) { 233 StringWriter sw = new StringWriter (); 234 try { 235 Utils.copyStreamsCloseAll(sw, new FileReader (source)); 236 panel.txtAreaValue.setText(sw.toString()); 237 } catch (IOException 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); 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 () { 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 return; 279 } 280 String name = isvnProps[i].getName(); 281 String value; 282 if (SvnUtils.isBinary(isvnProps[i].getData())) { 283 value = org.openide.util.NbBundle.getMessage(SvnProperties.class, "Binary_Content"); 284 } else { 285 String 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); return; 315 } 316 317 try { 318 if (isLoadedFromFile()) { 319 try { 320 client.propertySet(root, getPropertyName(), getLoadedValueFile(), panel.cbxRecursively.isSelected()); 321 } catch (IOException 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 () { 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); 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 <SvnPropertiesNode> lstSvnPropertiesNodes = Arrays.asList(svnPropertiesNodes); 367 String 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 e) { 372 } 373 374 propTable.setNodes((SvnPropertiesNode[]) lstSvnPropertiesNodes.toArray()); 375 } 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 event) { 391 validateUserInput(event); 392 } 393 394 public void removeUpdate(DocumentEvent event) { 395 validateUserInput(event); 396 } 397 398 public void changedUpdate(DocumentEvent event) { 399 validateUserInput(event); 400 } 401 402 private void validateUserInput(DocumentEvent event) { 403 404 Document doc = event.getDocument(); 405 synchronized (panel.txtAreaValue) { 406 if (doc.equals(panel.txtAreaValue.getDocument())) { 407 if (isLoadedFromFile()) { 408 EventQueue.invokeLater(new Runnable () { 409 public void run() { 410 panel.txtAreaValue.setFont(fontTextArea); 411 panel.txtAreaValue.setText(""); 412 } 413 }); 414 setLoadedFromFile(false); 415 } 416 } 417 } 418 String name = panel.comboName.getEditor().getItem().toString().trim(); 419 String 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 { 429 430 @Override 431 public void mouseClicked(MouseEvent event) { 432 if (event.getClickCount() == 2) { 434 int[] rows = propTable.getSelectedItems(); 435 SvnPropertiesNode[] svnPropertiesNodes = propTable.getNodes(); 436 if (svnPropertiesNodes == null) 437 return; 438 final String svnPropertyName = svnPropertiesNodes[propTable.getModelIndex(rows[0])].getName(); 439 final String svnPropertyValue = svnPropertiesNodes[propTable.getModelIndex(rows[0])].getValue(); 440 EventQueue.invokeLater(new Runnable () { 441 public void run() { 442 panel.comboName.getEditor().setItem(svnPropertyName); 443 panel.txtAreaValue.setText(svnPropertyValue); 444 } 445 }); 446 } 447 } 448 } 449 } | Popular Tags |