KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > wizards > GlobalRefreshWizardSelectionPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.team.internal.ui.wizards;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.*;
21 import org.eclipse.jface.wizard.*;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.team.internal.ui.*;
29 import org.eclipse.team.internal.ui.registry.SynchronizeWizardDescription;
30 import org.eclipse.team.internal.ui.synchronize.SynchronizeManager;
31 import org.eclipse.team.ui.TeamUI;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.model.BaseWorkbenchContentProvider;
34 import org.eclipse.ui.views.navigator.ResourceSorter;
35
36 /**
37  * Page that allows the user to select a set of resources that are managed
38  * by a synchronize participant.
39  *
40  * Remembers last participant
41  *
42  * @since 3.0
43  */

44 public class GlobalRefreshWizardSelectionPage extends WizardPage implements IDoubleClickListener, ISelectionChangedListener {
45     
46     private final static String JavaDoc DEFAULT_SELECTION= TeamUIPlugin.ID + "GlobalRefreshWizardSelectionPage.default_selection"; //$NON-NLS-1$
47

48     private TableViewer fViewer;
49     private IWizard wizard;
50     private List JavaDoc createdImages;
51
52     class MyContentProvider extends BaseWorkbenchContentProvider {
53         public Object JavaDoc[] getChildren(Object JavaDoc element) {
54             if(element instanceof SynchronizeManager) {
55                 SynchronizeManager manager = (SynchronizeManager)element;
56                 return manager.getWizardDescriptors();
57             }
58             return super.getChildren(element);
59         }
60     }
61     
62     class MyLabelProvider extends LabelProvider {
63         public String JavaDoc getText(Object JavaDoc element) {
64             if(element instanceof SynchronizeWizardDescription) {
65                 SynchronizeWizardDescription descriptor = (SynchronizeWizardDescription)element;
66                 return descriptor.getName();
67             }
68             return null;
69         }
70         
71         public Image getImage(Object JavaDoc element) {
72             if(element instanceof SynchronizeWizardDescription) {
73                 SynchronizeWizardDescription descriptor = (SynchronizeWizardDescription)element;
74                 ImageDescriptor d = descriptor.getImageDescriptor();
75                 if(createdImages == null) {
76                     createdImages = new ArrayList JavaDoc(3);
77                 }
78                 Image image = d.createImage();
79                 createdImages.add(image);
80                 return image;
81             }
82             return null;
83         }
84     }
85         
86     public GlobalRefreshWizardSelectionPage() {
87         super(TeamUIMessages.GlobalRefreshParticipantSelectionPage_0);
88         setDescription(TeamUIMessages.GlobalRefreshParticipantSelectionPage_1);
89         setTitle(TeamUIMessages.GlobalRefreshParticipantSelectionPage_2);
90     }
91
92     /* (non-Javadoc)
93      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
94      */

95     public void dispose() {
96         if (createdImages != null) {
97             for (Iterator JavaDoc it = createdImages.iterator(); it.hasNext();) {
98                 Image image = (Image) it.next();
99                 image.dispose();
100             }
101         }
102     }
103     
104     /**
105      * Save the page settings into the dialog settings
106      */

107     public void savePageSettings() {
108         if (fViewer.getControl().isDisposed())
109             return;
110         
111         final IStructuredSelection selection= (IStructuredSelection)fViewer.getSelection();
112         final Object JavaDoc selected= selection.getFirstElement();
113         if (!(selected instanceof SynchronizeWizardDescription))
114             return;
115         getDialogSettings().put(DEFAULT_SELECTION, ((SynchronizeWizardDescription)selected).getId());
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
120      */

121     public void createControl(Composite parent2) {
122         Composite top = new Composite(parent2, SWT.NULL);
123         top.setLayout(new GridLayout());
124         setControl(top);
125         PlatformUI.getWorkbench().getHelpSystem().setHelp(top, IHelpContextIds.REFRESH_WIZARD_SELECTION_PAGE);
126         
127         Label l = new Label(top, SWT.NULL);
128         l.setText(TeamUIMessages.GlobalRefreshParticipantSelectionPage_3);
129         fViewer = new TableViewer(top, SWT.BORDER);
130         GridData data = new GridData(GridData.FILL_BOTH);
131         fViewer.getControl().setLayoutData(data);
132         fViewer.setContentProvider(new MyContentProvider());
133         fViewer.addDoubleClickListener(this);
134         fViewer.setLabelProvider(new MyLabelProvider());
135         fViewer.setSorter(new ResourceSorter(ResourceSorter.NAME));
136         fViewer.setInput(TeamUI.getSynchronizeManager());
137         fViewer.addSelectionChangedListener(this);
138         
139         final SynchronizeWizardDescription selected= getDefaultSelection();
140         if (selected != null) {
141             fViewer.setSelection(new StructuredSelection(selected));
142         } else {
143             final Object JavaDoc object= fViewer.getElementAt(0);
144             if (object != null)
145                 fViewer.setSelection(new StructuredSelection(object));
146         }
147         fViewer.getTable().setFocus();
148         Dialog.applyDialogFont(parent2);
149     }
150     
151     private SynchronizeWizardDescription getDefaultSelection() {
152         
153         if (!(TeamUI.getSynchronizeManager() instanceof SynchronizeManager))
154             return null;
155
156         final String JavaDoc defaultSelection= getDialogSettings().get(DEFAULT_SELECTION);
157         if (defaultSelection == null)
158             return null;
159         
160         final SynchronizeManager syncManager= (SynchronizeManager)TeamUI.getSynchronizeManager();
161         final SynchronizeWizardDescription [] wizards= syncManager.getWizardDescriptors();
162         for (int i = 0; i < wizards.length; i++) {
163             if (defaultSelection.equals(wizards[i].getId())) {
164                 return wizards[i];
165             }
166         }
167         return null;
168     }
169
170     public void doubleClick(DoubleClickEvent event) {
171         selectionChanged(
172             new SelectionChangedEvent(
173                 event.getViewer(),
174                 event.getViewer().getSelection()));
175         getContainer().showPage(getNextPage());
176     }
177     
178     public void selectionChanged(SelectionChangedEvent event) {
179         // Initialize the wizard so we can tell whether to enable the
180
// Next button
181
ISelection selection = event.getSelection();
182         if (selection == null || !(selection instanceof IStructuredSelection)) {
183             wizard = null;
184             setPageComplete(false);
185             return;
186         }
187         IStructuredSelection ss = (IStructuredSelection) selection;
188         if (ss.size() != 1) {
189             wizard = null;
190             setPageComplete(false);
191             return;
192         }
193         SynchronizeWizardDescription selectedDescriptor = (SynchronizeWizardDescription)ss.getFirstElement();
194         try {
195             wizard = selectedDescriptor.createWizard();
196             wizard.addPages();
197             // Ask the container to update button enablement
198
setPageComplete(true);
199             setDescription(selectedDescriptor.getDescription());
200         } catch (CoreException e) {
201             Utils.handle(e);
202             setPageComplete(false);
203         }
204     }
205     
206     public IWizard getSelectedWizard() {
207         return this.wizard;
208     }
209     
210     public IWizardPage getNextPage() {
211         if (wizard == null) return null;
212         return wizard.getStartingPage();
213     }
214
215     public void setVisible(boolean visible) {
216         super.setVisible(visible);
217         if (visible) {
218             fViewer.getTable().setFocus();
219         }
220     }
221 }
222
Popular Tags