1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ColorCharTextField extends JTextPane {
private static final long serialVersionUID = 1L;
private int columns;
private int columnWidth;
/**
* @param colorMap définit les caractères qu'on peut saisir et leur couleur d'affichage associée
*/
public ColorCharTextField(Map<Character, Color> colorMap) {
this(null, 0, colorMap);
}
public ColorCharTextField(int column , Map<Character, Color> colorMap) {
this(null, column, colorMap);
}
public ColorCharTextField(String text, Color colorZero, Map<Character, Color> colorMap) {
this(text, 0, colorMap);
}
public ColorCharTextField(String text, int column, Map<Character, Color> colorMap) {
super();
Map<Character, Color> cleanColorMap = cleanColorMap(colorMap);
StyledDocument styledDoc = getStyledDocument();
if (styledDoc instanceof AbstractDocument) {
AbstractDocument doc = (AbstractDocument)styledDoc;
doc.setDocumentFilter(new ColorStyler(cleanColorMap));
}
if ( text!=null ) {
setText(text);
}
}
/**
* Supprimes tous les caractères avant le !, sauf l'espace (dont \n et \r)
* @param colorMap
* @return
*/
private Map<Character, Color> cleanColorMap(Map<Character, Color> colorMap) {
Map<Character, Color> map = new HashMap<Character, Color>();
for(Map.Entry<Character, Color> entries : colorMap.entrySet()) {
if ( entries.getKey()==' ' || entries.getKey()>='!' ) {
map.put(entries.getKey(), entries.getValue());
}
}
return map;
}
/**
* {@see JTextField#getColumns()}
*/
public int getColumns() {
return columns;
}
/**
* {@see JTextField#setColumns(int)}
*/
public void setColumns(int columns) {
int oldVal = this.columns;
if (columns < 0) {
throw new IllegalArgumentException("columns less than zero.");
}
if (columns != oldVal) {
this.columns = columns;
invalidate();
}
}
/**
* Returns the column width.
* The meaning of what a column is can be considered a fairly weak
* notion for some fonts. This method is used to define the width
* of a column. By default this is defined to be the width of the
* character <em>m</em> for the font used. This method can be
* redefined to be some alternative amount
*
* @return the column width >= 1
*/
protected int getColumnWidth() {
if (columnWidth == 0) {
FontMetrics metrics = getFontMetrics(getFont());
columnWidth = metrics.charWidth('m');
}
return columnWidth;
}
/**
* {@see JTextField#getPreferredSize()}
*/
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
if (columns != 0) {
Insets insets = getInsets();
size.width = columns * getColumnWidth() +
insets.left + insets.right;
}
return size;
}
/**
* {@see JTextField#setFont(Font)}
*/
public void setFont(Font f) {
super.setFont(f);
columnWidth = 0;
}
private static class ColorStyler extends DocumentFilter {
private final Map<Character,Color> colorMap;
public ColorStyler(Map<Character, Color> colorMap) {
this.colorMap=colorMap;
}
@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
text=filter(text); // on nettoie la chaine avant pour ne pas prendre en compte un remplacement par un caractère non accepté
if ( !"".equals(text) ) {
remove(fb, offset, length);
insertString(fb, offset, text, attrs);
}
}
private String filter(String text) {
StringBuilder sb = new StringBuilder(text.length());
for(int i=0; i<text.length(); i++) {
char c = text.charAt(i);
if ( colorMap.containsKey(c) ) {
sb.append(c);
}
}
return sb.toString();
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
super.remove(fb, offset, length);
}
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
for(int i=0; i<string.length(); i++) {
char c = string.charAt(i);
MutableAttributeSet mattr = new SimpleAttributeSet(attr);
Color color = colorMap.get(c);
if ( color!=null ) {
StyleConstants.setForeground(mattr, color);
super.insertString(fb, offset+i, String.valueOf(c), mattr);
}
}
}
}
} |
Partager