1 package jimm.datavision.layout.swing; 2 import jimm.datavision.field.Field; 3 import jimm.datavision.field.Format; 4 import java.awt.Color ; 5 import javax.swing.JTextPane ; 6 import javax.swing.text.Style ; 7 import javax.swing.text.StyleContext ; 8 import javax.swing.text.StyleConstants ; 9 10 16 public class SwingTextField extends AbstractSwingField { 17 18 public static final Color HIDDEN_FG_COLOR = Color.gray; 19 20 25 public SwingTextField(Field f) { 26 this(f, f.toString()); 27 } 28 29 35 public SwingTextField(Field f, String str) { 36 super(f, new JTextPane ()); 37 JTextPane textPane = (JTextPane )getComponent(); 38 textPane.setText(str); 39 textPane.setEditable(false); 40 format(); 41 } 42 43 46 public void format() { 47 JTextPane textPane = (JTextPane )getComponent(); 48 49 Format format = field.getFormat(); 50 Style style = StyleContext.getDefaultStyleContext() 51 .getStyle(StyleContext.DEFAULT_STYLE); 52 53 int selStart = textPane.getSelectionStart(); 55 int selEnd = textPane.getSelectionEnd(); 56 textPane.selectAll(); 57 58 StyleConstants.setBold(style, format.isBold()); 59 StyleConstants.setItalic(style, format.isItalic()); 60 StyleConstants.setUnderline(style, format.isUnderline()); 61 StyleConstants.setFontSize(style, (int)format.getSize()); 62 StyleConstants.setFontFamily(style, format.getFontFamilyName()); 63 64 StyleConstants.setForeground(style, getColor(format)); 66 67 switch (format.getAlign()) { 69 case Format.ALIGN_CENTER: 70 StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER); 71 break; 72 case Format.ALIGN_RIGHT: 73 StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT); 74 break; 75 default: 76 StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT); 77 break; 78 } 79 textPane.setParagraphAttributes(style, true); 80 81 textPane.setCaretPosition(selStart); 83 textPane.moveCaretPosition(selEnd); 84 85 makeBorders(); 86 } 87 88 91 public Color getColor() { 92 return getColor(field.getFormat()); 93 } 94 95 98 public Color getColor(Format format) { 99 Color color = format.getColor(); 100 if (!field.isVisible()) { if (color.equals(Color.black)) 102 color = HIDDEN_FG_COLOR; 103 else { 104 float[] hsb = new float[3]; 105 106 Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), 107 hsb); 108 hsb[1] = (float)0.5; hsb[2] = (float)0.9; color = Color.getHSBColor(hsb[0], hsb[1], hsb[2]); 111 } 112 } 113 return color; 114 } 115 116 117 } 118 | Popular Tags |