Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1c31b4708b410f087c3c4b236092317e2e5e15c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2011 Formal Mind GmbH and University of Dusseldorf.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Michael Jastram - initial API and implementation
 ******************************************************************************/
package org.eclipse.rmf.reqif10.agilegrid;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import org.agilemore.agilegrid.AgileGrid;
import org.agilemore.agilegrid.SWTResourceManager;
import org.agilemore.agilegrid.renderers.TextCellRenderer;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.eclipse.rmf.reqif10.AttributeValue;
import org.eclipse.rmf.reqif10.EnumValue;
import org.eclipse.rmf.reqif10.util.ProrUtil;
import org.eclipse.rmf.reqif10.util.Reqif10Util;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;

/**
 * @author Lukas Ladenberger
 * 
 */
public class AbstractProrCellRenderer extends TextCellRenderer {

	private final AdapterFactory adapterFactory;

	/**
	 * @param agileGrid
	 */
	public AbstractProrCellRenderer(AgileGrid agileGrid, AdapterFactory adapterFactory) {
		super(agileGrid);
		this.adapterFactory = adapterFactory;
	}

	protected int doDrawCellContentDefault(GC gc, Rectangle rect, Object value) {
		String stringValue;
		if (value instanceof AttributeValue) {
			Object v = Reqif10Util.getTheValue((AttributeValue) value);
			if (v instanceof XMLGregorianCalendar) {
				XMLGregorianCalendar cal = (XMLGregorianCalendar) v;
				Date date = cal.toGregorianCalendar().getTime();
				stringValue = DateFormat.getDateInstance().format(date);
			} else if (v instanceof List<?>) {
				stringValue = convertListToString((List<?>) v);
			} else {
				stringValue = v == null ? "" : v.toString();
			}
		} else {
			stringValue = value != null ? value.toString() : "";
		}

		int alignment = getAlignment();
		String wrappedText = wrapText(gc, stringValue, rect.width);
		drawTextImage(gc, wrappedText, alignment, null, alignment, rect.x + 3,
				rect.y + 2, rect.width - 6, rect.height - 4);
		return gc.textExtent(wrappedText).y;
	}

	private String convertListToString(List<?> list) {
		String stringValue;
		StringBuffer sb = new StringBuffer();
		for (Object object : list) {
			if (object instanceof EnumValue) {
				ItemProviderAdapter itemProvider = ProrUtil.getItemProvider(adapterFactory, object);
				sb.append(itemProvider.getText(object));

			} else {
				sb.append(object.toString());
			}
			sb.append("\n");
		}
		if (sb.length() > 0)
			sb.delete(sb.length() - 1, sb.length());
		stringValue = sb.toString();
		return stringValue;
	}

	// Workaround: Upon closing a UIEditor and reopening a new one, the color got
	// disposed. No idea why. This is a workaround.
	@Override
	protected void initialColor(int row, int col) {
		if (agileGrid.isCellSelected(row, col)) {
			background = SWTResourceManager.getColor(223, 227, 237);
		}
	}

}

Back to the top