]> 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
Imporve JCR keyring and remote repository exploring
[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.SingleJcrNode;
24 import org.argeo.jcr.ui.explorer.model.WorkspaceNode;
25
26 /** Centralizes some useful methods to build Uis with JCR */
27 public class JcrUiUtils {
28
29 /** Insure that the UI componant is not stale, refresh if needed */
30 public static void forceRefreshIfNeeded(TreeParent element) {
31 Node curNode;
32
33 if (element instanceof SingleJcrNode)
34 curNode = ((SingleJcrNode) element).getNode();
35 else if (element instanceof WorkspaceNode)
36 curNode = ((WorkspaceNode) element).getRootNode();
37 else
38 return;
39 // TODO implement specific methods for other cases
40
41 try {
42 // we mainly rely on nb of children
43 if (element.getChildren().length == curNode.getNodes().getSize())
44 return;
45 else {
46 // get rid of children of UI object
47 element.clearChildren();
48 element.getChildren();
49 }
50 } catch (RepositoryException re) {
51 throw new ArgeoException(
52 "Unexpected error while synchronising the UI with the JCR repository",
53 re);
54 }
55 }
56
57 /**
58 * Insure that a model element is inline with the underlying data by
59 * cleaning the corresponding subtree and building it again.
60 */
61 public static void forceRebuild(TreeParent element) {
62 // TODO implement this method if needed.
63 }
64 /**
65 * Workaround to get the alias of the repository that contains the given
66 * element. As we cannot browse the UI tree upward we recursively browse it
67 * downward until we find the given element
68 * */
69 // public static String getRepositoryAliasFromITreeElement(
70 // NodeContentProvider ncp, Object element) {
71 // RepositoryNode repositoryNode = null;
72 // if (element instanceof RepositoryNode)
73 // return ((RepositoryNode) element).getName();
74 // else if (element instanceof RepositoryRegister)
75 // throw new ArgeoException(
76 // "Cannot get alias for a repository register");
77 //
78 // // Get root elements
79 // Object[] elements = ncp.getElements(null);
80 //
81 // try {
82 // for (int i = 0; i < elements.length; i++) {
83 // if (elements[i] instanceof Node) {
84 // Node curNode = (Node) elements[i];
85 // if (curNode.isNodeType(ArgeoTypes.ARGEO_USER_HOME)) {
86 // // Do nothing, we'll find the node in the "normal" tree
87 // // and
88 // // get corresponding alias this way round
89 // } else
90 // throw new ArgeoException(
91 // "Normal nodes should not be at the root of NodeTreeViewer");
92 // } else if (elements[i] instanceof RepositoryRegister) {
93 // RepositoryRegister repositoryRegister = (RepositoryRegister) elements[i];
94 // Map<String, Repository> repositories = repositoryRegister
95 // .getRepositories();
96 //
97 // for (String name : repositories.keySet()) {
98 // boolean found = isElementInCurrentTreePart(
99 // ncp,
100 // new RepositoryNode(name, repositories.get(name)),
101 // (Node) element);
102 // if (found)
103 // return name;
104 // }
105 // } else
106 // throw new ArgeoException(
107 // "Unexpected object class at the root of NodeTreeViewer");
108 // }
109 // } catch (RepositoryException re) {
110 // throw new ArgeoException(
111 // "Unexpected error while retrieving Alias name", re);
112 // }
113 // return null;
114 // }
115 //
116 // /** implements the recursivity */
117 // private static boolean isElementInCurrentTreePart(NodeContentProvider
118 // ncp,
119 // Object parentElement, NodParente searchedElement) {
120 // boolean found = false;
121 // if (parentElement instanceof WorkspaceNode) {
122 // WorkspaceNode wn = (WorkspaceNode) parentElement;
123 // Object[] children = wn.getChildren();
124 // int i = children.length - 1;
125 // while (!found && i >= 0) {
126 // found = isElementInCurrentTreePart(ncp, children[i],
127 // searchedElement);
128 // }
129 // return found;
130 // } else if (parentElement instanceof RepositoryNode) {
131 // RepositoryNode rn = (RepositoryNode) parentElement;
132 // Object[] children = rn.getChildren();
133 // int i = children.length - 1;
134 // while (!found && i >= 0) {
135 // found = isElementInCurrentTreePart(ncp, children[i],
136 // searchedElement);
137 // }
138 // return found;
139 // } else {
140 // Node node = (Node) parentElement;
141 // if (node.equals(searchedElement))
142 // return true;
143 // NodeIterator ni;
144 // try {
145 // ni = node.getNodes();
146 // while (!found && ni.hasNext()) {
147 // found = isElementInCurrentTreePart(ncp, ni.nextNode(),
148 // searchedElement);
149 // }
150 // } catch (RepositoryException e) {
151 // throw new ArgeoException("unexpected erreur while recursively"
152 // + " recovering RepositoryNode for selected object", e);
153 // }
154 //
155 // return found;
156 // }
157 // }
158 }