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