KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.TreeSet JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.window.Window;
21 import org.eclipse.jface.wizard.WizardDialog;
22 import org.eclipse.pde.core.IBaseModel;
23 import org.eclipse.pde.core.IModelChangedEvent;
24 import org.eclipse.pde.core.plugin.IPluginElement;
25 import org.eclipse.pde.core.plugin.IPluginExtension;
26 import org.eclipse.pde.core.plugin.IPluginModelBase;
27 import org.eclipse.pde.core.plugin.IPluginObject;
28 import org.eclipse.pde.core.plugin.PluginRegistry;
29 import org.eclipse.pde.internal.core.TargetPlatformHelper;
30 import org.eclipse.pde.internal.core.ibundle.IBundle;
31 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
32 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
33 import org.eclipse.pde.internal.core.iproduct.IIntroInfo;
34 import org.eclipse.pde.internal.core.iproduct.IProduct;
35 import org.eclipse.pde.internal.core.iproduct.IProductModel;
36 import org.eclipse.pde.internal.core.iproduct.IProductModelFactory;
37 import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
38 import org.eclipse.pde.internal.core.text.bundle.RequireBundleHeader;
39 import org.eclipse.pde.internal.core.text.bundle.RequireBundleObject;
40 import org.eclipse.pde.internal.ui.PDEPlugin;
41 import org.eclipse.pde.internal.ui.PDEUIMessages;
42 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
43 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
44 import org.eclipse.pde.internal.ui.editor.PDESection;
45 import org.eclipse.pde.internal.ui.parts.ComboPart;
46 import org.eclipse.pde.internal.ui.util.ModelModification;
47 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
48 import org.eclipse.pde.internal.ui.wizards.product.ProductIntroWizard;
49 import org.eclipse.swt.SWT;
50 import org.eclipse.swt.events.SelectionAdapter;
51 import org.eclipse.swt.events.SelectionEvent;
52 import org.eclipse.swt.layout.GridData;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Label;
56 import org.eclipse.ui.forms.IFormColors;
57 import org.eclipse.ui.forms.editor.IFormPage;
58 import org.eclipse.ui.forms.widgets.FormToolkit;
59 import org.eclipse.ui.forms.widgets.Section;
60 import org.osgi.framework.Constants;
61
62
63 public class IntroSection extends PDESection {
64
65     private ComboPart fIntroCombo;
66     private IFile fManifest;
67     private String JavaDoc[] fAvailableIntroIds;
68     private static final String JavaDoc INTRO_PLUGIN_ID = "org.eclipse.ui.intro"; //$NON-NLS-1$
69
private static final double NEW_INTRO_SUPPORT_VERSION = 3.1;
70
71     public IntroSection(PDEFormPage page, Composite parent) {
72         super(page, parent, Section.DESCRIPTION);
73         createClient(getSection(), page.getEditor().getToolkit());
74     }
75     
76     public void createClient(Section section, FormToolkit toolkit) {
77
78         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
79         GridData data = new GridData(GridData.FILL_HORIZONTAL);
80         section.setLayoutData(data);
81         
82         section.setText(PDEUIMessages.IntroSection_sectionText);
83         section.setDescription(PDEUIMessages.IntroSection_sectionDescription);
84         
85         boolean canCreateNew = TargetPlatformHelper.getTargetVersion() >= NEW_INTRO_SUPPORT_VERSION;
86         
87         Composite client = toolkit.createComposite(section);
88         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, canCreateNew ? 3 : 2));
89         client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
90         
91         Label label = toolkit.createLabel(client, PDEUIMessages.IntroSection_introLabel, SWT.WRAP);
92         GridData td = new GridData();
93         td.horizontalSpan = canCreateNew ? 3 : 2;
94         label.setLayoutData(td);
95         
96         Label introLabel = toolkit.createLabel(client, PDEUIMessages.IntroSection_introInput);
97         introLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
98         
99         fIntroCombo = new ComboPart();
100         fIntroCombo.createControl(client, toolkit, SWT.READ_ONLY);
101         td = new GridData(GridData.FILL_HORIZONTAL);
102         fIntroCombo.getControl().setLayoutData(td);
103         loadManifestAndIntroIds(false);
104         if (fAvailableIntroIds != null ) fIntroCombo.setItems(fAvailableIntroIds);
105         fIntroCombo.add(""); //$NON-NLS-1$
106
fIntroCombo.addSelectionListener(new SelectionAdapter() {
107             public void widgetSelected(SelectionEvent e) {
108                 handleSelection();
109             }
110         });
111         
112         if (canCreateNew) {
113             Button button = toolkit.createButton(client, PDEUIMessages.IntroSection_new, SWT.PUSH);
114             button.setEnabled(isEditable());
115             button.setLayoutData(new GridData(GridData.FILL));
116             button.addSelectionListener(new SelectionAdapter() {
117                 public void widgetSelected(SelectionEvent e) {
118                     handleNewIntro();
119                 }
120             });
121         }
122         
123         fIntroCombo.getControl().setEnabled(isEditable());
124         
125         toolkit.paintBordersFor(client);
126         section.setClient(client);
127         // Register to be notified when the model changes
128
getModel().addModelChangedListener(this);
129     }
130     
131     private void handleSelection() {
132         if (!productDefined()) {
133             fIntroCombo.setText(""); //$NON-NLS-1$
134
return;
135         }
136         getIntroInfo().setId(fIntroCombo.getSelection());
137         addDependenciesAndPlugins();
138     }
139
140     private void loadManifestAndIntroIds(boolean onlyLoadManifest) {
141         TreeSet JavaDoc result = new TreeSet JavaDoc();
142         String JavaDoc introId;
143         IPluginModelBase[] plugins = PluginRegistry.getActiveModels();
144         for (int i = 0; i < plugins.length; i++) {
145             IPluginExtension[] extensions = plugins[i].getPluginBase().getExtensions();
146             for (int j = 0; j < extensions.length; j++) {
147                 String JavaDoc point = extensions[j].getPoint();
148                 if (point != null && point.equals("org.eclipse.ui.intro")) {//$NON-NLS-1$
149
IPluginObject[] children = extensions[j].getChildren();
150                     for (int k = 0; k < children.length; k++) {
151                         IPluginElement element = (IPluginElement)children[k];
152                         if ("introProductBinding".equals(element.getName())) {//$NON-NLS-1$
153
if (element.getAttribute("productId").getValue().equals(getProduct().getId())) { //$NON-NLS-1$
154
if (fManifest == null)
155                                     fManifest = (IFile)element.getPluginModel().getUnderlyingResource();
156                                 if (onlyLoadManifest)
157                                     return;
158                                 introId = element.getAttribute("introId").getValue(); //$NON-NLS-1$
159
if (introId != null)
160                                     result.add(introId);
161                             }
162                         }
163                     }
164                 }
165             }
166         }
167         fAvailableIntroIds = (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
168     }
169     
170     private void handleNewIntro() {
171         boolean needNewProduct = false;
172         if (!productDefined()) {
173             needNewProduct = true;
174             MessageDialog mdiag = new MessageDialog(PDEPlugin.getActiveWorkbenchShell(),
175                     PDEUIMessages.IntroSection_undefinedProductId, null,
176                     PDEUIMessages.IntroSection_undefinedProductIdMessage,
177                     MessageDialog.QUESTION, new String JavaDoc[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0);
178             if (mdiag.open() != Window.OK)
179                 return;
180         }
181         ProductIntroWizard wizard = new ProductIntroWizard(getProduct(), needNewProduct);
182         WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard);
183         dialog.create();
184         if (dialog.open() == Window.OK) {
185             String JavaDoc id = wizard.getIntroId();
186             fIntroCombo.add(id, 0);
187             fIntroCombo.setText(id);
188             getIntroInfo().setId(id);
189             addDependenciesAndPlugins();
190         }
191     }
192
193     public void refresh() {
194         String JavaDoc introId = getIntroInfo().getId();
195         if (introId == null) {
196             fIntroCombo.setText(""); //$NON-NLS-1$
197
} else {
198             fIntroCombo.setText(introId);
199         }
200         super.refresh();
201     }
202     
203     private IIntroInfo getIntroInfo() {
204         IIntroInfo info = getProduct().getIntroInfo();
205         if (info == null) {
206             info = getModel().getFactory().createIntroInfo();
207             getProduct().setIntroInfo(info);
208         }
209         return info;
210     }
211     
212     private IProduct getProduct() {
213         return getModel().getProduct();
214     }
215     
216     private IProductModel getModel() {
217         return (IProductModel)getPage().getPDEEditor().getAggregateModel();
218     }
219     
220     private boolean productDefined() {
221         String JavaDoc id = getProduct().getId();
222         return id != null && !id.equals(""); //$NON-NLS-1$
223
}
224     
225     private void addDependenciesAndPlugins() {
226         IProduct product = getProduct();
227         if (!product.useFeatures()) {
228             IProductModelFactory factory = product.getModel().getFactory();
229             IProductPlugin plugin = factory.createPlugin();
230             plugin.setId(INTRO_PLUGIN_ID);
231             product.addPlugins(new IProductPlugin[] {plugin});
232             boolean includeOptional = false;
233             IFormPage page = getPage().getEditor().findPage(ConfigurationPage.PLUGIN_ID);
234             if (page != null)
235                     includeOptional = ((ConfigurationPage)page).includeOptionalDependencies();
236             PluginSection.handleAddRequired(new IProductPlugin[] {plugin}, includeOptional);
237         }
238         if (fManifest == null) loadManifestAndIntroIds(true);
239         if (fManifest != null) addRequiredBundle();
240     }
241     
242     private void addRequiredBundle(){
243         ModelModification mod = new ModelModification(fManifest) {
244             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
245                 if (!(model instanceof IBundlePluginModelBase))
246                     return;
247                 IBundlePluginModelBase modelBase = (IBundlePluginModelBase)model;
248                 IBundle bundle = modelBase.getBundleModel().getBundle();
249                 IManifestHeader header = bundle.getManifestHeader(Constants.REQUIRE_BUNDLE);
250                 if (header instanceof RequireBundleHeader) {
251                     RequireBundleObject[] requires = ((RequireBundleHeader)header).getRequiredBundles();
252                     for (int i = 0; i < requires.length; i++)
253                         if (requires[i].getId().equals(INTRO_PLUGIN_ID))
254                             return;
255                     ((RequireBundleHeader)header).addBundle(INTRO_PLUGIN_ID);
256                 } else
257                     bundle.setHeader(Constants.REQUIRE_BUNDLE, INTRO_PLUGIN_ID);
258             }
259         };
260         PDEModelUtility.modifyModel(mod, null);
261     }
262
263     /* (non-Javadoc)
264      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
265      */

266     public void modelChanged(IModelChangedEvent e) {
267         // No need to call super, handling world changed event here
268
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
269             handleModelEventWorldChanged(e);
270         }
271     }
272
273     /**
274      * @param event
275      */

276     private void handleModelEventWorldChanged(IModelChangedEvent event) {
277         refresh();
278     }
279     
280     /* (non-Javadoc)
281      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
282      */

283     public void dispose() {
284         IProductModel model = getModel();
285         if (model != null) {
286             model.removeModelChangedListener(this);
287         }
288         super.dispose();
289     }
290     
291 }
292
Popular Tags