Ignore:
Timestamp:
02/05/06 12:39:00 (19 years ago)
Author:
boris
Message:
  • added a parent package field to new modelica class wizard
File:
1 edited

Legend:

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

    r288 r290  
    5454import org.eclipse.core.resources.ResourcesPlugin;
    5555import org.eclipse.core.runtime.CoreException;
    56 import org.eclipse.core.runtime.IPath;
    5756import org.eclipse.core.runtime.IProgressMonitor;
    5857import org.eclipse.core.runtime.IStatus;
    5958import org.eclipse.core.runtime.Path;
    6059import org.eclipse.core.runtime.Status;
    61 import org.eclipse.jface.dialogs.DialogPage;
    6260import org.eclipse.jface.dialogs.ErrorDialog;
    6361import org.eclipse.jface.dialogs.MessageDialog;
     
    6563import org.eclipse.jface.viewers.IStructuredSelection;
    6664import org.eclipse.jface.wizard.Wizard;
    67 import org.eclipse.jface.wizard.WizardPage;
    6865import org.eclipse.swt.SWT;
    6966import org.eclipse.swt.events.ModifyEvent;
    7067import org.eclipse.swt.events.ModifyListener;
    71 import org.eclipse.swt.events.SelectionAdapter;
    7268import org.eclipse.swt.events.SelectionEvent;
    7369import org.eclipse.swt.events.SelectionListener;
    7470import org.eclipse.swt.layout.GridData;
    75 import org.eclipse.swt.layout.GridLayout;
    7671import org.eclipse.swt.widgets.Combo;
    7772import org.eclipse.swt.widgets.Composite;
     
    8479import org.eclipse.ui.PartInitException;
    8580import org.eclipse.ui.PlatformUI;
    86 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
    8781import org.eclipse.ui.ide.IDE;
    8882import org.modelica.mdt.core.ModelicaCore;
     
    9488{
    9589    /* key widgets' tags for abbot */
    96     public static final String SOURCE_FOLDER_TAG = "sourceFolderTag";
    9790    public static final String CLASS_NAME_TAG = "classNameTag";
    9891    public static final String CLASS_TYPE_TAG = "classTypeTag";
     
    110103    public static int lastClassTypeSelected = 0;
    111104   
    112     public class NewClassPage extends WizardPage
    113     {
    114         private Text sourceFolder;
     105    public class NewClassPage extends NewTypePage
     106    {
    115107        private Text className;
    116108        private Combo classType;
     
    119111        private Button externalBody;
    120112   
    121         private IPath selection = null;
    122                
    123         private boolean sourceFolderValid = false;     
    124        
    125         public NewClassPage()
    126         {
    127             super("");
    128         }
    129 
     113        private boolean classNameValid = false;
     114       
    130115        public void createControl(Composite parent)
    131116        {
     117            super.createControl(parent);
    132118            setPageComplete(false);
     119           
     120            Composite composite = getControl();         
    133121            /*
    134122             * configure description of this page
     
    143131             * setup widgets for this page
    144132             */
    145             Composite composite= new Composite(parent, SWT.NONE);
    146             setControl(composite);
    147             composite.setFont(parent.getFont());
    148            
    149             GridLayout layout = new GridLayout();
    150             layout.numColumns = 3;
    151             composite.setLayout(layout);
    152            
    153133            GridData gd;
    154 
    155             /* source folder field */
     134           
     135            /* class name field */
    156136            Label l = new Label(composite, SWT.LEFT | SWT.WRAP);
    157             l.setText("Source folder:");
    158             gd = new GridData();
    159             gd.horizontalAlignment = GridData.BEGINNING;
    160             l.setLayoutData(gd);
    161            
    162             sourceFolder = new Text(composite, SWT.SINGLE | SWT.BORDER);
    163             gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
    164             sourceFolder.setLayoutData(gd);
    165             UIPlugin.tag(sourceFolder, SOURCE_FOLDER_TAG);
    166             setSourceFolder(selection);
    167            
    168             sourceFolder.addModifyListener(new ModifyListener()
    169             {
    170                     /* check if entered path exists */
    171                     public void modifyText(ModifyEvent e)
    172                     {
    173                         sourceFolderChanged();
    174                     }
    175             });
    176 
    177 
    178             Button b = new Button(composite, SWT.PUSH);
    179             b.setText("Browse...");
    180             gd = new GridData();
    181             gd.horizontalAlignment = GridData.END;
    182             b.setLayoutData(gd);
    183             b.addSelectionListener(new SelectionAdapter()
    184             {
    185                 public void widgetSelected(SelectionEvent e)
    186                 {
    187                     handleBrowse();
    188                 }
    189             });
    190 
    191            
    192             /* class name field */
    193             l = new Label(composite, SWT.LEFT | SWT.WRAP);
    194137            l.setText("Name:");
    195138            gd = new GridData();
     
    209152                public void modifyText(ModifyEvent e)
    210153                {
    211                     if (!ModelicaCore.isLegalIdentifierName(className.getText()))
     154                    if (ModelicaCore.isLegalIdentifierName(className.getText()))
    212155                    {
    213                         updateStatus("Class name is not valid. Illegal identifier.",
    214                                     DialogPage.WARNING);
     156                        classNameValid  = true;
     157                        setErrorMessage(null);
     158                        setPageComplete(isPageComplete());
    215159                    }
    216160                    else
    217161                    {
    218                         updateStatus(null, DialogPage.NONE);
     162                        classNameValid  = false;
     163                        setErrorMessage
     164                            ("Class name is not valid. Illegal identifier.");
     165                        setPageComplete(isPageComplete());
    219166                    }
    220167                }
     
    334281            externalBody.setEnabled
    335282                (classType.getText().equals("function"));
    336 
    337         }
    338        
    339         private void sourceFolderChanged()
    340         {
    341             IResource container = ResourcesPlugin.getWorkspace().getRoot()
    342                     .findMember(new Path(sourceFolder.getText()));
    343 
    344             sourceFolderValid = false;
    345             if (sourceFolder.getText().length() == 0) {
    346                 updateStatus("Folder container must be specified",
    347                         DialogPage.ERROR);
    348                 return;
    349             }
    350             if (container == null ||
    351                 (container.getType() &
    352                   (IResource.PROJECT | IResource.FOLDER)) == 0)
    353             {
    354                 updateStatus("Folder container must exist", DialogPage.ERROR);
    355                 return;
    356             }
    357             if (!container.isAccessible()) {
    358                 updateStatus("Project must be writable", DialogPage.ERROR);
    359                 return;
    360             }
    361             sourceFolderValid = true;
    362             updateStatus(null, DialogPage.NONE);
    363         }
    364        
    365         public void setSelection(IStructuredSelection selection)
    366         {
    367             if (selection == null || selection.size() != 1)
    368             {
    369                 return;
    370             }
    371             if (!(selection.getFirstElement() instanceof IResource))
    372             {
    373                 return;
    374             }
    375            
    376             /*
    377              * now we know that a single element of type IResource is selected
    378              */
    379             IResource sel = (IResource) selection.getFirstElement();
    380 
    381             this.selection = sel.getFullPath();
    382             if (sel.getType() == IResource.FILE)
    383             {
    384                 this.selection = this.selection.removeLastSegments(1);
    385             }
    386         }
    387        
    388         private void handleBrowse()
    389         {
    390             ContainerSelectionDialog dialog =
    391                 new ContainerSelectionDialog(
    392                     getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
    393                     "Choose a source folder:");
    394            
    395             if (dialog.open() == ContainerSelectionDialog.OK)
    396             {
    397                 Object[] result = dialog.getResult();
    398                 if (result.length == 1)
    399                 {
    400                     setSourceFolder((IPath) result[0]);
    401                 }
    402             }
    403         }
    404        
    405         private void updateStatus(String message, int messageType)
    406         {
    407             setMessage(message, messageType);
    408             setPageComplete((sourceFolderValid));
    409         }
    410 
    411 
    412         public void setSourceFolder(IPath path)
    413         {
    414             if (path != null)
    415             {               
    416                 sourceFolder.setText(path.makeRelative().toString());
    417                 setPageComplete(isPageComplete());
    418                 sourceFolderValid = true;
    419             }                       
     283        }
     284
     285        @Override
     286        public boolean isPageComplete()
     287        {
     288            return super.isPageComplete() && classNameValid;
    420289        }
    421290
     
    458327    public boolean performFinish()
    459328    {
    460         final String sourceFolder = classPage.sourceFolder.getText();
     329        final String sourceFolder = classPage.getSourceFolder();
     330        final String parentPackage = classPage.getParentPackge();
    461331        final String className = classPage.className.getText();
    462332        final String classType = classPage.classType.getText();
     
    475345                try
    476346                {
    477                     doFinish(sourceFolder, className, classType,
     347                    doFinish(sourceFolder, parentPackage, className, classType,
    478348                             initialEquationBlock, partialClass, externalBody,
    479349                             monitor);
     
    507377    }
    508378
    509     protected void doFinish(String sourceFolder, String className,
    510             String classType, boolean initialEquationBlock,
     379    protected void doFinish(String sourceFolder, String parentPackage,
     380            String className, String classType, boolean initialEquationBlock,
    511381            boolean partialClass, boolean haveExternalBody,
    512382            IProgressMonitor monitor)
     
    524394        try
    525395        {
    526             String contents = generateClassContents(className, classType,
    527                     initialEquationBlock, partialClass, haveExternalBody);
     396            String contents = generateClassContents(className, parentPackage,
     397                    classType, initialEquationBlock, partialClass,
     398                    haveExternalBody);
    528399            InputStream stream =
    529400                new ByteArrayInputStream(contents.getBytes());
     
    569440    }
    570441
    571     private String generateClassContents(String className, String classType,
    572             boolean initialEquationBlock, boolean partialClass, boolean haveExternalBody)
     442    private String generateClassContents(String className, String parentPackage,
     443            String classType, boolean initialEquationBlock,
     444            boolean partialClass, boolean haveExternalBody)
    573445    {
    574446        /*
     
    576448         */
    577449        String contents = "";
     450       
     451        if (!parentPackage.equals(""))
     452        {
     453            /* generate within clause if we are not top level package */
     454            contents += "within " + parentPackage + ";\n\n";
     455        }
    578456
    579457        if (canBePartial(classType) && partialClass)
     
    620498    public void init(IWorkbench workbench, IStructuredSelection selection)
    621499    {
    622         classPage.setSelection(selection);
     500        classPage.init(selection);
    623501        setWindowTitle("New Modelica Class");
    624502    }
Note: See TracChangeset for help on using the changeset viewer.