1 11 package org.eclipse.pde.internal.ui.editor.product; 12 13 import java.util.TreeSet ; 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 [] fAvailableIntroIds; 68 private static final String INTRO_PLUGIN_ID = "org.eclipse.ui.intro"; 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(""); 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 getModel().addModelChangedListener(this); 129 } 130 131 private void handleSelection() { 132 if (!productDefined()) { 133 fIntroCombo.setText(""); return; 135 } 136 getIntroInfo().setId(fIntroCombo.getSelection()); 137 addDependenciesAndPlugins(); 138 } 139 140 private void loadManifestAndIntroIds(boolean onlyLoadManifest) { 141 TreeSet result = new TreeSet (); 142 String 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 point = extensions[j].getPoint(); 148 if (point != null && point.equals("org.eclipse.ui.intro")) { 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())) { if (element.getAttribute("productId").getValue().equals(getProduct().getId())) { if (fManifest == null) 155 fManifest = (IFile)element.getPluginModel().getUnderlyingResource(); 156 if (onlyLoadManifest) 157 return; 158 introId = element.getAttribute("introId").getValue(); if (introId != null) 160 result.add(introId); 161 } 162 } 163 } 164 } 165 } 166 } 167 fAvailableIntroIds = (String [])result.toArray(new String [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 [] {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 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 introId = getIntroInfo().getId(); 195 if (introId == null) { 196 fIntroCombo.setText(""); } 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 id = getProduct().getId(); 222 return id != null && !id.equals(""); } 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 266 public void modelChanged(IModelChangedEvent e) { 267 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 269 handleModelEventWorldChanged(e); 270 } 271 } 272 273 276 private void handleModelEventWorldChanged(IModelChangedEvent event) { 277 refresh(); 278 } 279 280 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 |