Package net.sf.logsaw.rcp

Source Code of net.sf.logsaw.rcp.LogSawApplication

/*******************************************************************************
* Copyright (c) 2010, 2011 LogSaw project and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    LogSaw project committers - initial API and implementation
*******************************************************************************/
package net.sf.logsaw.rcp;

import java.io.IOException;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

/**
* This class controls all aspects of the application's execution
*/
public class LogSawApplication implements IApplication {

  /* (non-Javadoc)
   * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
   */
  @Override
  public Object start(IApplicationContext context) {
    Display display = PlatformUI.createDisplay();
    try {
      if (!lockInstanceLocation()) {
        MessageDialog.openError(Display.getDefault().getActiveShell(),
            Messages.LogSawApplication_title_alreadyRunning,
            Messages.LogSawApplication_message_alreadyRunning);
        return IApplication.EXIT_OK;
      }
      int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
      if (returnCode == PlatformUI.RETURN_RESTART) {
        return IApplication.EXIT_RESTART;
      }
      return IApplication.EXIT_OK;
    } finally {
      display.dispose();
    }
  }

  private boolean lockInstanceLocation() {
    try {
      Location workspaceLoc = Platform.getInstanceLocation();
      if (!workspaceLoc.isSet()) {
        // Use default
        workspaceLoc.set(workspaceLoc.getDefault(), false);
      }
      return workspaceLoc.lock();
    } catch (IOException e) {
      MessageDialog.openError(Display.getDefault().getActiveShell(),
          Messages.LogSawApplication_title_internalError,
          NLS.bind(Messages.LogSawApplication_message_internalError, e.getLocalizedMessage()));
      return false;
    }
  }

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

Related Classes of net.sf.logsaw.rcp.LogSawApplication

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.