1 11 package org.eclipse.pde.internal.ui.editor.target; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import org.eclipse.jface.viewers.ISelectionChangedListener; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.jface.viewers.SelectionChangedEvent; 20 import org.eclipse.jface.viewers.TableViewer; 21 import org.eclipse.jface.viewers.Viewer; 22 import org.eclipse.jface.viewers.ViewerComparator; 23 import org.eclipse.jface.window.Window; 24 import org.eclipse.osgi.service.resolver.BundleDescription; 25 import org.eclipse.pde.core.IModelChangedEvent; 26 import org.eclipse.pde.core.plugin.IPluginModelBase; 27 import org.eclipse.pde.core.plugin.PluginRegistry; 28 import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo; 29 import org.eclipse.pde.internal.core.itarget.ITarget; 30 import org.eclipse.pde.internal.core.itarget.ITargetModel; 31 import org.eclipse.pde.internal.core.itarget.ITargetPlugin; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 import org.eclipse.pde.internal.ui.PDEUIMessages; 34 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 35 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 36 import org.eclipse.pde.internal.ui.editor.TableSection; 37 import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor; 38 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 39 import org.eclipse.pde.internal.ui.parts.TablePart; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.layout.GridData; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.ui.actions.ActionFactory; 44 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 45 import org.eclipse.ui.forms.widgets.FormToolkit; 46 import org.eclipse.ui.forms.widgets.Section; 47 48 public class ImplicitDependenciesSection extends TableSection { 49 50 private TableViewer fViewer; 51 private static final int ADD_INDEX = 0; 52 private static final int REMOVE_INDEX = 1; 53 private static final int REMOVE_ALL_INDEX = 2; 54 55 public ImplicitDependenciesSection(PDEFormPage page, Composite parent) { 56 super(page, parent, Section.DESCRIPTION, new String [] {PDEUIMessages.ImplicitDependenicesSection_Add , PDEUIMessages.ImplicitDependenicesSection_Remove, PDEUIMessages.ImplicitDependenicesSection_RemoveAll}); 57 } 58 59 protected void createClient(Section section, FormToolkit toolkit) { 60 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 61 section.setText(PDEUIMessages.ImplicitDependenicesSection_Title); 62 section.setDescription(PDEUIMessages.TargetImplicitPluginsTab_desc); 63 Composite container = toolkit.createComposite(section); 64 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2)); 65 container.setLayoutData(new GridData(GridData.FILL_BOTH)); 66 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 67 fViewer = getTablePart().getTableViewer(); 68 fViewer.setContentProvider(new DefaultTableProvider() { 69 public Object [] getElements(Object inputElement) { 70 return getImplicitPluginsInfo().getPlugins(); 71 } 72 }); 73 fViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 74 fViewer.setComparator(new ViewerComparator() { 75 public int compare(Viewer viewer, Object e1, Object e2) { 76 ITargetPlugin p1 = (ITargetPlugin) e1; 77 ITargetPlugin p2 = (ITargetPlugin) e2; 78 return super.compare(viewer, p1.getId(), p2.getId()); 79 } 80 }); 81 fViewer.setInput(getTarget()); 82 fViewer.addSelectionChangedListener(new ISelectionChangedListener() { 83 public void selectionChanged(SelectionChangedEvent event) { 84 updateButtons(); 85 } 86 }); 87 88 toolkit.paintBordersFor(container); 89 section.setClient(container); 90 GridData gd = new GridData(GridData.FILL_BOTH); 91 section.setLayoutData(gd); 92 updateButtons(); 93 getModel().addModelChangedListener(this); 94 } 95 96 public boolean doGlobalAction(String actionId) { 97 if (actionId.equals(ActionFactory.DELETE.getId())) { 98 handleRemove(); 99 return true; 100 } 101 if (actionId.equals(ActionFactory.CUT.getId())) { 102 handleRemove(); 105 return false; 106 } 107 if (actionId.equals(ActionFactory.PASTE.getId())) { 108 doPaste(); 109 return true; 110 } 111 return false; 112 } 113 114 private void updateButtons() { 115 TablePart part = getTablePart(); 116 boolean empty = fViewer.getSelection().isEmpty(); 117 part.setButtonEnabled(1, !empty); 118 boolean hasElements = fViewer.getTable().getItemCount() > 0; 119 part.setButtonEnabled(2, hasElements); 120 } 121 122 protected void buttonSelected(int index) { 123 switch (index) { 124 case ADD_INDEX: 125 handleAdd(); 126 break; 127 case REMOVE_INDEX: 128 handleRemove(); 129 break; 130 case REMOVE_ALL_INDEX: 131 handleRemoveAll(); 132 } 133 } 134 135 protected void handleAdd() { 136 ElementListSelectionDialog dialog = new ElementListSelectionDialog( 137 PDEPlugin.getActiveWorkbenchShell(), 138 PDEPlugin.getDefault().getLabelProvider()); 139 140 dialog.setElements(getValidBundles()); 141 dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title); 142 dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message); 143 dialog.setMultipleSelection(true); 144 if (dialog.open() == Window.OK) { 145 Object [] models = dialog.getResult(); 146 ArrayList pluginsToAdd = new ArrayList (); 147 ITargetModel model = getModel(); 148 for (int i = 0; i < models.length; i++) { 149 BundleDescription desc = (BundleDescription) models[i]; 150 ITargetPlugin plugin = model.getFactory().createPlugin(); 151 plugin.setId(desc.getSymbolicName()); 152 plugin.setModel(model); 153 pluginsToAdd.add(plugin); 154 } 155 getImplicitPluginsInfo().addPlugins((ITargetPlugin[])pluginsToAdd.toArray(new ITargetPlugin[pluginsToAdd.size()])); 156 updateButtons(); 157 } 158 } 159 160 protected Object [] getValidBundles() { 161 ITargetPlugin[] plugins = getImplicitPluginsInfo().getPlugins(); 162 Set currentPlugins = new HashSet ((4/3) * plugins.length + 1); 163 for (int i = 0; i < plugins.length; i++) { 164 currentPlugins.add(plugins[i].getId()); 165 } 166 167 IPluginModelBase[] models = PluginRegistry.getActiveModels(false); 168 Set result = new HashSet ((4/3) * models.length + 1); 169 for (int i = 0; i < models.length; i++) { 170 BundleDescription desc = models[i].getBundleDescription(); 171 if (desc != null) { 172 if (!currentPlugins.contains(desc.getSymbolicName())) 173 result.add(desc); 174 } 175 } 176 return result.toArray(); 177 } 178 179 protected void handleRemove() { 180 Object [] src = ((IStructuredSelection)fViewer.getSelection()).toArray(); 181 ITargetPlugin[] plugins = new ITargetPlugin[src.length]; 182 System.arraycopy(src, 0, plugins, 0, src.length); 183 getImplicitPluginsInfo().removePlugins(plugins); 184 updateButtons(); 185 } 186 187 protected void handleRemoveAll() { 188 IImplicitDependenciesInfo info =getImplicitPluginsInfo(); 189 info.removePlugins(info.getPlugins()); 190 updateButtons(); 191 } 192 193 private IImplicitDependenciesInfo getImplicitPluginsInfo() { 194 IImplicitDependenciesInfo info = getTarget().getImplicitPluginsInfo(); 195 if (info == null) { 196 info = getModel().getFactory().createImplicitPluginInfo(); 197 getTarget().setImplicitPluginsInfo(info); 198 } 199 return info; 200 } 201 202 private ITarget getTarget() { 203 return getModel().getTarget(); 204 } 205 206 private ITargetModel getModel() { 207 return (ITargetModel)getPage().getPDEEditor().getAggregateModel(); 208 } 209 210 213 public void modelChanged(IModelChangedEvent e) { 214 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 215 handleModelEventWorldChanged(e); 216 return; 217 } 218 if (e.getChangeType() == IModelChangedEvent.CHANGE && 219 e.getChangedProperty().equals(IImplicitDependenciesInfo.P_IMPLICIT_PLUGINS)) { 220 ITargetPlugin[] plugins = (ITargetPlugin[])e.getOldValue(); 221 for (int i = 0; i < plugins.length; i++) 222 fViewer.remove(plugins[i]); 223 plugins = (ITargetPlugin[])e.getNewValue(); 224 for (int i = 0; i < plugins.length; i++) 225 fViewer.add(plugins[i]); 226 } 227 } 228 229 232 private void handleModelEventWorldChanged(IModelChangedEvent event) { 233 fViewer.setInput(getTarget()); 235 refresh(); 237 } 238 239 242 public void refresh() { 243 fViewer.refresh(); 244 updateButtons(); 245 super.refresh(); 246 } 247 248 251 protected void handleDoubleClick(IStructuredSelection selection) { 252 handleOpen(selection); 253 } 254 255 private void handleOpen(IStructuredSelection selection) { 256 Object object = selection.getFirstElement(); 257 ManifestEditor.openPluginEditor(((ITargetPlugin)object).getId()); 258 } 259 260 protected boolean canPaste(Object target, Object [] objects) { 261 for (int i = 0; i < objects.length; i++) { 262 if (!(objects[i] instanceof ITargetPlugin)) 263 return false; 264 } 265 return true; 266 } 267 268 protected void doPaste(Object target, Object [] objects) { 269 for (int i = 0; i < objects.length; i++) { 270 if ( objects[i] instanceof ITargetPlugin ) 271 getImplicitPluginsInfo().addPlugin((ITargetPlugin)objects[i]); 272 } 273 } 274 275 protected void selectionChanged(IStructuredSelection selection) { 276 getPage().getPDEEditor().setSelection(selection); 277 } 278 279 public void dispose() { 280 ITargetModel model = getModel(); 281 if (model != null) 282 model.removeModelChangedListener(this); 283 } 284 285 protected boolean createCount() { return true; } 286 } 287 | Popular Tags |