1
28 package demo.layout.hierarchic;
29
30 import demo.layout.hierarchic.CellSpanLayoutDemo.CellColorManager;
31
32 import y.geom.YInsets;
33 import y.view.GenericNodeRealizer;
34 import y.view.MultiplexingNodeEditor;
35 import y.view.NodeRealizer;
36 import y.view.tabular.TableGroupNodeRealizer;
37 import y.view.tabular.TableGroupNodeRealizer.Column;
38 import y.view.tabular.TableGroupNodeRealizer.Row;
39 import y.view.tabular.TableGroupNodeRealizer.Table;
40 import y.view.tabular.TableNodePainter;
41 import y.view.tabular.TableSizeEditor;
42
43 import java.awt.Color;
44 import java.awt.Graphics2D;
45 import java.awt.geom.Rectangle2D;
46 import java.io.IOException;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutputStream;
49 import java.util.Map;
50
51
56 class CellSpanRealizerFactory {
57
58 private static final String GRID_CONFIGURATION = "GRID_CONFIGURATION";
59
60
61
64 private CellSpanRealizerFactory() {
65 }
66
67
68
71 static void initConfigurations() {
72 final TableNodePainter painter = TableNodePainter.newDefaultInstance();
74 painter.setSubPainter(TableNodePainter.PAINTER_COLUMN_FOREGROUND, null);
75 painter.setSubPainter(TableNodePainter.PAINTER_COLUMN_BACKGROUND, null);
76 painter.setSubPainter(TableNodePainter.PAINTER_ROW_FOREGROUND, new CellPainter(true));
77 painter.setSubPainter(TableNodePainter.PAINTER_ROW_BACKGROUND, new CellPainter(false));
78 painter.setSubPainter(TableNodePainter.PAINTER_TABLE_BACKGROUND, new Background());
79
80 final Map map = TableGroupNodeRealizer.createDefaultConfigurationMap();
81 map.put(GenericNodeRealizer.Painter.class, painter);
82 map.put(GenericNodeRealizer.UserDataHandler.class, new CellColorsDataHandler());
83
84 final TableSizeEditor tableSizeEditor = new TableSizeEditor();
89 tableSizeEditor.setResizePolicy(TableSizeEditor.RESIZE_POLICY_IGNORE_CONTENT);
90 final MultiplexingNodeEditor editor = new MultiplexingNodeEditor();
91 editor.addNodeEditor(tableSizeEditor);
92 map.put(GenericNodeRealizer.GenericMouseInputEditorProvider.class, editor);
93
94
95 GenericNodeRealizer.getFactory().addConfiguration(GRID_CONFIGURATION, map);
97 }
98
99
100
107 private static final class Background implements GenericNodeRealizer.Painter {
108
113 public void paint(final NodeRealizer dummy, final Graphics2D gfx) {
114 paintImpl(dummy, gfx);
115 }
116
117
122 public void paintSloppy(final NodeRealizer dummy, final Graphics2D gfx) {
123 paintImpl(dummy, gfx);
124 }
125
126
129 private void paintImpl(final NodeRealizer dummy, final Graphics2D gfx) {
130 final Color oldColor = gfx.getColor();
131
132 final Rectangle2D.Double bnds = new Rectangle2D.Double(
133 dummy.getX(), dummy.getY(), dummy.getWidth(), dummy.getHeight());
134
135 final Color fc2 = dummy.getFillColor2();
138 if (fc2 != null) {
139 gfx.setColor(fc2);
140 gfx.fill(bnds);
141 }
142
143 final Color fc = dummy.getFillColor();
145 if (fc != null) {
146 final Table table = TableNodePainter.getTable(dummy);
147
148 final YInsets cInsets = table.getColumn(0).getInsets();
156 final YInsets rInsets = table.getRow(0).getInsets();
157
158 bnds.setFrame(
159 bnds.getX() + rInsets.left,
160 bnds.getY() + cInsets.top,
161 bnds.getWidth() - rInsets.left - rInsets.right,
162 bnds.getHeight() - cInsets.top - cInsets.bottom);
163 gfx.setColor(fc);
164 gfx.fill(bnds);
165 }
166
167 gfx.setColor(oldColor);
168 }
169 }
170
171
176 private static final class CellPainter implements GenericNodeRealizer.Painter {
177
180 private final boolean foreground;
181
182
185 CellPainter( final boolean foreground ) {
186 this.foreground = foreground;
187 }
188
189
193 public void paint(final NodeRealizer dummy, final Graphics2D gfx) {
194 paintImpl(dummy, gfx);
195 }
196
197
201 public void paintSloppy(final NodeRealizer dummy, final Graphics2D gfx) {
202 paintImpl(dummy, gfx);
203 }
204
205
209 private void paintImpl(final NodeRealizer dummy, final Graphics2D gfx) {
210 final Row row = TableNodePainter.getRow(dummy);
211
212 double x = dummy.getX();
213 final double y = dummy.getY();
214 final double h = dummy.getHeight();
215 final Rectangle2D.Double bnds = new Rectangle2D.Double(
216 x, y, dummy.getWidth(), h);
217 final YInsets insets = row.getInsets();
218 x += insets.left;
219
220 final Color oldColor = gfx.getColor();
221
222 final Table table = TableNodePainter.getTable(dummy);
223 final Color fg = dummy.getLineColor();
224 final CellColorManager manager = CellColorManager.getInstance(table);
225 for (int i = 0, n = table.columnCount(); i < n; ++i) {
226 final Column col = table.getColumn(i);
227 final Color color = foreground ? fg : manager.getCellColor(col, row);
228 final double w = col.getWidth();
229 if (color != null) {
230 bnds.setFrame(x, y, w, h);
231 gfx.setColor(color);
232 if (foreground) {
233 gfx.draw(bnds);
234 } else {
235 gfx.fill(bnds);
236 }
237 }
238 x += w;
239 }
240
241 gfx.setColor(oldColor);
242 }
243 }
244
245
249 private static final class CellColorsDataHandler
250 implements GenericNodeRealizer.UserDataHandler {
251
255 public void storeUserData(
256 final NodeRealizer context, final Object userData, final ObjectOutputStream oos
257 ) throws IOException {
258 throw new UnsupportedOperationException();
259 }
260
261
265 public Object readUserData(
266 final NodeRealizer context, final ObjectInputStream ois
267 ) throws IOException {
268 throw new UnsupportedOperationException();
269 }
270
271
275 public Object copyUserData(
276 final NodeRealizer srcContext,
277 final Object srcData,
278 final NodeRealizer targetContext
279 ) {
280 return new CellColorManager((CellColorManager) srcData);
281 }
282 }
283 }
284