2 package eu
.etaxonomy
.taxeditor
.ui
.element
;
5 import java
.util
.Map
.Entry
;
7 import org
.eclipse
.jface
.viewers
.IStructuredContentProvider
;
8 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
9 import org
.eclipse
.jface
.viewers
.LabelProvider
;
10 import org
.eclipse
.jface
.viewers
.TableViewer
;
11 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
12 import org
.eclipse
.jface
.viewers
.Viewer
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.graphics
.Image
;
15 import org
.eclipse
.swt
.widgets
.Label
;
16 import org
.eclipse
.swt
.widgets
.Table
;
17 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
22 * <p>KeyValueViewerElement class.</p>
26 public class KeyValueViewerElement
extends AbstractCdmFormElement
{
28 private TableViewer tableViewer
;
33 * <p>Constructor for KeyValueViewerElement.</p>
35 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
36 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
37 * @param keyHeading a {@link java.lang.String} object.
38 * @param valueHeading a {@link java.lang.String} object.
39 * @param map a {@link java.util.Map} object.
41 public KeyValueViewerElement(CdmFormFactory formFactory
,
42 ICdmFormElement formElement
, String keyHeading
, String valueHeading
, Map
<Object
, Object
> map
) {
43 super(formFactory
, formElement
);
45 label
= formFactory
.createLabel(getLayoutComposite(), "Meta Data");
46 label
.setLayoutData(LayoutConstants
.FILL(2, 1));
49 tableViewer
= new TableViewer(formElement
.getLayoutComposite(), SWT
.MULTI
| SWT
.H_SCROLL
50 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
51 addControl(tableViewer
.getControl());
53 createColumns(tableViewer
, keyHeading
, valueHeading
);
55 TableWrapData layoutData
= LayoutConstants
.FILL(2, 1);
56 layoutData
.heightHint
= 100;
58 tableViewer
.getControl().setLayoutData(layoutData
);
60 tableViewer
.setContentProvider(new KeyValueContentProvider());
61 tableViewer
.setLabelProvider(new KeyValueLabelProvider());
69 // This will create the columns for the table
70 private void createColumns(TableViewer viewer
, String keyHeading
, String valueHeading
) {
71 Table table
= viewer
.getTable();
72 String
[] titles
= {keyHeading
, valueHeading
};
73 int[] bounds
= { 100, 100};
75 for (int i
= 0; i
< titles
.length
; i
++) {
76 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
77 column
.getColumn().setText(titles
[i
]);
78 column
.getColumn().setWidth(bounds
[i
]);
79 column
.getColumn().setResizable(true);
80 column
.getColumn().setMoveable(true);
82 table
.setHeaderVisible(true);
83 table
.setLinesVisible(true);
90 * @param map a {@link java.util.Map} object.
92 public void setInput(Map
<?
extends Object
, ?
extends Object
> map
) {
93 tableViewer
.setInput(map
);
94 tableViewer
.refresh();
97 private class KeyValueContentProvider
implements IStructuredContentProvider
{
100 public Object
[] getElements(Object inputElement
) {
101 if(inputElement
instanceof Map
){
102 return ((Map
) inputElement
).entrySet().toArray();
108 public void dispose() {}
111 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}
115 private class KeyValueLabelProvider
extends LabelProvider
implements ITableLabelProvider
{
118 public Image
getColumnImage(Object element
, int columnIndex
) {
123 public String
getColumnText(Object element
, int columnIndex
) {
124 if(element
instanceof Entry
){
126 Entry
<?
,?
> entry
= (Entry
<?
,?
>) element
;
130 return entry
.getKey().toString();
132 return entry
.getValue().toString();
135 return element
.toString();