/plugins/SVNPlugin/tags/1.6.0/src/ise/plugin/svn/action/RevertAction.java

# · Java · 191 lines · 129 code · 20 blank · 42 comment · 27 complexity · f60045516e6fa34d6c8256a98b8b8d79 MD5 · raw file

  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn.action;
  26. import java.awt.event.ActionEvent;
  27. import java.io.*;
  28. import java.util.*;
  29. import java.util.logging.*;
  30. import javax.swing.JPanel;
  31. import javax.swing.JOptionPane;
  32. import ise.plugin.svn.gui.OutputPanel;
  33. import ise.plugin.svn.SVNPlugin;
  34. import ise.plugin.svn.command.Revert;
  35. import ise.plugin.svn.data.SVNData;
  36. import ise.plugin.svn.data.AddResults;
  37. import common.swingworker.*;
  38. import ise.plugin.svn.gui.AddResultsPanel;
  39. import ise.plugin.svn.io.ConsolePrintStream;
  40. import org.gjt.sp.jedit.Buffer;
  41. import org.gjt.sp.jedit.jEdit;
  42. import org.gjt.sp.jedit.View;
  43. /**
  44. * ActionListener to perform an svn revert.
  45. * This is not dependent on ProjectViewer.
  46. */
  47. public class RevertAction extends SVNAction {
  48. private List<String> paths = null;
  49. /**
  50. * @param view the View in which to display results
  51. * @param paths a list of paths to be added
  52. * @param username the username for the svn repository
  53. * @param password the password for the username
  54. */
  55. public RevertAction( View view, List<String> paths, String username, String password ) {
  56. super( view, jEdit.getProperty( "ips.Revert", "Revert" ) );
  57. if ( paths == null )
  58. throw new IllegalArgumentException( "paths may not be null" );
  59. this.paths = paths;
  60. setUsername( username );
  61. setPassword( password );
  62. }
  63. public void actionPerformed( ActionEvent ae ) {
  64. if ( paths != null && paths.size() > 0 ) {
  65. final SVNData data = new SVNData();
  66. // get the paths
  67. boolean recursive = false;
  68. for ( String path : paths ) {
  69. if ( path != null ) {
  70. File file = new File( path );
  71. if ( file.isDirectory() ) {
  72. recursive = true;
  73. }
  74. }
  75. }
  76. // user confirmations
  77. if ( recursive ) {
  78. // have the user verify they want a recursive revert
  79. int response = JOptionPane.showConfirmDialog( getView(), jEdit.getProperty( "ips.Recursively_revert_all_files_in_selected_directories?", "Recursively revert all files in selected directories?" ), jEdit.getProperty( "ips.Recursive_Revert?", "Recursive Revert?" ), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE );
  80. if ( response == JOptionPane.CANCEL_OPTION ) {
  81. return ;
  82. }
  83. recursive = response == JOptionPane.YES_OPTION;
  84. }
  85. else {
  86. // have the user confirm they really want to revert
  87. int response = JOptionPane.showConfirmDialog( getView(), jEdit.getProperty( "ips.Revert_selected_files?", "Revert selected files?" ), jEdit.getProperty( "ips.Confirm_Revert", "Confirm Revert" ), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE );
  88. if ( response == JOptionPane.NO_OPTION ) {
  89. return ;
  90. }
  91. }
  92. data.setRecursive( recursive );
  93. data.setPaths( paths );
  94. if ( getUsername() == null ) {
  95. verifyLogin( paths.get( 0 ) );
  96. if ( isCanceled() ) {
  97. return ;
  98. }
  99. data.setUsername( getUsername() );
  100. data.setPassword( getPassword() );
  101. }
  102. else {
  103. setUsername( data.getUsername() );
  104. setPassword( data.getPassword() );
  105. }
  106. data.setOut( new ConsolePrintStream( getView() ) );
  107. getView().getDockableWindowManager().showDockableWindow( "subversion" );
  108. final OutputPanel panel = SVNPlugin.getOutputPanel( getView() );
  109. panel.showConsole();
  110. Logger logger = panel.getLogger();
  111. logger.log( Level.INFO, jEdit.getProperty( "ips.Reverting_...", "Reverting ..." ) );
  112. for ( Handler handler : logger.getHandlers() ) {
  113. handler.flush();
  114. }
  115. class Runner extends SwingWorker<AddResults, Object> {
  116. @Override
  117. public AddResults doInBackground() {
  118. try {
  119. Revert revert = new Revert();
  120. return revert.revert( data );
  121. }
  122. catch ( Exception e ) {
  123. data.getOut().printError( e.getMessage() );
  124. }
  125. finally {
  126. data.getOut().close();
  127. }
  128. return null;
  129. }
  130. @Override
  131. public boolean cancel( boolean mayInterruptIfRunning ) {
  132. boolean cancelled = super.cancel( mayInterruptIfRunning );
  133. if ( cancelled ) {
  134. data.getOut().printError( "Stopped 'Revert' action." );
  135. data.getOut().close();
  136. }
  137. else {
  138. data.getOut().printError( "Unable to stop 'Revert' action." );
  139. }
  140. return cancelled;
  141. }
  142. @Override
  143. protected void done() {
  144. if ( isCancelled() ) {
  145. return ;
  146. }
  147. try {
  148. AddResults results = get();
  149. JPanel results_panel = new AddResultsPanel( results, AddResultsPanel.REVERT, getView(), getUsername(), getPassword() );
  150. panel.addTab( jEdit.getProperty( "ips.Revert", "Revert" ), results_panel );
  151. for ( String path : results.getPaths() ) {
  152. Buffer buffer = jEdit.getBuffer( path );
  153. if ( buffer != null ) {
  154. buffer.reload( RevertAction.this.getView() );
  155. }
  156. }
  157. }
  158. catch ( Exception e ) { // NOPMD
  159. // ignored
  160. }
  161. }
  162. }
  163. Runner runner = new Runner();
  164. panel.addWorker( "Revert", runner );
  165. runner.execute();
  166. }
  167. }
  168. }