]> git.argeo.org Git - lgpl/argeo-commons.git/blob - utils/JcrUiUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / utils / JcrUiUtils.java
1 package org.argeo.jcr.ui.explorer.utils;
2
3 import javax.jcr.Node;
4 import javax.jcr.RepositoryException;
5
6 import org.argeo.ArgeoException;
7 import org.argeo.eclipse.ui.TreeParent;
8 import org.argeo.jcr.ui.explorer.model.SingleJcrNode;
9 import org.argeo.jcr.ui.explorer.model.WorkspaceNode;
10
11 /** Centralizes some useful methods to build Uis with JCR */
12 public class JcrUiUtils {
13
14 /** Insure that the UI componant is not stale, refresh if needed */
15 public static void forceRefreshIfNeeded(TreeParent element) {
16 Node curNode;
17
18 if (element instanceof SingleJcrNode)
19 curNode = ((SingleJcrNode) element).getNode();
20 else if (element instanceof WorkspaceNode)
21 curNode = ((WorkspaceNode) element).getRootNode();
22 else
23 return;
24 // TODO implement specific methods for other cases
25
26 try {
27 // we mainly rely on nb of children
28 if (element.getChildren().length == curNode.getNodes().getSize())
29 return;
30 else {
31 // get rid of children of UI object
32 element.clearChildren();
33 element.getChildren();
34 }
35 } catch (RepositoryException re) {
36 throw new ArgeoException(
37 "Unexpected error while synchronising the UI with the JCR repository",
38 re);
39 }
40 }
41 /**
42 * Workaround to get the alias of the repository that contains the given
43 * element. As we cannot browse the UI tree upward we recursively browse it
44 * downward until we find the given element
45 * */
46 // public static String getRepositoryAliasFromITreeElement(
47 // NodeContentProvider ncp, Object element) {
48 // RepositoryNode repositoryNode = null;
49 // if (element instanceof RepositoryNode)
50 // return ((RepositoryNode) element).getName();
51 // else if (element instanceof RepositoryRegister)
52 // throw new ArgeoException(
53 // "Cannot get alias for a repository register");
54 //
55 // // Get root elements
56 // Object[] elements = ncp.getElements(null);
57 //
58 // try {
59 // for (int i = 0; i < elements.length; i++) {
60 // if (elements[i] instanceof Node) {
61 // Node curNode = (Node) elements[i];
62 // if (curNode.isNodeType(ArgeoTypes.ARGEO_USER_HOME)) {
63 // // Do nothing, we'll find the node in the "normal" tree
64 // // and
65 // // get corresponding alias this way round
66 // } else
67 // throw new ArgeoException(
68 // "Normal nodes should not be at the root of NodeTreeViewer");
69 // } else if (elements[i] instanceof RepositoryRegister) {
70 // RepositoryRegister repositoryRegister = (RepositoryRegister) elements[i];
71 // Map<String, Repository> repositories = repositoryRegister
72 // .getRepositories();
73 //
74 // for (String name : repositories.keySet()) {
75 // boolean found = isElementInCurrentTreePart(
76 // ncp,
77 // new RepositoryNode(name, repositories.get(name)),
78 // (Node) element);
79 // if (found)
80 // return name;
81 // }
82 // } else
83 // throw new ArgeoException(
84 // "Unexpected object class at the root of NodeTreeViewer");
85 // }
86 // } catch (RepositoryException re) {
87 // throw new ArgeoException(
88 // "Unexpected error while retrieving Alias name", re);
89 // }
90 // return null;
91 // }
92 //
93 // /** implements the recursivity */
94 // private static boolean isElementInCurrentTreePart(NodeContentProvider
95 // ncp,
96 // Object parentElement, NodParente searchedElement) {
97 // boolean found = false;
98 // if (parentElement instanceof WorkspaceNode) {
99 // WorkspaceNode wn = (WorkspaceNode) parentElement;
100 // Object[] children = wn.getChildren();
101 // int i = children.length - 1;
102 // while (!found && i >= 0) {
103 // found = isElementInCurrentTreePart(ncp, children[i],
104 // searchedElement);
105 // }
106 // return found;
107 // } else if (parentElement instanceof RepositoryNode) {
108 // RepositoryNode rn = (RepositoryNode) parentElement;
109 // Object[] children = rn.getChildren();
110 // int i = children.length - 1;
111 // while (!found && i >= 0) {
112 // found = isElementInCurrentTreePart(ncp, children[i],
113 // searchedElement);
114 // }
115 // return found;
116 // } else {
117 // Node node = (Node) parentElement;
118 // if (node.equals(searchedElement))
119 // return true;
120 // NodeIterator ni;
121 // try {
122 // ni = node.getNodes();
123 // while (!found && ni.hasNext()) {
124 // found = isElementInCurrentTreePart(ncp, ni.nextNode(),
125 // searchedElement);
126 // }
127 // } catch (RepositoryException e) {
128 // throw new ArgeoException("unexpected erreur while recursively"
129 // + " recovering RepositoryNode for selected object", e);
130 // }
131 //
132 // return found;
133 // }
134 // }
135 }