/plugins/ProjectViewer/tags/pv_2_1_3_0/projectviewer/action/SearchAction.java

# · Java · 154 lines · 81 code · 19 blank · 54 comment · 21 complexity · 742796fdf54795f6527ead054e1f0797 MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer.action;
  20. //{{{ Imports
  21. import java.util.HashSet;
  22. import java.util.Enumeration;
  23. import java.awt.Component;
  24. import java.awt.event.ActionEvent;
  25. import javax.swing.JMenuItem;
  26. import javax.swing.JOptionPane;
  27. import org.gjt.sp.jedit.jEdit;
  28. import org.gjt.sp.jedit.search.SearchDialog;
  29. import org.gjt.sp.jedit.search.DirectoryListSet;
  30. import org.gjt.sp.jedit.search.SearchAndReplace;
  31. import projectviewer.ProjectViewer;
  32. import projectviewer.vpt.VPTNode;
  33. //}}}
  34. /**
  35. * Opens the search dialog for the selected directory/project.
  36. *
  37. * @author Marcelo Vanzin
  38. * @version $Id: SearchAction.java 6333 2005-02-10 06:31:59Z vanza $
  39. */
  40. public class SearchAction extends Action {
  41. //{{{ +SearchAction() : <init>
  42. public SearchAction() {
  43. super("projectviewer_wrapper_search");
  44. } //}}}
  45. //{{{ +getText() : String
  46. /** Returns the text to be shown on the button and/or menu item. */
  47. public String getText() {
  48. return jEdit.getProperty("projectviewer.action.hypersearch");
  49. } //}}}
  50. //{{{ +actionPerformed(ActionEvent) : void
  51. /** Creates a new project. */
  52. public void actionPerformed(ActionEvent e) {
  53. VPTNode node = null;
  54. if (viewer != null) {
  55. node = viewer.getSelectedNode();
  56. }
  57. if (node == null) {
  58. node = ProjectViewer.getActiveProject(jEdit.getActiveView());
  59. }
  60. if (node != null) {
  61. SearchAndReplace.setSearchFileSet(new NodeFileSet(node));
  62. SearchDialog.showSearchDialog(jEdit.getActiveView(), null, SearchDialog.DIRECTORY);
  63. } else {
  64. JOptionPane.showMessageDialog(
  65. (viewer != null) ? (Component) viewer : (Component) jEdit.getActiveView(),
  66. jEdit.getProperty("projectviewer.acrion.search.error"),
  67. jEdit.getProperty("projectviewer.error"),
  68. JOptionPane.ERROR_MESSAGE);
  69. }
  70. } //}}}
  71. //{{{ +prepareForNode(VPTNode) : void
  72. /** Enable action only for the root node. */
  73. public void prepareForNode(VPTNode node) {
  74. if (node != null && (node.isDirectory() || node.isProject())) {
  75. cmItem.setVisible(true);
  76. if (node.isDirectory()) {
  77. ((JMenuItem)cmItem).setText(
  78. jEdit.getProperty("projectviewer.action.hypersearch_dir"));
  79. } else {
  80. ((JMenuItem)cmItem).setText(
  81. jEdit.getProperty("projectviewer.action.hypersearch_project"));
  82. }
  83. } else {
  84. cmItem.setVisible(false);
  85. }
  86. } //}}}
  87. //{{{ +class _NodeFileSet_
  88. /**
  89. * Implements a SearchFileSet representing files that are children of a given
  90. * node and its children.
  91. *
  92. * @since PV 2.1.0 (was private before)
  93. */
  94. public static class NodeFileSet extends DirectoryListSet {
  95. //{{{ Private Members
  96. private VPTNode node;
  97. //}}}
  98. //{{{ +NodeFileSet(VPTNode) : <init>
  99. public NodeFileSet(VPTNode node) {
  100. super(null, "*", true);
  101. this.node = node;
  102. }
  103. //}}}
  104. //{{{ +getDirectory() : String
  105. /** Returns the path to the node. */
  106. public String getDirectory() {
  107. return node.getNodePath();
  108. } //}}}
  109. //{{{ #_getFiles(Component) : String[]
  110. /** Returns an array with the files to be searched. */
  111. protected String[] _getFiles(Component comp) {
  112. HashSet fileset = new HashSet();
  113. addFiles(node, fileset);
  114. return (String[]) fileset.toArray(new String[fileset.size()]);
  115. }
  116. //}}}
  117. //{{{ -addFiles(VPTNode, HashSet) : void
  118. /**
  119. * Adds all the files below the given node to the list of search files,
  120. * recursively.
  121. */
  122. private void addFiles(VPTNode node, HashSet fileset) {
  123. Enumeration e = node.children();
  124. if (e != null)
  125. while(e.hasMoreElements()) {
  126. VPTNode n = (VPTNode) e.nextElement();
  127. if (n.isFile()) {
  128. fileset.add(n.getNodePath());
  129. } else if (n.getAllowsChildren() && isRecursive()) {
  130. addFiles(n, fileset);
  131. }
  132. }
  133. } //}}}
  134. } //}}}
  135. }