KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > StructuredViewerManager


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.navigator.extensions;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.viewers.IStructuredContentProvider;
17 import org.eclipse.jface.viewers.Viewer;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
20 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
21 import org.eclipse.ui.progress.UIJob;
22
23 /**
24  * <p>
25  * Provides a consistent mechanism to interact with StructuredViewers over time.
26  * The Common Navigator framework attempts to defer the loading of extensions,
27  * which also means defering the loading of Content Providers. To follow the
28  * contracts already in place by
29  * {@link org.eclipse.jface.viewers.ITreeContentProvider}, the Viewer, Old
30  * Input, and New Input parameters for
31  * {@link org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)}
32  * are cached for content providers that have not been loaded yet.
33  * </p>
34  * <p>
35  * <b>WARNING: </b> The following class is not inherently thread-safe.
36  * Appropriate measures should be taken to ensure that
37  * {@link #inputChanged(Object, Object)}and
38  * {@link #inputChanged(Viewer, Object, Object)}are not called concurrently
39  * with {@link #initialize(IStructuredContentProvider)}.
40  *
41  *
42  *
43  * @since 3.2
44  */

45 public class StructuredViewerManager {
46
47     private Viewer viewer;
48
49     private Object JavaDoc cachedOldInput;
50
51     private Object JavaDoc cachedNewInput;
52     
53     private UIJob refreshJob = new UIJob(
54             CommonNavigatorMessages.StructuredViewerManager_0) {
55         public IStatus runInUIThread(IProgressMonitor monitor) {
56             if(viewer != null) {
57                 try {
58                     if (viewer.getControl().isDisposed()) {
59                         return Status.OK_STATUS;
60                     }
61     
62                     
63                     Display display = viewer.getControl().getDisplay();
64                     if (!display.isDisposed() && viewer != null) {
65                         try {
66                             viewer.getControl().setRedraw(false);
67                             viewer.refresh();
68                         } finally {
69                             viewer.getControl().setRedraw(true);
70                         }
71                          
72                     }
73                 } catch (RuntimeException JavaDoc e) {
74                     NavigatorPlugin.logError(0, e.toString(), e);
75                 }
76             }
77             return Status.OK_STATUS;
78         }
79     };
80
81     /**
82      *
83      * @param aViewer
84      */

85     public StructuredViewerManager(Viewer aViewer) {
86         super();
87         viewer = aViewer;
88         refreshJob.setSystem(true);
89     }
90
91     /**
92      *
93      * @return The real viewer.
94      */

95     public Viewer getViewer() {
96         return viewer;
97     }
98
99     /**
100      *
101      * @param anOldInput
102      * @param aNewInput
103      */

104     public void inputChanged(Object JavaDoc anOldInput, Object JavaDoc aNewInput) {
105         cachedOldInput = anOldInput;
106         cachedNewInput = aNewInput;
107     }
108
109     /**
110      *
111      * @param aViewer
112      * @param anOldInput
113      * @param aNewInput
114      */

115     public void inputChanged(Viewer aViewer, Object JavaDoc anOldInput, Object JavaDoc aNewInput) {
116         viewer = aViewer;
117         cachedOldInput = anOldInput;
118         cachedNewInput = aNewInput;
119     }
120
121     /**
122      *
123      * @param aContentProvider
124      * @return True if all is well.
125      */

126     public boolean initialize(IStructuredContentProvider aContentProvider) {
127         boolean result = true;
128         try {
129             if (aContentProvider != null) {
130                 aContentProvider.inputChanged(viewer, cachedOldInput,
131                         cachedNewInput);
132             }
133         } catch (RuntimeException JavaDoc 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 }
149
Popular Tags