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
|
package test;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JButton;
import mcb.StyleDeTexte;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import javax.swing.JLabel;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class Test_HTMLKit extends JFrame
{
private static final long serialVersionUID = 1L;
protected JTextPane MyText;
protected JComboBox Fonts, Sizes;
protected JButton See,
Bold,
Finish,
Italic;
public HTMLEditorKit htmlKit = new HTMLEditorKit();
public HTMLDocument htmlDoc = null;
public static Style mainStyle = null;
protected String fontList[];
protected String fontSizes[] = {"8","10","11","12","14","16","18", "20","24","30","36","40","48","60"};
protected StyleDeTexte defaultStyle = new StyleDeTexte();
public Test_HTMLKit()
{
getContentPane().setLayout(null);
MyText = new JTextPane();
MyText.setBounds(224, 94, 306, 218);
this.setSize(580,413);
Fonts = new JComboBox();
Fonts.setBounds(25, 27, 187, 20);
getContentPane().add(Fonts);
See = new JButton("See HTML code ");
See.setBounds(43, 253, 130, 23);
Finish = new JButton("Exit");
Finish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
Finish.setBounds(43, 287, 130, 23);
Sizes = new JComboBox();
Sizes.setBounds(299, 27, 62, 20);
JLabel lblFontFamlies = new JLabel("Font Families");
lblFontFamlies.setBounds(74, 11, 99, 14);
JLabel lblFontSizes = new JLabel("Font sizes");
lblFontSizes.setBounds(299, 11, 99, 14);
Bold = new JButton("Bold");
Bold.setBounds(25, 58, 67, 23);
Italic = new JButton("Italic");
Italic.setBounds(25, 106, 67, 23);
getContentPane().add(MyText);
getContentPane().add(See);
getContentPane().add(Finish);
getContentPane().add(Sizes);
getContentPane().add(lblFontFamlies);
getContentPane().add(lblFontSizes);
getContentPane().add(Bold);
getContentPane().add(Italic);
this.setLocationRelativeTo(null);
initialize();
setVisible(true);
}
private void initialize()
{
// fill up the combo box
fontList = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
int pos = 0;
for (int i = 0; i < fontList.length; i++)
{
String wk = fontList[i];
Fonts.addItem(wk);
if (wk.equals("Garamond")) pos = i;
}
Fonts.setSelectedIndex(pos);
for (int i = 0; i < fontSizes.length; i++)
{
String wk = fontSizes[i];
Sizes.addItem(wk);
if (wk == "12") pos = i;
}
Sizes.setSelectedIndex(pos);
MyText.setBorder( BorderFactory.createLineBorder(Color.RED) );
initStyle() ;
MyText.setText("<html><head></head><body>Maître Corbeau sur un arbre, perché <br>Tenait en son bec un fromage.<br>" +
"Maître Renard par l'odeur, alléché,<br> lui tint à peu près ce langage. ");
définirListeners();
}
private void définirListeners()
{
Fonts.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) { clickOnFonts();}
});
Sizes.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {clickOnSIzes();}
});
Bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Action action = new StyledEditorKit.BoldAction();
action.actionPerformed(null);
}
});
Italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Action action = new StyledEditorKit.ItalicAction();
action.actionPerformed(null);
}
});
See.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
displayHTMLode();
}
});
}
public boolean testSelection()
{
if (MyText.getSelectedText() == null ) return false;
// int selStart = MyText.getSelectionStart();
// int selEnd = MyText.getSelectionEnd();
return true;
}
public void clickOnFonts()
{
if(!testSelection()) return;
String curFontName = (String) Fonts.getSelectedItem();
Action FontFamily = new StyledEditorKit.FontFamilyAction(curFontName, curFontName);
FontFamily.actionPerformed(null);
// restorerZoneSelectionnée();
}
public void clickOnSIzes()
{
if(!testSelection()) return;
String wk = (String) Sizes.getSelectedItem();
int curFontSize = Integer.parseInt(wk);
Action FontSize = new StyledEditorKit.FontSizeAction("" + curFontSize, curFontSize);
FontSize.actionPerformed(null);
// restorerZoneSelectionnée();
}
private void displayHTMLode()
{
try
{
OutputStream writer = new ByteArrayOutputStream();
htmlDoc = (HTMLDocument) MyText.getDocument();
int le = htmlDoc.getLength();
htmlKit.write(writer, htmlDoc, 0, le) ;
String texteFinal = writer.toString();
System.out.println(texteFinal);
JOptionPane.showMessageDialog(this, texteFinal );
return;
}
catch (Exception ex) { System.out.println(ex.toString());}
return;
}
public void initStyle()
{
MyText.setEditorKit(new StyledEditorKit());
MyText.setContentType("text/html");
htmlDoc = (HTMLDocument) MyText.getStyledDocument();
MutableAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setBold(attributes , true);
StyleConstants.setFontSize(attributes, 14);
StyleConstants.setFontFamily(attributes, "Garamond");
StyleConstants.setItalic(attributes, false);
StyleConstants.setAlignment(attributes,StyleConstants.ALIGN_RIGHT );
StyleConstants.setUnderline(attributes, false);
StyleConstants.setRightIndent(attributes, 0F);
StyleConstants.setLeftIndent(attributes, 0F);
StyleConstants.setForeground(attributes, Color.RED);
StyleConstants.setSubscript(attributes, false);
StyleConstants.setSuperscript(attributes, false);
StyleConstants.setLineSpacing(attributes, 1);
StyleConstants.setSpaceBelow(attributes, 1F);
StyleConstants.setSpaceAbove(attributes, 1F);
htmlDoc.setCharacterAttributes(0, htmlDoc.getLength(), attributes, true);
MyText.setDocument(htmlDoc);
}
public static void main (String[] arg)
{
new Test_HTMLKit();
}
} |
Partager