001: /*******************************************************************************
002: * Copyright (c) 2005, 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.target;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Set;
015:
016: import org.eclipse.jface.viewers.ISelectionChangedListener;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.viewers.SelectionChangedEvent;
019: import org.eclipse.jface.viewers.TableViewer;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.jface.viewers.ViewerComparator;
022: import org.eclipse.jface.window.Window;
023: import org.eclipse.osgi.service.resolver.BundleDescription;
024: import org.eclipse.pde.core.IModelChangedEvent;
025: import org.eclipse.pde.core.plugin.IPluginModelBase;
026: import org.eclipse.pde.core.plugin.PluginRegistry;
027: import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
028: import org.eclipse.pde.internal.core.itarget.ITarget;
029: import org.eclipse.pde.internal.core.itarget.ITargetModel;
030: import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
031: import org.eclipse.pde.internal.ui.PDEPlugin;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
034: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
035: import org.eclipse.pde.internal.ui.editor.TableSection;
036: import org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor;
037: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
038: import org.eclipse.pde.internal.ui.parts.TablePart;
039: import org.eclipse.swt.SWT;
040: import org.eclipse.swt.layout.GridData;
041: import org.eclipse.swt.widgets.Composite;
042: import org.eclipse.ui.actions.ActionFactory;
043: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
044: import org.eclipse.ui.forms.widgets.FormToolkit;
045: import org.eclipse.ui.forms.widgets.Section;
046:
047: public class ImplicitDependenciesSection extends TableSection {
048:
049: private TableViewer fViewer;
050: private static final int ADD_INDEX = 0;
051: private static final int REMOVE_INDEX = 1;
052: private static final int REMOVE_ALL_INDEX = 2;
053:
054: public ImplicitDependenciesSection(PDEFormPage page,
055: Composite parent) {
056: super (page, parent, Section.DESCRIPTION, new String[] {
057: PDEUIMessages.ImplicitDependenicesSection_Add,
058: PDEUIMessages.ImplicitDependenicesSection_Remove,
059: PDEUIMessages.ImplicitDependenicesSection_RemoveAll });
060: }
061:
062: protected void createClient(Section section, FormToolkit toolkit) {
063: section.setLayout(FormLayoutFactory.createClearGridLayout(
064: false, 1));
065: section
066: .setText(PDEUIMessages.ImplicitDependenicesSection_Title);
067: section
068: .setDescription(PDEUIMessages.TargetImplicitPluginsTab_desc);
069: Composite container = toolkit.createComposite(section);
070: container.setLayout(FormLayoutFactory
071: .createSectionClientGridLayout(false, 2));
072: container.setLayoutData(new GridData(GridData.FILL_BOTH));
073: createViewerPartControl(container, SWT.MULTI, 2, toolkit);
074: fViewer = getTablePart().getTableViewer();
075: fViewer.setContentProvider(new DefaultTableProvider() {
076: public Object[] getElements(Object inputElement) {
077: return getImplicitPluginsInfo().getPlugins();
078: }
079: });
080: fViewer.setLabelProvider(PDEPlugin.getDefault()
081: .getLabelProvider());
082: fViewer.setComparator(new ViewerComparator() {
083: public int compare(Viewer viewer, Object e1, Object e2) {
084: ITargetPlugin p1 = (ITargetPlugin) e1;
085: ITargetPlugin p2 = (ITargetPlugin) e2;
086: return super .compare(viewer, p1.getId(), p2.getId());
087: }
088: });
089: fViewer.setInput(getTarget());
090: fViewer
091: .addSelectionChangedListener(new ISelectionChangedListener() {
092: public void selectionChanged(
093: SelectionChangedEvent event) {
094: updateButtons();
095: }
096: });
097:
098: toolkit.paintBordersFor(container);
099: section.setClient(container);
100: GridData gd = new GridData(GridData.FILL_BOTH);
101: section.setLayoutData(gd);
102: updateButtons();
103: getModel().addModelChangedListener(this );
104: }
105:
106: public boolean doGlobalAction(String actionId) {
107: if (actionId.equals(ActionFactory.DELETE.getId())) {
108: handleRemove();
109: return true;
110: }
111: if (actionId.equals(ActionFactory.CUT.getId())) {
112: // delete here and let the editor transfer
113: // the selection to the clipboard
114: handleRemove();
115: return false;
116: }
117: if (actionId.equals(ActionFactory.PASTE.getId())) {
118: doPaste();
119: return true;
120: }
121: return false;
122: }
123:
124: private void updateButtons() {
125: TablePart part = getTablePart();
126: boolean empty = fViewer.getSelection().isEmpty();
127: part.setButtonEnabled(1, !empty);
128: boolean hasElements = fViewer.getTable().getItemCount() > 0;
129: part.setButtonEnabled(2, hasElements);
130: }
131:
132: protected void buttonSelected(int index) {
133: switch (index) {
134: case ADD_INDEX:
135: handleAdd();
136: break;
137: case REMOVE_INDEX:
138: handleRemove();
139: break;
140: case REMOVE_ALL_INDEX:
141: handleRemoveAll();
142: }
143: }
144:
145: protected void handleAdd() {
146: ElementListSelectionDialog dialog = new ElementListSelectionDialog(
147: PDEPlugin.getActiveWorkbenchShell(), PDEPlugin
148: .getDefault().getLabelProvider());
149:
150: dialog.setElements(getValidBundles());
151: dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title);
152: dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message);
153: dialog.setMultipleSelection(true);
154: if (dialog.open() == Window.OK) {
155: Object[] models = dialog.getResult();
156: ArrayList pluginsToAdd = new ArrayList();
157: ITargetModel model = getModel();
158: for (int i = 0; i < models.length; i++) {
159: BundleDescription desc = (BundleDescription) models[i];
160: ITargetPlugin plugin = model.getFactory()
161: .createPlugin();
162: plugin.setId(desc.getSymbolicName());
163: plugin.setModel(model);
164: pluginsToAdd.add(plugin);
165: }
166: getImplicitPluginsInfo().addPlugins(
167: (ITargetPlugin[]) pluginsToAdd
168: .toArray(new ITargetPlugin[pluginsToAdd
169: .size()]));
170: updateButtons();
171: }
172: }
173:
174: protected Object[] getValidBundles() {
175: ITargetPlugin[] plugins = getImplicitPluginsInfo().getPlugins();
176: Set currentPlugins = new HashSet((4 / 3) * plugins.length + 1);
177: for (int i = 0; i < plugins.length; i++) {
178: currentPlugins.add(plugins[i].getId());
179: }
180:
181: IPluginModelBase[] models = PluginRegistry
182: .getActiveModels(false);
183: Set result = new HashSet((4 / 3) * models.length + 1);
184: for (int i = 0; i < models.length; i++) {
185: BundleDescription desc = models[i].getBundleDescription();
186: if (desc != null) {
187: if (!currentPlugins.contains(desc.getSymbolicName()))
188: result.add(desc);
189: }
190: }
191: return result.toArray();
192: }
193:
194: protected void handleRemove() {
195: Object[] src = ((IStructuredSelection) fViewer.getSelection())
196: .toArray();
197: ITargetPlugin[] plugins = new ITargetPlugin[src.length];
198: System.arraycopy(src, 0, plugins, 0, src.length);
199: getImplicitPluginsInfo().removePlugins(plugins);
200: updateButtons();
201: }
202:
203: protected void handleRemoveAll() {
204: IImplicitDependenciesInfo info = getImplicitPluginsInfo();
205: info.removePlugins(info.getPlugins());
206: updateButtons();
207: }
208:
209: private IImplicitDependenciesInfo getImplicitPluginsInfo() {
210: IImplicitDependenciesInfo info = getTarget()
211: .getImplicitPluginsInfo();
212: if (info == null) {
213: info = getModel().getFactory().createImplicitPluginInfo();
214: getTarget().setImplicitPluginsInfo(info);
215: }
216: return info;
217: }
218:
219: private ITarget getTarget() {
220: return getModel().getTarget();
221: }
222:
223: private ITargetModel getModel() {
224: return (ITargetModel) getPage().getPDEEditor()
225: .getAggregateModel();
226: }
227:
228: /* (non-Javadoc)
229: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
230: */
231: public void modelChanged(IModelChangedEvent e) {
232: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
233: handleModelEventWorldChanged(e);
234: return;
235: }
236: if (e.getChangeType() == IModelChangedEvent.CHANGE
237: && e.getChangedProperty().equals(
238: IImplicitDependenciesInfo.P_IMPLICIT_PLUGINS)) {
239: ITargetPlugin[] plugins = (ITargetPlugin[]) e.getOldValue();
240: for (int i = 0; i < plugins.length; i++)
241: fViewer.remove(plugins[i]);
242: plugins = (ITargetPlugin[]) e.getNewValue();
243: for (int i = 0; i < plugins.length; i++)
244: fViewer.add(plugins[i]);
245: }
246: }
247:
248: /**
249: * @param event
250: */
251: private void handleModelEventWorldChanged(IModelChangedEvent event) {
252: // Reload input
253: fViewer.setInput(getTarget());
254: // Perform the refresh
255: refresh();
256: }
257:
258: /* (non-Javadoc)
259: * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
260: */
261: public void refresh() {
262: fViewer.refresh();
263: updateButtons();
264: super .refresh();
265: }
266:
267: /* (non-Javadoc)
268: * @see org.eclipse.pde.internal.ui.editor.TableSection#handleDoubleClick(org.eclipse.jface.viewers.IStructuredSelection)
269: */
270: protected void handleDoubleClick(IStructuredSelection selection) {
271: handleOpen(selection);
272: }
273:
274: private void handleOpen(IStructuredSelection selection) {
275: Object object = selection.getFirstElement();
276: ManifestEditor.openPluginEditor(((ITargetPlugin) object)
277: .getId());
278: }
279:
280: protected boolean canPaste(Object target, Object[] objects) {
281: for (int i = 0; i < objects.length; i++) {
282: if (!(objects[i] instanceof ITargetPlugin))
283: return false;
284: }
285: return true;
286: }
287:
288: protected void doPaste(Object target, Object[] objects) {
289: for (int i = 0; i < objects.length; i++) {
290: if (objects[i] instanceof ITargetPlugin)
291: getImplicitPluginsInfo().addPlugin(
292: (ITargetPlugin) objects[i]);
293: }
294: }
295:
296: protected void selectionChanged(IStructuredSelection selection) {
297: getPage().getPDEEditor().setSelection(selection);
298: }
299:
300: public void dispose() {
301: ITargetModel model = getModel();
302: if (model != null)
303: model.removeModelChangedListener(this );
304: }
305:
306: protected boolean createCount() {
307: return true;
308: }
309: }
|