001: package com.xoetrope.carousel.survey;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Font;
006: import java.awt.Graphics;
007: import java.awt.Graphics2D;
008: import java.awt.RenderingHints;
009: import java.awt.Shape;
010: import java.awt.geom.Rectangle2D;
011: import java.awt.geom.RoundRectangle2D;
012: import javax.swing.JComponent;
013: import javax.swing.JLayeredPane;
014:
015: /**
016: * A view of question group view and rule view header.
017: *
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p> $Revision: 1.5 $</p>
023: */
024: public class XHeaderField extends JComponent {
025: protected XSurveyComponentBig owner;
026: protected XRulesEditorPane editorPane;
027: protected Shape shape;
028:
029: public XHeaderField(XSurveyComponentBig o, XRulesEditorPane ep) {
030: super ();
031: owner = o;
032: editorPane = ep;
033: editorPane.add(this , JLayeredPane.PALETTE_LAYER);
034: setSize();
035: }
036:
037: public void setSize() {
038: int w = owner.getHeaderFieldWidth();
039: int h = owner.getHeaderFieldHeight();
040: Dimension size = new Dimension(w, h);
041: setSize(size);
042: setPreferredSize(size);
043: setMinimumSize(size);
044: createShape();
045: }
046:
047: protected void createShape() {
048: int w = owner.getHeaderFieldWidth() - 1;
049: int h = owner.getHeaderFieldHeight() - 1;
050: int arc = owner.getHeaderFieldArc();
051: shape = new RoundRectangle2D.Double(0, 0, w, h, arc, arc);
052: }
053:
054: public void paintHeader(Graphics2D g) {
055: int x, y = 0, w, h = owner.getHeaderFieldHeight() - 1;
056: int arc = owner.getHeaderFieldArc();
057: Color backgroundColor = owner.getHeaderBackground();
058: Color borderColor = owner.getHeaderFieldBorder();
059:
060: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
061: RenderingHints.VALUE_ANTIALIAS_ON);
062: x = 0;
063: w = owner.getHeaderIdWidth() - 1;
064: g.setColor(backgroundColor);
065: g.fillRoundRect(x, y, w, h, arc, arc);
066: g.setColor(borderColor);
067: g.drawRoundRect(x, y, w, h, arc, arc);
068:
069: x = owner.getHeaderIdWidth() + owner.getHeaderFieldGap();
070: w = owner.getHeaderNameWidth() - 1;
071: g.setColor(backgroundColor);
072: g.fillRoundRect(x, y, w, h, arc, arc);
073: g.setColor(borderColor);
074: g.drawRoundRect(x, y, w, h, arc, arc);
075:
076: x = owner.getHeaderIdWidth() + owner.getHeaderNameWidth() + 2
077: * owner.getHeaderFieldGap();
078: w = owner.getHeaderTargetWidth() - 1;
079: g.setColor(backgroundColor);
080: g.fillRoundRect(x, y, w, h, arc, arc);
081: g.setColor(borderColor);
082: g.drawRoundRect(x, y, w, h, arc, arc);
083:
084: Font font = owner.getHeaderFont();
085: Color fontColor = owner.getHeaderFontColor();
086: int idWidth = owner.getHeaderIdWidth();
087: int nameWidth = owner.getHeaderNameWidth();
088: int targetWidth = owner.getHeaderTargetWidth();
089: int fieldGap = owner.getHeaderFieldGap();
090: int textGap = owner.getHeaderTextGap();
091:
092: g.setFont(font);
093: g.setColor(fontColor);
094: Rectangle2D rc = null;
095:
096: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
097: RenderingHints.VALUE_ANTIALIAS_OFF);
098: String id = owner.getHeaderId();
099: String truncId = getTruncatedText(id, idWidth - 2 * textGap, g,
100: font);
101: rc = font.getStringBounds(truncId, g.getFontRenderContext());
102: x = (int) (idWidth - rc.getWidth()) / 2;
103: y = (int) (rc.getHeight());
104: g.drawString(truncId, x, y);
105:
106: String name = owner.getHeaderName();
107: String truncName = getTruncatedText(name, nameWidth - 2
108: * textGap, g, font);
109: rc = font.getStringBounds(truncName, g.getFontRenderContext());
110: x = idWidth + fieldGap + textGap;
111: y = (int) (rc.getHeight());
112: g.drawString(truncName, x, y);
113:
114: String target = owner.getHeaderTarget();
115: String truncTarget = getTruncatedText(target, targetWidth - 2
116: * textGap, g, font);
117: rc = font
118: .getStringBounds(truncTarget, g.getFontRenderContext());
119: x = (int) (idWidth + fieldGap + nameWidth + fieldGap + (targetWidth - rc
120: .getWidth()) / 2);
121: y = (int) (rc.getHeight());
122: g.drawString(truncTarget, x, y);
123:
124: }
125:
126: public String getTruncatedText(String desc, int width,
127: Graphics2D g, Font font) {
128: String truncDesc = "";
129:
130: Rectangle2D rc = font.getStringBounds(desc, g
131: .getFontRenderContext());
132: if (rc.getWidth() > width) {
133: do {
134: int eidx = (desc.length() > 0 ? desc.length() - 1 : 0);
135: desc = desc.substring(0, eidx);
136: truncDesc = desc + "...";
137: rc = font.getStringBounds(truncDesc, g
138: .getFontRenderContext());
139: } while (rc.getWidth() > width && desc.length() > 0);
140: } else {
141: truncDesc = desc;
142: }
143: return truncDesc;
144: }
145:
146: public void paint(Graphics g) {
147: Graphics2D g2d = (Graphics2D) g;
148: paintHeader(g2d);
149: }
150:
151: }
|