001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.copier.actions;
024:
025: import net.sf.jmoney.JMoneyPlugin;
026: import net.sf.jmoney.copier.CopierPlugin;
027: import net.sf.jmoney.model2.DatastoreManager;
028:
029: import org.eclipse.jface.action.IAction;
030: import org.eclipse.jface.dialogs.IDialogConstants;
031: import org.eclipse.jface.dialogs.MessageDialog;
032: import org.eclipse.jface.viewers.ISelection;
033: import org.eclipse.ui.IWorkbenchWindow;
034: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
035:
036: /**
037: * Our sample action implements workbench action delegate.
038: * The action proxy will be created by the workbench and
039: * shown in the UI. When the user tries to use the action,
040: * this delegate will be created and execution will be
041: * delegated to it.
042: * @see IWorkbenchWindowActionDelegate
043: */
044: public class CutSessionAction implements IWorkbenchWindowActionDelegate {
045: private IWorkbenchWindow window;
046:
047: /**
048: * The constructor.
049: */
050: public CutSessionAction() {
051: }
052:
053: /**
054: * The action has been activated. The argument of the
055: * method represents the 'real' action sitting
056: * in the workbench UI.
057: * @see IWorkbenchWindowActionDelegate#run
058: */
059: public void run(IAction action) {
060: DatastoreManager sessionManager = JMoneyPlugin.getDefault()
061: .getSessionManager();
062:
063: if (sessionManager == null) {
064: MessageDialog waitDialog = new MessageDialog(
065: window.getShell(),
066: "Menu item unavailable",
067: null, // accept the default window icon
068: "No session is open. "
069: + "This action is used to copy session data from one session to another. "
070: + "You must first use this action to save the contents of the current session. "
071: + "You must then open another session and then select the 'Paste Contents' action. "
072: + "The contents of the session will then be copied into the new session.",
073: MessageDialog.ERROR,
074: new String[] { IDialogConstants.OK_LABEL }, 0);
075: waitDialog.open();
076: return;
077: }
078:
079: // We call canClose now so that we are sure
080: // that we can later close the session without
081: // further user input. If the user does not
082: // provide at this time all the information required
083: // to close the session then we terminate the operation
084: // now.
085: if (!sessionManager.canClose(window)) {
086: return;
087: }
088:
089: // Save the session in a static location.
090: // The session is left open so that we can
091: // later read the data from it.
092: CopierPlugin.setSessionManager(sessionManager);
093:
094: // Set the current session to null.
095: // This must be done because otherwise the session
096: // would be closed when another session is loaded.
097: JMoneyPlugin.getDefault().setSessionManager(null);
098: }
099:
100: /**
101: * Selection in the workbench has been changed. We
102: * can change the state of the 'real' action here
103: * if we want, but this can only happen after
104: * the delegate has been created.
105: * @see IWorkbenchWindowActionDelegate#selectionChanged
106: */
107: public void selectionChanged(IAction action, ISelection selection) {
108: }
109:
110: /**
111: * We can use this method to dispose of any system
112: * resources we previously allocated.
113: * @see IWorkbenchWindowActionDelegate#dispose
114: */
115: public void dispose() {
116: }
117:
118: /**
119: * We will cache window object in order to
120: * be able to provide parent shell for the message dialog.
121: * @see IWorkbenchWindowActionDelegate#init
122: */
123: public void init(IWorkbenchWindow window) {
124: this.window = window;
125: }
126: }
|