KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > product > ProductInfoSection


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.product;
12
13 import org.eclipse.jface.action.IStatusLineManager;
14 import org.eclipse.jface.window.Window;
15 import org.eclipse.jface.wizard.WizardDialog;
16 import org.eclipse.pde.core.IModelChangedEvent;
17 import org.eclipse.pde.core.plugin.TargetPlatform;
18 import org.eclipse.pde.internal.core.iproduct.IProduct;
19 import org.eclipse.pde.internal.core.iproduct.IProductModel;
20 import org.eclipse.pde.internal.ui.PDEPlugin;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
23 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
24 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
25 import org.eclipse.pde.internal.ui.editor.PDESection;
26 import org.eclipse.pde.internal.ui.parts.ComboPart;
27 import org.eclipse.pde.internal.ui.parts.FormEntry;
28 import org.eclipse.pde.internal.ui.wizards.product.ProductDefinitionWizard;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.dnd.Clipboard;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.ui.IEditorSite;
43 import org.eclipse.ui.forms.IFormColors;
44 import org.eclipse.ui.forms.editor.IFormPage;
45 import org.eclipse.ui.forms.events.HyperlinkEvent;
46 import org.eclipse.ui.forms.events.IHyperlinkListener;
47 import org.eclipse.ui.forms.widgets.FormText;
48 import org.eclipse.ui.forms.widgets.FormToolkit;
49 import org.eclipse.ui.forms.widgets.Section;
50 import org.eclipse.ui.forms.widgets.TableWrapData;
51
52
53 public class ProductInfoSection extends PDESection {
54
55     private FormEntry fNameEntry;
56     private ComboPart fAppCombo;
57     private ComboPart fProductCombo;
58     private Button fPluginButton;
59     private Button fFeatureButton;
60     
61     private static int NUM_COLUMNS = 3;
62
63     public ProductInfoSection(PDEFormPage page, Composite parent) {
64         super(page, parent, Section.DESCRIPTION);
65         createClient(getSection(), page.getEditor().getToolkit());
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
70      */

71     protected void createClient(Section section, FormToolkit toolkit) {
72         section.setText(PDEUIMessages.ProductInfoSection_title);
73         section.setDescription(PDEUIMessages.ProductInfoSection_desc);
74         section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1));
75         TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB);
76         data.colspan = 2;
77         section.setLayoutData(data);
78         
79         Composite client = toolkit.createComposite(section);
80         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, NUM_COLUMNS));
81
82         IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();
83         
84         createNameEntry(client, toolkit, actionBars);
85         createIdEntry(client, toolkit, actionBars);
86         createApplicationEntry(client, toolkit, actionBars);
87         createConfigurationOption(client, toolkit);
88         
89         toolkit.paintBordersFor(client);
90         section.setClient(client);
91         
92         getModel().addModelChangedListener(this);
93     }
94     
95     public void dispose() {
96         IProductModel model = getModel();
97         if (model != null)
98             model.removeModelChangedListener(this);
99         super.dispose();
100     }
101     
102     private void createNameEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) {
103         createLabel(client, toolkit, PDEUIMessages.ProductInfoSection_titleLabel);
104
105         fNameEntry = new FormEntry(client, toolkit, PDEUIMessages.ProductInfoSection_productname, null, false);
106         fNameEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
107             public void textValueChanged(FormEntry entry) {
108                 getProduct().setName(entry.getValue().trim());
109             }
110         });
111         fNameEntry.setEditable(isEditable());
112     }
113     
114     private void createIdEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) {
115         createLabel(client, toolkit, ""); //$NON-NLS-1$
116
createLabel(client, toolkit, PDEUIMessages.ProductInfoSection_prodIdLabel);
117
118         Label label = toolkit.createLabel(client, PDEUIMessages.ProductInfoSection_id);
119         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
120         
121         fProductCombo = new ComboPart();
122         fProductCombo.createControl(client, toolkit, SWT.READ_ONLY);
123         fProductCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
124         fProductCombo.setItems(TargetPlatform.getProducts());
125         fProductCombo.add(""); //$NON-NLS-1$
126
fProductCombo.addSelectionListener(new SelectionAdapter() {
127             public void widgetSelected(SelectionEvent e) {
128                 getProduct().setId(fProductCombo.getSelection());
129             }
130         });
131         
132         Button button = toolkit.createButton(client, PDEUIMessages.ProductInfoSection_new, SWT.PUSH);
133         button.setEnabled(isEditable());
134         button.addSelectionListener(new SelectionAdapter() {
135             public void widgetSelected(SelectionEvent e) {
136                 handleNewDefinition();
137             }
138         });
139         fProductCombo.getControl().setEnabled(isEditable());
140     }
141
142     private void handleNewDefinition() {
143         ProductDefinitionWizard wizard = new ProductDefinitionWizard(getProduct());
144         WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard);
145         dialog.create();
146         if (dialog.open() == Window.OK) {
147             String JavaDoc id = wizard.getProductId();
148             IProduct product = getProduct();
149             product.setId(id);
150             product.setApplication(wizard.getApplication());
151         }
152     }
153
154     private void createApplicationEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) {
155         createLabel(client, toolkit, ""); //$NON-NLS-1$
156
createLabel(client, toolkit, PDEUIMessages.ProductInfoSection_appLabel);
157         
158         Label label = toolkit.createLabel(client, PDEUIMessages.ProductInfoSection_app, SWT.WRAP);
159         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
160         
161         fAppCombo = new ComboPart();
162         fAppCombo.createControl(client, toolkit, SWT.READ_ONLY);
163         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
164         gd.horizontalSpan = NUM_COLUMNS - 1;
165         fAppCombo.getControl().setLayoutData(gd);
166         fAppCombo.setItems(TargetPlatform.getApplications());
167         fAppCombo.add(""); //$NON-NLS-1$
168
fAppCombo.addSelectionListener(new SelectionAdapter() {
169             public void widgetSelected(SelectionEvent e) {
170                 getProduct().setApplication(fAppCombo.getSelection());
171             }
172         });
173         
174         fAppCombo.getControl().setEnabled(isEditable());
175     }
176     
177     private void createConfigurationOption(Composite client, FormToolkit toolkit) {
178         createLabel(client, toolkit, ""); //$NON-NLS-1$
179

180         Composite comp = toolkit.createComposite(client);
181         GridLayout layout = new GridLayout(3, false);
182         layout.marginWidth = layout.marginHeight = 0;
183         comp.setLayout(layout);
184         GridData gd = new GridData();
185         gd.horizontalSpan = 3;
186         comp.setLayoutData(gd);
187         
188         FormText text = toolkit.createFormText(comp, true);
189         text.setText(PDEUIMessages.Product_overview_configuration, true, true);
190         text.addHyperlinkListener(new IHyperlinkListener() {
191             public void linkEntered(HyperlinkEvent e) {
192                 getStatusLineManager().setMessage(e.getLabel());
193             }
194             public void linkExited(HyperlinkEvent e) {
195                 getStatusLineManager().setMessage(null);
196             }
197             public void linkActivated(HyperlinkEvent e) {
198                 String JavaDoc pageId = fPluginButton.getSelection() ? ConfigurationPage.PLUGIN_ID : ConfigurationPage.FEATURE_ID;
199                 getPage().getEditor().setActivePage(pageId);
200             }
201         });
202         
203         fPluginButton = toolkit.createButton(comp, PDEUIMessages.ProductInfoSection_plugins, SWT.RADIO);
204         gd = new GridData();
205         gd.horizontalIndent = 25;
206         fPluginButton.setLayoutData(gd);
207         fPluginButton.setEnabled(isEditable());
208         fPluginButton.addSelectionListener(new SelectionAdapter() {
209             public void widgetSelected(SelectionEvent e) {
210                 boolean selected = fPluginButton.getSelection();
211                 IProduct product = getProduct();
212                 if (selected == product.useFeatures()) {
213                     product.setUseFeatures(!selected);
214                     ((ProductEditor)getPage().getEditor()).updateConfigurationPage();
215                 }
216             }
217         });
218         
219         fFeatureButton = toolkit.createButton(comp, PDEUIMessages.ProductInfoSection_features, SWT.RADIO);
220         gd = new GridData();
221         gd.horizontalIndent = 25;
222         fFeatureButton.setLayoutData(gd);
223         fFeatureButton.setEnabled(isEditable());
224     }
225     
226     private void createLabel(Composite client, FormToolkit toolkit, String JavaDoc text) {
227         Label label = toolkit.createLabel(client, text, SWT.WRAP);
228         GridData gd = new GridData();
229         gd.horizontalSpan = NUM_COLUMNS;
230         label.setLayoutData(gd);
231     }
232     
233     /* (non-Javadoc)
234      * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
235      */

236     public void commit(boolean onSave) {
237         fNameEntry.commit();
238         super.commit(onSave);
239     }
240     
241     /* (non-Javadoc)
242      * @see org.eclipse.pde.internal.ui.editor.PDESection#cancelEdit()
243      */

244     public void cancelEdit() {
245         fNameEntry.cancelEdit();
246         super.cancelEdit();
247     }
248     
249     private IProductModel getModel() {
250         return (IProductModel) getPage().getPDEEditor().getAggregateModel();
251     }
252     
253     private IProduct getProduct() {
254         return getModel().getProduct();
255     }
256     
257     /* (non-Javadoc)
258      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
259      */

260     public void refresh() {
261         IProduct product = getProduct();
262         fNameEntry.setValue(product.getName(), true);
263         refreshProductCombo(product.getId());
264         fAppCombo.setText(product.getApplication());
265         fPluginButton.setSelection(!product.useFeatures());
266         fFeatureButton.setSelection(product.useFeatures());
267         super.refresh();
268     }
269     
270     public void modelChanged(IModelChangedEvent e) {
271         // No need to call super, handling world changed event here
272
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
273             handleModelEventWorldChanged(e);
274             return;
275         }
276         
277         String JavaDoc prop = e.getChangedProperty();
278         if (prop == null)
279             return;
280         if (prop.equals(IProduct.P_ID)) {
281             refreshProductCombo(e.getNewValue().toString());
282         } else if (prop.equals(IProduct.P_NAME)) {
283             fNameEntry.setValue(e.getNewValue().toString(), true);
284         } else if (prop.equals(IProduct.P_APPLICATION)) {
285             fAppCombo.setText(e.getNewValue().toString());
286         }
287     }
288     
289     /**
290      * @param event
291      */

292     private void handleModelEventWorldChanged(IModelChangedEvent event) {
293         // Store selection before refresh
294
boolean previousFeatureSelected = fFeatureButton.getSelection();
295         // Perform the refresh
296
refresh();
297         // Note: A deferred selection event is fired from radio buttons when
298
// their value is toggled, the user switches to another page, and the
299
// user switches back to the same page containing the radio buttons
300
// This appears to be a result of a SWT bug.
301
// If the radio button is the last widget to have focus when leaving
302
// the page, an event will be fired when entering the page again.
303
// An event is not fired if the radio button does not have focus.
304
// The solution is to redirect focus to a stable widget.
305
getPage().setLastFocusControl(fNameEntry.getText());
306         // Revert the configuration page if necessary
307
revertConfigurationPage(previousFeatureSelected);
308     }
309     
310     /**
311      * @param previousFeatureSelected
312      */

313     private void revertConfigurationPage(boolean previousFeatureSelected) {
314         // Compare selection from before and after the refresh
315
boolean currentFeatureSelected = fFeatureButton.getSelection();
316         if (previousFeatureSelected == currentFeatureSelected) {
317             // No update required
318
return;
319         }
320         // The configuration page needs to be updated
321
IFormPage currentPage = getPage().getEditor().getActivePageInstance();
322         // If the current page is the configuration page, switch to the
323
// overview page before doing the update; otherwise, widget disposed
324
// errors may result
325
if (currentPage instanceof ConfigurationPage) {
326             getPage().getEditor().setActivePage(OverviewPage.PAGE_ID);
327         }
328         ((ProductEditor)getPage().getEditor()).updateConfigurationPage();
329     }
330     
331     /**
332      * @param productId
333      */

334     private void refreshProductCombo(String JavaDoc productID) {
335         if (productID == null) {
336             productID = ""; //$NON-NLS-1$
337
} else if (fProductCombo.indexOf(productID) == -1) {
338             fProductCombo.add(productID, 0);
339         }
340         fProductCombo.setText(productID);
341     }
342     
343     private IStatusLineManager getStatusLineManager() {
344         IEditorSite site = getPage().getEditor().getEditorSite();
345         return site.getActionBars().getStatusLineManager();
346     }
347     
348     public boolean canPaste(Clipboard clipboard) {
349         Display d = getSection().getDisplay();
350         Control c = d.getFocusControl();
351         if (c instanceof Text)
352             return true;
353         return false;
354     }
355 }
356
Popular Tags