001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.feature;
011:
012: import java.util.Locale;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.window.Window;
019: import org.eclipse.pde.core.IModelChangedEvent;
020: import org.eclipse.pde.internal.core.ifeature.IEnvironment;
021: import org.eclipse.pde.internal.core.ifeature.IFeatureData;
022: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
023: import org.eclipse.pde.internal.ui.PDEPlugin;
024: import org.eclipse.pde.internal.ui.PDEUIMessages;
025: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
026: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
027: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
028: import org.eclipse.pde.internal.ui.editor.PDESection;
029: import org.eclipse.pde.internal.ui.parts.FormEntry;
030: import org.eclipse.swt.SWT;
031: import org.eclipse.swt.custom.BusyIndicator;
032: import org.eclipse.swt.dnd.Clipboard;
033: import org.eclipse.swt.dnd.RTFTransfer;
034: import org.eclipse.swt.dnd.TextTransfer;
035: import org.eclipse.swt.dnd.Transfer;
036: import org.eclipse.swt.dnd.TransferData;
037: import org.eclipse.swt.layout.GridData;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.ui.forms.IFormPart;
040: import org.eclipse.ui.forms.IManagedForm;
041: import org.eclipse.ui.forms.IPartSelectionListener;
042: import org.eclipse.ui.forms.widgets.ExpandableComposite;
043: import org.eclipse.ui.forms.widgets.FormToolkit;
044: import org.eclipse.ui.forms.widgets.Section;
045:
046: public class DataPortabilitySection extends PDESection implements
047: IFormPart, IPartSelectionListener {
048: public static Choice[] getArchChoices() {
049: return getKnownChoices(Platform.knownOSArchValues());
050: }
051:
052: private static Choice[] getKnownChoices(String[] values) {
053: Choice[] choices = new Choice[values.length];
054: for (int i = 0; i < choices.length; i++) {
055: choices[i] = new Choice(values[i], values[i]);
056: }
057: return choices;
058: }
059:
060: public static Choice[] getNLChoices() {
061: Locale[] locales = Locale.getAvailableLocales();
062: Choice[] choices = new Choice[locales.length];
063: for (int i = 0; i < locales.length; i++) {
064: Locale locale = locales[i];
065: choices[i] = new Choice(locale.toString(), locale
066: .toString()
067: + " - " + locale.getDisplayName()); //$NON-NLS-1$
068: }
069: return choices;
070: }
071:
072: public static Choice[] getOSChoices() {
073: return getKnownChoices(Platform.knownOSValues());
074: }
075:
076: public static Choice[] getWSChoices() {
077: return getKnownChoices(Platform.knownWSValues());
078: }
079:
080: private FormEntry fArchText;
081:
082: private IFeatureData fCurrentInput;
083:
084: private FormEntry fNlText;
085:
086: private FormEntry fOsText;
087:
088: private FormEntry fWsText;
089:
090: public DataPortabilitySection(PDEFormPage page, Composite parent) {
091: this (page, parent,
092: PDEUIMessages.FeatureEditor_DataDetailsSection_title,
093: PDEUIMessages.FeatureEditor_DataDetailsSection_desc,
094: SWT.NULL);
095: }
096:
097: public DataPortabilitySection(PDEFormPage page, Composite parent,
098: String title, String desc, int toggleStyle) {
099: super (page, parent, Section.DESCRIPTION
100: | ExpandableComposite.NO_TITLE | toggleStyle, false);
101: // getSection().setText(title);
102: getSection().setDescription(desc);
103: createClient(getSection(), page.getManagedForm().getToolkit());
104: }
105:
106: private void applyValue(String property, String value)
107: throws CoreException {
108: if (fCurrentInput == null)
109: return;
110: if (property.equals(IEnvironment.P_NL))
111: fCurrentInput.setNL(value);
112: else if (property.equals(IEnvironment.P_OS))
113: fCurrentInput.setOS(value);
114: else if (property.equals(IEnvironment.P_WS))
115: fCurrentInput.setWS(value);
116: else if (property.equals(IEnvironment.P_ARCH))
117: fCurrentInput.setArch(value);
118: }
119:
120: public void cancelEdit() {
121: fOsText.cancelEdit();
122: fWsText.cancelEdit();
123: fNlText.cancelEdit();
124: fArchText.cancelEdit();
125: super .cancelEdit();
126: }
127:
128: public boolean canPaste(Clipboard clipboard) {
129: TransferData[] types = clipboard.getAvailableTypes();
130: Transfer[] transfers = new Transfer[] {
131: TextTransfer.getInstance(), RTFTransfer.getInstance() };
132: for (int i = 0; i < types.length; i++) {
133: for (int j = 0; j < transfers.length; j++) {
134: if (transfers[j].isSupportedType(types[i]))
135: return true;
136: }
137: }
138: return false;
139: }
140:
141: private void clearField(String property) {
142: if (property.equals(IEnvironment.P_OS))
143: fOsText.setValue(null, true);
144: else if (property.equals(IEnvironment.P_WS))
145: fWsText.setValue(null, true);
146: else if (property.equals(IEnvironment.P_ARCH))
147: fArchText.setValue(null, true);
148: else if (property.equals(IEnvironment.P_NL))
149: fNlText.setValue(null, true);
150: }
151:
152: private void clearFields() {
153: fOsText.setValue(null, true);
154: fWsText.setValue(null, true);
155: fNlText.setValue(null, true);
156: fArchText.setValue(null, true);
157: }
158:
159: public void commit(boolean onSave) {
160: fOsText.commit();
161: fWsText.commit();
162: fNlText.commit();
163: fArchText.commit();
164: super .commit(onSave);
165: }
166:
167: public void createClient(Section section, FormToolkit toolkit) {
168:
169: section.setLayout(FormLayoutFactory.createClearGridLayout(
170: false, 1));
171: GridData data = new GridData(GridData.FILL_BOTH);
172: section.setLayoutData(data);
173:
174: Composite container = toolkit.createComposite(section);
175: container.setLayout(FormLayoutFactory
176: .createSectionClientGridLayout(false, 3));
177: container.setLayoutData(new GridData(GridData.FILL_BOTH));
178:
179: String editLabel = PDEUIMessages.SiteEditor_PortabilitySection_edit;
180:
181: fOsText = new FormEntry(container, toolkit,
182: PDEUIMessages.SiteEditor_PortabilitySection_os,
183: editLabel, false);
184: fOsText.setFormEntryListener(new FormEntryAdapter(this ) {
185:
186: public void browseButtonSelected(FormEntry entry) {
187: BusyIndicator.showWhile(fOsText.getText().getDisplay(),
188: new Runnable() {
189: public void run() {
190: Choice[] choices = getOSChoices();
191: openPortabilityChoiceDialog(
192: IEnvironment.P_OS, fOsText,
193: choices);
194: }
195: });
196: }
197:
198: public void textValueChanged(FormEntry text) {
199: try {
200: applyValue(IEnvironment.P_OS, text.getValue());
201: } catch (CoreException e) {
202: PDEPlugin.logException(e);
203: }
204: }
205: });
206: limitTextWidth(fOsText);
207: fOsText.setEditable(fCurrentInput != null && isEditable());
208:
209: fWsText = new FormEntry(container, toolkit,
210: PDEUIMessages.SiteEditor_PortabilitySection_ws,
211: editLabel, false);
212: fWsText.setFormEntryListener(new FormEntryAdapter(this ) {
213:
214: public void browseButtonSelected(FormEntry entry) {
215: BusyIndicator.showWhile(fWsText.getText().getDisplay(),
216: new Runnable() {
217: public void run() {
218: Choice[] choices = getWSChoices();
219: openPortabilityChoiceDialog(
220: IEnvironment.P_WS, fWsText,
221: choices);
222: }
223: });
224: }
225:
226: public void textValueChanged(FormEntry text) {
227: try {
228: applyValue(IEnvironment.P_WS, text.getValue());
229: } catch (CoreException e) {
230: PDEPlugin.logException(e);
231: }
232: }
233: });
234: limitTextWidth(fWsText);
235: fWsText.setEditable(fCurrentInput != null && isEditable());
236:
237: fNlText = new FormEntry(container, toolkit,
238: PDEUIMessages.SiteEditor_PortabilitySection_nl,
239: editLabel, false);
240:
241: fNlText.setFormEntryListener(new FormEntryAdapter(this ) {
242:
243: public void browseButtonSelected(FormEntry entry) {
244: BusyIndicator.showWhile(fNlText.getText().getDisplay(),
245: new Runnable() {
246: public void run() {
247: Choice[] choices = getNLChoices();
248: openPortabilityChoiceDialog(
249: IEnvironment.P_NL, fNlText,
250: choices);
251: }
252: });
253: }
254:
255: public void textValueChanged(FormEntry text) {
256: try {
257: applyValue(IEnvironment.P_NL, text.getValue());
258: } catch (CoreException e) {
259: PDEPlugin.logException(e);
260: }
261: }
262: });
263: limitTextWidth(fNlText);
264: fNlText.setEditable(fCurrentInput != null && isEditable());
265:
266: fArchText = new FormEntry(container, toolkit,
267: PDEUIMessages.SiteEditor_PortabilitySection_arch,
268: editLabel, false);
269: fArchText.setFormEntryListener(new FormEntryAdapter(this ) {
270:
271: public void browseButtonSelected(FormEntry entry) {
272: BusyIndicator.showWhile(fArchText.getText()
273: .getDisplay(), new Runnable() {
274: public void run() {
275: Choice[] choices = getArchChoices();
276: openPortabilityChoiceDialog(
277: IEnvironment.P_ARCH, fArchText, choices);
278: }
279: });
280: }
281:
282: public void textValueChanged(FormEntry text) {
283: try {
284: applyValue(IEnvironment.P_ARCH, text.getValue());
285: } catch (CoreException e) {
286: PDEPlugin.logException(e);
287: }
288: }
289:
290: });
291: limitTextWidth(fArchText);
292: fArchText.setEditable(fCurrentInput != null && isEditable());
293:
294: toolkit.paintBordersFor(container);
295: section.setClient(container);
296: }
297:
298: public void dispose() {
299: IFeatureModel model = (IFeatureModel) getPage().getModel();
300: if (model != null)
301: model.removeModelChangedListener(this );
302: super .dispose();
303: }
304:
305: /*
306: * (non-Javadoc)
307: *
308: * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
309: */
310: public void initialize(IManagedForm form) {
311: IFeatureModel model = (IFeatureModel) getPage().getModel();
312: if (model != null)
313: model.addModelChangedListener(this );
314: super .initialize(form);
315: }
316:
317: private void limitTextWidth(FormEntry entry) {
318: GridData gd = (GridData) entry.getText().getLayoutData();
319: gd.widthHint = 30;
320: }
321:
322: public void modelChanged(IModelChangedEvent e) {
323: markStale();
324: }
325:
326: private void openPortabilityChoiceDialog(String property,
327: FormEntry text, Choice[] choices) {
328: String value = text.getValue();
329:
330: PortabilityChoicesDialog dialog = new PortabilityChoicesDialog(
331: PDEPlugin.getActiveWorkbenchShell(), choices, value);
332: dialog.create();
333: dialog
334: .getShell()
335: .setText(
336: PDEUIMessages.SiteEditor_PortabilityChoicesDialog_title);
337:
338: int result = dialog.open();
339: if (result == Window.OK) {
340: value = dialog.getValue();
341: text.setValue(value);
342: try {
343: applyValue(property, value);
344: } catch (CoreException e) {
345: PDEPlugin.logException(e);
346: }
347: }
348: }
349:
350: public void refresh() {
351: if (fCurrentInput == null) {
352: clearFields();
353: } else {
354: setValue(IEnvironment.P_OS);
355: setValue(IEnvironment.P_WS);
356: setValue(IEnvironment.P_ARCH);
357: setValue(IEnvironment.P_NL);
358: }
359:
360: fOsText.setEditable(fCurrentInput != null && isEditable());
361: fWsText.setEditable(fCurrentInput != null && isEditable());
362: fNlText.setEditable(fCurrentInput != null && isEditable());
363: fArchText.setEditable(fCurrentInput != null && isEditable());
364:
365: super .refresh();
366: }
367:
368: public void selectionChanged(IFormPart part, ISelection selection) {
369: if (selection instanceof IStructuredSelection
370: && ((IStructuredSelection) selection).size() == 1) {
371: Object o = ((IStructuredSelection) selection)
372: .getFirstElement();
373: if (o instanceof IFeatureData) {
374: fCurrentInput = (IFeatureData) o;
375: } else {
376: fCurrentInput = null;
377: }
378: } else
379: fCurrentInput = null;
380: refresh();
381: }
382:
383: public void setFocus() {
384: if (fOsText != null)
385: fOsText.getText().setFocus();
386: }
387:
388: private void setValue(String property) {
389: if (fCurrentInput == null) {
390: clearField(property);
391: } else {
392: if (property.equals(IEnvironment.P_NL))
393: fNlText.setValue(fCurrentInput.getNL(), true);
394: else if (property.equals(IEnvironment.P_OS))
395: fOsText.setValue(fCurrentInput.getOS(), true);
396: else if (property.equals(IEnvironment.P_WS))
397: fWsText.setValue(fCurrentInput.getWS(), true);
398: else if (property.equals(IEnvironment.P_ARCH))
399: fArchText.setValue(fCurrentInput.getArch(), true);
400: }
401: }
402: }
|