Skip to content

Package: ECPFocusCellDrawHighlighter$1

ECPFocusCellDrawHighlighter$1

nameinstructionbranchcomplexitylinemethod
afterEditorActivated(ColumnViewerEditorActivationEvent)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
afterEditorDeactivated(ColumnViewerEditorDeactivationEvent)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
beforeEditorActivated(ColumnViewerEditorActivationEvent)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
beforeEditorDeactivated(ColumnViewerEditorDeactivationEvent)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Edgar Mueller - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.edit.internal.swt.controls;
15:
16: import org.eclipse.jface.viewers.CellLabelProvider;
17: import org.eclipse.jface.viewers.ColumnViewer;
18: import org.eclipse.jface.viewers.ColumnViewerEditor;
19: import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
20: import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener;
21: import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
22: import org.eclipse.jface.viewers.FocusCellHighlighter;
23: import org.eclipse.jface.viewers.IColorProvider;
24: import org.eclipse.jface.viewers.ViewerCell;
25: import org.eclipse.swt.SWT;
26: import org.eclipse.swt.graphics.Color;
27: import org.eclipse.swt.widgets.Event;
28: import org.eclipse.swt.widgets.Table;
29:
30: /**
31: * A concrete implementation of {@link FocusCellHighlighter} using by setting
32: * the control into owner draw mode and highlighting the currently selected
33: * cell. To make the use this class you should create the control with the {@link SWT#FULL_SELECTION} bit set
34: *
35: * <p>
36: * <strong>RAP specific:</strong> despite its name that was taken as-is from JFace/RCP to ease single-sourcing, this
37: * class does not use custom drawing to highlight the focused cell. <br />
38: * The focused cell is displayed using the selection colors of the system.
39: * </p>
40: *
41: * This class can be subclassed to configure how the coloring of the selected
42: * cell.
43: *
44: * @since 1.2
45: *
46: */
47: public class ECPFocusCellDrawHighlighter extends FocusCellHighlighter {
48:
49:         private final ColumnViewer viewer;
50:
51:         /**
52:          * Create a new instance which can be passed to a.
53:          *
54:          * @param viewer
55:          * the viewer
56:          */
57:         public ECPFocusCellDrawHighlighter(ColumnViewer viewer) {
58:                 super(viewer);
59:                 this.viewer = viewer;
60:                 // hookListener(viewer);
61:                 // RAP [if]
62:                 viewer.getControl().setData(Table.class.getName() + "#alwaysHideSelection", Boolean.TRUE); //$NON-NLS-1$
63:         }
64:
65:         private void markFocusedCell(Event event, ViewerCell cell) {
66:                 Color background = cell.getControl().isFocusControl() ? getSelectedCellBackgroundColor(cell)
67:                         : getSelectedCellBackgroundColorNoFocus(cell);
68:                 Color foreground = cell.getControl().isFocusControl() ? getSelectedCellForegroundColor(cell)
69:                         : getSelectedCellForegroundColorNoFocus(cell);
70:
71:                 if (foreground != null || background != null || onlyTextHighlighting(cell)) {
72:                         // GC gc = event.gc;
73:
74:                         if (background == null) {
75:                                 background = cell.getItem().getDisplay().getSystemColor(
76:                                         SWT.COLOR_LIST_SELECTION);
77:                         }
78:
79:                         if (foreground == null) {
80:                                 foreground = cell.getItem().getDisplay().getSystemColor(
81:                                         SWT.COLOR_LIST_SELECTION_TEXT);
82:                         }
83:
84:                         // gc.setBackground(background);
85:                         // gc.setForeground(foreground);
86:                         //
87:                         // if (onlyTextHighlighting(cell)) {
88:                         // Rectangle area = event.getBounds();
89:                         // Rectangle rect = cell.getTextBounds();
90:                         // if( rect != null ) {
91:                         // area.x = rect.x;
92:                         // }
93:                         // gc.fillRectangle(area);
94:                         // } else {
95:                         // gc.fillRectangle(event.getBounds());
96:                         // }
97:                         cell.setBackground(background);
98:                         cell.setForeground(foreground);
99:
100:                         // event.detail &= ~SWT.SELECTED;
101:                 }
102:         }
103:
104:         private void removeSelectionInformation(Event event, ViewerCell cell) {
105:                 // GC gc = event.gc;
106:                 // gc.setBackground(cell.getViewerRow().getBackground(
107:                 // cell.getColumnIndex()));
108:                 // gc.setForeground(cell.getViewerRow().getForeground(
109:                 // cell.getColumnIndex()));
110:                 // gc.fillRectangle(cell.getBounds());
111:                 // event.detail &= ~SWT.SELECTED;
112:                 Color cellBackground = null;
113:                 Color cellForeground = null;
114:                 final CellLabelProvider labelProvider = viewer.getLabelProvider(cell.getColumnIndex());
115:                 if (labelProvider instanceof IColorProvider) {
116:                         final IColorProvider columnLabelProvider = (IColorProvider) labelProvider;
117:                         cellBackground = columnLabelProvider.getBackground(cell.getElement());
118:                         cellForeground = columnLabelProvider.getForeground(cell.getElement());
119:                 }
120:                 cell.setBackground(cellBackground);
121:                 cell.setForeground(cellForeground);
122:         }
123:
124:         // private void hookListener(final ColumnViewer viewer) {
125:         //
126:         // Listener listener = new Listener() {
127:         //
128:         // public void handleEvent(Event event) {
129:         // // if ((event.detail & SWT.SELECTED) > 0) {
130:         // ViewerCell focusCell = getFocusCell();
131:         // ViewerRow row = viewer.getViewerRowFromItem(event.item);
132:         //
133:         // Assert
134:         // .isNotNull(row,
135:         // "Internal structure invalid. Item without associated row is not possible."); //$NON-NLS-1$
136:         //
137:         // // ViewerCell cell = row.getCell(event.index);
138:         // //
139:         // // if (focusCell == null || !cell.equals(focusCell)) {
140:         // // removeSelectionInformation(event, cell);
141:         // // } else {
142:         // // markFocusedCell(event, cell);
143:         // // }
144:         //
145:         // focusCellChanged( focusCell );
146:         // }
147:         //
148:         // };
149:         // // viewer.getControl().addListener(SWT.EraseItem, listener);
150:         // viewer.getControl().addListener(SWT.KeyDown, listener);
151:         // viewer.getControl().addListener(SWT.MouseDown, listener);
152:         // }
153:
154:         /**
155:          * The color to use when rendering the background of the selected cell when
156:          * the control has the input focus.
157:          *
158:          * @param cell
159:          * the cell which is colored
160:          * @return the color or <code>null</code> to use the default
161:          */
162:         protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
163:                 return null;
164:         }
165:
166:         /**
167:          * The color to use when rendering the foreground (=text) of the selected
168:          * cell when the control has the input focus.
169:          *
170:          * @param cell
171:          * the cell which is colored
172:          * @return the color or <code>null</code> to use the default
173:          */
174:         protected Color getSelectedCellForegroundColor(ViewerCell cell) {
175:                 return null;
176:         }
177:
178:         /**
179:          * The color to use when rendering the foreground (=text) of the selected
180:          * cell when the control has <b>no</b> input focus.
181:          *
182:          * @param cell
183:          * the cell which is colored
184:          * @return the color or <code>null</code> to use the same used when
185:          * control has focus
186:          */
187:         protected Color getSelectedCellForegroundColorNoFocus(ViewerCell cell) {
188:                 return null;
189:         }
190:
191:         /**
192:          * The color to use when rendering the background of the selected cell when
193:          * the control has <b>no</b> input focus.
194:          *
195:          * @param cell
196:          * the cell which is colored
197:          * @return the color or <code>null</code> to use the same used when
198:          * control has focus
199:          */
200:         protected Color getSelectedCellBackgroundColorNoFocus(ViewerCell cell) {
201:                 return null;
202:         }
203:
204:         /**
205:          * Controls whether the whole cell or only the text-area is highlighted.
206:          *
207:          * @param cell
208:          * the cell which is highlighted
209:          * @return <code>true</code> if only the text area should be highlighted
210:          */
211:         protected boolean onlyTextHighlighting(ViewerCell cell) {
212:                 // return false;
213:                 return true;
214:         }
215:
216:         @Override
217:         protected void init() {
218:                 final ColumnViewerEditorActivationListener listener = new ColumnViewerEditorActivationListener() {
219:                         @Override
220:                         public void afterEditorActivated(ColumnViewerEditorActivationEvent e) {
221:                         }
222:
223:                         @Override
224:                         public void afterEditorDeactivated(ColumnViewerEditorDeactivationEvent e) {
225:                                 focusCellChanged(getFocusCell(), null);
226:                         }
227:
228:                         @Override
229:                         public void beforeEditorActivated(ColumnViewerEditorActivationEvent e) {
230:                         }
231:
232:                         @Override
233:                         public void beforeEditorDeactivated(ColumnViewerEditorDeactivationEvent e) {
234:                         }
235:                 };
236:                 final ColumnViewerEditor editor = viewer.getColumnViewerEditor();
237:                 editor.addEditorActivationListener(listener);
238:         }
239:
240:         @Override
241:         protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
242:                 // super.focusCellChanged(newCell, oldCell);
243:                 //
244:                 // // Redraw new area
245:                 // if (newCell != null) {
246:                 // Rectangle rect = newCell.getBounds();
247:                 // int x = newCell.getColumnIndex() == 0 ? 0 : rect.x;
248:                 // int width = newCell.getColumnIndex() == 0 ? rect.x + rect.width
249:                 // : rect.width;
250:                 // // 1 is a fix for Linux-GTK
251:                 // newCell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
252:                 // true);
253:                 // }
254:                 //
255:                 // if (oldCell != null) {
256:                 // Rectangle rect = oldCell.getBounds();
257:                 // int x = oldCell.getColumnIndex() == 0 ? 0 : rect.x;
258:                 // int width = oldCell.getColumnIndex() == 0 ? rect.x + rect.width
259:                 // : rect.width;
260:                 // // 1 is a fix for Linux-GTK
261:                 // oldCell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
262:                 // true);
263:                 // }
264:                 if (oldCell != null) {
265:                         removeSelectionInformation(null, oldCell);
266:                 }
267:                 if (newCell != null) {
268:                         markFocusedCell(null, newCell);
269:                 }
270:         }
271: }