Source code: org/eclipse/ui/internal/EditorPane.java
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-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.IProgressMonitor;
14 import org.eclipse.jface.resource.JFaceColors;
15 import org.eclipse.jface.util.Assert;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.swt.widgets.MenuItem;
23 import org.eclipse.swt.widgets.Text;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IEditorReference;
27 import org.eclipse.ui.IEditorSite;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.part.EditorPart;
30 import org.eclipse.ui.presentations.StackPresentation;
31
32 /**
33 * An EditorPane is a subclass of PartPane offering extended
34 * behavior for workbench editors.
35 */
36 public class EditorPane extends PartPane {
37 private EditorStack workbook;
38
39 /**
40 * Constructs an editor pane for an editor part.
41 */
42 public EditorPane(IEditorReference ref, WorkbenchPage page, EditorStack workbook) {
43 super(ref, page);
44 this.workbook = workbook;
45 }
46 protected IWorkbenchPart createErrorPart(IWorkbenchPart oldPart) {
47 class ErrorEditorPart extends EditorPart {
48 private Text text;
49 public void doSave(IProgressMonitor monitor) {}
50 public void doSaveAs() {}
51 public void init(IEditorSite site, IEditorInput input) {
52 setSite(site);
53 setInput(input);
54 }
55 public boolean isDirty() {return false;}
56 public boolean isSaveAsAllowed() {return false;}
57 public void createPartControl(Composite parent) {
58 text = new Text(parent,SWT.MULTI|SWT.READ_ONLY|SWT.WRAP);
59 text.setForeground(JFaceColors.getErrorText(text.getDisplay()));
60 text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
61 text.setText(WorkbenchMessages.getString("EditorPane.errorMessage")); //$NON-NLS-1$
62 }
63 public void setFocus() {
64 if (text != null) text.setFocus();
65 }
66 protected void setPartName(String title) {
67 super.setPartName(title);
68 }
69 protected void setTitleToolTip(String text) {
70 super.setTitleToolTip(text);
71 }
72 }
73 IEditorPart oldEditorPart = (IEditorPart)oldPart;
74 EditorSite oldEditorSite = (EditorSite)oldEditorPart.getEditorSite();
75 ErrorEditorPart newPart = new ErrorEditorPart();
76 newPart.setPartName(oldPart.getTitle());
77 newPart.setTitleToolTip(oldPart.getTitleToolTip());
78 oldEditorSite.setPart(newPart);
79 newPart.init(oldEditorSite, oldEditorPart.getEditorInput());
80 return newPart;
81 }
82 /**
83 * Editor panes do not need a title bar. The editor
84 * title and close icon are part of the tab containing
85 * the editor. Tools and menus are added directly into
86 * the workbench toolbar and menu bar.
87 */
88 protected void createTitleBar() {
89 // do nothing
90 }
91 /**
92 * @see PartPane::doHide
93 */
94 public void doHide() {
95 getPage().closeEditor(getEditorReference(), true);
96 }
97
98 /**
99 * Answer the editor part child.
100 */
101 public IEditorReference getEditorReference() {
102 return (IEditorReference)getPartReference();
103 }
104 /**
105 * Answer the SWT widget style.
106 */
107 int getStyle() {
108 return SWT.NONE;
109 }
110 /**
111 * Answer the editor workbook container
112 */
113 public EditorStack getWorkbook() {
114 return workbook;
115 }
116
117 /**
118 * Notify the workbook page that the part pane has
119 * been activated by the user.
120 */
121 protected void requestActivation() {
122 // By clearing the active workbook if its not the one
123 // associated with the editor, we reduce draw flicker
124 if (!workbook.isActiveWorkbook())
125 workbook.getEditorArea().setActiveWorkbook(null, false);
126
127 super.requestActivation();
128 }
129 /**
130 * Set the editor workbook container
131 */
132 public void setWorkbook(EditorStack editorWorkbook) {
133 workbook = editorWorkbook;
134 }
135 /* (non-Javadoc)
136 * Method declared on PartPane.
137 */
138 /* package */ void shellActivated() {
139 //this.workbook.drawGradient();
140 }
141
142 /* (non-Javadoc)
143 * Method declared on PartPane.
144 */
145 /* package */ void shellDeactivated() {
146 //this.workbook.drawGradient();
147 }
148
149
150 /* (non-Javadoc)
151 * @see org.eclipse.ui.internal.LayoutPart#setFocus()
152 */
153 public void setFocus() {
154 super.setFocus();
155
156 workbook.becomeActiveWorkbook(true);
157 }
158 /**
159 * Indicate focus in part.
160 */
161 public void showFocus(boolean inFocus) {
162 if (inFocus)
163 this.workbook.becomeActiveWorkbook(true);
164 else
165 this.workbook.setActive(this.workbook.isActiveWorkbook() ? StackPresentation.AS_ACTIVE_NOFOCUS : StackPresentation.AS_INACTIVE);
166 }
167
168 /**
169 * Add the pin menu item on the editor system menu
170 */
171 protected void addPinEditorItem(Menu parent) {
172 boolean reuseEditor = WorkbenchPlugin.getDefault().getPreferenceStore().getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN);
173 if (!reuseEditor) {
174 return;
175 }
176
177 IWorkbenchPart part = getPartReference().getPart(false);
178 if (part == null) {
179 return;
180 }
181
182 final MenuItem item = new MenuItem(parent, SWT.CHECK);
183 item.setText(WorkbenchMessages.getString("EditorPane.pinEditor")); //$NON-NLS-1$
184 item.addSelectionListener(new SelectionAdapter() {
185 public void widgetSelected(SelectionEvent e) {
186 IWorkbenchPart part = getPartReference().getPart(true);
187 if (part == null) {
188 // this should never happen
189 item.setSelection(false);
190 item.setEnabled(false);
191 } else {
192 ((EditorSite) part.getSite()).setReuseEditor(!item.getSelection());
193 }
194 }
195 });
196 item.setEnabled(true);
197 item.setSelection(!((EditorSite) part.getSite()).getReuseEditor());
198 }
199
200 /**
201 * Update the title attributes for the pane.
202 */
203 public void updateTitles() {
204 // TODO commented during presentation refactor workbook.updateEditorTab(getEditorReference());
205 }
206
207
208 /* (non-Javadoc)
209 * @see org.eclipse.ui.internal.LayoutPart#testInvariants()
210 */
211 public void testInvariants() {
212 super.testInvariants();
213
214 if (getContainer() != null) {
215 Assert.isTrue(getContainer() == workbook);
216 }
217 }
218
219 /* (non-Javadoc)
220 * @see org.eclipse.ui.internal.PartPane#getName()
221 */
222 public String getName() {
223 return null;
224 }
225
226 /* (non-Javadoc)
227 * @see org.eclipse.ui.internal.PartPane#getToolBar()
228 */
229 public Control getToolBar() {
230 return null;
231 }
232
233 }