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.IPropertyListener; 18 import org.eclipse.ui.ISaveablePart; 19 import org.eclipse.ui.ISaveablesLifecycleListener; 20 import org.eclipse.ui.ISaveablesSource; 21 import org.eclipse.ui.IWorkbenchPage; 22 import org.eclipse.ui.IWorkbenchPart; 23 import org.eclipse.ui.IWorkbenchWindow; 24 import org.eclipse.ui.Saveable; 25 26 38 public class SaveAllAction extends PageEventAction implements IPropertyListener { 39 43 private List partsWithListeners = new ArrayList (1); 44 private IWorkbenchPart openPart; 45 46 51 public SaveAllAction(IWorkbenchWindow window) { 52 super(WorkbenchMessages.SaveAll_text, window); 53 setToolTipText(WorkbenchMessages.SaveAll_toolTip); 54 setId("saveAll"); setEnabled(false); 56 window.getWorkbench().getHelpSystem().setHelp(this, 57 IWorkbenchHelpContextIds.SAVE_ALL_ACTION); 58 setImageDescriptor(WorkbenchImages 59 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVEALL_EDIT)); 60 setDisabledImageDescriptor(WorkbenchImages 61 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVEALL_EDIT_DISABLED)); 62 setActionDefinitionId("org.eclipse.ui.file.saveAll"); } 64 65 68 public void pageActivated(IWorkbenchPage page) { 69 super.pageActivated(page); 70 updateState(); 71 } 72 73 76 public void pageClosed(IWorkbenchPage page) { 77 super.pageClosed(page); 78 updateState(); 79 } 80 81 84 public void partClosed(IWorkbenchPart part) { 85 super.partClosed(part); 86 if (part instanceof ISaveablePart) { 87 part.removePropertyListener(this); 88 partsWithListeners.remove(part); 89 updateState(); 90 } 91 } 92 93 96 public void partOpened(IWorkbenchPart part) { 97 super.partOpened(part); 98 if (part instanceof ISaveablePart) { 99 part.addPropertyListener(this); 100 partsWithListeners.add(part); 101 openPart = part; 109 updateState(); 110 openPart = null; 111 } 112 } 113 114 117 public void propertyChanged(Object source, int propID) { 118 if (source instanceof ISaveablePart) { 119 if (propID == ISaveablePart.PROP_DIRTY) { 120 updateState(); 121 } 122 } 123 } 124 125 128 public void run() { 129 if (getWorkbenchWindow() == null) { 130 return; 132 } 133 WorkbenchPage page = (WorkbenchPage) getActivePage(); 134 if (page != null) { 135 page.saveAllEditors(false, true); 138 updateState(); 139 } 140 } 141 142 146 protected void updateState() { 147 if (openPart != null && openPart.getSite().getPage().equals(getActivePage()) && ((ISaveablePart) openPart).isDirty()) { 149 setEnabled(true); 150 } 151 else { 152 WorkbenchPage page = (WorkbenchPage) getActivePage(); 153 if (page == null) { 154 setEnabled(false); 155 } else { 156 if (page.getDirtyParts().length > 0) { 157 setEnabled(true); 158 } else { 159 SaveablesList saveablesList = (SaveablesList) page 162 .getWorkbenchWindow().getWorkbench().getService( 163 ISaveablesLifecycleListener.class); 164 ISaveablesSource[] nonPartSources = saveablesList.getNonPartSources(); 165 for (int i = 0; i < nonPartSources.length; i++) { 166 Saveable[] saveables = nonPartSources[i].getSaveables(); 167 for (int j = 0; j < saveables.length; j++) { 168 if (saveables[j].isDirty()) { 169 setEnabled(true); 170 return; 171 } 172 } 173 } 174 setEnabled(false); 175 } 176 } 177 } 178 } 179 180 183 public void dispose() { 184 super.dispose(); 185 for (Iterator it = partsWithListeners.iterator(); it.hasNext();) { 186 IWorkbenchPart part = (IWorkbenchPart) it.next(); 187 part.removePropertyListener(this); 188 } 189 partsWithListeners.clear(); 190 } 191 } 192 | Popular Tags |