Changeset 895

Show
Ignore:
Timestamp:
05/10/12 18:13:06 (13 months ago)
Author:
anders
Message:

Redid the code generation wizard's UI with a file dialog for selecting the location of a DPF DSML/metamodel. The located file is then read and written to a file called metamodel.xmi in a folder called models. This ensures that we always have editor support for a given DSML.

Location:
dpf-model-editor/trunk/plugins/no.hib.dpf.codegen.xpand.ui/src/no/hib/dpf/codegen/xpand/ui
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • dpf-model-editor/trunk/plugins/no.hib.dpf.codegen.xpand.ui/src/no/hib/dpf/codegen/xpand/ui/DpfMetaModelUIPlugin.java

    r852 r895  
    296296                        return "";               
    297297                } 
    298                  
    299                 //Returns every IFile which matches isValidDsm 
    300 //              public boolean visit(IResource resource) throws CoreException { 
    301 //                      if (resource instanceof IFile) { 
    302 //                              IFile f = (IFile) resource; 
    303 //                              if (isValidDsm(f)) { 
    304 ////                                    if (JavaCore.create(f.getParent()) != null) { //dafuq 
    305 //                                              try { 
    306 //                                                      URI uri = URI.createURI(f.getFullPath().toString()); 
    307 ////                                                    DpfMetamodel p = loadDsm(uri); 
    308 //                                                      Specification spectmp = CoreFactory.eINSTANCE.loadSpecification(uri); 
    309 ////                                                    if (p != null) { 
    310 ////                                                            fileModels.put(f, p); 
    311 ////                                                    } 
    312 //                                                      if(spectmp != null) { 
    313 //                                                              fileModels.put(f, spectmp); 
    314 //                                                      } 
    315 //                                              } catch (Exception e) { 
    316 //                                                      System.out.println(); 
    317 //                                              } 
    318 ////                                    } 
    319 //                              } 
    320 //                      } 
    321 //                      return true; 
    322 //              } 
    323                  
    324 //              private boolean isValidDsm(IFile f) { 
    325 //              return !f.isDerived() 
    326 //                              && f.isAccessible() 
    327 //                              && !f.isLinked() 
    328 //                              && (f.getName().endsWith("dsm.xmi") && f.exists()); 
    329 //      } 
    330                  
    331 //              public synchronized final static DpfMetamodel loadDsm(URI uri) { 
    332 //                      Resource r = dpfResourceSet.getResource(uri, false); 
    333 //                      if (r == null) { 
    334 //                              // this resource has not been loaded before... 
    335 //                              // hence, demandLoad it 
    336 //                              r = dpfResourceSet.getResource(uri, true); 
    337 //                      } else { 
    338 //                              if (r.isLoaded()) { // Is this a request to reload the resource? 
    339 //                                      r.unload(); 
    340 //                              } 
    341 //                              try { 
    342 //                                      r.load(new HashMap<Object, Object>()); 
    343 //                              } catch (IOException e) { 
    344 //                                      throw new RuntimeException(e); 
    345 //                              } 
    346 //                      } 
    347 // 
    348 //                      List<EObject> c = r.getContents(); 
    349 //                      if (c.isEmpty()) { 
    350 //                              return null; 
    351 //                      } 
    352 //                      if (c.get(0) instanceof DpfMetamodel) { 
    353 //                              DpfMetamodel p = (DpfMetamodel) c.get(0); 
    354 //                              URIConverter.URI_MAP.put(URI.createURI(uri.lastSegment()), uri); 
    355 //                              return p; 
    356 //                      } 
    357 //                      return null; 
    358 //              } 
    359298        } 
    360299} 
  • dpf-model-editor/trunk/plugins/no.hib.dpf.codegen.xpand.ui/src/no/hib/dpf/codegen/xpand/ui/wizards/DpfGeneratorPage.java

    r766 r895  
    33import org.eclipse.jface.wizard.WizardPage; 
    44import org.eclipse.swt.SWT; 
     5import org.eclipse.swt.events.SelectionEvent; 
     6import org.eclipse.swt.events.SelectionListener; 
    57import org.eclipse.swt.layout.GridData; 
    68import org.eclipse.swt.layout.GridLayout; 
     9import org.eclipse.swt.widgets.Button; 
    710import org.eclipse.swt.widgets.Composite; 
     11import org.eclipse.swt.widgets.FileDialog; 
    812import org.eclipse.swt.widgets.Label; 
    913import org.eclipse.swt.widgets.Text; 
     
    1317        private Text projectName, metaModelLocation; 
    1418        private Label pname, mml; 
     19        private Button bml; 
    1520         
    1621        protected DpfGeneratorPage() { 
     
    2126 
    2227        @Override 
    23         public void createControl(Composite parent) { 
     28        public void createControl(final Composite parent) { 
    2429                Composite comp = new Composite(parent, SWT.NULL); 
    2530                 
    2631                GridLayout gridLayout = new GridLayout(); 
    27                 gridLayout.numColumns = 2; 
     32                gridLayout.numColumns = 3; 
    2833                 
    2934                comp.setLayout(gridLayout); 
    3035                 
    3136                pname = new Label(comp, SWT.RIGHT); 
    32                 pname.setText("Project Name:"); 
    33                  
    34                 projectName = new Text(comp, SWT.SINGLE); 
    35                 projectName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
     37                pname.setText("Project name:"); 
     38 
     39                projectName = new Text(comp, SWT.SINGLE | SWT.BORDER); 
     40                projectName.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 2, 1)); 
    3641                 
    3742                mml = new Label(comp, SWT.RIGHT); 
    38                 mml.setText("Metamodel Location:"); 
     43                mml.setText("Metamodel location:"); 
    3944                 
    40                 metaModelLocation = new Text(comp, SWT.SINGLE); 
     45                metaModelLocation = new Text(comp, SWT.SINGLE | SWT.BORDER); 
    4146                metaModelLocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
     47                 
     48                bml = new Button(comp, SWT.PUSH); 
     49                bml.setText("&Location"); 
     50                bml.addSelectionListener(new SelectionListener() { 
     51                         
     52                        @Override 
     53                        public void widgetSelected(SelectionEvent e) { 
     54                                FileDialog fd = new FileDialog(parent.getShell()); 
     55                                fd.setFilterExtensions(new String[] {"*.xmi"}); 
     56                                fd.setFilterNames(new String[] {"*.xmi"}); 
     57                                fd.open(); 
     58                                if(!fd.getFileName().isEmpty()) { 
     59                                        metaModelLocation.setText(fd.getFilterPath() + "/" + fd.getFileName()); 
     60                                } 
     61                        } 
     62                         
     63                        @Override 
     64                        public void widgetDefaultSelected(SelectionEvent e) { 
     65                        } 
     66                }); 
    4267                 
    4368                setControl(comp); 
  • dpf-model-editor/trunk/plugins/no.hib.dpf.codegen.xpand.ui/src/no/hib/dpf/codegen/xpand/ui/wizards/NewDpfGeneratorWizard.java

    r547 r895  
    1212 *******************************************************************************/ 
    1313import java.io.ByteArrayOutputStream; 
     14import java.io.File; 
    1415import java.io.IOException; 
    1516import java.io.InputStream; 
    1617import java.lang.reflect.InvocationTargetException; 
     18import java.net.MalformedURLException; 
    1719import java.net.URL; 
    1820import java.util.ArrayList; 
     
    2931import no.hib.dpf.codegen.xpand.ui.properties.DpfMetaModelProperties; 
    3032 
     33import org.apache.log4j.Logger; 
     34import org.eclipse.core.resources.IFolder; 
    3135import org.eclipse.core.resources.IProject; 
    3236import org.eclipse.core.resources.ResourcesPlugin; 
     
    5862 
    5963        private DpfGeneratorPage page; 
    60  
    6164        private DpfMetaModelProperties props; 
    62          
    6365        private IConfigurationElement configElement; 
    6466         
     67        private String workflowContent; 
    6568        private String metaModelLocation; 
    6669 
    67         private String workflowContent; 
     70        private Logger log; 
     71         
    6872         
    6973        public NewDpfGeneratorWizard() { 
     
    7175                super.setWindowTitle("New DPF Xpand Generator Project"); 
    7276                setNeedsProgressMonitor(true); 
     77                log = Logger.getLogger(NewDpfGeneratorWizard.class); 
    7378        } 
    7479 
     
    132137                if (p == null) 
    133138                        return; 
    134  
     139                 
     140                //We create a folder where our metamodel/models will reside. 
     141                IFolder mfolder = p.getFolder("models"); 
     142                if(!mfolder.exists()) { 
     143                        try { 
     144                                mfolder.create(false, true, monitor); 
     145                        } catch (CoreException e) { 
     146                                log.error("Could not create folder \"models\"", e); 
     147                        } 
     148                } 
     149                 
    135150                String encoding = ResourcesPlugin.getEncoding(); 
    136151                EclipseHelper.createFile(".settings/org.eclipse.core.resources.prefs", 
     
    150165                monitor.worked(1); 
    151166                 
    152                 //Cant access stuff outside of ui thread without this 
     167                //We can not access stuff outside of ui thread without this 
    153168                Display.getDefault().syncExec(new Runnable() { 
    154169                        @Override 
     
    157172                                try { 
    158173                                        props.setNature(true); 
    159                                         //We use .settings file instead 
    160 //                                      props.setDsmPaths(page.getMetaModelLocation()); 
    161                                         metaModelLocation = page.getMetaModelLocation(); 
     174                                        metaModelLocation = page.getMetaModelLocation();  
    162175                                } catch (CoreException e) { 
    163                                         System.err.println("Problem loading dsmlocation from wizard."); 
    164                                         e.printStackTrace(); 
     176                                        log.error("Could not load metamodel location from wizard", e); 
    165177                                } 
    166178                                 
    167179                                try { 
    168                                         WorkflowParser parse = new WorkflowParser(getContentStream("workflowtemplate.txt")); 
    169                                         parse.setProperty("dpf_metamodel", metaModelLocation); 
     180                                        WorkflowParser parse = new WorkflowParser(getUIContentStream("workflowtemplate.txt")); 
     181                                        parse.setProperty("dpf_metamodel", "platform:/resource/" + p.getName() + "/models/metamodel.xmi"); 
    170182                                        parse.setXpandEntryPoint("template::templ::main FOR dpf"); 
    171183                                        workflowContent = parse.getXml(); 
     
    179191                                        e.printStackTrace(); 
    180192                                } 
    181                                  
    182                          
    183193                        } 
    184194                }); 
    185195                 
     196                try { 
     197                        EclipseHelper.createFile("models/metamodel.xmi", p,  
     198                                        getContents(new File(metaModelLocation).toURI().toURL().openStream()), monitor); 
     199                        monitor.worked(1); 
     200                } catch (MalformedURLException e) { 
     201                        // TODO Auto-generated catch block 
     202                        e.printStackTrace(); 
     203                } catch (IOException e) { 
     204                        // TODO Auto-generated catch block 
     205                        e.printStackTrace(); 
     206                } 
    186207                 
    187208                EclipseHelper.createFile("src/workflow/workflow.mwe", p, workflowContent, monitor); 
    188                  
    189                 monitor.worked(1); 
    190                  
    191                 EclipseHelper.createFile("src/template/templ.xpt", p, getContents("xpttemplate.txt"), monitor); 
    192                  
     209                monitor.worked(1); 
     210                 
     211                EclipseHelper.createFile("src/template/templ.xpt", p, getContentsOfTemplateResource("xpttemplate.txt"), monitor); 
    193212                monitor.worked(1); 
    194213                 
     
    196215                                p,  
    197216                                "eclipse.preferences.version=1\n" 
    198                                 + "metamodel.location=" + metaModelLocation,  
     217                                + "metamodel.location=" + "platform:/resource/" + p.getName() + "/models/metamodel.xmi",  
    199218                                new NullProgressMonitor()); 
    200219                monitor.worked(1); 
     220                 
    201221        } 
    202222 
     
    215235        } 
    216236 
    217         private InputStream getContentStream(final String resource) { 
     237        private String getContents(final InputStream stream) { 
     238                try { 
     239                        final InputStream inputStream = stream; 
     240 
     241                        final byte[] buffer = new byte[4096]; 
     242                        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
     243 
     244                        while (true) { 
     245                                int read; 
     246                                read = inputStream.read(buffer); 
     247 
     248                                if (read == -1) { 
     249                                        break; 
     250                                } 
     251 
     252                                outputStream.write(buffer, 0, read); 
     253                        } 
     254 
     255                        outputStream.close(); 
     256                        inputStream.close(); 
     257 
     258                        return outputStream.toString("UTF-8"); 
     259                } 
     260                catch (final IOException e) { 
     261                        log.error("Could not load resource", e); 
     262                        return ""; 
     263                } 
     264        } 
     265         
     266        private URL findUIResource(String resource) { 
    218267                Bundle b = Platform.getBundle("no.hib.dpf.codegen.xpand.ui"); 
    219268                Enumeration<URL> ee = b.findEntries("resources", resource, false); 
     
    224273                        fileURL = ee.nextElement(); 
    225274                } 
    226                 try { 
    227                         return fileURL.openStream(); 
     275                return fileURL; 
     276        } 
     277         
     278        private String getContentsOfTemplateResource(final String resource) { 
     279                try { 
     280                        return getContents(findUIResource(resource).openStream()); 
     281                } catch (IOException e) { 
     282                        log.error("Could not load local template resource", e); 
     283                        return ""; 
     284                } 
     285        } 
     286         
     287        private InputStream getUIContentStream(final String resource) { 
     288                try { 
     289                        return findUIResource(resource).openStream(); 
    228290                } catch (IOException e) { 
    229291                        e.printStackTrace(); 
     
    231293                } 
    232294        } 
    233          
    234         private String getContents(final String resource) { 
    235                 Bundle b = Platform.getBundle("no.hib.dpf.codegen.xpand.ui"); 
    236                 Enumeration<URL> ee = b.findEntries("resources", resource, false); 
    237                  
    238                 URL fileURL = null; 
    239                  
    240                 if(ee.hasMoreElements()) { 
    241                         fileURL = ee.nextElement(); 
    242                 } 
    243                  
    244                 try { 
    245                         final InputStream inputStream = fileURL.openStream(); 
    246  
    247                         final byte[] buffer = new byte[4096]; 
    248                         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    249  
    250                         while (true) { 
    251                                 int read; 
    252                                 read = inputStream.read(buffer); 
    253  
    254                                 if (read == -1) { 
    255                                         break; 
    256                                 } 
    257  
    258                                 outputStream.write(buffer, 0, read); 
    259                         } 
    260  
    261                         outputStream.close(); 
    262                         inputStream.close(); 
    263  
    264                         return outputStream.toString("UTF-8"); 
    265                 } 
    266                 catch (final IOException e) { 
    267                         e.printStackTrace(); 
    268                         return ""; 
    269                 } 
    270         } 
    271          
    272295         
    273296        /**