KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > PartList


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.ui.IEditorPart;
15 import org.eclipse.ui.IEditorReference;
16 import org.eclipse.ui.IPropertyListener;
17 import org.eclipse.ui.ISaveablesLifecycleListener;
18 import org.eclipse.ui.IWorkbenchPart;
19 import org.eclipse.ui.IWorkbenchPartConstants;
20 import org.eclipse.ui.IWorkbenchPartReference;
21 import org.eclipse.ui.IWorkbenchPartSite;
22 import org.eclipse.ui.part.MultiEditor;
23
24 public abstract class PartList {
25     private IWorkbenchPartReference activePartReference;
26
27     private IEditorReference activeEditorReference;
28
29     // private List parts = new ArrayList();
30

31     private IPropertyListener listener = new IPropertyListener() {
32         public void propertyChanged(Object JavaDoc source, int propId) {
33             WorkbenchPartReference ref = (WorkbenchPartReference) source;
34
35             switch (propId) {
36             case WorkbenchPartReference.INTERNAL_PROPERTY_OPENED:
37                 partOpened(ref);
38                 break;
39             case WorkbenchPartReference.INTERNAL_PROPERTY_CLOSED:
40                 partClosed(ref);
41                 break;
42             case WorkbenchPartReference.INTERNAL_PROPERTY_VISIBLE: {
43                 if (ref.getVisible()) {
44                     partVisible(ref);
45                 } else {
46                     partHidden(ref);
47                 }
48                 break;
49             }
50             case IWorkbenchPartConstants.PROP_INPUT: {
51                 partInputChanged(ref);
52                 break;
53             }
54             }
55         }
56     };
57
58     public IWorkbenchPartReference getActivePartReference() {
59         return activePartReference;
60     }
61
62     public IEditorReference getActiveEditorReference() {
63         return activeEditorReference;
64     }
65
66     public IEditorPart getActiveEditor() {
67         return activeEditorReference == null ? null : activeEditorReference
68                 .getEditor(false);
69     }
70
71     public IWorkbenchPart getActivePart() {
72         return activePartReference == null ? null : activePartReference
73                 .getPart(false);
74     }
75
76     public void addPart(WorkbenchPartReference ref) {
77         Assert.isNotNull(ref);
78
79         ref.addInternalPropertyListener(listener);
80
81         // parts.add(ref);
82
firePartAdded(ref);
83
84         // If this part is already open, fire the "part opened" event
85
// immediately
86
if (ref.getPart(false) != null) {
87             partOpened(ref);
88         }
89
90         // If this part is already visible, fire the visibility event
91
// immediately
92
if (ref.getVisible()) {
93             partVisible(ref);
94         }
95     }
96
97     /**
98      * Sets the active part.
99      *
100      * @param ref
101      */

102     public void setActivePart(IWorkbenchPartReference ref) {
103         if (ref == activePartReference) {
104             return;
105         }
106
107         IWorkbenchPartReference oldPart = activePartReference;
108
109         // A part can't be activated until it is added
110
// Assert.isTrue(ref == null || parts.contains(ref));
111

112         if (ref != null) {
113             IWorkbenchPart part = ref.getPart(true);
114             Assert.isNotNull(part);
115             if (part instanceof MultiEditor) {
116                 IWorkbenchPartSite site = ((MultiEditor) part)
117                         .getActiveEditor().getSite();
118                 if (site instanceof PartSite) {
119                     ref = ((PartSite) site).getPane().getPartReference();
120                 }
121             }
122         }
123
124         activePartReference = ref;
125
126         fireActivePartChanged(oldPart, ref);
127     }
128
129     public void setActiveEditor(IEditorReference ref) {
130         if (ref == activeEditorReference) {
131             return;
132         }
133
134         // A part can't be activated until it is added
135
// Assert.isTrue(ref == null || parts.contains(ref));
136

137         if (ref != null) {
138             IWorkbenchPart part = ref.getPart(true);
139             Assert.isNotNull(part);
140             if (part instanceof MultiEditor) {
141                 IWorkbenchPartSite site = ((MultiEditor) part)
142                         .getActiveEditor().getSite();
143                 if (site instanceof PartSite) {
144                     ref = (IEditorReference) ((PartSite) site).getPane()
145                             .getPartReference();
146                 }
147             }
148         }
149
150         activeEditorReference = ref;
151
152         fireActiveEditorChanged(ref);
153     }
154
155     /**
156      * In order to remove a part, it must first be deactivated.
157      */

158     public void removePart(WorkbenchPartReference ref) {
159         Assert.isNotNull(ref);
160         // It is an error to remove a part that isn't in the list
161
// Assert.isTrue(parts.contains(ref));
162
// We're not allowed to remove the active part. We must deactivate it
163
// first.
164
Assert.isTrue(ref != activePartReference);
165         // We're not allowed to remove the active editor. We must deactivate it
166
// first.
167
Assert.isTrue(ref != activeEditorReference);
168
169         if (ref.getVisible()) {
170             ref.setVisible(false);
171         }
172
173         // If this part is currently open, fire the "part closed" event before
174
// removal
175
if (ref.getPart(false) != null) {
176             partClosed(ref);
177         }
178
179         ref.removeInternalPropertyListener(listener);
180
181         firePartRemoved(ref);
182     }
183
184     private void partInputChanged(WorkbenchPartReference ref) {
185         firePartInputChanged(ref);
186     }
187
188     private void partHidden(WorkbenchPartReference ref) {
189         // Part should not be null
190
Assert.isNotNull(ref);
191         // This event should only be fired if the part is actually visible
192
Assert.isTrue(!ref.getVisible());
193         // We shouldn't be receiving events from parts until they are in the
194
// list
195
// Assert.isTrue(parts.contains(ref));
196

197         firePartHidden(ref);
198     }
199
200     private void partOpened(WorkbenchPartReference ref) {
201         Assert.isNotNull(ref);
202
203         IWorkbenchPart actualPart = ref.getPart(false);
204         // We're firing the event that says "the part was just created"... so
205
// there better be a part there.
206
Assert.isNotNull(actualPart);
207         // Must be called after the part is inserted into the part list
208
// Assert.isTrue(parts.contains(ref));
209
// The active part must be opened before it is activated, so we should
210
// never get an
211
// open event for a part that is already active. (This either indicates
212
// that a redundant
213
// open event was fired or that a closed part was somehow activated)
214
Assert.isTrue(activePartReference != ref);
215         // The active editor must be opened before it is activated, so we should
216
// never get an
217
// open event for an editor that is already active. (This either
218
// indicates that a redundant
219
// open event was fired or that a closed editor was somehow activated)
220
Assert.isTrue(activeEditorReference != ref);
221
222         SaveablesList modelManager = (SaveablesList) actualPart
223                 .getSite().getService(ISaveablesLifecycleListener.class);
224         modelManager.postOpen(actualPart);
225
226         // Fire the "part opened" event
227
firePartOpened(ref);
228     }
229
230     /**
231      * Called when a concrete part is about to be destroyed. This is called
232      * BEFORE disposal happens, so the part should still be accessable from the
233      * part reference.
234      *
235      * @param ref
236      */

237     private void partClosed(WorkbenchPartReference ref) {
238         Assert.isNotNull(ref);
239
240         IWorkbenchPart actualPart = ref.getPart(false);
241         // Called before the part is disposed, so the part should still be
242
// there.
243
Assert.isNotNull(actualPart);
244         // Must be called before the part is actually removed from the part list
245
// Assert.isTrue(parts.contains(ref));
246
// Not allowed to close the active part. The part must be deactivated
247
// before it may
248
// be closed.
249
Assert.isTrue(activePartReference != ref);
250         // Not allowed to close the active editor. The editor must be
251
// deactivated before it may
252
// be closed.
253
Assert.isTrue(activeEditorReference != ref);
254
255         firePartClosed(ref);
256     }
257
258     private void partVisible(WorkbenchPartReference ref) {
259         // Part should not be null
260
Assert.isNotNull(ref);
261         // This event should only be fired if the part is actually visible
262
Assert.isTrue(ref.getVisible());
263         // We shouldn't be receiving events from parts until they are in the
264
// list
265
// Assert.isTrue(parts.contains(ref));
266
// Part must be open before it can be made visible
267
Assert.isNotNull(ref.getPart(false));
268
269         firePartVisible(ref);
270     }
271
272     /**
273      * Fire the event indicating that a part reference was just realized. That
274      * is, the concrete IWorkbenchPart has been attached to the part reference.
275      *
276      * @param part
277      * the reference that was create
278      */

279     protected abstract void firePartOpened(IWorkbenchPartReference part);
280
281     /**
282      * Fire the event indicating that a part reference was just realized. That
283      * is, the concrete IWorkbenchPart has been attached to the part reference.
284      *
285      * @param part
286      * the reference that was create
287      */

288     protected abstract void firePartClosed(IWorkbenchPartReference part);
289
290     /**
291      * Indicates that a new part reference was added to the list.
292      *
293      * @param part
294      */

295     protected abstract void firePartAdded(IWorkbenchPartReference part);
296
297     /**
298      * Indicates that a part reference was removed from the list
299      *
300      * @param part
301      */

302     protected abstract void firePartRemoved(IWorkbenchPartReference part);
303
304     /**
305      * Indicates that the active editor changed
306      *
307      * @param part
308      * active part reference or null if none
309      */

310     protected abstract void fireActiveEditorChanged(IWorkbenchPartReference ref);
311
312     /**
313      * Indicates that the active part has changed
314      *
315      * @param part
316      * active part reference or null if none
317      */

318     protected abstract void fireActivePartChanged(
319             IWorkbenchPartReference oldPart, IWorkbenchPartReference newPart);
320
321     /**
322      * Indicates that the part has been made visible
323      *
324      * @param ref
325      */

326     protected abstract void firePartVisible(IWorkbenchPartReference ref);
327
328     /**
329      * Indicates that the part has been hidden
330      *
331      * @param ref
332      */

333     protected abstract void firePartHidden(IWorkbenchPartReference ref);
334
335     /**
336      * Indicates that the part input has changed
337      *
338      * @param ref
339      */

340     protected abstract void firePartInputChanged(IWorkbenchPartReference ref);
341
342     protected abstract void firePartBroughtToTop(IWorkbenchPartReference ref);
343 }
344
Popular Tags