1 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 31 private IPropertyListener listener = new IPropertyListener() { 32 public void propertyChanged(Object 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 firePartAdded(ref); 83 84 if (ref.getPart(false) != null) { 87 partOpened(ref); 88 } 89 90 if (ref.getVisible()) { 93 partVisible(ref); 94 } 95 } 96 97 102 public void setActivePart(IWorkbenchPartReference ref) { 103 if (ref == activePartReference) { 104 return; 105 } 106 107 IWorkbenchPartReference oldPart = activePartReference; 108 109 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 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 158 public void removePart(WorkbenchPartReference ref) { 159 Assert.isNotNull(ref); 160 Assert.isTrue(ref != activePartReference); 165 Assert.isTrue(ref != activeEditorReference); 168 169 if (ref.getVisible()) { 170 ref.setVisible(false); 171 } 172 173 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 Assert.isNotNull(ref); 191 Assert.isTrue(!ref.getVisible()); 193 197 firePartHidden(ref); 198 } 199 200 private void partOpened(WorkbenchPartReference ref) { 201 Assert.isNotNull(ref); 202 203 IWorkbenchPart actualPart = ref.getPart(false); 204 Assert.isNotNull(actualPart); 207 Assert.isTrue(activePartReference != ref); 215 Assert.isTrue(activeEditorReference != ref); 221 222 SaveablesList modelManager = (SaveablesList) actualPart 223 .getSite().getService(ISaveablesLifecycleListener.class); 224 modelManager.postOpen(actualPart); 225 226 firePartOpened(ref); 228 } 229 230 237 private void partClosed(WorkbenchPartReference ref) { 238 Assert.isNotNull(ref); 239 240 IWorkbenchPart actualPart = ref.getPart(false); 241 Assert.isNotNull(actualPart); 244 Assert.isTrue(activePartReference != ref); 250 Assert.isTrue(activeEditorReference != ref); 254 255 firePartClosed(ref); 256 } 257 258 private void partVisible(WorkbenchPartReference ref) { 259 Assert.isNotNull(ref); 261 Assert.isTrue(ref.getVisible()); 263 Assert.isNotNull(ref.getPart(false)); 268 269 firePartVisible(ref); 270 } 271 272 279 protected abstract void firePartOpened(IWorkbenchPartReference part); 280 281 288 protected abstract void firePartClosed(IWorkbenchPartReference part); 289 290 295 protected abstract void firePartAdded(IWorkbenchPartReference part); 296 297 302 protected abstract void firePartRemoved(IWorkbenchPartReference part); 303 304 310 protected abstract void fireActiveEditorChanged(IWorkbenchPartReference ref); 311 312 318 protected abstract void fireActivePartChanged( 319 IWorkbenchPartReference oldPart, IWorkbenchPartReference newPart); 320 321 326 protected abstract void firePartVisible(IWorkbenchPartReference ref); 327 328 333 protected abstract void firePartHidden(IWorkbenchPartReference ref); 334 335 340 protected abstract void firePartInputChanged(IWorkbenchPartReference ref); 341 342 protected abstract void firePartBroughtToTop(IWorkbenchPartReference ref); 343 } 344 | Popular Tags |