📄 defaultcellrenderer.java
字号:
/*
* Copyright (C) 2004 by Friederich Kupzog Elektronik & Software
* 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
Authors:
Friederich Kupzog, fkmk@kupzog.de, www.kupzog.de/fkmk
Lorenz Maierhofer, lorenz.maierhofer@logicmindguide.com
*/
package de.kupzog.ktable.renderers;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import de.kupzog.ktable.KTableCellRenderer;
import de.kupzog.ktable.KTableModel;
import de.kupzog.ktable.SWTX;
/**
* Class that provides additional facilities commonly used when writing custom renderers.
*
* @author Lorenz Maierhofer <lorenz.maierhofer@logicmindguide.com>
*/
public class DefaultCellRenderer implements KTableCellRenderer {
// default colors:
public Color COLOR_TEXT = Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND);
public Color COLOR_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
public static Color COLOR_LINE_LIGHTGRAY = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
public static Color COLOR_LINE_DARKGRAY = Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
public static Color COLOR_BGFOCUS = SWTX.getColor(SWTX.COLOR_BGFOCUS);
public static Color COLOR_COMMENTSIGN = Display.getDefault().getSystemColor(SWT.COLOR_DARK_BLUE);
public static Color COLOR_FIXEDHIGHLIGHT = SWTX.getColor(SWTX.COLOR_FIXEDHIGHLIGHT);
public static Color COLOR_BGROWFOCUS = Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION);
public static Color COLOR_FGROWFOCUS = Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
/**
* Makes a button-like cell. <p>
* Used in: <br>
* - FixedCellRenderer
* */
public static final int STYLE_PUSH = 0;
/**
* Makes a flat looking cell. <p>
* Used in: <br>
* - FixedCellRenderer
*/
public static final int STYLE_FLAT = 1<<2;
/**
* Shows a sort indicator in the fixed cell if sorting is active for the column.<p>
* Has only an effect if the tablemodel is an instanceof KTableSortModel.<p>
* Used in: <br>
* - FixedCellRenderer
*/
public static final int INDICATION_SORT = 1<<3;
/**
* Color fixed cells with focus in a different color.<p>
* Has only an effect on fixed cells when
* <code>KTable.setHighlightSelectionInHeader(true)</code>
* was set.<p>
* Used in: <br>
* - CheckableCellRenderer<br>
* - FixedCellRenderer<br>
* - TextCellRenderer<br>
*/
public static final int INDICATION_FOCUS = 1<<4;
/**
* Color fixed cells with focus in a different color.<p>
* Has only an effect when <code>KTable.setHighlightSelectionInHeader(true)</code>
* was set.<p>
* Used in: <br>
* - CheckableCellRenderer<br>
* - FixedCellRenderer<br>
* - TextCellRenderer<br>
*/
public static final int INDICATION_FOCUS_ROW = 1<<5;
/**
* Make the header column show a button-pressed behavior when clicked.<p>
* Used in: <br>
* - FixedCellRenderer (Has only an effect when STYLE_PUSH is set.)<br>
* - CheckableCellRenderer<br>
*/
public static final int INDICATION_CLICKED = 1<<6;
/**
* Draws a little triangle in the upper right corner of the cell as an
* indication that additional information is available.<p>
* Used in: <br>
* - CheckableCellRenderer<br>
* - TextCellRenderer<br>
*/
public static final int INDICATION_COMMENT = 1<<7;
/**
* Draws a gradient in the figure representing the cell content.
* Used by: <br>
* - BarDiagramCellRenderer<br>
*/
public static final int INDICATION_GRADIENT = 1<<8;
/**
* The display to use when painting.
*/
protected final Display m_Display = Display.getCurrent();
/**
* Holds the default instance for this renderer.
*/
protected int m_Style = 0;
protected int m_alignment = SWTX.ALIGN_HORIZONTAL_LEFT | SWTX.ALIGN_VERTICAL_CENTER;
/**
* Holds the currently set bg and fg colors.
*/
protected Color m_bgColor=null, m_fgColor=null;
protected Font m_font = null;
private Font m_GCfont, m_TMPfont = null;
// the default cell renderer for fixed cells:
protected static final FixedCellRenderer m_FixedRenderer = new FixedCellRenderer(STYLE_FLAT);
protected static final TextCellRenderer m_TextRenderer = new TextCellRenderer(INDICATION_FOCUS);
/**
* The constructor that sets the style bits given.
* The default cellrenderer ignores all style bits.
* See subclasses for their honored style bits.
*/
public DefaultCellRenderer(int style) {
m_Style |= style;
}
/**
* Overwrites the style bits with the given one.
* @see getStyle() for accessing the style bits.
* @param style The styles to AND with the current style bits.
*/
public void setStyle(int style) {
m_Style = style;
}
/**
* @return Returns the currently set style.
*/
public int getStyle() {
return m_Style;
}
/* (non-Javadoc)
* @see de.kupzog.ktable.KTableCellRenderer#getOptimalWidth(org.eclipse.swt.graphics.GC, int, int, java.lang.Object, boolean)
*/
public int getOptimalWidth(GC gc, int col, int row, Object content, boolean fixed, KTableModel model) {
applyFont(gc);
int result = SWTX.getCachedStringExtent(gc, content.toString()).x + 8;
resetFont(gc);
return result;
}
/**
* A default implementation that paints cells in a way that is more or less
* Excel-like. Only the cell with focus looks very different.
* @see de.kupzog.ktable.KTableCellRenderer#drawCell(GC, Rectangle, int, int, Object, boolean, boolean, boolean, KTableModel)
*/
public void drawCell(GC gc, Rectangle rect, int col, int row, Object content,
boolean focus, boolean fixed, boolean clicked, KTableModel model) {
if (fixed) {
m_FixedRenderer.drawCell(gc, rect, col, row, content,
focus, fixed, clicked, model);
} else {
m_TextRenderer.drawCell(gc, rect, col, row, content,
focus, fixed, clicked, model);
}
}
/**
* Draws the actual cell content (text & image).
* @param gc The GC to use when painting.
* @param rect The cell area.
* @param text The text to draw.
* @param textColor The text color.
* @param backColor The background color to use.
*/
protected void drawCellContent(GC gc, Rectangle rect, String text, Image img,
Color textColor, Color backColor) {
// clear background and paint content:
gc.setBackground(backColor);
gc.setForeground(textColor);
gc.fillRectangle(rect);
SWTX.drawTextImage(gc, text, getAlignment(),
img, getAlignment(), rect.x + 3, rect.y+2,
rect.width - 6, rect.height-4);
}
/**
* Draws the actual cell content (text & image).
* @param gc The GC to use when painting.
* @param rect The cell area.
* @param text The text to draw.
* @param textColor The text color.
* @param backColor The background color to use.
*/
protected void drawVerticalCellContent(GC gc, Rectangle rect, String text, Image img,
Color textColor, Color backColor) {
if (rect.height<=0) rect.height=1;
if (rect.width<=0) rect.width = 1;
Image vImg = new Image(Display.getCurrent(), rect.height, rect.width);
GC gcImg = new GC(vImg);
applyFont(gcImg);
// clear background and paint content:
gcImg.setBackground(backColor);
gc.setBackground(backColor);
gcImg.setForeground(textColor);
gc.setForeground(textColor);
gcImg.fillRectangle(vImg.getBounds());
int alignment = mirrorAlignment();
SWTX.drawTextImage(gcImg, text, alignment,
img, alignment, 3, 3,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -