Search in sources :

Example 6 with Saveable

use of org.eclipse.ui.Saveable in project polymap4-core by Polymap4.

the class NavigatorSaveablesService method getActiveSaveablesFromTreePathProvider.

/**
 * @param selection
 * @param provider
 * @return the active saveables
 */
private Saveable[] getActiveSaveablesFromTreePathProvider(IStructuredSelection selection, ITreePathContentProvider provider) {
    Set result = new HashSet();
    for (Iterator it = selection.iterator(); it.hasNext(); ) {
        Object element = it.next();
        Saveable saveable = getSaveable(element);
        if (saveable != null) {
            result.add(saveable);
        } else {
            TreePath[] paths = provider.getParents(element);
            saveable = findSaveable(paths);
            if (saveable != null) {
                result.add(saveable);
            }
        }
    }
    return (Saveable[]) result.toArray(new Saveable[result.size()]);
}
Also used : Saveable(org.eclipse.ui.Saveable) HashSet(java.util.HashSet) Set(java.util.Set) TreePath(org.eclipse.jface.viewers.TreePath) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 7 with Saveable

use of org.eclipse.ui.Saveable in project polymap4-core by Polymap4.

the class NavigatorSaveablesService method computeSaveables.

/**
 * helper to compute the saveables for which elements are part of the tree.
 * Must be called from a synchronized method.
 *
 * @return the saveables
 */
private Saveable[] computeSaveables() {
    ITreeContentProvider contentProvider = (ITreeContentProvider) viewer.getContentProvider();
    boolean isTreepathContentProvider = contentProvider instanceof ITreePathContentProvider;
    Object viewerInput = viewer.getInput();
    List result = new ArrayList();
    Set roots = new HashSet(Arrays.asList(contentProvider.getElements(viewerInput)));
    SaveablesProvider[] saveablesProviders = getSaveablesProviders();
    for (int i = 0; i < saveablesProviders.length; i++) {
        SaveablesProvider saveablesProvider = saveablesProviders[i];
        Saveable[] saveables = saveablesProvider.getSaveables();
        for (int j = 0; j < saveables.length; j++) {
            Saveable saveable = saveables[j];
            Object[] elements = saveablesProvider.getElements(saveable);
            // the saveable is added to the result if at least one of the
            // elements representing the saveable appears in the tree, i.e.
            // if its parent chain leads to a root node.
            boolean foundRoot = false;
            for (int k = 0; !foundRoot && k < elements.length; k++) {
                Object element = elements[k];
                if (roots.contains(element)) {
                    result.add(saveable);
                    foundRoot = true;
                } else if (isTreepathContentProvider) {
                    ITreePathContentProvider treePathContentProvider = (ITreePathContentProvider) contentProvider;
                    TreePath[] parentPaths = treePathContentProvider.getParents(element);
                    for (int l = 0; !foundRoot && l < parentPaths.length; l++) {
                        TreePath parentPath = parentPaths[l];
                        for (int m = 0; !foundRoot && m < parentPath.getSegmentCount(); m++) {
                            if (roots.contains(parentPath.getSegment(m))) {
                                result.add(saveable);
                                foundRoot = true;
                            }
                        }
                    }
                } else {
                    while (!foundRoot && element != null) {
                        if (roots.contains(element)) {
                            // found a parent chain leading to a root. The
                            // saveable is part of the tree.
                            result.add(saveable);
                            foundRoot = true;
                        } else {
                            element = contentProvider.getParent(element);
                        }
                    }
                }
            }
        }
    }
    return (Saveable[]) result.toArray(new Saveable[result.size()]);
}
Also used : ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) Saveable(org.eclipse.ui.Saveable) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) TreePath(org.eclipse.jface.viewers.TreePath) ITreePathContentProvider(org.eclipse.jface.viewers.ITreePathContentProvider) ArrayList(java.util.ArrayList) List(java.util.List) SaveablesProvider(org.eclipse.ui.navigator.SaveablesProvider) HashSet(java.util.HashSet)

Example 8 with Saveable

use of org.eclipse.ui.Saveable in project polymap4-core by Polymap4.

the class NavigatorSaveablesService method recomputeSaveablesAndNotify.

private void recomputeSaveablesAndNotify(boolean recomputeProviders, String startedBundleIdOrNull) {
    if (recomputeProviders && startedBundleIdOrNull == null && saveablesProviders != null) {
        // TODO optimize this
        for (int i = 0; i < saveablesProviders.length; i++) {
            saveablesProviders[i].dispose();
        }
        saveablesProviders = null;
    } else if (startedBundleIdOrNull != null) {
        if (inactivePluginsWithSaveablesProviders.containsKey(startedBundleIdOrNull)) {
            updateSaveablesProviders(startedBundleIdOrNull);
        }
    }
    Set oldSaveables = new HashSet(Arrays.asList(currentSaveables));
    currentSaveables = computeSaveables();
    Set newSaveables = new HashSet(Arrays.asList(currentSaveables));
    final Set removedSaveables = new HashSet(oldSaveables);
    removedSaveables.removeAll(newSaveables);
    final Set addedSaveables = new HashSet(newSaveables);
    addedSaveables.removeAll(oldSaveables);
    if (addedSaveables.size() > 0) {
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                if (isDisposed()) {
                    return;
                }
                outsideListener.handleLifecycleEvent(new SaveablesLifecycleEvent(saveablesSource, SaveablesLifecycleEvent.POST_OPEN, (Saveable[]) addedSaveables.toArray(new Saveable[addedSaveables.size()]), false));
            }
        });
    }
    // an appropriate PRE_CLOSE
    if (removedSaveables.size() > 0) {
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                if (isDisposed()) {
                    return;
                }
                outsideListener.handleLifecycleEvent(new SaveablesLifecycleEvent(saveablesSource, SaveablesLifecycleEvent.PRE_CLOSE, (Saveable[]) removedSaveables.toArray(new Saveable[removedSaveables.size()]), true));
                outsideListener.handleLifecycleEvent(new SaveablesLifecycleEvent(saveablesSource, SaveablesLifecycleEvent.POST_CLOSE, (Saveable[]) removedSaveables.toArray(new Saveable[removedSaveables.size()]), false));
            }
        });
    }
}
Also used : Saveable(org.eclipse.ui.Saveable) HashSet(java.util.HashSet) Set(java.util.Set) HashSet(java.util.HashSet) SaveablesLifecycleEvent(org.eclipse.ui.SaveablesLifecycleEvent)

Aggregations

Saveable (org.eclipse.ui.Saveable)8 HashSet (java.util.HashSet)5 Set (java.util.Set)5 Iterator (java.util.Iterator)3 TreePath (org.eclipse.jface.viewers.TreePath)3 SaveablesProvider (org.eclipse.ui.navigator.SaveablesProvider)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ITreeContentProvider (org.eclipse.jface.viewers.ITreeContentProvider)1 ITreePathContentProvider (org.eclipse.jface.viewers.ITreePathContentProvider)1 SaveablesLifecycleEvent (org.eclipse.ui.SaveablesLifecycleEvent)1 NavigatorContentDescriptor (org.eclipse.ui.internal.navigator.extensions.NavigatorContentDescriptor)1 INavigatorContentDescriptor (org.eclipse.ui.navigator.INavigatorContentDescriptor)1