]> 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 /**
43 * Insure that a model element is inline with the underlying data by
44 * cleaning the corresponding subtree and building it again.
45 */
46 public static void forceRebuild(TreeParent element) {
47 // TODO implement this method if needed.
48 }
49 /**
50 * Workaround to get the alias of the repository that contains the given
51 * element. As we cannot browse the UI tree upward we recursively browse it
52 * downward until we find the given element
53 * */
54 // public static String getRepositoryAliasFromITreeElement(
55 // NodeContentProvider ncp, Object element) {
56 // RepositoryNode repositoryNode = null;
57 // if (element instanceof RepositoryNode)
58 // return ((RepositoryNode) element).getName();
59 // else if (element instanceof RepositoryRegister)
60 // throw new ArgeoException(
61 // "Cannot get alias for a repository register");
62 //
63 // // Get root elements
64 // Object[] elements = ncp.getElements(null);
65 //
66 // try {
67 // for (int i = 0; i < elements.length; i++) {
68 // if (elements[i] instanceof Node) {
69 // Node curNode = (Node) elements[i];
70 // if (curNode.isNodeType(ArgeoTypes.ARGEO_USER_HOME)) {
71 // // Do nothing, we'll find the node in the "normal" tree
72 // // and
73 // // get corresponding alias this way round
74 // } else
75 // throw new ArgeoException(
76 // "Normal nodes should not be at the root of NodeTreeViewer");
77 // } else if (elements[i] instanceof RepositoryRegister) {
78 // RepositoryRegister repositoryRegister = (RepositoryRegister) elements[i];
79 // Map<String, Repository> repositories = repositoryRegister
80 // .getRepositories();
81 //
82 // for (String name : repositories.keySet()) {
83 // boolean found = isElementInCurrentTreePart(
84 // ncp,
85 // new RepositoryNode(name, repositories.get(name)),
86 // (Node) element);
87 // if (found)
88 // return name;
89 // }
90 // } else
91 // throw new ArgeoException(
92 // "Unexpected object class at the root of NodeTreeViewer");
93 // }
94 // } catch (RepositoryException re) {
95 // throw new ArgeoException(
96 // "Unexpected error while retrieving Alias name", re);
97 // }
98 // return null;
99 // }
100 //
101 // /** implements the recursivity */
102 // private static boolean isElementInCurrentTreePart(NodeContentProvider
103 // ncp,
104 // Object parentElement, NodParente searchedElement) {
105 // boolean found = false;
106 // if (parentElement instanceof WorkspaceNode) {
107 // WorkspaceNode wn = (WorkspaceNode) parentElement;
108 // Object[] children = wn.getChildren();
109 // int i = children.length - 1;
110 // while (!found && i >= 0) {
111 // found = isElementInCurrentTreePart(ncp, children[i],
112 // searchedElement);
113 // }
114 // return found;
115 // } else if (parentElement instanceof RepositoryNode) {
116 // RepositoryNode rn = (RepositoryNode) parentElement;
117 // Object[] children = rn.getChildren();
118 // int i = children.length - 1;
119 // while (!found && i >= 0) {
120 // found = isElementInCurrentTreePart(ncp, children[i],
121 // searchedElement);
122 // }
123 // return found;
124 // } else {
125 // Node node = (Node) parentElement;
126 // if (node.equals(searchedElement))
127 // return true;
128 // NodeIterator ni;
129 // try {
130 // ni = node.getNodes();
131 // while (!found && ni.hasNext()) {
132 // found = isElementInCurrentTreePart(ncp, ni.nextNode(),
133 // searchedElement);
134 // }
135 // } catch (RepositoryException e) {
136 // throw new ArgeoException("unexpected erreur while recursively"
137 // + " recovering RepositoryNode for selected object", e);
138 // }
139 //
140 // return found;
141 // }
142 // }
143 }