View Javadoc
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.cms.ui.jcr;
17  
18  import javax.jcr.Node;
19  import javax.jcr.Property;
20  import javax.jcr.PropertyType;
21  import javax.jcr.RepositoryException;
22  
23  import org.argeo.cms.ui.jcr.model.RepositoriesElem;
24  import org.argeo.cms.ui.jcr.model.RepositoryElem;
25  import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
26  import org.argeo.cms.ui.jcr.model.WorkspaceElem;
27  import org.argeo.eclipse.ui.EclipseUiException;
28  import org.argeo.eclipse.ui.TreeParent;
29  
30  /** Useful methods to manage the JCR Browser */
31  public class JcrBrowserUtils {
32  
33  	public static String getPropertyTypeAsString(Property prop) {
34  		try {
35  			return PropertyType.nameFromValue(prop.getType());
36  		} catch (RepositoryException e) {
37  			throw new EclipseUiException("Cannot check type for " + prop, e);
38  		}
39  	}
40  
41  	/** Insure that the UI component is not stale, refresh if needed */
42  	public static void forceRefreshIfNeeded(TreeParent element) {
43  		Node curNode = null;
44  
45  		boolean doRefresh = false;
46  
47  		try {
48  			if (element instanceof SingleJcrNodeElem) {
49  				curNode = ((SingleJcrNodeElem) element).getNode();
50  			} else if (element instanceof WorkspaceElem) {
51  				curNode = ((WorkspaceElem) element).getRootNode();
52  			}
53  
54  			if (curNode != null && element.getChildren().length != curNode.getNodes().getSize())
55  				doRefresh = true;
56  			else if (element instanceof RepositoryElem) {
57  				RepositoryElem rn = (RepositoryElem) element;
58  				if (rn.isConnected()) {
59  					String[] wkpNames = rn.getAccessibleWorkspaceNames();
60  					if (element.getChildren().length != wkpNames.length)
61  						doRefresh = true;
62  				}
63  			} else if (element instanceof RepositoriesElem) {
64  				doRefresh = true;
65  				// Always force refresh for RepositoriesElem : the condition
66  				// below does not take remote repository into account and it is
67  				// not trivial to do so.
68  
69  				// RepositoriesElem rn = (RepositoriesElem) element;
70  				// if (element.getChildren().length !=
71  				// rn.getRepositoryRegister()
72  				// .getRepositories().size())
73  				// doRefresh = true;
74  			}
75  			if (doRefresh) {
76  				element.clearChildren();
77  				element.getChildren();
78  			}
79  		} catch (RepositoryException re) {
80  			throw new EclipseUiException("Unexpected error while synchronising the UI with the JCR repository", re);
81  		}
82  	}
83  }