1 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 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 noWorkingSetText; 45 private String workingSetText; 46 47 private static final String USE_WORKING_SET = "UseWorkingSet"; public static final String SELECTED_WORKING_SET = "SelectedWorkingSet"; 50 54 private IPropertyChangeListener workingSetChangeListener = new IPropertyChangeListener() { 55 public void propertyChange(PropertyChangeEvent event) { 56 String property = event.getProperty(); 57 Object newValue = event.getNewValue(); 58 59 if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property) && 60 newValue instanceof IWorkingSet) { 61 String newName = ((IWorkingSet) newValue).getName(); 62 int count = mruList.getItemCount(); 63 for (int i = 0; i < count; i++) { 64 String 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 noWorkingSetText, String workingSetText, IDialogSettings settings) { 84 this.shell = shell; 85 this.noWorkingSetText = noWorkingSetText; 86 this.workingSetText = workingSetText; 87 this.settings = settings; 88 } 89 90 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 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 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 147 private void handleMruSelection() { 148 String 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 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 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 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 String [] 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 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 229 private void initializeMru() { 230 IWorkingSet[] workingSets = PlatformUI.getWorkbench().getWorkingSetManager().getRecentWorkingSets(); 231 232 for (int i = 0; i < workingSets.length; i++) { 233 String 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 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 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 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 |