/plugins/SVNPlugin/tags/0.6.0/src/ise/plugin/svn/pv/SVNAction.java

# · Java · 193 lines · 116 code · 20 blank · 57 comment · 21 complexity · cfc13d8821c3df89b1617adf46a14552 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.pv;
  26. import java.awt.event.*;
  27. import java.util.*;
  28. import javax.swing.*;
  29. import javax.swing.tree.TreePath;
  30. import projectviewer.action.Action;
  31. import projectviewer.vpt.VPTNode;
  32. import ise.plugin.svn.PVHelper;
  33. import ise.plugin.svn.command.*;
  34. import ise.plugin.svn.library.PasswordHandler;
  35. import org.gjt.sp.jedit.jEdit;
  36. import org.gjt.sp.jedit.View;
  37. /**
  38. * ProjectViewer Action to be added to the PV context menu. This class serves
  39. * as the menu for a pull-out menu containing the subversion commands.
  40. */
  41. public class SVNAction extends projectviewer.action.Action {
  42. public final static String PREFIX = "ise.plugin.svn.pv.";
  43. private JMenu menu = null;
  44. public SVNAction() {
  45. // set up the menu to be added to Project Viewer's context menu
  46. menu = new JMenu( "Subversion" );
  47. // Each subversion command to be added to the context
  48. // menu has 2 properties, a label and a command. The properties have a numeric
  49. // suffix, the commands are added to the menu in the order of the suffix. The
  50. // label property is the displayed name of the command, e.g. "Commit", and the
  51. // code property is the fully qualified classname of a class in the
  52. // ise.plugin.svn.action package.
  53. String pbase = "ise.plugin.svn.action.";
  54. for ( int i = 1; i < 100; i++ ) {
  55. String label = jEdit.getProperty( pbase + "label." + i );
  56. if ( label == null ) {
  57. break;
  58. }
  59. if ( label.equals("-")) {
  60. menu.addSeparator();
  61. continue;
  62. }
  63. String classname = jEdit.getProperty( pbase + "code." + i );
  64. if ( classname == null ) {
  65. continue;
  66. }
  67. JMenuItem item = null;
  68. try {
  69. NodeActor action = ( NodeActor ) Class.forName( classname ).newInstance();
  70. item = new JMenuItem( label );
  71. item.addActionListener( ( ActionListener ) action );
  72. menu.add( item );
  73. }
  74. catch ( Exception e ) {
  75. // class not found or instantiation exception, don't worry
  76. // about it, assume it's a typo
  77. e.printStackTrace();
  78. continue;
  79. }
  80. }
  81. }
  82. // this will be displayed in the PV context menu
  83. public String getText() {
  84. return "Subversion";
  85. }
  86. // returns the menu 'Subversion' with a pull-out submenu containing the
  87. // subversion commands.
  88. public JComponent getMenuItem() {
  89. return menu;
  90. }
  91. // called by ProjectViewer to let us know the currently selected node in
  92. // the PV tree. This method updates the various action listeners that
  93. // execute the subversion commands so they know the current node and can
  94. // act accordingly.
  95. public void prepareForNode( VPTNode node ) {
  96. View view = viewer.getView();
  97. String project_name = PVHelper.getProjectName( view );
  98. String project_root = PVHelper.getProjectRoot( view );
  99. String username = jEdit.getProperty( PREFIX + project_name + ".username" );
  100. String password = jEdit.getProperty( PREFIX + project_name + ".password" );
  101. if ( password != null && password.length() > 0 ) {
  102. try {
  103. PasswordHandler ph = new PasswordHandler();
  104. password = ph.decrypt( password );
  105. }
  106. catch ( Exception e ) {
  107. password = "";
  108. }
  109. }
  110. for ( int i = 0; i < menu.getItemCount(); i++ ) {
  111. try {
  112. JMenuItem actor = ( JMenuItem ) menu.getItem( i );
  113. if (actor == null) {
  114. continue;
  115. }
  116. ActionListener[] listeners = actor.getActionListeners();
  117. for ( ActionListener al : listeners ) {
  118. if ( al instanceof NodeActor ) {
  119. ( ( NodeActor ) al ).prepareForNode( getSelectedNodes(), view, project_root, username, password );
  120. }
  121. }
  122. }
  123. catch ( ClassCastException e ) {
  124. // ignored, move on
  125. }
  126. }
  127. }
  128. public void actionPerformed( ActionEvent ae ) {
  129. // does nothing, this is the top of a pull out menu so has no specific
  130. // action other than to display the pull out.
  131. }
  132. private List<VPTNode> getSelectedNodes() {
  133. List<VPTNode> list = new ArrayList<VPTNode>();
  134. JTree tree = viewer.getCurrentTree();
  135. switch ( tree.getSelectionCount() ) {
  136. case 0:
  137. // no Selection, shouldn't happen, but just in case...
  138. break;
  139. case 1: {
  140. // single selection
  141. list.add( ( VPTNode ) tree.getLastSelectedPathComponent() );
  142. break;
  143. }
  144. default: {
  145. list = getSelectedArtifacts( tree.getSelectionPaths() );
  146. break;
  147. }
  148. }
  149. return list;
  150. }
  151. /**
  152. * Receives a collection of TreePath objects and returns the underlying
  153. * objects selected, removing a child when its parent has also been
  154. * selected.
  155. */
  156. private List<VPTNode> getSelectedArtifacts( TreePath[] paths ) {
  157. TreePath last = null;
  158. List<VPTNode> objs = new ArrayList<VPTNode>();
  159. for ( int i = 0; i < paths.length; i++ ) {
  160. if ( last != null && !last.isDescendant( paths[ i ] ) ) {
  161. last = null;
  162. }
  163. if ( last == null ) {
  164. last = paths[ i ];
  165. objs.add( ( VPTNode ) paths[ i ].getLastPathComponent() );
  166. }
  167. }
  168. return objs;
  169. }
  170. }