|
Lines 10-15
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jdt.internal.ui.workingsets; |
11 |
package org.eclipse.jdt.internal.ui.workingsets; |
| 12 |
|
12 |
|
|
|
13 |
import com.ibm.icu.text.Collator; |
| 14 |
|
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.Arrays; |
| 17 |
import java.util.Comparator; |
| 18 |
import java.util.List; |
| 19 |
|
| 13 |
import org.eclipse.core.runtime.Assert; |
20 |
import org.eclipse.core.runtime.Assert; |
| 14 |
|
21 |
|
| 15 |
import org.eclipse.swt.SWT; |
22 |
import org.eclipse.swt.SWT; |
|
Lines 20-31
Link Here
|
| 20 |
import org.eclipse.swt.layout.GridData; |
27 |
import org.eclipse.swt.layout.GridData; |
| 21 |
import org.eclipse.swt.layout.GridLayout; |
28 |
import org.eclipse.swt.layout.GridLayout; |
| 22 |
import org.eclipse.swt.widgets.Button; |
29 |
import org.eclipse.swt.widgets.Button; |
|
|
30 |
import org.eclipse.swt.widgets.Combo; |
| 23 |
import org.eclipse.swt.widgets.Composite; |
31 |
import org.eclipse.swt.widgets.Composite; |
| 24 |
import org.eclipse.swt.widgets.Label; |
32 |
import org.eclipse.swt.widgets.Label; |
| 25 |
import org.eclipse.swt.widgets.Text; |
|
|
| 26 |
|
33 |
|
| 27 |
import org.eclipse.jface.dialogs.Dialog; |
34 |
import org.eclipse.jface.dialogs.Dialog; |
| 28 |
import org.eclipse.jface.dialogs.IDialogConstants; |
35 |
import org.eclipse.jface.dialogs.IDialogConstants; |
|
|
36 |
import org.eclipse.jface.dialogs.IDialogSettings; |
| 29 |
import org.eclipse.jface.resource.JFaceResources; |
37 |
import org.eclipse.jface.resource.JFaceResources; |
| 30 |
import org.eclipse.jface.window.Window; |
38 |
import org.eclipse.jface.window.Window; |
| 31 |
|
39 |
|
|
Lines 36-60
Link Here
|
| 36 |
|
44 |
|
| 37 |
public class WorkingSetConfigurationBlock { |
45 |
public class WorkingSetConfigurationBlock { |
| 38 |
|
46 |
|
|
|
47 |
private static final String WORKINGSET_SELECTION_HISTORY= "workingset_selection_history"; //$NON-NLS-1$ |
| 48 |
private static final int MAX_HISTORY_SIZE= 5; |
| 49 |
|
| 39 |
private Label fLabel; |
50 |
private Label fLabel; |
| 40 |
private Text fText; |
51 |
private Combo fWorkingSetCombo; |
| 41 |
private Button fConfigure; |
52 |
private Button fConfigure; |
| 42 |
private IWorkingSet[] fSelectedWorkingSets; |
53 |
private IWorkingSet[] fSelectedWorkingSets; |
| 43 |
private String[] fWorkingSetIDs; |
54 |
private String[] fWorkingSetIDs; |
| 44 |
private String fMessage; |
55 |
private String fMessage; |
|
|
56 |
private Button fEnableButton; |
| 57 |
private ArrayList fSelectionHistory; |
| 58 |
private final IDialogSettings fSettings; |
| 59 |
private final String fEnableButtonText; |
| 45 |
|
60 |
|
| 46 |
/** |
61 |
/** |
| 47 |
* @param preSelectedWorkingSets the working sets which are selected when showning the block, not <b>null</b> |
62 |
* @param preSelectedWorkingSets the working sets which are selected when showning the block, not <b>null</b> |
| 48 |
* @param compatibleWorkingSetIds only working sets with an id in compatibleWorkingSetIds can be selected, not <b>null</b> |
63 |
* @param compatibleWorkingSetIds only working sets with an id in compatibleWorkingSetIds can be selected, not <b>null</b> |
|
|
64 |
* @param enableButtonText the text shown for the enable button, not <b>null</b> |
| 65 |
* @param settings to store/load the selection history, not <b>null</b> |
| 49 |
*/ |
66 |
*/ |
| 50 |
public WorkingSetConfigurationBlock(IWorkingSet[] preSelectedWorkingSets, String[] compatibleWorkingSetIds) { |
67 |
public WorkingSetConfigurationBlock(IWorkingSet[] preSelectedWorkingSets, String[] compatibleWorkingSetIds, String enableButtonText, IDialogSettings settings) { |
| 51 |
Assert.isNotNull(preSelectedWorkingSets); |
68 |
Assert.isNotNull(preSelectedWorkingSets); |
| 52 |
Assert.isNotNull(compatibleWorkingSetIds); |
69 |
Assert.isNotNull(compatibleWorkingSetIds); |
|
|
70 |
Assert.isNotNull(enableButtonText); |
| 71 |
Assert.isNotNull(settings); |
| 53 |
|
72 |
|
|
|
73 |
fEnableButtonText= enableButtonText; |
| 54 |
fSelectedWorkingSets= preSelectedWorkingSets; |
74 |
fSelectedWorkingSets= preSelectedWorkingSets; |
| 55 |
fWorkingSetIDs= compatibleWorkingSetIds; |
75 |
fWorkingSetIDs= compatibleWorkingSetIds; |
|
|
76 |
fSettings= settings; |
| 77 |
fSelectionHistory= loadSelectionHistory(settings, compatibleWorkingSetIds); |
| 56 |
} |
78 |
} |
| 57 |
|
79 |
|
| 58 |
/** |
80 |
/** |
| 59 |
* @param message the message to show to the user in the working set selection dialog |
81 |
* @param message the message to show to the user in the working set selection dialog |
| 60 |
*/ |
82 |
*/ |
|
Lines 66-72
Link Here
|
| 66 |
* @return the selected working sets, not <b>null</b> |
88 |
* @return the selected working sets, not <b>null</b> |
| 67 |
*/ |
89 |
*/ |
| 68 |
public IWorkingSet[] getSelectedWorkingSets() { |
90 |
public IWorkingSet[] getSelectedWorkingSets() { |
| 69 |
return fSelectedWorkingSets; |
91 |
if (fEnableButton.getSelection()) { |
|
|
92 |
return fSelectedWorkingSets; |
| 93 |
} else { |
| 94 |
return new IWorkingSet[0]; |
| 95 |
} |
| 70 |
} |
96 |
} |
| 71 |
|
97 |
|
| 72 |
/** |
98 |
/** |
|
Lines 79-93
Link Here
|
| 79 |
Composite composite= new Composite(parent, SWT.NONE); |
105 |
Composite composite= new Composite(parent, SWT.NONE); |
| 80 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); |
106 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); |
| 81 |
composite.setLayout(new GridLayout(numColumn, false)); |
107 |
composite.setLayout(new GridLayout(numColumn, false)); |
|
|
108 |
|
| 109 |
fEnableButton= new Button(composite, SWT.CHECK); |
| 110 |
fEnableButton.setText(fEnableButtonText); |
| 111 |
GridData enableData= new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 112 |
enableData.horizontalSpan= numColumn; |
| 113 |
fEnableButton.setLayoutData(enableData); |
| 114 |
fEnableButton.setSelection(fSelectedWorkingSets.length > 0); |
| 82 |
|
115 |
|
| 83 |
fLabel= new Label(composite, SWT.NONE); |
116 |
fLabel= new Label(composite, SWT.NONE); |
| 84 |
fLabel.setText(WorkingSetMessages.WorkingSetConfigurationBlock_WorkingSetText_name); |
117 |
fLabel.setText(WorkingSetMessages.WorkingSetConfigurationBlock_WorkingSetText_name); |
| 85 |
|
118 |
|
| 86 |
fText= new Text(composite, SWT.READ_ONLY | SWT.BORDER); |
119 |
fWorkingSetCombo= new Combo(composite, SWT.READ_ONLY | SWT.BORDER); |
| 87 |
GridData textData= new GridData(SWT.FILL, SWT.CENTER, true, false); |
120 |
GridData textData= new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 88 |
textData.horizontalSpan= numColumn - 2; |
121 |
textData.horizontalSpan= numColumn - 2; |
| 89 |
textData.horizontalIndent= 0; |
122 |
textData.horizontalIndent= 0; |
| 90 |
fText.setLayoutData(textData); |
123 |
fWorkingSetCombo.setLayoutData(textData); |
| 91 |
|
124 |
|
| 92 |
fConfigure= new Button(composite, SWT.PUSH); |
125 |
fConfigure= new Button(composite, SWT.PUSH); |
| 93 |
fConfigure.setText(WorkingSetMessages.WorkingSetConfigurationBlock_SelectWorkingSet_button); |
126 |
fConfigure.setText(WorkingSetMessages.WorkingSetConfigurationBlock_SelectWorkingSet_button); |
|
Lines 117-123
Link Here
|
| 117 |
} |
150 |
} |
| 118 |
}); |
151 |
}); |
| 119 |
|
152 |
|
| 120 |
updateWorkingSetSelection(); |
153 |
fEnableButton.addSelectionListener(new SelectionAdapter() { |
|
|
154 |
public void widgetSelected(SelectionEvent e) { |
| 155 |
updateEnableState(fEnableButton.getSelection()); |
| 156 |
} |
| 157 |
}); |
| 158 |
updateEnableState(fEnableButton.getSelection()); |
| 159 |
|
| 160 |
fWorkingSetCombo.addSelectionListener(new SelectionAdapter() { |
| 161 |
public void widgetSelected(SelectionEvent e) { |
| 162 |
updateSelectedWorkingSets(); |
| 163 |
} |
| 164 |
}); |
| 165 |
|
| 166 |
fWorkingSetCombo.setItems(getHistoryEntries()); |
| 167 |
if (fSelectedWorkingSets.length == 0 && fSelectionHistory.size() > 0) { |
| 168 |
fWorkingSetCombo.select(historyIndex((String)fSelectionHistory.get(0))); |
| 169 |
updateSelectedWorkingSets(); |
| 170 |
} else { |
| 171 |
updateWorkingSetSelection(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
private void updateEnableState(boolean enabled) { |
| 176 |
fLabel.setEnabled(enabled); |
| 177 |
fWorkingSetCombo.setEnabled(enabled); |
| 178 |
fConfigure.setEnabled(enabled); |
| 121 |
} |
179 |
} |
| 122 |
|
180 |
|
| 123 |
private void updateWorkingSetSelection() { |
181 |
private void updateWorkingSetSelection() { |
|
Lines 131-138
Link Here
|
| 131 |
buf.append(ws.getLabel()); |
189 |
buf.append(ws.getLabel()); |
| 132 |
} |
190 |
} |
| 133 |
} |
191 |
} |
|
|
192 |
|
| 193 |
String currentSelection= buf.toString(); |
| 194 |
int index= historyIndex(currentSelection); |
| 195 |
if (index >= 0) { |
| 196 |
historyInsert(currentSelection); |
| 197 |
fWorkingSetCombo.select(index); |
| 198 |
} else { |
| 199 |
historyInsert(currentSelection); |
| 200 |
fWorkingSetCombo.setItems(getHistoryEntries()); |
| 201 |
fWorkingSetCombo.select(historyIndex(currentSelection)); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
private String[] getHistoryEntries() { |
| 206 |
String[] history= (String[])fSelectionHistory.toArray(new String[fSelectionHistory.size()]); |
| 207 |
Arrays.sort(history, new Comparator() { |
| 208 |
public int compare(Object o1, Object o2) { |
| 209 |
return Collator.getInstance().compare(o1, o2); |
| 210 |
} |
| 211 |
}); |
| 212 |
return history; |
| 213 |
} |
| 214 |
|
| 215 |
private void historyInsert(String entry) { |
| 216 |
fSelectionHistory.remove(entry); |
| 217 |
fSelectionHistory.add(0, entry); |
| 218 |
storeSelectionHistory(fSettings); |
| 219 |
} |
| 220 |
|
| 221 |
private int historyIndex(String entry) { |
| 222 |
for (int i= 0; i < fWorkingSetCombo.getItemCount(); i++) { |
| 223 |
if (fWorkingSetCombo.getItem(i).equals(entry)) |
| 224 |
return i; |
| 225 |
} |
| 226 |
|
| 227 |
return -1; |
| 228 |
} |
| 229 |
|
| 230 |
private void updateSelectedWorkingSets() { |
| 231 |
String item= fWorkingSetCombo.getItem(fWorkingSetCombo.getSelectionIndex()); |
| 232 |
String[] workingSetNames= item.split(", "); //$NON-NLS-1$ |
| 233 |
|
| 234 |
IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager(); |
| 235 |
fSelectedWorkingSets= new IWorkingSet[workingSetNames.length]; |
| 236 |
for (int i= 0; i < workingSetNames.length; i++) { |
| 237 |
IWorkingSet set= workingSetManager.getWorkingSet(workingSetNames[i]); |
| 238 |
Assert.isNotNull(set); |
| 239 |
fSelectedWorkingSets[i]= set; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
private void storeSelectionHistory(IDialogSettings settings) { |
| 244 |
String[] history; |
| 245 |
if (fSelectionHistory.size() > MAX_HISTORY_SIZE) { |
| 246 |
List subList= fSelectionHistory.subList(0, MAX_HISTORY_SIZE); |
| 247 |
history= (String[])subList.toArray(new String[subList.size()]); |
| 248 |
} else { |
| 249 |
history= (String[])fSelectionHistory.toArray(new String[fSelectionHistory.size()]); |
| 250 |
} |
| 251 |
settings.put(WORKINGSET_SELECTION_HISTORY, history); |
| 252 |
} |
| 253 |
|
| 254 |
private ArrayList loadSelectionHistory(IDialogSettings settings, String[] compatibleWorkingSetIds) { |
| 255 |
String[] strings= settings.getArray(WORKINGSET_SELECTION_HISTORY); |
| 256 |
if (strings == null || strings.length == 0) |
| 257 |
return new ArrayList(); |
| 258 |
|
| 259 |
ArrayList result= new ArrayList(); |
| 260 |
|
| 261 |
IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager(); |
| 262 |
for (int i= 0; i < strings.length; i++) { |
| 263 |
String[] workingSetNames= strings[i].split(", "); //$NON-NLS-1$ |
| 264 |
boolean valid= true; |
| 265 |
for (int j= 0; j < workingSetNames.length && valid; j++) { |
| 266 |
IWorkingSet workingSet= workingSetManager.getWorkingSet(workingSetNames[j]); |
| 267 |
if (workingSet == null) { |
| 268 |
valid= false; |
| 269 |
} else { |
| 270 |
if (!contains(compatibleWorkingSetIds, workingSet.getId())) |
| 271 |
valid= false; |
| 272 |
} |
| 273 |
} |
| 274 |
if (valid) { |
| 275 |
result.add(strings[i]); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return result; |
| 280 |
} |
| 281 |
|
| 282 |
private boolean contains(String[] compatibleWorkingSetIds, String id) { |
| 283 |
for (int i= 0; i < compatibleWorkingSetIds.length; i++) { |
| 284 |
if (compatibleWorkingSetIds[i].equals(id)) |
| 285 |
return true; |
| 286 |
} |
| 134 |
|
287 |
|
| 135 |
fText.setText(buf.toString()); |
288 |
return false; |
| 136 |
} |
289 |
} |
| 137 |
|
290 |
|
| 138 |
private static int getButtonWidthHint(Button button) { |
291 |
private static int getButtonWidthHint(Button button) { |