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