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.Dimension;
018: import java.awt.FontMetrics;
019: import java.awt.Graphics;
020: import java.awt.Image;
021: import java.awt.Rectangle;
022: import java.awt.image.BufferedImage;
023:
024: import javax.swing.JLabel;
025: import javax.swing.SwingUtilities;
026:
027: import com.metaboss.applications.designstudio.Application;
028: import com.metaboss.applications.designstudio.BaseUserObject;
029:
030: public class ClassCellRenderer extends VertexCellRenderer {
031: private int mCaptionHeight = 0;
032: private int mFieldHeight = 0;
033:
034: public Dimension getPreferredSize() {
035: Image offscreen = new BufferedImage(50, 50,
036: BufferedImage.TYPE_INT_RGB);
037: Graphics g = offscreen.getGraphics();
038:
039: calculateHeights();
040:
041: DesignGraph pGraph = (DesignGraph) view.getGraph();
042: if (pGraph.getShowVertexFields()) {
043: Object[] lFields = getFields();
044: if (lFields != null) {
045:
046: int lFieldsHeight = (lFields != null) ? mFieldHeight
047: * lFields.length : 0;
048: int lHeight = mCaptionHeight + lFieldsHeight;
049: int lWidth = 20;
050:
051: g.setFont(Application.DEFAULT_FONT);
052: for (int i = 0; i < lFields.length; i++) {
053: String lText = lFields[i].toString();
054: int lTextWidth = (int) getFontMetrics(g.getFont())
055: .getStringBounds(lText, g).getWidth() + 10;
056: if (lTextWidth > lWidth)
057: lWidth = lTextWidth;
058: }
059: lWidth += 5;
060: if (lWidth < 100)
061: lWidth = 100;
062: if (lHeight < 50)
063: lHeight = 50;
064: return calculatePreferredSize(lWidth, lHeight, lWidth,
065: lHeight);
066: } else
067: return calculatePreferredSize(100, 50, 200, 200);
068: } else
069: return calculatePreferredSize(100, 50, 200, 200);
070: }
071:
072: protected void paintComponent(Graphics g) {
073: calculateHeights();
074:
075: Dimension lSize = getSize();
076: Rectangle lMainRect = new Rectangle(0, 0, lSize.width - 1,
077: lSize.height - 1);
078: Rectangle lCpationRect = new Rectangle(0, 0, lSize.width - 1,
079: mCaptionHeight);
080: Rectangle lFieldsRect = new Rectangle(0, mCaptionHeight,
081: lSize.width - 1, lMainRect.height - mCaptionHeight);
082:
083: g.setColor(getBackground());
084: fillRect(g, lMainRect);
085:
086: g.setColor(getForeground());
087: g.drawRect(lMainRect.x, lMainRect.y, lMainRect.width,
088: lMainRect.height);
089: g.drawLine(lCpationRect.x, lCpationRect.height,
090: lCpationRect.width, lCpationRect.height);
091:
092: paintCaption(g, lCpationRect);
093: DesignGraph pGraph = (DesignGraph) view.getGraph();
094: if (pGraph.getShowVertexFields())
095: paintFields(g, lFieldsRect);
096: }
097:
098: // paint fields
099: protected void paintFields(Graphics g, Rectangle pRect) {
100: Object[] lFields = getFields();
101: Rectangle lRect = new Rectangle(pRect);
102:
103: if (lFields == null || lFields.length == 0)
104: return;
105:
106: lRect.height = mFieldHeight;
107: for (int i = 0; i < lFields.length; i++) {
108: paintField(g, lFields[i], lRect);
109: lRect.y += mFieldHeight;
110: }
111: }
112:
113: // paint field
114: protected void paintField(Graphics g, Object pField, Rectangle pRect) {
115: Rectangle iconR = new Rectangle(0, 0, 0, 0);
116: Rectangle textR = new Rectangle(0, 0, 0, 0);
117:
118: String lFieldText = pField.toString();
119:
120: g.setFont(Application.DEFAULT_FONT);
121:
122: FontMetrics fm = g.getFontMetrics();
123: String lText = SwingUtilities.layoutCompoundLabel(fm,
124: lFieldText, Application.GRAPH_PUBLIC_ICON,
125: JLabel.CENTER, JLabel.WEST, JLabel.CENTER, JLabel.WEST,
126: pRect, iconR, textR, 0);
127:
128: int textX = textR.x;
129: int textY = textR.y + fm.getAscent();
130:
131: //g.drawString(lText, textX, textY);
132: Application.GRAPH_PUBLIC_ICON.paintIcon(this , g, 2, iconR.y);
133: g.drawString(lText, iconR.width + 4, textY);
134: }
135:
136: private void calculateHeights() {
137: mCaptionHeight = getFontMetrics(Application.VERTEX_FONT)
138: .getHeight() + 10;
139: mFieldHeight = getFontMetrics(Application.DEFAULT_FONT)
140: .getHeight() + 4;
141: }
142:
143: private Object[] getFields() {
144: BaseUserObject lObject = (BaseUserObject) ((VertexCellView) view)
145: .getUserObject();
146: return lObject.getFieldDescriptors();
147: }
148: }
|