001: /*******************************************************************************
002: * Copyright (c) 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 java.util.Vector;
013:
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.ISelectionChangedListener;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.jface.viewers.LabelProvider;
018: import org.eclipse.jface.viewers.SelectionChangedEvent;
019: import org.eclipse.jface.viewers.TableViewer;
020: import org.eclipse.jface.window.Window;
021: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
022: import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
023: import org.eclipse.pde.internal.core.schema.ChoiceRestriction;
024: import org.eclipse.pde.internal.core.schema.SchemaAttribute;
025: import org.eclipse.pde.internal.core.schema.SchemaEnumeration;
026: import org.eclipse.pde.internal.core.schema.SchemaSimpleType;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
029: import org.eclipse.swt.SWT;
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.layout.GridLayout;
035: import org.eclipse.swt.widgets.Button;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Table;
039: import org.eclipse.ui.forms.IFormColors;
040: import org.eclipse.ui.forms.widgets.FormToolkit;
041:
042: public class SchemaStringAttributeDetails extends
043: SchemaAttributeDetails {
044: private Button fTransTrue;
045: private Button fTransFalse;
046: private TableViewer fRestrictionsTable;
047: private Button fAddRestriction;
048: private Button fRemoveRestriction;
049:
050: public SchemaStringAttributeDetails(ElementSection section) {
051: super (section);
052: }
053:
054: protected void createTypeDetails(Composite parent,
055: FormToolkit toolkit) {
056: Color foreground = toolkit.getColors().getColor(
057: IFormColors.TITLE);
058:
059: Label label = toolkit.createLabel(parent,
060: PDEUIMessages.SchemaDetails_translatable);
061: label.setForeground(foreground);
062: GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
063: gd.horizontalIndent = 11;
064: gd.verticalIndent = 2;
065: label.setLayoutData(gd);
066: Button[] buttons = createTrueFalseButtons(parent, toolkit, 2);
067: fTransTrue = buttons[0];
068: fTransFalse = buttons[1];
069:
070: label = toolkit.createLabel(parent,
071: PDEUIMessages.SchemaAttributeDetails_restrictions);
072: label.setForeground(foreground);
073: gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
074: gd.horizontalIndent = 11;
075: gd.verticalIndent = 2;
076: label.setLayoutData(gd);
077:
078: Composite tableComp = toolkit.createComposite(parent);
079: GridLayout layout = new GridLayout();
080: layout.marginHeight = layout.marginWidth = 0;
081: tableComp.setLayout(layout);
082: tableComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
083:
084: Table table = toolkit.createTable(tableComp, SWT.BORDER
085: | SWT.MULTI | SWT.V_SCROLL);
086: gd = new GridData(GridData.FILL_HORIZONTAL);
087: gd.heightHint = 40;
088: gd.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
089: table.setLayoutData(gd);
090: fRestrictionsTable = new TableViewer(table);
091: fRestrictionsTable
092: .setContentProvider(new SchemaAttributeContentProvider());
093: fRestrictionsTable.setLabelProvider(new LabelProvider());
094:
095: Composite resButtonComp = toolkit.createComposite(parent);
096: layout = new GridLayout();
097: layout.marginHeight = layout.marginWidth = 0;
098: resButtonComp.setLayout(layout);
099: resButtonComp.setLayoutData(new GridData(
100: GridData.VERTICAL_ALIGN_BEGINNING));
101: fAddRestriction = toolkit.createButton(resButtonComp,
102: PDEUIMessages.SchemaAttributeDetails_addRestButton,
103: SWT.NONE);
104: fRemoveRestriction = toolkit.createButton(resButtonComp,
105: PDEUIMessages.SchemaAttributeDetails_removeRestButton,
106: SWT.NONE);
107: fAddRestriction.setLayoutData(new GridData(
108: GridData.FILL_HORIZONTAL));
109: fRemoveRestriction.setLayoutData(new GridData(
110: GridData.FILL_HORIZONTAL));
111: }
112:
113: public void updateFields(ISchemaObject object) {
114: if (!(object instanceof SchemaAttribute))
115: return;
116: super .updateFields(object);
117:
118: fTransTrue.setSelection(getAttribute().isTranslatable());
119: fTransFalse.setSelection(!getAttribute().isTranslatable());
120: fRestrictionsTable.setInput(new Object());
121:
122: boolean editable = isEditableElement();
123: fTransTrue.setEnabled(editable);
124: fTransFalse.setEnabled(editable);
125: fRestrictionsTable.getControl().setEnabled(editable);
126: fAddRestriction.setEnabled(editable);
127: fRemoveRestriction.setEnabled(!fRestrictionsTable
128: .getSelection().isEmpty()
129: && editable);
130: }
131:
132: public void hookListeners() {
133: super .hookListeners();
134: fTransTrue.addSelectionListener(new SelectionAdapter() {
135: public void widgetSelected(SelectionEvent e) {
136: if (blockListeners())
137: return;
138: getAttribute().setTranslatableProperty(
139: fTransTrue.getSelection());
140: }
141: });
142: fAddRestriction.addSelectionListener(new SelectionAdapter() {
143: public void widgetSelected(SelectionEvent e) {
144: if (blockListeners())
145: return;
146: NewRestrictionDialog dialog = new NewRestrictionDialog(
147: getPage().getSite().getShell());
148: if (dialog.open() != Window.OK)
149: return;
150: String text = dialog.getNewRestriction();
151: if (text != null && text.length() > 0) {
152: ISchemaSimpleType type = getAttribute().getType();
153: ChoiceRestriction res = (ChoiceRestriction) type
154: .getRestriction();
155: Vector vres = new Vector();
156: if (res != null) {
157: Object[] currRes = res.getChildren();
158: for (int i = 0; i < currRes.length; i++) {
159: vres.add(currRes[i]);
160: }
161: }
162: vres.add(new SchemaEnumeration(getAttribute()
163: .getSchema(), text));
164: if (res == null)
165: res = new ChoiceRestriction(getAttribute()
166: .getSchema());
167: res.setChildren(vres);
168: if (type instanceof SchemaSimpleType)
169: ((SchemaSimpleType) type).setRestriction(res);
170: fRestrictionsTable.refresh();
171: }
172: }
173: });
174: fRemoveRestriction.addSelectionListener(new SelectionAdapter() {
175: public void widgetSelected(SelectionEvent e) {
176: if (blockListeners())
177: return;
178: ISelection selection = fRestrictionsTable
179: .getSelection();
180: if (selection.isEmpty())
181: return;
182: if (!(selection instanceof IStructuredSelection))
183: return;
184: IStructuredSelection sselection = (IStructuredSelection) selection;
185: Object[] aselection = sselection.toArray();
186: ISchemaSimpleType type = getAttribute().getType();
187: ChoiceRestriction res = (ChoiceRestriction) type
188: .getRestriction();
189: Vector vres = new Vector();
190: if (res != null) {
191: Object[] currRes = res.getChildren();
192: for (int i = 0; i < currRes.length; i++) {
193: boolean stays = true;
194: for (int j = 0; j < aselection.length; j++) {
195: if (currRes[i].equals(aselection[j]))
196: stays = false;
197: }
198: if (stays)
199: vres.add(currRes[i]);
200: }
201: res.setChildren(vres);
202: if (type instanceof SchemaSimpleType) {
203: if (vres.size() == 0)
204: ((SchemaSimpleType) type)
205: .setRestriction(null);
206: else
207: ((SchemaSimpleType) type)
208: .setRestriction(res);
209: }
210: fRestrictionsTable.refresh();
211: }
212: }
213: });
214: fRestrictionsTable
215: .addSelectionChangedListener(new ISelectionChangedListener() {
216: public void selectionChanged(
217: SelectionChangedEvent event) {
218: if (blockListeners())
219: return;
220: fRemoveRestriction.setEnabled(getAttribute()
221: .getSchema().isEditable()
222: && !event.getSelection().isEmpty());
223: }
224: });
225: }
226: }
|