Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 870512756237c4a6392e13f9a91566583ccbec50 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package org.eclipse.papyrus.infra.nattable.painter;

import org.apache.commons.lang.StringUtils;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.decorator.CellPainterDecorator;
import org.eclipse.nebula.widgets.nattable.sort.config.DefaultSortConfiguration;
import org.eclipse.nebula.widgets.nattable.sort.painter.SortableHeaderTextPainter;
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.nebula.widgets.nattable.ui.util.CellEdgeEnum;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.manager.table.NattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.AbstractHeaderAxisConfiguration;
import org.eclipse.papyrus.infra.nattable.utils.HeaderAxisConfigurationManagementUtils;
import org.eclipse.papyrus.infra.nattable.utils.NattableConfigAttributes;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;


public class PapyrusSortableHeaderTextPainter extends SortableHeaderTextPainter {

	/**
	 * Creates the default {@link SortableHeaderTextPainter} that uses a {@link TextPainter} as base {@link ICellPainter} and decorate it with the
	 * {@link SortIconPainter} on the right
	 * edge of the cell.
	 */
	public PapyrusSortableHeaderTextPainter() {
		this(new TextPainter());
	}

	/**
	 * Creates a {@link SortableHeaderTextPainter} that uses the given {@link ICellPainter} as base {@link ICellPainter} and decorate it with the
	 * {@link SortIconPainter} on the right
	 * edge of the cell.
	 * 
	 * @param interiorPainter
	 *        the base {@link ICellPainter} to use
	 */
	public PapyrusSortableHeaderTextPainter(ICellPainter interiorPainter) {
		this(interiorPainter, CellEdgeEnum.RIGHT);
	}

	/**
	 * Creates a {@link SortableHeaderTextPainter} that uses the given {@link ICellPainter} as base {@link ICellPainter} and decorate it with the
	 * {@link SortIconPainter} on the specified
	 * edge of the cell.
	 * 
	 * @param interiorPainter
	 *        the base {@link ICellPainter} to use
	 * @param cellEdge
	 *        the edge of the cell on which the sort indicator decoration should be applied
	 */
	public PapyrusSortableHeaderTextPainter(ICellPainter interiorPainter, CellEdgeEnum cellEdge) {
		this(interiorPainter, cellEdge, new SortIconPainter(true));
	}

	/**
	 * Creates a {@link SortableHeaderTextPainter} that uses the given {@link ICellPainter} as base {@link ICellPainter} and decorate it with the
	 * given {@link ICellPainter} to use for sort
	 * related decoration on the specified edge of the cell.
	 * 
	 * @param interiorPainter
	 *        the base {@link ICellPainter} to use
	 * @param cellEdge
	 *        the edge of the cell on which the sort indicator decoration should be applied
	 * @param decoratorPainter
	 *        the {@link ICellPainter} that should be used to paint the sort related
	 *        decoration (by default the {@link SortIconPainter} will be used)
	 */
	public PapyrusSortableHeaderTextPainter(ICellPainter interiorPainter, CellEdgeEnum cellEdge, ICellPainter decoratorPainter) {
		setWrappedPainter(new CellPainterDecorator(interiorPainter, cellEdge, decoratorPainter));
	}

	/**
	 * Paints the triangular sort icon images.
	 * Adapted code from Nattable
	 */
	protected static class SortIconPainter extends SortableHeaderTextPainter.SortIconPainter {

		public SortIconPainter(boolean paintBg) {
			super(paintBg);
		}


		@Override
		public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
			int position = cell.getRowPosition();
			if(position == 0) {
				INattableModelManager manager = (NattableModelManager)configRegistry.getConfigAttribute(NattableConfigAttributes.NATTABLE_MODEL_MANAGER_CONFIG_ATTRIBUTE, DisplayMode.NORMAL, NattableConfigAttributes.NATTABLE_MODEL_MANAGER_ID);
				AbstractHeaderAxisConfiguration config = HeaderAxisConfigurationManagementUtils.getColumnAbstractHeaderAxisUsedInTable(manager.getTable());
				if(config != null && config.isDisplayIndex()) {
					return;
				}
			}

			Image image = getImage(cell, configRegistry);
			if(image != null) {
				Rectangle imageBounds = image.getBounds();
				imageBounds = new Rectangle(imageBounds.x, imageBounds.y, imageBounds.width, imageBounds.height);

				IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
				int y = bounds.y + CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, imageBounds.height);

				gc.drawImage(image, bounds.x + CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width), y);

				//a try to add the order of the column into the sort at the top of the arrow
				//				int x = bounds.x + CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width);
				//
				//				System.out.println("before =" + y);
				//				int yy = (y - imageBounds.height) - 2;//- 7;
				//				float halfHeight = ((float)getPreferredHeight(cell, gc, configRegistry)) / ((float)2);
				//				yy = (int)(y - (gc.getFont().getFontData()[0].height + 2));
				//				System.out.println("after =" + yy);
				//
				//				gc.drawText(Integer.toString(getSortSequence(cell)), x, yy); //$NON-NLS-1$
			}
		}

		@Override
		public int getPreferredHeight(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
			int width = super.getPreferredHeight(cell, gc, configRegistry);
			width += gc.getFont().getFontData()[0].height + 2;
			return width;
		}

		@Override
		public int getPreferredWidth(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
			return super.getPreferredWidth(cell, gc, configRegistry);
		}

		@Override
		protected Image getImage(ILayerCell cell, IConfigRegistry configRegistry) {

			Image icon = null;

			if(isSortedAscending(cell)) {
				icon = selectUpImage(getSortSequence(cell));
			} else if(isSortedDescending(cell)) {
				icon = selectDownImage(getSortSequence(cell));
			}
			//to change the color of the icon
			//			ImageData ideaImageData = icon.getImageData();
			//			int[] lineData = new int[ideaImageData.width];
			//			for(int y = 0; y < ideaImageData.height; y++) {
			//				ideaImageData.getPixels(0, y, ideaImageData.width, lineData, 0);
			//				// Analyze each pixel value in the line
			//				for(int x = 0; x < lineData.length; x++) {
			//					// Extract the red, green and blue component
			//					int pixelValue = lineData[x];
			//					ideaImageData.setPixel(x, y, ideaImageData.palette.getPixel(new RGB(0, 0, 250)));
			//				}
			//			};
			//			icon = new Image(Display.getDefault(), ideaImageData);


			return icon;
		}

		private boolean isSortedAscending(ILayerCell cell) {
			return cell.getConfigLabels().hasLabel(DefaultSortConfiguration.SORT_UP_CONFIG_TYPE);
		}

		private boolean isSortedDescending(ILayerCell cell) {
			return cell.getConfigLabels().hasLabel(DefaultSortConfiguration.SORT_DOWN_CONFIG_TYPE);
		}

		private int getSortSequence(ILayerCell cell) {
			int sortSeq = 0;

			for(String configLabel : cell.getConfigLabels().getLabels()) {
				if(configLabel.startsWith(DefaultSortConfiguration.SORT_SEQ_CONFIG_TYPE)) {
					String[] tokens = StringUtils.split(configLabel, "_"); //$NON-NLS-1$
					sortSeq = Integer.valueOf(tokens[tokens.length - 1]).intValue();
				}
			}
			return sortSeq;
		}

		private Image selectUpImage(int sortSequence) {
			//			return GUIHelper.getImage("up_0"); //$NON-NLS-1$
			switch(sortSequence) {
			case 0:
				return GUIHelper.getImage("up_0"); //$NON-NLS-1$
			case 1:
				return GUIHelper.getImage("up_1"); //$NON-NLS-1$
			case 2:
				return GUIHelper.getImage("up_2"); //$NON-NLS-1$
			default:
				return GUIHelper.getImage("up_2"); //$NON-NLS-1$
			}
		}

		private Image selectDownImage(int sortSequence) {
			//			return GUIHelper.getImage("down_0"); //$NON-NLS-1$
			switch(sortSequence) {
			case 0:
				return GUIHelper.getImage("down_0"); //$NON-NLS-1$
			case 1:
				return GUIHelper.getImage("down_1"); //$NON-NLS-1$
			case 2:
				return GUIHelper.getImage("down_2"); //$NON-NLS-1$
			default:
				return GUIHelper.getImage("down_2"); //$NON-NLS-1$
			}
		}


	}

}

Back to the top