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
| package org.janitrix.ui;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class AdvancedTextPane extends JTextPane {
//CLASS VAR.
static final long serialVersionUID = 0;
//PRIVATE VAR.
private StyledDocument styledDoc = getStyledDocument();
//PROTECTED VAR.
protected float COMPONENT_ALIGNEMENT = 0.82f;
/**
* Invoked for generate a style name.
* @param bold
* @param italic
* @param underline
* @param size
* @param color
* @return
*/
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();
}
/**
* Invoked for create and bet back the style define by the parameters.
* @param bold
* @param italic
* @param underline
* @param size
* @param color
* @return
*/
private Style getStyle(boolean bold, boolean italic, boolean underline, int size, Color color) {
String styleName = AdvancedTextPane.getNameStyle(bold, italic, underline, size, color);
Style style = styledDoc.getStyle(styleName);
if(style != null) {
return style;
}else {
style = styledDoc.addStyle(styleName, styledDoc.getStyle(StyleContext.DEFAULT_STYLE));
StyleConstants.setBold(style, bold);
StyleConstants.setItalic(style, italic);
StyleConstants.setUnderline(style, underline);
StyleConstants.setFontSize(style, size);
StyleConstants.setForeground(style, color);
return style;
}
}
/**
* Invoked for add a text to the end of the text pane.
* @param texte
* @param bold
* @param italic
* @param underline
* @param size
* @param color
*/
public synchronized void insertTextEnd(String texte, boolean bold,
boolean italic, boolean underline, int size, Color color) {
try {
styledDoc.insertString(styledDoc.getLength(),
texte, getStyle(bold, italic, underline, size, color));
setCaretPosition(styledDoc.getLength());
}catch(Exception e) {}
}
/**
* Invoked for insert a icon at the selection space.
* @param icon
*/
public synchronized void insertIconSelect(Icon icon) {
insertIcon(icon);
}
/**
* Invoked for add a icon to the end of the text pane.
* @param icon
*/
public synchronized void insertIconEnd(Icon icon) {
select(styledDoc.getLength(), styledDoc.getLength());
insertIconSelect(icon);
}
/**
* Invoked for insert a component at the selection place.
* @param c
*/
public synchronized void insertComponentSelect(JComponent c) {
c.setAlignmentY(COMPONENT_ALIGNEMENT);
insertComponent(c);
}
/**
* Invoked for add a component to the end of the text pane.
* @param c
*/
public synchronized void insertComponentEnd(JComponent c) {
select(styledDoc.getLength(), styledDoc.getLength());
insertComponentSelect(c);
}
} |
Partager