1 11 package org.eclipse.pde.internal.ui.editor.schema; 12 import java.util.Vector ; 13 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.jdt.core.IJavaElement; 17 import org.eclipse.jdt.core.IJavaProject; 18 import org.eclipse.jdt.core.IType; 19 import org.eclipse.jdt.core.JavaCore; 20 import org.eclipse.jdt.core.search.SearchEngine; 21 import org.eclipse.jdt.ui.IJavaElementSearchConstants; 22 import org.eclipse.jdt.ui.JavaUI; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.ISelectionChangedListener; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.LabelProvider; 27 import org.eclipse.jface.viewers.SelectionChangedEvent; 28 import org.eclipse.jface.viewers.TableViewer; 29 import org.eclipse.jface.window.Window; 30 import org.eclipse.jface.wizard.WizardDialog; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.pde.core.IModelChangedEvent; 33 import org.eclipse.pde.internal.core.ischema.IMetaAttribute; 34 import org.eclipse.pde.internal.core.ischema.ISchemaObject; 35 import org.eclipse.pde.internal.core.ischema.ISchemaRestriction; 36 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType; 37 import org.eclipse.pde.internal.core.schema.ChoiceRestriction; 38 import org.eclipse.pde.internal.core.schema.SchemaAttribute; 39 import org.eclipse.pde.internal.core.schema.SchemaEnumeration; 40 import org.eclipse.pde.internal.core.schema.SchemaSimpleType; 41 import org.eclipse.pde.internal.ui.PDEPlugin; 42 import org.eclipse.pde.internal.ui.PDEUIMessages; 43 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 44 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 45 import org.eclipse.pde.internal.ui.parts.ComboPart; 46 import org.eclipse.pde.internal.ui.parts.FormEntry; 47 import org.eclipse.pde.internal.ui.util.SWTUtil; 48 import org.eclipse.swt.SWT; 49 import org.eclipse.swt.custom.StackLayout; 50 import org.eclipse.swt.events.SelectionAdapter; 51 import org.eclipse.swt.events.SelectionEvent; 52 import org.eclipse.swt.graphics.Color; 53 import org.eclipse.swt.layout.GridData; 54 import org.eclipse.swt.layout.GridLayout; 55 import org.eclipse.swt.widgets.Button; 56 import org.eclipse.swt.widgets.Composite; 57 import org.eclipse.swt.widgets.Control; 58 import org.eclipse.swt.widgets.Label; 59 import org.eclipse.swt.widgets.Table; 60 import org.eclipse.ui.IActionBars; 61 import org.eclipse.ui.PartInitException; 62 import org.eclipse.ui.PlatformUI; 63 import org.eclipse.ui.dialogs.SelectionDialog; 64 import org.eclipse.ui.forms.IFormColors; 65 import org.eclipse.ui.forms.events.HyperlinkEvent; 66 import org.eclipse.ui.forms.widgets.FormToolkit; 67 68 public class SchemaAttributeDetails extends AbstractSchemaDetails { 69 70 private static final String JAVA_TYPE = "java"; private static final String RESOURCE_TYPE = "resource"; private static final int BOOL_IND = 0; 73 private static final int STR_IND = 1; 74 private static final int JAVA_IND = 2; 75 private static final int RES_IND = 3; 76 private static final String [] TYPES = new String [4]; 77 static { 78 TYPES[BOOL_IND]= BOOLEAN_TYPE; 79 TYPES[STR_IND] = STRING_TYPE; 80 TYPES[JAVA_IND] = JAVA_TYPE; 81 TYPES[RES_IND] = RESOURCE_TYPE; 82 } 83 private static final String [] USE = 84 new String [] {"optional", "required", "default"}; 86 private SchemaAttribute fAttribute; 87 private FormEntry fValue; 88 private FormEntry fName; 89 private Button fDepTrue; 90 private Button fDepFalse; 91 private Button fTransTrue; 92 private Button fTransFalse; 93 private ComboPart fType; 94 private ComboPart fUse; 95 private TableViewer fRestrictionsTable; 96 private FormEntry fClassEntry; 97 private FormEntry fInterfaceEntry; 98 private Button fAddRestriction; 99 private Button fRemoveRestriction; 100 private Composite fBooleanTypeComp; 101 private Composite fStringTypeComp; 102 private Composite fJavaTypeComp; 103 private Composite fResourceTypeComp; 104 private Composite fNotebook; 105 private StackLayout fNotebookLayout; 106 107 public SchemaAttributeDetails(ElementSection section) { 108 super(section, false); 109 } 110 111 class SchemaAttributeContentProvider extends DefaultTableProvider { 112 public Object [] getElements(Object inputElement) { 113 ISchemaSimpleType type = fAttribute.getType(); 114 ISchemaRestriction restriction = type.getRestriction(); 115 if (restriction != null) 116 return restriction.getChildren(); 117 return new Object [0]; 118 } 119 } 120 121 public void createDetails(Composite parent) { 122 FormToolkit toolkit = getManagedForm().getToolkit(); 123 Color foreground = toolkit.getColors().getColor(IFormColors.TITLE); 124 125 fName = new FormEntry(parent, toolkit, PDEUIMessages.SchemaDetails_name, SWT.NONE); 126 127 Label label = toolkit.createLabel(parent, PDEUIMessages.SchemaDetails_deprecated); 128 label.setForeground(foreground); 129 Button[] buttons = createTrueFalseButtons(parent, toolkit, 2); 130 fDepTrue = buttons[0]; 131 fDepFalse = buttons[1]; 132 133 label = toolkit.createLabel(parent, PDEUIMessages.SchemaAttributeDetails_use); 134 label.setForeground(foreground); 135 fUse = createComboPart(parent, toolkit, USE, 2); 136 137 fValue = new FormEntry(parent, toolkit, PDEUIMessages.SchemaAttributeDetails_defaultValue, null, false, 6); 138 139 label = toolkit.createLabel(parent, PDEUIMessages.SchemaAttributeDetails_type); 140 label.setForeground(foreground); 141 fType = createComboPart(parent, toolkit, TYPES, 2); 142 143 fNotebook = toolkit.createComposite(parent); 144 GridData gd = new GridData(GridData.FILL_BOTH); 145 gd.horizontalSpan = 3; 146 fNotebook.setLayoutData(gd); 147 fNotebook.setLayout(new GridLayout()); 148 fNotebookLayout = new StackLayout(); 149 fNotebook.setLayout(fNotebookLayout); 150 151 fBooleanTypeComp = createEmptyComposite(fNotebook, toolkit); 152 fStringTypeComp = createStringTypeComp(fNotebook, toolkit, foreground); 153 fJavaTypeComp = createJavaTypeComp(fNotebook, toolkit, foreground); 154 fResourceTypeComp = createEmptyComposite(fNotebook, toolkit); 155 156 toolkit.paintBordersFor(parent); 157 toolkit.paintBordersFor(fNotebook); 158 toolkit.paintBordersFor(fJavaTypeComp); 159 toolkit.paintBordersFor(fStringTypeComp); 160 setText(PDEUIMessages.SchemaAttributeDetails_title); 161 } 162 163 private Composite createEmptyComposite(Composite parent, FormToolkit toolkit) { 164 Composite comp = toolkit.createComposite(parent); 165 comp.setLayoutData(new GridData(GridData.FILL_BOTH)); 166 GridLayout layout = new GridLayout(3, false); 167 layout.marginHeight = 2; 168 layout.marginWidth = 0; 169 comp.setLayout(layout); 170 return comp; 171 } 172 173 private Composite createJavaTypeComp(Composite parent, FormToolkit toolkit, Color foreground) { 174 Composite comp = createEmptyComposite(parent, toolkit); 175 fClassEntry = new FormEntry(comp, toolkit, PDEUIMessages.SchemaAttributeDetails_extends, PDEUIMessages.SchemaAttributeDetails_browseButton, isEditable(), 13); 176 fInterfaceEntry = new FormEntry(comp, toolkit, PDEUIMessages.SchemaAttributeDetails_implements, PDEUIMessages.SchemaAttributeDetails_browseButton, isEditable(), 13); 177 return comp; 178 } 179 180 private Composite createStringTypeComp(Composite parent, FormToolkit toolkit, Color foreground) { 181 Composite comp = createEmptyComposite(parent, toolkit); 182 Label label = toolkit.createLabel(comp, PDEUIMessages.SchemaDetails_translatable); 183 label.setForeground(foreground); 184 GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 185 gd.horizontalIndent = 11; 186 gd.verticalIndent = 2; 187 label.setLayoutData(gd); 188 Button[] buttons = createTrueFalseButtons(comp, toolkit, 2); 189 fTransTrue = buttons[0]; 190 fTransFalse = buttons[1]; 191 192 label = toolkit.createLabel(comp, PDEUIMessages.SchemaAttributeDetails_restrictions); 193 label.setForeground(foreground); 194 gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 195 gd.horizontalIndent = 11; 196 gd.verticalIndent = 2; 197 label.setLayoutData(gd); 198 199 Composite tableComp = toolkit.createComposite(comp); 200 GridLayout layout = new GridLayout(); layout.marginHeight = layout.marginWidth = 0; 201 tableComp.setLayout(layout); 202 tableComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 203 204 Table table = toolkit.createTable(tableComp, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); 205 gd = new GridData(GridData.FILL_HORIZONTAL); 206 gd.heightHint = 40; 207 table.setLayoutData(gd); 208 fRestrictionsTable = new TableViewer(table); 209 fRestrictionsTable.setContentProvider(new SchemaAttributeContentProvider()); 210 fRestrictionsTable.setLabelProvider(new LabelProvider()); 211 212 Composite resButtonComp = toolkit.createComposite(comp); 213 layout = new GridLayout(); layout.marginHeight = layout.marginWidth = 0; 214 resButtonComp.setLayout(layout); 215 resButtonComp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 216 fAddRestriction = toolkit.createButton(resButtonComp, PDEUIMessages.SchemaAttributeDetails_addRestButton, SWT.NONE); 217 fRemoveRestriction = toolkit.createButton(resButtonComp, PDEUIMessages.SchemaAttributeDetails_removeRestButton, SWT.NONE); 218 fAddRestriction.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 219 fRemoveRestriction.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 220 return comp; 221 } 222 223 public void updateFields(ISchemaObject object) { 224 if (!(object instanceof SchemaAttribute)) 225 return; 226 fAttribute = (SchemaAttribute)object; 227 setDecription(NLS.bind(PDEUIMessages.SchemaAttributeDetails_description, fAttribute.getName())); 228 fRestrictionsTable.setInput(new Object ()); 229 fName.setValue(fAttribute.getName(), true); fDepTrue.setSelection(fAttribute.isDeprecated()); 231 fDepFalse.setSelection(!fAttribute.isDeprecated()); 232 233 fTransTrue.setSelection(fAttribute.isTranslatable()); 234 fTransFalse.setSelection(!fAttribute.isTranslatable()); 235 236 boolean isStringType = fAttribute.getType().getName().equals(STRING_TYPE); 237 int kind = fAttribute.getKind(); 238 fType.select(isStringType ? 1 + kind : 0); 239 240 fUse.select(fAttribute.getUse()); 241 Object value = fAttribute.getValue(); 242 fValue.setValue(value != null ? value.toString() : "", true); 244 updateJavaFields(); 245 246 boolean editable = isEditableElement(); 247 updateTabSelection(fType.getSelectionIndex()); 248 fTransTrue.setEnabled(editable); 249 fTransFalse.setEnabled(editable); 250 fRestrictionsTable.getControl().setEnabled(editable); 251 fAddRestriction.setEnabled(editable); 252 fRemoveRestriction.setEnabled( 253 !fRestrictionsTable.getSelection().isEmpty() && editable); 254 if (fAttribute.getUse() != 2) { 255 fValue.getLabel().setEnabled(false); 256 fValue.getText().setEditable(false); 257 } else { 258 fValue.setEditable(editable); 259 } 260 fName.setEditable(editable); 261 fDepTrue.setEnabled(editable); 262 fDepFalse.setEnabled(editable); 263 fType.setEnabled(editable); 264 fUse.setEnabled(editable); 265 fClassEntry.setEditable(editable); 266 fInterfaceEntry.setEditable(editable); 267 } 268 269 272 private void updateJavaFields() { 273 String basedOn = fAttribute.getBasedOn(); 274 if ((basedOn != null) && 275 (basedOn.length() > 0)) { 276 int index = basedOn.indexOf(":"); if (index == -1) { 278 String className = 279 basedOn.substring(basedOn.lastIndexOf(".") + 1); if ((className.length() > 1) && 281 (className.charAt(0) == 'I')) { 282 fClassEntry.setValue("", true); fInterfaceEntry.setValue(basedOn, true); 284 } else { 285 fClassEntry.setValue(basedOn, true); 286 fInterfaceEntry.setValue("", true); } 288 } else { 289 fClassEntry.setValue(basedOn.substring(0, index), true); 290 fInterfaceEntry.setValue(basedOn.substring(index + 1), true); 291 } 292 } else { 293 fClassEntry.setValue("", true); fInterfaceEntry.setValue("", true); } 296 297 } 298 299 public void hookListeners() { 300 IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars(); 301 fValue.setFormEntryListener(new FormEntryAdapter(this) { 302 public void textValueChanged(FormEntry entry) { 303 if (blockListeners()) 304 return; 305 fAttribute.setValue(fValue.getValue()); 306 } 307 }); 308 fName.setFormEntryListener(new FormEntryAdapter(this) { 309 public void textValueChanged(FormEntry entry) { 310 if (blockListeners()) 311 return; 312 fAttribute.setName(fName.getValue()); 313 setDecription(NLS.bind(PDEUIMessages.SchemaAttributeDetails_description, fAttribute.getName())); 314 } 315 }); 316 fDepTrue.addSelectionListener(new SelectionAdapter() { 317 public void widgetSelected(SelectionEvent e) { 318 if (blockListeners()) 319 return; 320 fAttribute.setDeprecatedProperty(fDepTrue.getSelection()); 321 } 322 }); 323 fTransTrue.addSelectionListener(new SelectionAdapter() { 324 public void widgetSelected(SelectionEvent e) { 325 if (blockListeners()) 326 return; 327 fAttribute.setTranslatableProperty(fTransTrue.getSelection()); 328 } 329 }); 330 fType.addSelectionListener(new SelectionAdapter() { 331 public void widgetSelected(SelectionEvent e) { 332 if (blockListeners()) 333 return; 334 String typeString = fType.getSelection(); 335 if (!typeString.equals(BOOLEAN_TYPE)) 336 typeString = STRING_TYPE; 337 338 fAttribute.setType(new SchemaSimpleType(fAttribute.getSchema(), typeString)); 339 340 int kind = fType.getSelectionIndex() - 1; fAttribute.setKind(kind > 0 ? kind : 0); 343 ISchemaSimpleType type = fAttribute.getType(); 344 if (type instanceof SchemaSimpleType 345 && kind != IMetaAttribute.STRING 346 && ((SchemaSimpleType) type).getRestriction() != null) { 347 ((SchemaSimpleType) type).setRestriction(null); 348 } 349 updateTabSelection(fType.getSelectionIndex()); 350 } 351 }); 352 fUse.addSelectionListener(new SelectionAdapter() { 353 public void widgetSelected(SelectionEvent e) { 354 if (blockListeners()) 355 return; 356 int use = fUse.getSelectionIndex(); 357 fAttribute.setUse(use); 358 fValue.getLabel().setEnabled(use == 2); 359 fValue.getText().setEditable(use == 2); 360 if (use == 2 && fValue.getValue().length() == 0) { 361 fValue.setValue(PDEUIMessages.SchemaAttributeDetails_defaultDefaultValue); 362 fValue.getText().setSelection(0, fValue.getValue().length()); 363 fValue.getText().setFocus(); 364 } else if (use != 2) 365 fValue.setValue(""); 367 } 368 }); 369 fClassEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) { 370 public void textValueChanged(FormEntry entry) { 371 if (blockListeners()) 372 return; 373 setBasedOn(); 374 } 375 public void linkActivated(HyperlinkEvent e) { 376 if (blockListeners()) 377 return; 378 String value = fClassEntry.getValue(); 379 value = handleLinkActivated(value, false); 380 if (value != null) 381 fClassEntry.setValue(value); 382 } 383 public void browseButtonSelected(FormEntry entry) { 384 if (blockListeners()) 385 return; 386 doOpenSelectionDialog( 387 IJavaElementSearchConstants.CONSIDER_CLASSES, fClassEntry); 388 } 389 }); 390 fInterfaceEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) { 391 public void textValueChanged(FormEntry entry) { 392 if (blockListeners()) 393 return; 394 setBasedOn(); 395 } 396 public void linkActivated(HyperlinkEvent e) { 397 if (blockListeners()) 398 return; 399 String value = fInterfaceEntry.getValue(); 400 value = handleLinkActivated(value, true); 401 if (value != null) 402 fInterfaceEntry.setValue(value); 403 } 404 public void browseButtonSelected(FormEntry entry) { 405 if (blockListeners()) 406 return; 407 doOpenSelectionDialog( 408 IJavaElementSearchConstants.CONSIDER_INTERFACES, fInterfaceEntry); 409 } 410 }); 411 fAddRestriction.addSelectionListener(new SelectionAdapter() { 412 public void widgetSelected(SelectionEvent e) { 413 if (blockListeners()) 414 return; 415 NewRestrictionDialog dialog = new NewRestrictionDialog(getPage().getSite().getShell()); 416 if (dialog.open() != Window.OK) return; 417 String text = dialog.getNewRestriction(); 418 if (text != null && text.length() > 0) { 419 ISchemaSimpleType type = fAttribute.getType(); 420 ChoiceRestriction res = (ChoiceRestriction)type.getRestriction(); 421 Vector vres = new Vector (); 422 if (res != null) { 423 Object [] currRes = res.getChildren(); 424 for (int i = 0; i < currRes.length; i++) { 425 vres.add(currRes[i]); 426 } 427 } 428 vres.add(new SchemaEnumeration(fAttribute.getSchema(), text)); 429 if (res == null) 430 res = new ChoiceRestriction(fAttribute.getSchema()); 431 res.setChildren(vres); 432 if (type instanceof SchemaSimpleType) 433 ((SchemaSimpleType)type).setRestriction(res); 434 fRestrictionsTable.refresh(); 435 } 436 } 437 }); 438 fRemoveRestriction.addSelectionListener(new SelectionAdapter() { 439 public void widgetSelected(SelectionEvent e) { 440 if (blockListeners()) 441 return; 442 ISelection selection = fRestrictionsTable.getSelection(); 443 if (selection.isEmpty()) return; 444 if (!(selection instanceof IStructuredSelection)) return; 445 IStructuredSelection sselection = (IStructuredSelection)selection; 446 Object [] aselection = sselection.toArray(); 447 ISchemaSimpleType type = fAttribute.getType(); 448 ChoiceRestriction res = (ChoiceRestriction)type.getRestriction(); 449 Vector vres = new Vector (); 450 if (res != null) { 451 Object [] currRes = res.getChildren(); 452 for (int i = 0; i < currRes.length; i++) { 453 boolean stays = true; 454 for (int j = 0; j < aselection.length; j++) { 455 if (currRes[i].equals(aselection[j])) 456 stays = false; 457 } 458 if (stays) vres.add(currRes[i]); 459 } 460 res.setChildren(vres); 461 if (type instanceof SchemaSimpleType) { 462 if (vres.size() == 0) 463 ((SchemaSimpleType)type).setRestriction(null); 464 else 465 ((SchemaSimpleType)type).setRestriction(res); 466 } 467 fRestrictionsTable.refresh(); 468 } 469 } 470 }); 471 fRestrictionsTable.addSelectionChangedListener(new ISelectionChangedListener() { 472 public void selectionChanged(SelectionChangedEvent event) { 473 if (blockListeners()) 474 return; 475 fRemoveRestriction.setEnabled(fAttribute.getSchema().isEditable() 476 && !event.getSelection().isEmpty()); 477 } 478 }); 479 } 480 481 private String handleLinkActivated(String value, boolean isInter) { 482 IProject project = getPage().getPDEEditor().getCommonProject(); 483 try { 484 if (project != null && project.hasNature(JavaCore.NATURE_ID)) { 485 IJavaProject javaProject = JavaCore.create(project); 486 IJavaElement element = javaProject.findType(value.replace('$', '.')); 487 if (element != null) 488 JavaUI.openInEditor(element); 489 else { 490 NewClassCreationWizard wizard = new NewClassCreationWizard(project, isInter); 491 WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard); 492 dialog.create(); 493 SWTUtil.setDialogSize(dialog, 400, 500); 494 if (dialog.open() == Window.OK) { 495 return wizard.getQualifiedName(); 496 } 497 } 498 } 499 } catch (PartInitException e1) { 500 } catch (CoreException e1) { 501 } 502 return null; 503 } 504 505 private void setBasedOn() { 506 String classEntry = fClassEntry.getValue().replaceAll(":", ""); String interfaceEntry = fInterfaceEntry.getValue().replaceAll(":", ""); StringBuffer sb = new StringBuffer (); 509 if (classEntry.length() > 0) 510 sb.append(classEntry); 511 if (classEntry.length() > 0 || interfaceEntry.length() > 0) 512 sb.append(":"); if (interfaceEntry.length() > 0) 514 sb.append(interfaceEntry); 515 fAttribute.setBasedOn(sb.length() > 0 ? sb.toString() : null); 516 } 517 518 private void doOpenSelectionDialog(int scopeType, FormEntry entry) { 519 try { 520 String filter = entry.getValue(); 521 filter = filter.substring(filter.lastIndexOf(".") + 1); SelectionDialog dialog = JavaUI.createTypeDialog( 523 PDEPlugin.getActiveWorkbenchShell(), 524 PlatformUI.getWorkbench().getProgressService(), 525 SearchEngine.createWorkspaceScope(), scopeType, false, filter); dialog.setTitle(PDEUIMessages.GeneralInfoSection_selectionTitle); 527 if (dialog.open() == Window.OK) { 528 IType type = (IType) dialog.getResult()[0]; 529 entry.setValue(type.getFullyQualifiedName('$')); 530 entry.commit(); 531 } 532 } catch (CoreException e) { 533 } 534 } 535 536 private void updateTabSelection(int kind) { 537 Control oldPage = fNotebookLayout.topControl; 538 switch (kind) { 539 case 0: 540 fNotebookLayout.topControl = fBooleanTypeComp; 541 break; 542 case 1: 543 fNotebookLayout.topControl = fStringTypeComp; 544 break; 545 case 2: 546 fNotebookLayout.topControl = fJavaTypeComp; 547 break; 548 case 3: 549 fNotebookLayout.topControl = fResourceTypeComp; 550 break; 551 } 552 if (oldPage != fNotebookLayout.topControl) 553 fNotebook.layout(); 554 } 555 556 public void modelChanged(IModelChangedEvent event) { 557 Object [] changedObjs = event.getChangedObjects(); 558 if(event.getChangeType() == IModelChangedEvent.INSERT && changedObjs.length > 0) { 559 if(changedObjs[0] instanceof SchemaAttribute) { 560 fName.getText().setFocus(); 561 } 562 } 563 super.modelChanged(event); 564 } 565 566 569 public void commit(boolean onSave) { 570 super.commit(onSave); 571 fName.commit(); 573 fClassEntry.commit(); 574 fInterfaceEntry.commit(); 575 fValue.commit(); 576 } 577 } 578 | Popular Tags |