001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.components;
016:
017: import java.awt.AlphaComposite;
018: import java.awt.Color;
019: import java.awt.Composite;
020: import java.awt.Dimension;
021: import java.awt.FontMetrics;
022: import java.awt.Graphics;
023: import java.awt.Graphics2D;
024: import java.awt.Image;
025: import java.awt.Rectangle;
026: import java.awt.image.BufferedImage;
027:
028: import javax.swing.JLabel;
029: import javax.swing.SwingUtilities;
030:
031: import org.jgraph.graph.DefaultGraphCell;
032: import org.jgraph.graph.VertexRenderer;
033:
034: import com.metaboss.applications.designstudio.Application;
035: import com.metaboss.applications.designstudio.BaseUserObject;
036:
037: /* Define the Renderer for the DesignGraph Vertex */
038:
039: public class VertexCellRenderer extends VertexRenderer {
040: protected float mAlpha = 0.80f;
041: protected float mVertexAlpha = 0.60f;
042:
043: public void paint(Graphics g) {
044: try {
045: if (selected)
046: super .paint(g);
047: else {
048: Graphics2D g2 = (Graphics2D) g;
049: Composite originalComposite = g2.getComposite();
050: g2.setComposite(AlphaComposite.getInstance(
051: AlphaComposite.SRC_OVER, mAlpha));
052: try {
053: super .paint(g2);
054: } finally {
055: g2.setComposite(originalComposite);
056: }
057: }
058: //super.paint(g);
059: paintSelectionBorder(g);
060:
061: DefaultGraphCell lCell = (DefaultGraphCell) view.getCell();
062: if (lCell != null) {
063: Object lObject = lCell.getUserObject();
064: if (lObject instanceof BaseUserObject) {
065: if (Application
066: .isErrorElementID(((BaseUserObject) lObject)
067: .getID())) {
068: Dimension d = getSize();
069: g.setColor(Color.red);
070: g.drawRect(1, 1, d.width - 3, d.height - 3);
071: }
072: }
073: }
074: } catch (IllegalArgumentException e) {
075: // JDK Bug: Zero length string passed to TextLayout constructor
076: }
077: }
078:
079: // calculate vertex prefered size
080: protected Dimension calculatePreferredSize(int pMinWidth,
081: int pMinHeight, int pMaxWidth, int pMaxHeight) {
082: Image offscreen = new BufferedImage(50, 50,
083: BufferedImage.TYPE_INT_RGB);
084: Graphics g = offscreen.getGraphics();
085: String lText = getText();
086:
087: g.setFont(getFont());
088: int lWidth = (int) getFontMetrics(g.getFont()).getStringBounds(
089: lText, g).getWidth() + 10;
090: int lHeight = getFontMetrics(g.getFont()).getHeight();
091:
092: if (pMinWidth > lWidth)
093: lWidth = pMinWidth;
094: if (pMinHeight > lHeight)
095: lHeight = pMinHeight;
096: if (pMaxWidth < lWidth)
097: lWidth = pMaxWidth;
098: if (pMaxHeight < lHeight)
099: lHeight = pMaxHeight;
100:
101: return new Dimension(lWidth, lHeight);
102: }
103:
104: // paint vertex caption
105: protected void paintCaption(Graphics g, Rectangle pRect) {
106: Rectangle iconR = new Rectangle(0, 0, 0, 0);
107: Rectangle textR = new Rectangle(0, 0, 0, 0);
108:
109: FontMetrics fm = g.getFontMetrics();
110: String lText = SwingUtilities.layoutCompoundLabel(fm,
111: getText(), null, JLabel.CENTER, JLabel.CENTER,
112: JLabel.CENTER, JLabel.CENTER, pRect, iconR, textR, 0);
113:
114: int textX = textR.x;
115: int textY = textR.y + fm.getAscent();
116:
117: g.drawString(lText, textX, textY);
118: }
119:
120: // fill rectangle
121: protected void fillRect(Graphics g, Rectangle pRect) {
122: if (selected)
123: g.fillRect(pRect.x, pRect.y, pRect.width, pRect.height);
124: else {
125: Graphics2D g2 = (Graphics2D) g;
126: Composite originalComposite = g2.getComposite();
127: g2.setComposite(AlphaComposite.getInstance(
128: AlphaComposite.SRC_OVER, mVertexAlpha));
129: try {
130: g.fillRect(pRect.x, pRect.y, pRect.width, pRect.height);
131: } finally {
132: g2.setComposite(originalComposite);
133: }
134: }
135: }
136:
137: // fill round rectangle
138: protected void fillRoundRect(Graphics g, Rectangle pRect,
139: int arcWidth, int arcHeight) {
140: if (selected)
141: g.fillRoundRect(pRect.x, pRect.y, pRect.width,
142: pRect.height, arcWidth, arcHeight);
143: else {
144: Graphics2D g2 = (Graphics2D) g;
145: Composite originalComposite = g2.getComposite();
146: g2.setComposite(AlphaComposite.getInstance(
147: AlphaComposite.SRC_OVER, mVertexAlpha));
148: try {
149: g.fillRoundRect(pRect.x, pRect.y, pRect.width,
150: pRect.height, arcWidth, arcHeight);
151: } finally {
152: g2.setComposite(originalComposite);
153: }
154: }
155: }
156:
157: // fill oval
158: protected void fillOval(Graphics g, Rectangle pRect) {
159: if (selected)
160: g.fillOval(pRect.x, pRect.y, pRect.width, pRect.height);
161: else {
162: Graphics2D g2 = (Graphics2D) g;
163: Composite originalComposite = g2.getComposite();
164: g2.setComposite(AlphaComposite.getInstance(
165: AlphaComposite.SRC_OVER, mVertexAlpha));
166: try {
167: g.fillOval(pRect.x, pRect.y, pRect.width, pRect.height);
168: } finally {
169: g2.setComposite(originalComposite);
170: }
171: }
172: }
173: }
|