Skip to main content
summaryrefslogtreecommitdiffstats
blob: fd780498ad9e78a24fa852de1bc8195baea9b14a (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Juan Cadavid (CEA LIST) juan.cadavid@cea.fr - Override of the paintCell() method
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.configuration;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter;
import org.eclipse.nebula.widgets.nattable.resize.command.RowResizeCommand;
import org.eclipse.nebula.widgets.nattable.style.CellStyleUtil;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.style.IStyle;
import org.eclipse.papyrus.infra.nattable.utils.Constants;
import org.eclipse.papyrus.infra.nattable.utils.ILabelProviderContextElement;
import org.eclipse.papyrus.infra.nattable.utils.LabelProviderCellContextElement;
import org.eclipse.papyrus.infra.nattable.utils.NattableConfigAttributes;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;

/**
 * Custom CellPainter to define the LabelProvider to use
 * 
 * @author Vincent Lorenzo
 * 
 */
//FIXME : we should use the TextPainter itself, now with the GenericDisplayConverter, it should works fine
public class CustomizedCellPainter extends TextPainter {

	/**
	 * 
	 * Constructor. We're overriding it to always set word-wrapping for our cells.
	 * 
	 */
	public CustomizedCellPainter() {
		super(false, true);//with (true,true), automatic newLine when the text is too long to be displayed.
	}

	/**
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.painter.cell.AbstractTextPainter#convertDataType(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell,
	 *      org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)
	 * 
	 * @param cell
	 * @param configRegistry
	 * @return
	 */
	@Override
	protected String convertDataType(final ILayerCell cell, final IConfigRegistry configRegistry) {
		final LabelProviderService serv = configRegistry.getConfigAttribute(NattableConfigAttributes.LABEL_PROVIDER_SERVICE_CONFIG_ATTRIBUTE, DisplayMode.NORMAL, NattableConfigAttributes.LABEL_PROVIDER_SERVICE_ID);
		final ILabelProviderContextElement contextElement = new LabelProviderCellContextElement(cell, configRegistry);
		final ILabelProvider provider = serv.getLabelProvider(Constants.TABLE_LABEL_PROVIDER_CONTEXT, contextElement);
		String str = provider.getText(contextElement);
		if(str == null) {
			str = ""; //$NON-NLS-1$
		}
		return str;
	}

	/**
	 * Overridden to show, additionally to the contents of a cell, a vertical arrow pointing down in case there are masked lines
	 * 
	 * @see org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter#paintCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell,
	 *      org.eclipse.swt.graphics.GC, org.eclipse.swt.graphics.Rectangle, org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)
	 * 
	 * @param cell
	 * @param gc
	 * @param rectangle
	 * @param configRegistry
	 */
	@Override
	public void paintCell(ILayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry) {
		super.paintCell(cell, gc, rectangle, configRegistry);
		IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
		int fontHeight = gc.getFontMetrics().getHeight();
		String text = convertDataType(cell, configRegistry);
		text = getTextToDisplay(cell, gc, rectangle.width, text);

		int numberOfNewLines = getNumberOfNewLines(text);
		//we're extending the row height (only if word wrapping is enabled)
		int contentHeight = (fontHeight * numberOfNewLines) + (spacing * 2);
		int contentToCellDiff = (cell.getBounds().height - rectangle.height);

		if(performRowResize(contentHeight, rectangle)) {
			ILayer layer = cell.getLayer();
			layer.doCommand(new RowResizeCommand(layer, cell.getRowPosition(), contentHeight + contentToCellDiff));
		}
		if(numberOfNewLines > 1) {

			int yStartPos = rectangle.y + CellStyleUtil.getVerticalAlignmentPadding(cellStyle, rectangle, contentHeight);
			String[] lines = text.split("\n"); //$NON-NLS-1$
			for(String line : lines) {
				int lineContentWidth = Math.min(getLengthFromCache(gc, line), rectangle.width);

				Image im = org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImage("org.eclipse.papyrus.infra.nattable", "/icons/arrow_down_end.png"); //$NON-NLS-1$ //$NON-NLS-2$
				gc.drawText(line, rectangle.x + CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, rectangle, lineContentWidth) + spacing, yStartPos + spacing, SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER);

				//We test if, given the current cell size and text position, we should display the down pointing arrow.
				if(contentHeight > rectangle.height && yStartPos + fontHeight > rectangle.height + rectangle.y) {
					int yDownRowIcon = rectangle.y + rectangle.height - im.getBounds().height;
					int xDownRowIcon = rectangle.x + rectangle.width - im.getBounds().width;
					gc.drawImage(im, xDownRowIcon, yDownRowIcon);
				}

				//after every line calculate the y start pos new
				yStartPos += fontHeight;
			}
		}


	}

}

Back to the top