1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.ui.IEditorPart; 19 import org.eclipse.ui.IPropertyListener; 20 import org.eclipse.ui.ISaveablePart; 21 import org.eclipse.ui.IViewPart; 22 import org.eclipse.ui.IWorkbenchPage; 23 import org.eclipse.ui.IWorkbenchPart; 24 import org.eclipse.ui.IWorkbenchWindow; 25 import org.eclipse.ui.internal.util.Util; 26 27 30 public abstract class BaseSaveAction extends ActiveEditorAction { 31 35 36 40 private List partsWithListeners = new ArrayList (1); 41 42 private final IPropertyListener propListener = new IPropertyListener() { 43 public void propertyChanged(Object source, int propId) { 44 if (source == getActiveEditor()) { 45 if (propId == IEditorPart.PROP_DIRTY) { 46 updateState(); 47 } 48 } 49 } 50 }; 51 52 60 protected BaseSaveAction(String text, IWorkbenchWindow window) { 61 super(text, window); 62 } 63 64 67 protected void editorActivated(IEditorPart part) { 68 if (part != null) { 69 part.addPropertyListener(propListener); 70 partsWithListeners.add(part); 71 } 72 } 73 74 77 protected void editorDeactivated(IEditorPart part) { 78 if (part != null) { 79 part.removePropertyListener(propListener); 80 partsWithListeners.remove(part); 81 } 82 } 83 84 private IViewPart activeView; 85 86 private final IPropertyListener propListener2 = new IPropertyListener() { 87 public void propertyChanged(Object source, int propId) { 88 if (source == activeView) { 89 if (propId == IEditorPart.PROP_DIRTY) { 90 updateState(); 91 } 92 } 93 } 94 }; 95 96 97 private ISaveablePart activeSaveablePart; 98 99 private final IPropertyListener propListener3 = new IPropertyListener() { 100 public void propertyChanged(Object source, int propId) { 101 if (source == activeSaveablePart) { 102 if (propId == IEditorPart.PROP_DIRTY) { 103 updateState(); 104 } 105 } 106 } 107 }; 108 109 112 public void pageActivated(IWorkbenchPage page) { 113 super.pageActivated(page); 114 updateActiveView(); 115 updateState(); 116 } 117 118 121 public void pageClosed(IWorkbenchPage page) { 122 super.pageClosed(page); 123 updateActiveView(); 124 updateState(); 125 } 126 127 130 public void partActivated(IWorkbenchPart part) { 131 super.partActivated(part); 132 if (part instanceof IViewPart) { 133 updateActiveView(); 134 updateState(); 135 } 136 } 137 138 141 public void partClosed(IWorkbenchPart part) { 142 super.partClosed(part); 143 if (part instanceof IViewPart) { 144 updateActiveView(); 145 updateState(); 146 } 147 } 148 149 152 public void partDeactivated(IWorkbenchPart part) { 153 super.partDeactivated(part); 154 if (part instanceof IViewPart) { 155 updateActiveView(); 156 updateState(); 157 } 158 } 159 160 164 private void updateActiveView() { 165 if (getActivePage() == null) { 166 setActiveView(null); 167 } else { 168 setActiveView(getActivePage().getActivePart()); 169 } 170 } 171 172 175 private void updateActiveSaveablePart() { 176 if (activeSaveablePart instanceof IWorkbenchPart) { 177 ((IWorkbenchPart)activeSaveablePart).removePropertyListener(propListener3); 178 partsWithListeners.remove(activeSaveablePart); 179 } 180 activeSaveablePart = getSaveableView(); 181 if (activeSaveablePart == activeView) { 182 activeSaveablePart = null; 184 } 185 if (activeSaveablePart instanceof IWorkbenchPart) { 186 ((IWorkbenchPart)activeSaveablePart).addPropertyListener(propListener3); 187 partsWithListeners.add(activeSaveablePart); 188 } 189 } 190 191 194 private void setActiveView(IWorkbenchPart part) { 195 if (activeView == part) { 196 return; 197 } 198 if (activeView != null) { 199 activeView.removePropertyListener(propListener2); 200 partsWithListeners.remove(activeView); 201 } 202 if (part instanceof IViewPart) { 203 activeView = (IViewPart) part; 204 } else { 205 activeView = null; 206 } 207 if (activeView != null) { 208 activeView.addPropertyListener(propListener2); 209 partsWithListeners.add(activeView); 210 } 211 updateActiveSaveablePart(); 212 } 213 214 protected final ISaveablePart getSaveableView() { 215 if (activeView == null) { 216 return null; 217 } 218 219 return (ISaveablePart) Util.getAdapter(activeView, ISaveablePart.class); 220 } 221 222 225 public void dispose() { 226 super.dispose(); 227 for (Iterator it = partsWithListeners.iterator(); it.hasNext();) { 228 IWorkbenchPart part = (IWorkbenchPart) it.next(); 229 part.removePropertyListener(propListener); 230 part.removePropertyListener(propListener2); 231 part.removePropertyListener(propListener3); 232 } 233 partsWithListeners.clear(); 234 } 235 } 236 | Popular Tags |