1 19 package org.netbeans.modules.subversion.ui.update; 20 21 import java.awt.Dialog ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.net.MalformedURLException ; 27 import javax.swing.JButton ; 28 import javax.swing.JRadioButton ; 29 import javax.swing.event.DocumentEvent ; 30 import javax.swing.event.DocumentListener ; 31 import org.netbeans.modules.subversion.RepositoryFile; 32 import org.netbeans.modules.subversion.ui.browser.RepositoryPaths; 33 import org.openide.DialogDescriptor; 34 import org.openide.DialogDisplayer; 35 import org.openide.ErrorManager; 36 import org.openide.util.HelpCtx; 37 import org.tigris.subversion.svnclientadapter.SVNRevision; 38 39 43 public class RevertModifications implements PropertyChangeListener { 44 45 private RevertModificationsPanel panel; 46 private JButton okButton; 47 private JButton cancelButton; 48 private RevertType[] types; 49 50 51 public RevertModifications(RepositoryFile repositoryFile) { 52 this (repositoryFile, null); 53 } 54 55 56 public RevertModifications(RepositoryFile repositoryFile, String defaultRevision) { 57 OneCommitRevertType ocrt = new OneCommitRevertType(repositoryFile, getPanel().oneCommitRadioButton); 58 types = new RevertType[] { 59 new LocalRevertType(getPanel().localChangesRadioButton), 60 ocrt, 61 new MoreCommitsRevertType(repositoryFile, getPanel().moreCommitsRadioButton) 62 }; 63 okButton = new JButton (org.openide.util.NbBundle.getMessage(RevertModifications.class, "CTL_RevertForm_Action_Revert")); okButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(RevertModifications.class, "ACSD_RevertForm_Action_Revert")); cancelButton = new JButton (org.openide.util.NbBundle.getMessage(RevertModifications.class, "CTL_RevertForm_Action_Cancel")); cancelButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(RevertModifications.class, "ACSD_RevertForm_Action_Cancel")); if (defaultRevision != null) { 68 panel.oneCommitRadioButton.setSelected(true); 69 panel.oneRevisionTextField.setText(defaultRevision); 70 ocrt.actionPerformed(null); 71 } 72 } 73 74 private RevertModificationsPanel getPanel() { 75 if(panel == null) { 76 panel = new RevertModificationsPanel(); 77 } 78 return panel; 79 } 80 81 RevisionInterval getRevisionInterval() { 82 for (int i = 0; i < types.length; i++) { 83 if(types[i].isSelected()) { 84 return types[i].getRevisionInterval(); 85 } 86 } 87 return null; 88 } 89 90 boolean revertNewFiles() { 91 for (int i = 0; i < types.length; i++) { 92 if(types[i].isSelected()) { 93 return types[i].revertNewFiles(); 94 } 95 } 96 return false; 97 } 98 99 public boolean showDialog() { 100 DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, org.openide.util.NbBundle.getMessage(RevertModifications.class, "CTL_RevertDialog")); dialogDescriptor.setOptions(new Object [] {okButton, cancelButton}); 102 103 dialogDescriptor.setModal(true); 104 dialogDescriptor.setHelpCtx(new HelpCtx(this.getClass())); 105 dialogDescriptor.setValid(false); 106 107 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 108 dialog.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(RevertModifications.class, "ACSD_RevertDialog")); dialog.setVisible(true); 110 dialog.setResizable(false); 111 boolean ret = dialogDescriptor.getValue() == okButton; 112 return ret; 113 } 114 115 public void propertyChange(PropertyChangeEvent evt) { 116 if( evt.getPropertyName().equals(RepositoryPaths.PROP_VALID) ) { 117 if(okButton != null) { 118 boolean valid = ((Boolean )evt.getNewValue()).booleanValue(); 119 okButton.setEnabled(valid); 120 } 121 } 122 } 123 124 protected void setMoreCommitsFieldsEnabled(boolean b) { 125 getPanel().startRevisionTextField.setEnabled(b); 126 getPanel().endRevisionTextField.setEnabled(b); 127 getPanel().startSearchButton.setEnabled(b); 128 getPanel().endSearchButton.setEnabled(b); 129 } 130 131 protected void setOneCommitFieldsEnabled(boolean b) { 132 getPanel().oneRevisionSearchButton.setEnabled(b); 133 getPanel().oneRevisionTextField.setEnabled(b); 134 } 135 136 static class RevisionInterval { 137 SVNRevision startRevision; 138 SVNRevision endRevision; 139 } 140 141 private abstract class RevertType implements ActionListener , DocumentListener { 142 private JRadioButton button; 143 144 RevertType(JRadioButton button) { 145 this.button = button; 146 button.addActionListener(this); 147 } 148 149 boolean isSelected() { 150 return button.isSelected(); 151 } 152 153 boolean revertNewFiles() { 154 return panel.revertNewFilesCheckBox.isSelected(); 155 } 156 157 public void insertUpdate(DocumentEvent e) { 158 validateUserInput(); 159 } 160 161 public void removeUpdate(DocumentEvent e) { 162 validateUserInput(); 163 } 164 165 public void changedUpdate(DocumentEvent e) { 166 validateUserInput(); 167 } 168 169 void validateUserInput() { 170 } 172 173 RevisionInterval getRevisionInterval() { 174 return null; } 176 177 protected SVNRevision getRevision(RepositoryPaths path) { 178 try { 179 return path.getRepositoryFiles()[0].getRevision(); 180 } catch (NumberFormatException ex) { 181 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 184 } catch (MalformedURLException ex) { 185 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 188 } 189 return null; 190 } 191 192 protected boolean validateRevision(SVNRevision revision) { 193 boolean valid = revision == null || revision.equals(SVNRevision.HEAD) || revision.getKind() == SVNRevision.Kind.number; 194 RevertModifications.this.okButton.setEnabled(valid); 195 return valid; 196 } 197 } 198 199 private class LocalRevertType extends RevertType { 200 201 LocalRevertType (JRadioButton button) { 202 super(button); 203 } 204 205 RevertModifications.RevisionInterval getRevisionInterval() { 206 return null; 207 } 208 209 public void actionPerformed(ActionEvent e) { 210 setOneCommitFieldsEnabled(false); 211 setMoreCommitsFieldsEnabled(false); 212 } 213 } 214 215 private class OneCommitRevertType extends RevertType { 216 217 private RepositoryPaths oneRevisionPath; 218 219 OneCommitRevertType (RepositoryFile repositoryFile, JRadioButton button) { 220 super(button); 221 oneRevisionPath = 222 new RepositoryPaths( 223 repositoryFile, 224 null, 225 null, 226 getPanel().oneRevisionTextField, 227 getPanel().oneRevisionSearchButton 228 ); 229 oneRevisionPath.addPropertyChangeListener(RevertModifications.this); 230 } 231 232 RevertModifications.RevisionInterval getRevisionInterval() { 233 SVNRevision revision = getRevision(oneRevisionPath); 234 RevisionInterval ret = new RevisionInterval(); 235 ret.startRevision = revision; 236 ret.endRevision = revision; 237 return ret; 238 } 239 240 void validateUserInput() { 241 validateRevision(getRevision(oneRevisionPath)); 242 } 243 244 public void actionPerformed(ActionEvent e) { 245 setOneCommitFieldsEnabled(true); 246 setMoreCommitsFieldsEnabled(false); 247 validateUserInput(); 248 } 249 250 } 251 252 private class MoreCommitsRevertType extends RevertType { 253 254 private RepositoryPaths endPath; 255 private RepositoryPaths startPath; 256 257 MoreCommitsRevertType (RepositoryFile repositoryFile, JRadioButton button) { 258 super(button); 259 startPath = 260 new RepositoryPaths( 261 repositoryFile, 262 null, 263 null, 264 getPanel().startRevisionTextField, 265 getPanel().startSearchButton 266 ); 267 startPath.addPropertyChangeListener(RevertModifications.this); 268 269 endPath = 270 new RepositoryPaths( 271 repositoryFile, 272 null, 273 null, 274 getPanel().endRevisionTextField, 275 getPanel().endSearchButton 276 ); 277 endPath.addPropertyChangeListener(RevertModifications.this); 278 } 279 280 RevertModifications.RevisionInterval getRevisionInterval() { 281 SVNRevision revision1 = getRevision(startPath); 282 SVNRevision revision2 = getRevision(endPath); 283 if(revision1 == null || revision2 == null) { 284 return null; 285 } 286 287 return getResortedRevisionInterval(revision1, revision2); 288 } 289 290 void validateUserInput() { 291 if(!validateRevision(getRevision(startPath))) { 292 return; 293 } 294 if(!validateRevision(getRevision(endPath))) { 295 return; 296 } 297 } 298 299 public void actionPerformed(ActionEvent e) { 300 setMoreCommitsFieldsEnabled(true); 301 setOneCommitFieldsEnabled(false); 302 validateUserInput(); 303 } 304 305 private RevisionInterval getResortedRevisionInterval(SVNRevision revision1, SVNRevision revision2) { 306 RevisionInterval ret = new RevisionInterval (); 307 if(revision1.equals(SVNRevision.HEAD) && 308 revision1.equals(SVNRevision.HEAD)) 309 { 310 ret.startRevision = revision1; 311 ret.endRevision = revision2; 312 } else if (revision1.equals(SVNRevision.HEAD)) { 313 ret.startRevision = revision2; 314 ret.endRevision = revision1; 315 } else if (revision2.equals(SVNRevision.HEAD)) { 316 ret.startRevision = revision1; 317 ret.endRevision = revision2; 318 } else { 319 Long r1 = Long.parseLong(revision1.toString()); 320 Long r2 = Long.parseLong(revision2.toString()); 321 if(r1.compareTo(r2) < 0) { 322 ret.startRevision = revision1; 323 ret.endRevision = revision2; 324 } else { 325 ret.startRevision = revision2; 326 ret.endRevision = revision1; 327 } 328 } 329 return ret; 330 } 331 332 } 333 334 } 335 | Popular Tags |