All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mxgraph.shape.mxHtmlTextShape Maven / Gradle / Ivy

/**
 * $Id: mxHtmlTextShape.java,v 1.8 2012-05-10 11:29:43 gaudenz Exp $
 * Copyright (c) 2010, Gaudenz Alder, David Benson
 */
package com.mxgraph.shape;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Map;

import javax.swing.CellRendererPane;

import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxLightweightLabel;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;

/**
 * To set global CSS for all HTML labels, use the following code:
 * 
 * 
 * mxGraphics2DCanvas.putTextShape(mxGraphics2DCanvas.TEXT_SHAPE_HTML,
 *   new mxHtmlTextShape()
 *   {
 *     protected String createHtmlDocument(Map style, String text)
 *     {
 *       return mxUtils.createHtmlDocument(style, text, 1, 0,
 *           "");
 *     }
 *   }
 * );
 * 
*/ public class mxHtmlTextShape implements mxITextShape { /** * Specifies if linefeeds should be replaced with breaks in HTML markup. * Default is true. */ protected boolean replaceHtmlLinefeeds = true; /** * Returns replaceHtmlLinefeeds */ public boolean isReplaceHtmlLinefeeds() { return replaceHtmlLinefeeds; } /** * Returns replaceHtmlLinefeeds */ public void setReplaceHtmlLinefeeds(boolean value) { replaceHtmlLinefeeds = value; } /** * */ protected String createHtmlDocument(Map style, String text) { return mxUtils.createHtmlDocument(style, text); } /** * */ public void paintShape(mxGraphics2DCanvas canvas, String text, mxCellState state, Map 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", "
"); } // Renders the scaled text textRenderer.setText(createHtmlDocument(style, text)); 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); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy