1 11 17 package org.eclipse.pde.internal.ui.editor.plugin.rows; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 21 import org.eclipse.pde.internal.core.ischema.ISchemaEnumeration; 22 import org.eclipse.pde.internal.core.ischema.ISchemaRestriction; 23 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType; 24 import org.eclipse.pde.internal.ui.PDEPlugin; 25 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 26 import org.eclipse.pde.internal.ui.editor.IContextPart; 27 import org.eclipse.pde.internal.ui.parts.ComboPart; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.events.SelectionAdapter; 30 import org.eclipse.swt.events.SelectionEvent; 31 import org.eclipse.swt.layout.GridData; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.ui.forms.widgets.FormToolkit; 34 35 36 public class ChoiceAttributeRow extends ExtensionAttributeRow { 37 protected ComboPart combo; 38 41 public ChoiceAttributeRow(IContextPart part, ISchemaAttribute att) { 42 super(part, att); 43 } 44 45 public void createContents(Composite parent, FormToolkit toolkit, int span) { 46 super.createContents(parent, toolkit, span); 47 createLabel(parent, toolkit); 48 combo = new ComboPart(); 49 combo.createControl(parent, toolkit, SWT.READ_ONLY); 50 ISchemaSimpleType type = getAttribute().getType(); 51 ISchemaRestriction restriction = type.getRestriction(); 52 if (restriction!=null) { 53 Object rchildren[] = restriction.getChildren(); 54 if (getUse()!=ISchemaAttribute.REQUIRED) 55 combo.add(""); for (int i=0; i<rchildren.length; i++) { 57 Object rchild = rchildren[i]; 58 if (rchild instanceof ISchemaEnumeration) 59 combo.add(((ISchemaEnumeration)rchild).getName()); 60 } 61 } 62 GridData gd = new GridData(span==2?GridData.FILL_HORIZONTAL:GridData.HORIZONTAL_ALIGN_FILL); 63 gd.widthHint = 20; 64 gd.horizontalSpan = span-1; 65 gd.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT; 66 combo.getControl().setLayoutData(gd); 67 combo.addSelectionListener(new SelectionAdapter() { 68 public void widgetSelected(SelectionEvent e) { 69 if (!blockNotification) markDirty(); 70 } 71 }); 72 combo.getControl().setEnabled(part.isEditable()); 73 } 74 77 protected void update() { 78 blockNotification=true; 79 String value = getValue(); 80 if (value!= null && isValid(value)) 81 combo.setText(value); 82 else if (getUse()==ISchemaAttribute.REQUIRED) 83 combo.setText(getValidValue()); 84 else 85 combo.setText(""); blockNotification = false; 87 dirty=false; 88 } 89 90 protected String getValidValue(){ 91 ISchemaAttribute attInfo = getAttribute(); 92 if (attInfo.getType().getRestriction()!= null) 93 return attInfo.getType().getRestriction().getChildren()[0].toString(); 94 return ""; } 96 97 protected boolean isValid(String value){ 98 if (getAttribute().getUse() != ISchemaAttribute.REQUIRED && value.equals("")) return true; 100 101 ISchemaRestriction restriction = getAttribute().getType().getRestriction(); 102 if (restriction == null) 103 return true; 104 Object [] children = restriction.getChildren(); 105 for (int i =0; i<children.length; i++){ 106 Object rchild = children[i]; 107 if (rchild instanceof ISchemaEnumeration && ((ISchemaEnumeration)rchild).getName().equals(value)) 108 return true; 109 } 110 return false; 111 } 112 public void commit() { 113 if (dirty && input != null) { 114 try { 115 String selection = combo.getSelection(); 116 if (selection.length()==0) selection = null; 117 input.setAttribute(getName(), selection); 118 dirty = false; 119 } catch (CoreException e) { 120 PDEPlugin.logException(e); 121 } 122 } 123 } 124 public void setFocus() { 125 combo.getControl().setFocus(); 126 } 127 } 128 | Popular Tags |