001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.part;
011:
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.jface.resource.ColorRegistry;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.graphics.Color;
016: import org.eclipse.swt.layout.FillLayout;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Event;
019: import org.eclipse.swt.widgets.Listener;
020: import org.eclipse.ui.IEditorInput;
021: import org.eclipse.ui.IEditorPart;
022: import org.eclipse.ui.IEditorSite;
023: import org.eclipse.ui.IPartListener2;
024: import org.eclipse.ui.IWorkbenchPart;
025: import org.eclipse.ui.IWorkbenchPartReference;
026: import org.eclipse.ui.PartInitException;
027: import org.eclipse.ui.internal.EditorSite;
028: import org.eclipse.ui.internal.IWorkbenchThemeConstants;
029: import org.eclipse.ui.internal.PartService;
030: import org.eclipse.ui.internal.PartSite;
031: import org.eclipse.ui.internal.WorkbenchPage;
032: import org.eclipse.ui.internal.WorkbenchWindow;
033: import org.eclipse.ui.themes.ITheme;
034:
035: /**
036: * A MultiEditor is a composite of editors.
037: *
038: * This class is intended to be subclassed.
039: *
040: */
041: public abstract class MultiEditor extends EditorPart {
042:
043: private int activeEditorIndex;
044:
045: private IEditorPart innerEditors[];
046:
047: private IPartListener2 propagationListener;
048:
049: /**
050: * Constructor for TileEditor.
051: */
052: public MultiEditor() {
053: super ();
054: }
055:
056: /*
057: * @see IEditorPart#doSave(IProgressMonitor)
058: */
059: public void doSave(IProgressMonitor monitor) {
060: for (int i = 0; i < innerEditors.length; i++) {
061: IEditorPart e = innerEditors[i];
062: e.doSave(monitor);
063: }
064: }
065:
066: /**
067: * Create the control of the inner editor.
068: *
069: * Must be called by subclass.
070: */
071: public Composite createInnerPartControl(Composite parent,
072: final IEditorPart e) {
073: Composite content = new Composite(parent, SWT.NONE);
074: content.setLayout(new FillLayout());
075: e.createPartControl(content);
076: parent.addListener(SWT.Activate, new Listener() {
077: public void handleEvent(Event event) {
078: if (event.type == SWT.Activate) {
079: activateEditor(e);
080: }
081: }
082: });
083: return content;
084: }
085:
086: /*
087: * @see IEditorPart#doSaveAs()
088: */
089: public void doSaveAs() {
090: //no-op
091: }
092:
093: /*
094: * @see IEditorPart#init(IEditorSite, IEditorInput)
095: */
096: public void init(IEditorSite site, IEditorInput input)
097: throws PartInitException {
098: init(site, (MultiEditorInput) input);
099: }
100:
101: /*
102: * @see IEditorPart#init(IEditorSite, IEditorInput)
103: */
104: public void init(IEditorSite site, MultiEditorInput input)
105: throws PartInitException {
106: setInput(input);
107: setSite(site);
108: setPartName(input.getName());
109: setTitleToolTip(input.getToolTipText());
110: setupEvents();
111: }
112:
113: /*
114: * @see IEditorPart#isDirty()
115: */
116: public boolean isDirty() {
117: for (int i = 0; i < innerEditors.length; i++) {
118: IEditorPart e = innerEditors[i];
119: if (e.isDirty()) {
120: return true;
121: }
122: }
123: return false;
124: }
125:
126: /*
127: * @see IEditorPart#isSaveAsAllowed()
128: */
129: public boolean isSaveAsAllowed() {
130: return false;
131: }
132:
133: /*
134: * @see IWorkbenchPart#setFocus()
135: */
136: public void setFocus() {
137: innerEditors[activeEditorIndex].setFocus();
138: updateGradient(innerEditors[activeEditorIndex]);
139: }
140:
141: /**
142: * Returns the active inner editor.
143: */
144: public final IEditorPart getActiveEditor() {
145: return innerEditors[activeEditorIndex];
146: }
147:
148: /**
149: * Returns an array with all inner editors.
150: */
151: public final IEditorPart[] getInnerEditors() {
152: return innerEditors;
153: }
154:
155: /**
156: * Set the inner editors.
157: *
158: * Should not be called by clients.
159: */
160: public final void setChildren(IEditorPart[] children) {
161: innerEditors = children;
162: activeEditorIndex = 0;
163: }
164:
165: /**
166: * Activates the given nested editor.
167: *
168: * @param part the nested editor
169: * @since 3.0
170: */
171: protected void activateEditor(IEditorPart part) {
172: IEditorPart oldEditor = getActiveEditor();
173: activeEditorIndex = getIndex(part);
174: IEditorPart e = getActiveEditor();
175: EditorSite innerSite = (EditorSite) e.getEditorSite();
176: ((WorkbenchPage) innerSite.getPage()).requestActivation(e);
177: updateGradient(oldEditor);
178: }
179:
180: /**
181: * Returns the index of the given nested editor.
182: *
183: * @return the index of the nested editor
184: * @since 3.0
185: */
186: protected int getIndex(IEditorPart editor) {
187: for (int i = 0; i < innerEditors.length; i++) {
188: if (innerEditors[i] == editor) {
189: return i;
190: }
191: }
192: return -1;
193: }
194:
195: /**
196: * Updates the gradient in the title bar.
197: */
198: public void updateGradient(IEditorPart editor) {
199: boolean activeEditor = editor == getSite().getPage()
200: .getActiveEditor();
201: boolean activePart = editor == getSite().getPage()
202: .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,
246: Gradient g);
247:
248: /**
249: * Return true if the shell is activated.
250: */
251: protected boolean getShellActivated() {
252: WorkbenchWindow window = (WorkbenchWindow) getSite().getPage()
253: .getWorkbenchWindow();
254: return window.getShellActivated();
255: }
256:
257: /**
258: * The colors used to draw the title bar of the inner editors
259: */
260: public static class Gradient {
261: public Color fgColor;
262:
263: public Color[] bgColors;
264:
265: public int[] bgPercents;
266: }
267:
268: /**
269: * Set up the MultiEditor to propagate events like partClosed().
270: *
271: * @since 3.2
272: */
273: private void setupEvents() {
274: propagationListener = new IPartListener2() {
275: public void partActivated(IWorkbenchPartReference partRef) {
276: }
277:
278: public void partBroughtToTop(IWorkbenchPartReference partRef) {
279: }
280:
281: public void partClosed(IWorkbenchPartReference partRef) {
282: IWorkbenchPart part = partRef.getPart(false);
283: if (part == MultiEditor.this && innerEditors != null) {
284: PartService partService = ((WorkbenchPage) getSite()
285: .getPage()).getPartService();
286: for (int i = 0; i < innerEditors.length; i++) {
287: IEditorPart editor = innerEditors[i];
288: IWorkbenchPartReference innerRef = ((PartSite) editor
289: .getSite()).getPartReference();
290: partService.firePartClosed(innerRef);
291: }
292: }
293: }
294:
295: public void partDeactivated(IWorkbenchPartReference partRef) {
296: }
297:
298: public void partOpened(IWorkbenchPartReference partRef) {
299: IWorkbenchPart part = partRef.getPart(false);
300: if (part == MultiEditor.this && innerEditors != null) {
301: PartService partService = ((WorkbenchPage) getSite()
302: .getPage()).getPartService();
303: for (int i = 0; i < innerEditors.length; i++) {
304: IEditorPart editor = innerEditors[i];
305: IWorkbenchPartReference innerRef = ((PartSite) editor
306: .getSite()).getPartReference();
307: partService.firePartOpened(innerRef);
308: }
309: }
310: }
311:
312: public void partHidden(IWorkbenchPartReference partRef) {
313: }
314:
315: public void partVisible(IWorkbenchPartReference partRef) {
316: }
317:
318: public void partInputChanged(IWorkbenchPartReference partRef) {
319: }
320: };
321: getSite().getPage().addPartListener(propagationListener);
322: }
323:
324: /**
325: * Release the added listener.
326: *
327: * @since 3.2
328: */
329: public void dispose() {
330: getSite().getPage().removePartListener(propagationListener);
331: super.dispose();
332: }
333: }
|