Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 302652
Collapse All | Expand All

(-)src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskEditorAttachmentPart.java (-20 / +97 lines)
Lines 29-42 Link Here
29
import org.eclipse.jface.viewers.OpenEvent;
29
import org.eclipse.jface.viewers.OpenEvent;
30
import org.eclipse.jface.viewers.StructuredSelection;
30
import org.eclipse.jface.viewers.StructuredSelection;
31
import org.eclipse.jface.viewers.Viewer;
31
import org.eclipse.jface.viewers.Viewer;
32
import org.eclipse.jface.viewers.ViewerSorter;
33
import org.eclipse.jface.window.ToolTip;
32
import org.eclipse.jface.window.ToolTip;
34
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
33
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
35
import org.eclipse.mylyn.internal.tasks.core.TaskAttachment;
34
import org.eclipse.mylyn.internal.tasks.core.TaskAttachment;
36
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiPreferenceConstants;
35
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiPreferenceConstants;
37
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
36
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
38
import org.eclipse.mylyn.internal.tasks.ui.commands.OpenTaskAttachmentHandler;
37
import org.eclipse.mylyn.internal.tasks.ui.commands.OpenTaskAttachmentHandler;
38
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
39
import org.eclipse.mylyn.internal.tasks.ui.util.ColumnState;
39
import org.eclipse.mylyn.internal.tasks.ui.util.ColumnState;
40
import org.eclipse.mylyn.internal.tasks.ui.views.AbstractTableSorter;
40
import org.eclipse.mylyn.internal.tasks.ui.views.AbstractTableViewerConfigurator;
41
import org.eclipse.mylyn.internal.tasks.ui.views.AbstractTableViewerConfigurator;
41
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskAttachmentWizard.Mode;
42
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskAttachmentWizard.Mode;
42
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
43
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
Lines 79-85 Link Here
79
	private class AttachmentTableViewer extends AbstractTableViewerConfigurator {
80
	private class AttachmentTableViewer extends AbstractTableViewerConfigurator {
80
		public AttachmentTableViewer(File stateFile) {
81
		public AttachmentTableViewer(File stateFile) {
81
			super(stateFile);
82
			super(stateFile);
82
			// ignore
83
		}
83
		}
84
84
85
		@Override
85
		@Override
Lines 118-141 Link Here
118
			tableViewer.setUseHashlookup(true);
118
			tableViewer.setUseHashlookup(true);
119
			ColumnViewerToolTipSupport.enableFor(tableViewer, ToolTip.NO_RECREATE);
119
			ColumnViewerToolTipSupport.enableFor(tableViewer, ToolTip.NO_RECREATE);
120
120
121
			tableViewer.setSorter(new ViewerSorter() {
121
			tableViewer.setSorter(tableSorter);
122
				@Override
123
				public int compare(Viewer viewer, Object e1, Object e2) {
124
					ITaskAttachment attachment1 = (ITaskAttachment) e1;
125
					ITaskAttachment attachment2 = (ITaskAttachment) e2;
126
					Date created1 = attachment1.getCreationDate();
127
					Date created2 = attachment2.getCreationDate();
128
					if (created1 != null && created2 != null) {
129
						return created1.compareTo(created2);
130
					} else if (created1 == null && created2 != null) {
131
						return -1;
132
					} else if (created1 != null && created2 == null) {
133
						return 1;
134
					} else {
135
						return 0;
136
					}
137
				}
138
			});
139
			List<ITaskAttachment> attachmentList = new ArrayList<ITaskAttachment>(attachments.size());
122
			List<ITaskAttachment> attachmentList = new ArrayList<ITaskAttachment>(attachments.size());
140
			for (TaskAttribute attribute : attachments) {
123
			for (TaskAttribute attribute : attachments) {
141
				TaskAttachment taskAttachment = new TaskAttachment(getModel().getTaskRepository(),
124
				TaskAttachment taskAttachment = new TaskAttachment(getModel().getTaskRepository(),
Lines 159-164 Link Here
159
				tableViewer.setInput(attachmentList.toArray());
142
				tableViewer.setInput(attachmentList.toArray());
160
			}
143
			}
161
		}
144
		}
145
146
		@Override
147
		protected AbstractTableSorter createTableSorter() {
148
149
			return new AbstractTableSorter() {
150
151
				@Override
152
				public void setDefault() {
153
					propertyIndex = 5;
154
					direction = SWT.UP;
155
				}
156
157
				@Override
158
				public int compare(Viewer viewer, Object e1, Object e2) {
159
					int erg = 0;
160
					ITaskAttachment attachment1 = (ITaskAttachment) e1;
161
					ITaskAttachment attachment2 = (ITaskAttachment) e2;
162
					switch (propertyIndex) {
163
					case 0:
164
						String id1 = attachment1.getUrl();
165
						String id2 = attachment2.getUrl();
166
						int i = id1.indexOf("?id="); //$NON-NLS-1$
167
						if (i != -1) {
168
							id1 = id1.substring(i + 4);
169
						} else {
170
							id1 = ""; //$NON-NLS-1$
171
						}
172
						i = id2.indexOf("?id="); //$NON-NLS-1$
173
						if (i != -1) {
174
							id2 = id2.substring(i + 4);
175
						} else {
176
							id2 = ""; //$NON-NLS-1$
177
						}
178
179
						erg = id1.length() - id2.length();
180
						if (erg == 0) {
181
							erg = id1.compareTo(id2);
182
						}
183
						break;
184
185
					case 1:
186
						String type1 = attachment1.getFileName();
187
						String type2 = attachment2.getFileName();
188
						if (AttachmentUtil.isContext(attachment1)) {
189
							type1 = Messages.AttachmentTableLabelProvider_Task_Context;
190
						} else if (attachment1.isPatch()) {
191
							type1 = Messages.AttachmentTableLabelProvider_Patch;
192
						}
193
						if (AttachmentUtil.isContext(attachment2)) {
194
							type2 = Messages.AttachmentTableLabelProvider_Task_Context;
195
						} else if (attachment2.isPatch()) {
196
							type2 = Messages.AttachmentTableLabelProvider_Patch;
197
						}
198
						erg = type1.compareTo(type2);
199
						break;
200
					case 2:
201
						String description1 = attachment1.getDescription();
202
						String description2 = attachment2.getDescription();
203
						erg = description1.compareTo(description2);
204
						break;
205
					case 3:
206
						Long length1 = attachment1.getLength();
207
						Long idLength2 = attachment2.getLength();
208
						length1 = length1 < 0 ? 0 : length1;
209
						idLength2 = idLength2 < 0 ? 0 : idLength2;
210
						erg = length1.compareTo(idLength2);
211
						break;
212
					case 4:
213
						String author1 = attachment1.getAuthor().toString();
214
						String author2 = attachment2.getAuthor().toString();
215
						erg = author1.compareTo(author2);
216
						break;
217
					case 5:
218
						Date created1 = attachment1.getCreationDate();
219
						Date created2 = attachment2.getCreationDate();
220
						if (created1 != null && created2 != null) {
221
							erg = created1.compareTo(created2);
222
						} else if (created1 == null && created2 != null) {
223
							erg = -1;
224
						} else if (created1 != null && created2 == null) {
225
							erg = 1;
226
						} else {
227
							erg = 0;
228
						}
229
						break;
230
					}
231
					// If descending order, flip the direction
232
					if (direction == DESCENDING) {
233
						erg = -erg;
234
					}
235
					return erg;
236
				}
237
			};
238
		}
162
	}
239
	}
163
240
164
	private AttachmentTableViewer attachmentsViewer;
241
	private AttachmentTableViewer attachmentsViewer;
(-)src/org/eclipse/mylyn/internal/tasks/ui/views/AbstractTableSorter.java (+85 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 Frank Becker 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
 *     Frank Becker - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui.views;
13
14
import java.io.Serializable;
15
16
import org.eclipse.jface.viewers.Viewer;
17
import org.eclipse.jface.viewers.ViewerSorter;
18
import org.eclipse.ui.IMemento;
19
20
public abstract class AbstractTableSorter extends ViewerSorter implements Serializable {
21
22
	private static final long serialVersionUID = 8048730201424836905L;
23
24
	protected int propertyIndex;
25
26
	protected static final int ASCENDING = 0;
27
28
	protected static final int DESCENDING = 1;
29
30
	protected int direction = DESCENDING;
31
32
	public AbstractTableSorter() {
33
		this.propertyIndex = 0;
34
		direction = DESCENDING;
35
	}
36
37
	public void setColumn(int column) {
38
		if (column == this.propertyIndex) {
39
			// Same column as last sort; toggle the direction
40
			direction = 1 - direction;
41
		} else {
42
			// New column; do an ascending sort
43
			this.propertyIndex = column;
44
			direction = DESCENDING;
45
		}
46
	}
47
48
	@Override
49
	public abstract int compare(Viewer viewer, Object e1, Object e2);
50
51
	public abstract void setDefault();
52
53
	protected int getPropertyIndex() {
54
		return propertyIndex;
55
	}
56
57
	protected void setPropertyIndex(int propertyIndex) {
58
		this.propertyIndex = propertyIndex;
59
	}
60
61
	protected int getDirection() {
62
		return direction;
63
	}
64
65
	protected void setDirection(int direction) {
66
		this.direction = direction;
67
	}
68
69
	public void saveState(IMemento memento) {
70
		IMemento child = memento.createChild("AbstractTableSorter"); //$NON-NLS-1$
71
		child.putInteger("propertyIndex", propertyIndex); //$NON-NLS-1$
72
		child.putInteger("direction", direction); //$NON-NLS-1$
73
74
	}
75
76
	public void readState(IMemento memento) {
77
		IMemento child = memento.getChild("AbstractTableSorter"); //$NON-NLS-1$
78
		if (child != null) {
79
			propertyIndex = child.getInteger("propertyIndex"); //$NON-NLS-1$
80
			direction = child.getInteger("direction"); //$NON-NLS-1$
81
		} else {
82
			setDefault();
83
		}
84
	}
85
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/views/AbstractTableViewerConfigurator.java (-5 / +34 lines)
Lines 31-36 Link Here
31
import org.eclipse.swt.SWT;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.events.ControlEvent;
32
import org.eclipse.swt.events.ControlEvent;
33
import org.eclipse.swt.events.ControlListener;
33
import org.eclipse.swt.events.ControlListener;
34
import org.eclipse.swt.events.SelectionAdapter;
35
import org.eclipse.swt.events.SelectionEvent;
34
import org.eclipse.swt.layout.GridLayout;
36
import org.eclipse.swt.layout.GridLayout;
35
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Composite;
36
import org.eclipse.swt.widgets.Table;
38
import org.eclipse.swt.widgets.Table;
Lines 51-65 Link Here
51
53
52
	protected int[] orderArray;
54
	protected int[] orderArray;
53
55
56
	protected AbstractTableSorter tableSorter;
57
54
	public AbstractTableViewerConfigurator(File stateFile) {
58
	public AbstractTableViewerConfigurator(File stateFile) {
55
		super();
59
		super();
56
		this.stateFile = stateFile;
60
		this.stateFile = stateFile;
61
		this.tableSorter = createTableSorter();
57
	}
62
	}
58
63
59
	abstract protected void setDefaultColumnInfos();
64
	abstract protected void setDefaultColumnInfos();
60
65
61
	abstract protected void setupTableViewer();
66
	abstract protected void setupTableViewer();
62
67
68
	abstract protected AbstractTableSorter createTableSorter();
69
63
	private void readStateFile() {
70
	private void readStateFile() {
64
		if (stateFile.exists()) {
71
		if (stateFile.exists()) {
65
			try {
72
			try {
Lines 78-83 Link Here
78
					for (int i = 0; i < orderStringArray.length; i++) {
85
					for (int i = 0; i < orderStringArray.length; i++) {
79
						orderArray[i] = Integer.parseInt(orderStringArray[i]);
86
						orderArray[i] = Integer.parseInt(orderStringArray[i]);
80
					}
87
					}
88
					tableSorter.readState(memento);
81
				} catch (WorkbenchException e) {
89
				} catch (WorkbenchException e) {
82
					StatusHandler.log(new Status(IStatus.WARNING, TasksUiPlugin.ID_PLUGIN,
90
					StatusHandler.log(new Status(IStatus.WARNING, TasksUiPlugin.ID_PLUGIN,
83
							"The TableViewerState cache could not be read", e)); //$NON-NLS-1$
91
							"The TableViewerState cache could not be read", e)); //$NON-NLS-1$
Lines 114-119 Link Here
114
			orderString += colPos;
122
			orderString += colPos;
115
		}
123
		}
116
		child.putString("order", orderString); //$NON-NLS-1$
124
		child.putString("order", orderString); //$NON-NLS-1$
125
		tableSorter.saveState(memento);
117
126
118
		try {
127
		try {
119
			FileWriter writer = new FileWriter(stateFile);
128
			FileWriter writer = new FileWriter(stateFile);
Lines 134-155 Link Here
134
143
135
	public void create(FormToolkit toolkit, Composite parent, int initialColumnCount) {
144
	public void create(FormToolkit toolkit, Composite parent, int initialColumnCount) {
136
		table = createTable(parent, toolkit);
145
		table = createTable(parent, toolkit);
146
		tableViewer = new TableViewer(table);
137
		columnInfos = new ArrayList<ColumnState>(initialColumnCount);
147
		columnInfos = new ArrayList<ColumnState>(initialColumnCount);
138
		readStateFile();
148
		readStateFile();
139
		if (columnInfos.size() == 0) {
149
		if (columnInfos.size() == 0) {
140
			setDefaultColumnInfos();
150
			setDefaultColumnInfos();
141
		}
151
		}
142
		adjustColumInfos();
152
		adjustColumInfos();
143
		for (int index = 0; index < columnInfos.size(); index++) {
153
		for (int i = 0; i < columnInfos.size(); i++) {
144
			ColumnState colState = columnInfos.get(index);
154
			final int index = i;
145
			final TableColumn column = new TableColumn(table, colState.getAlignment(), index);
155
			ColumnState colState = columnInfos.get(i);
156
			final TableColumn column = new TableColumn(table, colState.getAlignment(), i);
146
			column.setText(colState.getName());
157
			column.setText(colState.getName());
147
			column.setWidth(colState.getWidths());
158
			column.setWidth(colState.getWidths());
148
			column.setMoveable(true);
159
			column.setMoveable(true);
149
			column.addControlListener(createColumnControlListener(table, column, index));
160
			column.addControlListener(createColumnControlListener(table, column, i));
161
			column.addSelectionListener(new SelectionAdapter() {
162
				@Override
163
				public void widgetSelected(SelectionEvent e) {
164
					tableSorter.setColumn(index);
165
					int dir = table.getSortDirection();
166
					if (table.getSortColumn() == column) {
167
						dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
168
					} else {
169
						dir = SWT.DOWN;
170
					}
171
					table.setSortDirection(dir);
172
					table.setSortColumn(column);
173
					tableViewer.refresh();
174
					writeStateFile();
175
				}
176
			});
150
		}
177
		}
151
178
152
		tableViewer = new TableViewer(table);
179
		table.setSortDirection(tableSorter.getDirection() == 1 ? SWT.DOWN : SWT.UP);
180
		table.setSortColumn(table.getColumn(tableSorter.getPropertyIndex()));
181
153
		table.setColumnOrder(orderArray);
182
		table.setColumnOrder(orderArray);
154
		setupTableViewer();
183
		setupTableViewer();
155
	}
184
	}

Return to bug 302652