]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEIconPainter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.nattable / src / org / simantics / browsing / ui / nattable / GEIconPainter.java
1 package org.simantics.browsing.ui.nattable;
2
3 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
4 import org.eclipse.nebula.widgets.nattable.layer.ILayer;
5 import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
6 import org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundPainter;
7 import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
8 import org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter;
9 import org.eclipse.nebula.widgets.nattable.resize.command.ColumnResizeCommand;
10 import org.eclipse.nebula.widgets.nattable.resize.command.RowResizeCommand;
11 import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
12 import org.eclipse.nebula.widgets.nattable.style.CellStyleUtil;
13 import org.eclipse.nebula.widgets.nattable.style.IStyle;
14 import org.eclipse.swt.graphics.GC;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Rectangle;
17
18 /**
19  * Modified org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter, which does not allow setting wrapped painter
20  * 
21  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
22  *
23  */
24 public class GEIconPainter extends BackgroundPainter {
25         
26     protected boolean calculateByWidth;
27     protected boolean calculateByHeight;
28         
29         public GEIconPainter(ICellPainter painter) {
30                 super(painter);
31         }
32         
33          @Override
34             public int getPreferredWidth(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
35                 Image image = getImage(cell, configRegistry);
36                 if (image != null) {
37                     return image.getBounds().width;
38                 } else {
39                     return 0;
40                 }
41             }
42
43             @Override
44             public int getPreferredHeight(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
45                 Image image = getImage(cell, configRegistry);
46                 if (image != null) {
47                     return image.getBounds().height;
48                 } else {
49                     return 0;
50                 }
51             }
52
53             @Override
54             public ICellPainter getCellPainterAt(int x, int y, ILayerCell cell, GC gc,
55                     Rectangle bounds, IConfigRegistry configRegistry) {
56
57                 Image image = getImage(cell, configRegistry);
58                 if (image != null) {
59                     Rectangle imageBounds = image.getBounds();
60                     IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
61                     int x0 = bounds.x
62                             + CellStyleUtil.getHorizontalAlignmentPadding(
63                                     cellStyle, bounds, imageBounds.width);
64                     int y0 = bounds.y
65                             + CellStyleUtil.getVerticalAlignmentPadding(
66                                     cellStyle, bounds, imageBounds.height);
67                     if (x >= x0 && x < x0 + imageBounds.width
68                             && y >= y0 && y < y0 + imageBounds.height) {
69                         return super.getCellPainterAt(x, y, cell, gc, bounds, configRegistry);
70                     }
71                 }
72                 return null;
73             }
74
75             @Override
76             public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
77                
78                 
79                 Image image = getImage(cell, configRegistry);
80                 if (image != null) {
81                     Rectangle imageBounds = image.getBounds();
82                     IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
83
84                     int contentHeight = imageBounds.height;
85                     if (this.calculateByHeight && (contentHeight > bounds.height)) {
86                         int contentToCellDiff = (cell.getBounds().height - bounds.height);
87                         ILayer layer = cell.getLayer();
88                         layer.doCommand(new RowResizeCommand(
89                                 layer,
90                                 cell.getRowPosition(),
91                                 contentHeight + contentToCellDiff));
92                     }
93
94                     int contentWidth = imageBounds.width;
95                     if (this.calculateByWidth && (contentWidth > bounds.width)) {
96                         int contentToCellDiff = (cell.getBounds().width - bounds.width);
97                         ILayer layer = cell.getLayer();
98                         layer.doCommand(new ColumnResizeCommand(
99                                 layer,
100                                 cell.getColumnPosition(),
101                                 contentWidth + contentToCellDiff));
102                     }
103                     int px = CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width);
104                     int py = CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, imageBounds.height);
105                     Rectangle b = new Rectangle(bounds.x + px + imageBounds.width, bounds.y, bounds.width - px - imageBounds.width, bounds.height);
106                     super.paintCell(cell, gc, b, configRegistry);
107                     gc.drawImage(
108                             image,
109                             bounds.x + px,
110                             bounds.y + py);
111                 } else {
112                         super.paintCell(cell, gc, bounds, configRegistry);
113                 }
114             }
115             
116 //          @Override
117 //          public Rectangle getWrappedPainterBounds(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
118 //              Image image = getImage(cell, configRegistry);
119 //              if (image != null) {
120 //                      Rectangle imageBounds = image.getBounds();
121 //                       IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
122 //                      int px = CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width);
123 //                      int py = CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, imageBounds.height);
124 //                      Rectangle b = new Rectangle(bounds.x + px + imageBounds.width, bounds.y, bounds.width - px - imageBounds.width, bounds.height);
125 //                      return b;
126 //                          
127 //              }
128 //              return super.getWrappedPainterBounds(cell, gc, bounds, configRegistry);
129 //          }
130
131             /**
132              *
133              * @param cell
134              *            The {@link ILayerCell} for which this {@link ImagePainter} is
135              *            called.
136              * @param configRegistry
137              *            The current {@link IConfigRegistry} to retrieve the cell style
138              *            information from.
139              * @return The {@link Image} that should be painted by this
140              *         {@link ImagePainter}.
141              */
142             protected Image getImage(ILayerCell cell, IConfigRegistry configRegistry) {
143                 return CellStyleUtil.getCellStyle(cell, configRegistry).getAttributeValue(CellStyleAttributes.IMAGE);
144             }
145
146             /**
147              * @return <code>true</code> if this {@link ImagePainter} is resizing the
148              *         cell width to show the whole configured image, <code>false</code>
149              *         if the cell width is not touched by this painter.
150              */
151             public boolean isCalculateByWidth() {
152                 return this.calculateByWidth;
153             }
154
155             /**
156              * Configure whether the {@link ImagePainter} should calculate the cell
157              * dimensions by containing image width. This means the <b>width</b> of the
158              * cell is calculated by image width.
159              *
160              * @param calculateByWidth
161              *            <code>true</code> to calculate and modify the cell dimension
162              *            according to the image width, <code>false</code> to not
163              *            modifying the cell dimensions.
164              */
165             public void setCalculateByWidth(boolean calculateByWidth) {
166                 this.calculateByWidth = calculateByWidth;
167             }
168
169             /**
170              * @return <code>true</code> if this {@link ImagePainter} is resizing the
171              *         cell height to show the whole configured image,
172              *         <code>false</code> if the cell height is not touched by this
173              *         painter.
174              */
175             public boolean isCalculateByHeight() {
176                 return this.calculateByHeight;
177             }
178
179             /**
180              * Configure whether the {@link ImagePainter} should calculate the cell
181              * dimensions by containing image height. This means the <b>height</b> of
182              * the cell is calculated by image height.
183              *
184              * @param calculateByHeight
185              *            <code>true</code> to calculate and modify the cell dimension
186              *            according to the image height, <code>false</code> to not
187              *            modifying the cell dimensions.
188              */
189             public void setCalculateByHeight(boolean calculateByHeight) {
190                 this.calculateByHeight = calculateByHeight;
191             }
192
193 }