Search in sources :

Example 1 with NodeTypeRegistry

use of org.apache.sling.ide.transport.NodeTypeRegistry in project sling by apache.

the class JcrNode method changePrimaryType.

void changePrimaryType(String newPrimaryType) {
    Repository repository = ServerUtil.getDefaultRepository(getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    if (ntManager == null) {
        MessageDialog.openWarning(null, "Unable to change primary type", "Unable to change primary type since project " + getProject().getName() + " is not associated with a server or the server is not started.");
        return;
    }
    try {
        if (!ntManager.isAllowedPrimaryChildNodeType(getParent().getPrimaryType(), newPrimaryType)) {
            if (!MessageDialog.openQuestion(null, "Unable to change primary type", "Parent (type '" + getParent().getPrimaryType() + "')" + " does not accept child with primary type '" + newPrimaryType + "'. Change anyway?")) {
                return;
            }
        }
    } catch (RepositoryException e1) {
        MessageDialog.openWarning(null, "Unable to change primary type", "Exception occured while trying to " + "verify node types: " + e1);
        return;
    }
    String thisNodeType = getPrimaryType();
    final SerializationKind currentSk = getSerializationKind(thisNodeType);
    final SerializationKind newSk = getSerializationKind(newPrimaryType);
    if (currentSk.equals(newSk)) {
        if (newSk != SerializationKind.FOLDER) {
            // easiest - we should just be able to change the type in the .content.xml
            properties.doSetPropertyValue("jcr:primaryType", newPrimaryType);
        } else {
            if (thisNodeType.equals("nt:folder")) {
                // switching away from an nt:folder might require creating a .content.xml
                createVaultFile((IFolder) resource, ".content.xml", newPrimaryType);
            } else if (newPrimaryType.equals("nt:folder")) {
                // 1)
                if (domElement != null) {
                    MessageDialog.openWarning(null, "Unable to change primaryType", "Unable to change jcr:primaryType to nt:folder" + " since the node is contained in a .content.xml");
                    return;
                }
                // verify 2)
                if (!verifyNodeTypeChange(ntManager, newPrimaryType)) {
                    return;
                }
                if (!(resource instanceof IFolder)) {
                    MessageDialog.openWarning(null, "Unable to change primaryType", "Unable to change jcr:primaryType to nt:folder" + " as there is no underlying folder");
                    return;
                }
                IFolder folder = (IFolder) resource;
                // 3) delete the .content.xml
                IFile contentXml = folder.getFile(".content.xml");
                if (contentXml.exists()) {
                    try {
                        contentXml.delete(true, new NullProgressMonitor());
                    } catch (CoreException e) {
                        Logger logger = Activator.getDefault().getPluginLogger();
                        logger.error("Could not delete " + contentXml.getFullPath() + ", e=" + e, e);
                        MessageDialog.openError(null, "Could not delete file", "Could not delete " + contentXml.getFullPath() + ", " + e);
                    }
                }
            } else {
                properties.doSetPropertyValue("jcr:primaryType", newPrimaryType);
            }
        }
        return;
    }
    if (newSk == SerializationKind.FOLDER) {
        // switching to a folder
        if (currentSk == SerializationKind.FILE) {
            MessageDialog.openWarning(null, "Unable to change primary type", "Changing from a file to a folder type is currently not supported");
            return;
        }
        if (newPrimaryType.equals("nt:folder")) {
            // verify
            if (!verifyNodeTypeChange(ntManager, newPrimaryType)) {
                return;
            }
        }
        try {
            // create the new directory structure pointing to 'this'
            IFolder newFolder = getParent().prepareCreateFolderChild(getJcrPathName());
            if (!newPrimaryType.equals("nt:folder")) {
                // move any children from the existing 'this' to a new vault file
                createVaultFileWithContent(newFolder, ".content.xml", newPrimaryType, domElement);
            }
            // remove myself
            if (domElement != null) {
                domElement.remove();
                if (underlying != null) {
                    underlying.save();
                }
            }
            // add a pointer in the corresponding .content.xml to point to this (folder) child
            getParent().createDomChild(getJcrPathName(), null);
            if (newPrimaryType.equals("nt:folder")) {
                // delete the .content.xml
                if (properties != null && properties.getUnderlying() != null) {
                    IFile contentXml = properties.getUnderlying().file;
                    if (contentXml != null && contentXml.exists()) {
                        contentXml.delete(true, new NullProgressMonitor());
                    }
                }
            }
            ServerUtil.triggerIncrementalBuild(newFolder, null);
            return;
        } catch (CoreException e) {
            MessageDialog.openWarning(null, "Unable to change primaryType", "Exception occurred: " + e);
            Logger logger = Activator.getDefault().getPluginLogger();
            logger.error("Exception occurred", e);
            return;
        }
    } else if (newSk == SerializationKind.FILE) {
        MessageDialog.openWarning(null, "Unable to change primary type", "Changing to/from a file is currently not supported");
        return;
    } else {
        // otherwise we're going from a folder to partial-or-full
        if (domElement == null && (resource instanceof IFolder)) {
            createVaultFile((IFolder) resource, ".content.xml", newPrimaryType);
        } else {
            // set the "pointer"'s jcr:primaryType
            if (domElement.getAttributeMap().containsKey("jcr:primaryType")) {
                domElement.setAttribute("jcr:primaryType", newPrimaryType);
            } else {
                domElement.addAttribute("jcr:primaryType", newPrimaryType);
            }
            // then copy all the other attributes - plus children if there are nay
            Element propDomElement = properties.getDomElement();
            if (propDomElement != null) {
                List<Attribute> attributes = propDomElement.getAttributes();
                for (Iterator<Attribute> it = attributes.iterator(); it.hasNext(); ) {
                    Attribute anAttribute = it.next();
                    if (anAttribute.getName().startsWith("xmlns:")) {
                        continue;
                    }
                    if (anAttribute.getName().equals("jcr:primaryType")) {
                        continue;
                    }
                    if (domElement.getAttributeMap().containsKey(anAttribute.getName())) {
                        domElement.setAttribute(anAttribute.getName(), anAttribute.getValue());
                    } else {
                        domElement.addAttribute(anAttribute);
                    }
                }
                List<Element> c2 = propDomElement.getChildren();
                if (c2 != null && c2.size() != 0) {
                    domElement.addNodes(c2);
                }
            }
            if (properties.getUnderlying() != null && properties.getUnderlying().file != null) {
                try {
                    properties.getUnderlying().file.delete(true, new NullProgressMonitor());
                    // prune empty directories:
                    prune(properties.getUnderlying().file.getParent());
                } catch (CoreException e) {
                    MessageDialog.openError(null, "Unable to change primary type", "Could not delete vault file " + properties.getUnderlying().file + ": " + e);
                    Activator.getDefault().getPluginLogger().error("Error changing jcr:primaryType. Could not delete vault file " + properties.getUnderlying().file + ": " + e.getMessage(), e);
                    return;
                }
            }
            underlying.save();
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) Attribute(de.pdark.decentxml.Attribute) Element(de.pdark.decentxml.Element) RepositoryException(org.apache.sling.ide.transport.RepositoryException) Logger(org.apache.sling.ide.log.Logger) Repository(org.apache.sling.ide.transport.Repository) CoreException(org.eclipse.core.runtime.CoreException) SerializationKind(org.apache.sling.ide.serialization.SerializationKind) Iterator(java.util.Iterator) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with NodeTypeRegistry

use of org.apache.sling.ide.transport.NodeTypeRegistry in project sling by apache.

the class JcrNode method validateDrop.

public IStatus validateDrop(int operation, TransferData transferType) {
    Repository repository = ServerUtil.getDefaultRepository(getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    if (ntManager == null) {
        return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, 1, "Cannot drop element here because corresponding server is not started! (Needed to determine node types)", null);
    }
    // let's support plain files first
    try {
        if (getPrimaryType().equals("nt:file")) {
            // hard-code the most prominent case: cannot drop onto a file
            return new Status(IStatus.CANCEL, Activator.PLUGIN_ID, 1, "Cannot drop element onto nt:file", null);
        }
        if (ntManager.isAllowedPrimaryChildNodeType(getPrimaryType(), "nt:file")) {
            return Status.OK_STATUS;
        } else {
            return Status.CANCEL_STATUS;
        }
    } catch (RepositoryException e) {
        Activator.getDefault().getPluginLogger().error("validateDrop: Got Exception while " + "verifying nodeType: " + e, e);
        return Status.CANCEL_STATUS;
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Repository(org.apache.sling.ide.transport.Repository) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) RepositoryException(org.apache.sling.ide.transport.RepositoryException)

Example 3 with NodeTypeRegistry

use of org.apache.sling.ide.transport.NodeTypeRegistry in project sling by apache.

the class JcrNode method pasteFromClipboard.

/**
     * Paste from the clipboard to this (container) node.
     * <p>
     * Copyright Note: The code of this method was ported from eclipse'
     * PasteAction, which due to visibility restrictions was not reusable.
     * <p>
     * @param clipboard
     */
public void pasteFromClipboard(Clipboard clipboard) {
    if (!canBePastedTo(clipboard)) {
        // should not occur due to 'canBePastedTo' check done by 
        // corresponding action - checking here nevertheless
        MessageDialog.openInformation(null, "Cannot paste", "No applicable node (type) for pasting found.");
        return;
    }
    Repository repository = ServerUtil.getDefaultRepository(getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    if (ntManager == null) {
        MessageDialog.openWarning(null, "Cannot paste", "Cannot paste if corresponding server is not started");
        return;
    }
    // try the resource transfer
    IResource[] resourceData = (IResource[]) clipboard.getContents(ResourceTransfer.getInstance());
    if (resourceData != null && resourceData.length > 0) {
        if (resourceData[0].getType() == IResource.PROJECT) {
            // do not support project pasting onto a jcr node
            MessageDialog.openInformation(null, "Cannot paste project(s)", "Pasting of a project onto a (JCR) node is not possible");
            return;
        } else {
            CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(null);
            operation.copyResources(resourceData, getDropContainer());
        }
        return;
    }
    // try the file transfer
    String[] fileData = (String[]) clipboard.getContents(FileTransfer.getInstance());
    if (fileData != null) {
        CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(null);
        operation.copyFiles(fileData, getDropContainer());
        return;
    }
    // then try the text transfer
    String text = (String) clipboard.getContents(TextTransfer.getInstance());
    if ((text != null) && (this.domElement != null)) {
        try {
            Document document = TolerantXMLParser.parse(text, "pasted from clipboard");
            this.domElement.addNode(document.getRootElement());
            this.underlying.save();
        } catch (IOException e) {
            MessageDialog.openError(null, "Could not paste from clipboard", "Exception encountered while pasting from clipboard: " + e);
            Activator.getDefault().getPluginLogger().error("Error pasting from clipboard: " + e, e);
        }
    }
}
Also used : Repository(org.apache.sling.ide.transport.Repository) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) IOException(java.io.IOException) Document(de.pdark.decentxml.Document) CopyFilesAndFoldersOperation(org.eclipse.ui.actions.CopyFilesAndFoldersOperation) IResource(org.eclipse.core.resources.IResource)

Example 4 with NodeTypeRegistry

use of org.apache.sling.ide.transport.NodeTypeRegistry in project sling by apache.

the class JcrNode method canBePastedTo.

public boolean canBePastedTo(Clipboard clipboard) {
    Repository repository = ServerUtil.getDefaultRepository(getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    IResource[] resourceData = (IResource[]) clipboard.getContents(ResourceTransfer.getInstance());
    if (resourceData != null) {
        IContainer container = getDropContainer();
        return container != null;
    }
    String[] fileData = (String[]) clipboard.getContents(FileTransfer.getInstance());
    if (fileData != null) {
        IContainer container = getDropContainer();
        return container != null;
    }
    String text = (String) clipboard.getContents(TextTransfer.getInstance());
    if (text != null) {
        return (domElement != null);
    }
    return false;
}
Also used : Repository(org.apache.sling.ide.transport.Repository) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 5 with NodeTypeRegistry

use of org.apache.sling.ide.transport.NodeTypeRegistry in project sling by apache.

the class JcrNewNodeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection sel = HandlerUtil.getCurrentSelection(event);
    JcrNode node = SelectionUtils.getFirst(sel, JcrNode.class);
    if (node == null) {
        return null;
    }
    Shell shell = HandlerUtil.getActiveShell(event);
    if (!node.canCreateChild()) {
        MessageDialog.openInformation(shell, "Cannot create node", "Node is not covered by the workspace filter as defined in filter.xml");
        return null;
    }
    Repository repository = ServerUtil.getDefaultRepository(node.getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    if (ntManager == null) {
        if (!doNotAskAgain) {
            MessageDialog dialog = new MessageDialog(null, "Unable to validate node type", null, "Unable to validate node types since project " + node.getProject().getName() + " is not associated with a server or the server is not started.", MessageDialog.QUESTION_WITH_CANCEL, new String[] { "Cancel", "Continue (do not ask again)", "Continue" }, 1) {

                @Override
                protected void configureShell(Shell shell) {
                    super.configureShell(shell);
                    setShellStyle(getShellStyle() | SWT.SHEET);
                }
            };
            int choice = dialog.open();
            if (choice <= 0) {
                return null;
            }
            if (choice == 1) {
                doNotAskAgain = true;
            }
        }
    }
    final NodeType nodeType = node.getNodeType();
    if (nodeType != null && nodeType.getName() != null && nodeType.getName().equals("nt:file")) {
        MessageDialog.openInformation(shell, "Cannot create node", "Node of type nt:file cannot have children");
        return null;
    }
    try {
        final NewNodeDialog nnd = new NewNodeDialog(shell, node, ntManager);
        if (nnd.open() == IStatus.OK) {
            node.createChild(nnd.getValue(), nnd.getChosenNodeType());
            return null;
        }
    } catch (RepositoryException e1) {
        Activator.getDefault().getPluginLogger().warn("Could not open NewNodeDialog due to " + e1, e1);
    }
    return null;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Repository(org.apache.sling.ide.transport.Repository) JcrNode(org.apache.sling.ide.eclipse.ui.nav.model.JcrNode) NodeType(javax.jcr.nodetype.NodeType) ISelection(org.eclipse.jface.viewers.ISelection) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) RepositoryException(org.apache.sling.ide.transport.RepositoryException) MessageDialog(org.eclipse.jface.dialogs.MessageDialog)

Aggregations

NodeTypeRegistry (org.apache.sling.ide.transport.NodeTypeRegistry)8 Repository (org.apache.sling.ide.transport.Repository)7 RepositoryException (org.apache.sling.ide.transport.RepositoryException)4 NodeType (javax.jcr.nodetype.NodeType)2 IResource (org.eclipse.core.resources.IResource)2 Attribute (de.pdark.decentxml.Attribute)1 Document (de.pdark.decentxml.Document)1 Element (de.pdark.decentxml.Element)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 JcrNode (org.apache.sling.ide.eclipse.ui.nav.model.JcrNode)1 Logger (org.apache.sling.ide.log.Logger)1 SerializationKind (org.apache.sling.ide.serialization.SerializationKind)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 CoreException (org.eclipse.core.runtime.CoreException)1