1 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 43 public abstract class MultiEditor extends EditorPart { 44 45 private int activeEditorIndex; 46 47 private IEditorPart innerEditors[]; 48 49 private IPartListener2 propagationListener; 50 51 54 public MultiEditor() { 55 super(); 56 } 57 58 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 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 91 public void doSaveAs() { 92 } 94 95 98 public void init(IEditorSite site, IEditorInput input) 99 throws PartInitException { 100 init(site, (MultiEditorInput) input); 101 } 102 103 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 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 131 public boolean isSaveAsAllowed() { 132 return false; 133 } 134 135 138 public void setFocus() { 139 innerEditors[activeEditorIndex].setFocus(); 140 updateGradient(innerEditors[activeEditorIndex]); 141 } 142 143 146 public final IEditorPart getActiveEditor() { 147 return innerEditors[activeEditorIndex]; 148 } 149 150 153 public final IEditorPart[] getInnerEditors() { 154 return innerEditors; 155 } 156 157 162 public final void setChildren(IEditorPart[] children) { 163 innerEditors = children; 164 activeEditorIndex = 0; 165 } 166 167 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 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 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 245 protected abstract void drawGradient(IEditorPart innerEditor, Gradient g); 246 247 250 protected boolean getShellActivated() { 251 WorkbenchWindow window = (WorkbenchWindow) getSite().getPage() 252 .getWorkbenchWindow(); 253 return window.getShellActivated(); 254 } 255 256 259 public static class Gradient { 260 public Color fgColor; 261 262 public Color[] bgColors; 263 264 public int[] bgPercents; 265 } 266 267 268 269 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 330 public void dispose() { 331 getSite().getPage().removePartListener(propagationListener); 332 super.dispose(); 333 } 334 } 335 | Popular Tags |