Package tool

Source Code of tool.ToolPlugin

package tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.statushandlers.StatusManager;
import org.osgi.framework.BundleContext;

import tool.model.ToolPlan;
import tool.repository.CommandException;
import tool.repository.NotConnectedException;
import tool.repository.RepositorySession;


/**
* The activator class controls the plug-in life cycle
*/
public class ToolPlugin extends AbstractUIPlugin implements IStartup{

  // The plug-in ID
  public static final String PLUGIN_ID = "Tool"; //$NON-NLS-1$

  // The shared instance
  private static ToolPlugin plugin;
 
  private FormColors formColors;
  public static final String IMG_FORM_BG = "formBg"; //$NON-NLS-1$
  public static final String IMG_LARGE = "large"; //$NON-NLS-1$
  public static final String IMG_HORIZONTAL = "horizontal"; //$NON-NLS-1$
  public static final String IMG_VERTICAL = "vertical"; //$NON-NLS-1$
  public static final String IMG_SAMPLE = "sample"; //$NON-NLS-1$
  public static final String IMG_WIZBAN = "wizban"; //$NON-NLS-1$
  public static final String IMG_LINKTO_HELP = "linkto_help"; //$NON-NLS-1$
  public static final String IMG_HELP_TOPIC = "help_topic"; //$NON-NLS-1$
  public static final String IMG_CLOSE = "close"; //$NON-NLS-1$
 
  public static final String FORTE_ROOT_PREFERENCE = "ForteRoot";
  public static final String FORTE_LOGGER_PREFERENCE = "ForteLogger";

  public static final String TOOL_NAVIGATOR_ID = "tool.navigator";

  public static final String METHOD_SIGNATURE_PREFERENCE = "ForteMethodShowSignature";

  public static final String USE_PARSER_PREFERENCE = "ForteUseForteParser";
 
  private static int nextPort = 15001; // the interface port
 
  /**
   * The constructor
   */
  public ToolPlugin() {
  }
 

  /*
   * (non-Javadoc)
   * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
   */
  public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this;
    setupResourceChangeListener();
    ToolPlan.getForteLibraryCache();
   
  }

  /*
   * (non-Javadoc)
   * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
   */
  public void stop(BundleContext context) throws Exception {
    plugin = null;
    super.stop(context);
  }

  /**
   * Returns the shared instance
   *
   * @return the shared instance
   */
  public static ToolPlugin getDefault() {
    return plugin;
  }
 


  /**
   * Returns an image descriptor for the image file at the given
   * plug-in relative path
   *
   * @param path the path
   * @return the image descriptor
   */
  public static ImageDescriptor getImageDescriptor(String path) {
    return imageDescriptorFromPlugin(PLUGIN_ID, path);
  }
 
  protected void setupResourceChangeListener(){
    IResourceChangeListener rcl = new IResourceChangeListener() {
      public void resourceChanged(IResourceChangeEvent event) {
        IProject project = event.getResource().getProject();
        RepositorySession session = RepositorySession.getRepositorySession(project);
        if (session != null){
          try {
            if (session.isWorkspaceOpen()){
              session.closeWorkspace();
            }
            if (session.isRepositoryOpen()){
              session.closeRepository();
            }
            session.killInterfaceProcess();
          } catch (NotConnectedException e) {
            log(IStatus.WARNING, "Error terminating repository interface", e);
          } catch (CommandException e) {
            log(IStatus.WARNING, "Error terminating repository interface", e);
          }
        }
      }
    };
    ResourcesPlugin.getWorkspace().addResourceChangeListener(rcl, IResourceChangeEvent.PRE_CLOSE);
   
  }

  public static void log(int level, String message){
    plugin.getLog().log(new Status(level, PLUGIN_ID, message, null));
  }
  public static void log(int level, String message, Throwable e){
    plugin.getLog().log(new Status(level, PLUGIN_ID, message, e));
  }

  public static Image getImage(String imagePath) {
    ImageDescriptor imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(ToolPlugin.PLUGIN_ID, imagePath);
        Image image = imageDescriptor.createImage();

        return image;
    }

    public static ImageDescriptor getIDEImageDescriptor(String imagePath) {
        return AbstractUIPlugin.imageDescriptorFromPlugin(ToolPlugin.PLUGIN_ID, imagePath);
    }
  public FormColors getFormColors(Display display) {
    if (formColors == null) {
      formColors = new FormColors(display);
      formColors.markShared();
    }
    return formColors;
  }
 
  protected void initializeDefaultPreferences(IPreferenceStore store) {
    String forteRoot = System.getenv("FORTE_ROOT");
    store.setDefault(FORTE_ROOT_PREFERENCE, (forteRoot==null) ? "<no forte root>" : forteRoot);
    store.setDefault(FORTE_LOGGER_PREFERENCE, ToolProjectSupport.DEFAULT_LOG_FLAGS);
    store.setDefault(METHOD_SIGNATURE_PREFERENCE, false);
    store.setDefault(USE_PARSER_PREFERENCE, false);
  }
  public static boolean isSignatureShown(){
    IPreferenceStore store =
      ToolPlugin.getDefault().getPreferenceStore();
    return store.getBoolean(METHOD_SIGNATURE_PREFERENCE);
  }

  public static boolean useParser(){
    IPreferenceStore store =
      ToolPlugin.getDefault().getPreferenceStore();
    return store.getBoolean(USE_PARSER_PREFERENCE);
  }

  public static String getForteRoot(){
    IPreferenceStore store =
      ToolPlugin.getDefault().getPreferenceStore();
    return store.getString(FORTE_ROOT_PREFERENCE);
  }

  public static String getForteLogger() {
    IPreferenceStore store =
      ToolPlugin.getDefault().getPreferenceStore();
    return store.getString(FORTE_LOGGER_PREFERENCE);
  }

  public static String stack2string(Throwable e) {
      try {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
      }
      catch(Exception e2) {
        return "bad stack2string";
      }
     }
  public static void showError(String message, Throwable e) {
    IStatus status = new Status(IStatus.ERROR, ToolPlugin.PLUGIN_ID, message, e);
    //ErrorDialog.openError(Display.getDefault().getActiveShell(), "Error", message, status);
    StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.SHOW);
  }
 
  public static MessageConsole findConsole(String name) {
    MessageConsole myConsole = null;
    ConsolePlugin plugin = ConsolePlugin.getDefault();
    if (plugin != null){
      IConsoleManager conMan = plugin.getConsoleManager();
      IConsole[] existing = conMan.getConsoles();
      for (int i = 0; i < existing.length; i++)
        if (name.equals(existing[i].getName()))
          return (MessageConsole) existing[i];
      //no console found, so create a new one
      myConsole = new MessageConsole(name, null);
      conMan.addConsoles(new IConsole[]{myConsole});
    }
    return myConsole;
  }

 
  public static void copyFile(File f1, File f2){
      try{
          InputStream in = new FileInputStream(f1);
         
          //For Append the file.
//          OutputStream out = new FileOutputStream(f2,true);

          //For Overwrite the file.
          OutputStream out = new FileOutputStream(f2);

          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0){
            out.write(buf, 0, len);
          }
          in.close();
          out.close();
          System.out.println("File copied.");
        }
        catch(FileNotFoundException ex){
          showError("File copy error", ex);
          System.exit(0);
        }
        catch(IOException e){
          showError("File copy error", e);     
        }
      }
  /**
   * returns the next interface port number
   * @return
   */
  public synchronized static String getNextPort(){
    String port = Integer.toString(nextPort);
    nextPort++;
    return port;
  }


  @Override
  public void earlyStartup() {
    System.out.println("earlyStartup() called");
   
  }
}
TOP

Related Classes of tool.ToolPlugin

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.