1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.ui.IEditorPart; 18 import org.eclipse.ui.IEditorReference; 19 import org.eclipse.ui.IPropertyListener; 20 import org.eclipse.ui.IWorkbenchPage; 21 import org.eclipse.ui.IWorkbenchPart; 22 import org.eclipse.ui.IWorkbenchWindow; 23 24 27 public class CloseAllSavedAction extends PageEventAction implements 28 IPropertyListener { 29 30 34 private List partsWithListeners = new ArrayList (1); 35 36 40 public CloseAllSavedAction(IWorkbenchWindow window) { 41 super(WorkbenchMessages.CloseAllSavedAction_text, window); 42 setToolTipText(WorkbenchMessages.CloseAllSavedAction_toolTip); 43 setId("closeAllSaved"); updateState(); 46 window.getWorkbench().getHelpSystem().setHelp(this, 47 IWorkbenchHelpContextIds.CLOSE_ALL_SAVED_ACTION); 48 setActionDefinitionId("org.eclipse.ui.file.closeAllSaved"); } 50 51 54 public void pageActivated(IWorkbenchPage page) { 55 super.pageActivated(page); 56 updateState(); 57 } 58 59 62 public void pageClosed(IWorkbenchPage page) { 63 super.pageClosed(page); 64 updateState(); 65 } 66 67 70 public void partClosed(IWorkbenchPart part) { 71 super.partClosed(part); 72 if (part instanceof IEditorPart) { 73 part.removePropertyListener(this); 74 partsWithListeners.remove(part); 75 updateState(); 76 } 77 } 78 79 82 public void partOpened(IWorkbenchPart part) { 83 super.partOpened(part); 84 if (part instanceof IEditorPart) { 85 part.addPropertyListener(this); 86 partsWithListeners.add(part); 87 updateState(); 88 } 89 } 90 91 94 public void propertyChanged(Object source, int propID) { 95 if (source instanceof IEditorPart) { 96 if (propID == IEditorPart.PROP_DIRTY) { 97 updateState(); 98 } 99 } 100 } 101 102 105 public void run() { 106 if (getWorkbenchWindow() == null) { 107 return; 109 } 110 IWorkbenchPage page = getActivePage(); 111 if (page != null) { 112 ((WorkbenchPage) page).closeAllSavedEditors(); 113 } 114 } 115 116 119 private void updateState() { 120 IWorkbenchPage page = getActivePage(); 121 if (page == null) { 122 setEnabled(false); 123 return; 124 } 125 IEditorReference editors[] = page.getEditorReferences(); 126 for (int i = 0; i < editors.length; i++) { 127 if (!editors[i].isDirty()) { 128 setEnabled(true); 129 return; 130 } 131 } 132 setEnabled(false); 133 } 134 135 138 public void dispose() { 139 super.dispose(); 140 for (Iterator it = partsWithListeners.iterator(); it.hasNext();) { 141 IWorkbenchPart part = (IWorkbenchPart) it.next(); 142 part.removePropertyListener(this); 143 } 144 partsWithListeners.clear(); 145 } 146 147 } 148 | Popular Tags |