1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.ui.IEditorPart; 14 import org.eclipse.ui.IWorkbenchPage; 15 import org.eclipse.ui.IWorkbenchPart; 16 import org.eclipse.ui.IWorkbenchWindow; 17 import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; 18 19 61 public abstract class ActiveEditorAction extends PageEventAction { 62 63 private IEditorPart activeEditor; 64 65 73 protected ActiveEditorAction(String text, IWorkbenchWindow window) { 74 super(text, window); 75 updateState(); 76 } 77 78 84 protected void editorActivated(IEditorPart part) { 85 } 86 87 93 protected void editorDeactivated(IEditorPart part) { 94 } 95 96 102 public final IEditorPart getActiveEditor() { 103 return activeEditor; 104 } 105 106 109 public void pageActivated(IWorkbenchPage page) { 110 super.pageActivated(page); 111 updateActiveEditor(); 112 updateState(); 113 } 114 115 118 public void pageClosed(IWorkbenchPage page) { 119 super.pageClosed(page); 120 updateActiveEditor(); 121 updateState(); 122 } 123 124 127 public void partActivated(IWorkbenchPart part) { 128 super.partActivated(part); 129 if (part instanceof IEditorPart) { 130 updateActiveEditor(); 131 updateState(); 132 } 133 } 134 135 138 public void partBroughtToTop(IWorkbenchPart part) { 139 super.partBroughtToTop(part); 140 if (part instanceof IEditorPart) { 141 updateActiveEditor(); 142 updateState(); 143 } 144 } 145 146 149 public void partClosed(IWorkbenchPart part) { 150 super.partClosed(part); 151 if (part instanceof IEditorPart) { 152 updateActiveEditor(); 153 updateState(); 154 } 155 } 156 157 160 public void partDeactivated(IWorkbenchPart part) { 161 super.partDeactivated(part); 162 if (part instanceof IEditorPart) { 163 updateActiveEditor(); 164 updateState(); 165 } 166 } 167 168 171 private void setActiveEditor(IEditorPart part) { 172 if (activeEditor == part) { 173 return; 174 } 175 if (activeEditor != null) { 176 editorDeactivated(activeEditor); 177 } 178 activeEditor = part; 179 if (activeEditor != null) { 180 editorActivated(activeEditor); 181 } 182 } 183 184 188 private void updateActiveEditor() { 189 if (getActivePage() == null) { 190 setActiveEditor(null); 191 } else { 192 setActiveEditor(getActivePage().getActiveEditor()); 193 } 194 } 195 196 202 protected void updateState() { 203 setEnabled(getActiveEditor() != null); 204 } 205 206 } 207 | Popular Tags |