1 /*******************************************************************************
2  * Copyright (c) 2014 TwelveTone LLC and others.
3  *
4  * 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  * Steven Spungin <steven@spungin.tv> - initial API and implementation, Bug 432555, Bug 436889
13  *******************************************************************************/
14 
15 package org.eclipse.e4.tools.emf.ui.internal.common.component.tabs;
16 
17 import org.eclipse.e4.core.contexts.IEclipseContext;
18 import org.eclipse.e4.tools.emf.ui.internal.ResourceProvider;
19 import org.eclipse.e4.tools.services.IResourcePool;
20 import org.eclipse.emf.ecore.EAttribute;
21 import org.eclipse.emf.ecore.EObject;
22 import org.eclipse.jface.viewers.ColumnLabelProvider;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.TableViewerColumn;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Color;
27 import org.eclipse.swt.graphics.Image;
28 
29 /**
30  * A table viewer column for editing an EMF EAttribute If the object does not
31  * have an attribute with the given ID, the field is disabled.
32  *
33  * @author Steven Spungin
34  *
35  */
36 public class EAttributeTableViewerColumn {
37 
38 	private TableViewerColumn tableViewerColumn;
39 	private IResourcePool resourcePool;
40 	private String attName;
41 	private TableViewer tvResults;
42 
EAttributeTableViewerColumn(final TableViewer tvResults, String label, final String attName, final IEclipseContext context)43 	public EAttributeTableViewerColumn(final TableViewer tvResults, String label, final String attName,
44 		final IEclipseContext context) {
45 		this.tvResults = tvResults;
46 		this.attName = attName;
47 		tableViewerColumn = new TableViewerColumn(tvResults, SWT.NONE);
48 
49 		// CAN be null. Used for checkbox icon.
50 		resourcePool = context.get(IResourcePool.class);
51 
52 		tableViewerColumn.getColumn().setText(label);
53 		tableViewerColumn.setLabelProvider(new ColumnLabelProvider() {
54 			@Override
55 			public String getText(Object element) {
56 				final EObject eObject = (EObject) element;
57 				final EAttribute eAtt = EmfUtil.getAttribute(eObject, attName);
58 				Object value;
59 				if (eAtt != null) {
60 					value = eObject.eGet(eAtt);
61 				} else {
62 					value = ""; //$NON-NLS-1$
63 				}
64 				switch (EAttributeEditingSupport.getAttributeType(element, attName)) {
65 				case BOOLEAN:
66 					// if no icons provided, use text instead of checkbox
67 					return resourcePool == null && (Boolean) value ? "X" : ""; //$NON-NLS-1$ //$NON-NLS-2$
68 				case STRING:
69 				case NOT_AN_ATTRIBUTE:
70 				case OTHER:
71 				default:
72 					return super.getText(value);
73 				}
74 			}
75 
76 			@Override
77 			public Image getImage(Object element) {
78 				switch (EAttributeEditingSupport.getAttributeType(element, attName)) {
79 				case BOOLEAN:
80 					if (resourcePool != null) {
81 						final Object value = EmfUtil.getAttributeValue((EObject) element, attName);
82 						if (value != null && value.equals(true)) {
83 							return resourcePool.getImageUnchecked(ResourceProvider.IMG_Widgets_checkbox_obj);
84 						}
85 					}
86 					// fall through
87 					//$FALL-THROUGH$
88 				case STRING:
89 				case NOT_AN_ATTRIBUTE:
90 				case OTHER:
91 				default:
92 					return super.getImage(element);
93 				}
94 			}
95 
96 			@Override
97 			public Color getBackground(Object element) {
98 				return EAttributeTableViewerColumn.this.getBackground(element);
99 			}
100 		});
101 		tableViewerColumn.setEditingSupport(new EAttributeEditingSupport(tvResults, attName, context));
102 	}
103 
getBackground(Object element)104 	public Color getBackground(Object element) {
105 		final EObject eObject = (EObject) element;
106 		final EAttribute eAtt = EmfUtil.getAttribute(eObject, attName);
107 		if (eAtt == null) {
108 			return tvResults.getTable().getDisplay().getSystemColor(SWT.COLOR_GRAY);
109 		}
110 		return null;
111 	}
112 
dispose()113 	public void dispose() {
114 		tableViewerColumn.getColumn().dispose();
115 	}
116 
getTableViewerColumn()117 	public TableViewerColumn getTableViewerColumn() {
118 		return tableViewerColumn;
119 	}
120 
121 }
122