KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.net.URL JavaDoc;
14
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.CheckStateChangedEvent;
18 import org.eclipse.jface.viewers.CheckboxTableViewer;
19 import org.eclipse.jface.viewers.ICheckStateListener;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.IStructuredContentProvider;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.jface.viewers.ViewerSorter;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.BusyIndicator;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.TableItem;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.forms.HyperlinkSettings;
41 import org.eclipse.ui.forms.widgets.ScrolledFormText;
42 import org.eclipse.update.internal.core.UpdateCore;
43 import org.eclipse.update.internal.core.UpdateManagerUtils;
44 import org.eclipse.update.internal.ui.UpdateUI;
45 import org.eclipse.update.internal.ui.UpdateUIImages;
46 import org.eclipse.update.internal.ui.UpdateUIMessages;
47 import org.eclipse.update.internal.ui.model.DiscoveryFolder;
48 import org.eclipse.update.internal.ui.model.SiteBookmark;
49 import org.eclipse.update.internal.ui.model.SiteCategory;
50 import org.eclipse.update.internal.ui.model.UpdateModel;
51 import org.eclipse.update.internal.ui.parts.SWTUtil;
52 import org.eclipse.update.operations.IUpdateModelChangedListener;
53 import org.eclipse.update.search.EnvironmentFilter;
54 import org.eclipse.update.search.UpdateSearchRequest;
55 import org.eclipse.update.search.UpdateSearchScope;
56
57 public class SitePage extends BannerPage implements ISearchProvider {
58
59     class SitesLabelProvider extends LabelProvider {
60
61         public Image getImage(Object JavaDoc obj) {
62             if (obj instanceof SiteBookmark)
63                 return UpdateUI.getDefault().getLabelProvider().get(
64                     UpdateUIImages.DESC_SITE_OBJ);
65             return super.getImage(obj);
66         }
67
68         public String JavaDoc getText(Object JavaDoc obj) {
69             if (obj instanceof SiteBookmark) {
70                 return ((SiteBookmark) obj).getLabel();
71             }
72             return super.getText(obj);
73         }
74     }
75
76     class ModelListener implements IUpdateModelChangedListener {
77         public void objectChanged(Object JavaDoc object, String JavaDoc property) {
78             viewer.refresh();
79             checkItems();
80         }
81
82         public void objectsAdded(Object JavaDoc parent, Object JavaDoc[] children) {
83             viewer.refresh();
84             checkItems();
85         }
86
87         public void objectsRemoved(Object JavaDoc parent, Object JavaDoc[] children) {
88             viewer.refresh();
89             checkItems();
90         }
91     }
92
93     private static DiscoveryFolder discoveryFolder = new DiscoveryFolder();
94     private CheckboxTableViewer viewer;
95     private ScrolledFormText descLabel;
96     private Button addSiteButton;
97     private Button addLocalButton;
98     private Button addLocalZippedButton;
99     private Button editButton;
100     private Button removeButton;
101     private Button exportButton;
102     private Button importButton;
103     private Button envFilterCheck;
104     private Button automaticallySelectMirrorsCheckbox;
105     private EnvironmentFilter envFilter;
106     private UpdateSearchRequest searchRequest;
107     private ModelListener modelListener;
108     
109     private boolean automaticallySelectMirrors = true;
110
111     public SitePage(UpdateSearchRequest searchRequest) {
112         super("SitePage"); //$NON-NLS-1$
113
this.searchRequest = searchRequest;
114         setTitle(UpdateUIMessages.SitePage_title);
115         setDescription(UpdateUIMessages.SitePage_desc);
116         UpdateUI.getDefault().getLabelProvider().connect(this);
117         envFilter = new EnvironmentFilter();
118
119         modelListener = new ModelListener();
120         UpdateUI.getDefault().getUpdateModel().addUpdateModelChangedListener(
121             modelListener);
122     }
123
124     private void toggleEnvFilter(boolean add) {
125         if (add)
126             searchRequest.addFilter(envFilter);
127         else
128             searchRequest.removeFilter(envFilter);
129     }
130
131     public void dispose() {
132         UpdateUI.getDefault().getLabelProvider().disconnect(this);
133         UpdateUI
134             .getDefault()
135             .getUpdateModel()
136             .removeUpdateModelChangedListener(
137             modelListener);
138         super.dispose();
139     }
140
141     /*
142      * (non-Javadoc) @see
143      * org.eclipse.update.internal.ui.wizards.BannerPage#createContents(org.eclipse.swt.widgets.Composite)
144      */

145     protected Control createContents(Composite parent) {
146         Composite client = new Composite(parent, SWT.NULL);
147         GridLayout layout = new GridLayout();
148         layout.numColumns = 2;
149         client.setLayout(layout);
150
151         Label label = new Label(client, SWT.NULL);
152         label.setText(UpdateUIMessages.SitePage_label);
153         GridData gd = new GridData();
154         gd.horizontalSpan = 2;
155         label.setLayoutData(gd);
156
157         createViewer(client);
158
159         Composite buttonContainer = new Composite(client, SWT.NULL);
160         buttonContainer.setLayoutData(new GridData(GridData.FILL_VERTICAL));
161         layout = new GridLayout();
162         layout.marginWidth = 0;
163         layout.marginHeight = 0;
164         buttonContainer.setLayout(layout);
165
166         addSiteButton = new Button(buttonContainer, SWT.PUSH);
167         addSiteButton.setText(UpdateUIMessages.SitePage_addUpdateSite);
168         addSiteButton.setLayoutData(
169             new GridData(GridData.HORIZONTAL_ALIGN_FILL));
170         SWTUtil.setButtonDimensionHint(addSiteButton);
171         addSiteButton.addSelectionListener(new SelectionAdapter() {
172             public void widgetSelected(SelectionEvent e) {
173                 handleAddSite();
174             }
175         });
176
177         addLocalButton = new Button(buttonContainer, SWT.PUSH);
178         addLocalButton.setText(UpdateUIMessages.SitePage_addLocalSite);
179         addLocalButton.setLayoutData(
180             new GridData(GridData.HORIZONTAL_ALIGN_FILL));
181         SWTUtil.setButtonDimensionHint(addLocalButton);
182         addLocalButton.addSelectionListener(new SelectionAdapter() {
183             public void widgetSelected(SelectionEvent e) {
184                 handleAddLocal();
185             }
186         });
187
188         addLocalZippedButton = new Button(buttonContainer, SWT.PUSH);
189         addLocalZippedButton.setText(UpdateUIMessages.SitePage_addLocalZippedSite);
190         addLocalZippedButton.setLayoutData(
191             new GridData(GridData.HORIZONTAL_ALIGN_FILL));
192         SWTUtil.setButtonDimensionHint(addLocalZippedButton);
193         addLocalZippedButton.addSelectionListener(new SelectionAdapter() {
194             public void widgetSelected(SelectionEvent e) {
195                 handleAddLocalZipped();
196             }
197         });
198         
199         // separator
200
new Label(buttonContainer, SWT.None);
201         
202         editButton = new Button(buttonContainer, SWT.PUSH);
203         editButton.setText(UpdateUIMessages.SitePage_edit);
204         editButton.setEnabled(false);
205         editButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
206         SWTUtil.setButtonDimensionHint(editButton);
207         editButton.addSelectionListener(new SelectionAdapter() {
208             public void widgetSelected(SelectionEvent e) {
209                 handleEdit();
210             }
211         });
212
213         removeButton = new Button(buttonContainer, SWT.PUSH);
214         removeButton.setText(UpdateUIMessages.SitePage_remove);
215         removeButton.setEnabled(false);
216         removeButton.setLayoutData(
217             new GridData(GridData.HORIZONTAL_ALIGN_FILL));
218         SWTUtil.setButtonDimensionHint(removeButton);
219         removeButton.addSelectionListener(new SelectionAdapter() {
220             public void widgetSelected(SelectionEvent e) {
221                 handleRemove();
222             }
223         });
224         
225         // separator
226
new Label(buttonContainer, SWT.None);
227         
228         importButton = new Button(buttonContainer, SWT.PUSH);
229         importButton.setText(UpdateUIMessages.SitePage_import);
230         importButton.setLayoutData(
231             new GridData(GridData.HORIZONTAL_ALIGN_FILL));
232         SWTUtil.setButtonDimensionHint(importButton);
233         importButton.addSelectionListener(new SelectionAdapter() {
234             public void widgetSelected(SelectionEvent e) {
235                 handleImport();
236             }
237         });
238         
239         exportButton = new Button(buttonContainer, SWT.PUSH);
240         exportButton.setText(UpdateUIMessages.SitePage_export);
241         exportButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
242         SWTUtil.setButtonDimensionHint(exportButton);
243         exportButton.addSelectionListener(new SelectionAdapter() {
244             public void widgetSelected(SelectionEvent e) {
245                 handleExport();
246             }
247         });
248
249         descLabel = new ScrolledFormText(client, true);
250         descLabel.setText(""); //$NON-NLS-1$
251
descLabel.setBackground(parent.getBackground());
252         HyperlinkSettings settings = new HyperlinkSettings(parent.getDisplay());
253         descLabel.getFormText().setHyperlinkSettings(settings);
254         
255         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
256         gd.horizontalSpan = 1;
257         gd.heightHint = 30;
258         descLabel.setLayoutData(gd);
259         
260         envFilterCheck = new Button(client, SWT.CHECK);
261         envFilterCheck.setText(UpdateUIMessages.SitePage_ignore);
262         envFilterCheck.setSelection(true);
263         toggleEnvFilter(true);
264         envFilterCheck.addSelectionListener(new SelectionAdapter() {
265             public void widgetSelected(SelectionEvent e) {
266                 toggleEnvFilter(envFilterCheck.getSelection());
267             }
268         });
269         gd = new GridData();
270         gd.horizontalSpan = 2;
271         gd.verticalAlignment = SWT.BOTTOM;
272         envFilterCheck.setLayoutData(gd);
273
274         
275         automaticallySelectMirrorsCheckbox = new Button(client, SWT.CHECK);
276         automaticallySelectMirrorsCheckbox.setText(UpdateUIMessages.SitePage_automaticallySelectMirrors);
277         automaticallySelectMirrorsCheckbox.setSelection(true);
278         automaticallySelectMirrorsCheckbox.addSelectionListener(new SelectionAdapter() {
279             public void widgetSelected(SelectionEvent e) {
280                 automaticallySelectMirrors = automaticallySelectMirrorsCheckbox.getSelection();
281                 UpdateCore.getPlugin().getPluginPreferences().setValue(UpdateCore.P_AUTOMATICALLY_CHOOSE_MIRROR, automaticallySelectMirrors);
282             }
283         });
284         gd = new GridData();
285         gd.horizontalSpan = 2;
286         gd.verticalAlignment = SWT.BOTTOM;
287         automaticallySelectMirrorsCheckbox.setLayoutData(gd);
288         
289         Dialog.applyDialogFont(parent);
290
291         PlatformUI.getWorkbench().getHelpSystem().setHelp(client, "org.eclipse.update.ui.SitePage"); //$NON-NLS-1$
292

293         return client;
294     }
295
296     private void createViewer(Composite parent) {
297         viewer =
298             CheckboxTableViewer.newCheckList(
299                 parent,
300                 SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
301         viewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
302         viewer.setContentProvider(new IStructuredContentProvider() {
303             public Object JavaDoc[] getElements(Object JavaDoc parent) {
304                 return getAllSiteBookmarks();
305             }
306
307             public void dispose() {
308             }
309
310             public void inputChanged(Viewer viewer, Object JavaDoc oldInput,
311                     Object JavaDoc newInput) {
312             }
313         });
314         viewer.setLabelProvider(new SitesLabelProvider());
315         viewer.setInput(UpdateUI.getDefault().getUpdateModel());
316         
317         // bug # 83212
318
viewer.setSorter( new ViewerSorter());
319         
320         
321         initializeItems();
322
323         viewer.addCheckStateListener(new ICheckStateListener() {
324             public void checkStateChanged(CheckStateChangedEvent e) {
325                 Object JavaDoc element = e.getElement();
326                 if (element instanceof SiteBookmark)
327                     handleSiteChecked((SiteBookmark) element, e.getChecked());
328             }
329         });
330
331         viewer
332             .addSelectionChangedListener(new ISelectionChangedListener() {
333             public void selectionChanged(SelectionChangedEvent e) {
334                 handleSelectionChanged((IStructuredSelection) e.getSelection());
335             }
336         });
337
338     }
339
340     private void initializeItems() {
341         checkItems();
342         updateSearchRequest();
343     }
344
345     private void checkItems() {
346         TableItem[] items = viewer.getTable().getItems();
347         for (int i = 0; i < items.length; i++) {
348             SiteBookmark bookmark = (SiteBookmark) items[i].getData();
349             viewer.setChecked(bookmark, bookmark.isSelected());
350         }
351     }
352
353     private void handleAddSite() {
354         NewUpdateSiteDialog dialog = new NewUpdateSiteDialog(getShell(), getAllSiteBookmarks());
355         dialog.create();
356         dialog.getShell().setText(UpdateUIMessages.SitePage_new);
357         if (dialog.open() == NewUpdateSiteDialog.OK)
358             updateSearchRequest();
359     }
360
361     private void handleAddLocal() {
362         SiteBookmark siteBookmark = LocalSiteSelector.getLocaLSite(getShell(), this.getAllSiteBookmarks());
363         if (siteBookmark != null) {
364             if (handleNameEdit(siteBookmark) == EditSiteDialog.OK) {
365                 siteBookmark.setSelected(true);
366                 UpdateModel model = UpdateUI.getDefault().getUpdateModel();
367                 model.addBookmark(siteBookmark);
368                 model.saveBookmarks();
369                 updateSearchRequest();
370             }
371         }
372         return;
373     }
374
375     private void handleAddLocalZipped() {
376         SiteBookmark siteBookmark = LocalSiteSelector
377                 .getLocaLZippedSite(getShell(), this.getAllSiteBookmarks());
378         if (siteBookmark != null) {
379             if (handleNameEdit(siteBookmark) == EditSiteDialog.OK) {
380                 siteBookmark.setSelected(true);
381                 UpdateModel model = UpdateUI.getDefault().getUpdateModel();
382                 model.addBookmark(siteBookmark);
383                 model.saveBookmarks();
384                 updateSearchRequest();
385             }
386         }
387         return;
388     }
389
390     private void handleRemove() {
391         BusyIndicator
392             .showWhile(viewer.getControl().getDisplay(), new Runnable JavaDoc() {
393             public void run() {
394                 UpdateModel updateModel =
395                     UpdateUI.getDefault().getUpdateModel();
396                 IStructuredSelection ssel =
397                     (IStructuredSelection) viewer.getSelection();
398                 SiteBookmark bookmark = (SiteBookmark) ssel.getFirstElement();
399                 String JavaDoc selName = bookmark.getLabel();
400                 boolean answer = MessageDialog
401                                 .openQuestion(
402                                         getShell(),
403                                         UpdateUIMessages.SitePage_remove_location_conf_title,
404                                         UpdateUIMessages.SitePage_remove_location_conf
405                                                 + " " + selName); //$NON-NLS-1$
406

407                 if (answer && !bookmark.isReadOnly()) {
408                     updateModel.removeBookmark(bookmark);
409                     updateSearchRequest();
410                 }
411             }
412         });
413     }
414
415     private void handleEdit() {
416         IStructuredSelection ssel =
417             (IStructuredSelection) viewer.getSelection();
418         SiteBookmark bookmark = (SiteBookmark) ssel.getFirstElement();
419         URL JavaDoc oldURL = bookmark.getURL();
420         EditSiteDialog dialog = new EditSiteDialog(getShell(), bookmark, getAllSiteBookmarks());
421         dialog.create();
422         String JavaDoc title = bookmark.isLocal() ? UpdateUIMessages.SitePage_dialogEditLocal : UpdateUIMessages.SitePage_dialogEditUpdateSite;
423                                                                                                                                                   // //$NON-NLS-2$
424
dialog.getShell().setText(title);
425         if (dialog.open() == EditSiteDialog.OK ) {
426             URL JavaDoc newURL = bookmark.getURL();
427             if (!UpdateManagerUtils.sameURL(oldURL, newURL)) {
428                 UpdateModel model = UpdateUI.getDefault().getUpdateModel();
429                 model.fireObjectChanged(bookmark, null);
430                 updateSearchRequest();
431             }
432         }
433     }
434
435     private void handleImport() {
436         SiteBookmark[] siteBookmarks = SitesImportExport.getImportedBookmarks(getShell());
437         if (siteBookmarks != null && siteBookmarks.length > 0) {
438             UpdateModel model = UpdateUI.getDefault().getUpdateModel();
439             SiteBookmark[] currentBookmarks = getAllSiteBookmarks();
440             for (int i=0; i<siteBookmarks.length; i++) {
441                 boolean siteExists = false;
442                 for (int j=0; !siteExists && j<currentBookmarks.length; j++)
443                     if (currentBookmarks[j].getURL().equals(siteBookmarks[i].getURL()))
444                         siteExists = true;
445                 if (!siteExists)
446                     model.addBookmark(siteBookmarks[i]);
447             }
448             model.saveBookmarks();
449             updateSearchRequest();
450         }
451         return;
452     }
453     
454     private void handleExport() {
455         SitesImportExport.exportBookmarks(getShell(), getAllSiteBookmarks());
456     }
457     
458     private int handleNameEdit(SiteBookmark bookmark) {
459         EditSiteDialog dialog = new EditSiteDialog(getShell(), bookmark, getAllSiteBookmarks(), true);
460         dialog.create();
461         String JavaDoc title = bookmark.isLocal() ? UpdateUIMessages.SitePage_dialogEditLocal : UpdateUIMessages.SitePage_dialogEditUpdateSite;
462         // //$NON-NLS-2$
463
dialog.getShell().setText(title);
464         int rc = dialog.open();
465         return rc;
466     }
467
468     private void handleSiteChecked(SiteBookmark bookmark, boolean checked) {
469         if (bookmark.isUnavailable()) {
470             bookmark.setSelected(false);
471             viewer.setChecked(bookmark, false);
472             return;
473         }
474         
475         bookmark.setSelected(checked);
476         updateSearchRequest();
477     }
478
479
480     private void handleSelectionChanged(IStructuredSelection ssel) {
481         boolean enable = false;
482         Object JavaDoc item = ssel.getFirstElement();
483         String JavaDoc description = null;
484         if (item instanceof SiteBookmark) {
485             enable = !((SiteBookmark) item).isReadOnly();
486             description = ((SiteBookmark)item).getDescription();
487         } else if (item instanceof SiteCategory) {
488 // IURLEntry descEntry = ((SiteCategory)item).getCategory().getDescription();
489
// if (descEntry != null)
490
// description = descEntry.getAnnotation();
491
}
492         editButton.setEnabled(enable);
493         removeButton.setEnabled(enable);
494
495         if (description == null)
496             description = ""; //$NON-NLS-1$
497
descLabel.setText(UpdateManagerUtils.getWritableXMLString(description));
498     }
499
500     private void updateSearchRequest() {
501         Object JavaDoc[] checked = viewer.getCheckedElements();
502
503         UpdateSearchScope scope = new UpdateSearchScope();
504         int nsites = 0;
505
506         for (int i = 0; i < checked.length; i++) {
507             if (checked[i] instanceof SiteBookmark) {
508                 SiteBookmark bookmark = (SiteBookmark) checked[i];
509                 scope.addSearchSite(
510                     bookmark.getLabel(),
511                     bookmark.getURL(),
512                     bookmark.getIgnoredCategories());
513                 nsites++;
514             }
515         }
516         searchRequest.setScope(scope);
517         setPageComplete(nsites > 0);
518     }
519
520     public UpdateSearchRequest getSearchRequest() {
521         return searchRequest;
522     }
523     
524     public void setVisible(boolean value) {
525         super.setVisible(value);
526         if (value) {
527             // Reset all unavailable sites, so they can be tried again if the user wants it
528
SiteBookmark[] bookmarks = getAllSiteBookmarks();
529             for (int i=0; i<bookmarks.length; i++) {
530                 if (bookmarks[i].isUnavailable())
531                     bookmarks[i].setUnavailable(false);
532             }
533             automaticallySelectMirrors = UpdateCore.getPlugin().getPluginPreferences().getBoolean(UpdateCore.P_AUTOMATICALLY_CHOOSE_MIRROR);
534             automaticallySelectMirrorsCheckbox.setSelection(automaticallySelectMirrors);
535         }
536     }
537     
538     private SiteBookmark[] getAllSiteBookmarks() {
539         UpdateModel model = UpdateUI.getDefault().getUpdateModel();
540         Object JavaDoc[] bookmarks = model.getBookmarkLeafs();
541         Object JavaDoc[] sitesToVisit =
542             discoveryFolder.getChildren(discoveryFolder);
543         SiteBookmark[] all = new SiteBookmark[bookmarks.length + sitesToVisit.length];
544         System.arraycopy(bookmarks, 0, all, 0, bookmarks.length);
545         System.arraycopy(
546             sitesToVisit,
547             0,
548             all,
549             bookmarks.length,
550             sitesToVisit.length);
551         return all;
552     }
553
554     public boolean isPageComplete() {
555         return viewer.getCheckedElements().length != 0;
556     }
557 }
558
Popular Tags