001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.schema;
011:
012: import org.eclipse.jface.layout.GridLayoutFactory;
013: import org.eclipse.osgi.util.NLS;
014: import org.eclipse.pde.core.IModelChangedEvent;
015: import org.eclipse.pde.internal.core.ischema.IMetaAttribute;
016: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
017: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
018: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
019: import org.eclipse.pde.internal.core.ischema.ISchemaRestriction;
020: import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
021: import org.eclipse.pde.internal.core.schema.SchemaAttribute;
022: import org.eclipse.pde.internal.core.schema.SchemaSimpleType;
023: import org.eclipse.pde.internal.ui.PDEUIMessages;
024: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
025: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
026: import org.eclipse.pde.internal.ui.parts.ComboPart;
027: import org.eclipse.pde.internal.ui.parts.FormEntry;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.custom.StackLayout;
030: import org.eclipse.swt.events.SelectionAdapter;
031: import org.eclipse.swt.events.SelectionEvent;
032: import org.eclipse.swt.graphics.Color;
033: import org.eclipse.swt.layout.GridData;
034: import org.eclipse.swt.widgets.Button;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.swt.widgets.Label;
037: import org.eclipse.ui.forms.IFormColors;
038: import org.eclipse.ui.forms.widgets.FormToolkit;
039:
040: public abstract class SchemaAttributeDetails extends
041: AbstractSchemaDetails {
042:
043: private SchemaAttribute fAttribute;
044: private FormEntry fValue;
045: private FormEntry fName;
046: private Button fDepTrue;
047: private Button fDepFalse;
048: private ComboPart fType;
049: private ComboPart fUseDefault;
050: private ComboPart fUseOther;
051: private StackLayout fUseLayout;
052: private Composite fUseComp;
053: private Composite fUseCompDefault;
054: private Composite fUseCompOther;
055:
056: public SchemaAttributeDetails(ElementSection section) {
057: super (section, false, true);
058: }
059:
060: class SchemaAttributeContentProvider extends DefaultTableProvider {
061: public Object[] getElements(Object inputElement) {
062: ISchemaSimpleType type = fAttribute.getType();
063: ISchemaRestriction restriction = type.getRestriction();
064: if (restriction != null)
065: return restriction.getChildren();
066: return new Object[0];
067: }
068: }
069:
070: public void createDetails(Composite parent) {
071: FormToolkit toolkit = getManagedForm().getToolkit();
072: Color foreground = toolkit.getColors().getColor(
073: IFormColors.TITLE);
074:
075: fName = new FormEntry(parent, toolkit,
076: PDEUIMessages.SchemaDetails_name, SWT.NONE);
077: // Ensures label columns on every detail page are same width
078: ((GridData) fName.getLabel().getLayoutData()).widthHint = minLabelWeight;
079:
080: Label label = toolkit.createLabel(parent,
081: PDEUIMessages.SchemaDetails_deprecated);
082: label.setForeground(foreground);
083: Button[] buttons = createTrueFalseButtons(parent, toolkit, 2);
084: fDepTrue = buttons[0];
085: fDepFalse = buttons[1];
086:
087: label = toolkit.createLabel(parent,
088: PDEUIMessages.SchemaAttributeDetails_use);
089: label.setForeground(foreground);
090:
091: fUseComp = toolkit.createComposite(parent);
092: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
093: gd.horizontalSpan = 2;
094: fUseComp.setLayoutData(gd);
095: fUseLayout = new StackLayout();
096: fUseComp.setLayout(fUseLayout);
097:
098: fUseCompDefault = toolkit.createComposite(fUseComp);
099: fUseCompDefault.setLayout(GridLayoutFactory.fillDefaults()
100: .margins(0, 0).numColumns(2).create());
101:
102: fUseDefault = createComboPart(fUseCompDefault, toolkit,
103: ISchemaAttribute.USE_TABLE, 1, SWT.NONE);
104: fValue = new FormEntry(fUseCompDefault, toolkit, null, 0, 1);
105:
106: fUseCompOther = toolkit.createComposite(fUseComp);
107: fUseCompOther.setLayout(GridLayoutFactory.fillDefaults()
108: .margins(0, 0).create());
109:
110: fUseOther = createComboPart(fUseCompOther, toolkit,
111: ISchemaAttribute.USE_TABLE, 1);
112:
113: label = toolkit.createLabel(parent,
114: PDEUIMessages.SchemaAttributeDetails_type);
115: label.setForeground(foreground);
116: fType = createComboPart(parent, toolkit,
117: ISchemaAttribute.TYPES, 2);
118:
119: createTypeDetails(parent, toolkit);
120:
121: toolkit.paintBordersFor(parent);
122: setText(PDEUIMessages.SchemaAttributeDetails_title);
123: }
124:
125: protected abstract void createTypeDetails(Composite parent,
126: FormToolkit toolkit);
127:
128: public void updateFields(ISchemaObject object) {
129: if (!(object instanceof SchemaAttribute))
130: return;
131: fAttribute = (SchemaAttribute) object;
132: setDecription(NLS.bind(
133: PDEUIMessages.SchemaAttributeDetails_description,
134: fAttribute.getName()));
135: fName.setValue(fAttribute.getName(), true); //$NON-NLS-1$
136: fDepTrue.setSelection(fAttribute.isDeprecated());
137: fDepFalse.setSelection(!fAttribute.isDeprecated());
138:
139: boolean isStringType = fAttribute.getType().getName().equals(
140: ISchemaAttribute.TYPES[ISchemaAttribute.STR_IND]);
141: int kind = fAttribute.getKind();
142: fType.select(isStringType ? 1 + kind : 0);
143:
144: fUseDefault.select(fAttribute.getUse());
145: fUseOther.select(fAttribute.getUse());
146: Object value = fAttribute.getValue();
147: fValue
148: .setValue(
149: value != null ? value.toString()
150: : PDEUIMessages.SchemaAttributeDetails_defaultDefaultValue,
151: true);
152:
153: boolean editable = isEditableElement();
154: if (fAttribute.getUse() != 2) {
155: fUseLayout.topControl = fUseCompOther;
156: } else {
157: fUseLayout.topControl = fUseCompDefault;
158: }
159: fUseComp.layout();
160: fName.setEditable(editable);
161: fDepTrue.setEnabled(editable);
162: fDepFalse.setEnabled(editable);
163: fType.setEnabled(editable);
164: fUseDefault.setEnabled(editable);
165: fUseOther.setEnabled(editable);
166: }
167:
168: public void hookListeners() {
169: fValue.setFormEntryListener(new FormEntryAdapter(this ) {
170: public void textValueChanged(FormEntry entry) {
171: if (blockListeners())
172: return;
173: fAttribute.setValue(fValue.getValue());
174: }
175: });
176: fName.setFormEntryListener(new FormEntryAdapter(this ) {
177: public void textValueChanged(FormEntry entry) {
178: if (blockListeners())
179: return;
180: boolean revert = false;
181: if (fName.getValue().length() == 0)
182: revert = true;
183: else {
184: ISchemaObject parent = fAttribute.getParent();
185: while (!(parent instanceof ISchemaElement))
186: parent = parent.getParent();
187: ISchemaElement element = (ISchemaElement) parent;
188: ISchemaAttribute[] attributes = element
189: .getAttributes();
190: for (int i = 0; i < attributes.length; i++) {
191: if (attributes[i] != fAttribute
192: && attributes[i].getName()
193: .equalsIgnoreCase(
194: fName.getValue())) {
195: revert = true;
196: break;
197: }
198: }
199: }
200: if (revert)
201: fName.setValue(fAttribute.getName(), true);
202: else {
203: fAttribute.setName(fName.getValue());
204: setDecription(NLS
205: .bind(
206: PDEUIMessages.SchemaAttributeDetails_description,
207: fAttribute.getName()));
208: }
209: }
210: });
211: fDepTrue.addSelectionListener(new SelectionAdapter() {
212: public void widgetSelected(SelectionEvent e) {
213: if (blockListeners())
214: return;
215: fAttribute.setDeprecatedProperty(fDepTrue
216: .getSelection());
217: }
218: });
219: fType.addSelectionListener(new SelectionAdapter() {
220: public void widgetSelected(SelectionEvent e) {
221: if (blockListeners())
222: return;
223: String typeString = fType.getSelection();
224: if (!typeString
225: .equals(ISchemaAttribute.TYPES[ISchemaAttribute.BOOL_IND]))
226: typeString = ISchemaAttribute.TYPES[ISchemaAttribute.STR_IND];
227:
228: fAttribute.setType(new SchemaSimpleType(fAttribute
229: .getSchema(), typeString));
230:
231: int kind = fType.getSelectionIndex() - 1; // adjust for "boolean" in combo
232: fAttribute.setKind(kind > 0 ? kind : 0); // kind could be -1
233:
234: ISchemaSimpleType type = fAttribute.getType();
235: if (type instanceof SchemaSimpleType
236: && kind != IMetaAttribute.STRING
237: && ((SchemaSimpleType) type).getRestriction() != null) {
238: ((SchemaSimpleType) type).setRestriction(null);
239: }
240: fireSelectionChange();
241: }
242: });
243: fUseDefault.addSelectionListener(new SelectionAdapter() {
244: public void widgetSelected(SelectionEvent e) {
245: if (blockListeners())
246: return;
247: int i = fUseDefault.getSelectionIndex();
248: setBlockListeners(true);
249: fUseOther.select(i);
250: setBlockListeners(false);
251: doUseChange(i);
252: }
253: });
254: fUseOther.addSelectionListener(new SelectionAdapter() {
255: public void widgetSelected(SelectionEvent e) {
256: if (blockListeners())
257: return;
258: int i = fUseOther.getSelectionIndex();
259: setBlockListeners(true);
260: fUseDefault.select(i);
261: setBlockListeners(false);
262: doUseChange(i);
263: }
264: });
265: }
266:
267: private void doUseChange(int index) {
268: fAttribute.setUse(index);
269: if (index == 2) {
270: fUseLayout.topControl = fUseCompDefault;
271: fUseComp.layout();
272: if (fValue
273: .getValue()
274: .equals(
275: PDEUIMessages.SchemaAttributeDetails_defaultDefaultValue))
276: fValue.getText().setSelection(0,
277: fValue.getValue().length());
278: fValue.getText().setFocus();
279: } else if (index != 2) {
280: fUseLayout.topControl = fUseCompOther;
281: fUseComp.layout();
282: fValue
283: .setValue(PDEUIMessages.SchemaAttributeDetails_defaultDefaultValue);
284: fUseCompOther.setFocus();
285: }
286: }
287:
288: public void modelChanged(IModelChangedEvent event) {
289: Object[] changedObjs = event.getChangedObjects();
290: if (event.getChangeType() == IModelChangedEvent.INSERT
291: && changedObjs.length > 0) {
292: if (changedObjs[0] instanceof SchemaAttribute) {
293: fName.getText().setFocus();
294: }
295: }
296: super .modelChanged(event);
297: }
298:
299: /* (non-Javadoc)
300: * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
301: */
302: public void commit(boolean onSave) {
303: super .commit(onSave);
304: // Only required for form entries
305: fName.commit();
306: fValue.commit();
307: }
308:
309: protected SchemaAttribute getAttribute() {
310: return fAttribute;
311: }
312: }
|