001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.diagram;
024:
025: import java.awt.BasicStroke;
026: import java.awt.Dimension;
027: import java.awt.GradientPaint;
028: import java.awt.Graphics;
029: import java.awt.Graphics2D;
030: import java.util.Collections;
031: import java.util.LinkedList;
032:
033: import org.jgraph.graph.CellViewRenderer;
034: import org.jgraph.graph.DefaultCellViewFactory;
035: import org.jgraph.graph.GraphConstants;
036: import org.jgraph.graph.VertexRenderer;
037: import org.jgraph.graph.VertexView;
038:
039: public class DiagramCellViewFactory extends DefaultCellViewFactory {
040:
041: public static boolean isBlank(String str) {
042: return str == null || str.trim().length() == 0;
043: }
044:
045: public static class RoundedRectangleRenderer extends VertexRenderer {
046:
047: private static final int INITIAL_LINE_LENGTH = 10;
048: private static final double LINE_COEFF = 9;
049: private static final int SPACE_TOLERANCE = 5;
050:
051: /**
052: * Return a slightly larger preferred size than for a rectangle.
053: */
054: public Dimension getPreferredSize() {
055: super .getPreferredSize().width += super .getPreferredSize().height / 4;
056: return super .getPreferredSize();
057: }
058:
059: public void paint(Graphics g) {
060: if (view.getCell() instanceof DiagramCell
061: && !((DiagramCell) view.getCell()).isRounded()) {
062: super .paint(g);
063: } else {
064: Graphics2D g2 = (Graphics2D) g;
065: Dimension size = getSize();
066: boolean tmp = selected;
067:
068: int big = Math.max(size.width, size.height);
069: int small = Math.min(size.width, size.height);
070: int arc = Math.min(15, Math.min(big / 5, small / 2));
071: if (super .isOpaque()) {
072: g.setColor(super .getBackground());
073: if (gradientColor != null && !preview) {
074: setOpaque(false);
075: g2.setPaint(new GradientPaint(0, 0,
076: getBackground(), getWidth(),
077: getHeight(), gradientColor, true));
078: }
079: g.fillRoundRect(borderWidth / 2, borderWidth / 2,
080: size.width - (int) (borderWidth * 1.5),
081: size.height - (int) (borderWidth * 1.5),
082: arc, arc);
083: }
084:
085: try {
086: setBorder(null);
087: setOpaque(false);
088: selected = false;
089: super .paint(g);
090: } finally {
091: selected = tmp;
092: }
093:
094: // g2.setStroke(new BasicStroke(1));
095: // g2.setColor(Color.RED);
096: // g.drawOval((int) size.getWidth()/2 - 15, (int) size.getHeight()/2 - 10, 30, 20);
097:
098: if (bordercolor != null) {
099: g.setColor(bordercolor);
100:
101: BasicStroke stroke = new BasicStroke(borderWidth);
102:
103: g2.setStroke(stroke);
104: g.drawRoundRect(borderWidth / 2, borderWidth / 2,
105: size.width - (int) (borderWidth * 1.5),
106: size.height - (int) (borderWidth * 1.5),
107: arc, arc);
108: }
109:
110: if (selected) {
111: g2.setStroke(GraphConstants.SELECTION_STROKE);
112: g.setColor(highlightColor);
113: g.drawRoundRect(borderWidth / 2, borderWidth / 2,
114: size.width - (int) (borderWidth * 1.5),
115: size.height - (int) (borderWidth * 1.5),
116: arc, arc);
117: }
118: }
119: }
120:
121: private class BreakPoint implements Comparable {
122: private int weight;
123: private boolean gobbleSpace;
124: private int position;
125: private char ch;
126:
127: public BreakPoint(int position, char ch, int median, int max) {
128: this .position = position;
129: this .ch = ch;
130: gobbleSpace = ch == ' ';
131: weight = Math.abs(max - position);
132: if (position > max) {
133: weight += SPACE_TOLERANCE;
134: }
135: switch (ch) {
136: case ' ':
137: break;
138: case '.':
139: case ',':
140: case '-':
141: case ':':
142: case ';':
143: weight += SPACE_TOLERANCE * 2;
144: break;
145: default:
146: weight += SPACE_TOLERANCE * SPACE_TOLERANCE * 2;
147: }
148: }
149:
150: public int compareTo(Object otherBp) {
151: return weight - ((BreakPoint) otherBp).weight;
152: }
153:
154: boolean gobbleSpace() {
155: return gobbleSpace;
156: }
157:
158: public String toString() {
159: return position + ": " + ch;
160: }
161: }
162:
163: public void setText(String text) {
164: if (text == null) {
165: super .setText(null);
166: } else {
167: int lineCount = (int) Math.round(Math.sqrt(text
168: .length()
169: / LINE_COEFF));
170: if (lineCount > 1) {
171: int lineLength = (int) (Math
172: .max(INITIAL_LINE_LENGTH, lineCount
173: * LINE_COEFF));
174: int max = lineLength;
175: StringBuffer sb = new StringBuffer("<html>");
176: int from = 0;
177: while (from + lineLength < text.length()) {
178: LinkedList breakPoints = new LinkedList();
179: for (int i = Math.max(0, from + lineLength
180: - SPACE_TOLERANCE); i < Math.min(text
181: .length(), from + lineLength
182: + SPACE_TOLERANCE); ++i) {
183: breakPoints.add(new BreakPoint(i, text
184: .charAt(i), from + lineLength, from
185: + max));
186: }
187: Collections.sort(breakPoints);
188: BreakPoint bp = (BreakPoint) breakPoints
189: .getFirst();
190: String newLine = text.substring(from,
191: bp.position);
192: max = Math.max(max, newLine.length() - 1);
193: sb.append(newLine);
194: sb.append("<br>");
195: from = bp.position;
196: if (bp.gobbleSpace) {
197: ++from;
198: }
199: }
200: sb.append(text.substring(from));
201: sb.append("</html>");
202: super .setText(sb.toString());
203: } else {
204: super .setText(text);
205: }
206: }
207:
208: }
209:
210: }
211:
212: private static final CellViewRenderer renderer = new RoundedRectangleRenderer();
213:
214: protected VertexView createVertexView(final Object v) {
215: if (v instanceof DiagramCell) {
216: VertexView view = new VertexView() {
217: public CellViewRenderer getRenderer() {
218: return DiagramCellViewFactory.renderer;
219: }
220: };
221:
222: view.setCell(v);
223: return view;
224: }
225: return super.createVertexView(v);
226:
227: }
228: }
|