]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JcrBrowserUtils.java
8649023e4e4fa4e816d62a29386490a8dfb02c12
[lgpl/argeo-commons.git] / JcrBrowserUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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.eclipse.ui.workbench.jcr.internal;
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.eclipse.ui.workbench.jcr.internal.model.RepositoriesElem;
24 import org.argeo.eclipse.ui.workbench.jcr.internal.model.RepositoryElem;
25 import org.argeo.eclipse.ui.workbench.jcr.internal.model.SingleJcrNodeElem;
26 import org.argeo.eclipse.ui.workbench.jcr.internal.model.WorkspaceElem;
27
28 /** Centralizes useful methods to manage the JCR Browser */
29 public class JcrBrowserUtils {
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 SingleJcrNodeElem) {
39 curNode = ((SingleJcrNodeElem) element).getNode();
40 } else if (element instanceof WorkspaceElem) {
41 curNode = ((WorkspaceElem) element).getRootNode();
42 }
43
44 if (curNode != null
45 && element.getChildren().length != curNode.getNodes()
46 .getSize())
47 doRefresh = true;
48 else if (element instanceof RepositoryElem) {
49 RepositoryElem rn = (RepositoryElem) 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 RepositoriesElem) {
56 doRefresh = true;
57 // Always force refresh for RepositoriesElem : the condition
58 // below does not take remote repository into account and it is
59 // not trivial to do so.
60
61 // RepositoriesElem rn = (RepositoriesElem) element;
62 // if (element.getChildren().length !=
63 // rn.getRepositoryRegister()
64 // .getRepositories().size())
65 // doRefresh = true;
66 }
67 if (doRefresh) {
68 element.clearChildren();
69 element.getChildren();
70 }
71 } catch (RepositoryException re) {
72 throw new ArgeoException(
73 "Unexpected error while synchronising the UI with the JCR repository",
74 re);
75 }
76 }
77 }