OpenSessionAction.java :  » ERP-CRM-Financial » jmoney » net » sf » jmoney » serializeddatastore » actions » Java Open Source

Java Open Source » ERP CRM Financial » jmoney 
jmoney » net » sf » jmoney » serializeddatastore » actions » OpenSessionAction.java
/*
*
*  JMoney - A Personal Finance Manager
*  Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
*
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/

package net.sf.jmoney.serializeddatastore.actions;

import java.io.File;

import net.sf.jmoney.JMoneyPlugin;
import net.sf.jmoney.serializeddatastore.IFileDatastore;
import net.sf.jmoney.serializeddatastore.SerializedDatastorePlugin;
import net.sf.jmoney.serializeddatastore.SessionManager;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class OpenSessionAction implements IWorkbenchWindowActionDelegate {
  private IWorkbenchWindow window;
  /**
   * The constructor.
   */
  public OpenSessionAction() {
  }

  /**
   * The action has been activated. The argument of the
   * method represents the 'real' action sitting
   * in the workbench UI.
   * @see IWorkbenchWindowActionDelegate#run
   */
  public void run(IAction action) {
        if (JMoneyPlugin.getDefault().saveOldSession(window)) {
            
            FileDialog dialog = new FileDialog(window.getShell());
            dialog.setFilterExtensions(SerializedDatastorePlugin.getFilterExtensions());
            dialog.setFilterNames(SerializedDatastorePlugin.getFilterNames());
            String fileName = dialog.open();
            
            if (fileName != null) {
                File sessionFile = new File(fileName);
                
                IConfigurationElement elements[] = SerializedDatastorePlugin.getElements(fileName);
                
                if (elements.length == 0) {
                  /*
                   * The user has entered an extension that is not recognized.
                   */
                  MessageDialog.openError(window.getShell(), "Invalid Filename", "You have entered a file name with an unrecognized extension.  The supported extensions can be found by using the 'Files of type' drop-down in the 'Open' dialog.");
                  return;
                }
                
                // TODO: It is possible that multiple plug-ins may
                // use the same file extension.  There are two possible
                // approaches to this: either ask the user which is
                // the format of the file, or we try to load the file
                // using each in turn until one works. 

                // For time being, we simply use the first entry.
                IFileDatastore fileDatastore;
                String fileFormatId;
        try {
          fileDatastore = (IFileDatastore)elements[0].createExecutableExtension("class");
          fileFormatId = elements[0].getDeclaringExtension().getNamespaceIdentifier() + '.' + elements[0].getAttribute("id");
        } catch (CoreException e) {
          e.printStackTrace();
          throw new RuntimeException("internal error");
        }

        SessionManager sessionManager = new SessionManager(fileFormatId, fileDatastore, sessionFile);
        boolean isGoodFileRead = fileDatastore.readSession(sessionFile, sessionManager, window);
        if (isGoodFileRead) {
          JMoneyPlugin.getDefault().setSessionManager(sessionManager);
        }
            }
        }
    
  }

  /**
   * Selection in the workbench has been changed. We 
   * can change the state of the 'real' action here
   * if we want, but this can only happen after 
   * the delegate has been created.
   * @see IWorkbenchWindowActionDelegate#selectionChanged
   */
  public void selectionChanged(IAction action, ISelection selection) {
  }

  /**
   * We can use this method to dispose of any system
   * resources we previously allocated.
   * @see IWorkbenchWindowActionDelegate#dispose
   */
  public void dispose() {
  }

  /**
   * We will cache window object in order to
   * be able to provide parent shell for the message dialog.
   * @see IWorkbenchWindowActionDelegate#init
   */
  public void init(IWorkbenchWindow window) {
    this.window = window;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.