- Timestamp:
- 02/05/06 12:39:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/org.modelica.mdt.ui/src/org/modelica/mdt/ui/wizards/NewClassWizard.java
r288 r290 54 54 import org.eclipse.core.resources.ResourcesPlugin; 55 55 import org.eclipse.core.runtime.CoreException; 56 import org.eclipse.core.runtime.IPath;57 56 import org.eclipse.core.runtime.IProgressMonitor; 58 57 import org.eclipse.core.runtime.IStatus; 59 58 import org.eclipse.core.runtime.Path; 60 59 import org.eclipse.core.runtime.Status; 61 import org.eclipse.jface.dialogs.DialogPage;62 60 import org.eclipse.jface.dialogs.ErrorDialog; 63 61 import org.eclipse.jface.dialogs.MessageDialog; … … 65 63 import org.eclipse.jface.viewers.IStructuredSelection; 66 64 import org.eclipse.jface.wizard.Wizard; 67 import org.eclipse.jface.wizard.WizardPage;68 65 import org.eclipse.swt.SWT; 69 66 import org.eclipse.swt.events.ModifyEvent; 70 67 import org.eclipse.swt.events.ModifyListener; 71 import org.eclipse.swt.events.SelectionAdapter;72 68 import org.eclipse.swt.events.SelectionEvent; 73 69 import org.eclipse.swt.events.SelectionListener; 74 70 import org.eclipse.swt.layout.GridData; 75 import org.eclipse.swt.layout.GridLayout;76 71 import org.eclipse.swt.widgets.Combo; 77 72 import org.eclipse.swt.widgets.Composite; … … 84 79 import org.eclipse.ui.PartInitException; 85 80 import org.eclipse.ui.PlatformUI; 86 import org.eclipse.ui.dialogs.ContainerSelectionDialog;87 81 import org.eclipse.ui.ide.IDE; 88 82 import org.modelica.mdt.core.ModelicaCore; … … 94 88 { 95 89 /* key widgets' tags for abbot */ 96 public static final String SOURCE_FOLDER_TAG = "sourceFolderTag";97 90 public static final String CLASS_NAME_TAG = "classNameTag"; 98 91 public static final String CLASS_TYPE_TAG = "classTypeTag"; … … 110 103 public static int lastClassTypeSelected = 0; 111 104 112 public class NewClassPage extends WizardPage 113 { 114 private Text sourceFolder; 105 public class NewClassPage extends NewTypePage 106 { 115 107 private Text className; 116 108 private Combo classType; … … 119 111 private Button externalBody; 120 112 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 130 115 public void createControl(Composite parent) 131 116 { 117 super.createControl(parent); 132 118 setPageComplete(false); 119 120 Composite composite = getControl(); 133 121 /* 134 122 * configure description of this page … … 143 131 * setup widgets for this page 144 132 */ 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 153 133 GridData gd; 154 155 /* source folderfield */134 135 /* class name field */ 156 136 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);194 137 l.setText("Name:"); 195 138 gd = new GridData(); … … 209 152 public void modifyText(ModifyEvent e) 210 153 { 211 if ( !ModelicaCore.isLegalIdentifierName(className.getText()))154 if (ModelicaCore.isLegalIdentifierName(className.getText())) 212 155 { 213 updateStatus("Class name is not valid. Illegal identifier.", 214 DialogPage.WARNING); 156 classNameValid = true; 157 setErrorMessage(null); 158 setPageComplete(isPageComplete()); 215 159 } 216 160 else 217 161 { 218 updateStatus(null, DialogPage.NONE); 162 classNameValid = false; 163 setErrorMessage 164 ("Class name is not valid. Illegal identifier."); 165 setPageComplete(isPageComplete()); 219 166 } 220 167 } … … 334 281 externalBody.setEnabled 335 282 (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; 420 289 } 421 290 … … 458 327 public boolean performFinish() 459 328 { 460 final String sourceFolder = classPage.sourceFolder.getText(); 329 final String sourceFolder = classPage.getSourceFolder(); 330 final String parentPackage = classPage.getParentPackge(); 461 331 final String className = classPage.className.getText(); 462 332 final String classType = classPage.classType.getText(); … … 475 345 try 476 346 { 477 doFinish(sourceFolder, className, classType,347 doFinish(sourceFolder, parentPackage, className, classType, 478 348 initialEquationBlock, partialClass, externalBody, 479 349 monitor); … … 507 377 } 508 378 509 protected void doFinish(String sourceFolder, String className,510 String class Type, boolean initialEquationBlock,379 protected void doFinish(String sourceFolder, String parentPackage, 380 String className, String classType, boolean initialEquationBlock, 511 381 boolean partialClass, boolean haveExternalBody, 512 382 IProgressMonitor monitor) … … 524 394 try 525 395 { 526 String contents = generateClassContents(className, classType, 527 initialEquationBlock, partialClass, haveExternalBody); 396 String contents = generateClassContents(className, parentPackage, 397 classType, initialEquationBlock, partialClass, 398 haveExternalBody); 528 399 InputStream stream = 529 400 new ByteArrayInputStream(contents.getBytes()); … … 569 440 } 570 441 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) 573 445 { 574 446 /* … … 576 448 */ 577 449 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 } 578 456 579 457 if (canBePartial(classType) && partialClass) … … 620 498 public void init(IWorkbench workbench, IStructuredSelection selection) 621 499 { 622 classPage. setSelection(selection);500 classPage.init(selection); 623 501 setWindowTitle("New Modelica Class"); 624 502 }
Note: See TracChangeset
for help on using the changeset viewer.