Ignore:
Timestamp:
09/27/05 16:00:50 (19 years ago)
Author:
boris
Message:
  • added some more modifiers checkboxes to new class dialog
  • reworked somewhat the structure of the code that generates the class file
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/org.modelica.mdt/src/org/modelica/mdt/NewClassWizard.java

    r35 r36  
    4949public class NewClassWizard extends Wizard implements INewWizard
    5050{
     51   
    5152    public class NewClassPage extends WizardPage
    5253    {
     
    5556        private Combo classType;
    5657        private Button initialEquation;
     58        private Button partialClass;
     59        private Button externalBody;
    5760   
    5861        private IPath selection = null;
     
    188191                public void widgetSelected(SelectionEvent e)
    189192                {
     193                    /*
     194                     * update state of the modifiers checkboxes
     195                     */
    190196                    initialEquation.setEnabled
    191                     (NewClassWizard.classTypeHaveEquations(classType.getText()));                   
     197                    (NewClassWizard.classTypeHaveEquations(classType.getText()));
     198                   
     199                    partialClass.setEnabled
     200                    (NewClassWizard.canBePartial(classType.getText()));
     201                   
     202                    externalBody.setEnabled(classType.getText().equals("function"));
     203
    192204                }
    193205
     
    197209            });
    198210           
    199            
    200211            /* fill upp 'empty' space */
    201212            new Label(composite, SWT.NONE);
    202             new Label(composite, SWT.NONE);
     213
     214           
     215            /* create separator */
     216            l = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
     217            gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
     218            gd.horizontalSpan = 3;
     219            l.setLayoutData(gd);
     220           
     221           
     222            /* modifiers section */
     223            l = new Label(composite, SWT.NONE);
     224            l.setText("Modifiers:");
    203225           
    204226            /* initial equation block */
     
    208230            gd = new GridData();
    209231            gd.horizontalAlignment = GridData.BEGINNING;
     232            gd.horizontalSpan = 2;
    210233            initialEquation.setLayoutData(gd);
     234           
     235            /* partial class */
     236            new Label(composite, SWT.NONE); /* empty label for padding */
     237            partialClass = new Button(composite, SWT.CHECK);
     238            partialClass.setText("is partial class");
     239           
     240            gd = new GridData();
     241            gd.horizontalAlignment = GridData.BEGINNING;
     242            gd.horizontalSpan = 2;
     243            partialClass.setLayoutData(gd);
     244
     245            /* external body */
     246            new Label(composite, SWT.NONE); /* empty label for padding */
     247            externalBody = new Button(composite, SWT.CHECK);
     248            externalBody.setText("have external body");
     249            externalBody.setEnabled(false);
     250           
     251            gd = new GridData();
     252            gd.horizontalAlignment = GridData.BEGINNING;
     253            gd.horizontalSpan = 2;
     254            externalBody.setLayoutData(gd);
     255           
    211256
    212257        }
     
    311356
    312357    /**
    313      * @param classType
     358     * @param classType name of the class type, e.g. 'record'
    314359     * @return false if class type can't have equations, e.g.
    315      * false is returned for record and connector
     360     * false is returned for record
    316361     */
    317362    public static boolean classTypeHaveEquations(String classType)
    318363    {
    319364        return 
    320         !(classType.equals("record") || classType.equals("connector"));
    321     }
     365        !(classType.equals("record")
     366                || classType.equals("type")
     367                || classType.equals("connector")
     368                || classType.equals("function"));
     369    }
     370   
     371    /**
     372     * @param classType name of the class type, e.g. 'record'
     373     * @return false if class type can't have equations, e.g.
     374     * false is returned for type
     375     */
     376    public static boolean canBePartial(String classType)
     377    {
     378        return 
     379        (!(classType.equals("type")
     380                || classType.equals("function")));
     381    }
     382
    322383   
    323384    @Override
     
    329390        final boolean initialEquationBlock =
    330391            classPage.initialEquation.getSelection();
     392        final boolean partialClass =
     393            classPage.partialClass.getSelection();
     394        final boolean externalBody =
     395            classPage.externalBody.getSelection();
     396
    331397       
    332398        IRunnableWithProgress op = new IRunnableWithProgress() {
     
    337403                {
    338404                    doFinish(sourceFolder, className, classType,
    339                             initialEquationBlock, monitor);
     405                            initialEquationBlock, partialClass, externalBody, monitor);
    340406                }
    341407                catch (CoreException e)
     
    367433
    368434    protected void doFinish(String sourceFolder, String className,
    369             String classType, boolean initialEquationBlock, IProgressMonitor monitor)
     435            String classType, boolean initialEquationBlock, boolean partialClass,
     436            boolean haveExternalBody, IProgressMonitor monitor)
    370437    throws CoreException
    371438    {
     
    380447        final IFile file = container.getFile(new Path(className+".mo"));
    381448       
    382         boolean haveEquationBlock =
    383             classTypeHaveEquations(classType);
    384        
    385         String contents =
    386             classType + " " + className + "\n\n" +
    387             ( haveEquationBlock ? "equation\n\n" : ""  ) +
    388             ( (haveEquationBlock && initialEquationBlock)
    389                     ? "initial equation\n\n" : "" ) +
    390             "end " + className + ";";
    391        
    392    
    393449        try
    394450        {
    395             InputStream stream = new ByteArrayInputStream(contents.getBytes());
     451            String contents = generateClassContentes(className, classType,
     452                    initialEquationBlock, partialClass, haveExternalBody);
     453            InputStream stream =
     454                new ByteArrayInputStream(contents.getBytes());
    396455            if (file.exists())
    397456            {
     
    433492    }
    434493
     494    private String generateClassContentes(String className, String classType,
     495            boolean initialEquationBlock, boolean partialClass, boolean haveExternalBody)
     496    {
     497        /*
     498         * the gory logic of generating a skeleton for a modelica class
     499         */
     500        String contents = "";
     501
     502        if (canBePartial(classType) && partialClass)
     503        {
     504            contents += "partial ";
     505        }
     506       
     507        contents += classType + " " + className;
     508       
     509        if (classTypeHaveEquations(classType))
     510        {
     511            contents += "\n\nequation\n";
     512            if (initialEquationBlock)
     513            {
     514                contents += "\ninitial equation\n";
     515            }
     516        }
     517       
     518        if (classType.equals("function"))
     519        {
     520            if (haveExternalBody)
     521            {
     522                contents += "\nexternal";
     523            }
     524            else
     525            {   
     526                contents += "\nalgorithm\n";
     527            }
     528        }
     529
     530        if (!classType.equals("type"))
     531        {
     532            contents += "\nend " + className;
     533        }
     534
     535        contents += ";";
     536        return contents ;
     537    }
     538
    435539    public void init(IWorkbench workbench, IStructuredSelection selection)
    436540    {
Note: See TracChangeset for help on using the changeset viewer.