KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > preferences > TargetImplicitPluginsTab


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.preferences;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.core.runtime.Preferences;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.jface.viewers.ViewerComparator;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.osgi.service.resolver.BundleDescription;
26 import org.eclipse.osgi.service.resolver.State;
27 import org.eclipse.pde.core.plugin.IPluginModelBase;
28 import org.eclipse.pde.core.plugin.PluginRegistry;
29 import org.eclipse.pde.internal.core.ICoreConstants;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
32 import org.eclipse.pde.internal.core.itarget.ITarget;
33 import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
34 import org.eclipse.pde.internal.ui.IHelpContextIds;
35 import org.eclipse.pde.internal.ui.PDEPlugin;
36 import org.eclipse.pde.internal.ui.PDEUIMessages;
37 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
38 import org.eclipse.pde.internal.ui.util.SWTUtil;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.KeyAdapter;
41 import org.eclipse.swt.events.KeyEvent;
42 import org.eclipse.swt.events.SelectionAdapter;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Control;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.ui.PlatformUI;
51 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
52
53 public class TargetImplicitPluginsTab {
54     
55     private TableViewer fElementViewer;
56     protected Set JavaDoc fElements;
57     
58     private Button fAddButton;
59     private Button fRemoveButton;
60     private Button fRemoveAllButton;
61     
62     private TargetPlatformPreferencePage fPage;
63     
64     public TargetImplicitPluginsTab(TargetPlatformPreferencePage page) {
65         fPage = page;
66     }
67     
68     class ContentProvider extends DefaultTableProvider {
69         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
70             if (fElements == null)
71                 loadTable();
72             return fElements.toArray();
73         }
74     }
75     
76     public Control createContents(Composite parent) {
77         Composite container = new Composite(parent, SWT.NONE);
78         GridLayout layout = new GridLayout(2, false);
79         container.setLayout(layout);
80         container.setLayoutData(new GridData(GridData.FILL_BOTH));
81         
82         createLabel(container);
83         createTable(container);
84         createButtons(container);
85         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.IMPLICIT_PLUGINS_PREFERENCE_PAGE);
86         return container;
87     }
88     
89     private void createLabel(Composite container) {
90         Label label = new Label(container, SWT.NONE);
91         label.setText(PDEUIMessages.TargetImplicitPluginsTab_desc);
92         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
93         gd.horizontalSpan = 2;
94         label.setLayoutData(gd);
95     }
96     
97     private void createTable(Composite container) {
98         fElementViewer = new TableViewer(container, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
99         GridData gd = new GridData(GridData.FILL_BOTH);
100         fElementViewer.getControl().setLayoutData(gd);
101         fElementViewer.setContentProvider(new ContentProvider());
102         fElementViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
103         fElementViewer.setInput(PDEPlugin.getDefault());
104         fElementViewer.setComparator(new ViewerComparator());
105         fElementViewer.addSelectionChangedListener(new ISelectionChangedListener() {
106             public void selectionChanged(SelectionChangedEvent event) {
107                 updateButtons();
108             }
109         });
110         fElementViewer.getTable().addKeyListener(new KeyAdapter() {
111             public void keyPressed(KeyEvent e) {
112                 if (e.character == SWT.DEL && e.stateMask == 0) {
113                     handleRemove();
114                 }
115             }
116         });
117     }
118     
119     protected void loadTable() {
120         Preferences preferences = PDECore.getDefault().getPluginPreferences();
121         String JavaDoc value = preferences.getString(ICoreConstants.IMPLICIT_DEPENDENCIES);
122         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(value,","); //$NON-NLS-1$
123
fElements = new HashSet JavaDoc((4/3) * tokens.countTokens() + 1);
124         while (tokens.hasMoreElements()) {
125             IPluginModelBase base = PluginRegistry.findModel(tokens.nextToken());
126             if (base != null) {
127                 BundleDescription desc = base.getBundleDescription();
128                 fElements.add(desc);
129                 fElementViewer.add(desc);
130             }
131         }
132     }
133     
134     private void createButtons(Composite container) {
135         Composite buttonContainer = new Composite(container, SWT.NONE);
136         GridLayout layout = new GridLayout();
137         layout.marginWidth = layout.marginHeight = 0;
138         buttonContainer.setLayout(layout);
139         buttonContainer.setLayoutData(new GridData(GridData.FILL_VERTICAL));
140         
141         fAddButton = new Button(buttonContainer, SWT.PUSH);
142         fAddButton.setText(PDEUIMessages.SourceBlock_add);
143         fAddButton.setLayoutData(new GridData(GridData.FILL | GridData.VERTICAL_ALIGN_BEGINNING));
144         SWTUtil.setButtonDimensionHint(fAddButton);
145         fAddButton.addSelectionListener(new SelectionAdapter() {
146             public void widgetSelected(SelectionEvent e) {
147                 handleAdd();
148             }
149         });
150         
151         fRemoveButton = new Button(buttonContainer, SWT.PUSH);
152         fRemoveButton.setText(PDEUIMessages.SourceBlock_remove);
153         fRemoveButton.setLayoutData(new GridData(GridData.FILL | GridData.VERTICAL_ALIGN_BEGINNING));
154         SWTUtil.setButtonDimensionHint(fRemoveButton);
155         fRemoveButton.addSelectionListener(new SelectionAdapter() {
156             public void widgetSelected(SelectionEvent e) {
157                 handleRemove();
158             }
159         });
160         
161         fRemoveAllButton = new Button(buttonContainer, SWT.PUSH);
162         fRemoveAllButton.setText(PDEUIMessages.TargetImplicitPluginsTab_removeAll3);
163         fRemoveAllButton.setLayoutData(new GridData(GridData.FILL | GridData.VERTICAL_ALIGN_BEGINNING));
164         SWTUtil.setButtonDimensionHint(fRemoveAllButton);
165         fRemoveAllButton.addSelectionListener(new SelectionAdapter() {
166             public void widgetSelected(SelectionEvent e) {
167                 handleRemoveAll();
168             }
169         });
170         if (fElements.size() == 0) {
171             fRemoveButton.setEnabled(false);
172             fRemoveAllButton.setEnabled(false);
173         }
174     }
175     
176     private void updateButtons() {
177         boolean empty = fElementViewer.getSelection().isEmpty();
178         fRemoveButton.setEnabled(!empty);
179         fRemoveAllButton.setEnabled(fElementViewer.getElementAt(0) != null);
180     }
181     
182     private void handleAdd() {
183         ElementListSelectionDialog dialog = new ElementListSelectionDialog(
184                 PDEPlugin.getActiveWorkbenchShell(),
185                 PDEPlugin.getDefault().getLabelProvider());
186         
187         dialog.setElements(getValidBundles());
188         dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title);
189         dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message);
190         dialog.setMultipleSelection(true);
191         if (dialog.open() == Window.OK) {
192             Object JavaDoc[] bundles = dialog.getResult();
193             for (int i = 0; i < bundles.length; i++) {
194                 fElementViewer.add(bundles[i]);
195                 fElements.add(bundles[i]);
196             }
197             updateButtons();
198         }
199     }
200     
201     protected Object JavaDoc[] getValidBundles() {
202         
203         Set JavaDoc currentPlugins = new HashSet JavaDoc((4/3) * fElements.size() + 1);
204         Iterator JavaDoc it = fElements.iterator();
205         while (it.hasNext()) {
206             BundleDescription desc = (BundleDescription)it.next();
207             currentPlugins.add(desc.getSymbolicName());
208         }
209         
210         IPluginModelBase[] models = fPage.getCurrentModels();
211         Set JavaDoc result = new HashSet JavaDoc((4/3) * models.length + 1);
212         for (int i = 0; i < models.length; i++) {
213             BundleDescription desc = models[i].getBundleDescription();
214             if (!currentPlugins.contains(desc.getSymbolicName()))
215                 result.add(desc);
216         }
217         return result.toArray();
218     }
219     
220     private void handleRemove() {
221         IStructuredSelection ssel = (IStructuredSelection)fElementViewer.getSelection();
222         Iterator JavaDoc it = ssel.iterator();
223         while (it.hasNext()) {
224             Object JavaDoc item = it.next();
225             fElements.remove(item);
226             fElementViewer.remove(item);
227         }
228         if (fElements.size() == 0)
229             fRemoveButton.setEnabled(false);
230         updateButtons();
231     }
232     
233     private void handleRemoveAll(){
234         fElementViewer.remove(fElements.toArray());
235         fElements.clear();
236         updateButtons();
237     }
238
239     public void performDefauls() {
240         fElementViewer.remove(fElements.toArray());
241         fElements.clear();
242         fRemoveButton.setEnabled(false);
243     }
244
245     public void performOk() {
246         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
247         Iterator JavaDoc it = fElements.iterator();
248         while (it.hasNext()) {
249             if (buffer.length() > 0)
250                 buffer.append(","); //$NON-NLS-1$
251
BundleDescription desc = (BundleDescription) it.next();
252             buffer.append(desc.getSymbolicName());
253         }
254         Preferences preferences = PDECore.getDefault().getPluginPreferences();
255         preferences.setValue(ICoreConstants.IMPLICIT_DEPENDENCIES, buffer.toString());
256     }
257     
258     public void loadTargetProfile(ITarget target) {
259         fElements.clear();
260         IImplicitDependenciesInfo info = target.getImplicitPluginsInfo();
261         if (info != null) {
262             State state = fPage.getCurrentState().getState();
263             ITargetPlugin[] plugins = info.getPlugins();
264             for (int i = 0; i < plugins.length; i++) {
265                 BundleDescription desc = state.getBundle(plugins[i].getId(), null);
266                 if (desc != null)
267                     fElements.add(desc);
268             }
269         }
270         fElementViewer.refresh();
271     }
272     
273     protected String JavaDoc[] getImplicitPlugins() {
274         String JavaDoc[] result = new String JavaDoc[fElements.size()];
275         Iterator JavaDoc iter = fElements.iterator();
276         int i = 0;
277         while (iter.hasNext()) {
278             result[i++] = ((BundleDescription)iter.next()).getSymbolicName();
279         }
280         return result;
281     }
282
283 }
284
Popular Tags