001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.navigator.extensions;
011:
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.jface.viewers.IStructuredContentProvider;
016: import org.eclipse.jface.viewers.Viewer;
017: import org.eclipse.swt.widgets.Display;
018: import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
019: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
020: import org.eclipse.ui.progress.UIJob;
021:
022: /**
023: * <p>
024: * Provides a consistent mechanism to interact with StructuredViewers over time.
025: * The Common Navigator framework attempts to defer the loading of extensions,
026: * which also means defering the loading of Content Providers. To follow the
027: * contracts already in place by
028: * {@link org.eclipse.jface.viewers.ITreeContentProvider}, the Viewer, Old
029: * Input, and New Input parameters for
030: * {@link org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)}
031: * are cached for content providers that have not been loaded yet.
032: * </p>
033: * <p>
034: * <b>WARNING: </b> The following class is not inherently thread-safe.
035: * Appropriate measures should be taken to ensure that
036: * {@link #inputChanged(Object, Object)}and
037: * {@link #inputChanged(Viewer, Object, Object)}are not called concurrently
038: * with {@link #initialize(IStructuredContentProvider)}.
039: *
040: *
041: *
042: * @since 3.2
043: */
044: public class StructuredViewerManager {
045:
046: private Viewer viewer;
047:
048: private Object cachedOldInput;
049:
050: private Object cachedNewInput;
051:
052: private UIJob refreshJob = new UIJob(
053: CommonNavigatorMessages.StructuredViewerManager_0) {
054: public IStatus runInUIThread(IProgressMonitor monitor) {
055: if (viewer != null) {
056: try {
057: if (viewer.getControl().isDisposed()) {
058: return Status.OK_STATUS;
059: }
060:
061: Display display = viewer.getControl().getDisplay();
062: if (!display.isDisposed() && viewer != null) {
063: try {
064: viewer.getControl().setRedraw(false);
065: viewer.refresh();
066: } finally {
067: viewer.getControl().setRedraw(true);
068: }
069:
070: }
071: } catch (RuntimeException e) {
072: NavigatorPlugin.logError(0, e.toString(), e);
073: }
074: }
075: return Status.OK_STATUS;
076: }
077: };
078:
079: /**
080: *
081: * @param aViewer
082: */
083: public StructuredViewerManager(Viewer aViewer) {
084: super ();
085: viewer = aViewer;
086: refreshJob.setSystem(true);
087: }
088:
089: /**
090: *
091: * @return The real viewer.
092: */
093: public Viewer getViewer() {
094: return viewer;
095: }
096:
097: /**
098: *
099: * @param anOldInput
100: * @param aNewInput
101: */
102: public void inputChanged(Object anOldInput, Object aNewInput) {
103: cachedOldInput = anOldInput;
104: cachedNewInput = aNewInput;
105: }
106:
107: /**
108: *
109: * @param aViewer
110: * @param anOldInput
111: * @param aNewInput
112: */
113: public void inputChanged(Viewer aViewer, Object anOldInput,
114: Object aNewInput) {
115: viewer = aViewer;
116: cachedOldInput = anOldInput;
117: cachedNewInput = aNewInput;
118: }
119:
120: /**
121: *
122: * @param aContentProvider
123: * @return True if all is well.
124: */
125: public boolean initialize(
126: IStructuredContentProvider aContentProvider) {
127: boolean result = true;
128: try {
129: if (aContentProvider != null) {
130: aContentProvider.inputChanged(viewer, cachedOldInput,
131: cachedNewInput);
132: }
133: } catch (RuntimeException e) {
134: NavigatorPlugin.logError(0, e.toString(), e);
135: result = false;
136: }
137: return result;
138: }
139:
140: /**
141: *
142: */
143: public void safeRefresh() {
144: refreshJob.schedule(10);
145:
146: }
147:
148: }
|