KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > PortabilitySection


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.feature;
12
13 import java.util.Locale JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.pde.core.IModelChangedEvent;
19 import org.eclipse.pde.internal.core.ifeature.IEnvironment;
20 import org.eclipse.pde.internal.core.ifeature.IFeature;
21 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
22 import org.eclipse.pde.internal.ui.PDEPlugin;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
25 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
26 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
27 import org.eclipse.pde.internal.ui.editor.PDESection;
28 import org.eclipse.pde.internal.ui.parts.FormEntry;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.BusyIndicator;
31 import org.eclipse.swt.dnd.Clipboard;
32 import org.eclipse.swt.dnd.RTFTransfer;
33 import org.eclipse.swt.dnd.TextTransfer;
34 import org.eclipse.swt.dnd.Transfer;
35 import org.eclipse.swt.dnd.TransferData;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.ui.forms.IManagedForm;
39 import org.eclipse.ui.forms.widgets.FormToolkit;
40 import org.eclipse.ui.forms.widgets.Section;
41 import org.eclipse.ui.forms.widgets.TableWrapData;
42
43 public class PortabilitySection extends PDESection {
44     public static Choice[] getArchChoices() {
45         return getKnownChoices(Platform.knownOSArchValues());
46     }
47
48     private static Choice[] getKnownChoices(String JavaDoc[] values) {
49         Choice[] choices = new Choice[values.length];
50         for (int i = 0; i < choices.length; i++) {
51             choices[i] = new Choice(values[i], values[i]);
52         }
53         return choices;
54     }
55
56     public static Choice[] getNLChoices() {
57         Locale JavaDoc[] locales = Locale.getAvailableLocales();
58         Choice[] choices = new Choice[locales.length];
59         for (int i = 0; i < locales.length; i++) {
60             Locale JavaDoc locale = locales[i];
61             choices[i] = new Choice(locale.toString(), locale.toString()
62                     + " - " + locale.getDisplayName()); //$NON-NLS-1$
63
}
64         return choices;
65     }
66
67     public static Choice[] getOSChoices() {
68         return getKnownChoices(Platform.knownOSValues());
69     }
70
71     public static Choice[] getWSChoices() {
72         return getKnownChoices(Platform.knownWSValues());
73     }
74
75     private FormEntry fArchText;
76
77     private FormEntry fNlText;
78
79     private FormEntry fOsText;
80
81     private FormEntry fWsText;
82
83     public PortabilitySection(FeatureFormPage page, Composite parent) {
84         this(page, parent, PDEUIMessages.FeatureEditor_PortabilitySection_title,
85                 PDEUIMessages.FeatureEditor_PortabilitySection_desc, SWT.NULL);
86     }
87
88     public PortabilitySection(PDEFormPage page, Composite parent, String JavaDoc title,
89             String JavaDoc desc, int toggleStyle) {
90         super(page, parent, Section.DESCRIPTION | toggleStyle);
91         getSection().setText(title);
92         getSection().setDescription(desc);
93         createClient(getSection(), page.getManagedForm().getToolkit());
94     }
95
96     private void applyValue(String JavaDoc property, String JavaDoc value) throws CoreException {
97         IFeatureModel model = (IFeatureModel) getPage().getModel();
98         IFeature feature = model.getFeature();
99         if (property.equals(IEnvironment.P_NL))
100             feature.setNL(value);
101         else if (property.equals(IEnvironment.P_OS))
102             feature.setOS(value);
103         else if (property.equals(IEnvironment.P_WS))
104             feature.setWS(value);
105         else if (property.equals(IEnvironment.P_ARCH))
106             feature.setArch(value);
107     }
108
109     public void cancelEdit() {
110         fOsText.cancelEdit();
111         fWsText.cancelEdit();
112         fNlText.cancelEdit();
113         fArchText.cancelEdit();
114         super.cancelEdit();
115     }
116
117     public boolean canPaste(Clipboard clipboard) {
118         TransferData[] types = clipboard.getAvailableTypes();
119         Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(),
120                 RTFTransfer.getInstance() };
121         for (int i = 0; i < types.length; i++) {
122             for (int j = 0; j < transfers.length; j++) {
123                 if (transfers[j].isSupportedType(types[i]))
124                     return true;
125             }
126         }
127         return false;
128     }
129
130     public void commit(boolean onSave) {
131         fOsText.commit();
132         fWsText.commit();
133         fNlText.commit();
134         fArchText.commit();
135         super.commit(onSave);
136     }
137
138     public void createClient(Section section, FormToolkit toolkit) {
139
140         section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1));
141         TableWrapData twd = new TableWrapData();
142         twd.grabHorizontal = true;
143         section.setLayoutData(twd);
144         
145         Composite container = toolkit.createComposite(section);
146         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
147         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
148
149         String JavaDoc editLabel = PDEUIMessages.FeatureEditor_PortabilitySection_edit;
150
151         fOsText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_PortabilitySection_os, editLabel, false);
152         fOsText.setFormEntryListener(new FormEntryAdapter(this) {
153
154             public void browseButtonSelected(FormEntry entry) {
155                 BusyIndicator.showWhile(fOsText.getText().getDisplay(),
156                         new Runnable JavaDoc() {
157                             public void run() {
158                                 Choice[] choices = getOSChoices();
159                                 openPortabilityChoiceDialog(IEnvironment.P_OS,
160                                         fOsText, choices);
161                             }
162                         });
163             }
164
165             public void textValueChanged(FormEntry text) {
166                 try {
167                     applyValue(IEnvironment.P_OS, text.getValue());
168                 } catch (CoreException e) {
169                     PDEPlugin.logException(e);
170                 }
171             }
172         });
173         limitTextWidth(fOsText);
174         fOsText.setEditable(isEditable());
175
176         fWsText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_PortabilitySection_ws, editLabel, false);
177         fWsText.setFormEntryListener(new FormEntryAdapter(this) {
178
179             public void browseButtonSelected(FormEntry entry) {
180                 BusyIndicator.showWhile(fWsText.getText().getDisplay(),
181                         new Runnable JavaDoc() {
182                             public void run() {
183                                 Choice[] choices = getWSChoices();
184                                 openPortabilityChoiceDialog(IEnvironment.P_WS,
185                                         fWsText, choices);
186                             }
187                         });
188             }
189
190             public void textValueChanged(FormEntry text) {
191                 try {
192                     applyValue(IEnvironment.P_WS, text.getValue());
193                 } catch (CoreException e) {
194                     PDEPlugin.logException(e);
195                 }
196             }
197         });
198         limitTextWidth(fWsText);
199         fWsText.setEditable(isEditable());
200
201         fNlText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_PortabilitySection_nl, editLabel, false);
202
203         fNlText.setFormEntryListener(new FormEntryAdapter(this) {
204
205             public void browseButtonSelected(FormEntry entry) {
206                 BusyIndicator.showWhile(fNlText.getText().getDisplay(),
207                         new Runnable JavaDoc() {
208                             public void run() {
209                                 Choice[] choices = getNLChoices();
210                                 openPortabilityChoiceDialog(IEnvironment.P_NL,
211                                         fNlText, choices);
212                             }
213                         });
214             }
215
216             public void textValueChanged(FormEntry text) {
217                 try {
218                     applyValue(IEnvironment.P_NL, text.getValue());
219                 } catch (CoreException e) {
220                     PDEPlugin.logException(e);
221                 }
222             }
223         });
224         limitTextWidth(fNlText);
225         fNlText.setEditable(isEditable());
226
227         fArchText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_PortabilitySection_arch, editLabel, false);
228         fArchText.setFormEntryListener(new FormEntryAdapter(this) {
229
230             public void browseButtonSelected(FormEntry entry) {
231                 BusyIndicator.showWhile(fArchText.getText().getDisplay(),
232                         new Runnable JavaDoc() {
233                             public void run() {
234                                 Choice[] choices = getArchChoices();
235                                 openPortabilityChoiceDialog(
236                                         IEnvironment.P_ARCH, fArchText, choices);
237                             }
238                         });
239             }
240
241             public void textValueChanged(FormEntry text) {
242                 try {
243                     applyValue(IEnvironment.P_ARCH, text.getValue());
244                 } catch (CoreException e) {
245                     PDEPlugin.logException(e);
246                 }
247             }
248
249         });
250         limitTextWidth(fArchText);
251         fArchText.setEditable(isEditable());
252
253         toolkit.paintBordersFor(container);
254         section.setClient(container);
255     }
256
257     public void dispose() {
258         IFeatureModel model = (IFeatureModel) getPage().getModel();
259         if (model != null)
260             model.removeModelChangedListener(this);
261         super.dispose();
262     }
263
264     /*
265      * (non-Javadoc)
266      *
267      * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
268      */

269     public void initialize(IManagedForm form) {
270         IFeatureModel model = (IFeatureModel) getPage().getModel();
271         if (model != null)
272             model.addModelChangedListener(this);
273         super.initialize(form);
274     }
275
276     private void limitTextWidth(FormEntry entry) {
277         GridData gd = (GridData) entry.getText().getLayoutData();
278         gd.widthHint = 30;
279     }
280
281     public void modelChanged(IModelChangedEvent e) {
282         markStale();
283     }
284
285     private void openPortabilityChoiceDialog(String JavaDoc property, FormEntry text,
286             Choice[] choices) {
287         String JavaDoc value = text.getValue();
288
289         PortabilityChoicesDialog dialog = new PortabilityChoicesDialog(
290                 PDEPlugin.getActiveWorkbenchShell(), choices, value);
291         dialog.create();
292         dialog.getShell().setText(PDEUIMessages.FeatureEditor_PortabilityChoicesDialog_title);
293
294         int result = dialog.open();
295         if (result == Window.OK) {
296             value = dialog.getValue();
297             text.setValue(value);
298             try {
299                 applyValue(property, value);
300             } catch (CoreException e) {
301                 PDEPlugin.logException(e);
302             }
303         }
304     }
305
306     public void refresh() {
307         setValue(IEnvironment.P_OS);
308         setValue(IEnvironment.P_WS);
309         setValue(IEnvironment.P_ARCH);
310         setValue(IEnvironment.P_NL);
311         super.refresh();
312     }
313
314     public void setFocus() {
315         if (fOsText != null)
316             fOsText.getText().setFocus();
317     }
318
319     private void setValue(String JavaDoc property) {
320         IFeatureModel model = (IFeatureModel) getPage().getModel();
321         IFeature feature = model.getFeature();
322         if (property.equals(IEnvironment.P_NL))
323             fNlText.setValue(feature.getNL(), true);
324         else if (property.equals(IEnvironment.P_OS))
325             fOsText.setValue(feature.getOS(), true);
326         else if (property.equals(IEnvironment.P_WS))
327             fWsText.setValue(feature.getWS(), true);
328         else if (property.equals(IEnvironment.P_ARCH))
329             fArchText.setValue(feature.getArch(), true);
330     }
331 }
332
Popular Tags