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.preferences;
011:
012: import java.util.HashSet;
013: import java.util.Iterator;
014: import java.util.Set;
015: import java.util.StringTokenizer;
016:
017: import org.eclipse.core.runtime.Preferences;
018: import org.eclipse.jface.viewers.ISelectionChangedListener;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.TableViewer;
022: import org.eclipse.jface.viewers.ViewerComparator;
023: import org.eclipse.jface.window.Window;
024: import org.eclipse.osgi.service.resolver.BundleDescription;
025: import org.eclipse.osgi.service.resolver.State;
026: import org.eclipse.pde.core.plugin.IPluginModelBase;
027: import org.eclipse.pde.core.plugin.PluginRegistry;
028: import org.eclipse.pde.internal.core.ICoreConstants;
029: import org.eclipse.pde.internal.core.PDECore;
030: import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
031: import org.eclipse.pde.internal.core.itarget.ITarget;
032: import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
033: import org.eclipse.pde.internal.ui.IHelpContextIds;
034: import org.eclipse.pde.internal.ui.PDEPlugin;
035: import org.eclipse.pde.internal.ui.PDEUIMessages;
036: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
037: import org.eclipse.pde.internal.ui.util.SWTUtil;
038: import org.eclipse.swt.SWT;
039: import org.eclipse.swt.events.KeyAdapter;
040: import org.eclipse.swt.events.KeyEvent;
041: import org.eclipse.swt.events.SelectionAdapter;
042: import org.eclipse.swt.events.SelectionEvent;
043: import org.eclipse.swt.layout.GridData;
044: import org.eclipse.swt.layout.GridLayout;
045: import org.eclipse.swt.widgets.Button;
046: import org.eclipse.swt.widgets.Composite;
047: import org.eclipse.swt.widgets.Control;
048: import org.eclipse.swt.widgets.Label;
049: import org.eclipse.ui.PlatformUI;
050: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
051:
052: public class TargetImplicitPluginsTab {
053:
054: private TableViewer fElementViewer;
055: protected Set fElements;
056:
057: private Button fAddButton;
058: private Button fRemoveButton;
059: private Button fRemoveAllButton;
060:
061: private TargetPlatformPreferencePage fPage;
062:
063: public TargetImplicitPluginsTab(TargetPlatformPreferencePage page) {
064: fPage = page;
065: }
066:
067: class ContentProvider extends DefaultTableProvider {
068: public Object[] getElements(Object inputElement) {
069: if (fElements == null)
070: loadTable();
071: return fElements.toArray();
072: }
073: }
074:
075: public Control createContents(Composite parent) {
076: Composite container = new Composite(parent, SWT.NONE);
077: GridLayout layout = new GridLayout(2, false);
078: container.setLayout(layout);
079: container.setLayoutData(new GridData(GridData.FILL_BOTH));
080:
081: createLabel(container);
082: createTable(container);
083: createButtons(container);
084: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
085: IHelpContextIds.IMPLICIT_PLUGINS_PREFERENCE_PAGE);
086: return container;
087: }
088:
089: private void createLabel(Composite container) {
090: Label label = new Label(container, SWT.NONE);
091: label.setText(PDEUIMessages.TargetImplicitPluginsTab_desc);
092: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
093: gd.horizontalSpan = 2;
094: label.setLayoutData(gd);
095: }
096:
097: private void createTable(Composite container) {
098: fElementViewer = new TableViewer(container, SWT.SINGLE
099: | SWT.V_SCROLL | SWT.BORDER);
100: GridData gd = new GridData(GridData.FILL_BOTH);
101: fElementViewer.getControl().setLayoutData(gd);
102: fElementViewer.setContentProvider(new ContentProvider());
103: fElementViewer.setLabelProvider(PDEPlugin.getDefault()
104: .getLabelProvider());
105: fElementViewer.setInput(PDEPlugin.getDefault());
106: fElementViewer.setComparator(new ViewerComparator());
107: fElementViewer
108: .addSelectionChangedListener(new ISelectionChangedListener() {
109: public void selectionChanged(
110: SelectionChangedEvent event) {
111: updateButtons();
112: }
113: });
114: fElementViewer.getTable().addKeyListener(new KeyAdapter() {
115: public void keyPressed(KeyEvent e) {
116: if (e.character == SWT.DEL && e.stateMask == 0) {
117: handleRemove();
118: }
119: }
120: });
121: }
122:
123: protected void loadTable() {
124: Preferences preferences = PDECore.getDefault()
125: .getPluginPreferences();
126: String value = preferences
127: .getString(ICoreConstants.IMPLICIT_DEPENDENCIES);
128: StringTokenizer tokens = new StringTokenizer(value, ","); //$NON-NLS-1$
129: fElements = new HashSet((4 / 3) * tokens.countTokens() + 1);
130: while (tokens.hasMoreElements()) {
131: IPluginModelBase base = PluginRegistry.findModel(tokens
132: .nextToken());
133: if (base != null) {
134: BundleDescription desc = base.getBundleDescription();
135: fElements.add(desc);
136: fElementViewer.add(desc);
137: }
138: }
139: }
140:
141: private void createButtons(Composite container) {
142: Composite buttonContainer = new Composite(container, SWT.NONE);
143: GridLayout layout = new GridLayout();
144: layout.marginWidth = layout.marginHeight = 0;
145: buttonContainer.setLayout(layout);
146: buttonContainer.setLayoutData(new GridData(
147: GridData.FILL_VERTICAL));
148:
149: fAddButton = new Button(buttonContainer, SWT.PUSH);
150: fAddButton.setText(PDEUIMessages.SourceBlock_add);
151: fAddButton.setLayoutData(new GridData(GridData.FILL
152: | GridData.VERTICAL_ALIGN_BEGINNING));
153: SWTUtil.setButtonDimensionHint(fAddButton);
154: fAddButton.addSelectionListener(new SelectionAdapter() {
155: public void widgetSelected(SelectionEvent e) {
156: handleAdd();
157: }
158: });
159:
160: fRemoveButton = new Button(buttonContainer, SWT.PUSH);
161: fRemoveButton.setText(PDEUIMessages.SourceBlock_remove);
162: fRemoveButton.setLayoutData(new GridData(GridData.FILL
163: | GridData.VERTICAL_ALIGN_BEGINNING));
164: SWTUtil.setButtonDimensionHint(fRemoveButton);
165: fRemoveButton.addSelectionListener(new SelectionAdapter() {
166: public void widgetSelected(SelectionEvent e) {
167: handleRemove();
168: }
169: });
170:
171: fRemoveAllButton = new Button(buttonContainer, SWT.PUSH);
172: fRemoveAllButton
173: .setText(PDEUIMessages.TargetImplicitPluginsTab_removeAll3);
174: fRemoveAllButton.setLayoutData(new GridData(GridData.FILL
175: | GridData.VERTICAL_ALIGN_BEGINNING));
176: SWTUtil.setButtonDimensionHint(fRemoveAllButton);
177: fRemoveAllButton.addSelectionListener(new SelectionAdapter() {
178: public void widgetSelected(SelectionEvent e) {
179: handleRemoveAll();
180: }
181: });
182: if (fElements.size() == 0) {
183: fRemoveButton.setEnabled(false);
184: fRemoveAllButton.setEnabled(false);
185: }
186: }
187:
188: private void updateButtons() {
189: boolean empty = fElementViewer.getSelection().isEmpty();
190: fRemoveButton.setEnabled(!empty);
191: fRemoveAllButton
192: .setEnabled(fElementViewer.getElementAt(0) != null);
193: }
194:
195: private void handleAdd() {
196: ElementListSelectionDialog dialog = new ElementListSelectionDialog(
197: PDEPlugin.getActiveWorkbenchShell(), PDEPlugin
198: .getDefault().getLabelProvider());
199:
200: dialog.setElements(getValidBundles());
201: dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title);
202: dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message);
203: dialog.setMultipleSelection(true);
204: if (dialog.open() == Window.OK) {
205: Object[] bundles = dialog.getResult();
206: for (int i = 0; i < bundles.length; i++) {
207: fElementViewer.add(bundles[i]);
208: fElements.add(bundles[i]);
209: }
210: updateButtons();
211: }
212: }
213:
214: protected Object[] getValidBundles() {
215:
216: Set currentPlugins = new HashSet((4 / 3) * fElements.size() + 1);
217: Iterator it = fElements.iterator();
218: while (it.hasNext()) {
219: BundleDescription desc = (BundleDescription) it.next();
220: currentPlugins.add(desc.getSymbolicName());
221: }
222:
223: IPluginModelBase[] models = fPage.getCurrentModels();
224: Set result = new HashSet((4 / 3) * models.length + 1);
225: for (int i = 0; i < models.length; i++) {
226: BundleDescription desc = models[i].getBundleDescription();
227: if (!currentPlugins.contains(desc.getSymbolicName()))
228: result.add(desc);
229: }
230: return result.toArray();
231: }
232:
233: private void handleRemove() {
234: IStructuredSelection ssel = (IStructuredSelection) fElementViewer
235: .getSelection();
236: Iterator it = ssel.iterator();
237: while (it.hasNext()) {
238: Object item = it.next();
239: fElements.remove(item);
240: fElementViewer.remove(item);
241: }
242: if (fElements.size() == 0)
243: fRemoveButton.setEnabled(false);
244: updateButtons();
245: }
246:
247: private void handleRemoveAll() {
248: fElementViewer.remove(fElements.toArray());
249: fElements.clear();
250: updateButtons();
251: }
252:
253: public void performDefauls() {
254: fElementViewer.remove(fElements.toArray());
255: fElements.clear();
256: fRemoveButton.setEnabled(false);
257: }
258:
259: public void performOk() {
260: StringBuffer buffer = new StringBuffer();
261: Iterator it = fElements.iterator();
262: while (it.hasNext()) {
263: if (buffer.length() > 0)
264: buffer.append(","); //$NON-NLS-1$
265: BundleDescription desc = (BundleDescription) it.next();
266: buffer.append(desc.getSymbolicName());
267: }
268: Preferences preferences = PDECore.getDefault()
269: .getPluginPreferences();
270: preferences.setValue(ICoreConstants.IMPLICIT_DEPENDENCIES,
271: buffer.toString());
272: }
273:
274: public void loadTargetProfile(ITarget target) {
275: fElements.clear();
276: IImplicitDependenciesInfo info = target
277: .getImplicitPluginsInfo();
278: if (info != null) {
279: State state = fPage.getCurrentState().getState();
280: ITargetPlugin[] plugins = info.getPlugins();
281: for (int i = 0; i < plugins.length; i++) {
282: BundleDescription desc = state.getBundle(plugins[i]
283: .getId(), null);
284: if (desc != null)
285: fElements.add(desc);
286: }
287: }
288: fElementViewer.refresh();
289: }
290:
291: protected String[] getImplicitPlugins() {
292: String[] result = new String[fElements.size()];
293: Iterator iter = fElements.iterator();
294: int i = 0;
295: while (iter.hasNext()) {
296: result[i++] = ((BundleDescription) iter.next())
297: .getSymbolicName();
298: }
299: return result;
300: }
301:
302: }
|