KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > GlobalRefreshElementSelectionPage


1 /*******************************************************************************
2  * Copyright (c) 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.synchronize;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.team.internal.ui.*;
27 import org.eclipse.ui.*;
28 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
29 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
30
31 public abstract class GlobalRefreshElementSelectionPage extends WizardPage {
32
33     private boolean scopeCheckingElement = false;
34     
35     // Set of scope hint to determine the initial selection
36
private Button participantScope;
37     private Button selectedResourcesScope;
38     private Button workingSetScope;
39     
40     // The checked tree viewer
41
private ContainerCheckedTreeViewer fViewer;
42     
43     // Working set label and holder
44
private Text workingSetLabel;
45     private IWorkingSet[] workingSets;
46     private IDialogSettings settings;
47     
48     // dialog settings
49
/**
50      * Settings constant for section name (value <code>SynchronizeResourceSelectionDialog</code>).
51      */

52     private static final String JavaDoc STORE_SECTION = "SynchronizeResourceSelectionDialog"; //$NON-NLS-1$
53
/**
54      * Settings constant for working sets (value <code>SynchronizeResourceSelectionDialog.STORE_WORKING_SET</code>).
55      */

56     private static final String JavaDoc STORE_WORKING_SETS = "SynchronizeResourceSelectionDialog.STORE_WORKING_SETS"; //$NON-NLS-1$
57

58     protected GlobalRefreshElementSelectionPage(String JavaDoc pageName) {
59         super(pageName);
60         IDialogSettings s = TeamUIPlugin.getPlugin().getDialogSettings();
61         this.settings = s.getSection(STORE_SECTION);
62         if(settings == null) {
63             settings = s.addNewSection(STORE_SECTION);
64         }
65     }
66
67     /* (non-Javadoc)
68      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
69      */

70     public void createControl(Composite parent2) {
71         Composite top = new Composite(parent2, SWT.NULL);
72         top.setLayout(new GridLayout());
73         initializeDialogUnits(top);
74
75         GridData data = new GridData(GridData.FILL_BOTH);
76         data.widthHint = 50;
77         top.setLayoutData(data);
78         setControl(top);
79
80         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.SYNC_RESOURCE_SELECTION_PAGE);
81         
82         Label l = new Label(top, SWT.NULL);
83         l.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_5);
84
85         // The viewer
86
fViewer = createViewer(top);
87
88         Composite selectGroup = new Composite(top, SWT.NULL);
89         GridLayout layout = new GridLayout();
90         layout.numColumns = 2;
91         layout.marginHeight = 0;
92         layout.marginWidth = 0;
93         //layout.makeColumnsEqualWidth = false;
94
selectGroup.setLayout(layout);
95         data = new GridData(GridData.FILL_HORIZONTAL);
96         selectGroup.setLayoutData(data);
97
98         Button selectAll = new Button(selectGroup, SWT.NULL);
99         selectAll.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_12);
100         selectAll.addSelectionListener(new SelectionAdapter() {
101
102             public void widgetSelected(SelectionEvent e) {
103                 participantScope.setSelection(true);
104                 selectedResourcesScope.setSelection(false);
105                 workingSetScope.setSelection(false);
106                 updateParticipantScope();
107                 scopeCheckingElement = true;
108                 updateOKStatus();
109                 scopeCheckingElement = false;
110             }
111         });
112         setButtonLayoutData(selectAll);
113
114         Button deSelectAll = new Button(selectGroup, SWT.NULL);
115         deSelectAll.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_13);
116         deSelectAll.addSelectionListener(new SelectionAdapter() {
117
118             public void widgetSelected(SelectionEvent e) {
119                 fViewer.setCheckedElements(new Object JavaDoc[0]);
120                 updateOKStatus();
121             }
122         });
123         setButtonLayoutData(deSelectAll);
124
125         // Scopes
126
Group scopeGroup = new Group(top, SWT.NULL);
127         scopeGroup.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_6);
128         layout = new GridLayout();
129         layout.numColumns = 3;
130         layout.makeColumnsEqualWidth = false;
131         scopeGroup.setLayout(layout);
132         data = new GridData(GridData.FILL_HORIZONTAL);
133         data.widthHint = 50;
134         scopeGroup.setLayoutData(data);
135
136         participantScope = new Button(scopeGroup, SWT.RADIO);
137         participantScope.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_7);
138         participantScope.addSelectionListener(new SelectionAdapter() {
139
140             public void widgetSelected(SelectionEvent e) {
141                 updateParticipantScope();
142             }
143         });
144
145         selectedResourcesScope = new Button(scopeGroup, SWT.RADIO);
146         selectedResourcesScope.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_8);
147         selectedResourcesScope.addSelectionListener(new SelectionAdapter() {
148
149             public void widgetSelected(SelectionEvent e) {
150                 updateSelectedResourcesScope();
151             }
152         });
153         data = new GridData();
154         data.horizontalSpan = 2;
155         selectedResourcesScope.setLayoutData(data);
156
157         workingSetScope = new Button(scopeGroup, SWT.RADIO);
158         workingSetScope.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_10);
159         workingSetScope.addSelectionListener(new SelectionAdapter() {
160
161             public void widgetSelected(SelectionEvent e) {
162                 if (isWorkingSetSelected()) {
163                     updateWorkingSetScope();
164                 }
165             }
166         });
167
168         workingSetLabel = new Text(scopeGroup, SWT.BORDER);
169         workingSetLabel.setEditable(false);
170         data = new GridData(GridData.FILL_HORIZONTAL);
171         workingSetLabel.setLayoutData(data);
172
173         Button selectWorkingSetButton = new Button(scopeGroup, SWT.NULL);
174         selectWorkingSetButton.setText(TeamUIMessages.GlobalRefreshResourceSelectionPage_11);
175         selectWorkingSetButton.addSelectionListener(new SelectionAdapter() {
176
177             public void widgetSelected(SelectionEvent e) {
178                 selectWorkingSetAction();
179             }
180         });
181         data = new GridData(GridData.HORIZONTAL_ALIGN_END);
182         selectWorkingSetButton.setLayoutData(data);
183         Dialog.applyDialogFont(selectWorkingSetButton);
184
185         initializeScopingHint();
186         Dialog.applyDialogFont(top);
187     }
188
189     protected abstract ContainerCheckedTreeViewer createViewer(Composite top);
190     
191     /**
192      * Allow the finish button to be pressed if there are checked resources.
193      *
194      */

195     protected void updateOKStatus() {
196         if(fViewer != null) {
197             if(! scopeCheckingElement) {
198                 if(! selectedResourcesScope.getSelection()) {
199                     selectedResourcesScope.setSelection(true);
200                     participantScope.setSelection(false);
201                     workingSetScope.setSelection(false);
202                     updateSelectedResourcesScope();
203                 }
204             }
205             setPageComplete(areAnyElementsChecked());
206         } else {
207             setPageComplete(false);
208         }
209     }
210     
211     /**
212      * Returns <code>true</code> if any of the root resources are grayed.
213      */

214     private boolean areAnyElementsChecked() {
215         TreeItem[] item = fViewer.getTree().getItems();
216         for (int i = 0; i < item.length; i++) {
217             TreeItem child = item[i];
218             if(child.getChecked() || child.getGrayed()) {
219                 return true;
220             }
221         }
222         return false;
223     }
224     
225     /**
226      * Return the list of top-most resources that have been checked.
227      *
228      * @return the list of top-most resources that have been checked or an
229      * empty list if nothing is selected.
230      */

231     public Object JavaDoc[] getRootElement() {
232         TreeItem[] item = fViewer.getTree().getItems();
233         List JavaDoc checked = new ArrayList JavaDoc();
234         for (int i = 0; i < item.length; i++) {
235             TreeItem child = item[i];
236             collectCheckedItems(child, checked);
237         }
238         return checked.toArray(new Object JavaDoc[checked.size()]);
239     }
240     
241     protected void initializeScopingHint() {
242         String JavaDoc working_sets = settings.get(STORE_WORKING_SETS);
243         if (working_sets == null || working_sets.equals("")) { //$NON-NLS-1$
244
participantScope.setSelection(true);
245             updateParticipantScope();
246         } else {
247             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(working_sets, " ,"); //$NON-NLS-1$
248
ArrayList JavaDoc ws = new ArrayList JavaDoc();
249             while (st.hasMoreTokens()) {
250                 String JavaDoc workingSetName = st.nextToken();
251                 if (workingSetName != null && workingSetName.equals("") == false) { //$NON-NLS-1$
252
IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
253                     IWorkingSet workingSet = workingSetManager.getWorkingSet(workingSetName);
254                     if (workingSet != null) {
255                         ws.add(workingSet);
256                     }
257                 }
258             }
259             if(! ws.isEmpty()) {
260                 this.workingSets = (IWorkingSet[]) ws.toArray(new IWorkingSet[ws.size()]);
261                 updateWorkingSetScope();
262                 updateWorkingSetLabel();
263                 participantScope.setSelection(false);
264                 selectedResourcesScope.setSelection(false);
265                 workingSetScope.setSelection(true);
266             }
267         }
268     }
269     
270     /* (non-Javadoc)
271      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
272      */

273     public void dispose() {
274         if(workingSets != null && isWorkingSetSelected()) {
275             String JavaDoc concatsWorkingSets = makeWorkingSetLabel();
276             settings.put(STORE_WORKING_SETS, concatsWorkingSets);
277         } else {
278             settings.put(STORE_WORKING_SETS, (String JavaDoc)null);
279         }
280     }
281     
282     private void updateParticipantScope() {
283         if(isWorkspaceSelected()) {
284             scopeCheckingElement = true;
285             checkAll();
286             setPageComplete(getRootElement().length > 0);
287             scopeCheckingElement = false;
288         }
289     }
290
291     protected abstract void checkAll();
292     
293     private void updateSelectedResourcesScope() {
294         setPageComplete(getRootElement().length > 0);
295     }
296     
297     private void selectWorkingSetAction() {
298         IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
299         IWorkingSetSelectionDialog dialog = manager.createWorkingSetSelectionDialog(getShell(), true);
300         dialog.open();
301         IWorkingSet[] sets = dialog.getSelection();
302         if(sets != null) {
303             workingSets = sets;
304         } else {
305             // dialog cancelled
306
return;
307         }
308         updateWorkingSetScope();
309         updateWorkingSetLabel();
310         
311         participantScope.setSelection(false);
312         selectedResourcesScope.setSelection(false);
313         workingSetScope.setSelection(true);
314     }
315     
316     private void updateWorkingSetScope() {
317         if(workingSets != null) {
318             scopeCheckingElement = true;
319             boolean hasElements = checkWorkingSetElements();
320             scopeCheckingElement = false;
321             setPageComplete(hasElements);
322         } else {
323             scopeCheckingElement = true;
324             fViewer.setCheckedElements(new Object JavaDoc[0]);
325             scopeCheckingElement = false;
326             setPageComplete(false);
327         }
328     }
329
330     protected abstract boolean checkWorkingSetElements();
331     
332     private void collectCheckedItems(TreeItem item, List JavaDoc checked) {
333         if(item.getChecked() && !item.getGrayed()) {
334             checked.add(item.getData());
335         } else if(item.getGrayed()) {
336             TreeItem[] children = item.getItems();
337             for (int i = 0; i < children.length; i++) {
338                 TreeItem child = children[i];
339                 collectCheckedItems(child, checked);
340             }
341         }
342     }
343     
344     private void updateWorkingSetLabel() {
345         if (workingSets == null || workingSets.length == 0) {
346             workingSetLabel.setText(TeamUIMessages.StatisticsPanel_noWorkingSet);
347         } else {
348             workingSetLabel.setText(makeWorkingSetLabel());
349         }
350     }
351
352     private String JavaDoc makeWorkingSetLabel() {
353         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
354         for (int i = 0; i < workingSets.length; i++) {
355             IWorkingSet set = workingSets[i];
356             if(i != 0) buffer.append(" ,"); //$NON-NLS-1$
357
buffer.append(set.getName());
358         }
359         return buffer.toString();
360     }
361     
362     protected boolean isWorkspaceSelected() {
363         return participantScope.getSelection();
364     }
365     
366     protected void setWorkspaceSelected(boolean selected) {
367          workingSetScope.setSelection(!selected);
368          selectedResourcesScope.setSelection(!selected);
369          participantScope.setSelection(selected);
370     }
371
372     protected boolean isWorkingSetSelected() {
373         return workingSetScope.getSelection();
374     }
375
376     public IWorkingSet[] getWorkingSets() {
377         return workingSets;
378     }
379
380     public ContainerCheckedTreeViewer getViewer() {
381         return fViewer;
382     }
383     
384     protected boolean isSelectedResourcesSelected() {
385         return selectedResourcesScope.getSelection();
386     }
387 }
388
Popular Tags