KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > provisioner > update > UpdateSiteProvisionerPage


1 /*******************************************************************************
2  * Copyright (c) 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.wizards.provisioner.update;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.LabelProvider;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.pde.internal.ui.IHelpContextIds;
26 import org.eclipse.pde.internal.ui.PDELabelProvider;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.pde.internal.ui.PDEPluginImages;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.util.SWTUtil;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.KeyAdapter;
33 import org.eclipse.swt.events.KeyEvent;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.ui.PlatformUI;
43
44 public class UpdateSiteProvisionerPage extends WizardPage {
45
46     private List JavaDoc fElements = new ArrayList JavaDoc();
47     private Button fAddButton;
48     private Button fRemoveButton;
49     private Button fEditButton;
50     private TableViewer fViewer;
51
52     class UpdateSiteContentProvider implements IStructuredContentProvider {
53
54         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
55             if(inputElement instanceof List JavaDoc)
56                 return ((List JavaDoc) inputElement).toArray();
57             return null;
58         }
59
60         public void dispose() {}
61
62         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
63
64     }
65
66     protected UpdateSiteProvisionerPage(String JavaDoc pageName) {
67         super(pageName);
68         setTitle(PDEUIMessages.UpdateSiteWizardPage_title);
69         setDescription(PDEUIMessages.UpdateSiteWizardPage_description);
70         setPageComplete(false);
71     }
72
73     public void createControl(Composite parent) {
74         Composite client = new Composite(parent, SWT.NONE);
75         GridLayout layout = new GridLayout();
76         layout.marginWidth = 2;
77         layout.numColumns = 2;
78         client.setLayout(layout);
79         client.setLayoutData(new GridData(GridData.FILL_BOTH));
80
81         Label label = new Label(client, SWT.None);
82         label.setText(PDEUIMessages.UpdateSiteWizardPage_label);
83         GridData gd = new GridData();
84         gd.horizontalSpan = 2;
85         label.setLayoutData(gd);
86
87         fViewer = new TableViewer(client, SWT.BORDER);
88         fViewer.setContentProvider(new UpdateSiteContentProvider());
89         fViewer.setLabelProvider(new LabelProvider() {
90
91             public Image getImage(Object JavaDoc element) {
92                 PDELabelProvider provider = PDEPlugin.getDefault().getLabelProvider();
93                 return provider.get(PDEPluginImages.DESC_SITE_OBJ);
94             }
95
96             public String JavaDoc getText(Object JavaDoc element) {
97                 IUpdateSiteProvisionerEntry entry =
98                     (IUpdateSiteProvisionerEntry) element;
99                 return entry.getSiteLocation();
100             }
101
102         });
103         fViewer.setInput(fElements);
104         gd = new GridData(GridData.FILL_BOTH);
105         gd.verticalSpan = 3;
106         fViewer.getControl().setLayoutData(gd);
107
108         fViewer.addSelectionChangedListener(new ISelectionChangedListener () {
109
110             public void selectionChanged(SelectionChangedEvent event) {
111                 updateButtons();
112             }
113
114         });
115
116         fViewer.getControl().addKeyListener(new KeyAdapter() {
117             public void keyPressed(KeyEvent event) {
118                 if (event.character == SWT.DEL &&
119                         event.stateMask == 0) {
120                     if(fViewer.getSelection() != null) {
121                         handleRemove();
122                     }
123                 }
124             }
125         });
126
127         PlatformUI.getWorkbench().getHelpSystem().setHelp(client,
128                 IHelpContextIds.UPDATE_SITE_PROVISIONING_PAGE);
129         createButtons(client);
130         setControl(client);
131     }
132
133     protected void createButtons(Composite parent) {
134         fAddButton = new Button(parent, SWT.PUSH);
135         fAddButton.setText(PDEUIMessages.UpdateSiteWizardPage_add);
136         fAddButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
137         SWTUtil.setButtonDimensionHint(fAddButton);
138         fAddButton.addSelectionListener(new SelectionAdapter() {
139             public void widgetSelected(SelectionEvent e) {
140                 handleAdd();
141             }
142         });
143
144         fEditButton = new Button(parent, SWT.PUSH);
145         fEditButton.setText(PDEUIMessages.UpdateSiteWizardPage_edit);
146         fEditButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
147         SWTUtil.setButtonDimensionHint(fEditButton);
148         fEditButton.addSelectionListener(new SelectionAdapter() {
149             public void widgetSelected(SelectionEvent e) {
150                 IUpdateSiteProvisionerEntry entry =
151                     (IUpdateSiteProvisionerEntry) ((IStructuredSelection) fViewer.getSelection()).getFirstElement();
152                 handleEdit(entry);
153             }
154         });
155
156         fRemoveButton = new Button(parent, SWT.PUSH);
157         fRemoveButton.setText(PDEUIMessages.UpdateSiteWizardPage_remove);
158         fRemoveButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
159         SWTUtil.setButtonDimensionHint(fRemoveButton);
160         fRemoveButton.addSelectionListener(new SelectionAdapter() {
161             public void widgetSelected(SelectionEvent e) {
162                 handleRemove();
163             }
164         });
165         updateButtons();
166     }
167
168     protected void handleRemove() {
169         Object JavaDoc[] elements = ((IStructuredSelection)fViewer.getSelection()).toArray();
170         for (int i = 0; i < elements.length; i++)
171             fElements.remove(elements[i]);
172
173         fViewer.refresh();
174         updateButtons();
175         fViewer.getTable().select(fElements.size() - 1);
176         setPageComplete(!fElements.isEmpty());
177     }
178
179     protected void handleAdd() {
180         UpdateSiteProvisionerDialog dialog = new UpdateSiteProvisionerDialog(
181                 getShell(),
182                 null,
183                 null,
184                 PDEUIMessages.UpdateSiteProvisionerDialog_addTitle
185         );
186         int status = dialog.open();
187         if (status == Window.OK) {
188             fElements.add(dialog.getEntry());
189             fViewer.refresh();
190             fViewer.getTable().select(fElements.indexOf(dialog.getEntry()));
191             setPageComplete(true);
192             updateButtons();
193         }
194     }
195
196     protected void handleEdit(IUpdateSiteProvisionerEntry entry) {
197         UpdateSiteProvisionerDialog dialog = new UpdateSiteProvisionerDialog(
198                 getShell(),
199                 entry.getInstallLocation(),
200                 entry.getSiteLocation(),
201                 PDEUIMessages.UpdateSiteProvisionerDialog_editTitle
202         );
203         int status = dialog.open();
204         if (status == Window.OK) {
205             fElements.remove(entry);
206             fElements.add(dialog.getEntry());
207             fViewer.refresh();
208             fViewer.getTable().select(fElements.indexOf(dialog.getEntry()));
209             setPageComplete(true);
210         }
211         updateButtons();
212     }
213
214     protected void updateButtons() {
215         int num = fViewer.getTable().getSelectionCount();
216         fRemoveButton.setEnabled(num > 0);
217         fEditButton.setEnabled(num == 1);
218     }
219
220     public IUpdateSiteProvisionerEntry[] getEntries() {
221         return (IUpdateSiteProvisionerEntry[]) fElements.toArray(new UpdateSiteProvisionerEntry[fElements.size()]);
222     }
223
224 }
225
Popular Tags