Skip to content

Package: PropertiesComposite$1

PropertiesComposite$1

nameinstructionbranchcomplexitylinemethod
widgetDefaultSelected(SelectionEvent)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
widgetSelected(SelectionEvent)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
{...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /********************************************************************************
2: * Copyright (c) 2011 Eike Stepper (Berlin, Germany) and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eike Stepper - initial API and implementation
13: ********************************************************************************/
14: package org.eclipse.emf.ecp.internal.ui.composites;
15:
16: import java.util.Map;
17: import java.util.Map.Entry;
18:
19: import org.eclipse.emf.ecp.core.util.ECPProperties;
20: import org.eclipse.emf.ecp.internal.ui.Messages;
21: import org.eclipse.emf.ecp.internal.ui.dialogs.PropertyDialog;
22: import org.eclipse.emf.ecp.internal.ui.model.PropertiesContentProvider;
23: import org.eclipse.emf.ecp.internal.ui.model.PropertiesLabelProvider;
24: import org.eclipse.jface.viewers.IStructuredSelection;
25: import org.eclipse.jface.viewers.TableViewer;
26: import org.eclipse.jface.viewers.TableViewerColumn;
27: import org.eclipse.jface.viewers.ViewerSorter;
28: import org.eclipse.jface.window.Window;
29: import org.eclipse.swt.SWT;
30: import org.eclipse.swt.events.SelectionEvent;
31: import org.eclipse.swt.events.SelectionListener;
32: import org.eclipse.swt.layout.GridData;
33: import org.eclipse.swt.layout.GridLayout;
34: import org.eclipse.swt.widgets.Button;
35: import org.eclipse.swt.widgets.Composite;
36: import org.eclipse.swt.widgets.Table;
37: import org.eclipse.swt.widgets.TableColumn;
38:
39: /**
40: * @author Eike Stepper
41: */
42: public class PropertiesComposite extends Composite {
43:         private final TableViewer tableViewer;
44:
45:         private final boolean editable;
46:
47:         public PropertiesComposite(Composite parent, boolean editable, final ECPProperties properties) {
48:                 super(parent, SWT.NONE);
49:                 this.editable = editable;
50:                 setLayout(new GridLayout(1, false));
51:
52:                 int style = SWT.BORDER | SWT.FULL_SELECTION;
53:                 if (!editable) {
54:                         style |= SWT.READ_ONLY;
55:                 }
56:
57:                 tableViewer = new TableViewer(this, style);
58:                 final Table table = tableViewer.getTable();
59:                 table.setLinesVisible(true);
60:                 table.setHeaderVisible(true);
61:                 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
62:
63:                 final TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
64:                 final TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
65:                 tblclmnNewColumn.setWidth(122);
66:                 tblclmnNewColumn.setText(Messages.PropertiesComposite_TableColumnName_Key);
67:
68:                 final TableViewerColumn tableViewerColumn1 = new TableViewerColumn(tableViewer, SWT.NONE);
69:                 final TableColumn tableColumn1 = tableViewerColumn1.getColumn();
70:                 tableColumn1.setWidth(314);
71:                 tableColumn1.setText(Messages.PropertiesComposite_TableColumnName_Value);
72:
73:                 tableViewer.setLabelProvider(new PropertiesLabelProvider());
74:                 tableViewer.setContentProvider(new PropertiesContentProvider());
75:                 tableViewer.setSorter(new ViewerSorter());
76:                 tableViewer.setInput(properties);
77:
78:                 if (editable) {
79:                         final Composite buttonBar = new Composite(this, SWT.NONE);
80:                         final GridLayout gridLayoutButtonBar = new GridLayout(3, false);
81:                         gridLayoutButtonBar.marginWidth = 0;
82:                         gridLayoutButtonBar.marginHeight = 0;
83:                         buttonBar.setLayout(gridLayoutButtonBar);
84:
85:                         final Button addButton = new Button(buttonBar, SWT.NONE);
86:                         addButton.setText(Messages.PropertiesComposite_AddProperty);
87:                         addButton.addSelectionListener(new SelectionListener() {
88:                                 @Override
89:                                 public void widgetSelected(SelectionEvent e) {
90:                                         final PropertyDialog dialog = new PropertyDialog(table.getShell());
91:•                                        if (dialog.open() == Window.OK) {
92:                                                 final String key = dialog.getKey();
93:                                                 final String value = dialog.getValue();
94:                                                 properties.addProperty(key, value);
95:                                         }
96:                                 }
97:
98:                                 @Override
99:                                 public void widgetDefaultSelected(SelectionEvent e) {
100:                                         widgetSelected(e);
101:                                 }
102:                         });
103:
104:                         final Button editButton = new Button(buttonBar, SWT.NONE);
105:                         editButton.setText(Messages.PropertiesComposite_EditProperty);
106:                         editButton.addSelectionListener(new SelectionListener() {
107:                                 @Override
108:                                 public void widgetSelected(SelectionEvent e) {
109:                                         final IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
110:
111:                                         @SuppressWarnings("unchecked")
112:                                         final Map.Entry<String, String> property = (Entry<String, String>) selection.getFirstElement();
113:
114:                                         final PropertyDialog dialog = new PropertyDialog(table.getShell(), false, property.getKey(),
115:                                                 property
116:                                                         .getValue());
117:                                         if (dialog.open() == Window.OK) {
118:                                                 properties.addProperty(dialog.getKey(), dialog.getValue());
119:                                         }
120:                                 }
121:
122:                                 @Override
123:                                 public void widgetDefaultSelected(SelectionEvent e) {
124:                                         // Do nothing
125:                                 }
126:                         });
127:
128:                         final Button removeButton = new Button(buttonBar, SWT.NONE);
129:                         removeButton.setText(Messages.PropertiesComposite_RemoveProperty);
130:                         removeButton.addSelectionListener(new SelectionListener() {
131:                                 @Override
132:                                 public void widgetSelected(SelectionEvent e) {
133:                                         final IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
134:
135:                                         @SuppressWarnings("unchecked")
136:                                         final Map.Entry<String, String> property = (Entry<String, String>) selection.getFirstElement();
137:
138:                                         properties.removeProperty(property.getKey());
139:                                 }
140:
141:                                 @Override
142:                                 public void widgetDefaultSelected(SelectionEvent e) {
143:                                         // Do nothing
144:                                 }
145:                         });
146:                 }
147:         }
148:
149:         public final TableViewer getTableViewer() {
150:                 return tableViewer;
151:         }
152:
153:         public final boolean isEditable() {
154:                 return editable;
155:         }
156:
157:         @Override
158:         protected void checkSubclass() {
159:                 // Disable the check that prevents subclassing of SWT components
160:         }
161: }