KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > imports > PluginImportWizardDetailedPage


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 /*
12  * Created on May 31, 2003
13  *
14  * To change this generated comment go to
15  * Window>Preferences>Java>Code Generation>Code Template
16  */

17 package org.eclipse.pde.internal.ui.wizards.imports;
18
19
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.viewers.DoubleClickEvent;
27 import org.eclipse.jface.viewers.IDoubleClickListener;
28 import org.eclipse.jface.viewers.IStructuredContentProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.osgi.util.NLS;
32 import org.eclipse.pde.core.plugin.IPluginModelBase;
33 import org.eclipse.pde.internal.core.PDECore;
34 import org.eclipse.pde.internal.core.WorkspaceModelManager;
35 import org.eclipse.pde.internal.core.util.PatternConstructor;
36 import org.eclipse.pde.internal.ui.IHelpContextIds;
37 import org.eclipse.pde.internal.ui.PDEPlugin;
38 import org.eclipse.pde.internal.ui.PDEUIMessages;
39 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
40 import org.eclipse.pde.internal.ui.util.SWTUtil;
41 import org.eclipse.pde.internal.ui.wizards.ListUtil;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.custom.ScrolledComposite;
44 import org.eclipse.swt.events.ModifyEvent;
45 import org.eclipse.swt.events.ModifyListener;
46 import org.eclipse.swt.events.SelectionAdapter;
47 import org.eclipse.swt.events.SelectionEvent;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Group;
53 import org.eclipse.swt.widgets.Label;
54 import org.eclipse.swt.widgets.Table;
55 import org.eclipse.swt.widgets.TableItem;
56 import org.eclipse.swt.widgets.Text;
57 import org.eclipse.ui.PlatformUI;
58
59 public class PluginImportWizardDetailedPage extends BaseImportWizardSecondPage {
60
61     
62     class ContentProvider
63         extends DefaultContentProvider
64         implements IStructuredContentProvider {
65         public Object JavaDoc[] getElements(Object JavaDoc element) {
66             return fModels;
67         }
68     }
69
70     private Label fCountLabel;
71     private TableViewer fAvailableListViewer;
72     private Text fFilterText;
73     
74     public PluginImportWizardDetailedPage(String JavaDoc pageName, PluginImportWizardFirstPage firstPage) {
75         super(pageName, firstPage);
76         setTitle(PDEUIMessages.ImportWizard_DetailedPage_title);
77         setMessage(PDEUIMessages.ImportWizard_DetailedPage_desc);
78     }
79
80     public void createControl(Composite parent) {
81         Composite container = new Composite(parent, SWT.NONE);
82         GridLayout layout = new GridLayout();
83         layout.numColumns = 3;
84         layout.makeColumnsEqualWidth = false;
85         layout.horizontalSpacing = 5;
86         layout.verticalSpacing = 10;
87         container.setLayout(layout);
88                 
89         createScrollArea(container);
90         createAvailableList(container).setLayoutData(new GridData(GridData.FILL_BOTH));
91         createButtonArea(container);
92         createImportList(container).setLayoutData(new GridData(GridData.FILL_BOTH));
93         updateCount();
94         createComputationsOption(container, 3);
95         addViewerListeners();
96         
97         initialize();
98         setControl(container);
99         Dialog.applyDialogFont(container);
100         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.PLUGIN_IMPORT_SECOND_PAGE);
101     }
102     
103     private void initialize(){
104         setPageComplete(false);
105     }
106     
107     private void addViewerListeners() {
108         fAvailableListViewer.addDoubleClickListener(new IDoubleClickListener() {
109             public void doubleClick(DoubleClickEvent event) {
110                 handleAdd();
111             }
112         });
113                 
114         fImportListViewer.addDoubleClickListener(new IDoubleClickListener() {
115             public void doubleClick(DoubleClickEvent event) {
116                 handleRemove();
117             }
118         });
119         
120         fFilterText.addModifyListener(new ModifyListener(){
121             public void modifyText(ModifyEvent e) {
122                 handleFilter();
123             }
124         });
125         
126     }
127
128     private Composite createAvailableList(Composite parent) {
129         Composite container = new Composite(parent, SWT.NONE);
130         GridLayout layout = new GridLayout();
131         layout.marginWidth = 0;
132         layout.marginHeight = 0;
133         container.setLayout(layout);
134         container.setLayoutData(new GridData());
135
136         Label label = new Label(container, SWT.NONE);
137         label.setText(PDEUIMessages.ImportWizard_DetailedPage_availableList);
138
139         Table table = new Table(container, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
140         GridData gd = new GridData(GridData.FILL_BOTH);
141         gd.heightHint = 200;
142         gd.widthHint = 225;
143         table.setLayoutData(gd);
144
145         fAvailableListViewer = new TableViewer(table);
146         fAvailableListViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
147         fAvailableListViewer.setContentProvider(new ContentProvider());
148         fAvailableListViewer.setInput(PDECore.getDefault().getModelManager());
149         fAvailableListViewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
150
151         return container;
152     }
153     
154     
155     private Composite createButtonArea(Composite parent) {
156         ScrolledComposite comp = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
157         GridLayout layout = new GridLayout();
158         layout.marginWidth = layout.marginHeight = 0;
159         comp.setLayoutData(new GridData(GridData.FILL_VERTICAL));
160         Composite container = new Composite(comp, SWT.NONE);
161         layout = new GridLayout();
162         layout.marginWidth = 0;
163         container.setLayout(layout);
164         GridData gd = new GridData(GridData.FILL_VERTICAL);
165         gd.verticalIndent = 15;
166         container.setLayoutData(gd);
167         
168         Button button = new Button(container, SWT.PUSH);
169         button.setText(PDEUIMessages.ImportWizard_DetailedPage_existing);
170         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
171         button.addSelectionListener(new SelectionAdapter() {
172             public void widgetSelected(SelectionEvent e) {
173                 handleExistingProjects();
174             }
175         });
176         SWTUtil.setButtonDimensionHint(button);
177
178         button = new Button(container, SWT.PUSH);
179         button.setText(PDEUIMessages.ImportWizard_DetailedPage_existingUnshared);
180         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
181         button.addSelectionListener(new SelectionAdapter() {
182             public void widgetSelected(SelectionEvent e) {
183                 handleExistingUnshared();
184             }
185         });
186         SWTUtil.setButtonDimensionHint(button);
187         
188         button = new Button(container, SWT.PUSH);
189         button.setText(PDEUIMessages.ImportWizard_DetailedPage_add);
190         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
191         button.addSelectionListener(new SelectionAdapter() {
192             public void widgetSelected(SelectionEvent e) {
193                 handleAdd();
194             }
195         });
196         SWTUtil.setButtonDimensionHint(button);
197         
198         button = new Button(container, SWT.PUSH);
199         button.setText(PDEUIMessages.ImportWizard_DetailedPage_addAll);
200         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
201         button.addSelectionListener(new SelectionAdapter() {
202             public void widgetSelected(SelectionEvent e) {
203                 handleAddAll();
204             }
205         });
206         SWTUtil.setButtonDimensionHint(button);
207         
208         button = new Button(container, SWT.PUSH);
209         button.setText(PDEUIMessages.ImportWizard_DetailedPage_remove);
210         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
211         button.addSelectionListener(new SelectionAdapter() {
212             public void widgetSelected(SelectionEvent e) {
213                 handleRemove();
214             }
215         });
216         SWTUtil.setButtonDimensionHint(button);
217         
218         button = new Button(container, SWT.PUSH);
219         button.setText(PDEUIMessages.ImportWizard_DetailedPage_removeAll);
220         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
221         button.addSelectionListener(new SelectionAdapter() {
222             public void widgetSelected(SelectionEvent e) {
223                 handleRemoveAll();
224             }
225         });
226         SWTUtil.setButtonDimensionHint(button);
227         
228         button = new Button(container, SWT.PUSH);
229         button.setText(PDEUIMessages.ImportWizard_DetailedPage_swap);
230         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
231         button.addSelectionListener(new SelectionAdapter() {
232             public void widgetSelected(SelectionEvent e) {
233                 handleSwap();
234             }
235         });
236         SWTUtil.setButtonDimensionHint(button);
237         
238         button = new Button(container, SWT.PUSH);
239         button.setText(PDEUIMessages.ImportWizard_DetailedPage_addRequired);
240         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
241         button.addSelectionListener(new SelectionAdapter() {
242             public void widgetSelected(SelectionEvent e) {
243                 handleAddRequiredPlugins();
244             }
245
246         });
247         SWTUtil.setButtonDimensionHint(button);
248         
249         fCountLabel = new Label(container, SWT.NONE);
250         fCountLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
251         comp.setContent(container);
252         comp.setMinHeight(250);
253         comp.setExpandHorizontal(true);
254         comp.setExpandVertical(true);
255         return container;
256     }
257     
258     private Composite createScrollArea(Composite parent){
259         Group container = new Group(parent, SWT.NONE);
260         GridLayout layout = new GridLayout(2,false);
261         layout.marginWidth = layout.marginHeight = 6;
262         container.setLayout(layout);
263         
264         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
265         gd.horizontalSpan=3;
266         container.setLayoutData(gd);
267         container.setText(PDEUIMessages.ImportWizard_DetailedPage_locate);
268     
269         Label filterLabel = new Label(container, SWT.NONE);
270         filterLabel.setText(PDEUIMessages.ImportWizard_DetailedPage_search);
271         
272         fFilterText = new Text(container, SWT.BORDER);
273         fFilterText.setText(""); //$NON-NLS-1$
274
gd = new GridData(GridData.FILL_HORIZONTAL);
275         fFilterText.setLayoutData(gd);
276             
277         return container;
278     }
279     public void setVisible(boolean visible) {
280         super.setVisible(visible);
281         if (visible)
282             fFilterText.setFocus();
283         setPageComplete(visible && fImportListViewer.getTable().getItemCount() > 0);
284         
285     }
286     protected void refreshPage() {
287         fAvailableListViewer.refresh();
288         fImportListViewer.getTable().removeAll();
289         pageChanged();
290     }
291     protected void pageChanged() {
292         updateCount();
293         handleFilter();
294         setPageComplete(fImportListViewer.getTable().getItemCount() > 0);
295     }
296     private void updateCount() {
297         fCountLabel.setText(
298             NLS.bind(PDEUIMessages.ImportWizard_DetailedPage_count, (new String JavaDoc[] {
299             new Integer JavaDoc(fImportListViewer.getTable().getItemCount()).toString(),
300             new Integer JavaDoc(fModels.length).toString()})));
301         fCountLabel.getParent().layout();
302     }
303     
304     private void handleAdd() {
305         IStructuredSelection ssel = (IStructuredSelection)fAvailableListViewer.getSelection();
306         if (ssel.size() > 0) {
307             Table table = fAvailableListViewer.getTable();
308             int index = table.getSelectionIndices()[0];
309             fAvailableListViewer.remove(ssel.toArray());
310             fImportListViewer.add(ssel.toArray());
311             table.setSelection(index < table.getItemCount() ? index : table.getItemCount() -1);
312             pageChanged();
313         }
314     }
315
316     private void handleAddAll() {
317         TableItem[] items = fAvailableListViewer.getTable().getItems();
318
319         ArrayList JavaDoc data = new ArrayList JavaDoc();
320         for (int i = 0; i < items.length; i++) {
321             data.add(items[i].getData());
322         }
323         if (data.size() > 0) {
324             fImportListViewer.add(data.toArray());
325             fAvailableListViewer.remove(data.toArray());
326             pageChanged();
327         }
328     }
329     
330     private void handleFilter() {
331         if (fFilterText == null ||fFilterText.getText().trim().length() == 0)
332             return;
333
334         String JavaDoc text = fFilterText.getText().trim();
335         if (!text.endsWith("*")) //$NON-NLS-1$
336
text += "*"; //$NON-NLS-1$
337
Pattern JavaDoc pattern = PatternConstructor.createPattern(text, true);
338         TableItem[] tableItems = fAvailableListViewer.getTable().getItems();
339         ArrayList JavaDoc results = new ArrayList JavaDoc();
340         for (int i = 0; i<tableItems.length; i++){
341             Object JavaDoc data = tableItems[i].getData();
342             if (data instanceof IPluginModelBase){
343                 IPluginModelBase model = (IPluginModelBase)data;
344                 if (pattern.matcher(model.getPluginBase().getId()).matches())
345                     results.add(tableItems[i]);
346             }
347         }
348         if (results.size()>0){
349             TableItem[] selectionList = (TableItem[])results.toArray(new TableItem[results.size()]);
350             fAvailableListViewer.getTable().setSelection(selectionList);
351         } else {
352             fAvailableListViewer.setSelection(null);
353         }
354     }
355     
356     private void handleRemove() {
357         IStructuredSelection ssel = (IStructuredSelection)fImportListViewer.getSelection();
358         if (ssel.size() > 0) {
359             Table table = fImportListViewer.getTable();
360             int index = table.getSelectionIndices()[0];
361             fImportListViewer.remove(ssel.toArray());
362             fAvailableListViewer.add(ssel.toArray());
363             table.setSelection(index < table.getItemCount() ? index : table.getItemCount() -1);
364             pageChanged();
365         }
366     }
367     
368     private void handleRemoveAll() {
369         handleRemoveAll(true);
370     }
371     
372     private void handleRemoveAll(boolean refresh) {
373         TableItem[] items = fImportListViewer.getTable().getItems();
374         
375         ArrayList JavaDoc data = new ArrayList JavaDoc();
376         for (int i = 0; i < items.length; i++) {
377             data.add(items[i].getData());
378         }
379         if (data.size() > 0) {
380             fAvailableListViewer.add(data.toArray());
381             fImportListViewer.remove(data.toArray());
382             pageChanged();
383         }
384     }
385     
386     private void handleSwap() {
387         TableItem[] aItems = fAvailableListViewer.getTable().getItems();
388         TableItem[] iItems = fImportListViewer.getTable().getItems();
389         
390         ArrayList JavaDoc data = new ArrayList JavaDoc();
391         for (int i = 0; i < iItems.length; i++) {
392             data.add(iItems[i].getData());
393         }
394         if (data.size() > 0) {
395             fAvailableListViewer.add(data.toArray());
396             fImportListViewer.remove(data.toArray());
397         }
398         
399         data.clear();
400         for (int i = 0; i < aItems.length; i++) {
401             data.add(aItems[i].getData());
402         }
403         if (data.size() > 0) {
404             fImportListViewer.add(data.toArray());
405             fAvailableListViewer.remove(data.toArray());
406         }
407         pageChanged();
408     }
409     
410     private void handleExistingProjects() {
411         handleRemoveAll(false);
412         ArrayList JavaDoc result = new ArrayList JavaDoc();
413         for (int i = 0; i < fModels.length; i++) {
414             String JavaDoc id = fModels[i].getPluginBase().getId();
415             IProject project =
416                 (IProject) PDEPlugin.getWorkspace().getRoot().findMember(id);
417             if (project != null
418                 && project.isOpen()
419                 && WorkspaceModelManager.isPluginProject(project)) {
420                 result.add(fModels[i]);
421             }
422         }
423         if (result.size() > 0) {
424             fImportListViewer.add(result.toArray());
425             fAvailableListViewer.remove(result.toArray());
426         }
427         pageChanged();
428     }
429     
430     private void handleExistingUnshared() {
431         handleRemoveAll(false);
432         ArrayList JavaDoc result = new ArrayList JavaDoc();
433         for (int i = 0; i < fModels.length; i++) {
434             String JavaDoc id = fModels[i].getPluginBase().getId();
435             IProject project =
436                 (IProject) PDEPlugin.getWorkspace().getRoot().findMember(id);
437             if (project != null
438                 && WorkspaceModelManager.isUnsharedProject(project)
439                 && WorkspaceModelManager.isPluginProject(project)) {
440                 result.add(fModels[i]);
441             }
442         }
443         if (result.size() > 0) {
444             fImportListViewer.add(result.toArray());
445             fAvailableListViewer.remove(result.toArray());
446         }
447         pageChanged();
448     }
449     
450     private void handleAddRequiredPlugins() {
451         TableItem[] items = fImportListViewer.getTable().getItems();
452         if (items.length == 0)
453             return;
454         if (items.length == 1) {
455             IPluginModelBase model = (IPluginModelBase) items[0].getData();
456             if (model.getPluginBase().getId().equals("org.eclipse.core.boot")) { //$NON-NLS-1$
457
return;
458             }
459         }
460                         
461         ArrayList JavaDoc result = new ArrayList JavaDoc();
462         for (int i = 0; i < items.length; i++) {
463             addPluginAndDependencies((IPluginModelBase) items[i].getData(), result, fAddFragmentsButton.getSelection());
464         }
465
466         handleRemoveAll(false);
467         fImportListViewer.add(result.toArray());
468         fAvailableListViewer.remove(result.toArray());
469         pageChanged();
470     }
471     
472
473 }
474
Popular Tags