KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > TargetSiteDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.update.internal.ui.wizards;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Observable JavaDoc;
17 import java.util.Observer JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredContentProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.ITableLabelProvider;
29 import org.eclipse.jface.viewers.LabelProvider;
30 import org.eclipse.jface.viewers.SelectionChangedEvent;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.jface.viewers.TableViewer;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.jface.viewers.ViewerFilter;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.DirectoryDialog;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.update.configuration.IConfiguredSite;
49 import org.eclipse.update.configuration.IInstallConfiguration;
50 import org.eclipse.update.core.ISite;
51 import org.eclipse.update.internal.operations.UpdateUtils;
52 import org.eclipse.update.internal.ui.UpdateLabelProvider;
53 import org.eclipse.update.internal.ui.UpdateUI;
54 import org.eclipse.update.internal.ui.UpdateUIMessages;
55 import org.eclipse.update.internal.ui.parts.DefaultContentProvider;
56 import org.eclipse.update.internal.ui.parts.SWTUtil;
57 import org.eclipse.update.operations.IInstallFeatureOperation;
58
59 public class TargetSiteDialog extends Dialog {
60     protected static final String JavaDoc MOST_RECEANTLY_USED_SITE_URL = "mostReceantlyUsedSiteURL"; //$NON-NLS-1$
61
private TableViewer siteViewer;
62     private IInstallConfiguration config;
63     private Button addButton;
64     private Button deleteButton;
65     private IInstallFeatureOperation[] jobs;
66     private WorkingCopy workingCopy;
67     
68     class WorkingCopy extends Observable JavaDoc {
69         private ArrayList JavaDoc sites=new ArrayList JavaDoc();
70         private ArrayList JavaDoc added=new ArrayList JavaDoc();
71         private IConfiguredSite targetSite;
72
73         public WorkingCopy() {
74             Object JavaDoc [] initial = config.getConfiguredSites();
75             for (int i=0; i<initial.length; i++)
76                 sites.add(initial[i]);
77             for (int i=0; i<jobs.length; i++) {
78                 IConfiguredSite jsite = jobs[i].getTargetSite();
79                 if (targetSite==null)
80                     targetSite = jsite;
81                 else
82                     if (!targetSite.equals(jsite))
83                         targetSite = null;
84             }
85         }
86         
87         public void addSite(IConfiguredSite site) {
88             sites.add(site);
89             added.add(site);
90             setChanged();
91             notifyObservers(site);
92             clearChanged();
93         }
94         
95         public void removeSite(IConfiguredSite site) {
96             sites.remove(site);
97             added.remove(site);
98             setChanged();
99             notifyObservers(site);
100             clearChanged();
101         }
102         
103         public boolean isNewlyAdded(IConfiguredSite site) {
104             return added.contains(site);
105         }
106         
107         public void commit() {
108             // add new sites to the config
109
for (int i=0; i<added.size(); i++) {
110                 config.addConfiguredSite((IConfiguredSite)added.get(i));
111             }
112             // set selected site to the job
113
for (int i=0; i<jobs.length; i++) {
114                 jobs[i].setTargetSite(targetSite);
115             }
116         }
117         
118         public IConfiguredSite [] getSites() {
119             return (IConfiguredSite[])sites.toArray(new IConfiguredSite[sites.size()]);
120         }
121         
122         public IConfiguredSite [] getAddedSites() {
123             return (IConfiguredSite[])added.toArray(new IConfiguredSite[added.size()]);
124         }
125         
126         public IConfiguredSite getTargetSite() {
127             return targetSite;
128         }
129         
130         public void setTargetSite(IConfiguredSite site) {
131             this.targetSite = site;
132         }
133     }
134     
135     class SitesContentProvider extends DefaultContentProvider implements IStructuredContentProvider {
136         public Object JavaDoc[] getElements(Object JavaDoc parent) {
137             return workingCopy.getSites();
138         }
139     }
140     
141     class SitesLabelProvider extends LabelProvider implements ITableLabelProvider {
142         
143         public Image getColumnImage(Object JavaDoc obj, int col) {
144             UpdateLabelProvider provider = UpdateUI.getDefault().getLabelProvider();
145             return provider.getLocalSiteImage((IConfiguredSite) obj);
146         }
147     
148         public String JavaDoc getColumnText(Object JavaDoc obj, int col) {
149             if (col == 0) {
150                 ISite site = ((IConfiguredSite) obj).getSite();
151                 return new File JavaDoc(site.getURL().getFile()).toString();
152             }
153             return null;
154         }
155     }
156
157
158     /**
159      * Constructor for ReviewPage2
160      */

161     public TargetSiteDialog(Shell parentShell, IInstallConfiguration config, IInstallFeatureOperation[] jobs) {
162         super(parentShell);
163         this.config = config;
164         UpdateUI.getDefault().getLabelProvider().connect(this);
165         this.jobs = jobs;
166         workingCopy = new WorkingCopy();
167     }
168
169     public boolean close() {
170         UpdateUI.getDefault().getLabelProvider().disconnect(this);
171         return super.close();
172     }
173
174     public Control createDialogArea(Composite parent) {
175         Composite client = new Composite(parent, SWT.NULL);
176         GridLayout layout = new GridLayout();
177         layout.numColumns = 2;
178         layout.marginWidth = layout.marginHeight = 10;
179         client.setLayout(layout);
180         client.setLayoutData(new GridData(GridData.FILL_BOTH));
181
182         Composite leftPanel = new Composite(client, SWT.NULL);
183         GridLayout centerLayout = new GridLayout();
184         centerLayout.numColumns = 1;
185         centerLayout.marginWidth = centerLayout.marginHeight = 0;
186         leftPanel.setLayout(centerLayout);
187         leftPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
188         
189         Label label = new Label(leftPanel, SWT.NULL);
190         label.setText(UpdateUIMessages.InstallWizard_TargetPage_siteLabel);
191         createSiteViewer(leftPanel);
192
193         Composite rightPanel = new Composite(client, SWT.NULL);
194         GridLayout rightLayout = new GridLayout();
195         rightLayout.numColumns = 1;
196         rightLayout.marginWidth = rightLayout.marginHeight = 0;
197         rightPanel.setLayout(rightLayout);
198         rightPanel.setLayoutData(new GridData(GridData.FILL_VERTICAL));
199
200         new Label(rightPanel, SWT.NULL);
201         Composite buttonContainer = new Composite(rightPanel, SWT.NULL);
202         GridLayout blayout = new GridLayout();
203         blayout.marginWidth = blayout.marginHeight = 0;
204         buttonContainer.setLayout(blayout);
205         buttonContainer.setLayoutData(new GridData(GridData.FILL_VERTICAL));
206         
207         addButton = new Button(buttonContainer, SWT.PUSH);
208         addButton.setText(UpdateUIMessages.InstallWizard_TargetPage_new);
209         addButton.addSelectionListener(new SelectionAdapter() {
210             public void widgetSelected(SelectionEvent e) {
211                 addTargetLocation();
212             }
213         });
214
215         addButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
216         SWTUtil.setButtonDimensionHint(addButton);
217         
218         deleteButton = new Button(buttonContainer, SWT.PUSH);
219         deleteButton.setText(UpdateUIMessages.InstallWizard_TargetPage_delete);
220         deleteButton.addSelectionListener(new SelectionAdapter() {
221             public void widgetSelected(SelectionEvent e) {
222                 try {
223                     removeSelection();
224                 }
225                 catch (CoreException ex) {
226                     UpdateUI.logException(ex);
227                 }
228             }
229         });
230         deleteButton.setEnabled(false);
231         deleteButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
232         SWTUtil.setButtonDimensionHint(deleteButton);
233
234         PlatformUI.getWorkbench().getHelpSystem().setHelp(client, "org.eclipse.update.ui.MultiTargetPage2"); //$NON-NLS-1$
235
Dialog.applyDialogFont(parent);
236         
237         siteViewer.setInput(jobs[0]);
238         IConfiguredSite affinitySite = UpdateUtils.getDefaultTargetSite(config, jobs[0], true);
239         if (jobs[0].getTargetSite() != null)
240             siteViewer.setSelection(new StructuredSelection(jobs[0].getTargetSite()));
241         addButton.setEnabled(affinitySite == null);
242         
243         return client;
244     }
245
246     private void createSiteViewer(Composite parent) {
247         siteViewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
248         GridData gd = new GridData(GridData.FILL_BOTH);
249         gd.widthHint = 200;
250         siteViewer.getTable().setLayoutData(gd);
251         siteViewer.setContentProvider(new SitesContentProvider());
252         siteViewer.setLabelProvider(new SitesLabelProvider());
253         siteViewer.addFilter(new ViewerFilter() {
254             public boolean select(Viewer v, Object JavaDoc parent, Object JavaDoc obj) {
255                 IInstallFeatureOperation job = (IInstallFeatureOperation) siteViewer.getInput();
256                 return getSiteVisibility((IConfiguredSite) obj, job);
257             }
258         });
259         siteViewer.addSelectionChangedListener(new ISelectionChangedListener() {
260             public void selectionChanged(SelectionChangedEvent event) {
261                 IStructuredSelection ssel = (IStructuredSelection) event.getSelection();
262                 selectTargetSite(ssel);
263                 updateButtons(ssel);
264             }
265         });
266         workingCopy.addObserver(new Observer JavaDoc() {
267             public void update(Observable JavaDoc arg0, Object JavaDoc arg1) {
268                 siteViewer.refresh();
269             }
270         });
271     }
272     
273     protected void okPressed() {
274         workingCopy.commit();
275         super.okPressed();
276     }
277     
278     protected void createButtonsForButtonBar(Composite parent) {
279         super.createButtonsForButtonBar(parent);
280         updateButtons((IStructuredSelection)siteViewer.getSelection());
281     }
282
283     private void updateButtons(IStructuredSelection selection) {
284         deleteButton.setEnabled(canDelete(selection));
285         Button okButton = getButton(IDialogConstants.OK_ID);
286         if (okButton!=null)
287             okButton.setEnabled(!selection.isEmpty());
288     }
289     
290     private boolean canDelete(IStructuredSelection selection) {
291         if (selection.isEmpty()) return false;
292         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
293             IConfiguredSite site = (IConfiguredSite)iter.next();
294             if (!workingCopy.isNewlyAdded(site))
295                 return false;
296         }
297         return true;
298     }
299
300     private void selectTargetSite(IStructuredSelection selection) {
301         IConfiguredSite site = (IConfiguredSite) selection.getFirstElement();
302         if (site!=null) {
303             IDialogSettings master = UpdateUI.getDefault().getDialogSettings();
304             IDialogSettings section = master.getSection(MOST_RECEANTLY_USED_SITE_URL);
305             if (section==null)
306                 section = master.addNewSection(MOST_RECEANTLY_USED_SITE_URL);
307             section.put(MOST_RECEANTLY_USED_SITE_URL, site.getSite().getURL().toExternalForm());
308             workingCopy.setTargetSite(site);
309         }
310     }
311
312     private void addTargetLocation() {
313         DirectoryDialog dd = new DirectoryDialog(getShell());
314         dd.setMessage(UpdateUIMessages.InstallWizard_TargetPage_location_message);
315         String JavaDoc path = dd.open();
316         if (path != null) {
317             addConfiguredSite(getShell(), config, new File JavaDoc(path));
318         }
319     }
320     
321     private void removeSelection() throws CoreException {
322         IStructuredSelection selection = (IStructuredSelection) siteViewer.getSelection();
323         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
324             IConfiguredSite targetSite = (IConfiguredSite) iter.next();
325             workingCopy.removeSite(targetSite);
326         }
327         siteViewer.refresh();
328     }
329
330     private IConfiguredSite addConfiguredSite(Shell shell, IInstallConfiguration config, File JavaDoc file) {
331         try {
332             IConfiguredSite csite = config.createConfiguredSite(file);
333             IStatus status = csite.verifyUpdatableStatus();
334             if (status.isOK())
335                 workingCopy.addSite(csite);
336             else
337                 throw new CoreException(status);
338             siteViewer.setSelection(new StructuredSelection(csite));
339             siteViewer.getControl().setFocus();
340             return csite;
341         } catch (CoreException e) {
342             String JavaDoc title = UpdateUIMessages.InstallWizard_TargetPage_location_error_title;
343             ErrorDialog.openError(shell, title, null, e.getStatus());
344             UpdateUI.logException(e,false);
345             return null;
346         }
347     }
348     
349     private boolean getSiteVisibility(IConfiguredSite site, IInstallFeatureOperation job) {
350         // Do not allow installing into a non-updateable site
351
if (!site.isUpdatable())
352             return false;
353         
354         // If affinity site is known, only it should be shown
355
IConfiguredSite affinitySite = UpdateUtils.getAffinitySite(config, job.getFeature());
356         if (affinitySite != null) {
357             // Must compare referenced sites because
358
// configured sites themselves may come from
359
// different configurations
360
return site.getSite().equals(affinitySite.getSite());
361         }
362         
363         // Co-locate updates with the old feature
364
if (job.getOldFeature() != null) {
365             IConfiguredSite oldSite = UpdateUtils.getSiteWithFeature(config, job.getOldFeature().getVersionedIdentifier().getIdentifier());
366             return (site == oldSite);
367         }
368
369         // Allow installing into any site that is updateable and there is no affinity specified
370
return true;
371     }
372     
373     public IConfiguredSite [] getAddedSites() {
374         return workingCopy.getAddedSites();
375     }
376 }
377
Popular Tags