]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/utils/JcrUiUtils.java
af06ba22074a1e863dea1dc60a354367614bf164
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / utils / JcrUiUtils.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr.ui.explorer.utils;
17
18 import javax.jcr.Node;
19 import javax.jcr.RepositoryException;
20
21 import org.argeo.ArgeoException;
22 import org.argeo.eclipse.ui.TreeParent;
23 import org.argeo.jcr.ui.explorer.model.RepositoriesNode;
24 import org.argeo.jcr.ui.explorer.model.RepositoryNode;
25 import org.argeo.jcr.ui.explorer.model.SingleJcrNode;
26 import org.argeo.jcr.ui.explorer.model.WorkspaceNode;
27
28 /** Centralizes some useful methods to build UIs with JCR */
29 public class JcrUiUtils {
30
31 /** Insure that the UI component is not stale, refresh if needed */
32 public static void forceRefreshIfNeeded(TreeParent element) {
33 Node curNode = null;
34
35 boolean doRefresh = false;
36
37 try {
38 if (element instanceof SingleJcrNode) {
39 curNode = ((SingleJcrNode) element).getNode();
40 } else if (element instanceof WorkspaceNode) {
41 curNode = ((WorkspaceNode) element).getRootNode();
42 }
43
44 if (curNode != null
45 && element.getChildren().length != curNode.getNodes()
46 .getSize())
47 doRefresh = true;
48 else if (element instanceof RepositoryNode) {
49 RepositoryNode rn = (RepositoryNode) element;
50 String[] wkpNames = rn.getAccessibleWorkspaceNames();
51 if (element.getChildren().length != wkpNames.length)
52 doRefresh = true;
53 } else if (element instanceof RepositoriesNode) {
54 RepositoriesNode rn = (RepositoriesNode) element;
55 if (element.getChildren().length != rn.getRepositoryRegister()
56 .getRepositories().size())
57 doRefresh = true;
58 }
59 if (doRefresh) {
60 element.clearChildren();
61 element.getChildren();
62 }
63 } catch (RepositoryException re) {
64 throw new ArgeoException(
65 "Unexpected error while synchronising the UI with the JCR repository",
66 re);
67 }
68 }
69
70 /**
71 * Insure that a model element is inline with the underlying data by
72 * cleaning the corresponding subtree and building it again.
73 */
74 public static void forceRebuild(TreeParent element) {
75 // TODO implement this method if needed.
76 }
77 /**
78 * Workaround to get the alias of the repository that contains the given
79 * element. As we cannot browse the UI tree upward we recursively browse it
80 * downward until we find the given element
81 * */
82 // public static String getRepositoryAliasFromITreeElement(
83 // NodeContentProvider ncp, Object element) {
84 // RepositoryNode repositoryNode = null;
85 // if (element instanceof RepositoryNode)
86 // return ((RepositoryNode) element).getName();
87 // else if (element instanceof RepositoryRegister)
88 // throw new ArgeoException(
89 // "Cannot get alias for a repository register");
90 //
91 // // Get root elements
92 // Object[] elements = ncp.getElements(null);
93 //
94 // try {
95 // for (int i = 0; i < elements.length; i++) {
96 // if (elements[i] instanceof Node) {
97 // Node curNode = (Node) elements[i];
98 // if (curNode.isNodeType(ArgeoTypes.ARGEO_USER_HOME)) {
99 // // Do nothing, we'll find the node in the "normal" tree
100 // // and
101 // // get corresponding alias this way round
102 // } else
103 // throw new ArgeoException(
104 // "Normal nodes should not be at the root of NodeTreeViewer");
105 // } else if (elements[i] instanceof RepositoryRegister) {
106 // RepositoryRegister repositoryRegister = (RepositoryRegister) elements[i];
107 // Map<String, Repository> repositories = repositoryRegister
108 // .getRepositories();
109 //
110 // for (String name : repositories.keySet()) {
111 // boolean found = isElementInCurrentTreePart(
112 // ncp,
113 // new RepositoryNode(name, repositories.get(name)),
114 // (Node) element);
115 // if (found)
116 // return name;
117 // }
118 // } else
119 // throw new ArgeoException(
120 // "Unexpected object class at the root of NodeTreeViewer");
121 // }
122 // } catch (RepositoryException re) {
123 // throw new ArgeoException(
124 // "Unexpected error while retrieving Alias name", re);
125 // }
126 // return null;
127 // }
128 //
129 // /** implements the recursivity */
130 // private static boolean isElementInCurrentTreePart(NodeContentProvider
131 // ncp,
132 // Object parentElement, NodParente searchedElement) {
133 // boolean found = false;
134 // if (parentElement instanceof WorkspaceNode) {
135 // WorkspaceNode wn = (WorkspaceNode) parentElement;
136 // Object[] children = wn.getChildren();
137 // int i = children.length - 1;
138 // while (!found && i >= 0) {
139 // found = isElementInCurrentTreePart(ncp, children[i],
140 // searchedElement);
141 // }
142 // return found;
143 // } else if (parentElement instanceof RepositoryNode) {
144 // RepositoryNode rn = (RepositoryNode) parentElement;
145 // Object[] children = rn.getChildren();
146 // int i = children.length - 1;
147 // while (!found && i >= 0) {
148 // found = isElementInCurrentTreePart(ncp, children[i],
149 // searchedElement);
150 // }
151 // return found;
152 // } else {
153 // Node node = (Node) parentElement;
154 // if (node.equals(searchedElement))
155 // return true;
156 // NodeIterator ni;
157 // try {
158 // ni = node.getNodes();
159 // while (!found && ni.hasNext()) {
160 // found = isElementInCurrentTreePart(ncp, ni.nextNode(),
161 // searchedElement);
162 // }
163 // } catch (RepositoryException e) {
164 // throw new ArgeoException("unexpected erreur while recursively"
165 // + " recovering RepositoryNode for selected object", e);
166 // }
167 //
168 // return found;
169 // }
170 // }
171 }