KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > 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.site;
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.isite.ISiteFeature;
23 import org.eclipse.pde.internal.core.isite.ISiteModel;
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.editor.feature.Choice;
31 import org.eclipse.pde.internal.ui.editor.feature.PortabilityChoicesDialog;
32 import org.eclipse.pde.internal.ui.parts.FormEntry;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.custom.BusyIndicator;
35 import org.eclipse.swt.dnd.Clipboard;
36 import org.eclipse.swt.dnd.RTFTransfer;
37 import org.eclipse.swt.dnd.TextTransfer;
38 import org.eclipse.swt.dnd.Transfer;
39 import org.eclipse.swt.dnd.TransferData;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.ui.forms.IFormPart;
43 import org.eclipse.ui.forms.IManagedForm;
44 import org.eclipse.ui.forms.IPartSelectionListener;
45 import org.eclipse.ui.forms.widgets.FormToolkit;
46 import org.eclipse.ui.forms.widgets.Section;
47
48 public class PortabilitySection extends PDESection implements IFormPart,
49         IPartSelectionListener {
50     public static Choice[] getArchChoices() {
51         return getKnownChoices(Platform.knownOSArchValues());
52     }
53
54     private static Choice[] getKnownChoices(String JavaDoc[] values) {
55         Choice[] choices = new Choice[values.length];
56         for (int i = 0; i < choices.length; i++) {
57             choices[i] = new Choice(values[i], values[i]);
58         }
59         return choices;
60     }
61
62     public static Choice[] getNLChoices() {
63         Locale JavaDoc[] locales = Locale.getAvailableLocales();
64         Choice[] choices = new Choice[locales.length];
65         for (int i = 0; i < locales.length; i++) {
66             Locale JavaDoc locale = locales[i];
67             choices[i] = new Choice(locale.toString(), locale.toString()
68                     + " - " + locale.getDisplayName()); //$NON-NLS-1$
69
}
70         return choices;
71     }
72
73     public static Choice[] getOSChoices() {
74         return getKnownChoices(Platform.knownOSValues());
75     }
76
77     public static Choice[] getWSChoices() {
78         return getKnownChoices(Platform.knownWSValues());
79     }
80
81     private FormEntry fArchText;
82
83     private ISiteFeature fCurrentSiteFeature;
84
85     private FormEntry fNlText;
86
87     private FormEntry fOsText;
88
89     private FormEntry fWsText;
90
91     public PortabilitySection(PDEFormPage page, Composite parent) {
92         this(page, parent, PDEUIMessages.SiteEditor_PortabilitySection_title,
93                 PDEUIMessages.SiteEditor_PortabilitySection_desc, SWT.NULL);
94     }
95
96     public PortabilitySection(PDEFormPage page, Composite parent, String JavaDoc title,
97             String JavaDoc desc, int toggleStyle) {
98         super(page, parent, Section.DESCRIPTION | toggleStyle);
99         getSection().setText(title);
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 (fCurrentSiteFeature == null)
106             return;
107         if (property.equals(IEnvironment.P_NL))
108             fCurrentSiteFeature.setNL(value);
109         else if (property.equals(IEnvironment.P_OS))
110             fCurrentSiteFeature.setOS(value);
111         else if (property.equals(IEnvironment.P_WS))
112             fCurrentSiteFeature.setWS(value);
113         else if (property.equals(IEnvironment.P_ARCH))
114             fCurrentSiteFeature.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     }
146
147     private void clearFields() {
148         fOsText.setValue(null, true);
149         fWsText.setValue(null, true);
150         if (fNlText != null)
151             fNlText.setValue(null, true);
152         fArchText.setValue(null, true);
153     }
154
155     public void commit(boolean onSave) {
156         fOsText.commit();
157         fWsText.commit();
158         if (fNlText != null)
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         Composite container = toolkit.createComposite(section);
168         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
169         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
170
171         GridData data = new GridData(GridData.FILL_BOTH);
172         section.setLayoutData(data);
173
174         String JavaDoc editLabel = PDEUIMessages.SiteEditor_PortabilitySection_edit;
175
176         fOsText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_PortabilitySection_os, editLabel, false);
177         fOsText.setFormEntryListener(new FormEntryAdapter(this) {
178
179             public void browseButtonSelected(FormEntry entry) {
180                 BusyIndicator.showWhile(fOsText.getText().getDisplay(),
181                         new Runnable JavaDoc() {
182                             public void run() {
183                                 Choice[] choices = getOSChoices();
184                                 openPortabilityChoiceDialog(IEnvironment.P_OS,
185                                         fOsText, choices);
186                             }
187                         });
188             }
189
190             public void textValueChanged(FormEntry text) {
191                 try {
192                     applyValue(IEnvironment.P_OS, text.getValue());
193                 } catch (CoreException e) {
194                     PDEPlugin.logException(e);
195                 }
196             }
197         });
198         limitTextWidth(fOsText);
199         fOsText.setEditable(isEditable());
200
201         fWsText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_PortabilitySection_ws, editLabel, false);
202         fWsText.setFormEntryListener(new FormEntryAdapter(this) {
203
204             public void browseButtonSelected(FormEntry entry) {
205                 BusyIndicator.showWhile(fWsText.getText().getDisplay(),
206                         new Runnable JavaDoc() {
207                             public void run() {
208                                 Choice[] choices = getWSChoices();
209                                 openPortabilityChoiceDialog(IEnvironment.P_WS,
210                                         fWsText, choices);
211                             }
212                         });
213             }
214
215             public void textValueChanged(FormEntry text) {
216                 try {
217                     applyValue(IEnvironment.P_WS, text.getValue());
218                 } catch (CoreException e) {
219                     PDEPlugin.logException(e);
220                 }
221             }
222         });
223         limitTextWidth(fWsText);
224         fWsText.setEditable(isEditable());
225
226         fNlText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_PortabilitySection_nl, editLabel, false);
227
228         fNlText.setFormEntryListener(new FormEntryAdapter(this) {
229
230             public void browseButtonSelected(FormEntry entry) {
231                 BusyIndicator.showWhile(fNlText.getText().getDisplay(),
232                         new Runnable JavaDoc() {
233                             public void run() {
234                                 Choice[] choices = getNLChoices();
235                                 openPortabilityChoiceDialog(IEnvironment.P_NL,
236                                         fNlText, choices);
237                             }
238                         });
239             }
240
241             public void textValueChanged(FormEntry text) {
242                 try {
243                     applyValue(IEnvironment.P_NL, text.getValue());
244                 } catch (CoreException e) {
245                     PDEPlugin.logException(e);
246                 }
247             }
248         });
249         limitTextWidth(fNlText);
250         fNlText.setEditable(isEditable());
251
252         fArchText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_PortabilitySection_arch, editLabel, false);
253         fArchText.setFormEntryListener(new FormEntryAdapter(this) {
254
255             public void browseButtonSelected(FormEntry entry) {
256                 BusyIndicator.showWhile(fArchText.getText().getDisplay(),
257                         new Runnable JavaDoc() {
258                             public void run() {
259                                 Choice[] choices = getArchChoices();
260                                 openPortabilityChoiceDialog(
261                                         IEnvironment.P_ARCH, fArchText, choices);
262                             }
263                         });
264             }
265
266             public void textValueChanged(FormEntry text) {
267                 try {
268                     applyValue(IEnvironment.P_ARCH, text.getValue());
269                 } catch (CoreException e) {
270                     PDEPlugin.logException(e);
271                 }
272             }
273
274         });
275         limitTextWidth(fArchText);
276         fArchText.setEditable(isEditable());
277
278         toolkit.paintBordersFor(container);
279         section.setClient(container);
280     }
281
282     public void dispose() {
283         ISiteModel model = (ISiteModel) getPage().getModel();
284         if (model != null)
285             model.removeModelChangedListener(this);
286         super.dispose();
287     }
288
289     /*
290      * (non-Javadoc)
291      *
292      * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
293      */

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