Changeset 19 for trunk/src/org/modelica
- Timestamp:
- 09/21/05 14:55:29 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/modelica/mdt/NewClassWizard.java
r18 r19 2 2 3 3 4 import java.io.ByteArrayInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.lang.reflect.InvocationTargetException; 8 import java.util.regex.Pattern; 9 10 import org.eclipse.core.resources.IContainer; 11 import org.eclipse.core.resources.IFile; 12 import org.eclipse.core.resources.IResource; 13 import org.eclipse.core.resources.IWorkspaceRoot; 14 import org.eclipse.core.resources.ResourcesPlugin; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.jface.dialogs.DialogPage; 20 import org.eclipse.jface.dialogs.MessageDialog; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 4 22 import org.eclipse.jface.viewers.IStructuredSelection; 5 23 import org.eclipse.jface.wizard.Wizard; 6 24 import org.eclipse.jface.wizard.WizardPage; 7 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.ModifyEvent; 27 import org.eclipse.swt.events.ModifyListener; 28 import org.eclipse.swt.events.SelectionAdapter; 29 import org.eclipse.swt.events.SelectionEvent; 8 30 import org.eclipse.swt.layout.GridData; 9 31 import org.eclipse.swt.layout.GridLayout; … … 15 37 import org.eclipse.ui.INewWizard; 16 38 import org.eclipse.ui.IWorkbench; 39 import org.eclipse.ui.IWorkbenchPage; 40 import org.eclipse.ui.PartInitException; 41 import org.eclipse.ui.PlatformUI; 42 import org.eclipse.ui.dialogs.ContainerSelectionDialog; 43 import org.eclipse.ui.ide.IDE; 17 44 18 45 public class NewClassWizard extends Wizard implements INewWizard … … 20 47 public class NewClassPage extends WizardPage 21 48 { 49 private Text sourceFolder; 50 private Text className; 51 private Combo classType; 52 53 54 private IPath selection = null; 55 56 57 /* regexp pattern of a valid modelica class name */ 58 //TODO add complete regexp for modelcia class name 59 // see modelica specification page 9 (and perhaps some other pages as well) 60 // http://www.modelica.org/documents/ModelicaSpec22.pdf 61 Pattern classNamePattern = Pattern.compile("[a-zA-Z]\\w*"); 62 63 private boolean classNameValid = false; 64 private boolean sourceFolderValid = false; 65 22 66 public NewClassPage() 23 67 { 24 super(" wee");68 super(""); 25 69 } 26 70 27 71 public void createControl(Composite parent) 28 72 { 29 initializeDialogUnits(parent); 30 73 setPageComplete(false); 31 74 /* 32 75 * configure description of this page … … 51 94 GridData gd; 52 95 53 /* source folder fi led */96 /* source folder field */ 54 97 Label l = new Label(composite, SWT.LEFT | SWT.WRAP); 55 98 l.setText("Source folder:"); … … 58 101 l.setLayoutData(gd); 59 102 60 Text t= new Text(composite, SWT.SINGLE | SWT.BORDER);103 sourceFolder = new Text(composite, SWT.SINGLE | SWT.BORDER); 61 104 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 62 t.setLayoutData(gd); 105 sourceFolder.setLayoutData(gd); 106 setSourceFolder(selection); 107 108 sourceFolder.addModifyListener(new ModifyListener() 109 { 110 /* check if entered path exists */ 111 public void modifyText(ModifyEvent e) 112 { 113 sourceFolderChanged(); 114 } 115 }); 116 63 117 64 118 Button b = new Button(composite, SWT.PUSH); … … 67 121 gd.horizontalAlignment = GridData.END; 68 122 b.setLayoutData(gd); 123 b.addSelectionListener(new SelectionAdapter() 124 { 125 public void widgetSelected(SelectionEvent e) 126 { 127 handleBrowse(); 128 } 129 }); 130 69 131 70 132 /* class name field */ … … 75 137 l.setLayoutData(gd); 76 138 77 t= new Text(composite, SWT.SINGLE | SWT.BORDER);139 className = new Text(composite, SWT.SINGLE | SWT.BORDER); 78 140 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 79 141 gd.horizontalSpan = 2; 80 t.setLayoutData(gd); 81 82 /* superclass field */ 142 className.setLayoutData(gd); 143 144 className.addModifyListener(new ModifyListener() 145 { 146 /* check if entered classname is valid */ 147 public void modifyText(ModifyEvent e) 148 { 149 if (!isLegalClassName(className.getText())) 150 { 151 classNameValid = false; 152 updateStatus("Class name is not valid. Illegal identifier.", 153 DialogPage.WARNING); 154 } 155 else 156 { 157 classNameValid = true; 158 updateStatus(null, DialogPage.NONE); 159 } 160 } 161 }); 162 className.setFocus(); 163 164 /* class type field */ 83 165 l = new Label(composite, SWT.LEFT | SWT.WRAP); 84 l.setText(" Superclass:");166 l.setText("Type:"); 85 167 gd = new GridData(); 86 168 gd.horizontalAlignment = GridData.BEGINNING; 87 169 l.setLayoutData(gd); 88 170 89 t = new Text(composite, SWT.SINGLE | SWT.BORDER); 90 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 91 gd.horizontalSpan = 2; 92 t.setLayoutData(gd); 93 94 /* type field */ 95 l = new Label(composite, SWT.LEFT | SWT.WRAP); 96 l.setText("Type:"); 171 classType = new Combo(composite, SWT.READ_ONLY); 172 classType.setItems(new String [] {"class", "model", "connector", "record", 173 "block", "type", "function"}); 174 classType.setVisibleItemCount(7); 175 classType.select(0); 176 97 177 gd = new GridData(); 98 178 gd.horizontalAlignment = GridData.BEGINNING; 99 l.setLayoutData(gd); 100 101 Combo c = new Combo(composite, SWT.READ_ONLY); 102 c.setItems(new String [] {"class", "model", "connector", "record", 103 "block", "type", "function"}); 104 c.setVisibleItemCount(7); 105 c.select(0); 106 107 gd = new GridData(); 108 gd.horizontalAlignment = GridData.BEGINNING; 109 c.setLayoutData(gd); 110 111 } 112 113 } 114 115 private IStructuredSelection selection; 116 179 classType.setLayoutData(gd); 180 181 } 182 183 private void sourceFolderChanged() 184 { 185 IResource container = ResourcesPlugin.getWorkspace().getRoot() 186 .findMember(new Path(sourceFolder.getText())); 187 188 sourceFolderValid = false; 189 if (sourceFolder.getText().length() == 0) { 190 updateStatus("File container must be specified", DialogPage.ERROR); 191 return; 192 } 193 if (container == null 194 || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) { 195 updateStatus("File container must exist", DialogPage.ERROR); 196 return; 197 } 198 if (!container.isAccessible()) { 199 updateStatus("Project must be writable", DialogPage.ERROR); 200 return; 201 } 202 sourceFolderValid = true; 203 updateStatus(null, DialogPage.NONE); 204 } 205 206 /** 207 * @return true if name is a valid modelica class name 208 */ 209 protected boolean isLegalClassName(String name) 210 { 211 return classNamePattern.matcher(name).matches(); 212 } 213 214 public void init(IStructuredSelection selection) 215 { 216 if (selection == null || selection.size() != 1) 217 { 218 return; 219 } 220 if (!(selection.getFirstElement() instanceof IResource)) 221 { 222 return; 223 } 224 225 /* 226 * now we know that a single element of type IResource is selected 227 */ 228 IResource sel = (IResource) selection.getFirstElement(); 229 230 this.selection = sel.getFullPath(); 231 if (sel.getType() == IResource.FILE) 232 { 233 this.selection = this.selection.removeLastSegments(1); 234 } 235 } 236 237 private void handleBrowse() 238 { 239 ContainerSelectionDialog dialog = 240 new ContainerSelectionDialog( 241 getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, 242 "Choose a source folder:"); 243 244 if (dialog.open() == ContainerSelectionDialog.OK) 245 { 246 Object[] result = dialog.getResult(); 247 if (result.length == 1) 248 { 249 setSourceFolder((IPath) result[0]); 250 } 251 } 252 } 253 254 private void updateStatus(String message, int messageType) 255 { 256 setMessage(message, messageType); 257 setPageComplete((classNameValid && sourceFolderValid)); 258 } 259 260 261 public void setSourceFolder(IPath path) 262 { 263 if (path != null) 264 { 265 sourceFolder.setText(path.makeRelative().toString()); 266 setPageComplete(isPageComplete()); 267 sourceFolderValid = true; 268 } 269 } 270 271 } 272 273 274 private NewClassPage classPage = new NewClassPage(); 275 117 276 public NewClassWizard() 118 277 { 119 278 super(); 120 // TODO Auto-generated constructor stub121 279 } 122 280 … … 124 282 public boolean performFinish() 125 283 { 126 // TODO Auto-generated method stub 284 final String sourceFolder = classPage.sourceFolder.getText(); 285 final String className = classPage.className.getText(); 286 final String classType = classPage.classType.getText(); 287 288 IRunnableWithProgress op = new IRunnableWithProgress() { 289 public void run(IProgressMonitor monitor) 290 throws InvocationTargetException 291 { 292 try 293 { 294 doFinish(sourceFolder, className, classType, monitor); 295 } 296 catch (CoreException e) 297 { 298 throw new InvocationTargetException(e); 299 } 300 finally 301 { 302 monitor.done(); 303 } 304 } 305 }; 306 try { 307 getContainer().run(true, false, op); 308 } catch (InterruptedException e) { 309 return false; 310 } catch (InvocationTargetException e) { 311 Throwable realException = e.getTargetException(); 312 MessageDialog.openError(getShell(), "Error", realException.getMessage()); 313 return false; 314 } 127 315 return true; 128 316 } 129 317 318 protected void doFinish(String sourceFolder, String className, 319 String classType, IProgressMonitor monitor) 320 throws CoreException 321 { 322 // create a sample file 323 monitor.beginTask("Creating " + className, 2); 324 325 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 326 IResource resource = root.findMember(new Path(sourceFolder)); 327 328 IContainer container = (IContainer) resource; 329 330 final IFile file = container.getFile(new Path(className+".mo")); 331 String contents = 332 classType + " " + className + "\n\n" + 333 (classType.equals("record") ? "" : "equation\n\n" ) + 334 "end " + className + ";"; 335 336 try { 337 InputStream stream = new ByteArrayInputStream(contents.getBytes()); 338 if (file.exists()) { 339 file.appendContents(stream, true, true, monitor); 340 } else { 341 file.create(stream, true, monitor); 342 } 343 stream.close(); 344 } catch (IOException e) { 345 } 346 monitor.worked(1); 347 monitor.setTaskName("Opening file for editing..."); 348 getShell().getDisplay().asyncExec(new Runnable() { 349 public void run() { 350 IWorkbenchPage page = 351 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 352 try { 353 IDE.openEditor(page, file, true); 354 } catch (PartInitException e) { 355 } 356 } 357 }); 358 monitor.worked(1); 359 360 } 361 130 362 public void init(IWorkbench workbench, IStructuredSelection selection) 131 363 { 132 this.selection = selection;364 classPage.init(selection); 133 365 setWindowTitle("New Modelica Class"); 134 366 } … … 137 369 public void addPages() 138 370 { 139 NewClassPage page = new NewClassPage(); 140 addPage(page); 371 addPage(classPage); 141 372 } 142 373 }
Note: See TracChangeset
for help on using the changeset viewer.