KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > AbstractSchemaDetails


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.schema;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.pde.core.IModelChangedEvent;
15 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
16 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
17 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
18 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
19 import org.eclipse.pde.internal.ui.PDEUIMessages;
20 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
21 import org.eclipse.pde.internal.ui.editor.PDEDetails;
22 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
23 import org.eclipse.pde.internal.ui.parts.ComboPart;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Menu;
35 import org.eclipse.swt.widgets.Spinner;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.forms.IFormColors;
38 import org.eclipse.ui.forms.IFormPart;
39 import org.eclipse.ui.forms.widgets.ExpandableComposite;
40 import org.eclipse.ui.forms.widgets.FormToolkit;
41 import org.eclipse.ui.forms.widgets.Section;
42
43 public abstract class AbstractSchemaDetails extends PDEDetails {
44
45     protected static final String JavaDoc STRING_TYPE = "string"; //$NON-NLS-1$
46
protected static final String JavaDoc BOOLEAN_TYPE = "boolean"; //$NON-NLS-1$
47
protected static final String JavaDoc[] BOOLS =
48         new String JavaDoc[] { Boolean.toString(true), Boolean.toString(false) };
49     
50     private Section fSection;
51     private Text fDtdLabel;
52     private ElementSection fElementSection;
53     private boolean fShowDTD;
54     private Spinner fMinOccurSpinner;
55     private Spinner fMaxOccurSpinner;
56     private Button fUnboundSelect;
57     private Label fMinLabel;
58     private Label fMaxLabel;
59     private boolean fBlockListeners = false;
60     
61     public AbstractSchemaDetails(ElementSection section, boolean showDTD) {
62         fElementSection = section;
63         fShowDTD = showDTD;
64     }
65     
66     public void modelChanged(IModelChangedEvent event) {
67         if (event.getChangeType() == IModelChangedEvent.REMOVE)
68             return;
69         Object JavaDoc[] objects = event.getChangedObjects();
70         for (int i = 0; i < objects.length; i++) {
71             if (objects[i] instanceof ISchemaCompositor)
72                 updateDTDLabel(objects[i]);
73         }
74     }
75     
76     public final void createContents(Composite parent) {
77
78         parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
79         FormToolkit toolkit = getManagedForm().getToolkit();
80         fSection = toolkit.createSection(parent, Section.DESCRIPTION | ExpandableComposite.TITLE_BAR);
81         fSection.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
82         fSection.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
83         
84         GridData gd = new GridData(GridData.FILL_BOTH);
85         fSection.setLayoutData(gd);
86         
87         // Align the master and details section headers (misalignment caused
88
// by section toolbar icons)
89
getPage().alignSectionHeaders(fElementSection.getSection(), fSection);
90         
91         Composite client = toolkit.createComposite(fSection);
92         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
93         
94         createDetails(client);
95         
96         if (fShowDTD) {
97             Label label = toolkit.createLabel(client, PDEUIMessages.AbstractSchemaDetails_dtdLabel);
98             label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
99             gd = new GridData(GridData.FILL_HORIZONTAL);
100             gd.horizontalSpan = 3;
101             gd.verticalIndent = 15;
102             label.setLayoutData(gd);
103             
104             fDtdLabel = toolkit.createText(client, "", SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);//$NON-NLS-1$
105
gd = new GridData(GridData.FILL_HORIZONTAL);
106             gd.horizontalSpan = 3;
107             gd.heightHint = 60;
108             fDtdLabel.setLayoutData(gd);
109             fDtdLabel.setEditable(false);
110             // remove pop-up menu
111
fDtdLabel.setMenu(new Menu(client));
112         }
113         
114         toolkit.paintBordersFor(client);
115         fSection.setClient(client);
116         markDetailsPart(fSection);
117         
118         hookListeners();
119     }
120     
121     public abstract void createDetails(Composite parent);
122     public abstract void updateFields(ISchemaObject obj);
123     public abstract void hookListeners();
124     
125     public boolean isEditableElement() {
126         return fElementSection.isEditable();
127     }
128     
129     protected void setDecription(String JavaDoc desc) {
130         fSection.setDescription(desc);
131     }
132     protected void setText(String JavaDoc title) {
133         fSection.setText(title);
134     }
135     public String JavaDoc getContextId() {
136         return SchemaInputContext.CONTEXT_ID;
137     }
138     public PDEFormPage getPage() {
139         return (PDEFormPage)getManagedForm().getContainer();
140     }
141     public boolean isEditable() {
142         return getPage().getPDEEditor().getAggregateModel().isEditable();
143     }
144     public void fireSaveNeeded() {
145         markDirty();
146         getPage().getPDEEditor().fireSaveNeeded(getContextId(), false);
147     }
148     public void selectionChanged(IFormPart part, ISelection selection) {
149         if (!(part instanceof ElementSection))
150             return;
151         Object JavaDoc obj = ((IStructuredSelection)selection).getFirstElement();
152         updateDTDLabel(obj);
153         if (obj instanceof ISchemaObject) {
154             setBlockListeners(true);
155             updateFields((ISchemaObject)obj);
156             setBlockListeners(false);
157         }
158     }
159     
160     private void updateDTDLabel(Object JavaDoc changeObject) {
161         if (!fShowDTD || fDtdLabel.isDisposed()) return;
162         if (changeObject instanceof ISchemaAttribute) {
163             changeObject = ((ISchemaAttribute) changeObject).getParent();
164         } else if (changeObject instanceof ISchemaCompositor) {
165             while (changeObject != null) {
166                 if (changeObject instanceof ISchemaElement)
167                     break;
168                 changeObject = ((ISchemaCompositor)changeObject).getParent();
169             }
170         }
171         if (changeObject instanceof ISchemaElement)
172             fDtdLabel.setText(((ISchemaElement)changeObject).getDTDRepresentation(false));
173     }
174     
175     protected void fireMasterSelection(ISelection selection) {
176         fElementSection.fireSelection(selection);
177     }
178     
179     protected ComboPart createComboPart(Composite parent, FormToolkit toolkit, String JavaDoc[] items, int colspan) {
180         ComboPart cp = new ComboPart();
181         cp.createControl(parent, toolkit, SWT.READ_ONLY);
182         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
183         gd.horizontalSpan = colspan;
184         cp.getControl().setLayoutData(gd);
185         cp.setItems(items);
186         cp.getControl().setEnabled(isEditable());
187         return cp;
188     }
189     
190     protected Button[] createTrueFalseButtons(Composite parent, FormToolkit toolkit, int colSpan) {
191         Composite comp = toolkit.createComposite(parent, SWT.NONE);
192         GridLayout gl = new GridLayout(2, false);
193         gl.marginHeight = gl.marginWidth = 0;
194         comp.setLayout(gl);
195         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
196         gd.horizontalSpan = colSpan;
197         comp.setLayoutData(gd);
198         Button tButton = toolkit.createButton(comp, BOOLS[0], SWT.RADIO);
199         Button fButton = toolkit.createButton(comp, BOOLS[1], SWT.RADIO);
200         gd = new GridData();
201         gd.horizontalIndent = 20;
202         fButton.setLayoutData(gd);
203         return new Button[] {tButton, fButton};
204     }
205     
206     protected Composite createMinOccurComp(Composite parent, FormToolkit toolkit) {
207         fMinLabel = toolkit.createLabel(parent, PDEUIMessages.AbstractSchemaDetails_minOccurLabel);
208         fMinLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
209         Composite comp = toolkit.createComposite(parent);
210         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
211         gd.horizontalSpan = 2;
212         GridLayout layout = new GridLayout();
213         layout.marginHeight = layout.marginWidth = 0;
214         comp.setLayout(layout);
215         comp.setLayoutData(gd);
216         fMinOccurSpinner = new Spinner(comp, SWT.BORDER);
217         fMinOccurSpinner.setMinimum(0);
218         fMinOccurSpinner.setMaximum(999);
219         return comp;
220     }
221     
222     protected Composite createMaxOccurComp(Composite parent, FormToolkit toolkit) {
223         fMaxLabel = toolkit.createLabel(parent, PDEUIMessages.AbstractSchemaDetails_maxOccurLabel);
224         fMaxLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
225         Composite comp = toolkit.createComposite(parent);
226         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
227         gd.horizontalSpan = 2;
228         GridLayout layout = new GridLayout(3, false);
229         layout.marginHeight = layout.marginWidth = 0;
230         comp.setLayout(layout);
231         comp.setLayoutData(gd);
232         
233         fMaxOccurSpinner = new Spinner(comp, SWT.BORDER);
234         fMaxOccurSpinner.setMinimum(1);
235         fMaxOccurSpinner.setMaximum(999);
236         fMaxOccurSpinner.setIncrement(1);
237         
238         fUnboundSelect = toolkit.createButton(comp, PDEUIMessages.AbstractSchemaDetails_unboundedButton, SWT.CHECK);
239         gd = new GridData();
240         gd.horizontalIndent = 10;
241         fUnboundSelect.setLayoutData(gd);
242         fUnboundSelect.addSelectionListener(new SelectionAdapter() {
243             public void widgetSelected(SelectionEvent e) {
244                 if (blockListeners())
245                     return;
246                 fMaxOccurSpinner.setEnabled(!fUnboundSelect.getSelection()
247                         && isEditableElement());
248             }
249         });
250         
251         return comp;
252     }
253     
254     protected int getMinOccur() {
255         if (fMinOccurSpinner != null)
256             return fMinOccurSpinner.getSelection();
257         return 0;
258     }
259     
260     protected int getMaxOccur() {
261         if (fMaxOccurSpinner != null) {
262             if (fMaxOccurSpinner.isEnabled())
263                 return fMaxOccurSpinner.getSelection();
264             return Integer.MAX_VALUE;
265         }
266         return 1;
267     }
268     
269     protected void updateMinOccur(int min) {
270         if (fMinOccurSpinner != null)
271             fMinOccurSpinner.setSelection(min);
272     }
273     
274     protected void updateMaxOccur(int max) {
275         if (fMaxOccurSpinner == null) return;
276         boolean isMax = max == Integer.MAX_VALUE;
277         fUnboundSelect.setSelection(isMax);
278         fMaxOccurSpinner.setEnabled(!isMax);
279         if (!isMax)
280             fMaxOccurSpinner.setSelection(max);
281     }
282     
283     protected void hookMinOccur(SelectionAdapter adapter) {
284         fMinOccurSpinner.addSelectionListener(adapter);
285         fMinOccurSpinner.addModifyListener(new ModifyListener() {
286             public void modifyText(ModifyEvent e) {
287                 if (blockListeners())
288                     return;
289                 int minOccur = fMinOccurSpinner.getSelection();
290                 if (minOccur > getMaxOccur())
291                     fMinOccurSpinner.setSelection(minOccur - 1);
292             }
293         });
294     }
295     
296     protected void hookMaxOccur(SelectionAdapter adapter) {
297         fUnboundSelect.addSelectionListener(adapter);
298         fMaxOccurSpinner.addSelectionListener(adapter);
299         fMaxOccurSpinner.addModifyListener(new ModifyListener() {
300             public void modifyText(ModifyEvent e) {
301                 if (blockListeners())
302                     return;
303                 int maxValue = fMaxOccurSpinner.getSelection();
304                 if (maxValue < getMinOccur())
305                     fMaxOccurSpinner.setSelection(maxValue + 1);
306             }
307         });
308     }
309     
310     protected void enableMinMax(boolean enable) {
311         fMinOccurSpinner.setEnabled(enable);
312         fMaxOccurSpinner.setEnabled(!fUnboundSelect.getSelection() && enable);
313         fUnboundSelect.setEnabled(enable);
314         fMinLabel.setEnabled(enable);
315         fMaxLabel.setEnabled(enable);
316     }
317     
318     protected boolean blockListeners() {
319         return fBlockListeners;
320     }
321     
322     protected void setBlockListeners(boolean blockListeners) {
323         fBlockListeners = blockListeners;
324     }
325 }
326
Popular Tags