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
|
package swingo;
import java.awt.Color;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class JOTextPane extends JTextPane {
private StyledDocument styledDocument = getStyledDocument();
protected float COMPONENT_ALIGNEMENT = 0.82f;
// Constructeur
public JOTextPane() {
super();
}
// Méthode pour générer le nom du style passé en paramètre
private static String getNameStyle(boolean bold, boolean italic, boolean underline, int size, Color color) {
StringBuffer sb = new StringBuffer();
if(bold) sb.append("1");
else sb.append("0");
if(italic) sb.append("1");
else sb.append("0");
if(underline) sb.append("1");
else sb.append("0");
sb.append(size);
sb.append(color.getRGB());
return sb.toString();
}
// Méthode pour créer et récupérer le style passé en paramètre
private Style getStyle(boolean bold, boolean italic, boolean underline, int size, Color color) {
// Récupère le nom du style
String styleName = getNameStyle(bold, italic, underline, size, color);
// On récupère le style (Si il n'existe pas on récupère null)
Style style = styledDocument.getStyle(styleName);
// Si le style existe on le retourne
if(style != null) return style;
// Si le style n'existe pas on le créer
else {
// Création du nouveau style
Style styleDefaut = styledDocument.getStyle(StyleContext.DEFAULT_STYLE);
style = styledDocument.addStyle(styleName, styleDefaut);
StyleConstants.setBold(style, bold);
StyleConstants.setItalic(style, italic);
StyleConstants.setUnderline(style, underline);
StyleConstants.setFontSize(style, size);
StyleConstants.setForeground(style, color);
return style;
}
}
// Ajout un texte à la fin
public synchronized void insertTextEnd(String texte, boolean bold, boolean italic, boolean underline, int size, Color color) {
try { styledDocument.insertString(styledDocument.getLength(), texte, getStyle(bold, italic, underline, size, color)); } catch(BadLocationException e) {}
}
// Ajoute une icone à la place de la sélection
public synchronized void insertIconSelect(Icon icon) {
insertIcon(icon);
}
// Ajoute d'une icone à la fin
public synchronized void insertIconEnd(Icon icon) {
select(styledDocument.getLength(), styledDocument.getLength());
insertIconSelect(icon);
}
// Ajoute d'un composant à la place de la sélection
public synchronized void insertComponentSelect(JComponent c) {
c.setAlignmentY(COMPONENT_ALIGNEMENT);
insertComponent(c);
}
// Ajoute d'un composant à la fin
public synchronized void insertComponentEnd(JComponent c) {
select(styledDocument.getLength(), styledDocument.getLength());
insertComponentSelect(c);
}
} |