KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > WorkingSetSelectionArea


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.ccvs.ui;
12
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.jface.dialogs.IDialogSettings;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.team.internal.ui.dialogs.DialogArea;
25 import org.eclipse.ui.*;
26 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
27
28 /**
29  * @author Administrator
30  *
31  * To change this generated comment edit the template variable "typecomment":
32  * Window>Preferences>Java>Templates.
33  * To enable and disable the creation of type comments go to
34  * Window>Preferences>Java>Code Generation.
35  */

36 public class WorkingSetSelectionArea extends DialogArea {
37
38     private Button noWorkingSetButton;
39     private Button workingSetButton;
40     private Combo mruList;
41     private Button selectButton;
42     private IWorkingSet workingSet, oldWorkingSet;
43     
44     private String JavaDoc noWorkingSetText;
45     private String JavaDoc workingSetText;
46     
47     private static final String JavaDoc USE_WORKING_SET = "UseWorkingSet"; //$NON-NLS-1$
48
public static final String JavaDoc SELECTED_WORKING_SET = "SelectedWorkingSet"; //$NON-NLS-1$
49

50     /*
51      * Used to update the mru list box when working sets are
52      * renamed in the working set selection dialog.
53      */

54     private IPropertyChangeListener workingSetChangeListener = new IPropertyChangeListener() {
55         public void propertyChange(PropertyChangeEvent event) {
56             String JavaDoc property = event.getProperty();
57             Object JavaDoc newValue = event.getNewValue();
58
59             if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property) &&
60                 newValue instanceof IWorkingSet) {
61                 String JavaDoc newName = ((IWorkingSet) newValue).getName();
62                 int count = mruList.getItemCount();
63                 for (int i = 0; i < count; i++) {
64                     String JavaDoc item = mruList.getItem(i);
65                     IWorkingSet workingSet = (IWorkingSet) mruList.getData(item);
66                     if (workingSet == newValue) {
67                         boolean isTopItem = (mruList.getData(mruList.getText()) == workingSet);
68                         mruList.remove(i);
69                         mruList.add(newName, i);
70                         mruList.setData(newName, workingSet);
71                         if (isTopItem) {
72                             mruList.setText(newName);
73                         }
74                         break;
75                     }
76                 }
77             }
78         }
79     };
80     private final IDialogSettings settings;
81     private final Shell shell;
82         
83     public WorkingSetSelectionArea(Shell shell, String JavaDoc noWorkingSetText, String JavaDoc workingSetText, IDialogSettings settings) {
84         this.shell = shell;
85         this.noWorkingSetText = noWorkingSetText;
86         this.workingSetText = workingSetText;
87         this.settings = settings;
88     }
89     
90     /**
91      * Overrides method in Dialog
92      *
93      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
94      */

95     public void createArea(Composite parent) {
96         Dialog.applyDialogFont(parent);
97         final Composite composite = createComposite(parent, 2, false);
98         initializeDialogUnits(composite);
99         composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
100         GridLayout layout = new GridLayout();
101         layout.marginWidth = 0;
102         layout.numColumns = 2;
103         composite.setLayout(layout);
104
105         // Create the checkbox to enable/disable working set use
106
noWorkingSetButton = createRadioButton(composite, noWorkingSetText, 2);
107         workingSetButton = createRadioButton(composite, workingSetText, 2);
108         workingSetButton.addSelectionListener(new SelectionAdapter() {
109             public void widgetSelected(SelectionEvent e) {
110                 handleWorkingSetButtonSelection();
111             }
112         });
113         
114         boolean useWorkingSet = false;
115         if (settings != null) {
116             useWorkingSet = settings.getBoolean(USE_WORKING_SET);
117         }
118         noWorkingSetButton.setSelection(!useWorkingSet);
119         workingSetButton.setSelection(useWorkingSet);
120
121         // Create the combo/button which allows working set selection
122
mruList = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
123         GridData data = new GridData(SWT.FILL, SWT.CENTER, true, true);
124         data.horizontalIndent= 15;
125         mruList.setLayoutData(data);
126
127         selectButton = createButton(composite, CVSUIMessages.WorkingSetSelectionArea_workingSetOther, GridData.HORIZONTAL_ALIGN_FILL);
128         selectButton.addSelectionListener(new SelectionAdapter() {
129             public void widgetSelected(SelectionEvent event) {
130                 handleWorkingSetSelection();
131             }
132         });
133
134         initializeMru();
135         initializeWorkingSet();
136         
137         mruList.addSelectionListener(new SelectionAdapter() {
138             public void widgetSelected(SelectionEvent e) {
139                 handleMruSelection();
140             }
141         });
142     }
143
144     /**
145      * Method handleMruSelection.
146      */

147     private void handleMruSelection() {
148         String JavaDoc selectedWorkingSet = mruList.getText();
149         oldWorkingSet = workingSet;
150         workingSet = (IWorkingSet) mruList.getData(selectedWorkingSet);
151         if (settings != null)
152             settings.put(SELECTED_WORKING_SET, selectedWorkingSet);
153         handleWorkingSetChange();
154     }
155     
156     /**
157      * Opens the working set selection dialog if the "Other..." item
158      * is selected in the most recently used working set list.
159      */

160     private void handleWorkingSetSelection() {
161         IWorkingSetSelectionDialog dialog = PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSetSelectionDialog(shell, false);
162         IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
163         IWorkingSet workingSet = workingSetManager.getWorkingSet(mruList.getText());
164
165         if (workingSet != null) {
166             dialog.setSelection(new IWorkingSet[]{workingSet});
167         }
168         // add a change listener to detect a working set name change
169
workingSetManager.addPropertyChangeListener(workingSetChangeListener);
170         if (dialog.open() == Window.OK) {
171             IWorkingSet[] result = dialog.getSelection();
172             if (result != null && result.length > 0) {
173                 workingSet = result[0];
174                 String JavaDoc workingSetName = workingSet.getName();
175                 if (mruList.indexOf(workingSetName) != -1) {
176                     mruList.remove(workingSetName);
177                 }
178                 mruList.add(workingSetName, 0);
179                 mruList.setText(workingSetName);
180                 mruList.setData(workingSetName, workingSet);
181                 handleMruSelection();
182             }
183             else {
184                 workingSet = null;
185             }
186             // remove deleted working sets from the mru list box
187
String JavaDoc[] mruNames = mruList.getItems();
188             for (int i = 0; i < mruNames.length; i++) {
189                 if (workingSetManager.getWorkingSet(mruNames[i]) == null) {
190                     mruList.remove(mruNames[i]);
191                 }
192             }
193         }
194         workingSetManager.removePropertyChangeListener(workingSetChangeListener);
195     }
196     
197     /**
198      * Sets the enabled state of the most recently used working set list
199      * based on the checked state of the working set check box.
200      */

201     private void handleWorkingSetButtonSelection() {
202         boolean useWorkingSet = workingSetButton.getSelection();
203         if (settings != null)
204             settings.put(USE_WORKING_SET, useWorkingSet);
205         mruList.setEnabled(useWorkingSet);
206         selectButton.setEnabled(useWorkingSet);
207         if (useWorkingSet && mruList.getSelectionIndex() >= 0) {
208             handleMruSelection();
209         } else if (!useWorkingSet) {
210             handleDeselection();
211         }
212     }
213     
214     private void handleDeselection() {
215         oldWorkingSet = workingSet;
216         workingSet = null;
217         handleWorkingSetChange();
218     }
219
220     private void handleWorkingSetChange() {
221         firePropertyChangeChange(SELECTED_WORKING_SET, oldWorkingSet, workingSet);
222     }
223     
224     /**
225      * Populates the most recently used working set list with MRU items from
226      * the working set manager as well as adds an item to enable selection of
227      * a working set not in the MRU list.
228      */

229     private void initializeMru() {
230         IWorkingSet[] workingSets = PlatformUI.getWorkbench().getWorkingSetManager().getRecentWorkingSets();
231
232         for (int i = 0; i < workingSets.length; i++) {
233             String JavaDoc workingSetName = workingSets[i].getName();
234             mruList.add(workingSetName);
235             mruList.setData(workingSetName, workingSets[i]);
236         }
237         if (workingSets.length > 0) {
238             mruList.setText(workingSets[0].getName());
239         }
240     }
241     
242     /**
243      * Initializes the state of the working set part of the dialog.
244      */

245     private void initializeWorkingSet() {
246         if (workingSet == null && settings != null && settings.getBoolean(USE_WORKING_SET)) {
247             IWorkingSet mruSet = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(settings.get(SELECTED_WORKING_SET));
248             if (mruSet != null) {
249                 // the call to setWorkingSet will re-invoke the initializeWorkingSet method
250
setWorkingSet(mruSet);
251                 return;
252             }
253         }
254         workingSetButton.setSelection(workingSet != null);
255         handleWorkingSetButtonSelection();
256         if (workingSet != null && mruList.indexOf(workingSet.getName()) != -1) {
257             mruList.setText(workingSet.getName());
258         }
259         handleWorkingSetChange();
260     }
261     
262     /**
263      * Sets the working set that should be selected in the most recently
264      * used working set list.
265      *
266      * @param workingSet the working set that should be selected.
267      * has to exist in the list returned by
268      * org.eclipse.ui.IWorkingSetManager#getRecentWorkingSets().
269      * Must not be null.
270      */

271     public void setWorkingSet(IWorkingSet workingSet) {
272         oldWorkingSet = this.workingSet;
273         this.workingSet = workingSet;
274
275         if (workingSetButton != null && mruList != null) {
276             initializeWorkingSet();
277         }
278     }
279     
280 }
281
Popular Tags