001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.ui.IEditorPart;
013: import org.eclipse.ui.ISaveablePart;
014: import org.eclipse.ui.ISaveablesSource;
015: import org.eclipse.ui.IWorkbenchPage;
016: import org.eclipse.ui.IWorkbenchWindow;
017:
018: /**
019: * Workbench common <code>Save</code> action.
020: */
021: public class SaveAction extends BaseSaveAction implements
022: IBackgroundSaveListener {
023:
024: /**
025: * Create an instance of this class
026: *
027: * @param window the window
028: */
029: public SaveAction(IWorkbenchWindow window) {
030: super (WorkbenchMessages.SaveAction_text, window);
031: setText(WorkbenchMessages.SaveAction_text);
032: setToolTipText(WorkbenchMessages.SaveAction_toolTip);
033: setId("save"); //$NON-NLS-1$
034: window.getWorkbench().getHelpSystem().setHelp(this ,
035: IWorkbenchHelpContextIds.SAVE_ACTION);
036: setImageDescriptor(WorkbenchImages
037: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVE_EDIT));
038: setDisabledImageDescriptor(WorkbenchImages
039: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVE_EDIT_DISABLED));
040: setActionDefinitionId("org.eclipse.ui.file.save"); //$NON-NLS-1$
041: ((WorkbenchWindow) window).addBackgroundSaveListener(this );
042: }
043:
044: public void dispose() {
045: ((WorkbenchWindow) getWorkbenchWindow())
046: .removeBackgroundSaveListener(this );
047: super .dispose();
048: }
049:
050: /* (non-Javadoc)
051: * Method declared on IAction.
052: * Performs the <code>Save</code> action by calling the
053: * <code>IEditorPart.doSave</code> method on the active editor.
054: */
055: public void run() {
056: if (getWorkbenchWindow() == null) {
057: // action has been disposed
058: return;
059: }
060: /* **********************************************************************************
061: * The code below was added to track the view with focus
062: * in order to support save actions from a view (see bug 10234).
063: */
064: ISaveablePart saveView = getSaveableView();
065: if (saveView != null) {
066: ((WorkbenchPage) getActivePart().getSite().getPage())
067: .savePart(saveView, getActivePart(), false);
068: return;
069: }
070:
071: IEditorPart part = getActiveEditor();
072: if (part != null) {
073: IWorkbenchPage page = part.getSite().getPage();
074: page.saveEditor(part, false);
075: }
076: }
077:
078: /* (non-Javadoc)
079: * Method declared on ActiveEditorAction.
080: */
081: protected void updateState() {
082: /* **********************************************************************************
083: * The code below was added to track the view with focus
084: * in order to support save actions from a view (see bug 10234).
085: */
086: ISaveablePart saveable = getSaveableView();
087: if (saveable == null) {
088: saveable = getActiveEditor();
089: }
090: /* **********************************************************************************/
091: if (saveable instanceof ISaveablesSource) {
092: ISaveablesSource modelSource = (ISaveablesSource) saveable;
093: setEnabled(SaveableHelper.needsSave(modelSource));
094: return;
095: }
096: setEnabled(saveable != null && saveable.isDirty());
097: }
098:
099: public void handleBackgroundSaveStarted() {
100: updateState();
101: }
102: }
|