KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > MultiEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.part;
13
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.resource.ColorRegistry;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.layout.FillLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Event;
21 import org.eclipse.swt.widgets.Listener;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IEditorSite;
25 import org.eclipse.ui.IPartListener2;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchPartReference;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.internal.EditorSite;
30 import org.eclipse.ui.internal.IWorkbenchThemeConstants;
31 import org.eclipse.ui.internal.PartService;
32 import org.eclipse.ui.internal.PartSite;
33 import org.eclipse.ui.internal.WorkbenchPage;
34 import org.eclipse.ui.internal.WorkbenchWindow;
35 import org.eclipse.ui.themes.ITheme;
36
37 /**
38  * A MultiEditor is a composite of editors.
39  *
40  * This class is intended to be subclassed.
41  *
42  */

43 public abstract class MultiEditor extends EditorPart {
44
45     private int activeEditorIndex;
46
47     private IEditorPart innerEditors[];
48
49     private IPartListener2 propagationListener;
50
51     /**
52      * Constructor for TileEditor.
53      */

54     public MultiEditor() {
55         super();
56     }
57
58     /*
59      * @see IEditorPart#doSave(IProgressMonitor)
60      */

61     public void doSave(IProgressMonitor monitor) {
62         for (int i = 0; i < innerEditors.length; i++) {
63             IEditorPart e = innerEditors[i];
64             e.doSave(monitor);
65         }
66     }
67
68     /**
69      * Create the control of the inner editor.
70      *
71      * Must be called by subclass.
72      */

73     public Composite createInnerPartControl(Composite parent,
74             final IEditorPart e) {
75         Composite content = new Composite(parent, SWT.NONE);
76         content.setLayout(new FillLayout());
77         e.createPartControl(content);
78         parent.addListener(SWT.Activate, new Listener() {
79             public void handleEvent(Event event) {
80                 if (event.type == SWT.Activate) {
81                     activateEditor(e);
82                 }
83             }
84         });
85         return content;
86     }
87
88     /*
89      * @see IEditorPart#doSaveAs()
90      */

91     public void doSaveAs() {
92         //no-op
93
}
94
95     /*
96      * @see IEditorPart#init(IEditorSite, IEditorInput)
97      */

98     public void init(IEditorSite site, IEditorInput input)
99             throws PartInitException {
100         init(site, (MultiEditorInput) input);
101     }
102
103     /*
104      * @see IEditorPart#init(IEditorSite, IEditorInput)
105      */

106     public void init(IEditorSite site, MultiEditorInput input)
107             throws PartInitException {
108         setInput(input);
109         setSite(site);
110         setPartName(input.getName());
111         setTitleToolTip(input.getToolTipText());
112         setupEvents();
113     }
114
115     /*
116      * @see IEditorPart#isDirty()
117      */

118     public boolean isDirty() {
119         for (int i = 0; i < innerEditors.length; i++) {
120             IEditorPart e = innerEditors[i];
121             if (e.isDirty()) {
122                 return true;
123             }
124         }
125         return false;
126     }
127
128     /*
129      * @see IEditorPart#isSaveAsAllowed()
130      */

131     public boolean isSaveAsAllowed() {
132         return false;
133     }
134
135     /*
136      * @see IWorkbenchPart#setFocus()
137      */

138     public void setFocus() {
139         innerEditors[activeEditorIndex].setFocus();
140         updateGradient(innerEditors[activeEditorIndex]);
141     }
142
143     /**
144      * Returns the active inner editor.
145      */

146     public final IEditorPart getActiveEditor() {
147         return innerEditors[activeEditorIndex];
148     }
149
150     /**
151      * Returns an array with all inner editors.
152      */

153     public final IEditorPart[] getInnerEditors() {
154         return innerEditors;
155     }
156
157     /**
158      * Set the inner editors.
159      *
160      * Should not be called by clients.
161      */

162     public final void setChildren(IEditorPart[] children) {
163         innerEditors = children;
164         activeEditorIndex = 0;
165     }
166
167     /**
168      * Activates the given nested editor.
169      *
170      * @param part the nested editor
171      * @since 3.0
172      */

173     protected void activateEditor(IEditorPart part) {
174         IEditorPart oldEditor = getActiveEditor();
175         activeEditorIndex = getIndex(part);
176         IEditorPart e = getActiveEditor();
177         EditorSite innerSite = (EditorSite) e.getEditorSite();
178         ((WorkbenchPage) innerSite.getPage()).requestActivation(e);
179         updateGradient(oldEditor);
180     }
181
182     /**
183      * Returns the index of the given nested editor.
184      *
185      * @return the index of the nested editor
186      * @since 3.0
187      */

188     protected int getIndex(IEditorPart editor) {
189         for (int i = 0; i < innerEditors.length; i++) {
190             if (innerEditors[i] == editor) {
191                 return i;
192             }
193         }
194         return -1;
195     }
196
197     /**
198      * Updates the gradient in the title bar.
199      */

200     public void updateGradient(IEditorPart editor) {
201         boolean activeEditor = editor == getSite().getPage().getActiveEditor();
202         boolean activePart = editor == getSite().getPage().getActivePart();
203
204         ITheme theme = editor.getEditorSite().getWorkbenchWindow()
205                 .getWorkbench().getThemeManager().getCurrentTheme();
206         Gradient g = new Gradient();
207
208         ColorRegistry colorRegistry = theme.getColorRegistry();
209         if (activePart) {
210             g.fgColor = colorRegistry
211                     .get(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR);
212             g.bgColors = new Color[2];
213             g.bgColors[0] = colorRegistry
214                     .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START);
215             g.bgColors[1] = colorRegistry
216                     .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END);
217         } else {
218             if (activeEditor) {
219                 g.fgColor = colorRegistry
220                         .get(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR);
221                 g.bgColors = new Color[2];
222                 g.bgColors[0] = colorRegistry
223                         .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START);
224                 g.bgColors[1] = colorRegistry
225                         .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END);
226             } else {
227                 g.fgColor = colorRegistry
228                         .get(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR);
229                 g.bgColors = new Color[2];
230                 g.bgColors[0] = colorRegistry
231                         .get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START);
232                 g.bgColors[1] = colorRegistry
233                         .get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_END);
234             }
235         }
236         g.bgPercents = new int[] { theme
237                 .getInt(IWorkbenchThemeConstants.ACTIVE_TAB_PERCENT) };
238
239         drawGradient(editor, g);
240     }
241
242     /**
243      * Draw the gradient in the title bar.
244      */

245     protected abstract void drawGradient(IEditorPart innerEditor, Gradient g);
246
247     /**
248      * Return true if the shell is activated.
249      */

250     protected boolean getShellActivated() {
251         WorkbenchWindow window = (WorkbenchWindow) getSite().getPage()
252                 .getWorkbenchWindow();
253         return window.getShellActivated();
254     }
255
256     /**
257      * The colors used to draw the title bar of the inner editors
258      */

259     public static class Gradient {
260         public Color fgColor;
261
262         public Color[] bgColors;
263
264         public int[] bgPercents;
265     }
266     
267     
268     
269     /**
270      * Set up the MultiEditor to propagate events like partClosed().
271      *
272      * @since 3.2
273      */

274     private void setupEvents() {
275         propagationListener = new IPartListener2() {
276             public void partActivated(IWorkbenchPartReference partRef) {
277             }
278
279             public void partBroughtToTop(IWorkbenchPartReference partRef) {
280             }
281
282             public void partClosed(IWorkbenchPartReference partRef) {
283                 IWorkbenchPart part = partRef.getPart(false);
284                 if (part == MultiEditor.this && innerEditors != null) {
285                     PartService partService = ((WorkbenchPage) getSite()
286                             .getPage()).getPartService();
287                     for (int i = 0; i < innerEditors.length; i++) {
288                         IEditorPart editor = innerEditors[i];
289                         IWorkbenchPartReference innerRef = ((PartSite) editor
290                                 .getSite()).getPartReference();
291                         partService.firePartClosed(innerRef);
292                     }
293                 }
294             }
295
296             public void partDeactivated(IWorkbenchPartReference partRef) {
297             }
298
299             public void partOpened(IWorkbenchPartReference partRef) {
300                 IWorkbenchPart part = partRef.getPart(false);
301                 if (part == MultiEditor.this && innerEditors != null) {
302                     PartService partService = ((WorkbenchPage) getSite()
303                             .getPage()).getPartService();
304                     for (int i = 0; i < innerEditors.length; i++) {
305                         IEditorPart editor = innerEditors[i];
306                         IWorkbenchPartReference innerRef = ((PartSite) editor
307                                 .getSite()).getPartReference();
308                         partService.firePartOpened(innerRef);
309                     }
310                 }
311             }
312
313             public void partHidden(IWorkbenchPartReference partRef) {
314             }
315
316             public void partVisible(IWorkbenchPartReference partRef) {
317             }
318
319             public void partInputChanged(IWorkbenchPartReference partRef) {
320             }
321         };
322         getSite().getPage().addPartListener(propagationListener);
323     }
324
325     /**
326      * Release the added listener.
327      *
328      * @since 3.2
329      */

330     public void dispose() {
331         getSite().getPage().removePartListener(propagationListener);
332         super.dispose();
333     }
334 }
335
Popular Tags