[JTextPane] mauvais alignement en rtf
Bonjour,
j'ai implémenté un composant qui hérite de JTextPane, ce composant me permet de mettre en forme mon texte, cette mise en forme est plutot simple, changement de police, de taille, gras, italique etc...et alignement (gauche, droit, centré, justifié)
au niveau de l'affichage du composant, tout se passe bien, c'est juste au niveau de la sauvegarde, quand je sauvegarde ca dans un fichier rtf, ce dernier ne prend plus en compte l'alignement.
Tout est bon sauf l'alignement.
Voici le code de mon composant, qu'en pensez vous ?
Code:
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
| public class JTextPane extends javax.swing.JTextPane {
private static final long serialVersionUID = -8905324578389242069L;
public transient Style _def;
public transient Style _style;
private RTFEditorKit _rtfEditorKit;
public JTextPane() {
super();
this.initialize();
}
public JTextPane(StyledDocument doc) {
super(doc);
this.initialize();
}
private void initialize() {
this._def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
this._style = this.addStyle("default", this._def);
this._rtfEditorKit = new RTFEditorKit();
this.setEditorKitForContentType("text/rtf", this._rtfEditorKit);
}
...
public void setAlignment(int alignment) {
StyleConstants.setAlignment(this._style, alignment);
this.setCharacterAttributes(this._style, true);
Document doc = this.getDocument();
int length = this.getSelectionEnd() - this.getSelectionStart();
System.out.println("position = "+this.getSelectionStart()+" => "+length);
((StyledDocument) doc).setParagraphAttributes(this.getSelectionStart(),length,this._style,false);
}
public void saveAs(File dest) throws IOException, BadLocationException {
FileOutputStream fileOutput = new FileOutputStream(dest);
this._rtfEditorKit.write(fileOutput, this.getDocument(), 0, this.getDocument().getLength());
fileOutput.close();
}
public void open(File src) throws IOException, BadLocationException {
this.setText("");
FileInputStream fileInput = new FileInputStream(src);
this._rtfEditorKit.read(fileInput, this.getDocument(), 0);
fileInput.close();
}
} |