X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fruntime%2Forg.argeo.eclipse.ui.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Fjcr%2Fviews%2FGenericJcrBrowser.java;h=414cb010ac71d4a6974429c16d9ad8104477b78e;hb=5a00c032d8c7b9617777db83b0a6d60ab6b74d18;hp=e39bc56599e130d8fc01258826ff81e14bf990ea;hpb=92daeb190be66653f39544ae9b5a2f2c31a60797;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java index e39bc5659..414cb010a 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java @@ -1,71 +1,55 @@ package org.argeo.eclipse.ui.jcr.views; +import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.Comparator; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; -import javax.jcr.Item; -import javax.jcr.LoginException; import javax.jcr.Node; -import javax.jcr.NodeIterator; import javax.jcr.Property; -import javax.jcr.PropertyIterator; import javax.jcr.PropertyType; -import javax.jcr.Repository; import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.nodetype.NodeType; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; -import org.argeo.eclipse.ui.TreeParent; +import org.argeo.eclipse.ui.dialogs.Error; +import org.argeo.eclipse.ui.jcr.browser.NodeContentProvider; +import org.argeo.eclipse.ui.jcr.browser.NodeLabelProvider; +import org.argeo.eclipse.ui.jcr.browser.PropertiesContentProvider; +import org.argeo.eclipse.ui.jcr.browser.RepositoryNode; +import org.argeo.eclipse.ui.jcr.browser.WorkspaceNode; +import org.argeo.eclipse.ui.specific.FileHandler; import org.argeo.jcr.RepositoryRegister; +import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.viewers.ColumnLabelProvider; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.ISelectionChangedListener; -import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.ITreeContentProvider; -import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; -import org.eclipse.jface.viewers.TreeNode; import org.eclipse.jface.viewers.TreeViewer; -import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Menu; import org.eclipse.ui.part.ViewPart; public class GenericJcrBrowser extends ViewPart { - // private final static Log log = - // LogFactory.getLog(GenericJcrBrowser.class); + private static Log log = LogFactory.getLog(GenericJcrBrowser.class); private TreeViewer nodesViewer; private TableViewer propertiesViewer; - // private Session jcrSession; private RepositoryRegister repositoryRegister; - private Comparator itemComparator = new Comparator() { - public int compare(Item o1, Item o2) { - try { - return o1.getName().compareTo(o2.getName()); - } catch (RepositoryException e) { - throw new ArgeoException("Cannot compare " + o1 + " and " + o2, - e); - } - } - }; - @Override public void createPartControl(Composite parent) { parent.setLayout(new FillLayout()); @@ -78,6 +62,7 @@ public class GenericJcrBrowser extends ViewPart { GridLayout gl = new GridLayout(1, false); top.setLayout(gl); + // nodes viewer nodesViewer = new TreeViewer(top, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); nodesViewer.getTree().setLayoutData( @@ -98,21 +83,72 @@ public class GenericJcrBrowser extends ViewPart { }); nodesViewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { + if (event.getSelection() == null + || event.getSelection().isEmpty()) + return; Object obj = ((IStructuredSelection) event.getSelection()) .getFirstElement(); if (obj instanceof RepositoryNode) { ((RepositoryNode) obj).login(); + nodesViewer.refresh(obj); } else if (obj instanceof WorkspaceNode) { ((WorkspaceNode) obj).login(); + nodesViewer.refresh(obj); + } // call the openFile commands on node + else if (obj instanceof Node) { + Node node = (Node) obj; + try { + if (node.isNodeType("nt:file")) { + + Node child = node.getNodes().nextNode(); + if (!child.isNodeType("nt:resource")) { + Error.show("Cannot open file children Node that are not of 'nt:resource' type."); + return; + } + InputStream fis = null; + + try { + fis = (InputStream) child + .getProperty("jcr:data").getBinary() + .getStream(); + + String name = node.getName(); + + // Instantiate the generic object that fits for + // both + // RCP & RAP. + FileHandler fh = new FileHandler(); + fh.openFile(name, fis); + // fh.openFile(file); + } catch (Exception e) { + throw new ArgeoException( + "Stream error while opening file", e); + } finally { + IOUtils.closeQuietly(fis); + } + } + } catch (RepositoryException re) { + re.printStackTrace(); + + } } } }); + + // context menu + MenuManager menuManager = new MenuManager(); + Menu menu = menuManager.createContextMenu(nodesViewer.getTree()); + nodesViewer.getTree().setMenu(menu); + getSite().registerContextMenu(menuManager, nodesViewer); + getSite().setSelectionProvider(nodesViewer); + nodesViewer.setInput(repositoryRegister); Composite bottom = new Composite(sashForm, SWT.NONE); bottom.setLayout(new GridLayout(1, false)); + // properties viewer propertiesViewer = new TableViewer(bottom); propertiesViewer.getTable().setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true)); @@ -185,236 +221,85 @@ public class GenericJcrBrowser extends ViewPart { return new int[] { 70, 30 }; } - public void setRepositoryRegister(RepositoryRegister repositoryRegister) { - this.repositoryRegister = repositoryRegister; - } - /* - * NODES + * NOTIFICATION */ - protected Object[] childrenNodes(Node parentNode) { - try { - List children = new ArrayList(); - NodeIterator nit = parentNode.getNodes(); - while (nit.hasNext()) { - Node node = nit.nextNode(); - children.add(node); - } - Node[] arr = children.toArray(new Node[children.size()]); - Arrays.sort(arr, itemComparator); - return arr; - } catch (RepositoryException e) { - throw new ArgeoException("Cannot list children of " + parentNode, e); - } + public void refresh(Object obj) { + nodesViewer.refresh(obj); } - private class NodeContentProvider implements ITreeContentProvider { - - public Object[] getElements(Object inputElement) { - return getChildren(inputElement); - } - - public Object[] getChildren(Object parentElement) { - if (parentElement instanceof Node) { - return childrenNodes((Node) parentElement); - } else if (parentElement instanceof RepositoryNode) { - return ((RepositoryNode) parentElement).getChildren(); - } else if (parentElement instanceof WorkspaceNode) { - Session session = ((WorkspaceNode) parentElement).getSession(); - if (session == null) - return new Object[0]; - - try { - return childrenNodes(session.getRootNode()); - } catch (RepositoryException e) { - throw new ArgeoException("Cannot retrieve root node of " - + session, e); - } - } else if (parentElement instanceof RepositoryRegister) { - RepositoryRegister repositoryRegister = (RepositoryRegister) parentElement; - List nodes = new ArrayList(); - Map repositories = repositoryRegister - .getRepositories(); - for (String name : repositories.keySet()) { - nodes.add(new RepositoryNode(name, repositories.get(name))); - } - return nodes.toArray(); - } else { - return new Object[0]; - } - } - - public Object getParent(Object element) { - try { - if (element instanceof Node) { - return ((Node) element).getParent(); - } - return null; - } catch (RepositoryException e) { - throw new ArgeoException( - "Cannot retrieve parent of " + element, e); - } - } - - public boolean hasChildren(Object element) { - try { - if (element instanceof Node) { - return ((Node) element).hasNodes(); - } else if (element instanceof RepositoryNode) { - return ((RepositoryNode) element).hasChildren(); - } else if (element instanceof WorkspaceNode) { - return ((WorkspaceNode) element).getSession() != null; - } - return false; - } catch (RepositoryException e) { - throw new ArgeoException("Cannot check children of " + element, - e); - } - } - - public void dispose() { - } - - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - } - + public void nodeAdded(Node parentNode, Node newNode) { + nodesViewer.refresh(parentNode); + nodesViewer.expandToLevel(newNode, 0); } - class NodeLabelProvider extends LabelProvider { - - public String getText(Object element) { - try { - if (element instanceof Node) { - Node node = (Node) element; - String label = node.getName(); - // try { - // Item primaryItem = node.getPrimaryItem(); - // label = primaryItem instanceof Property ? ((Property) - // primaryItem) - // .getValue().getString() - // + " (" - // + node.getName() - // + ")" : node.getName(); - // } catch (RepositoryException e) { - // label = node.getName(); - // } - StringBuffer mixins = new StringBuffer(""); - for (NodeType type : node.getMixinNodeTypes()) - mixins.append(' ').append(type.getName()); - - return label + " [" + node.getPrimaryNodeType().getName() - + mixins + "]"; - } - return element.toString(); - } catch (RepositoryException e) { - throw new ArgeoException("Cannot get text for of " + element, e); - } - } - + public void nodeRemoved(Node parentNode) { + + List al = new ArrayList(); + al.add(parentNode); + + IStructuredSelection newSel = new StructuredSelection(al); + // IStructuredSelection newSel = new StructuredSelection(parentNode); + + if (log.isDebugEnabled()) + log.debug("new selection size = " + newSel.size()); + + nodesViewer.setSelection(newSel, true); + IStructuredSelection tmpSel = (IStructuredSelection) nodesViewer + .getSelection(); + + if (log.isDebugEnabled()) + log.debug("set selection size = " + tmpSel.size()); + + nodesViewer.refresh(); + + // + // log.debug(" Class selected (Parent 1ST element) : " + // + tmpSel.getFirstElement().getClass()); + // setFocus(); + // + // nodesViewer.refresh(parentNode); + + // // Call the refresh node command + // try { + // IWorkbench iw = JcrUiPlugin.getDefault().getWorkbench(); + // IHandlerService handlerService = (IHandlerService) iw + // .getService(IHandlerService.class); + // + // // get the command from plugin.xml + // IWorkbenchWindow window = iw.getActiveWorkbenchWindow(); + // ICommandService cmdService = (ICommandService) window + // .getService(ICommandService.class); + // Command cmd = cmdService + // .getCommand(OpenEbiDetailsEditor.COMMAND_NAME); + // + // // log.debug("cmd : " + cmd); + // ArrayList parameters = new + // ArrayList(); + // + // // get the parameter + // IParameter iparam = cmd + // .getParameter(OpenEbiDetailsEditor.PARAM_UUID); + // + // Parameterization params = new Parameterization(iparam, + // ((String[]) obj)[0]); + // parameters.add(params); + // + // // build the parameterized command + // ParameterizedCommand pc = new ParameterizedCommand(cmd, + // parameters.toArray(new Parameterization[parameters.size()])); + // + // // execute the command + // handlerService = (IHandlerService) window + // .getService(IHandlerService.class); + // handlerService.executeCommand(pc, null); + // + // } catch (Exception e) { + // throw new ArgeoException("Error opening EBI", e); + // } } - /* - * PROPERTIES - */ - private class PropertiesContentProvider implements - IStructuredContentProvider { - - public void dispose() { - } - - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - } - - public Object[] getElements(Object inputElement) { - try { - if (inputElement instanceof Node) { - Set props = new TreeSet(itemComparator); - PropertyIterator pit = ((Node) inputElement) - .getProperties(); - while (pit.hasNext()) - props.add(pit.nextProperty()); - return props.toArray(); - } - return new Object[] {}; - } catch (RepositoryException e) { - throw new ArgeoException("Cannot get element for " - + inputElement, e); - } - } - - } - - private class RepositoryNode extends TreeParent { - private final String name; - private final Repository repository; - private Session defaultSession = null; - - public RepositoryNode(String name, Repository repository) { - super(name); - this.name = name; - this.repository = repository; - } - - public Repository getRepository() { - return repository; - } - - public Session getDefaultSession() { - return defaultSession; - } - - public void login() { - try { - defaultSession = repository.login(); - String[] wkpNames = defaultSession.getWorkspace() - .getAccessibleWorkspaceNames(); - for (String wkpName : wkpNames) { - if (wkpName.equals(defaultSession.getWorkspace().getName())) - addChild(new WorkspaceNode(repository, wkpName, - defaultSession)); - else - addChild(new WorkspaceNode(repository, wkpName)); - } - nodesViewer.refresh(this); - } catch (RepositoryException e) { - throw new ArgeoException( - "Cannot connect to repository " + name, e); - } - } - } - - private class WorkspaceNode extends TreeParent { - private final String name; - private final Repository repository; - private Session session = null; - - public WorkspaceNode(Repository repository, String name) { - this(repository, name, null); - } - - public WorkspaceNode(Repository repository, String name, Session session) { - super(name); - this.name = name; - this.repository = repository; - this.session = session; - } - - public Session getSession() { - return session; - } - - public void login() { - try { - if (session != null) - session.logout(); - - session = repository.login(name); - nodesViewer.refresh(this); - } catch (RepositoryException e) { - throw new ArgeoException( - "Cannot connect to repository " + name, e); - } - } - + public void setRepositoryRegister(RepositoryRegister repositoryRegister) { + this.repositoryRegister = repositoryRegister; } }