PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/soa.ui/src/org/netbeans/modules/soa/ui/tree/impl/TreeFinderProcessor.java

https://bitbucket.org/rsaqc/netbeans-soa
Java | 421 lines | 213 code | 21 blank | 187 comment | 50 complexity | fb089cd6574648c69cfd12c8faa11950 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.soa.ui.tree.impl;
  43. import java.util.ArrayList;
  44. import java.util.List;
  45. import java.util.Stack;
  46. import javax.swing.tree.TreePath;
  47. import org.netbeans.modules.soa.ui.tree.DataObjectHolder;
  48. import org.netbeans.modules.soa.ui.tree.ExtTreeModel;
  49. import org.netbeans.modules.soa.ui.tree.TreeItem;
  50. import org.netbeans.modules.soa.ui.tree.TreeItemFinder;
  51. import org.netbeans.modules.soa.ui.tree.TreeItemFinder.FindResult;
  52. /**
  53. * The processor, which looks for a tree item inside of TreeModel by
  54. * a list of finders. The result of looking for is a TreePath.
  55. *
  56. * @author nk160297
  57. */
  58. public class TreeFinderProcessor {
  59. private ExtTreeModel mTreeModel;
  60. public TreeFinderProcessor(ExtTreeModel treeModel) {
  61. mTreeModel = treeModel;
  62. }
  63. /**
  64. * Looks for the first node, which satisfies the search conditions,
  65. * which are specified by the finderList argument.
  66. * The finderList contains the list of TreeItemFinder objects which
  67. * have to be applied sequentially.
  68. *
  69. * @param helper
  70. * @return TreePath of the found tree item.
  71. */
  72. public TreePath findFirstNode(List<TreeItemFinder> finderList) {
  73. if (finderList == null || finderList.isEmpty()) {
  74. return null;
  75. }
  76. //
  77. Object rootNode = mTreeModel.getRoot();
  78. Stack<Object> locationStack = new Stack<Object>();
  79. locationStack.push(rootNode);
  80. //
  81. for (TreeItemFinder finder : finderList) {
  82. //
  83. boolean found = findFirstChild(locationStack, finder, -1);
  84. //
  85. if (!found) {
  86. return null;
  87. }
  88. }
  89. //
  90. TreePath result = new TreePath(locationStack.toArray());
  91. return result;
  92. }
  93. public boolean findFirstChild(Stack<Object> locationStack,
  94. TreeItemFinder finder, int maxDepth) {
  95. //
  96. Object parentNode = locationStack.peek();
  97. List<Object> children = mTreeModel.getChildren(parentNode);
  98. if (children != null && children.size() != 0) {
  99. maxDepth--;
  100. for (Object child : children) {
  101. locationStack.push(child);
  102. //
  103. if (checkNode(locationStack, finder, maxDepth)) {
  104. return true;
  105. }
  106. //
  107. locationStack.pop();
  108. }
  109. }
  110. //
  111. return false;
  112. }
  113. /**
  114. * Looks for the nodes, which satisfies the search conditions,
  115. * which are specified by the finderList argument.
  116. * Then the data object is taken from each node and collected in the
  117. * result list.
  118. *
  119. * @param list of finders
  120. * @return List of data objects of the found tree item.
  121. */
  122. public List<Object> findAllDataObjects(List<TreeItemFinder> finderList) {
  123. if (finderList == null || finderList.isEmpty()) {
  124. return null;
  125. }
  126. //
  127. ArrayList<List<Object>> foundLocations = new ArrayList<List<Object>>();
  128. //
  129. Object rootNode = mTreeModel.getRoot();
  130. Stack<Object> locationStack = new Stack<Object>();
  131. locationStack.push(rootNode);
  132. //
  133. for (TreeItemFinder finder : finderList) {
  134. fillNodesList(foundLocations, locationStack, finder, -1, false);
  135. }
  136. //
  137. ArrayList<Object> result = new ArrayList<Object>();
  138. for (List<Object> location : foundLocations) {
  139. Object tailObject = location.get(location.size() - 1);
  140. assert tailObject instanceof DataObjectHolder;
  141. Object dataObject = ((DataObjectHolder)tailObject).getDataObject();
  142. result.add(dataObject);
  143. }
  144. return result;
  145. }
  146. /**
  147. * Looks for the tree pathes which tail satisfies the search conditions,
  148. * which are specified by the finder.
  149. *
  150. * @param list of finders
  151. * @return List of tree items
  152. */
  153. public List<TreePath> findAllTreePaths(TreeItemFinder finder) {
  154. List<TreeItemFinder> finderList = new ArrayList<TreeItemFinder>(1);
  155. finderList.add(finder);
  156. return findAllTreePaths(finderList);
  157. }
  158. /**
  159. * Looks for the tree pathes which tail satisfies the search conditions,
  160. * which are specified by the finderList argument.
  161. *
  162. * @param list of finders
  163. * @return List of tree items
  164. */
  165. public List<TreePath> findAllTreePaths(List<TreeItemFinder> finderList) {
  166. if (finderList == null || finderList.isEmpty()) {
  167. return null;
  168. }
  169. //
  170. ArrayList<List<Object>> foundLocations = new ArrayList<List<Object>>();
  171. //
  172. Object rootNode = mTreeModel.getRoot();
  173. Stack<Object> locationStack = new Stack<Object>();
  174. locationStack.push(rootNode);
  175. //
  176. for (TreeItemFinder finder : finderList) {
  177. fillNodesList(foundLocations, locationStack, finder, -1, false);
  178. }
  179. //
  180. ArrayList<TreePath> result = new ArrayList<TreePath>();
  181. for (List<Object> location : foundLocations) {
  182. TreePath treePath = new TreePath(location.toArray());
  183. result.add(treePath);
  184. }
  185. return result;
  186. }
  187. /**
  188. * An auxiliary method is intended to help search nodes recursively.
  189. * The locationStack parameter specifies a chain of MapperTreeNode
  190. * objects, which points to the tree node, from which the searching
  191. * has to be started.
  192. * <p>
  193. * The finder parameter is an object which makes decision.
  194. * It has to be implemented externally.
  195. * <p>
  196. * The maxDepth parameter specifies the maximum depth do
  197. * which the recursive algorithm can go.
  198. * <p>
  199. * If it equals to -1, then infinite depth is emplied.
  200. * <p>
  201. * if it equals to 0 than it means that it only necessary to check
  202. * if the top node in the stack satisfies to the searching conditions.
  203. * <p>
  204. * if it equals to 1 than it means that searching is requested
  205. * only among direct children of the source node.
  206. */
  207. public boolean checkNode(Stack<Object> locationStack,
  208. TreeItemFinder finder, int maxDepth) {
  209. //
  210. Object parentNode = locationStack.peek();
  211. Object dataObject = getDataObject(parentNode);
  212. //
  213. FindResult fr = finder.process(dataObject, null);
  214. //
  215. if (fr.isFit()) {
  216. return true;
  217. }
  218. //
  219. if (maxDepth == 0) {
  220. return false;
  221. }
  222. //
  223. if (fr.drillDeeper()) {
  224. return findFirstChild(locationStack, finder, maxDepth);
  225. }
  226. return false;
  227. }
  228. /**
  229. * Takes a tree item and returns an associated data object.
  230. * The data objec here is the object, which will be analysed by finders.
  231. * This method is intended to be overriden if necessary.
  232. * Now it has the default implementation.
  233. *
  234. * @param treeItem
  235. * @return
  236. */
  237. protected Object getDataObject(Object treeItem) {
  238. if (treeItem instanceof DataObjectHolder) {
  239. return ((DataObjectHolder)treeItem).getDataObject();
  240. } else {
  241. return treeItem;
  242. }
  243. }
  244. //-------------------------------------------------------------------------
  245. /**
  246. * An auxiliary method is intended to help search nodes recursively.
  247. * See description of the findFirstNode method.
  248. * Unlike the findFirstNode it can find more then one node.
  249. */
  250. public void fillNodesList(
  251. List<List<Object>> foundLocationsList,
  252. Stack<Object> locationStack,
  253. TreeItemFinder finder,
  254. int maxDepth,
  255. boolean lookDeeperIfFound) {
  256. //
  257. Object parentNode = locationStack.peek();
  258. Object dataObject = getDataObject(parentNode);
  259. //
  260. FindResult fr = finder.process(dataObject, null);
  261. //
  262. if (fr.isFit()) {
  263. // Copy location stack content to separate list and save it to result list.
  264. ArrayList<Object> foundLocation =
  265. new ArrayList<Object>(locationStack);
  266. foundLocationsList.add(foundLocation);
  267. if (!lookDeeperIfFound) {
  268. return;
  269. }
  270. }
  271. //
  272. if (maxDepth == 0) {
  273. return;
  274. }
  275. //
  276. if (fr.drillDeeper()) {
  277. List<Object> children = mTreeModel.getChildren(parentNode);
  278. maxDepth--;
  279. for (Object child : children) {
  280. locationStack.push(child);
  281. //
  282. fillNodesList(foundLocationsList, locationStack,
  283. finder, maxDepth, lookDeeperIfFound);
  284. //
  285. locationStack.pop();
  286. }
  287. }
  288. return;
  289. }
  290. /**
  291. * Looks for the first child of the specified parent according to the
  292. * finder.
  293. * @param parentPath
  294. * @param finder
  295. * @return the tree path of the found child or null.
  296. */
  297. public TreePath findChild(TreePath parentPath, TreeItemFinder finder) {
  298. if (finder == null) {
  299. return null;
  300. }
  301. //
  302. Object parentObj = parentPath.getLastPathComponent();
  303. assert parentObj instanceof TreeItem;
  304. //
  305. List<Object> children = mTreeModel.getChildren((Object)parentObj);
  306. for (Object childNode : children) {
  307. Object childDo = getDataObject(childNode);
  308. assert childDo != null;
  309. //
  310. FindResult fr = finder.process(childDo, null);
  311. //
  312. if (fr.isFit()) {
  313. return parentPath.pathByAddingChild(childNode);
  314. }
  315. }
  316. //
  317. return null;
  318. }
  319. /**
  320. * Looks for the set of children of the specified parent according to the
  321. * finder.
  322. * @param parentPath
  323. * @param finder
  324. * @return the tree path of the found child or null.
  325. */
  326. public List<TreePath> findChildren(TreePath parentPath, TreeItemFinder finder) {
  327. if (finder == null) {
  328. return null;
  329. }
  330. //
  331. Object parentObj = parentPath.getLastPathComponent();
  332. assert parentObj instanceof TreeItem;
  333. //
  334. ArrayList<TreePath> result = new ArrayList<TreePath>();
  335. List<Object> children = mTreeModel.getChildren(parentObj);
  336. for (Object childNode : children) {
  337. Object childDo = getDataObject(childNode);
  338. assert childDo != null;
  339. //
  340. FindResult fr = finder.process(childDo, null);
  341. //
  342. if (fr.isFit()) {
  343. TreePath foundChildPath = parentPath.pathByAddingChild(childNode);
  344. result.add(foundChildPath);
  345. }
  346. }
  347. //
  348. return result;
  349. }
  350. /**
  351. * Looks for a child node by data object
  352. * @param parentPath
  353. * @param dataObject
  354. * @return
  355. */
  356. public TreePath findChildByDataObj(TreePath parentPath, Object dataObject) {
  357. if (dataObject == null) {
  358. return null;
  359. }
  360. //
  361. Object parentObj = parentPath.getLastPathComponent();
  362. assert parentObj instanceof TreeItem;
  363. //
  364. List<Object> children = mTreeModel.getChildren(parentObj);
  365. for (Object childNode : children) {
  366. Object childDo = getDataObject(childNode);
  367. assert childDo != null;
  368. //
  369. if (childDo.equals(dataObject)) {
  370. TreePath result = parentPath.pathByAddingChild(childNode);
  371. return result;
  372. }
  373. }
  374. //
  375. return null;
  376. }
  377. /**
  378. * Looks for a child node by index
  379. * @param parentPath
  380. * @param dataObject
  381. * @return
  382. */
  383. public TreePath findChildByIndex(TreePath parentPath, int index) {
  384. if (index < 0) {
  385. return null;
  386. }
  387. //
  388. Object parentObj = parentPath.getLastPathComponent();
  389. assert parentObj instanceof TreeItem;
  390. //
  391. List<Object> children = mTreeModel.getChildren(parentObj);
  392. if (index >= children.size()) {
  393. return null;
  394. }
  395. //
  396. Object childNode = children.get(index);
  397. TreePath result = parentPath.pathByAddingChild(childNode);
  398. return result;
  399. }
  400. }