Search in sources :

Example 11 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project bndtools by bndtools.

the class BndEditor method init.

@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    try {
        // Work out our input file and subscribe to resource changes
        final String resourceName;
        IResource inputResource = ResourceUtil.getResource(input);
        if (inputResource != null) {
            inputResource.getWorkspace().addResourceChangeListener(this);
            resourceName = inputResource.getName();
            inputFile = inputResource.getLocation().toFile();
        } else {
            IStorage storage = (IStorage) input.getAdapter(IStorage.class);
            if (storage != null) {
                resourceName = storage.getName();
            } else {
                resourceName = input.getName();
            }
            inputFile = null;
        }
        model.setBndResourceName(resourceName);
        // Initialise pages and title
        initPages(site, input);
        setSourcePage(sourcePage);
        setPartNameForInput(input);
        IDocumentProvider docProvider = sourcePage.getDocumentProvider();
        // #1625: Ensure the IDocumentProvider is not null.
        if (docProvider != null) {
            docProvider.addElementStateListener(new ElementStateListener());
            if (!Central.hasWorkspaceDirectory()) {
                // default ws will be created we can load immediately
                loadEditModel();
            } else {
                // a real ws will be resolved so we need to load async
                Central.onWorkspaceInit(new Success<Workspace, Void>() {

                    @Override
                    public Promise<Void> call(Promise<Workspace> resolved) throws Exception {
                        try {
                            loadEditModel();
                            return Promises.resolved(null);
                        } catch (Exception e) {
                            logger.logError("Failed to load edit model", e);
                            return Promises.failed(e);
                        }
                    }
                });
            }
        }
    } catch (Exception e1) {
        throw Exceptions.duck(e1);
    }
}
Also used : Promise(org.osgi.util.promise.Promise) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IStorage(org.eclipse.core.resources.IStorage) IElementStateListener(org.eclipse.ui.texteditor.IElementStateListener) IResource(org.eclipse.core.resources.IResource) PartInitException(org.eclipse.ui.PartInitException) BadLocationException(aQute.bnd.properties.BadLocationException) IOException(java.io.IOException) Workspace(aQute.bnd.build.Workspace)

Example 12 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project bndtools by bndtools.

the class BndEditor method resourceChanged.

@Override
public void resourceChanged(IResourceChangeEvent event) {
    IResource myResource = ResourceUtil.getResource(getEditorInput());
    IResourceDelta delta = event.getDelta();
    if (delta == null)
        return;
    IPath fullPath = myResource.getFullPath();
    delta = delta.findMember(fullPath);
    if (delta == null)
        return;
    // Delegate to any interested pages
    for (Object page : pages) {
        if (page instanceof IResourceChangeListener) {
            ((IResourceChangeListener) page).resourceChanged(event);
        }
    }
    // Close editor if file removed or switch to new location if file moved
    if (delta.getKind() == IResourceDelta.REMOVED) {
        if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(delta.getMovedToPath());
            final FileEditorInput newInput = new FileEditorInput(file);
            setInput(newInput);
            Display display = getEditorSite().getShell().getDisplay();
            if (display != null) {
                SWTConcurrencyUtil.execForDisplay(display, true, new Runnable() {

                    @Override
                    public void run() {
                        setPartNameForInput(newInput);
                        sourcePage.setInput(newInput);
                    }
                });
            }
        } else {
            close(false);
        }
    } else // File content updated externally => reload all pages
    if ((delta.getKind() & IResourceDelta.CHANGED) > 0 && (delta.getFlags() & IResourceDelta.CONTENT) > 0) {
        if (!saving.get()) {
            final IDocumentProvider docProvider = sourcePage.getDocumentProvider();
            // #1625: Ensure the IDocumentProvider is not null.
            if (docProvider != null) {
                final IDocument document = docProvider.getDocument(getEditorInput());
                SWTConcurrencyUtil.execForControl(getEditorSite().getShell(), true, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            model.loadFrom(new IDocumentWrapper(document));
                            updateIncludedPages();
                        } catch (IOException e) {
                            logger.logError("Failed to reload document", e);
                        }
                    }
                });
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) IOException(java.io.IOException) IDocumentWrapper(bndtools.editor.model.IDocumentWrapper) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IFileEditorInput(org.eclipse.ui.IFileEditorInput) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument) IResourceDelta(org.eclipse.core.resources.IResourceDelta) Display(org.eclipse.swt.widgets.Display)

Aggregations

IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)12 IFile (org.eclipse.core.resources.IFile)5 IOException (java.io.IOException)4 IDocumentWrapper (bndtools.editor.model.IDocumentWrapper)3 IResource (org.eclipse.core.resources.IResource)3 CoreException (org.eclipse.core.runtime.CoreException)3 IDocument (org.eclipse.jface.text.IDocument)3 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)3 PartInitException (org.eclipse.ui.PartInitException)3 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)3 Workspace (aQute.bnd.build.Workspace)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 IPath (org.eclipse.core.runtime.IPath)2 WizardDialog (org.eclipse.jface.wizard.WizardDialog)2 IEditorInput (org.eclipse.ui.IEditorInput)2 IEditorPart (org.eclipse.ui.IEditorPart)2 FileEditorInput (org.eclipse.ui.part.FileEditorInput)2 ByteArray (org.talend.core.model.properties.ByteArray)2 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)2