001: package jimm.datavision.layout.swing;
002:
003: import jimm.datavision.field.Field;
004: import jimm.datavision.field.Format;
005: import java.awt.Color;
006: import javax.swing.JTextPane;
007: import javax.swing.text.Style;
008: import javax.swing.text.StyleContext;
009: import javax.swing.text.StyleConstants;
010:
011: /**
012: * A Swing field is the visual representation of a report field.
013: *
014: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
015: * @see SwingLE
016: */
017: public class SwingTextField extends AbstractSwingField {
018:
019: public static final Color HIDDEN_FG_COLOR = Color.gray;
020:
021: /**
022: * Constructor.
023: *
024: * @param f report field
025: */
026: public SwingTextField(Field f) {
027: this (f, f.toString());
028: }
029:
030: /**
031: * Constructor.
032: *
033: * @param f report field
034: * @param str label text
035: */
036: public SwingTextField(Field f, String str) {
037: super (f, new JTextPane());
038: JTextPane textPane = (JTextPane) getComponent();
039: textPane.setText(str);
040: textPane.setEditable(false);
041: format();
042: }
043:
044: /**
045: * Formats the label according to the field's formatting specifications.
046: */
047: public void format() {
048: JTextPane textPane = (JTextPane) getComponent();
049:
050: Format format = field.getFormat();
051: Style style = StyleContext.getDefaultStyleContext().getStyle(
052: StyleContext.DEFAULT_STYLE);
053:
054: // Save selection and select all text
055: int selStart = textPane.getSelectionStart();
056: int selEnd = textPane.getSelectionEnd();
057: textPane.selectAll();
058:
059: StyleConstants.setBold(style, format.isBold());
060: StyleConstants.setItalic(style, format.isItalic());
061: StyleConstants.setUnderline(style, format.isUnderline());
062: StyleConstants.setFontSize(style, (int) format.getSize());
063: StyleConstants.setFontFamily(style, format.getFontFamilyName());
064:
065: // Color is based on visibility flag of field
066: StyleConstants.setForeground(style, getColor(format));
067:
068: // Align
069: switch (format.getAlign()) {
070: case Format.ALIGN_CENTER:
071: StyleConstants.setAlignment(style,
072: StyleConstants.ALIGN_CENTER);
073: break;
074: case Format.ALIGN_RIGHT:
075: StyleConstants.setAlignment(style,
076: StyleConstants.ALIGN_RIGHT);
077: break;
078: default:
079: StyleConstants.setAlignment(style,
080: StyleConstants.ALIGN_LEFT);
081: break;
082: }
083: textPane.setParagraphAttributes(style, true);
084:
085: // Restore selection
086: textPane.setCaretPosition(selStart);
087: textPane.moveCaretPosition(selEnd);
088:
089: makeBorders();
090: }
091:
092: /**
093: * Returns field color based on visibility.
094: */
095: public Color getColor() {
096: return getColor(field.getFormat());
097: }
098:
099: /**
100: * Returns field color based on visibility.
101: */
102: public Color getColor(Format format) {
103: Color color = format.getColor();
104: if (!field.isVisible()) { // If hidden, lighten color
105: if (color.equals(Color.black))
106: color = HIDDEN_FG_COLOR;
107: else {
108: float[] hsb = new float[3];
109:
110: Color.RGBtoHSB(color.getRed(), color.getGreen(), color
111: .getBlue(), hsb);
112: hsb[1] = (float) 0.5; // Saturation
113: hsb[2] = (float) 0.9; // Brightness
114: color = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
115: }
116: }
117: return color;
118: }
119:
120: }
|