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

#
Java | 218 lines | 136 code | 27 blank | 55 comment | 39 complexity | 174f52caaf18f76e8a0d913d896653ef MD5 | raw file

✨ Summary
  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.io.FileInputStream;
  22. import java.io.InputStream;
  23. import java.io.IOException;
  24. import java.io.Reader;
  25. import java.util.HashSet;
  26. import java.util.Enumeration;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29. import java.awt.Component;
  30. import java.awt.event.ActionEvent;
  31. import javax.swing.JMenuItem;
  32. import javax.swing.JOptionPane;
  33. import org.gjt.sp.jedit.jEdit;
  34. import org.gjt.sp.jedit.MiscUtilities;
  35. import org.gjt.sp.jedit.search.SearchDialog;
  36. import org.gjt.sp.jedit.search.DirectoryListSet;
  37. import org.gjt.sp.jedit.search.SearchAndReplace;
  38. import org.gjt.sp.util.Log;
  39. import projectviewer.ProjectManager;
  40. import projectviewer.ProjectViewer;
  41. import projectviewer.config.ProjectViewerConfig;
  42. import projectviewer.vpt.VPTFile;
  43. import projectviewer.vpt.VPTNode;
  44. //}}}
  45. /**
  46. * Opens the search dialog for the selected directory/project.
  47. *
  48. * @author Marcelo Vanzin
  49. * @version $Id: SearchAction.java 8933 2007-02-09 07:59:29Z vanza $
  50. */
  51. public class SearchAction extends Action {
  52. //{{{ +SearchAction() : <init>
  53. public SearchAction() {
  54. super("projectviewer_wrapper_search");
  55. } //}}}
  56. //{{{ +getText() : String
  57. /** Returns the text to be shown on the button and/or menu item. */
  58. public String getText() {
  59. return jEdit.getProperty("projectviewer.action.hypersearch");
  60. } //}}}
  61. //{{{ +actionPerformed(ActionEvent) : void
  62. /** Creates a new project. */
  63. public void actionPerformed(ActionEvent e) {
  64. VPTNode node = null;
  65. if (viewer != null) {
  66. node = viewer.getSelectedNode();
  67. }
  68. if (node == null) {
  69. node = ProjectViewer.getActiveProject(jEdit.getActiveView());
  70. }
  71. if (node != null) {
  72. if (node.isProject()) {
  73. ProjectManager mgr = ProjectManager.getInstance();
  74. if (!mgr.isLoaded(node.getName())) {
  75. node = mgr.getProject(node.getName());
  76. }
  77. } else if (node.isLeaf()) {
  78. node = (VPTNode) node.getParent();
  79. }
  80. SearchAndReplace.setSearchFileSet(new NodeFileSet(node));
  81. SearchDialog.showSearchDialog(jEdit.getActiveView(), null, SearchDialog.DIRECTORY);
  82. } else {
  83. JOptionPane.showMessageDialog(
  84. (viewer != null) ? (Component) viewer : (Component) jEdit.getActiveView(),
  85. jEdit.getProperty("projectviewer.action.search.error"),
  86. jEdit.getProperty("projectviewer.error"),
  87. JOptionPane.ERROR_MESSAGE);
  88. }
  89. } //}}}
  90. //{{{ +prepareForNode(VPTNode) : void
  91. /** Enable action only for the root node. */
  92. public void prepareForNode(VPTNode node) {
  93. cmItem.setVisible(true);
  94. if (node != null) {
  95. if (node.isDirectory()) {
  96. ((JMenuItem)cmItem).setText(
  97. jEdit.getProperty("projectviewer.action.hypersearch_dir"));
  98. } else if (node.isProject()) {
  99. ((JMenuItem)cmItem).setText(
  100. jEdit.getProperty("projectviewer.action.hypersearch_project"));
  101. } else {
  102. ((JMenuItem)cmItem).setText(
  103. jEdit.getProperty("projectviewer.action.hypersearch_parent"));
  104. }
  105. } else {
  106. cmItem.setVisible(false);
  107. }
  108. } //}}}
  109. //{{{ +class _NodeFileSet_
  110. /**
  111. * Implements a SearchFileSet representing files that are children of a given
  112. * node and its children.
  113. *
  114. * @since PV 2.1.0 (was private before)
  115. */
  116. public static class NodeFileSet extends DirectoryListSet {
  117. //{{{ Private Members
  118. private boolean skipBinary;
  119. private Pattern pFilter;
  120. private VPTNode node;
  121. //}}}
  122. //{{{ +NodeFileSet(VPTNode) : <init>
  123. public NodeFileSet(VPTNode node) {
  124. super(null, "*", true);
  125. this.node = node;
  126. this.skipBinary = jEdit.getBooleanProperty("search.skipBinary.toggle", false)
  127. && ProjectViewerConfig.getInstance().hasBinaryFileCheck();
  128. }
  129. //}}}
  130. //{{{ +getDirectory() : String
  131. /** Returns the path to the node. */
  132. public String getDirectory() {
  133. return node.getNodePath();
  134. } //}}}
  135. //{{{ #_getFiles(Component) : String[]
  136. /** Returns an array with the files to be searched. */
  137. protected String[] _getFiles(Component comp) {
  138. String filter = getFileFilter();
  139. if (filter != null && filter.length() > 0 && !filter.equals("*")) {
  140. pFilter = Pattern.compile(MiscUtilities.globToRE(filter));
  141. }
  142. HashSet fileset = new HashSet();
  143. addFiles(node, fileset);
  144. return (String[]) fileset.toArray(new String[fileset.size()]);
  145. }
  146. //}}}
  147. //{{{ -addFiles(VPTNode, HashSet) : void
  148. /**
  149. * Adds all the files below the given node to the list of search files,
  150. * recursively.
  151. */
  152. private void addFiles(VPTNode node, HashSet fileset) {
  153. Enumeration e = node.children();
  154. if (e != null)
  155. while(e.hasMoreElements()) {
  156. VPTNode n = (VPTNode) e.nextElement();
  157. if (n.isFile()) {
  158. if (pFilter != null &&
  159. !pFilter.matcher(n.getNodePath()).matches())
  160. {
  161. // filtered out.
  162. continue;
  163. }
  164. if (!((VPTFile)n).getFile().exists()) {
  165. continue;
  166. }
  167. if (skipBinary) {
  168. InputStream is = null;
  169. Reader r = null;
  170. try {
  171. is = new FileInputStream(n.getNodePath());
  172. r = MiscUtilities.autodetect(is, null);
  173. if (MiscUtilities.isBinary(r)) {
  174. continue;
  175. }
  176. } catch (IOException ioe) {
  177. Log.log(Log.ERROR, this, ioe);
  178. continue;
  179. } finally {
  180. if (r != null) try { r.close(); } catch (Exception ex) { }
  181. if (is != null) try { is.close(); } catch (Exception ex) { }
  182. }
  183. }
  184. fileset.add(n.getNodePath());
  185. } else if (n.getAllowsChildren() && isRecursive()) {
  186. addFiles(n, fileset);
  187. }
  188. }
  189. } //}}}
  190. } //}}}
  191. }