KickJava   Java API By Example, From Geeks To Geeks.

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


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

295     public void initialize(IManagedForm form) {
296         IFeatureModel model = (IFeatureModel) getPage().getModel();
297         if (model != null)
298             model.addModelChangedListener(this);
299         super.initialize(form);
300     }
301
302     private void limitTextWidth(FormEntry entry) {
303         GridData gd = (GridData) entry.getText().getLayoutData();
304         gd.widthHint = 30;
305     }
306
307     public void modelChanged(IModelChangedEvent e) {
308         markStale();
309     }
310
311     private void openPortabilityChoiceDialog(String JavaDoc property, FormEntry text,
312             Choice[] choices) {
313         String JavaDoc value = text.getValue();
314
315         PortabilityChoicesDialog dialog = new PortabilityChoicesDialog(
316                 PDEPlugin.getActiveWorkbenchShell(), choices, value);
317         dialog.create();
318         dialog.getShell().setText(PDEUIMessages.SiteEditor_PortabilityChoicesDialog_title);
319
320         int result = dialog.open();
321         if (result == Window.OK) {
322             value = dialog.getValue();
323             text.setValue(value);
324             try {
325                 applyValue(property, value);
326             } catch (CoreException e) {
327                 PDEPlugin.logException(e);
328             }
329         }
330     }
331
332     public void refresh() {
333         if (fCurrentInput == null) {
334             clearFields();
335         } else {
336             setValue(IEnvironment.P_OS);
337             setValue(IEnvironment.P_WS);
338             setValue(IEnvironment.P_ARCH);
339             setValue(IEnvironment.P_NL);
340         }
341
342         fOsText.setEditable(fCurrentInput != null && isEditable());
343         fWsText.setEditable(fCurrentInput != null && isEditable());
344         fNlText.setEditable(fCurrentInput != null && isEditable());
345         fArchText.setEditable(fCurrentInput != null && isEditable());
346
347         super.refresh();
348     }
349
350     public void selectionChanged(IFormPart part, ISelection selection) {
351         if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
352             Object JavaDoc o = ((IStructuredSelection) selection).getFirstElement();
353             if (o instanceof IFeatureChild) {
354                 fCurrentInput = (IFeatureChild) o;
355             } else {
356                 fCurrentInput = null;
357             }
358         } else
359             fCurrentInput = null;
360         refresh();
361     }
362
363     public void setFocus() {
364         if (fOsText != null)
365             fOsText.getText().setFocus();
366     }
367
368     private void setValue(String JavaDoc property) {
369         if (fCurrentInput == null) {
370             clearField(property);
371         } else {
372             if (property.equals(IEnvironment.P_NL))
373                 fNlText.setValue(fCurrentInput.getNL(), true);
374             else if (property.equals(IEnvironment.P_OS))
375                 fOsText.setValue(fCurrentInput.getOS(), true);
376             else if (property.equals(IEnvironment.P_WS))
377                 fWsText.setValue(fCurrentInput.getWS(), true);
378             else if (property.equals(IEnvironment.P_ARCH))
379                 fArchText.setValue(fCurrentInput.getArch(), true);
380         }
381     }
382 }
383
Popular Tags