KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.text.DecimalFormat JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.dialogs.IDialogSettings;
27 import org.eclipse.jface.resource.ImageDescriptor;
28 import org.eclipse.jface.viewers.DoubleClickEvent;
29 import org.eclipse.jface.viewers.IDoubleClickListener;
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.IStructuredContentProvider;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.jface.viewers.SelectionChangedEvent;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.jface.viewers.TableViewer;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.jface.viewers.ViewerSorter;
40 import org.eclipse.osgi.util.NLS;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.ControlAdapter;
43 import org.eclipse.swt.events.ControlEvent;
44 import org.eclipse.swt.events.SelectionAdapter;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.graphics.Image;
47 import org.eclipse.swt.graphics.Point;
48 import org.eclipse.swt.graphics.Rectangle;
49 import org.eclipse.swt.layout.GridData;
50 import org.eclipse.swt.layout.GridLayout;
51 import org.eclipse.swt.widgets.Button;
52 import org.eclipse.swt.widgets.Composite;
53 import org.eclipse.swt.widgets.Control;
54 import org.eclipse.swt.widgets.Event;
55 import org.eclipse.swt.widgets.Label;
56 import org.eclipse.swt.widgets.Listener;
57 import org.eclipse.swt.widgets.Menu;
58 import org.eclipse.swt.widgets.MenuItem;
59 import org.eclipse.swt.widgets.Table;
60 import org.eclipse.swt.widgets.TableColumn;
61 import org.eclipse.ui.PlatformUI;
62 import org.eclipse.update.configuration.IConfiguredSite;
63 import org.eclipse.update.configuration.IInstallConfiguration;
64 import org.eclipse.update.configuration.IInstallConfigurationChangedListener;
65 import org.eclipse.update.configuration.LocalSystemInfo;
66 import org.eclipse.update.core.IFeature;
67 import org.eclipse.update.core.ISite;
68 import org.eclipse.update.internal.core.ConfiguredSite;
69 import org.eclipse.update.internal.core.UpdateCore;
70 import org.eclipse.update.internal.operations.UpdateUtils;
71 import org.eclipse.update.internal.ui.UpdateLabelProvider;
72 import org.eclipse.update.internal.ui.UpdateUI;
73 import org.eclipse.update.internal.ui.UpdateUIImages;
74 import org.eclipse.update.internal.ui.UpdateUIMessages;
75 import org.eclipse.update.internal.ui.parts.DefaultContentProvider;
76 import org.eclipse.update.internal.ui.parts.SWTUtil;
77 import org.eclipse.update.operations.IInstallFeatureOperation;
78
79 public class TargetPage extends BannerPage implements IDynamicPage {
80     
81     private static final int FEATURE_NAME_COLUMN = 0;
82     private static final int FEATURE_VERSION_COLUMN = 1;
83     private static final int FEATURE_SIZE_COLUMN = 2;
84     private static final int INSTALLATION_DIRECTORY_COLUMN = 3;
85     
86     private float featureNameColumnProcetange = 0.25f;
87     private float featureVersionColumnProcetange = 0.25f;
88     private float featureSizeColumnProcetange = 0.15f;
89     
90     private TableViewer jobViewer;
91     private IInstallConfiguration config;
92     private Label requiredSpaceLabel;
93     private Label availableSpaceLabel;
94     private IInstallFeatureOperation[] jobs;
95     //private IInstallFeatureOperation currentJob;
96
private Label installLocation;
97     private Button changeLocation;
98     private IConfiguredSite [] addedSites;
99     
100     private boolean isUpdate; // whether the wizard is updating a feature or installing a new one
101

102     class JobsContentProvider
103         extends DefaultContentProvider
104         implements IStructuredContentProvider {
105         public Object JavaDoc[] getElements(Object JavaDoc parent) {
106             return jobs;
107         }
108     }
109
110     class JobsLabelProvider
111         extends LabelProvider
112         implements ITableLabelProvider {
113             
114         public Image getColumnImage(Object JavaDoc obj, int col) {
115             
116             if (col > 0) {
117                 return null;
118             }
119             
120             UpdateLabelProvider provider = UpdateUI.getDefault().getLabelProvider();
121
122             IInstallFeatureOperation job = (IInstallFeatureOperation) obj;
123             ImageDescriptor base =
124                 job.getFeature().isPatch()
125                     ? UpdateUIImages.DESC_EFIX_OBJ
126                     : UpdateUIImages.DESC_FEATURE_OBJ;
127             int flags = 0;
128             if (job.getTargetSite() == null)
129                 flags = UpdateLabelProvider.F_ERROR;
130             return provider.get(base, flags);
131         }
132
133         public String JavaDoc getColumnText(Object JavaDoc obj, int col) {
134             
135             IFeature feature = ((IInstallFeatureOperation) obj).getFeature();
136             IConfiguredSite csite =((IInstallFeatureOperation)obj).getTargetSite();
137             ISite site = csite!=null?csite.getSite():null;
138             if (col == FEATURE_NAME_COLUMN) {
139                 return feature.getLabel();
140             } else if (col == FEATURE_VERSION_COLUMN) {
141                 return feature.getVersionedIdentifier().getVersion().toString();
142             } else if (col == FEATURE_SIZE_COLUMN) {
143                 if (site==null)
144                     return ""; //$NON-NLS-1$
145
long requiredSpace = site.getDownloadSizeFor(feature) + site.getInstallSizeFor(feature);
146                 return getSizeString(requiredSpace);
147             } else if (col == INSTALLATION_DIRECTORY_COLUMN) {
148                 if (site==null)
149                     return ""; //$NON-NLS-1$
150
return site.getURL().getFile().toString();
151             }
152             return ""; //$NON-NLS-1$
153
}
154     }
155     
156     class JobViewerSorter extends ViewerSorter {
157         
158         
159         private static final int ASCENDING = 0;
160
161         private static final int DESCENDING = 1;
162
163         private int column;
164
165         private int direction;
166
167         public void doSort(int column) {
168
169             if (column == this.column) {
170                 direction = 1 - direction;
171             } else {
172                 this.column = column;
173                 direction = ASCENDING;
174             }
175         }
176
177         /**
178          * Compares the object for sorting
179          */

180         public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
181
182             int rc = 0;
183             
184             IFeature feature1 = ((IInstallFeatureOperation) o1).getFeature();
185             IFeature feature2 = ((IInstallFeatureOperation) o2).getFeature();
186
187             String JavaDoc featureName1 = feature1.getLabel();
188             String JavaDoc featureName2 = feature2.getLabel();
189             
190             String JavaDoc featureId1 = feature1.getVersionedIdentifier().getVersion().toString();
191             String JavaDoc featureId2 = feature2.getVersionedIdentifier().getVersion().toString();
192
193             String JavaDoc installationDirectory1 = ((IInstallFeatureOperation)o1).getTargetSite().getSite().getURL().getFile().toString();
194             String JavaDoc installationDirectory2 = ((IInstallFeatureOperation)o2).getTargetSite().getSite().getURL().getFile().toString();
195
196             switch (column) {
197             case FEATURE_NAME_COLUMN:
198                 rc = collator.compare(featureName1, featureName2);
199                 break;
200             case FEATURE_VERSION_COLUMN:
201                 rc = collator.compare(featureId1, featureId2);
202                 break;
203             case INSTALLATION_DIRECTORY_COLUMN:
204                 rc = collator.compare(installationDirectory1, installationDirectory2);
205                 break;
206             }
207
208             // If descending order, flip the direction
209
if (direction == DESCENDING)
210                 rc = -rc;
211
212             return rc;
213         }
214     }
215     
216     class ConfigListener implements IInstallConfigurationChangedListener {
217         public void installSiteAdded(IConfiguredSite csite) {
218             
219             // set the site as target for all jobs without a target
220
for (int i=0; jobs != null && i<jobs.length; i++)
221                 if (jobs[i].getTargetSite() == null && getSiteVisibility(csite, jobs[i])) {
222                     jobs[i].setTargetSite(csite);
223                 }
224
225             jobViewer.refresh();
226         }
227
228         public void installSiteRemoved(IConfiguredSite csite) {
229             // remove the target site for all jobs that use it
230
// set the site as target for all jobs without a target
231
boolean refreshJobs = false;
232             for (int i=0; jobs != null && i<jobs.length; i++)
233                 if (jobs[i].getTargetSite() == csite) {
234                     jobs[i].setTargetSite(null);
235                     refreshJobs = true;
236                 }
237                 
238             pageChanged();
239             
240             jobViewer.refresh();
241             if (refreshJobs) {
242                 jobViewer.getControl().setFocus();
243             }
244         }
245     }
246
247     /**
248      * Constructor for ReviewPage2
249      */

250     public TargetPage(IInstallConfiguration config, boolean isUpdate) {
251         super("Target"); //$NON-NLS-1$
252
setTitle(UpdateUIMessages.InstallWizard_TargetPage_title);
253         setDescription(UpdateUIMessages.InstallWizard_TargetPage_desc);
254         this.config = config;
255         UpdateUI.getDefault().getLabelProvider().connect(this);
256         this.isUpdate = isUpdate;
257     }
258
259     public void setJobs(IInstallFeatureOperation[] jobs) {
260         //we need only unique features
261
//using Set here allows us to get rid of a duplicate elements
262
Set JavaDoc jobsSet = new HashSet JavaDoc();
263         for (int i = 0; i < jobs.length; i++) {
264             jobsSet.add(jobs[i]);
265         }
266         
267         this.jobs = (IInstallFeatureOperation[]) jobsSet.toArray(new IInstallFeatureOperation[jobsSet.size()]);
268     }
269
270     public void dispose() {
271         UpdateUI.getDefault().getLabelProvider().disconnect(this);
272 // config.removeInstallConfigurationChangedListener(configListener);
273
super.dispose();
274     }
275
276     public Control createContents(Composite parent) {
277         Composite client = new Composite(parent, SWT.NULL);
278         GridLayout layout = new GridLayout();
279         layout.numColumns = 3;
280         layout.marginWidth = layout.marginHeight = 0;
281         client.setLayout(layout);
282         client.setLayoutData(new GridData(GridData.FILL_BOTH));
283         
284         Label label = new Label(client, SWT.NULL);
285         label.setText(UpdateUIMessages.InstallWizard_TargetPage_jobsLabel);
286         createJobViewer(client);
287
288         label = new Label(client, SWT.NULL);
289         label.setText(UpdateUIMessages.InstallWizard_TargetPage_location);
290         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
291         label.setLayoutData(gd);
292
293         installLocation = new Label(client, SWT.NULL);
294         installLocation.setText(""); //$NON-NLS-1$
295
gd = new GridData(GridData.FILL_HORIZONTAL);
296         installLocation.setLayoutData(gd);
297         
298         changeLocation = new Button(client, SWT.PUSH);
299         changeLocation.setText(UpdateUIMessages.InstallWizard_TargetPage_location_change);
300         changeLocation.addSelectionListener(new SelectionAdapter() {
301             public void widgetSelected(SelectionEvent e) {
302                 changeLocationOfFeatures();
303             }
304
305             
306         });
307             
308         Composite status = new Composite(client, SWT.NULL);
309         gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
310         gd.horizontalSpan = 3;
311         status.setLayoutData(gd);
312         layout = new GridLayout();
313         layout.numColumns = 2;
314         status.setLayout(layout);
315         label = new Label(status, SWT.NULL);
316         label.setText(UpdateUIMessages.InstallWizard_TargetPage_requiredSpace);
317         requiredSpaceLabel = new Label(status, SWT.NULL);
318         requiredSpaceLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
319         label = new Label(status, SWT.NULL);
320         label.setText(UpdateUIMessages.InstallWizard_TargetPage_availableSpace);
321         availableSpaceLabel = new Label(status, SWT.NULL);
322         availableSpaceLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
323
324         PlatformUI.getWorkbench().getHelpSystem().setHelp(client, "org.eclipse.update.ui.MultiTargetPage2"); //$NON-NLS-1$
325

326         Dialog.applyDialogFont(parent);
327         
328         return client;
329     }
330
331     private void createJobViewer(Composite parent) {
332         
333         final Table table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.MULTI | SWT.RESIZE | SWT.FULL_SELECTION);
334         
335         TableColumn featureNameColumn = new TableColumn(table, SWT.LEFT, FEATURE_NAME_COLUMN);
336         featureNameColumn.setText(UpdateUIMessages.TargetPage_FeatureNameColumn);
337         featureNameColumn.setWidth(75);
338         
339         TableColumn featureVersionColumn = new TableColumn(table, SWT.LEFT, FEATURE_VERSION_COLUMN);
340         featureVersionColumn.setText(UpdateUIMessages.TargetPage_Feature_Version);
341         featureVersionColumn.setWidth(75);
342         
343         TableColumn featureSizeColumn = new TableColumn(table, SWT.LEFT, FEATURE_SIZE_COLUMN);
344         featureSizeColumn.setText(UpdateUIMessages.TargetPage_Feature_Size);
345         featureSizeColumn.setWidth(75);
346         
347         TableColumn featureLocationColumn = new TableColumn(table, SWT.LEFT, INSTALLATION_DIRECTORY_COLUMN);
348         featureLocationColumn.setText(UpdateUIMessages.TargetPage_InstallationDirectoryColumn);
349         featureLocationColumn.setWidth(75);
350         jobViewer = new TableViewer(table);
351         
352         //jobViewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.MULTI);
353
GridData gd = new GridData(GridData.FILL_BOTH);
354         gd.horizontalSpan = 3;
355         gd.widthHint = 150;
356         gd.heightHint = 200;
357         jobViewer.getTable().setLayoutData(gd);
358         jobViewer.setContentProvider(new JobsContentProvider());
359         jobViewer.setLabelProvider(new JobsLabelProvider());
360         jobViewer.setSorter(new JobViewerSorter());
361         
362         jobViewer.addDoubleClickListener(new IDoubleClickListener() {
363                 public void doubleClick(DoubleClickEvent event) {
364                     changeLocationOfFeatures();
365                 }
366             }
367         );
368         
369         table.addListener(SWT.MenuDetect, new Listener() {
370               public void handleEvent(Event event) {
371                   Menu menu = new Menu (getShell(), SWT.POP_UP);
372                     MenuItem item = new MenuItem (menu, SWT.PUSH);
373                     item.setText(UpdateUIMessages.InstallWizard_TargetPage_location_change);
374                     item.addListener(SWT.Selection, new Listener () {
375                             public void handleEvent (Event e) {
376                                 changeLocationOfFeatures();
377                             }
378                         }
379                     );
380                     menu.setLocation (event.x, event.y);
381                     menu.setVisible (true);
382               }
383             }
384         );
385         featureNameColumn.addSelectionListener(new SelectionAdapter() {
386                 public void widgetSelected(SelectionEvent event) {
387                     ((JobViewerSorter) jobViewer.getSorter()).doSort(FEATURE_NAME_COLUMN);
388                     jobViewer.refresh();
389                 }
390             }
391         );
392         
393         featureVersionColumn.addSelectionListener(new SelectionAdapter() {
394                 public void widgetSelected(SelectionEvent event) {
395                     ((JobViewerSorter) jobViewer.getSorter()).doSort(FEATURE_VERSION_COLUMN);
396                     jobViewer.refresh();
397                 }
398             }
399         );
400         
401         featureSizeColumn.addSelectionListener(new SelectionAdapter() {
402                 public void widgetSelected(SelectionEvent event) {
403                     ((JobViewerSorter) jobViewer.getSorter()).doSort(FEATURE_SIZE_COLUMN);
404                     jobViewer.refresh();
405                 }
406             }
407         );
408         
409         featureLocationColumn.addSelectionListener(new SelectionAdapter() {
410                 public void widgetSelected(SelectionEvent event) {
411                     ((JobViewerSorter) jobViewer.getSorter()).doSort(INSTALLATION_DIRECTORY_COLUMN);
412                     jobViewer.refresh();
413                 }
414             }
415         );
416         
417         table.addControlListener(new ControlAdapter() {
418             public void controlResized(ControlEvent e) {
419
420
421                 Rectangle area = table.getClientArea();
422                 Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
423
424                 int tableWidth = area.width - 3 * table.getBorderWidth();
425                 if (preferredSize.y > area.height + table.getHeaderHeight()){
426                     Point vBarSize = table.getVerticalBar().getSize();
427                     tableWidth -= vBarSize.x;
428                 }
429                 
430                 TableColumn featureNameColumn = table.getColumn(FEATURE_NAME_COLUMN);
431                 TableColumn featureVersionColumn = table.getColumn(FEATURE_VERSION_COLUMN);
432                 TableColumn featureSizeColumn = table.getColumn(FEATURE_SIZE_COLUMN);
433                 TableColumn featureLocationColumn = table.getColumn(INSTALLATION_DIRECTORY_COLUMN);
434
435                 featureNameColumn.setWidth((int)(tableWidth * featureNameColumnProcetange));
436                 featureVersionColumn.setWidth((int)(tableWidth * featureVersionColumnProcetange));
437                 featureSizeColumn.setWidth((int)(tableWidth * featureSizeColumnProcetange));
438                 featureLocationColumn.setWidth(tableWidth - featureNameColumn.getWidth() - featureVersionColumn.getWidth() - featureSizeColumn.getWidth());
439
440                 
441                 featureNameColumnProcetange = featureNameColumn.getWidth() / (float)tableWidth;
442                 featureVersionColumnProcetange = featureVersionColumn.getWidth() / (float)tableWidth;
443                 featureSizeColumnProcetange = featureSizeColumn.getWidth() / (float)tableWidth;
444             }
445         });
446
447
448         jobViewer.addSelectionChangedListener(new ISelectionChangedListener() {
449             public void selectionChanged(SelectionChangedEvent event) {
450                 updateStatus();
451
452             }
453         });
454         
455         table.setHeaderVisible(true);
456         table.setLinesVisible(true);
457     }
458
459     public void setVisible(boolean visible) {
460         if (visible) {
461             initializeDefaultTargetSites();
462             jobViewer.setInput(jobs);
463             if (jobViewer.getSelection().isEmpty() && jobs.length > 0)
464                 jobViewer.setSelection(new StructuredSelection(jobs[0]));
465         }
466         
467         super.setVisible(visible);
468     }
469
470     private void verifyNotEmpty(boolean empty) {
471         String JavaDoc errorMessage = null;
472         if (empty)
473             errorMessage = UpdateUIMessages.InstallWizard_TargetPage_location_empty;
474         setErrorMessage(errorMessage);
475         setPageComplete(!empty);
476     }
477
478
479     private void updateStatus() {
480         
481         IStructuredSelection selectedJobs = (IStructuredSelection)jobViewer.getSelection();
482         IInstallFeatureOperation selectedJob = (IInstallFeatureOperation)selectedJobs.getFirstElement();
483     
484         requiredSpaceLabel.setText(""); //$NON-NLS-1$
485
availableSpaceLabel.setText(""); //$NON-NLS-1$
486
installLocation.setText(""); //$NON-NLS-1$
487

488         if (selectedJob == null) {
489             return;
490         }
491         
492         IConfiguredSite site = selectedJob.getTargetSite(); /*(IConfiguredSite) element;*/
493         File JavaDoc file = new File JavaDoc(site.getSite().getURL().getFile());
494         long available = -1;
495         long required = -1;
496         if (areAllTargetSitesSame()) {
497             available = LocalSystemInfo.getFreeSpace(file);
498             required = computeRequiredSizeFor(site);
499             //add the download size to space required to do operation since all plugins and nonplugin data will be downloaded first
500
required += computeDownloadSizeFor(site);
501             installLocation.setText(new File JavaDoc(selectedJob.getTargetSite().getSite().getURL().getFile()).toString());
502         }
503         
504         if (available == LocalSystemInfo.SIZE_UNKNOWN)
505             available = -1;
506         
507         requiredSpaceLabel.setText(getSizeString(required));
508         availableSpaceLabel.setText(getSizeString(available));
509         
510         
511         //if the available space was retireved from the OS and the required space is greater that the available space, do not let the user continue
512
if(available != LocalSystemInfo.SIZE_UNKNOWN && required > available){
513             this.setPageComplete(false);
514             //TODO: set error message: "...not enough space..."
515
}else
516             this.setPageComplete(true);
517     }
518     
519     private boolean areAllTargetSitesSame() {
520         IStructuredSelection selectedJobs = (IStructuredSelection)jobViewer.getSelection();
521         Iterator JavaDoc iterator = selectedJobs.iterator();
522         URL JavaDoc site = null;
523         if (iterator != null) {
524             while(iterator.hasNext()) {
525                 IInstallFeatureOperation current = (IInstallFeatureOperation) iterator.next();
526                 if (site != null) {
527                     if (!site.equals(current.getTargetSite().getSite().getURL())) {
528                         return false;
529                     }
530                 } else {
531                     site = current.getTargetSite().getSite().getURL();
532                 }
533             }
534             return true;
535         }
536         return false;
537     }
538
539     private String JavaDoc getSizeString(long size) {
540         if (size <= 0) {
541             return UpdateUIMessages.InstallWizard_TargetPage_unknownSize;
542         } else {
543             double order = 1024.0;
544             double sizeInMB = size / order;
545             double sizeInGB = size / order / order;
546             if ( sizeInMB < 1) {
547                 return NLS.bind(UpdateUIMessages.InstallWizard_TargetPage_size_KB, "" + size); //$NON-NLS-1$
548
} else {
549                 String JavaDoc pattern = "#.##"; //$NON-NLS-1$
550
DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc(pattern);
551                 if (sizeInGB < 1) {
552                     return NLS.bind(UpdateUIMessages.InstallWizard_TargetPage_size_MB, formatter.format(sizeInMB));
553                 } else {
554                     return NLS.bind(UpdateUIMessages.InstallWizard_TargetPage_size_GB, formatter.format(sizeInGB));
555                 }
556             }
557         }
558     }
559
560     private long computeRequiredSizeFor(IConfiguredSite site) {
561         long totalSize = 0;
562         for (int i = 0; i < jobs.length; i++) {
563             if (site.equals(jobs[i].getTargetSite())) {
564                 long jobSize = site.getSite().getInstallSizeFor(jobs[i].getFeature());
565                 if (jobSize == -1)
566                     return -1;
567                 totalSize += jobSize;
568             }
569         }
570         return totalSize;
571     }
572     
573     private long computeDownloadSizeFor(IConfiguredSite site) {
574         long totalSize = 0;
575         for (int i = 0; i < jobs.length; i++) {
576             if (site.equals(jobs[i].getTargetSite())) {
577                 long jobSize = site.getSite().getDownloadSizeFor(jobs[i].getFeature());
578                 if (jobSize == -1)
579                     return -1;
580                 totalSize += jobSize;
581             }
582         }
583         return totalSize;
584     }
585
586     private void pageChanged() {
587         boolean empty = false;
588         for (int i=0; jobs!=null && i<jobs.length; i++) {
589             if (jobs[i].getTargetSite() == null) {
590                 empty = true;
591                 break;
592             }
593         
594             IFeature feature = jobs[i].getFeature();
595             if (feature.isPatch()) {
596                 // Patches must go together with the features
597
// they are patching.
598

599                 // Check current jobs
600
IInstallFeatureOperation patchedFeatureJob = findPatchedFeature(feature);
601                 if (patchedFeatureJob != null
602                     && patchedFeatureJob.getTargetSite() != null
603                     && !jobs[i].getTargetSite().equals(patchedFeatureJob.getTargetSite())) {
604                     String JavaDoc msg = NLS.bind(UpdateUIMessages.InstallWizard_TargetPage_patchError, (new String JavaDoc[] {
605                     feature.getLabel(),
606                     patchedFeatureJob.getFeature().getLabel()}));
607                     setErrorMessage(msg);
608                     setPageComplete(false);
609                     return;
610                 }
611                 // Check installed features
612
IFeature patchedFeature = UpdateUtils.getPatchedFeature(feature);
613                 if (patchedFeature != null
614                     && !jobs[i].getTargetSite().equals(patchedFeature.getSite().getCurrentConfiguredSite())) {
615                     String JavaDoc msg = NLS.bind(UpdateUIMessages.InstallWizard_TargetPage_patchError2, (new String JavaDoc[] {
616                     feature.getLabel(),
617                     patchedFeature.getLabel(),
618                     patchedFeature.getSite().getCurrentConfiguredSite().getSite().getURL().getFile()}));
619                     setErrorMessage(msg);
620                     setPageComplete(false);
621                     return;
622                 }
623             }
624         }
625         verifyNotEmpty(empty);
626     }
627     
628     
629     private boolean getSiteVisibility(IConfiguredSite site, IInstallFeatureOperation job) {
630         // Do not allow installing into a non-updateable site
631
if (!site.isUpdatable())
632             return false;
633         
634         // If affinity site is known, only it should be shown
635
IConfiguredSite affinitySite = UpdateUtils.getAffinitySite(config, job.getFeature());
636         if (affinitySite != null) {
637             // Must compare referenced sites because
638
// configured sites themselves may come from
639
// different configurations
640
return site.getSite().equals(affinitySite.getSite());
641         }
642         
643         // Co-locate updates with the old feature
644
if (job.getOldFeature() != null) {
645             IConfiguredSite oldSite = UpdateUtils.getSiteWithFeature(config, job.getOldFeature().getVersionedIdentifier().getIdentifier());
646             return (site == oldSite);
647         }
648
649         // Allow installing into any site that is updateable and there is no affinity specified
650
return true;
651     }
652     
653     private void initializeDefaultTargetSites() {
654         
655         
656         IConfiguredSite mostReceantlyUsedSite = getMostReceantlyUsedSite();
657         for (int i = 0; i < jobs.length; i++) {
658             if (jobs[i].getTargetSite() != null)
659                 continue;
660             
661             IConfiguredSite affinitySite = UpdateUtils.getAffinitySite(config, jobs[i].getFeature());
662             if (affinitySite != null) {
663                 jobs[i].setTargetSite(affinitySite);
664                 continue;
665             }
666
667             IConfiguredSite defaultSite = UpdateUtils.getDefaultTargetSite(config, jobs[i], false);
668             if (defaultSite != null) {
669                 jobs[i].setTargetSite(defaultSite);
670                 continue;
671             }
672             
673             if (mostReceantlyUsedSite != null) {
674                 jobs[i].setTargetSite(mostReceantlyUsedSite);
675                 continue;
676             }
677             
678             IConfiguredSite csite = getFirstTargetSite(jobs[i]);
679             if (csite == null && Platform.getInstallLocation().isReadOnly() && isUpdate == false) {
680                 // there are no updateable sites, the installation location is read-only and we are installing a new feature
681
// make an update site in the configuration area
682
String JavaDoc configurationLocation = Platform.getConfigurationLocation().getURL().getFile();
683                 File JavaDoc site = new File JavaDoc(configurationLocation);
684                 if (!ConfiguredSite.canWrite(site)) {
685                     // if we cannot write to the configuration area then make an update site in the user's home directory
686
site = new File JavaDoc(System.getProperty("user.home") + File.separator + ".eclipse" + File.separator + //$NON-NLS-1$ //$NON-NLS-2$
687
Platform.getProduct().getId() + File.separator + "updates"); //$NON-NLS-1$
688
}
689                 try {
690                     csite = config.createConfiguredSite(site);
691                     config.addConfiguredSite(csite);
692                     IStatus status = csite.verifyUpdatableStatus();
693                     if (!status.isOK())
694                         throw new CoreException(status);
695                 } catch (CoreException e) {
696                     // there was a problem, the user must choose an installation site
697
csite = null;
698                     // no need to check if the directory exists because File.delete() returns false if it's not there
699
deleteDir(site);
700                 }
701             }
702
703             jobs[i].setTargetSite(csite);
704         }
705     }
706     
707         private boolean deleteDir(File JavaDoc dir) {
708         if (dir.isDirectory()) {
709             String JavaDoc[] files = dir.list();
710             for (int i = 0; i < files.length; i++) {
711                 if (!deleteDir(new File JavaDoc(dir, files[i]))) {
712                     return false;
713                 }
714             }
715         }
716         return dir.delete();
717     }
718     
719     private IConfiguredSite getMostReceantlyUsedSite() {
720         IDialogSettings master = UpdateUI.getDefault().getDialogSettings();
721         IDialogSettings section = master.getSection(TargetSiteDialog.MOST_RECEANTLY_USED_SITE_URL);
722         if (section == null) {
723             return null;
724         }
725         String JavaDoc mostReceantlyUsedSiteURLString = section.get(TargetSiteDialog.MOST_RECEANTLY_USED_SITE_URL);
726         if (mostReceantlyUsedSiteURLString == null) {
727             return null;
728         }
729         
730         URL JavaDoc mostReceantlyUsedSiteURL = null;
731         try {
732             mostReceantlyUsedSiteURL = new URL JavaDoc(mostReceantlyUsedSiteURLString);
733         } catch (MalformedURLException JavaDoc mue) {
734             UpdateCore.log("Url format is wrong for the mostReceantlyUsedSiteURL in preferences", mue); //$NON-NLS-1$
735
mue.printStackTrace();
736             return null;
737         }
738         IConfiguredSite[] sites = config.getConfiguredSites();
739         for (int i = 0; i < sites.length; i++) {
740             IConfiguredSite configuredSite = sites[i];
741             if (mostReceantlyUsedSiteURL.equals(configuredSite.getSite().getURL())) {
742                 return configuredSite;
743             }
744         }
745         return null;
746     }
747     
748
749     private IConfiguredSite getFirstTargetSite(IInstallFeatureOperation job) {
750         IConfiguredSite[] sites = config.getConfiguredSites();
751         for (int i = 0; i < sites.length; i++) {
752             IConfiguredSite csite = sites[i];
753             if (getSiteVisibility(csite, job) && csite.getSite().getCurrentConfiguredSite().verifyUpdatableStatus().isOK())
754                 return csite;
755         }
756         return null;
757     }
758     
759     public IInstallFeatureOperation findPatchedFeature(IFeature patch) {
760         for (int i=0; i<jobs.length; i++) {
761             IFeature target = jobs[i].getFeature();
762             if (!target.equals(patch) && UpdateUtils.isPatch(target, patch))
763                 return jobs[i];
764         }
765         return null;
766             }
767     
768     /* (non-Javadoc)
769      * @see org.eclipse.jface.wizard.IWizardPage#isPageComplete()
770      */

771     public boolean isPageComplete() {
772         // check if all jobs have a target site set
773
for (int i = 0; jobs != null && i < jobs.length; i++) {
774             if (jobs[i].getTargetSite() == null)
775                 return false;
776         }
777         return super.isPageComplete();
778     }
779
780     /**
781      * @param job
782      */

783 /* private void setTargetLocation(IInstallFeatureOperation job) {
784         if (job != null && job.getTargetSite() != null) {
785             installLocation.setText(new File(job.getTargetSite().getSite().getURL().getFile()).toString());
786             updateStatus(job.getTargetSite());
787         }
788     }*/

789     
790     private IInstallFeatureOperation[] toJobArray(Iterator JavaDoc selectedJobs) {
791         
792         if (selectedJobs == null)
793             return new IInstallFeatureOperation[0];
794         
795         ArrayList JavaDoc result = new ArrayList JavaDoc();
796         
797         while (selectedJobs.hasNext()) {
798             result.add(selectedJobs.next());
799         }
800         
801         return (IInstallFeatureOperation[])result.toArray(new IInstallFeatureOperation[result.size()]);
802     }
803
804     private void changeLocationOfFeatures() {
805         IStructuredSelection selection = (IStructuredSelection) jobViewer.getSelection();
806         if (selection == null)
807             return;
808         Iterator JavaDoc selectedJob = selection.iterator();
809         if (selectedJob == null)
810             return;
811         
812         TargetSiteDialog dialog = new TargetSiteDialog(getShell(), config, toJobArray(selection.iterator()));
813         dialog.create();
814
815         SWTUtil.setDialogSize(dialog, 400, 300);
816         
817         dialog.getShell().setText(UpdateUIMessages.SitePage_new);
818         if ( dialog.open() == Dialog.OK) {
819             pageChanged();
820             jobViewer.refresh();
821             updateStatus();
822             addedSites = dialog.getAddedSites();
823         }
824     }
825     
826     void removeAddedSites() {
827         if (addedSites!=null) {
828             for (int i=0; i<addedSites.length; i++) {
829                 config.removeConfiguredSite(addedSites[i]);
830             }
831         }
832     }
833 }
834
Popular Tags