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.web.interaction;
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: import biz.hammurapi.web.webdiagram.Activity;
040: import biz.hammurapi.web.webdiagram.Entity;
041:
042: public class ArgunCellViewFactory extends DefaultCellViewFactory {
043:
044: public static boolean isBlank(String str) {
045: return str == null || str.trim().length() == 0;
046: }
047:
048: public static class RoundedRectangleRenderer extends VertexRenderer {
049:
050: private static final int INITIAL_LINE_LENGTH = 10;
051: private static final double LINE_COEFF = 9;
052: private static final int SPACE_TOLERANCE = 5;
053:
054: /**
055: * Return a slightly larger preferred size than for a rectangle.
056: */
057: public Dimension getPreferredSize() {
058: super .getPreferredSize().width += super .getPreferredSize().height / 4;
059: return super .getPreferredSize();
060: }
061:
062: public void paint(Graphics g) {
063: if (view.getCell() instanceof Entity) {
064: super .paint(g);
065: } else {
066: Graphics2D g2 = (Graphics2D) g;
067: Dimension size = getSize();
068: boolean tmp = selected;
069:
070: int big = Math.max(size.width, size.height);
071: int small = Math.min(size.width, size.height);
072: int arc = Math.min(15, Math.min(big / 5, small / 2));
073: if (super .isOpaque()) {
074: g.setColor(super .getBackground());
075: if (gradientColor != null && !preview) {
076: setOpaque(false);
077: g2.setPaint(new GradientPaint(0, 0,
078: getBackground(), getWidth(),
079: getHeight(), gradientColor, true));
080: }
081: g.fillRoundRect(borderWidth / 2, borderWidth / 2,
082: size.width - (int) (borderWidth * 1.5),
083: size.height - (int) (borderWidth * 1.5),
084: arc, arc);
085: }
086:
087: try {
088: setBorder(null);
089: setOpaque(false);
090: selected = false;
091: super .paint(g);
092: } finally {
093: selected = tmp;
094: }
095:
096: if (bordercolor != null) {
097: g.setColor(bordercolor);
098: boolean hasGuard = false;
099: boolean hasAction = false;
100:
101: Object cell = view.getCell();
102:
103: if (cell instanceof Step) {
104: hasGuard = !isBlank(((Step) cell).getGuard());
105: hasAction = !isBlank(((Step) cell).getAction());
106: } else if (cell instanceof biz.hammurapi.web.process.Task) {
107:
108: }
109:
110: int actualBorderWidth = hasAction ? borderWidth + 1
111: : borderWidth;
112: BasicStroke stroke = hasGuard ? new BasicStroke(
113: actualBorderWidth, BasicStroke.CAP_BUTT,
114: BasicStroke.JOIN_BEVEL, 1, new float[] { 5,
115: 5 }, 0) : new BasicStroke(
116: actualBorderWidth);
117:
118: g2.setStroke(stroke);
119: g.drawRoundRect(borderWidth / 2, borderWidth / 2,
120: size.width - (int) (borderWidth * 1.5),
121: size.height - (int) (borderWidth * 1.5),
122: arc, arc);
123: }
124:
125: if (selected) {
126: g2.setStroke(GraphConstants.SELECTION_STROKE);
127: g.setColor(highlightColor);
128: g.drawRoundRect(borderWidth / 2, borderWidth / 2,
129: size.width - (int) (borderWidth * 1.5),
130: size.height - (int) (borderWidth * 1.5),
131: arc, arc);
132: }
133: }
134: }
135:
136: private class BreakPoint implements Comparable {
137: private int weight;
138: private boolean gobbleSpace;
139: private int position;
140: private char ch;
141:
142: public BreakPoint(int position, char ch, int median, int max) {
143: this .position = position;
144: this .ch = ch;
145: gobbleSpace = ch == ' ';
146: weight = Math.abs(max - position);
147: if (position > max) {
148: weight += SPACE_TOLERANCE;
149: }
150: switch (ch) {
151: case ' ':
152: break;
153: case '.':
154: case ',':
155: case '-':
156: case ':':
157: case ';':
158: weight += SPACE_TOLERANCE * 2;
159: break;
160: default:
161: weight += SPACE_TOLERANCE * SPACE_TOLERANCE * 2;
162: }
163: }
164:
165: public int compareTo(Object otherBp) {
166: return weight - ((BreakPoint) otherBp).weight;
167: }
168:
169: boolean gobbleSpace() {
170: return gobbleSpace;
171: }
172:
173: public String toString() {
174: return position + ": " + ch;
175: }
176: }
177:
178: public void setText(String text) {
179: if (text == null) {
180: super .setText(null);
181: } else {
182: int lineCount = (int) Math.round(Math.sqrt(text
183: .length()
184: / LINE_COEFF));
185: if (lineCount > 1) {
186: int lineLength = (int) (Math
187: .max(INITIAL_LINE_LENGTH, lineCount
188: * LINE_COEFF));
189: int max = lineLength;
190: StringBuffer sb = new StringBuffer("<html>");
191: int from = 0;
192: while (from + lineLength < text.length()) {
193: LinkedList breakPoints = new LinkedList();
194: for (int i = Math.max(0, from + lineLength
195: - SPACE_TOLERANCE); i < Math.min(text
196: .length(), from + lineLength
197: + SPACE_TOLERANCE); ++i) {
198: breakPoints.add(new BreakPoint(i, text
199: .charAt(i), from + lineLength, from
200: + max));
201: }
202: Collections.sort(breakPoints);
203: BreakPoint bp = (BreakPoint) breakPoints
204: .getFirst();
205: String newLine = text.substring(from,
206: bp.position);
207: max = Math.max(max, newLine.length() - 1);
208: sb.append(newLine);
209: sb.append("<br>");
210: from = bp.position;
211: if (bp.gobbleSpace) {
212: ++from;
213: }
214: }
215: sb.append(text.substring(from));
216: sb.append("</html>");
217: super .setText(sb.toString());
218: } else {
219: super .setText(text);
220: }
221: }
222:
223: }
224:
225: }
226:
227: private static final CellViewRenderer renderer = new RoundedRectangleRenderer();
228:
229: protected VertexView createVertexView(final Object v) {
230: if (v instanceof Entity || v instanceof Activity
231: || v instanceof Step
232: || v instanceof biz.hammurapi.web.process.Task) {
233: VertexView view = new VertexView() {
234: public CellViewRenderer getRenderer() {
235: return ArgunCellViewFactory.renderer;
236: }
237: };
238:
239: view.setCell(v);
240: return view;
241: }
242: return super.createVertexView(v);
243:
244: }
245: }
|