Skip to main content
summaryrefslogtreecommitdiffstats
blob: 57d47a8dc8225c7757d7c68e1f1c3eeb070a9885 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
 * Copyright (c) 2013 Ericsson
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Jacques Bouthillier - Initial Implementation of the table view
 ******************************************************************************/
package org.eclipse.mylyn.gerrit.dashboard.ui.internal.model;



import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.mylyn.gerrit.dashboard.core.GerritTask;
import org.eclipse.mylyn.gerrit.dashboard.ui.GerritUi;
import org.eclipse.mylyn.gerrit.dashboard.ui.internal.commands.table.AdjustMyStarredHandler;
import org.eclipse.mylyn.gerrit.dashboard.ui.internal.utils.UIUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * 	This class implements the implementation of the review table view.
 *
 * @author Jacques Bouthillier
 * @version $Revision: 1.0 $
 * 
 */
public class UIReviewTable {

	private final int TABLE_STYLE = (SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);

	// ------------------------------------------------------------------------
	// Variables
	// ------------------------------------------------------------------------
	private TableViewer fViewer;

	// ------------------------------------------------------------------------
	// Constructors
	// ------------------------------------------------------------------------

	public UIReviewTable() {

	}

	// ------------------------------------------------------------------------
	// Methods
	// ------------------------------------------------------------------------

	public TableViewer createTableViewerSection(Composite aParent) {
		// Create a form to maintain the search data
		Composite viewerForm = UIUtils.createsGeneralComposite(aParent,
				SWT.BORDER | SWT.SHADOW_ETCHED_IN);

		GridData gribDataViewer = new GridData(GridData.FILL_BOTH);
		viewerForm.setLayoutData(gribDataViewer);

		// Add a listener when the view is resized
		GridLayout layout = new GridLayout();
		layout.numColumns = ReviewTableDefinition.values().length;
		layout.makeColumnsEqualWidth = false;
		layout.marginWidth = 0;
		layout.marginHeight = 0;

		viewerForm.setLayout(layout);

		// Create the table viewer to maintain the list of reviews
		fViewer = new TableViewer(viewerForm, TABLE_STYLE);
		fViewer = buildAndLayoutTable(fViewer);

		// Set the content provider and the Label provider and the sorter
		fViewer.setContentProvider(new ReviewTableContentProvider());

		// Set the viewer for the provider
		ReviewTableLabelProvider tableProvider = new ReviewTableLabelProvider();
		fViewer.setLabelProvider(tableProvider);
		ReviewTableSorter.bind(fViewer);

		// Create the help context id for the viewer's control
		// PlatformUI
		// .getWorkbench()
		// .getHelpSystem()
		// .setHelp(fViewer.getControl(),
		// "org.eclipse.mylyn.gerrit.dashboard.ui.viewer");

		//
		fViewer.getTable().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				GerritUi.Ftracer.traceInfo("Table selection: "
						+ e.toString());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});

		// Add a Key event and mouse down listener
		fViewer.getTable().addListener(SWT.MouseDown, mouseButtonListener);

		return fViewer;

	}

	/**
	 * Create each column for the List of Reviews
	 * 
	 * @param aParent
	 * @param aViewer
	 */
	private TableViewer buildAndLayoutTable(final TableViewer aViewer) {
		final Table table = aViewer.getTable();
		
		//Get the review table definition
		ReviewTableDefinition[] tableInfo = ReviewTableDefinition.values();
		int size = tableInfo.length;
		GerritUi.Ftracer.traceInfo("Table	Name	Width	Resize Moveable");
		for  (int index = 0; index < size; index++) {
			GerritUi.Ftracer.traceInfo("index [ " + index + 
					" ] "  + tableInfo[index].getName() + 
					"\t: " + tableInfo[index].getWidth() +
					"\t: " + tableInfo[index].getResize() +
					"\t: " + tableInfo[index].getMoveable() );
			TableViewerColumn col = createTableViewerColumn(tableInfo[index]);
			
			GridData gribData = new GridData(GridData.FILL_BOTH);
			gribData.minimumWidth = tableInfo[index].getWidth();
			col.getColumn().getParent().setLayoutData(gribData);
		}

		TableLayout tableLayout = new TableLayout();
		table.setLayout(tableLayout);
		table.addControlListener(new ControlListener() {
			
			@Override
			public void controlResized(ControlEvent e) {
				table.setRedraw(false);
				Point tableSize = table.getSize();
				Point parentSize = table.getParent().getSize();
				//Adjust the width  according to its parent
				int minimumTableWidth = ReviewTableDefinition.getMinimumWidth();
				int mimimumSubjectWidth = ReviewTableDefinition.SUBJECT.getWidth();
				int minProjectWidth = ReviewTableDefinition.PROJECT.getWidth();
				
				//Adjust the subject and project column to take the remaining space
				int scrollWidth = table.getVerticalBar().getSize().x;
				//If not visible, take the extra space
				if (!table.getVerticalBar().isVisible()) {
					scrollWidth = 0;
				}

				int computeExtraWidth = parentSize.x - 10 - ( minimumTableWidth ) - scrollWidth ;
				int newSubjectWidth = mimimumSubjectWidth;
				int newProjectWidth = minProjectWidth;
				//If extra space, redistribute it to specific column
				if (computeExtraWidth > 0) {
					//Assign some to subject and some to Project
					int value = 2*computeExtraWidth  /3;
					newSubjectWidth = mimimumSubjectWidth + value; // 2/3 of the extra
					newProjectWidth = minProjectWidth + computeExtraWidth- value;       // 1/3 of the extra
				}
				//Subject column
				table.getColumn(2).setWidth(newSubjectWidth);
				//Project column
				table.getColumn(4).setWidth(newProjectWidth);

				table.setSize(parentSize.x - 10, tableSize.y);
				table.setRedraw(true);
				
			}
			
			@Override
			public void controlMoved(ControlEvent e) {
				
			}
		});

		
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		return aViewer;
	}
	

	/**
	 * Create each column in the review table list
	 * 
	 * @param ReviewTableDefinition
	 * @return TableViewerColumn
	 */
	private TableViewerColumn createTableViewerColumn(
			ReviewTableDefinition aTableInfo) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(fViewer,
				SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(aTableInfo.getName());
		column.setWidth(aTableInfo.getWidth());
		column.setAlignment(aTableInfo.getAlignment());
		column.setResizable(aTableInfo.getResize());
		column.setMoveable(aTableInfo.getMoveable());
		return viewerColumn;

	}

	private Listener mouseButtonListener = new Listener() {
		public void handleEvent(Event aEvent) {
			GerritUi.Ftracer.traceInfo("mouseButtonListener() for "
					+ aEvent.button);
			switch (aEvent.type) {
			case SWT.MouseDown:
				// Left Click
				if (aEvent.button == 1) {

					// Process the Item table handling
					processItemSelection();

				}
				// For now, use button 2 to modify the starred value column 1
				if (aEvent.button == 2) {
					// Select the new item in the table
					Table table = fViewer.getTable();
					table.deselectAll();
					Point p = new Point(aEvent.x, aEvent.y);
					TableItem tbi = fViewer.getTable().getItem(p);
					if (tbi != null) {
						table.setSelection(tbi);						
					}

					// Execute the command to adjust the column: ID with the
					// starred information
					AdjustMyStarredHandler handler = new AdjustMyStarredHandler();
					try {
						handler.execute(new ExecutionEvent());
					} catch (ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				// Right Click
				if (aEvent.button == 3) {
					// Process the Item table handling
					// processItemSelection();
				}
				break;
			default:
				break;
			}
		}

	};

	/**
	 * Process the selected data from the item table
	 */
	private void processItemSelection() {
		ISelection tableSelection = fViewer.getSelection();
		GerritUi.Ftracer
				.traceInfo("Selected : " + tableSelection.getClass());
		if (tableSelection.isEmpty()) {
			GerritUi.Ftracer.traceInfo("Selected table selection is EMPTY ");

		} else {
			if (tableSelection instanceof IStructuredSelection ) {
				Object obj = ((IStructuredSelection) tableSelection).getFirstElement();
				GerritUi.Ftracer.traceInfo("Selected table selection class: " + obj.getClass() ); 
				if (obj instanceof  GerritTask) {
					GerritTask item = (GerritTask) obj;
					GerritUi.Ftracer.traceInfo("Selected table OBJECT selection ID: "  + item.getAttribute(GerritTask.SHORT_CHANGE_ID) + 
							"\t subject: " + item.getAttribute(GerritTask.SUBJECT)); 				
				}
			}
		}
		// if (tableSelection.length == 1) {
		// ReviewTableListItem selected = tableSelection[0];
		// }
		// if (GerritTableView.getActiveView() != null) {
		// ArrayList<IReviewEntityItem> itemlist = getSelectedItems();
		// // Number of item to set the check flag
		// if (itemlist.size() > 0) {
		// // Only display the first one
		// IReviewEntityItem item = itemlist.get(0);
		// ReviewItemNavigatorViewPart.getInstance().displayInfo(item);
		// } else {
		// ReviewItemNavigatorAction.updateItemNavigatorIcon();
		// }
		// }
		// // Set the review view toolbar
		// ReviewItemNavigatorAction.updateItemNavigatorIcon();
		//
		// ReviewTableCommonAction.setToolbarButtonsSensitivity();
	}

}

Back to the top