KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > search > HyperSearchOperationNode


1 /*
2  * HyperSearchOperationNode.java - Top result node of a HyperSearch request
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998, 1999, 2000, 2001, 2002 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.search;
24
25 //{{{ Imports
26
import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.swing.JTree JavaDoc;
34 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
35 import javax.swing.tree.DefaultTreeModel JavaDoc;
36 import javax.swing.tree.MutableTreeNode JavaDoc;
37 import javax.swing.tree.TreePath JavaDoc;
38 //}}}
39

40 /**
41  * @author Slava Pestov
42  * @version $Id: HyperSearchOperationNode.java 8316 2007-01-06 18:13:27Z kpouer $
43  */

44 public class HyperSearchOperationNode
45 {
46     private boolean treeViewDisplayed;
47     private final String JavaDoc searchString;
48     private List JavaDoc<DefaultMutableTreeNode JavaDoc> resultNodes;
49     
50     //{{{ HyperSearchOperationNode constructor
51
public HyperSearchOperationNode(String JavaDoc searchString)
52     {
53         this.searchString = searchString;
54     }//}}}
55

56     //{{{ toString() method
57
public String JavaDoc toString()
58     {
59         return searchString;
60     }//}}}
61

62     //{{{ isTreeViewDisplayed() method
63
public boolean isTreeViewDisplayed()
64     {
65         return treeViewDisplayed;
66     }//}}}
67

68     //{{{ setTreeViewDisplayed() method
69
public void setTreeViewDisplayed(boolean treeViewDisplayed)
70     {
71         this.treeViewDisplayed = treeViewDisplayed;
72     }//}}}
73

74     //{{{ restoreFlatNodes() method
75
public void restoreFlatNodes(JTree JavaDoc resultTree, DefaultMutableTreeNode JavaDoc operNode)
76     {
77         for (int i = 0; i < resultNodes.size(); i++)
78         {
79             DefaultMutableTreeNode JavaDoc element = resultNodes.get(i);
80             if (element.getUserObject() instanceof HyperSearchFileNode)
81                 ((HyperSearchFileNode)element.getUserObject()).showFullPath = true;
82
83             operNode.insert(element, operNode.getChildCount());
84         }
85
86         ((DefaultTreeModel JavaDoc)resultTree.getModel()).nodeStructureChanged(operNode);
87         
88         for (Enumeration JavaDoc e = operNode.children(); e.hasMoreElements();)
89         {
90             DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc)e.nextElement();
91             resultTree.expandPath(new TreePath JavaDoc(node.getPath()));
92         }
93         resultTree.scrollPathToVisible(
94             new TreePath JavaDoc(operNode.getPath()));
95     }//}}}
96

97     //{{{ cacheFlatNodes() method
98
public void cacheResultNodes(DefaultMutableTreeNode JavaDoc operNode)
99     {
100         resultNodes = new ArrayList JavaDoc<DefaultMutableTreeNode JavaDoc>(operNode.getChildCount());
101         for (Enumeration JavaDoc e = operNode.children(); e.hasMoreElements();)
102             resultNodes.add((DefaultMutableTreeNode JavaDoc) e.nextElement());
103     }//}}}
104

105     //{{{ removeNodeFromCache() method
106
public static void removeNodeFromCache(MutableTreeNode JavaDoc mnode)
107     {
108         DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc)mnode;
109         if (node.getUserObject() instanceof HyperSearchOperationNode)
110             return;
111         
112         DefaultMutableTreeNode JavaDoc tmpNode = node;
113         while ((tmpNode = (DefaultMutableTreeNode JavaDoc) tmpNode.getParent()) != null)
114         {
115             if (!(tmpNode.getUserObject() instanceof HyperSearchOperationNode))
116                 continue;
117             HyperSearchOperationNode operNode = (HyperSearchOperationNode) tmpNode.getUserObject();
118             if (operNode.resultNodes != null)
119             {
120                 // the nodes aren't cached so no need to remove the node from cache
121
operNode.resultNodes.remove(node);
122             }
123             break;
124         }
125         
126     }//}}}
127

128     //{{{ insertTreeNodes() method
129
public void insertTreeNodes(JTree JavaDoc resultTree, DefaultMutableTreeNode JavaDoc operNode)
130     {
131         String JavaDoc fileSep = System.getProperty("file.separator");
132         String JavaDoc fileSepRegex = System.getProperty("file.separator");
133         if (fileSep.equals("\\"))
134             fileSepRegex = "\\\\";
135         
136         //find the highest level common path
137
String JavaDoc[] topPathTmp = null;
138         int topPathNdx = -1;
139
140         for (int i = 0;i < resultNodes.size();i++)
141         {
142             DefaultMutableTreeNode JavaDoc fileTreeNode = resultNodes.get(i);
143             Object JavaDoc obj = fileTreeNode.getUserObject();
144             if (!(obj instanceof HyperSearchFileNode))
145                 continue;
146             HyperSearchFileNode fileNode = (HyperSearchFileNode)obj;
147
148             int pos = fileNode.path.lastIndexOf(fileSep);
149             String JavaDoc pathName = fileNode.path.substring(0, pos);
150             String JavaDoc[] paths = pathName.split(fileSepRegex);
151             if (topPathNdx == -1)
152             {
153                 topPathNdx = paths.length;
154                 topPathTmp = paths;
155             }
156             else if (paths.length < topPathNdx)
157             {
158                 topPathNdx = paths.length;
159                 topPathTmp = paths;
160             }
161             else
162             {
163                 for (int ndx =0 ; ndx < topPathNdx; ndx++)
164                 {
165                     if (!paths[ndx].equals(topPathTmp[ndx]))
166                     {
167                         topPathNdx = ndx;
168                         break;
169                     }
170                 }
171             }
172         }
173         String JavaDoc[] topPath = new String JavaDoc[topPathNdx];
174         String JavaDoc topPathPath = "";
175         for (int ndx = 0 ; ndx < topPathNdx; ndx++)
176         {
177             topPath[ndx] = topPathTmp[ndx];
178             topPathPath = topPathPath.concat(topPath[ndx] + fileSep);
179         }
180         Map JavaDoc<String JavaDoc, DefaultMutableTreeNode JavaDoc> treeNodes = new HashMap JavaDoc<String JavaDoc, DefaultMutableTreeNode JavaDoc>();
181         HyperSearchFolderNode folderNode =
182             new HyperSearchFolderNode(new File JavaDoc(topPathPath), true);
183         DefaultMutableTreeNode JavaDoc folderTreeNode = new DefaultMutableTreeNode JavaDoc(folderNode);
184         operNode.insert(folderTreeNode, operNode.getChildCount());
185         treeNodes.put(topPathPath, folderTreeNode);
186         
187         for (int i = 0;i < resultNodes.size();i++)
188         {
189             DefaultMutableTreeNode JavaDoc fileTreeNode = resultNodes.get(i);
190             Object JavaDoc obj = fileTreeNode.getUserObject();
191             if (!(obj instanceof HyperSearchFileNode))
192                 continue;
193             HyperSearchFileNode fileNode = (HyperSearchFileNode)obj;
194
195             fileNode.showFullPath = false;
196             int pos = fileNode.path.lastIndexOf(fileSep);
197             String JavaDoc pathName = fileNode.path.substring(0, pos);
198             String JavaDoc[] paths = pathName.split(fileSepRegex);
199             
200             DefaultMutableTreeNode JavaDoc insNode = folderTreeNode;
201             String JavaDoc partialPath = topPathPath;
202             for (int ndx = topPathNdx; ndx < paths.length; ndx++)
203             {
204                 partialPath = partialPath.concat(paths[ndx] + fileSep);
205                 DefaultMutableTreeNode JavaDoc tmpNode = treeNodes.get(partialPath);
206                 if (tmpNode == null)
207                 {
208                     HyperSearchFolderNode tmpFolderNode =
209                         new HyperSearchFolderNode(new File JavaDoc(partialPath), false);
210                     tmpNode = new DefaultMutableTreeNode JavaDoc(tmpFolderNode);
211                     insNode.insert(tmpNode, insNode.getChildCount());
212                     treeNodes.put(partialPath, tmpNode);
213                 }
214                 insNode = tmpNode;
215             }
216             insNode.insert(fileTreeNode, insNode.getChildCount());
217             treeNodes.put(fileNode.path, insNode);
218         }
219         
220     }//}}}
221
}
222
Popular Tags