KickJava   Java API By Example, From Geeks To Geeks.

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


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.IFeatureData;
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 DataPortabilitySection extends PDESection implements IFormPart,
48         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 IFeatureData fCurrentInput;
83
84     private FormEntry fNlText;
85
86     private FormEntry fOsText;
87
88     private FormEntry fWsText;
89
90     public DataPortabilitySection(PDEFormPage page, Composite parent) {
91         this(page, parent, PDEUIMessages.FeatureEditor_DataDetailsSection_title,
92                 PDEUIMessages.FeatureEditor_DataDetailsSection_desc, SWT.NULL);
93     }
94
95     public DataPortabilitySection(PDEFormPage page, Composite parent,
96             String JavaDoc title, String JavaDoc desc, int toggleStyle) {
97         super(page, parent, Section.DESCRIPTION | ExpandableComposite.NO_TITLE
98                 | toggleStyle, false);
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 (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_BOTH);
168         section.setLayoutData(data);
169         
170         Composite container = toolkit.createComposite(section);
171         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
172         container.setLayoutData(new GridData(GridData.FILL_BOTH));
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(fCurrentInput !=null && 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(fCurrentInput !=null && 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(fCurrentInput !=null && 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(fCurrentInput !=null && isEditable());
277
278         toolkit.paintBordersFor(container);
279         section.setClient(container);
280     }
281
282     public void dispose() {
283         IFeatureModel model = (IFeatureModel) 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         IFeatureModel model = (IFeatureModel) 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 (fCurrentInput == null) {
333             clearFields();
334         } else {
335             setValue(IEnvironment.P_OS);
336             setValue(IEnvironment.P_WS);
337             setValue(IEnvironment.P_ARCH);
338             setValue(IEnvironment.P_NL);
339         }
340
341         fOsText.setEditable(fCurrentInput != null && isEditable());
342         fWsText.setEditable(fCurrentInput != null && isEditable());
343         fNlText.setEditable(fCurrentInput != null && isEditable());
344         fArchText.setEditable(fCurrentInput != null && isEditable());
345
346         super.refresh();
347     }
348
349     public void selectionChanged(IFormPart part, ISelection selection) {
350         if (selection instanceof IStructuredSelection && ((IStructuredSelection) selection).size() == 1) {
351             Object JavaDoc o = ((IStructuredSelection) selection).getFirstElement();
352             if (o instanceof IFeatureData) {
353                 fCurrentInput = (IFeatureData) o;
354             } else {
355                 fCurrentInput = null;
356             }
357         } else
358             fCurrentInput = null;
359         refresh();
360     }
361
362     public void setFocus() {
363         if (fOsText != null)
364             fOsText.getText().setFocus();
365     }
366
367     private void setValue(String JavaDoc property) {
368         if (fCurrentInput == null) {
369             clearField(property);
370         } else {
371             if (property.equals(IEnvironment.P_NL))
372                 fNlText.setValue(fCurrentInput.getNL(), true);
373             else if (property.equals(IEnvironment.P_OS))
374                 fOsText.setValue(fCurrentInput.getOS(), true);
375             else if (property.equals(IEnvironment.P_WS))
376                 fWsText.setValue(fCurrentInput.getWS(), true);
377             else if (property.equals(IEnvironment.P_ARCH))
378                 fArchText.setValue(fCurrentInput.getArch(), true);
379         }
380     }
381 }
382
Popular Tags