Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aec14dc88e77ea5c43eda23d07350612917cfff9 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * 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.pror.reqif10.editor.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.SWTX;
import org.agilemore.agilegrid.renderers.TextCellRenderer;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.eclipse.rmf.pror.reqif10.util.ProrUtil;
import org.eclipse.rmf.reqif10.AttributeValue;
import org.eclipse.rmf.reqif10.EnumValue;
import org.eclipse.rmf.reqif10.datatypes.XhtmlContent;
import org.eclipse.rmf.reqif10.util.Reqif10Util;
import org.eclipse.swt.graphics.Color;
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 XhtmlContent) {
				stringValue = "XHTML NOT YET SUPPORTED";
			} 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.
	protected void initialColor(int row, int col) {
		if (agileGrid.isCellSelected(row, col)) {
			background = SWTResourceManager.getColor(223, 227, 237);
		}
	}
	
	@Override
	protected void drawGridLines(GC gc, Rectangle rect, int row, int col) {
		Color vBorderColor = COLOR_LINE_LIGHTGRAY;
		Color hBorderColor = COLOR_LINE_LIGHTGRAY;
		
		if (agileGrid instanceof ProrAgileGrid){
			if (((ProrAgileGrid)agileGrid).dndHoverCell != null && row == ((ProrAgileGrid)agileGrid).dndHoverCell.row && ((ProrAgileGrid)agileGrid).dndHoverCellMode == 0){
				hBorderColor = COLOR_LINE_DARKGRAY;
			}
		}
		
		if ((style & INDICATION_SELECTION_ROW) != 0) {
			vBorderColor = COLOR_BGROWSELECTION;
			hBorderColor = COLOR_BGROWSELECTION;
		}

		if ((agileGrid.getStyle() & SWTX.NOT_SHOW_GRID_LINE) == SWTX.NOT_SHOW_GRID_LINE) {
			vBorderColor = COLOR_BACKGROUND;
			hBorderColor = COLOR_BACKGROUND;
		}

		drawDefaultCellLine(gc, rect, vBorderColor, hBorderColor);
	}
	
	protected void drawCellContent(GC gc, Rectangle rect, int row, int col) {
		this.foreground = this.getDefaultForeground();
		this.background = this.getDefaultBackground();

		if (agileGrid instanceof ProrAgileGrid) {
			if (((ProrAgileGrid) agileGrid).dndHoverCell != null
					&& row == ((ProrAgileGrid) agileGrid).dndHoverCell.row
					&& ((ProrAgileGrid) agileGrid).dndHoverCellMode == 1) {
				this.background = COLOR_BGROWSELECTION;
			}
		}

		// initial color for current cell.
		initialColor(row, col);

		// Clear background.
		clearCellContentRect(gc, rect);

		// draw text and image in the given area.
		doDrawCellContent(gc, rect, row, col);
	}

}

Back to the top