下面列出了怎么用javax.swing.CellRendererPane的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Constructs a new graphics export canvas.
*/
public mxGraphicsCanvas2D(Graphics2D g)
{
setGraphics(g);
state.g = g;
// Initializes the cell renderer pane for drawing HTML markup
try
{
rendererPane = new CellRendererPane();
}
catch (Exception e)
{
log.log(Level.WARNING, "Failed to initialize renderer pane", e);
}
}
private void initializeComponents(Component comp) {
if (comp instanceof CellRendererPane) {
return;
}
if (comp instanceof Container) {
Container c = (Container) comp;
c.addContainerListener(this);
Component comps[] = c.getComponents();
for (Component element : comps) {
initializeComponents(element);
}
}
DropTarget primaryDropTarget = comp.getDropTarget();
if (primaryDropTarget != null) {
new CascadedDropTarget(comp, primaryDropTarget, globalDropTarget);
}
}
private void deinitializeComponents(Component comp) {
if (comp instanceof CellRendererPane) {
return;
}
if (comp instanceof Container) {
Container c = (Container) comp;
c.removeContainerListener(this);
Component comps[] = c.getComponents();
for (Component element : comps) {
deinitializeComponents(element);
}
}
DropTarget dt = comp.getDropTarget();
if (dt instanceof CascadedDropTarget) {
CascadedDropTarget target = (CascadedDropTarget) dt;
DropTarget newTarget = target.removeDropTarget(globalDropTarget);
comp.setDropTarget(newTarget);
}
}
@Override
public void scrollRectToVisible(Rectangle contentRect) {
Container parent;
int dx = getX(), dy = getY();
for (parent = getParent();
!(parent == null) &&
!(parent instanceof JComponent) &&
!(parent instanceof CellRendererPane);
parent = parent.getParent()) {
Rectangle bounds = parent.getBounds();
dx += bounds.x;
dy += bounds.y;
}
if (!(parent == null) && !(parent instanceof CellRendererPane)) {
contentRect.x += dx;
contentRect.y += dy;
((JComponent) parent).scrollRectToVisible(contentRect);
contentRect.x -= dx;
contentRect.y -= dy;
}
}
@Override
public void installUI(JComponent component) {
grid = (JGrid) component;
rendererPane = new CellRendererPane();
grid.add(rendererPane);
// gridHeader = (JGrid) component;
component.setOpaque(false);
LookAndFeel.installColorsAndFont(
component,
"TableHeader.background",
"TableHeader.foreground",
"TableHeader.font");
installDefaults();
installListeners();
installKeyboardActions();
}
/**
* Constructs a new graphics canvas for the given graphics object.
*/
public mxGraphics2DCanvas(Graphics2D g)
{
this.g = g;
// Initializes the cell renderer pane for drawing HTML markup
try
{
rendererPane = new CellRendererPane();
}
catch (Exception e)
{
log.log(Level.WARNING, "Failed to initialize renderer pane", e);
}
}
private void paintCells(List<DomainFileInfo> domainFileInfos, CellRendererPane rendererPane,
Graphics g) {
TableColumnModel cm = table.getColumnModel();
int columnMargin = cm.getColumnMargin();
Rectangle clip = g.getClipBounds();
int yOffset = clip.y;
int rowCount = domainFileInfos.size();
int columnCount = table.getColumnCount();
int modelRow = model.getRowIndex(domainFileInfos.get(0));
Rectangle cellRect = table.getCellRect(modelRow, 0, false);
int startYOffset = cellRect.y;
TableColumn aColumn;
int columnWidth;
for (int row = 0; row < rowCount; row++) {
if (clip.y + clip.height < yOffset - startYOffset) {
return; // no need to paint past the end of our visible area
}
modelRow = model.getRowIndex(domainFileInfos.get(row));
cellRect = table.getCellRect(modelRow, 0, false);
cellRect.y -= startYOffset; // paint the row at the top of the graphics, not where it really lives
yOffset += cellRect.height;
for (int column = 0; column < columnCount; column++) {
aColumn = cm.getColumn(column);
columnWidth = aColumn.getWidth();
cellRect.width = columnWidth - columnMargin;
paintCell(rendererPane, g, cellRect, modelRow, column);
cellRect.x += columnWidth;
}
}
}
private void paintCell(CellRendererPane rendererPane, Graphics g, Rectangle cellRect, int row,
int column) {
TableCellRenderer tableRenderer = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(tableRenderer, row, column);
rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y, cellRect.width,
cellRect.height, true);
}
/** Creates a new instance of DefaultOutlineTreeCellRenderer */
public DefaultOutlineCellRenderer() {
theCheckBox = createCheckBox();
// In order to paint the check-box correctly, following condition must be true:
// SwingUtilities.getAncestorOfClass(CellRendererPane.class, theCheckBox) != null
// (See e.g.: paintSkin() method in com/sun/java/swing/plaf/windows/XPStyle.java)
fakeCellRendererPane = new CellRendererPane();
fakeCellRendererPane.add(theCheckBox);
}
/**
* Forwards the {@code scrollRectToVisible()} message to the
* {@code JComponent}'s parent. Components that can service
* the request, such as {@code JViewport},
* override this method and perform the scrolling.
*
* @param aRect the visible {@code Rectangle}
* @see javax.swing.JViewport
*/
@Override
public void scrollRectToVisible(Rectangle aRect)
{
Container parent;
int dx = getX();
int dy = getY();
for (parent = getParent(); (parent != null) && !(parent instanceof JComponent)
&& !(parent instanceof CellRendererPane); parent = parent.getParent())
{
final Rectangle bounds = parent.getBounds();
dx += bounds.x;
dy += bounds.y;
}
if ((parent != null) && !(parent instanceof CellRendererPane))
{
aRect.x += dx;
aRect.y += dy;
((JComponent) parent).scrollRectToVisible(aRect);
aRect.x -= dx;
aRect.y -= dy;
}
}
/**
* Forwards the {@code scrollRectToVisible()} message to the
* {@code JComponent}'s parent. Components that can service
* the request, such as {@code JViewport},
* override this method and perform the scrolling.
*
* @param aRect the visible {@code Rectangle}
* @see javax.swing.JViewport
*/
@Override
public void scrollRectToVisible(Rectangle aRect)
{
Container parent;
int dx = getX();
int dy = getY();
for (parent = getParent(); (parent != null) && !(parent instanceof JComponent)
&& !(parent instanceof CellRendererPane); parent = parent.getParent())
{
final Rectangle bounds = parent.getBounds();
dx += bounds.x;
dy += bounds.y;
}
if ((parent != null) && !(parent instanceof CellRendererPane))
{
aRect.x += dx;
aRect.y += dy;
((JComponent) parent).scrollRectToVisible(aRect);
aRect.x -= dx;
aRect.y -= dy;
}
}
void addTo(Component component) {
if (component == null || component instanceof CellRendererPane) {
return;
}
if (component instanceof Container) {
addTo((Container)component);
}
MnemonicWrapper.getWrapper(component);
}
/**
* Initializes <code>this.list</code> by calling <code>installDefaults()</code>,
* <code>installListeners()</code>, and <code>installKeyboardActions()</code>
* in order.
*
* @see #installDefaults
* @see #installListeners
* @see #installKeyboardActions
*/
public void installUI(JComponent c)
{
list = (JList)c;
rendererPane = new CellRendererPane();
list.add(rendererPane);
installDefaults();
installListeners();
installKeyboardActions();
}
/**
*
*/
public CellRendererPane getRendererPane()
{
return rendererPane;
}
/**
*
*/
public void paintShape(mxGraphics2DCanvas canvas, String text,
mxCellState state, Map<String, Object> style)
{
mxLightweightLabel textRenderer = mxLightweightLabel
.getSharedInstance();
CellRendererPane rendererPane = canvas.getRendererPane();
Rectangle rect = state.getLabelBounds().getRectangle();
Graphics2D g = canvas.getGraphics();
if (textRenderer != null
&& rendererPane != null
&& (g.getClipBounds() == null || g.getClipBounds().intersects(
rect)))
{
double scale = canvas.getScale();
int x = rect.x;
int y = rect.y;
int w = rect.width;
int h = rect.height;
if (!mxUtils.isTrue(style, mxConstants.STYLE_HORIZONTAL, true))
{
g.rotate(-Math.PI / 2, x + w / 2, y + h / 2);
g.translate(w / 2 - h / 2, h / 2 - w / 2);
int tmp = w;
w = h;
h = tmp;
}
// Replaces the linefeeds with BR tags
if (isReplaceHtmlLinefeeds())
{
text = text.replaceAll("\n", "<br>");
}
// Renders the scaled text
textRenderer.setText(createHtmlDocument(style, text,
(int) Math.round(w / state.getView().getScale()),
(int) Math.round(h / state.getView().getScale())));
textRenderer.setFont(mxUtils.getFont(style, canvas.getScale()));
g.scale(scale, scale);
rendererPane.paintComponent(g, textRenderer, rendererPane,
(int) (x / scale) + mxConstants.LABEL_INSET,
(int) (y / scale) + mxConstants.LABEL_INSET,
(int) (w / scale), (int) (h / scale), true);
}
}
/** Paint each of the given records that is inside of the clips */
private void paintRecords(List<DomainFileInfo> records, Graphics g) {
CellRendererPane rendererPane = new CellRendererPane();
paintCells(records, rendererPane, g);
}
@Override
public void componentAdded(ContainerEvent e) {
if (!(e.getContainer() instanceof CellRendererPane))
processAddition(e.getChild());
}
@Override
public void stateChanged(ChangeEvent e) {
SubstanceCoreUtilities.testComponentStateChangeThreadingViolation(progressBar);
//if (displayTimeline != null) { //Main Change - this should be first
// displayTimeline.abort();
//}
int currValue = progressBar.getValue();
int span = progressBar.getMaximum() - progressBar.getMinimum();
int barRectWidth = progressBar.getWidth() - 2 * margin;
int barRectHeight = progressBar.getHeight() - 2 * margin;
int totalPixels = (progressBar.getOrientation() == JProgressBar.HORIZONTAL) ? barRectWidth
: barRectHeight;
int pixelDelta = (span <= 0) ? 0 : (currValue - displayedValue)
* totalPixels / span;
/*displayTimeline = new Timeline(progressBar);
displayTimeline.addPropertyToInterpolate(Timeline
.<Integer>property("displayedValue").from(displayedValue)
.to(currValue).setWith(new TimelinePropertyBuilder.PropertySetter<Integer>() {
@Override
public void set(Object obj, String fieldName,
Integer value) {
displayedValue = value;
progressBar.repaint();
}
}));
displayTimeline.setEase(new Spline(0.4f));
AnimationConfigurationManager.getInstance().configureTimeline(
displayTimeline);*/
boolean isInCellRenderer = (SwingUtilities.getAncestorOfClass(
CellRendererPane.class, progressBar) != null);
//if (false) {//currValue > 0 && !isInCellRenderer && Math.abs(pixelDelta) > 5) {
// displayTimeline.play();
//} else {
displayedValue = currValue;
progressBar.repaint();
//}
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
rendererPane = new CellRendererPane();
c.add(rendererPane);
}