/bundles/plugins-trunk/SVNPlugin/src/ise/plugin/svn/action/CommitAction.java

# · Java · 200 lines · 137 code · 22 blank · 41 comment · 27 complexity · a1df7ee7ce7e499685fbb9886f473a3b 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 ise.plugin.svn.gui.OutputPanel;
  27. import ise.plugin.svn.SVNPlugin;
  28. import ise.plugin.svn.command.Commit;
  29. import ise.plugin.svn.data.CommitData;
  30. import ise.plugin.svn.gui.CommitDialog;
  31. import ise.plugin.svn.gui.CommitResultsPanel;
  32. import ise.plugin.svn.io.ConsolePrintStream;
  33. import ise.plugin.svn.library.GUIUtils;
  34. import common.swingworker.*;
  35. import java.awt.event.ActionEvent;
  36. import java.io.File;
  37. import java.util.*;
  38. import java.util.logging.*;
  39. import javax.swing.JPanel;
  40. import javax.swing.JOptionPane;
  41. import org.gjt.sp.jedit.View;
  42. import org.gjt.sp.jedit.jEdit;
  43. import org.gjt.sp.jedit.Buffer;
  44. import org.tmatesoft.svn.core.wc.*;
  45. /**
  46. * ActionListener to perform an svn commit.
  47. * This is not dependent on ProjectViewer.
  48. */
  49. public class CommitAction extends SVNAction {
  50. private CommitDialog dialog = null;
  51. private TreeMap<String, String> paths = null; // <path, status>, where status is added, modified, etc
  52. /**
  53. * @param view the View in which to display results
  54. * @param paths a list of paths to be added
  55. * @param username the username for the svn repository
  56. * @param password the password for the username
  57. */
  58. public CommitAction( View view, TreeMap<String, String> paths, String username, String password ) {
  59. super( view, jEdit.getProperty( "ips.Commit", "Commit" ) );
  60. if ( paths == null )
  61. throw new IllegalArgumentException( "paths may not be null" );
  62. this.paths = paths;
  63. setUsername( username );
  64. setPassword( password );
  65. }
  66. public void actionPerformed( ActionEvent ae ) {
  67. if ( paths != null && paths.size() > 0 ) {
  68. // check for /tag/ and warn user if it appears they are
  69. // trying to commit to a tag directory
  70. List<String> possible_tags = new ArrayList<String>();
  71. SVNWCClient client = SVNClientManager.newInstance().getWCClient();
  72. Set<String> keys = paths.keySet();
  73. for ( String path : keys ) {
  74. try {
  75. SVNInfo info = client.doInfo( new File( path ), SVNRevision.WORKING );
  76. if ( info != null && info.getURL().toString().indexOf( "/tags/" ) > -1 ) {
  77. possible_tags.add( path );
  78. }
  79. }
  80. catch ( Exception e ) {
  81. e.printStackTrace();
  82. }
  83. }
  84. if ( possible_tags != null && possible_tags.size() > 0 ) {
  85. StringBuffer msg = new StringBuffer();
  86. msg.append( "It appears you may be attempting to commit some files to a tag:\n\n" );
  87. for ( String path : possible_tags ) {
  88. msg.append( path ).append( "\n" );
  89. }
  90. msg.append( "\n" );
  91. msg.append( "Are you sure you want to commit these files?" );
  92. int no = JOptionPane.showConfirmDialog( getView(), msg, jEdit.getProperty( "ips.Confirm_Commit", "Confirm Commit" ), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE );
  93. if ( no == JOptionPane.NO_OPTION ) {
  94. return ;
  95. }
  96. }
  97. dialog = new CommitDialog( getView(), paths, false );
  98. GUIUtils.center( getView(), dialog );
  99. dialog.setVisible( true );
  100. final CommitData data = dialog.getCommitData();
  101. if ( data == null ) {
  102. return ; // null means user canceled
  103. }
  104. if ( getUsername() == null ) {
  105. verifyLogin( ( String ) paths.firstKey() );
  106. if ( isCanceled() ) {
  107. return ;
  108. }
  109. }
  110. data.setUsername( getUsername() );
  111. data.setPassword( getPassword() );
  112. data.setOut( new ConsolePrintStream( getView() ) );
  113. getView().getDockableWindowManager().showDockableWindow( "subversion" );
  114. final OutputPanel panel = SVNPlugin.getOutputPanel( getView() );
  115. panel.showConsole();
  116. final Logger logger = panel.getLogger();
  117. logger.log( Level.INFO, jEdit.getProperty( "ips.Committing_...", "Committing ..." ) );
  118. for ( Handler handler : logger.getHandlers() ) {
  119. handler.flush();
  120. }
  121. class Runner extends SwingWorker<CommitData, Object> {
  122. @Override
  123. public CommitData doInBackground() {
  124. try {
  125. Commit commit = new Commit( );
  126. return commit.commit( data );
  127. }
  128. catch ( Exception e ) {
  129. data.getOut().printError( e.getMessage() );
  130. }
  131. finally {
  132. data.getOut().close();
  133. }
  134. return null;
  135. }
  136. @Override
  137. public boolean cancel( boolean mayInterruptIfRunning ) {
  138. boolean cancelled = super.cancel( mayInterruptIfRunning );
  139. if ( cancelled ) {
  140. data.getOut().printError( "Stopped 'Commit' action." );
  141. data.getOut().close();
  142. }
  143. else {
  144. data.getOut().printError( "Unable to stop 'Commit' action." );
  145. }
  146. return cancelled;
  147. }
  148. @Override
  149. protected void done() {
  150. if ( isCancelled() ) {
  151. return ;
  152. }
  153. try {
  154. JPanel results_panel = new CommitResultsPanel( get() );
  155. panel.addTab( jEdit.getProperty( "ips.Commit", "Commit" ), results_panel );
  156. // fix for 2081908
  157. for ( String path : paths.keySet() ) {
  158. updateStatus(path);
  159. Buffer buffer = jEdit.getBuffer( path );
  160. if ( buffer != null ) {
  161. buffer.reload( getView() );
  162. }
  163. }
  164. }
  165. catch ( Exception e ) { // NOPMD
  166. // ignored
  167. }
  168. }
  169. }
  170. Runner runner = new Runner();
  171. panel.addWorker( "Commit", runner );
  172. runner.execute();
  173. }
  174. }
  175. }