Search in sources :

Example 6 with InternalXSDMultiPageEditor

use of org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor in project webtools.sourceediting by eclipse.

the class OpenInNewEditor method run.

public void run() {
    Object selection = ((IStructuredSelection) getSelection()).getFirstElement();
    IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = null;
    IEditorInput editorInput = null;
    if (workbenchWindow != null) {
        page = workbenchWindow.getActivePage();
        IEditorPart activeEditor = page.getActiveEditor();
        if (activeEditor != null) {
            editorInput = activeEditor.getEditorInput();
        }
    }
    if (selection instanceof XSDBaseAdapter) {
        XSDBaseAdapter xsdAdapter = (XSDBaseAdapter) selection;
        XSDConcreteComponent fComponent = (XSDConcreteComponent) xsdAdapter.getTarget();
        XSDSchema schema = fComponent.getSchema();
        boolean isReference = false;
        if (fComponent instanceof XSDFeature) {
            isReference = ((XSDFeature) fComponent).isFeatureReference();
            fComponent = ((XSDFeature) fComponent).getResolvedFeature();
        }
        String schemaLocation = null;
        IPath schemaPath = null;
        IFile schemaFile = null;
        // Special case any imports/includes
        if (selection instanceof XSDSchemaDirectiveAdapter) {
            XSDSchemaDirective dir = (XSDSchemaDirective) ((XSDSchemaDirectiveAdapter) selection).getTarget();
            // force load of imported schema
            if (dir instanceof XSDImportImpl) {
                ((XSDImportImpl) dir).importSchema();
            }
            if (dir.getResolvedSchema() != null) {
                schemaLocation = URIHelper.removePlatformResourceProtocol(dir.getResolvedSchema().getSchemaLocation());
                schemaPath = new Path(schemaLocation);
                schemaFile = ResourcesPlugin.getWorkspace().getRoot().getFile(schemaPath);
                schema = dir.getResolvedSchema();
                fComponent = dir.getResolvedSchema();
            }
        } else // Handle any other external components
        if (fComponent.getSchema() != null && fComponent.eContainer() instanceof XSDSchema || fComponent.eContainer() instanceof XSDRedefine || isReference) {
            schemaLocation = URIHelper.removePlatformResourceProtocol(fComponent.getSchema().getSchemaLocation());
            schemaPath = new Path(schemaLocation);
            schemaFile = ResourcesPlugin.getWorkspace().getRoot().getFile(schemaPath);
            try {
                XSDSchema xsdSchema = (XSDSchema) getWorkbenchPart().getAdapter(XSDSchema.class);
                if (fComponent.getSchema() == xsdSchema) {
                    IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
                    if (editorPart instanceof InternalXSDMultiPageEditor) {
                        ((InternalXSDMultiPageEditor) editorPart).openOnGlobalReference(fComponent);
                    }
                    return;
                }
            } catch (Exception e) {
            }
        }
        // If the schemaFile exists in the workspace
        if (page != null && schemaFile != null && schemaFile.exists()) {
            try {
                // Get the current editor's schema
                XSDSchema xsdSchema = (XSDSchema) getWorkbenchPart().getAdapter(XSDSchema.class);
                IEditorPart editorPart = null;
                // are in the same resource file....hence multiple schemas in the same file.
                if (xsdSchema != null && fComponent.getRootContainer().eResource() == xsdSchema.eResource() && xsdSchema != schema) {
                    String editorName = null;
                    XSDFileEditorInput xsdFileEditorInput = new XSDFileEditorInput(schemaFile, fComponent.getSchema());
                    // Try to use the same editor name as the current one
                    if (editorInput != null) {
                        editorName = editorInput.getName();
                        xsdFileEditorInput.setEditorName(editorName);
                    }
                    editorPart = getExistingEditorForInlineSchema(page, schemaFile, schema);
                    if (editorPart == null) {
                        editorPart = page.openEditor(xsdFileEditorInput, XSDEditorPlugin.EDITOR_ID, true, 0);
                    }
                } else {
                    editorPart = page.openEditor(new FileEditorInput(schemaFile), XSDEditorPlugin.EDITOR_ID);
                }
                if (editorPart instanceof InternalXSDMultiPageEditor) {
                    ((InternalXSDMultiPageEditor) editorPart).openOnGlobalReference(fComponent);
                }
            } catch (Exception e) {
            }
        } else {
            // open the xsd externally
            if (schemaLocation != null)
                openExternalFiles(page, schemaLocation, fComponent);
        }
    }
}
Also used : XSDSchemaDirectiveAdapter(org.eclipse.wst.xsd.ui.internal.adapters.XSDSchemaDirectiveAdapter) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) XSDRedefine(org.eclipse.xsd.XSDRedefine) XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) IPath(org.eclipse.core.runtime.IPath) InternalXSDMultiPageEditor(org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) XSDImportImpl(org.eclipse.xsd.impl.XSDImportImpl) XSDFeature(org.eclipse.xsd.XSDFeature) XSDFileEditorInput(org.eclipse.wst.xsd.ui.internal.editor.XSDFileEditorInput) XSDFileEditorInput(org.eclipse.wst.xsd.ui.internal.editor.XSDFileEditorInput) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ADTReadOnlyFileEditorInput(org.eclipse.wst.xsd.ui.internal.adt.editor.ADTReadOnlyFileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) XSDBaseAdapter(org.eclipse.wst.xsd.ui.internal.adapters.XSDBaseAdapter) IEditorInput(org.eclipse.ui.IEditorInput) XSDSchema(org.eclipse.xsd.XSDSchema)

Aggregations

IEditorPart (org.eclipse.ui.IEditorPart)6 InternalXSDMultiPageEditor (org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor)6 IPath (org.eclipse.core.runtime.IPath)5 Path (org.eclipse.core.runtime.Path)5 PartInitException (org.eclipse.ui.PartInitException)5 IFile (org.eclipse.core.resources.IFile)4 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)4 IEditorInput (org.eclipse.ui.IEditorInput)3 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)3 ADTReadOnlyFileEditorInput (org.eclipse.wst.xsd.ui.internal.adt.editor.ADTReadOnlyFileEditorInput)3 URI (java.net.URI)2 IFileStore (org.eclipse.core.filesystem.IFileStore)2 IEditorReference (org.eclipse.ui.IEditorReference)2 FileEditorInput (org.eclipse.ui.part.FileEditorInput)2 ADTFileStoreEditorInput (org.eclipse.wst.xsd.ui.internal.adt.editor.ADTFileStoreEditorInput)2 XSDFileEditorInput (org.eclipse.wst.xsd.ui.internal.editor.XSDFileEditorInput)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 FileStoreEditorInput (org.eclipse.ui.ide.FileStoreEditorInput)1 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)1 XSDBaseAdapter (org.eclipse.wst.xsd.ui.internal.adapters.XSDBaseAdapter)1