Package org.jampa

Source Code of org.jampa.Application

/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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.
*/

package org.jampa;

import java.net.URL;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.jampa.controllers.Controller;
import org.jampa.logging.Log;
import org.jampa.utils.CommandLineParser;
import org.jampa.utils.SystemUtils;


/**
* This class controls all aspects of the application's execution
*/
public class Application implements IApplication {
 
  public static final String ID = "jampa-core"; //$NON-NLS-1$

  private void setUserHome(String userHome) {
   
    SystemUtils.redefineUserHome(userHome);
   
    Location instanceLoc = Platform.getInstanceLocation();
    try {
      instanceLoc.set(new URL("file", null, SystemUtils.applicationDirectory), false); //$NON-NLS-1$
    } catch (Exception e) {
      Log.getInstance(CommandLineParser.class).error(e.getMessage());
    }
  }
 
  /* (non-Javadoc)
   * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
   */
  public Object start(IApplicationContext context) {
   
    /*
     * User Home setting. This block must stay first, and no call to Log.getInstance() should
     * be made before the end of this block, as it will set the wrong path for Jampa's log file.
     */
    CommandLineParser parser = new CommandLineParser();
    parser.parse(Platform.getApplicationArgs());
   
    String userHome;
    if (parser.hasCustomDirectory()) {
      userHome = parser.getCustomDirectory();
    } else {
      userHome = System.getProperty("user.home"); //$NON-NLS-1$
    }
    setUserHome(userHome);
    /*
     * End of User Home setting.
     */
   
    if (parser.getCustomDirectoryErrorMessage() != null) {
      Log.getInstance(Application.class).warn(parser.getCustomDirectoryErrorMessage()); //$NON-NLS-1$
    }
   
    Log.getInstance(Application.class).info(Platform.getBundle(ID).getHeaders().get("Bundle-Name") + " version " + Platform.getBundle(ID).getHeaders().get("Bundle-Version") + " started."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    Log.getInstance(Application.class).info("Platform informations: " + Platform.getOSArch() + ", " + Platform.getOS() + ", " + Platform.getNL());     //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$     
   
    PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS, false);
    PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR, IWorkbenchPreferenceConstants.TOP_RIGHT);   
    PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.KEY_CONFIGURATION_ID, "Jampa.keySheme"); //$NON-NLS-1$
   
    Log.getInstance(Application.class).info("fileSeparator = " + SystemUtils.fileSeparator); //$NON-NLS-1$
    Log.getInstance(Application.class).info("userHome = " + SystemUtils.userHome); //$NON-NLS-1$
    Log.getInstance(Application.class).info("applicationDirectory = " + SystemUtils.applicationDirectory); //$NON-NLS-1$
    Log.getInstance(Application.class).info("playlistDirectory = " + SystemUtils.playlistDirectory); //$NON-NLS-1$
    Log.getInstance(Application.class).info("podcastDirectory = " + SystemUtils.podcastDirectory); //$NON-NLS-1$
    Log.getInstance(Application.class).info("currentDirectory = " + SystemUtils.currentDir); //$NON-NLS-1$
       
    if (parser.isPurgeDemanded()) {
      SystemUtils.purgeConfiguration();
    }
   
    SystemUtils.createConfiguration();   
   
    Controller.getInstance().setCommandLineParser(parser);
   
    Display display = PlatformUI.createDisplay();
    try {
      int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
      if (returnCode == PlatformUI.RETURN_RESTART) {
        return IApplication.EXIT_RESTART;
      }
      return IApplication.EXIT_OK;
    } finally {
      Log.getInstance(Application.class).info("Application stoped."); //$NON-NLS-1$
      display.dispose();
    }
  }

  /* (non-Javadoc)
   * @see org.eclipse.equinox.app.IApplication#stop()
   */
  public void stop() {
    final IWorkbench workbench = PlatformUI.getWorkbench();
    if (workbench == null)
      return;
    final Display display = workbench.getDisplay();
    display.syncExec(new Runnable() {
      public void run() {
        if (!display.isDisposed())
          workbench.close();
      }
    });
  }
}
TOP

Related Classes of org.jampa.Application

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.