001: /*
002: * HyperSearchOperationNode.java - Top result node of a HyperSearch request
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 1999, 2000, 2001, 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.search;
024:
025: //{{{ Imports
026: import java.io.File;
027: import java.util.ArrayList;
028: import java.util.Enumeration;
029: import java.util.HashMap;
030: import java.util.List;
031: import java.util.Map;
032:
033: import javax.swing.JTree;
034: import javax.swing.tree.DefaultMutableTreeNode;
035: import javax.swing.tree.DefaultTreeModel;
036: import javax.swing.tree.MutableTreeNode;
037: import javax.swing.tree.TreePath;
038:
039: //}}}
040:
041: /**
042: * @author Slava Pestov
043: * @version $Id: HyperSearchOperationNode.java 9522 2007-05-10 09:53:53Z kpouer $
044: */
045: public class HyperSearchOperationNode {
046: private boolean treeViewDisplayed;
047: private final String searchString;
048: private List<DefaultMutableTreeNode> resultNodes;
049: private SearchMatcher searchMatcher;
050:
051: //{{{ HyperSearchOperationNode constructor
052: public HyperSearchOperationNode(String searchString,
053: SearchMatcher searchMatcher) {
054: this .searchString = searchString;
055: this .searchMatcher = searchMatcher;
056: }//}}}
057:
058: //{{{ toString() method
059: public String toString() {
060: return searchString;
061: }//}}}
062:
063: //{{{ isTreeViewDisplayed() method
064: public boolean isTreeViewDisplayed() {
065: return treeViewDisplayed;
066: }//}}}
067:
068: //{{{ setTreeViewDisplayed() method
069: public void setTreeViewDisplayed(boolean treeViewDisplayed) {
070: this .treeViewDisplayed = treeViewDisplayed;
071: }//}}}
072:
073: //{{{ restoreFlatNodes() method
074: public void restoreFlatNodes(JTree resultTree,
075: DefaultMutableTreeNode operNode) {
076: for (int i = 0; i < resultNodes.size(); i++) {
077: DefaultMutableTreeNode element = resultNodes.get(i);
078: if (element.getUserObject() instanceof HyperSearchFileNode)
079: ((HyperSearchFileNode) element.getUserObject()).showFullPath = true;
080:
081: operNode.insert(element, operNode.getChildCount());
082: }
083:
084: ((DefaultTreeModel) resultTree.getModel())
085: .nodeStructureChanged(operNode);
086:
087: for (Enumeration e = operNode.children(); e.hasMoreElements();) {
088: DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
089: .nextElement();
090: resultTree.expandPath(new TreePath(node.getPath()));
091: }
092: resultTree
093: .scrollPathToVisible(new TreePath(operNode.getPath()));
094: }//}}}
095:
096: //{{{ cacheFlatNodes() method
097: public void cacheResultNodes(DefaultMutableTreeNode operNode) {
098: resultNodes = new ArrayList<DefaultMutableTreeNode>(operNode
099: .getChildCount());
100: for (Enumeration e = operNode.children(); e.hasMoreElements();)
101: resultNodes.add((DefaultMutableTreeNode) e.nextElement());
102: }//}}}
103:
104: //{{{ removeNodeFromCache() method
105: public static void removeNodeFromCache(MutableTreeNode mnode) {
106: DefaultMutableTreeNode node = (DefaultMutableTreeNode) mnode;
107: if (node.getUserObject() instanceof HyperSearchOperationNode)
108: return;
109:
110: DefaultMutableTreeNode tmpNode = node;
111: while ((tmpNode = (DefaultMutableTreeNode) tmpNode.getParent()) != null) {
112: if (!(tmpNode.getUserObject() instanceof HyperSearchOperationNode))
113: continue;
114: HyperSearchOperationNode operNode = (HyperSearchOperationNode) tmpNode
115: .getUserObject();
116: if (operNode.resultNodes != null) {
117: // the nodes aren't cached so no need to remove the node from cache
118: operNode.resultNodes.remove(node);
119: }
120: break;
121: }
122:
123: }//}}}
124:
125: //{{{ insertTreeNodes() method
126: public void insertTreeNodes(JTree resultTree,
127: DefaultMutableTreeNode operNode) {
128: String fileSep = System.getProperty("file.separator");
129: String fileSepRegex = System.getProperty("file.separator");
130: if (fileSep.equals("\\"))
131: fileSepRegex = "\\\\";
132:
133: //find the highest level common path
134: String[] topPathTmp = null;
135: int topPathNdx = -1;
136:
137: for (int i = 0; i < resultNodes.size(); i++) {
138: DefaultMutableTreeNode fileTreeNode = resultNodes.get(i);
139: Object obj = fileTreeNode.getUserObject();
140: if (!(obj instanceof HyperSearchFileNode))
141: continue;
142: HyperSearchFileNode fileNode = (HyperSearchFileNode) obj;
143:
144: int pos = fileNode.path.lastIndexOf(fileSep);
145: String pathName = fileNode.path.substring(0, pos);
146: String[] paths = pathName.split(fileSepRegex);
147: if (topPathNdx == -1) {
148: topPathNdx = paths.length;
149: topPathTmp = paths;
150: } else if (paths.length < topPathNdx) {
151: topPathNdx = paths.length;
152: topPathTmp = paths;
153: } else {
154: for (int ndx = 0; ndx < topPathNdx; ndx++) {
155: if (!paths[ndx].equals(topPathTmp[ndx])) {
156: topPathNdx = ndx;
157: break;
158: }
159: }
160: }
161: }
162: String[] topPath = new String[topPathNdx];
163: String topPathPath = "";
164: for (int ndx = 0; ndx < topPathNdx; ndx++) {
165: topPath[ndx] = topPathTmp[ndx];
166: topPathPath = topPathPath.concat(topPath[ndx] + fileSep);
167: }
168: Map<String, DefaultMutableTreeNode> treeNodes = new HashMap<String, DefaultMutableTreeNode>();
169: HyperSearchFolderNode folderNode = new HyperSearchFolderNode(
170: new File(topPathPath), true);
171: DefaultMutableTreeNode folderTreeNode = new DefaultMutableTreeNode(
172: folderNode);
173: operNode.insert(folderTreeNode, operNode.getChildCount());
174: treeNodes.put(topPathPath, folderTreeNode);
175:
176: for (int i = 0; i < resultNodes.size(); i++) {
177: DefaultMutableTreeNode fileTreeNode = resultNodes.get(i);
178: Object obj = fileTreeNode.getUserObject();
179: if (!(obj instanceof HyperSearchFileNode))
180: continue;
181: HyperSearchFileNode fileNode = (HyperSearchFileNode) obj;
182:
183: fileNode.showFullPath = false;
184: int pos = fileNode.path.lastIndexOf(fileSep);
185: String pathName = fileNode.path.substring(0, pos);
186: String[] paths = pathName.split(fileSepRegex);
187:
188: DefaultMutableTreeNode insNode = folderTreeNode;
189: String partialPath = topPathPath;
190: for (int ndx = topPathNdx; ndx < paths.length; ndx++) {
191: partialPath = partialPath.concat(paths[ndx] + fileSep);
192: DefaultMutableTreeNode tmpNode = treeNodes
193: .get(partialPath);
194: if (tmpNode == null) {
195: HyperSearchFolderNode tmpFolderNode = new HyperSearchFolderNode(
196: new File(partialPath), false);
197: tmpNode = new DefaultMutableTreeNode(tmpFolderNode);
198: insNode.insert(tmpNode, insNode.getChildCount());
199: treeNodes.put(partialPath, tmpNode);
200: }
201: insNode = tmpNode;
202: }
203: insNode.insert(fileTreeNode, insNode.getChildCount());
204: treeNodes.put(fileNode.path, insNode);
205: }
206:
207: }//}}}
208:
209: //{{{ getSearchMatcher() method
210: SearchMatcher getSearchMatcher() {
211: return searchMatcher;
212: }//}}}
213: }
|