Ignore:
Timestamp:
08/26/11 20:34:27 (13 years ago)
Author:
wschamai
Message:
 
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/modelicaml/org.openmodelica.modelicaml.simulation/src/org/openmodelica/modelicaml/simulation/handlers/CGFromEntireModelAndSimThisClassWithOMCSimCenterAction.java

    r850 r932  
    3636
    3737import java.io.IOException;
     38import java.util.List;
    3839
    3940import org.eclipse.core.commands.AbstractHandler;
     
    4647import org.eclipse.core.resources.ResourcesPlugin;
    4748import org.eclipse.core.runtime.CoreException;
     49import org.eclipse.core.runtime.IAdaptable;
    4850import org.eclipse.core.runtime.IProgressMonitor;
    4951import org.eclipse.core.runtime.IStatus;
     
    6062import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
    6163import org.eclipse.emf.transaction.TransactionalEditingDomain;
    62 import org.eclipse.gmt.modisco.infra.browser.uicore.internal.model.ModelElementItem;
     64import org.eclipse.jface.viewers.ISelection;
    6365import org.eclipse.jface.viewers.IStructuredSelection;
    6466import org.eclipse.jface.wizard.WizardDialog;
     67import org.eclipse.papyrus.core.utils.BusinessModelResolver;
    6568import org.eclipse.papyrus.core.utils.EditorUtils;
    66 import org.eclipse.papyrus.diagram.common.editparts.IUMLEditPart;
    6769import org.eclipse.papyrus.resource.uml.UmlModel;
    6870import org.eclipse.papyrus.resource.uml.UmlUtils;
    6971import org.eclipse.swt.widgets.Shell;
    7072import org.eclipse.ui.PlatformUI;
    71 import org.eclipse.uml2.uml.Element;
    7273import org.eclipse.uml2.uml.NamedElement;
    7374import org.openmodelica.modelicaml.common.services.StringUtls;
     
    9394 * @author rmwscham
    9495 */
    95 public class GenerateModelicaCodeFromEntireModelicaMLModelAndSimulationThisClassWithOMCSimCenterAction extends AbstractHandler {
     96public class CGFromEntireModelAndSimThisClassWithOMCSimCenterAction extends AbstractHandler {
    9697
    9798    // private ModelManager modelManager = null;
     
    124125     */
    125126    public Object execute(ExecutionEvent event) throws ExecutionException {
    126         TransactionalEditingDomain editingDomain = EditorUtils
    127                 .getTransactionalEditingDomain();
     127        TransactionalEditingDomain editingDomain = EditorUtils.getTransactionalEditingDomain();
    128128        editingDomain.getCommandStack().execute(getCommand(editingDomain));
    129129        return null;
     
    139139    protected Command getCommand(TransactionalEditingDomain editingDomain) {
    140140
    141         Element umlElement = null;
     141        EObject umlElement = null;
    142142        IStructuredSelection selection = (IStructuredSelection) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();     
    143143        Object input = ((IStructuredSelection) selection).getFirstElement();
    144144       
    145         if (input instanceof ModelElementItem) {
    146             EObject eObject = ((ModelElementItem)input).getEObject();
    147             if ( eObject instanceof Element ) {
    148                 umlElement = (Element)eObject;
    149             }
    150         }
    151         else if (input instanceof IUMLEditPart) {
    152             umlElement = ((IUMLEditPart)input).getUMLElement();
    153         }
    154        
    155         UmlModel umlModel = UmlUtils.getUmlModel();
    156         modelFileURI = umlModel.getResourceURI().toString();
    157 
    158         modelName = umlModel.getResourceURI().lastSegment();
    159         project = umlModel.getResourceURI().path().replace(modelName, "")
    160                 .replace("/resource/", "");
    161 
    162        
    163 //      String projectName = umlModel.getResourceURI().path().replace(modelName, "").replace("/resource/", "");
    164         String projectName = umlModel.getResource().getURI().segment(1);
    165         IWorkspace workspace = ResourcesPlugin.getWorkspace();
    166         IWorkspaceRoot root = workspace.getRoot();
    167         IProject iProject = root.getProject(projectName);
    168        
    169         String projectPath = iProject.getLocationURI().toString().replaceFirst("file:\\/", "");
    170        
    171         String modelElementQualifiedName = ((NamedElement)umlElement).getQualifiedName();
    172         String temp_modelElementWoSpecChar = StringUtls.replaceSpecCharExceptThis(modelElementQualifiedName, "::");
    173         final String modelElementDotPath = temp_modelElementWoSpecChar.replaceAll("::", ".");
    174         final String packageMoFilePath = projectPath+"/code-gen/"+ StringUtls.replaceSpecChar(umlElement.getModel().getName()) + "/" +"package.mo";
    175 
    176         URI chainURI = URI.createPlatformPluginURI("/org.openmodelica.modelicaml.gen.modelica/bin/code_generation.chain",true);
    177         ResourceSet rs = new ResourceSetImpl();
    178         Resource r = (Resource) rs.createResource(chainURI);
    179         try {
    180             r.load(null);
    181         } catch (IOException e1) {
    182             // TODO Auto-generated catch block
    183             e1.printStackTrace();
    184         }
    185 
    186         myChain = (CChain) r.getContents().get(0);
    187 
    188         String modelFilePath = modelFileURI.replace("platform:/resource/", "");
    189         String outputFolderPath = project;
    190         String logPath = project + "/errors.log";
    191 
    192         // Don't create Parameter Files... simply set the correct path into them
    193         EList<File> files = myChain.getParametersFiles();
    194         for (File file : files) {
    195             if (file instanceof CModel) {
    196                 file.setPath(modelFilePath);
    197             } else if (file instanceof CFolder) {
    198                 file.setPath(outputFolderPath);
    199             } else if (file instanceof CLog) {
    200                 file.setPath(logPath);
    201             }
    202         }
    203 
    204         Job job = new Job("Modelica Simulation with OMCSim Center") {
    205             protected IStatus run(IProgressMonitor monitor) {
    206                 Boolean RegenerateCodeBeforeEachSimulation = Platform.getPreferencesService().getBoolean("org.openmodelica.modelicaml.preferences", "RegenerateCodeBeforeEachSimulation", false, null);
    207                 if (RegenerateCodeBeforeEachSimulation) {
    208                     runchain(monitor);
     145//      if (input instanceof ModelElementItem) {
     146//      EObject eObject = ((ModelElementItem)input).getEObject();
     147//      if ( eObject instanceof Element ) {
     148//          umlElement = (Element)eObject;
     149//      }
     150//  }
     151//  else if (input instanceof IUMLEditPart) {
     152//      umlElement = ((IUMLEditPart)input).getUMLElement();
     153//  }
     154       
     155        umlElement = (EObject) adaptSelectedElement(getCurrentSelections().get(0));
     156       
     157        if (umlElement instanceof NamedElement) {
     158       
     159            UmlModel umlModel = UmlUtils.getUmlModel();
     160            modelFileURI = umlModel.getResourceURI().toString();
     161
     162            modelName = umlModel.getResourceURI().lastSegment();
     163            project = umlModel.getResourceURI().path().replace(modelName, "").replace("/resource/", "");
     164           
     165//          String projectName = umlModel.getResourceURI().path().replace(modelName, "").replace("/resource/", "");
     166            String projectName = umlModel.getResource().getURI().segment(1);
     167            IWorkspace workspace = ResourcesPlugin.getWorkspace();
     168            IWorkspaceRoot root = workspace.getRoot();
     169            IProject iProject = root.getProject(projectName);
     170           
     171            String projectPath = iProject.getLocationURI().toString().replaceFirst("file:\\/", "");
     172           
     173            String modelElementQualifiedName = ((NamedElement)umlElement).getQualifiedName();
     174            String temp_modelElementWoSpecChar = StringUtls.replaceSpecCharExceptThis(modelElementQualifiedName, "::");
     175            final String modelElementDotPath = temp_modelElementWoSpecChar.replaceAll("::", ".");
     176            final String packageMoFilePath = projectPath+"/code-gen/"+ StringUtls.replaceSpecChar( ((NamedElement)umlElement).getModel().getName()) + "/" +"package.mo";
     177
     178            URI chainURI = URI.createPlatformPluginURI("/org.openmodelica.modelicaml.gen.modelica/bin/code_generation.chain",true);
     179            ResourceSet rs = new ResourceSetImpl();
     180            Resource r = (Resource) rs.createResource(chainURI);
     181            try {
     182                r.load(null);
     183            } catch (IOException e1) {
     184                // TODO Auto-generated catch block
     185                e1.printStackTrace();
     186            }
     187
     188            myChain = (CChain) r.getContents().get(0);
     189
     190            String modelFilePath = modelFileURI.replace("platform:/resource/", "");
     191            String outputFolderPath = project;
     192            String logPath = project + "/errors.log";
     193
     194            // Don't create Parameter Files... simply set the correct path into them
     195            EList<File> files = myChain.getParametersFiles();
     196            for (File file : files) {
     197                if (file instanceof CModel) {
     198                    file.setPath(modelFilePath);
     199                } else if (file instanceof CFolder) {
     200                    file.setPath(outputFolderPath);
     201                } else if (file instanceof CLog) {
     202                    file.setPath(logPath);
    209203                }
    210                 return Status.OK_STATUS;
    211             }
    212         };
    213         job.setUser(true);
    214         job.schedule();
    215        
    216        
    217         NewSimulationProjectFromModellingEnvironmentWizard projectWizard = new NewSimulationProjectFromModellingEnvironmentWizard(packageMoFilePath, modelElementDotPath, job);
    218         WizardDialog wizard = new WizardDialog(
    219                 this.shell, projectWizard
    220                 );
    221         wizard.open();
    222        
    223         if (projectWizard.getCreateConfig().equals("interactive")) {
    224             WizardDialog wizardinteractive = new WizardDialog(
    225                     this.shell,
    226                     new SessionConfiguration_InteractiveWizard());
    227             wizardinteractive.open();
    228         } else if (projectWizard.getCreateConfig().equals("noninteractive")) {
    229             WizardDialog wizardnoninteractive = new WizardDialog(
    230                     this.shell,
    231                     new SessionConfiguration_NonInteractiveWizard());
    232             wizardnoninteractive.open();
    233         }
    234        
    235         CompoundCommand cc = new CompoundCommand("Modelica Code Generation");
    236         return (cc.unwrap());
    237        
    238     }
    239 
     204            }
     205
     206            Job job = new Job("Modelica Simulation with OMCSim Center") {
     207                protected IStatus run(IProgressMonitor monitor) {
     208                    Boolean RegenerateCodeBeforeEachSimulation = Platform.getPreferencesService().getBoolean("org.openmodelica.modelicaml.preferences", "RegenerateCodeBeforeEachSimulation", false, null);
     209                    if (RegenerateCodeBeforeEachSimulation) {
     210                        runchain(monitor);
     211                    }
     212                    return Status.OK_STATUS;
     213                }
     214            };
     215            job.setUser(true);
     216            job.schedule();
     217           
     218           
     219            NewSimulationProjectFromModellingEnvironmentWizard projectWizard = new NewSimulationProjectFromModellingEnvironmentWizard(packageMoFilePath, modelElementDotPath, job);
     220            WizardDialog wizard = new WizardDialog(
     221                    this.shell, projectWizard
     222                    );
     223            wizard.open();
     224           
     225            if (projectWizard.getCreateConfig().equals("interactive")) {
     226                WizardDialog wizardinteractive = new WizardDialog(
     227                        this.shell,
     228                        new SessionConfiguration_InteractiveWizard());
     229                wizardinteractive.open();
     230            } else if (projectWizard.getCreateConfig().equals("noninteractive")) {
     231                WizardDialog wizardnoninteractive = new WizardDialog(
     232                        this.shell,
     233                        new SessionConfiguration_NonInteractiveWizard());
     234                wizardnoninteractive.open();
     235            }
     236           
     237            CompoundCommand cc = new CompoundCommand("Modelica Code Generation");
     238            return (cc.unwrap());           
     239        }
     240       
     241        return null;
     242    }
     243
     244   
     245    private List<Object> getCurrentSelections() {
     246        ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
     247        if(selection instanceof IStructuredSelection) {
     248            IStructuredSelection structuredSelection = (IStructuredSelection)selection;
     249            return structuredSelection.toList();
     250        }
     251   
     252        return null;
     253    }
     254   
     255    protected EObject adaptSelectedElement( Object selection) {
     256        EObject eObject = null;
     257        if(selection != null) {
     258            if(selection instanceof IAdaptable) {
     259                selection = ((IAdaptable)selection).getAdapter(EObject.class);
     260            }
     261            Object businessObject = BusinessModelResolver.getInstance().getBusinessModel(selection);
     262            if(businessObject instanceof EObject) {
     263                eObject = (EObject)businessObject;
     264            }
     265        }
     266        return eObject;
     267    }
     268   
    240269    /**
    241270     * Runchain.
Note: See TracChangeset for help on using the changeset viewer.