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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
| public class MyText extends JTextPane {
private MyDoc doc; // ton document a appliquer sur le composant de texte
private MyFrame parent; // ta fenetre, pour accéder aux boutons...
private StyleContext style;
private static String DEB_GRAS = "<b>",
FIN_GRAS = "</b>",
DEB_ITAL = "<i>",
FIN_ITAL = "</i>",
DEB_SOUL = "<u>",
FIN_SOUL = "</u>",
COLOR1 = "<span style='color=\"",
COLOR2 = "\";'>",
COLOR3 = "</c>";
//Pour accéder au StyleContext du composant.
public StyleContext getStyleContext() {
return this.style;
}
public MyText(MyFrame parent) {
super();
this.parent = parent;
style = new StyleContext();
this.doc = new MyDoc(style,this);
this.setDocument(this.doc); // c'est ca qui te permet de dire au composant de texte qu'il faut utiliser MyDoc plutot que le DefaultStyledDocument...
}
// modifie le style d'ecriture d'un texte deja ecrit
public void newStyle(String etat) {
int tempo = Integer.valueOf(etat).intValue();
int[] sel = getSelection();
System.out.println("passage par newStyle dans MyText "+etat+". {"+sel[0]+","+sel[1]+"}");
int length = 1, offset = 0;
if (tempo == StyleConstants.ALIGN_CENTER
|| tempo == StyleConstants.ALIGN_JUSTIFIED
|| tempo == StyleConstants.ALIGN_LEFT
|| tempo == StyleConstants.ALIGN_RIGHT) {
// ici on ne change que l'alignement du texte dans le composant
Style defaut = style.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(defaut, tempo);
System.out.println("change align");
this.doc.setParagraphAttributes(0,this.doc.getLength(),defaut,false);
} else {
// ici on change le style
// nous devons appliquer uniquement le changement de style sur le point precis (par ex le soulignement), et ce à tous les éventuels style de la sélection
while (sel[0]+offset < sel[1]) {
// on parcours dabord toute la sélection
System.out.println("on est a : "+(sel[0]+offset)+" et on va a : "+sel[1]);
AttributeSet start = this.doc.getCharacterElement(sel[0]+offset).getAttributes();
// et on cherche les éventuelles ruptures de style
while ((MyText.isEqualsStyle(this.doc.getCharacterElement(sel[0]+offset+length).getAttributes(),start)) && (length < (sel[1]-sel[0]-offset))) {
length++;
}
System.out.println("de : "+(sel[0]+offset)+" à : "+(sel[0]+offset+length)+" on fait laction");
this.doc.setCharacterAttributes(sel[0]+offset,length,getStyle(sel[0]+offset,tempo),true);
offset += length;
length = 0;
}
System.out.println("fin de newStyle()");
}
}
//Retourne les indices de début et de fin de la sélection dans le composant.
public int[] getSelection() {
//System.out.println("passage par getSelection dans JText");
int[] tempo = new int[2];
tempo[0] = this.getSelectionStart();
tempo[1] = this.getSelectionEnd();
return tempo;
}
// Permet de changer la sélection du texte.
public void setSelection(int debut, int fin) {
this.setSelectionStart(debut);
this.setSelectionEnd(fin);
}
//Permet de créé un Style en fonction des valeurs des champs de la fenetre
public Style getStyle() {
//System.out.println("passage par getStyle dans JText");
Style defaut = style.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setBold(defaut,parent.isGras());
StyleConstants.setItalic(defaut, parent.isItal());
StyleConstants.setUnderline(defaut, parent.isSoul());
//System.out.println(new Color(parent.getColor()));
StyleConstants.setForeground(defaut, new Color(parent.getColor()));
return defaut;
}
//Permet de créé un Style en fonction des valeurs des champs de la fenetre, et de l'index du charactère de départ. Le champ etat spécifie l'action demandé, gras, souligne, etc...
public Style getStyle(int idx, int etat) {
//System.out.println("passage par getStyle dans JText");
Style defaut = style.getStyle(StyleContext.DEFAULT_STYLE);
AttributeSet tempo = this.doc.getCharacterElement(idx).getAttributes();
StyleConstants.setBold(defaut,StyleConstants.isBold(tempo));
StyleConstants.setItalic(defaut,StyleConstants.isItalic(tempo));
StyleConstants.setUnderline(defaut,StyleConstants.isUnderline(tempo));
StyleConstants.setForeground(defaut,StyleConstants.getForeground(tempo));
if (etat == 4) {
StyleConstants.setBold(defaut,parent.isGras());
} else if (etat == 5) {
StyleConstants.setItalic(defaut, parent.isItal());
} else if (etat == 6) {
StyleConstants.setUnderline(defaut, parent.isSoul());
} else if (etat == 8) {
//System.out.println(new Color(parent.getColor()));
StyleConstants.setForeground(defaut, new Color(parent.getColor()));
}
return defaut;
}
// compare les deux styles en parametres
private static boolean isEqualsStyle(AttributeSet style1, AttributeSet style2) {
boolean tempo = true;
tempo = (tempo && (StyleConstants.isBold(style1) == StyleConstants.isBold(style2)));
//System.out.println("step1"+tempo);
tempo = (tempo && (StyleConstants.isItalic(style1) == StyleConstants.isItalic(style2)));
//System.out.println("step2"+tempo);
tempo = (tempo && (StyleConstants.isUnderline(style1) == StyleConstants.isUnderline(style2)));
//System.out.println("step3"+tempo);
tempo = (tempo && (StyleConstants.getForeground(style1).equals(StyleConstants.getForeground(style2))));
//System.out.println("-->"+tempo);
return tempo;
}
// retourne la balise HTML d'ouverture en fonction des parametres de style
private String getDebutBalise(boolean isItal, boolean isBold, boolean isUnder,
int rgb) {
String tempo = new String();
tempo += MyText.COLOR1 + "#"+
Integer.toHexString(rgb).toUpperCase().substring(2) + MyText.COLOR2;
if (isItal) {
tempo += MyText.DEB_ITAL;
}
if (isBold) {
tempo += MyText.DEB_GRAS;
}
if (isUnder) {
tempo += MyText.DEB_SOUL;
}
return tempo;
}
// retourne la balise HTML de fermeture en fonction des parametres de style
private String getFinBalise(boolean isItal, boolean isBold, boolean isUnder) {
String tempo = new String();
if (isUnder) {
tempo += MyText.FIN_SOUL;
}
if (isBold) {
tempo += MyText.FIN_GRAS;
}
if (isItal) {
tempo += MyText.FIN_ITAL;
}
tempo += MyText.COLOR3;
return tempo;
}
//Retourne le texte formaté, c'est à dire avec les balises HTML décrivants les styles.
public String getText() {
if (super.getText().length() == 0) {
return "";
} else {
String retourn = new String();
AttributeSet start = this.doc.getCharacterElement(0).getAttributes();
retourn += getDebutBalise(
StyleConstants.isItalic(start),
StyleConstants.isBold(start),
StyleConstants.isUnderline(start),
StyleConstants.getForeground(start).getRGB()
);
try {
retourn += this.getText(0,1);
} catch (BadLocationException ble) { ble.printStackTrace(); }
try {
for (int i=1;(i<this.doc.getLength());i++) {
if(!MyText.isEqualsStyle(this.doc.getCharacterElement(i).getAttributes(),start)) {
AttributeSet tempo = this.doc.getCharacterElement(i).getAttributes();
retourn += getFinBalise(
StyleConstants.isItalic(start),
StyleConstants.isBold(start),
StyleConstants.isUnderline(start) );
retourn += getDebutBalise(
StyleConstants.isItalic(tempo),
StyleConstants.isBold(tempo),
StyleConstants.isUnderline(tempo),
StyleConstants.getForeground(tempo).getRGB()
);
start = tempo;
}
try {
retourn += this.getText(i,1);
} catch (BadLocationException ble) { ble.printStackTrace(); }
}
start = this.doc.getCharacterElement(this.doc.getLength()-1).getAttributes();
retourn += getFinBalise(
StyleConstants.isItalic(start),
StyleConstants.isBold(start),
StyleConstants.isUnderline(start) );
} catch (Exception e) { System.out.println("taille nulle ?? "+e); }
System.out.println(retourn);
return retourn;
}
}
//Interprète le texte formaté et affiche son résultat. Idéal si l'on veut faire une fonction de modification...
public void initText(String texte) {
texte = texte.replaceAll("\\\\n","\n");
texte = texte.replaceAll("\\\\\"","\"");
String tempo = new String();
Style crtStyle = this.style.getStyle(StyleContext.DEFAULT_STYLE);
if (!texte.startsWith(MyText.COLOR1)) {
StyleConstants.setForeground(crtStyle,new Color(this.parent.getInitTxColor()));
}
int debut = 0;
boolean fini = false;
while (!fini) {
int fin = texte.indexOf("<",debut);
if (fin != -1) {
tempo += texte.substring(debut,fin);
// on commence par insérer la portion de texte (si le texte commence par une balise, cela n'insérera rien...
this.doc.insertStringSpecial(this.doc.getLength(),texte.substring(debut,fin),crtStyle);
int offset = 0;
if(texte.startsWith(MyText.DEB_GRAS,fin)) {
StyleConstants.setBold(crtStyle, true);
offset = MyText.DEB_GRAS.length();
} else if (texte.startsWith(MyText.FIN_GRAS,fin)) {
StyleConstants.setBold(crtStyle,false);
offset = MyText.FIN_GRAS.length();
} else if (texte.startsWith(MyText.DEB_ITAL,fin)) {
StyleConstants.setItalic(crtStyle,true);
offset = MyText.DEB_ITAL.length();
} else if (texte.startsWith(MyText.FIN_ITAL,fin)) {
StyleConstants.setItalic(crtStyle,false);
offset = MyText.FIN_ITAL.length();
} else if (texte.startsWith(MyText.DEB_SOUL,fin)) {
StyleConstants.setUnderline(crtStyle,true);
offset = MyText.DEB_SOUL.length();
} else if (texte.startsWith(MyText.FIN_SOUL,fin)) {
StyleConstants.setUnderline(crtStyle,false);
offset = MyText.FIN_SOUL.length();
} else if (texte.startsWith(MyText.COLOR1,fin)) {
int color = this.parent.getInitTxColor();
try {
//System.out.println("color : "+texte.substring(fin+MyText.COLOR1.length()+1,fin+MyText.COLOR1.length()+6+1));
color = Integer.parseInt(texte.substring(fin+MyText.COLOR1.length()+1,fin+MyText.COLOR1.length()+6+1),16);
StyleConstants.setForeground(crtStyle,new Color(color));
} catch (Exception e) {
System.out.println("probleme de parse sur la couleur... "+e);
}
offset = MyText.COLOR1.length() + 6 + 1 + MyText.COLOR2.length();
} else if (texte.startsWith(MyText.COLOR3,fin)) {
offset = MyText.COLOR3.length();
} else {
// alors cest ke cest un crochet dans le texte, tout simplement...
//System.out.println("[ simple at "+fin+" --> "+texte.charAt(fin));
tempo += ""+texte.charAt(fin);
this.doc.insertStringSpecial(this.doc.getLength(),""+texte.charAt(fin),crtStyle);
offset ++;
}
debut = fin+offset;
//System.out.println("il nous reste : "+texte.substring(debut));
} else {
tempo += texte.substring(debut);
this.doc.insertStringSpecial(this.doc.getLength(),texte.substring(debut),crtStyle);
fini = true;
}
}
// Initialise les boutons de la fenetre avec le dernier style rencontré
if (this.doc.getLength() > 0) {
renewStyle(this.doc.getCharacterElement(this.doc.getLength()-1).getAttributes());
}
//this.doc.insertString(0,tempo,null);
//System.out.println(super.getText());
//System.out.println(getText());
}
//Permet de déclencher la mise à jour des boutons dans la fenetre
public void renewStyle(AttributeSet a) {
this.parent.setGras(StyleConstants.isBold(a));
this.parent.setItalique(StyleConstants.isItalic(a));
this.parent.setSouligne(StyleConstants.isUnderline(a));
this.parent.setColor(StyleConstants.getForeground(a).getRGB());
}
//Pour insérer les balises autour de la sélection. Permet d'utiliser des gestionnaires exterieurs pour des fonctionnalités autres que l'affichage d'un texte brut. Si tu veut mettre une balise d'image etc...
public void insertBalises(String debutBalise, String finBalise) {
int[] sel = this.getSelection();
Style tempo = getStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setBold(tempo,false);
StyleConstants.setItalic(tempo,false);
StyleConstants.setUnderline(tempo,false);
StyleConstants.setForeground(tempo,new Color(this.parent.getInitTxColor()));
if (debutBalise!=null && debutBalise.length() > 0) {
this.doc.insertStringSpecial(sel[0],debutBalise,tempo);
}
sel[0] += debutBalise.length();
sel[1] += debutBalise.length();
if (debutBalise!=null && debutBalise.length()>0 && finBalise!=null && finBalise.length()>0) {
this.doc.insertStringSpecial(sel[1],finBalise,tempo);
}
this.setSelection(sel[0],sel[1]);
}
} |
Partager