Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2004, 2007 Mylyn project committers 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 |
|
9 |
package org.eclipse.mylyn.internal.tasks.ui.dialogs; |
10 |
|
11 |
import java.util.ArrayList; |
12 |
import java.util.Arrays; |
13 |
import java.util.Comparator; |
14 |
|
15 |
import org.eclipse.jface.window.IShellProvider; |
16 |
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView; |
17 |
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListTableSorter.SortByIndex; |
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.Button; |
24 |
import org.eclipse.swt.widgets.Combo; |
25 |
import org.eclipse.swt.widgets.Composite; |
26 |
import org.eclipse.swt.widgets.Control; |
27 |
import org.eclipse.swt.widgets.Label; |
28 |
import org.eclipse.ui.dialogs.SelectionDialog; |
29 |
|
30 |
public class TaskListSortDialog extends SelectionDialog { |
31 |
private Combo[] priorityCombos; |
32 |
|
33 |
private Button[] ascendingButtons; |
34 |
|
35 |
private Button[] descendingButtons; |
36 |
|
37 |
private final String[] propertyText; |
38 |
|
39 |
private boolean dirty = false; |
40 |
|
41 |
private final TaskListView taskListView; |
42 |
|
43 |
public TaskListSortDialog(IShellProvider parentShell, TaskListView taskListView) { |
44 |
super(parentShell.getShell()); |
45 |
propertyText = new String[3]; |
46 |
propertyText[0] = "Priority"; |
47 |
propertyText[1] = "Summary"; |
48 |
propertyText[2] = "Date Created"; |
49 |
this.taskListView = taskListView; |
50 |
setTitle(TaskListView.LABEL_VIEW + " Sorting"); |
51 |
} |
52 |
|
53 |
@Override |
54 |
protected Control createDialogArea(Composite parent) { |
55 |
Composite composite = (Composite) super.createDialogArea(parent); |
56 |
|
57 |
initializeDialogUnits(composite); |
58 |
|
59 |
Composite prioritiesArea = new Composite(composite, SWT.NULL); |
60 |
prioritiesArea.setLayout(new GridLayout(3, false)); |
61 |
|
62 |
Label sortByLabel = new Label(prioritiesArea, SWT.NULL); |
63 |
sortByLabel.setText("Sort order:"); |
64 |
GridData data = new GridData(); |
65 |
data.horizontalSpan = 3; |
66 |
sortByLabel.setLayoutData(data); |
67 |
|
68 |
ascendingButtons = new Button[2]; |
69 |
descendingButtons = new Button[2]; |
70 |
priorityCombos = new Combo[2]; |
71 |
|
72 |
for (int i = 0; i < 2; i++) { |
73 |
final int index = i; |
74 |
Label numberLabel = new Label(prioritiesArea, SWT.NULL); |
75 |
numberLabel.setText("" + (i + 1) + "."); |
76 |
priorityCombos[i] = new Combo(prioritiesArea, SWT.READ_ONLY); |
77 |
priorityCombos[i].setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
78 |
|
79 |
Composite directionGroup = new Composite(prioritiesArea, SWT.NONE); |
80 |
directionGroup.setLayout(new GridLayout(2, false)); |
81 |
ascendingButtons[i] = new Button(directionGroup, SWT.RADIO); |
82 |
ascendingButtons[i].setText("Ascending"); |
83 |
ascendingButtons[i].addSelectionListener(new SelectionAdapter() { |
84 |
@Override |
85 |
public void widgetSelected(SelectionEvent e) { |
86 |
markDirty(); |
87 |
} |
88 |
}); |
89 |
descendingButtons[i] = new Button(directionGroup, SWT.RADIO); |
90 |
descendingButtons[i].setText("Descending"); |
91 |
descendingButtons[i].addSelectionListener(new SelectionAdapter() { |
92 |
@Override |
93 |
public void widgetSelected(SelectionEvent e) { |
94 |
markDirty(); |
95 |
} |
96 |
}); |
97 |
if (i < priorityCombos.length - 1) { |
98 |
priorityCombos[i].addSelectionListener(new SelectionAdapter() { |
99 |
@Override |
100 |
public void widgetSelected(SelectionEvent e) { |
101 |
int oldSelectionDirection = 1; |
102 |
if (descendingButtons[index].getSelection()) { |
103 |
oldSelectionDirection = -1; |
104 |
} |
105 |
ArrayList<String> oldSelectionList = new ArrayList<String>( |
106 |
Arrays.asList(priorityCombos[index].getItems())); |
107 |
oldSelectionList.removeAll(Arrays.asList(priorityCombos[index + 1].getItems())); |
108 |
if (oldSelectionList.size() != 1) { |
109 |
return; |
110 |
} |
111 |
String oldSelection = oldSelectionList.get(0); |
112 |
String newSelection = priorityCombos[index].getItem(priorityCombos[index].getSelectionIndex()); |
113 |
if (oldSelection.equals(newSelection)) { |
114 |
return; |
115 |
} |
116 |
for (int j = index + 1; j < priorityCombos.length; j++) { |
117 |
int newSelectionIndex = priorityCombos[j].indexOf(newSelection); |
118 |
//this combo's current selection is equal to newSelection |
119 |
if (priorityCombos[j].getSelectionIndex() == newSelectionIndex) { |
120 |
priorityCombos[j].remove(newSelection); |
121 |
int insertionPoint = -1 |
122 |
- Arrays.binarySearch(priorityCombos[j].getItems(), oldSelection, |
123 |
columnComparator); |
124 |
if (insertionPoint >= 0 && insertionPoint <= priorityCombos[j].getItemCount()) { |
125 |
priorityCombos[j].add(oldSelection, insertionPoint); |
126 |
} else { |
127 |
priorityCombos[j].add(oldSelection); |
128 |
} |
129 |
priorityCombos[j].select(priorityCombos[j].indexOf(oldSelection)); |
130 |
ascendingButtons[index].setSelection(ascendingButtons[j].getSelection()); |
131 |
descendingButtons[index].setSelection(descendingButtons[j].getSelection()); |
132 |
ascendingButtons[j].setSelection(oldSelectionDirection == 1); |
133 |
descendingButtons[j].setSelection(oldSelectionDirection == -1); |
134 |
} |
135 |
//this combo contains newSelection |
136 |
else if (newSelectionIndex >= 0) { |
137 |
String currentText = priorityCombos[j].getText(); |
138 |
priorityCombos[j].remove(newSelection); |
139 |
int insertionPoint = -1 |
140 |
- Arrays.binarySearch(priorityCombos[j].getItems(), oldSelection, |
141 |
columnComparator); |
142 |
if (insertionPoint >= 0 && insertionPoint <= priorityCombos[j].getItemCount()) { |
143 |
priorityCombos[j].add(oldSelection, insertionPoint); |
144 |
priorityCombos[j].select(priorityCombos[j].indexOf(currentText)); |
145 |
} else { |
146 |
priorityCombos[j].add(oldSelection); |
147 |
} |
148 |
} |
149 |
} |
150 |
markDirty(); |
151 |
} |
152 |
}); |
153 |
} else { |
154 |
priorityCombos[i].addSelectionListener(new SelectionAdapter() { |
155 |
@Override |
156 |
public void widgetSelected(SelectionEvent e) { |
157 |
markDirty(); |
158 |
} |
159 |
}); |
160 |
} |
161 |
|
162 |
} |
163 |
int a[] = new int[2]; |
164 |
int b[] = new int[2]; |
165 |
switch (taskListView.getSorter().getSortByIndex()) { |
166 |
case PRIORITY: |
167 |
a[0] = 0; |
168 |
break; |
169 |
case SUMMARY: |
170 |
a[0] = 1; |
171 |
break; |
172 |
case DATE_CREATED: |
173 |
a[0] = 2; |
174 |
break; |
175 |
} |
176 |
|
177 |
switch (taskListView.getSorter().getSortByIndex2()) { |
178 |
case PRIORITY: |
179 |
a[1] = 0; |
180 |
break; |
181 |
case SUMMARY: |
182 |
a[1] = 1; |
183 |
break; |
184 |
case DATE_CREATED: |
185 |
a[1] = 2; |
186 |
break; |
187 |
} |
188 |
b[0] = taskListView.getSorter().getSortDirection(); |
189 |
b[1] = taskListView.getSorter().getSortDirection2(); |
190 |
updateUI(a, b); |
191 |
return composite; |
192 |
} |
193 |
|
194 |
@Override |
195 |
protected void okPressed() { |
196 |
if (isDirty()) { |
197 |
taskListView.getSorter() |
198 |
.setSortByIndex( |
199 |
SortByIndex.valueOf(priorityCombos[0].getItem(priorityCombos[0].getSelectionIndex()) |
200 |
.toUpperCase())); |
201 |
taskListView.getSorter() |
202 |
.setSortByIndex2( |
203 |
SortByIndex.valueOf(priorityCombos[1].getItem(priorityCombos[1].getSelectionIndex()) |
204 |
.toUpperCase())); |
205 |
if (descendingButtons[0].getSelection()) { |
206 |
taskListView.getSorter().setSortDirection(-1); |
207 |
} else { |
208 |
taskListView.getSorter().setSortDirection(1); |
209 |
} |
210 |
if (descendingButtons[1].getSelection()) { |
211 |
taskListView.getSorter().setSortDirection2(-1); |
212 |
} else { |
213 |
taskListView.getSorter().setSortDirection2(1); |
214 |
} |
215 |
|
216 |
} |
217 |
super.okPressed(); |
218 |
} |
219 |
|
220 |
/** |
221 |
* @return boolean |
222 |
*/ |
223 |
public boolean isDirty() { |
224 |
return dirty; |
225 |
} |
226 |
|
227 |
/** |
228 |
* Sets the dirty flag to true. |
229 |
*/ |
230 |
public void markDirty() { |
231 |
dirty = true; |
232 |
} |
233 |
|
234 |
private final Comparator<String> columnComparator = new Comparator<String>() { |
235 |
public int compare(String arg0, String arg1) { |
236 |
int index0 = -1; |
237 |
int index1 = -1; |
238 |
for (int i = 0; i < propertyText.length; i++) { |
239 |
if (propertyText[i].equals(arg0)) { |
240 |
index0 = i; |
241 |
} |
242 |
if (propertyText[i].equals(arg1)) { |
243 |
index1 = i; |
244 |
} |
245 |
} |
246 |
return index0 - index1; |
247 |
} |
248 |
}; |
249 |
|
250 |
private void updateUI(int[] priorities, int[] directions) { |
251 |
ArrayList<String> availablePriorities = new ArrayList<String>(Arrays.asList(propertyText)); |
252 |
|
253 |
for (int i = 0; i < priorityCombos.length; i++) { |
254 |
priorityCombos[i].removeAll(); |
255 |
for (int j = 0; j < availablePriorities.size(); j++) { |
256 |
priorityCombos[i].add(availablePriorities.get(j)); |
257 |
} |
258 |
priorityCombos[i].select(priorityCombos[i].indexOf(propertyText[priorities[i]])); |
259 |
availablePriorities.remove(propertyText[priorities[i]]); |
260 |
|
261 |
ascendingButtons[i].setSelection(directions[i] == 1); |
262 |
descendingButtons[i].setSelection(directions[i] == -1); |
263 |
} |
264 |
} |
265 |
|
266 |
} |