KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > SaveAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.ui.IEditorPart;
14 import org.eclipse.ui.ISaveablePart;
15 import org.eclipse.ui.ISaveablesSource;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.IWorkbenchWindow;
18
19 /**
20  * Workbench common <code>Save</code> action.
21  */

22 public class SaveAction extends BaseSaveAction implements IBackgroundSaveListener {
23
24     /**
25      * Create an instance of this class
26      *
27      * @param window the window
28      */

29     public SaveAction(IWorkbenchWindow window) {
30         super(WorkbenchMessages.SaveAction_text, window);
31         setText(WorkbenchMessages.SaveAction_text);
32         setToolTipText(WorkbenchMessages.SaveAction_toolTip);
33         setId("save"); //$NON-NLS-1$
34
window.getWorkbench().getHelpSystem().setHelp(this,
35                 IWorkbenchHelpContextIds.SAVE_ACTION);
36         setImageDescriptor(WorkbenchImages
37                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVE_EDIT));
38         setDisabledImageDescriptor(WorkbenchImages
39                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVE_EDIT_DISABLED));
40         setActionDefinitionId("org.eclipse.ui.file.save"); //$NON-NLS-1$
41
((WorkbenchWindow)window).addBackgroundSaveListener(this);
42     }
43     
44     public void dispose() {
45         ((WorkbenchWindow)getWorkbenchWindow()).removeBackgroundSaveListener(this);
46         super.dispose();
47     }
48
49     /* (non-Javadoc)
50      * Method declared on IAction.
51      * Performs the <code>Save</code> action by calling the
52      * <code>IEditorPart.doSave</code> method on the active editor.
53      */

54     public void run() {
55         if (getWorkbenchWindow() == null) {
56             // action has been disposed
57
return;
58         }
59         /* **********************************************************************************
60          * The code below was added to track the view with focus
61          * in order to support save actions from a view (see bug 10234).
62          */

63         ISaveablePart saveView = getSaveableView();
64         if (saveView != null) {
65             ((WorkbenchPage) getActivePart().getSite().getPage()).savePart(
66                     saveView, getActivePart(), false);
67             return;
68         }
69
70         IEditorPart part = getActiveEditor();
71         if (part != null) {
72             IWorkbenchPage page = part.getSite().getPage();
73             page.saveEditor(part, false);
74         }
75     }
76
77     /* (non-Javadoc)
78      * Method declared on ActiveEditorAction.
79      */

80     protected void updateState() {
81         /* **********************************************************************************
82          * The code below was added to track the view with focus
83          * in order to support save actions from a view (see bug 10234).
84          */

85         ISaveablePart saveable = getSaveableView();
86         if (saveable == null) {
87             saveable = getActiveEditor();
88         }
89         /* **********************************************************************************/
90         if (saveable instanceof ISaveablesSource) {
91             ISaveablesSource modelSource = (ISaveablesSource) saveable;
92             setEnabled(SaveableHelper.needsSave(modelSource));
93             return;
94         }
95         setEnabled(saveable != null && saveable.isDirty());
96     }
97
98     public void handleBackgroundSaveStarted() {
99         updateState();
100     }
101 }
102
Popular Tags