Search in sources :

Example 46 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project eclipse.platform.text by eclipse.

the class SearchResultViewer method handleLabelProviderChanged.

@Override
protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
    Object[] changed = event.getElements();
    if (changed != null && !fResourceToItemsMapper.isEmpty()) {
        ArrayList<Object> others = new ArrayList<>(changed.length);
        for (Object curr : changed) {
            if (curr instanceof IResource)
                fResourceToItemsMapper.resourceChanged((IResource) curr);
            else if (curr instanceof IAdaptable) {
                IResource resource = ((IAdaptable) curr).getAdapter(IResource.class);
                if (resource != null)
                    fResourceToItemsMapper.resourceChanged(resource);
            } else
                others.add(curr);
        }
        if (others.isEmpty()) {
            return;
        }
        event = new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource(), others.toArray());
    }
    super.handleLabelProviderChanged(event);
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) ArrayList(java.util.ArrayList) IBaseLabelProvider(org.eclipse.jface.viewers.IBaseLabelProvider) LabelProviderChangedEvent(org.eclipse.jface.viewers.LabelProviderChangedEvent) IResource(org.eclipse.core.resources.IResource)

Example 47 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project eclipse.platform.text by eclipse.

the class TextFileDocumentProvider method createFileInfo.

/**
 * Creates and returns the file info object
 * for the given element.
 * <p>
 * Subclasses which extend {@link org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo}
 * will probably have to extend this method as well.
 * </p>
 *
 * @param element the element
 * @return a file info object of type <code>FileInfo</code>
 * 			 or <code>null</code> if none can be created
 * @throws CoreException if the file info object could not successfully be created
 */
protected FileInfo createFileInfo(Object element) throws CoreException {
    if (!(element instanceof IAdaptable))
        return null;
    IAdaptable adaptable = (IAdaptable) element;
    IFile file = null;
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    ITextFileBuffer fileBuffer = null;
    LocationKind locationKind = null;
    file = adaptable.getAdapter(IFile.class);
    if (file != null) {
        IPath location = file.getFullPath();
        locationKind = LocationKind.IFILE;
        manager.connect(location, locationKind, getProgressMonitor());
        fileBuffer = manager.getTextFileBuffer(location, locationKind);
    } else {
        ILocationProvider provider = adaptable.getAdapter(ILocationProvider.class);
        if (provider instanceof ILocationProviderExtension) {
            URI uri = ((ILocationProviderExtension) provider).getURI(element);
            if (ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(uri).length == 0) {
                IFileStore fileStore = EFS.getStore(uri);
                manager.connectFileStore(fileStore, getProgressMonitor());
                fileBuffer = manager.getFileStoreTextFileBuffer(fileStore);
            }
        }
        if (fileBuffer == null && provider != null) {
            IPath location = provider.getPath(element);
            if (location == null)
                return null;
            locationKind = LocationKind.NORMALIZE;
            manager.connect(location, locationKind, getProgressMonitor());
            fileBuffer = manager.getTextFileBuffer(location, locationKind);
            file = FileBuffers.getWorkspaceFileAtLocation(location);
        }
    }
    if (fileBuffer != null) {
        fileBuffer.requestSynchronizationContext();
        FileInfo info = createEmptyFileInfo();
        info.fTextFileBuffer = fileBuffer;
        info.fTextFileBufferLocationKind = locationKind;
        info.fCachedReadOnlyState = isSystemFileReadOnly(info);
        if (file != null)
            info.fModel = createAnnotationModel(file);
        if (info.fModel == null)
            info.fModel = info.fTextFileBuffer.getAnnotationModel();
        setUpSynchronization(info);
        return info;
    }
    return null;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IFile(org.eclipse.core.resources.IFile) LocationKind(org.eclipse.core.filebuffers.LocationKind) IPath(org.eclipse.core.runtime.IPath) IFileInfo(org.eclipse.core.filesystem.IFileInfo) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IFileStore(org.eclipse.core.filesystem.IFileStore) URI(java.net.URI)

Example 48 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project eclipse.platform.text by eclipse.

the class TextSourceViewerConfiguration method getRegisteredHyperlinkDetectors.

/**
 * Returns the registered hyperlink detectors which are used to detect
 * hyperlinks in the given source viewer.
 *
 * @param sourceViewer the source viewer to be configured by this configuration
 * @return an array with hyperlink detectors or <code>null</code> if no hyperlink detectors are registered
 * @since 3.3
 */
protected final IHyperlinkDetector[] getRegisteredHyperlinkDetectors(ISourceViewer sourceViewer) {
    HyperlinkDetectorRegistry registry = EditorsUI.getHyperlinkDetectorRegistry();
    Map<String, IAdaptable> targets = getHyperlinkDetectorTargets(sourceViewer);
    Assert.isNotNull(targets);
    IHyperlinkDetector[] result = null;
    Iterator<Entry<String, IAdaptable>> iter = targets.entrySet().iterator();
    while (iter.hasNext()) {
        Entry<String, IAdaptable> target = iter.next();
        String targetId = target.getKey();
        IAdaptable context = target.getValue();
        result = merge(result, registry.createHyperlinkDetectors(targetId, context));
    }
    return result;
}
Also used : HyperlinkDetectorRegistry(org.eclipse.ui.texteditor.HyperlinkDetectorRegistry) IAdaptable(org.eclipse.core.runtime.IAdaptable) Entry(java.util.Map.Entry) IHyperlinkDetector(org.eclipse.jface.text.hyperlink.IHyperlinkDetector)

Example 49 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project eclipse.platform.text by eclipse.

the class FileBufferOperationAction method selectionChanged.

@Override
public void selectionChanged(IAction action, ISelection selection) {
    fResources = new HashSet<>();
    fLocation = null;
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        Iterator<?> e = structuredSelection.iterator();
        while (e.hasNext()) {
            Object element = e.next();
            if (element instanceof IResource)
                fResources.add(element);
            else if (element instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable) element;
                Object adapter = adaptable.getAdapter(IResource.class);
                if (adapter instanceof IResource)
                    fResources.add(adapter);
            }
        }
    }
    if (selection instanceof ITextSelection) {
        IWorkbenchWindow window = getWorkbenchWindow();
        if (window != null) {
            IWorkbenchPart workbenchPart = window.getPartService().getActivePart();
            if (workbenchPart instanceof IEditorPart) {
                IEditorPart editorPart = (IEditorPart) workbenchPart;
                IEditorInput input = editorPart.getEditorInput();
                Object adapter = input.getAdapter(IResource.class);
                if (adapter instanceof IResource)
                    fResources.add(adapter);
                else {
                    adapter = input.getAdapter(ILocationProvider.class);
                    if (adapter instanceof ILocationProvider) {
                        ILocationProvider provider = (ILocationProvider) adapter;
                        fLocation = provider.getPath(input);
                    }
                }
            }
        }
    }
    action.setText(getText());
    action.setEnabled(!fResources.isEmpty() || fLocation != null);
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) IResource(org.eclipse.core.resources.IResource) ITextSelection(org.eclipse.jface.text.ITextSelection) IEditorInput(org.eclipse.ui.IEditorInput)

Example 50 with IAdaptable

use of org.eclipse.core.runtime.IAdaptable in project eclipse.platform.text by eclipse.

the class TextViewerUndoManagerTest method internalTestTransferNonTextOp.

// --- DocumentUndoManager only ---
public void internalTestTransferNonTextOp(final boolean isUndoable) throws Exception {
    Object context = new Object();
    DocumentUndoManager tempUndoManager = new DocumentUndoManager(new Document());
    tempUndoManager.connect(context);
    IUndoableOperation operation = new AbstractOperation("") {

        @Override
        public boolean canUndo() {
            return isUndoable;
        }

        @Override
        public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            return Status.OK_STATUS;
        }

        @Override
        public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            return Status.OK_STATUS;
        }

        @Override
        public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            return Status.OK_STATUS;
        }
    };
    operation.addContext(tempUndoManager.getUndoContext());
    OperationHistoryFactory.getOperationHistory().add(operation);
    assertEquals(isUndoable, tempUndoManager.undoable());
    final DocumentUndoManager undoManager = new DocumentUndoManager(new Document());
    Object newContext = new Object();
    undoManager.connect(newContext);
    undoManager.addDocumentUndoListener(new IDocumentUndoListener() {

        @Override
        public void documentUndoNotification(DocumentUndoEvent event) {
            fail();
        }
    });
    undoManager.transferUndoHistory(tempUndoManager);
    tempUndoManager.disconnect(context);
    assertEquals(isUndoable, undoManager.undoable());
    undoManager.undo();
    assertEquals(false, undoManager.undoable());
    undoManager.disconnect(newContext);
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AbstractOperation(org.eclipse.core.commands.operations.AbstractOperation) DocumentUndoEvent(org.eclipse.text.undo.DocumentUndoEvent) IUndoableOperation(org.eclipse.core.commands.operations.IUndoableOperation) IDocumentUndoListener(org.eclipse.text.undo.IDocumentUndoListener) DocumentUndoManager(org.eclipse.text.undo.DocumentUndoManager) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IAdaptable (org.eclipse.core.runtime.IAdaptable)58 IResource (org.eclipse.core.resources.IResource)21 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)15 IFile (org.eclipse.core.resources.IFile)13 IProject (org.eclipse.core.resources.IProject)12 ISelection (org.eclipse.jface.viewers.ISelection)8 CoreException (org.eclipse.core.runtime.CoreException)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IEditorInput (org.eclipse.ui.IEditorInput)6 List (java.util.List)5 ExecutionException (org.eclipse.core.commands.ExecutionException)5 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)5 Iterator (java.util.Iterator)4 IPath (org.eclipse.core.runtime.IPath)4 IStatus (org.eclipse.core.runtime.IStatus)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)4 IEditorPart (org.eclipse.ui.IEditorPart)4 IOException (java.io.IOException)3 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)3