]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui.workbench/src/org/simantics/utils/ui/workbench/dialogs/PropertyDialog.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui.workbench / src / org / simantics / utils / ui / workbench / dialogs / PropertyDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * Created on 13.10.2005
14  * @author Toni Kalajainen 
15  */
16 package org.simantics.utils.ui.workbench.dialogs;
17
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.jface.viewers.IStructuredContentProvider;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Table;
29 import org.eclipse.swt.widgets.TableColumn;
30 import org.eclipse.ui.dialogs.ListDialog;
31 import org.simantics.utils.ui.jface.StringListLabelProvider;
32 import org.simantics.utils.ui.workbench.ui.TableColumnSorter;
33
34
35
36 /**
37  * PropertyDialog is a dialog window popup for properties (key, value pairs)
38  *
39  */
40 public class PropertyDialog extends ListDialog {
41
42     public final static String KEY_COLUMN_NAME = "Key";
43     public final static String VALUE_COLUMN_NAME = "Value";
44     
45     protected List<String[]>data;
46     protected StringListLabelProvider labelProvider;
47     
48     public PropertyDialog(Shell parent) {
49         super(parent);
50         
51         this.setContentProvider(new IStructuredContentProvider() {
52             public Object[] getElements(Object inputElement) {
53                 List<?> lines = (List<?>) inputElement;
54                 return lines.toArray(new String[0][0]);
55             }
56             public void dispose() {
57             }
58             public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
59             }
60         });
61     }
62
63     public int columnNameToColumn(String name) {
64         if (name.equals(KEY_COLUMN_NAME)) return 0;
65         if (name.equals(VALUE_COLUMN_NAME)) return 1;
66         return -1;
67     }
68     
69     @Override
70     protected Control createDialogArea(Composite container) {
71         Control composite = super.createDialogArea(container);
72         TableViewer viewer = getTableViewer();
73         Table table = viewer.getTable();
74         
75         //viewer.setUseHashlookup(true);
76         table.setHeaderVisible(true);
77         table.setLinesVisible(true);
78         
79         
80         
81         final TableColumn tableColumn = new TableColumn(table, table.getStyle(), 0);
82         tableColumn.setWidth(100);
83         tableColumn.setText(KEY_COLUMN_NAME);
84
85         final TableColumn tableColumn_2 = new TableColumn(table, table.getStyle(), 1);
86         tableColumn_2.setWidth(190);
87         tableColumn_2.setText(VALUE_COLUMN_NAME);
88         
89         // Dialog area is created when dialog.open is called
90         // which is after input has been set
91         // Therefore there columns have not been created at the time
92         // of setting input object before columns have been set.
93         // Re-setting input forces to read columns
94         viewer.setInput( getTableViewer().getInput() );
95         viewer.setColumnProperties(new String[] {KEY_COLUMN_NAME, VALUE_COLUMN_NAME});
96
97         TableColumnSorter.attachTableColumnSorter( viewer );
98
99         return composite;
100     }
101     
102     public void setInput(String lines[][]) {
103         labelProvider = new StringListLabelProvider(lines);
104         this.setLabelProvider( labelProvider );
105         data = labelProvider.getData();
106         super.setInput(data);
107     }
108     
109     @Override
110     protected int getTableStyle()
111     {
112         return SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION; 
113     }
114     
115     public void setInput(Map<String, String> properties) {
116         String lines[][] = new String[properties.size()][];
117         int i = 0;
118         for (String key : properties.keySet()) {
119             String line[] = new String[] {key, properties.get(key)};
120             lines[i++] = line;
121         }
122         setInput(lines);
123     }
124 }
125
126