Activator.java :  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » ui » ide » Java Open Source

Java Open Source » ESB » cbesb 1.2 
cbesb 1.2 » com » bostechcorp » cbesb » ui » ide » Activator.java
/*
 * ChainBuilder ESB
 *          Visual Enterprise Integration
 * 
 * Copyright (C) 2006 Bostech Corporation
 * 
 * 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., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *
 * $Id$
 */

package com.bostechcorp.cbesb.ui.ide;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.bostechcorp.cbesb.common.i18n.I18N;
import com.bostechcorp.cbesb.common.i18n.Messages;
import com.bostechcorp.cbesb.ui.ide.classpath.CBESBCoreClasspathContainer;
import com.bostechcorp.cbesb.ui.ide.classpath.CBJBISAClasspathContainer;
import com.bostechcorp.cbesb.ui.ide.classpath.ClassPathContainerRepository;
import com.bostechcorp.cbesb.ui.util.tagged.mdl.TaggedMDLFileDocument;
import com.bostechcorp.cbesb.ui.util.tagged.mdl.TaggedMDLFileParser;

public class Activator extends AbstractUIPlugin {

  // The plug-in ID
  public static final String PLUGIN_ID = "com.bostechcorp.cbesb.ui.ide";

  // The shared instance
  private static Activator plugin;
  
  public static String projectName = null;

  /**
   * The constructor
   */
  public Activator() {
    plugin = this;
  }

  /**
   * Remove the file from the tag.
   */
  public void removeFromTag(IPath path) {
    File file = new File(path.toString());
    /*if (file.isDirectory()) {
      System.out.println("I got here folder!");
      String[] children = file.list();
      deleteDir(path);
    } else {*/
      if (path.toString().endsWith(".mdl")) {
        String name = path.toString().substring(
            path.toString().lastIndexOf("/") + 1,
            path.toString().length());
        deleteMdl(path, name);
      }
    //}

    
  }

  private void deleteDir(IPath filePath) {
    String filepath = filePath.toString();
    String xmlPath = null;
    String fileName = null;
    if (filepath.contains("/x12/")) {
      xmlPath = filepath.substring(0, filepath.indexOf("/x12"))
          + "/taggedMDL.xml";
      fileName = filepath.substring(filepath.indexOf("x12/"), filepath
          .length());
      File taggedMDLFile = new File(xmlPath);
      if(!taggedMDLFile.exists())return;
      
      TaggedMDLFileParser parser = new TaggedMDLFileParser();
      TaggedMDLFileDocument doc = parser.load(taggedMDLFile);
      
      for (int i = 0; i < doc.getMDLList().getAllMDLFile().length; i++) {
        if(doc.getMDLList().getAllMDLFile()[i].getName().contains(fileName))
          doc.getMDLList().removeSubproject(doc.getMDLList().getAllMDLFile()[i].getName());
//          doc.getMDLList().getAllMDLFile()[i];
      }
      doc.getMDLList().removeSubproject(fileName);
      try {
        doc.serialize(new FileOutputStream(taggedMDLFile));
      } catch (FileNotFoundException e) {
         //TODO
        //e.printStackTrace();
      }
    }

  }

  private void deleteMdl(IPath path, String name) {
     
    /* taggedMDL.xml */
    String filepath = path.toString();
    String xmlPath = null;
    String fileName = null;
    if (filepath.contains("/x12/")) {
      xmlPath = filepath.substring(0, filepath.indexOf("/x12"))
          + "/taggedMDL.xml";
      fileName = filepath.substring(filepath.indexOf("x12/"), filepath
          .length());
    } 
    else if(filepath.contains("/hl7/")){
      xmlPath = filepath.substring(0, filepath.indexOf("/hl7"))
        + "/taggedMDL.xml";
      fileName = filepath.substring(filepath.indexOf("hl7/"), filepath
          .length());
    }
    else {
      xmlPath = filepath.substring(0, filepath.length() - name.length())
          + "taggedMDL.xml";
      fileName = name;
    }
    File taggedMDLFile = new File(xmlPath);
    if(!taggedMDLFile.exists())return;
    TaggedMDLFileParser parser = new TaggedMDLFileParser();
    TaggedMDLFileDocument doc = null;
    try
    {
      doc = parser.load(taggedMDLFile);
    
    
      doc.getMDLList().removeSubproject(fileName);
    }
    catch(Exception e){
      //TODO
    }
    try {
      doc.serialize(new FileOutputStream(taggedMDLFile));
    } catch (FileNotFoundException e) {
       
      e.printStackTrace();
    }
  }

  /**
   * (non-Javadoc)
   * 
   * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
   */
  public void start(BundleContext context) throws Exception {
    super.start(context);
    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    IResourceChangeListener listener = new IResourceChangeListener() {
      public void resourceChanged(IResourceChangeEvent event) {
        try {
          event.getDelta().accept(new IResourceDeltaVisitor() {
            public boolean visit(IResourceDelta delta)
                throws CoreException {
              switch (delta.getKind()) {
              case IResourceDelta.ADDED:
              {
                
                break;
              }
              case IResourceDelta.REMOVED:
                // System.out.println("the extension is
                // "+delta.getFullPath());
                // System.out.println("the location is
                // "+delta.getResource().getLocation().toString());
                // System.out.println("the name is
                // "+delta.getResource().getName());
                Path path = new Path(delta.getResource()
                    .getLocation().toString());
                removeFromTag(path);
                break;
              case IResourceDelta.CHANGED:
              {
                
                break;
              }
              default:
                break;
              }
              return true;
            }

          });
        } catch (CoreException ex) {
//TODO
        }
        
      }
    };

    workspace.addResourceChangeListener(listener,
        IResourceChangeEvent.PRE_CLOSE
            | IResourceChangeEvent.PRE_DELETE
            | IResourceChangeEvent.PRE_AUTO_BUILD
            | IResourceChangeEvent.POST_AUTO_BUILD
            | IResourceChangeEvent.POST_CHANGE);
    // workspace.addResourceChangeListener(listener);
    // ClassPathContainerRepository.getInstance().addClassPathEntry(J2EE14ClasspathContainer.CLASSPATH_CONTAINER);
    ClassPathContainerRepository.getInstance().addClassPathEntry(
        CBESBCoreClasspathContainer.CLASSPATH_CONTAINER);
    ClassPathContainerRepository.getInstance().addClassPathEntry(
        CBJBISAClasspathContainer.CLASSPATH_CONTAINER);
    
    
  
  }

  /*
   * (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 Activator getDefault() {
    return plugin;
  }

  /**
   * Gets the base installation dir of the plugin
   * 
   * @return The installation dir
   */
  public String getBaseDir() {
    try {
      URL installURL = Platform
          .asLocalURL(this.getBundle().getEntry("/"));//$NON-NLS-1$
      return installURL.getFile().toString();
    } catch (IOException ioe) { // "Cannot compute base installation dir of
      // plugin"
      logError(I18N.getString(Messages.IDE_ERR_CANNOT_INSTALL), ioe);//$NON-NLS-1$
    }
    return null;
  }

  /********************************************
   */

  /**
   * Logs the specified status with this plug-in's log.
   * 
   * @param status
   *            status to log
   */
  public static void log(IStatus status) {
    getDefault().getLog().log(status);
  }

  /**
   * Logs an internal error with the specified throwable
   * 
   * @param e
   *            the exception to be logged
   */
  public static void log(Throwable e) {
    log(new Status(IStatus.ERROR, getUniqueIdentifier(), 0,
        "Internal Error", e));//$NON-NLS-1$
  }

  /**
   * Logs an internal error with the specified message.
   * 
   * @param message
   *            the error message to log
   */
  public static void logError(String message) {
    logError(message, null);
  }

  /**
   * Logs a throwable with the specified message.
   * 
   * @param message
   *            Message to log
   * @param throwable
   *            Throwable to log
   */
  public static void logError(String message, Throwable throwable) {
    log(new Status(IStatus.ERROR, getUniqueIdentifier(), 0, message,
        throwable));
  }

  /**
   * Convenience method which returns the unique identifier of this plugin.
   * 
   * @return The unique indentifier value
   */
  public static String getUniqueIdentifier() {
    if (getDefault() == null) {
      // If the default instance is not yet initialized,
      // return a static identifier. This identifier must
      // match the plugin id defined in plugin.xml
      return "org.jboss.ide.eclipse.core";//$NON-NLS-1$
    }
    return getDefault().getBundle().getSymbolicName();
  }

}
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.