|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.carbide.cpp.internal.pi.button.ui; |
|
19 |
|
20 import java.util.ArrayList; |
|
21 import java.util.Set; |
|
22 |
|
23 import org.eclipse.jface.layout.GridDataFactory; |
|
24 import org.eclipse.jface.resource.FontRegistry; |
|
25 import org.eclipse.jface.viewers.ArrayContentProvider; |
|
26 import org.eclipse.jface.viewers.IFontProvider; |
|
27 import org.eclipse.jface.viewers.ITableLabelProvider; |
|
28 import org.eclipse.jface.viewers.LabelProvider; |
|
29 import org.eclipse.jface.viewers.TableViewer; |
|
30 import org.eclipse.jface.viewers.Viewer; |
|
31 import org.eclipse.jface.viewers.ViewerSorter; |
|
32 import org.eclipse.swt.SWT; |
|
33 import org.eclipse.swt.events.SelectionEvent; |
|
34 import org.eclipse.swt.events.SelectionListener; |
|
35 import org.eclipse.swt.graphics.Font; |
|
36 import org.eclipse.swt.graphics.Image; |
|
37 import org.eclipse.swt.widgets.Composite; |
|
38 import org.eclipse.swt.widgets.Table; |
|
39 import org.eclipse.swt.widgets.TableColumn; |
|
40 |
|
41 import com.nokia.carbide.cpp.pi.button.IBupEventMap; |
|
42 import com.nokia.carbide.cpp.pi.button.IBupEventMapEditable; |
|
43 |
|
44 public class BupMapTableViewer extends TableViewer { |
|
45 // Set the table column property names |
|
46 private final String KEY_CODE_COLUMN = Messages.getString("BupMapTableViewer.keycode"); //$NON-NLS-1$ |
|
47 private final String ENUM_COLUMN = Messages.getString("BupMapTableViewer.enum"); //$NON-NLS-1$ |
|
48 private final String LABEL_COLUMN = Messages.getString("BupMapTableViewer.label"); //$NON-NLS-1$ |
|
49 private static final FontRegistry fontRegistry = new FontRegistry(); |
|
50 private static Font regularFont = fontRegistry.get(""); //$NON-NLS-1$ |
|
51 private static Font boldFont = fontRegistry.getBold(""); //$NON-NLS-1$ |
|
52 private TableColumn columnKeyCode; |
|
53 private TableColumn columnEnum; |
|
54 private TableColumn columnLabel; |
|
55 |
|
56 // Set column names |
|
57 private String[] columnNames = new String[] { |
|
58 KEY_CODE_COLUMN, |
|
59 ENUM_COLUMN, |
|
60 LABEL_COLUMN |
|
61 }; |
|
62 |
|
63 public class BupMapEntry { |
|
64 public int keyCode; |
|
65 public String enumString; |
|
66 public String label; |
|
67 public boolean modified; |
|
68 } |
|
69 |
|
70 private ArrayList<BupMapEntry> mapList = new ArrayList<BupMapEntry>(); |
|
71 private BupMapEntrySorter mapEntrySorter = new BupMapEntrySorter(); |
|
72 public BupMapTableViewer mapTableViewer = this; |
|
73 boolean modifible = false; |
|
74 |
|
75 public class BupMapEntrySorter extends ViewerSorter { |
|
76 // public constants indicating sorting type |
|
77 public final static int SORT_BY_KEYCODE = 0; |
|
78 public final static int SORT_BY_ENUM = 1; |
|
79 public final static int SORT_BY_LABEL = 2; |
|
80 |
|
81 TableColumn columnCurrentSort = columnKeyCode; |
|
82 int dirKeyCode = SWT.UP; |
|
83 int dirEnum = SWT.UP; |
|
84 int dirLabel = SWT.UP; |
|
85 |
|
86 public BupMapEntrySorter () { |
|
87 super(); |
|
88 } |
|
89 |
|
90 // only sort by keyCode |
|
91 public int compare(Viewer viewer, Object o1, Object o2) { |
|
92 if (!(o1 instanceof BupMapEntry) || |
|
93 !(o2 instanceof BupMapEntry)) { |
|
94 return 0; |
|
95 } |
|
96 |
|
97 // ask table for sorting column criterion |
|
98 TableColumn tableColumn = ((TableViewer)viewer).getTable().getSortColumn(); |
|
99 int result = 0; |
|
100 if (tableColumn == columnKeyCode) { |
|
101 result = new Integer(((BupMapEntry)o1).keyCode).compareTo(((BupMapEntry)o2).keyCode); |
|
102 if (dirKeyCode != SWT.UP) { |
|
103 result *= -1; |
|
104 } |
|
105 } else if (tableColumn == columnEnum) { |
|
106 result = ((BupMapEntry)o1).enumString.compareTo(((BupMapEntry)o2).enumString); |
|
107 if (dirEnum != SWT.UP) { |
|
108 result *= -1; |
|
109 } |
|
110 }else if (tableColumn == columnLabel) { |
|
111 result = ((BupMapEntry)o1).label.compareTo(((BupMapEntry)o2).label); |
|
112 if (dirLabel != SWT.UP) { |
|
113 result *= -1; |
|
114 } |
|
115 } |
|
116 |
|
117 return result; |
|
118 } |
|
119 |
|
120 /** |
|
121 * @param currentSortColumn |
|
122 */ |
|
123 public void setSortCriteria(TableColumn columnNewSort) { |
|
124 |
|
125 if (columnCurrentSort != columnNewSort) { |
|
126 // update sorting column criterion to table |
|
127 mapTableViewer.getTable().setSortColumn(columnNewSort); |
|
128 columnCurrentSort = columnNewSort; |
|
129 } else { |
|
130 // update sorting order we keep locally, I wish there was getSortDirection(TableColumn) |
|
131 if (columnNewSort == columnKeyCode) { |
|
132 dirKeyCode = dirKeyCode == SWT.UP ? SWT.DOWN : SWT.UP; |
|
133 } else if (columnNewSort == columnEnum) { |
|
134 dirEnum = dirEnum == SWT.UP ? SWT.DOWN : SWT.UP; |
|
135 }else if (columnNewSort == columnLabel) { |
|
136 dirLabel = dirLabel == SWT.UP ? SWT.DOWN : SWT.UP; |
|
137 } |
|
138 } |
|
139 if (columnNewSort == columnKeyCode) { |
|
140 mapTableViewer.getTable().setSortDirection(dirKeyCode); |
|
141 } else if (columnNewSort == columnEnum) { |
|
142 mapTableViewer.getTable().setSortDirection(dirEnum); |
|
143 }else if (columnNewSort == columnLabel) { |
|
144 mapTableViewer.getTable().setSortDirection(dirLabel); |
|
145 } |
|
146 } |
|
147 |
|
148 } |
|
149 |
|
150 public class BupMapTableLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider { |
|
151 |
|
152 /* (non-Javadoc) |
|
153 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int) |
|
154 */ |
|
155 public Image getColumnImage(Object arg0, int arg1) { |
|
156 return null; |
|
157 } |
|
158 |
|
159 /* (non-Javadoc) |
|
160 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int) |
|
161 */ |
|
162 public String getColumnText(Object element, int columnIndex) { |
|
163 String result = ""; //$NON-NLS-1$ |
|
164 BupMapEntry mapEntry = (BupMapEntry) element; |
|
165 |
|
166 switch(columnIndex) { |
|
167 case 0: |
|
168 result = Integer.toHexString(mapEntry.keyCode); |
|
169 break; |
|
170 case 1: |
|
171 result = mapEntry.enumString; |
|
172 break; |
|
173 case 2: |
|
174 result = mapEntry.label; |
|
175 break; |
|
176 } |
|
177 return result; |
|
178 } |
|
179 |
|
180 /* (non-Javadoc) |
|
181 * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object) |
|
182 */ |
|
183 public Font getFont(Object element) { |
|
184 if (((BupMapEntry)element).modified) { |
|
185 return boldFont; |
|
186 } |
|
187 return regularFont; |
|
188 } |
|
189 } |
|
190 |
|
191 /** |
|
192 * @param parent |
|
193 */ |
|
194 public BupMapTableViewer(Composite parent, boolean modify) { |
|
195 super(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | (modify ? SWT.FULL_SELECTION | SWT.SINGLE : SWT.NONE)); |
|
196 final int KEYCODE_WIDTH = 60; |
|
197 final int ENUM_WIDTH = 150; |
|
198 final int LABEL_WIDTH = 200; |
|
199 |
|
200 modifible = modify; |
|
201 |
|
202 setSorter(mapEntrySorter); |
|
203 |
|
204 SelectionListener columnSelectinListener = new SelectionListener () { |
|
205 |
|
206 public void widgetDefaultSelected(SelectionEvent arg0) { |
|
207 } |
|
208 |
|
209 public void widgetSelected(SelectionEvent arg0) { |
|
210 // determine new sort column and direction |
|
211 TableColumn currentSortColumn = (TableColumn) arg0.widget; |
|
212 |
|
213 mapEntrySorter.setSortCriteria(currentSortColumn); |
|
214 |
|
215 // sort the data based on column and direction criteria, sort well pick that up from table |
|
216 mapTableViewer.getSorter().sort(mapTableViewer, mapList.toArray()); |
|
217 mapTableViewer.setInput(mapList.toArray()); |
|
218 } |
|
219 }; |
|
220 |
|
221 columnKeyCode = new TableColumn(getTable(), SWT.LEFT, 0); |
|
222 columnKeyCode.setText(Messages.getString("BupMapTableViewer.hex")); //$NON-NLS-1$ |
|
223 columnKeyCode.setWidth(60); |
|
224 columnKeyCode.addSelectionListener (columnSelectinListener); |
|
225 |
|
226 columnEnum = new TableColumn(getTable(), SWT.LEFT, 1); |
|
227 columnEnum.setText(Messages.getString("BupMapTableViewer.tKeyCode")); //$NON-NLS-1$ |
|
228 columnEnum.setWidth(150); |
|
229 columnEnum.addSelectionListener (columnSelectinListener); |
|
230 |
|
231 columnLabel = new TableColumn(getTable(), SWT.LEFT, 2); |
|
232 columnLabel.setText(Messages.getString("BupMapTableViewer.Label")); //$NON-NLS-1$ |
|
233 columnLabel.setWidth(200); |
|
234 columnLabel.addSelectionListener (columnSelectinListener); |
|
235 |
|
236 setColumnProperties(columnNames); |
|
237 |
|
238 Table mappingTable = getTable(); |
|
239 // initial size, so scroll bar show up |
|
240 GridDataFactory.fillDefaults().grab(true, true).hint(KEYCODE_WIDTH + ENUM_WIDTH + LABEL_WIDTH - 15, 200).applyTo(mappingTable); |
|
241 mappingTable.setHeaderVisible(true); |
|
242 mappingTable.setLinesVisible(true); |
|
243 |
|
244 setContentProvider(new ArrayContentProvider()); |
|
245 setLabelProvider(new BupMapTableLabelProvider()); |
|
246 |
|
247 setInput(mapList.toArray(new BupMapEntry[mapList.size()])); |
|
248 } |
|
249 |
|
250 public void loadMap(IBupEventMap map) { |
|
251 mapList.clear(); |
|
252 Set<Integer> keyCodeSet = map.getKeyCodeSet(); |
|
253 BupMapEntry entry; |
|
254 for (Integer keyCode : keyCodeSet) { |
|
255 entry = new BupMapEntry(); |
|
256 entry.keyCode = keyCode.intValue(); |
|
257 entry.enumString = map.getEnum(keyCode); |
|
258 entry.label = map.getLabel(keyCode); |
|
259 if (map instanceof IBupEventMapEditable) { |
|
260 entry.modified = ((IBupEventMapEditable)map).isModified(keyCode); |
|
261 } else { |
|
262 entry.modified = false; |
|
263 } |
|
264 mapList.add(entry); |
|
265 } |
|
266 setInput(mapList.toArray(new BupMapEntry[mapList.size()])); |
|
267 } |
|
268 } |