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
|
public class TestAlignement extends JApplet {
public JTextPane viewer = new JTextPane();
public JEditorPane sourcePane = new JEditorPane();
public JPanel panel = new JPanel();
public HTMLEditorKit k = new HTMLEditorKit();
public HTMLDocument doc = (HTMLDocument) k.createDefaultDocument();
public TestAlignement() {
// Construction de l'Interface Graphique
// Panel en haut avec un label et le champ de saisie
viewer.setContentType("text/html");
viewer.setEditable(true);
viewer.setEditorKit(k);
viewer.setDocument(doc);
String text = "<html>\n" +
"<body>\n" +
"<head></head>"+
"<p align=\"center\">\n" +
"qsdqsd\n" +
"</p>\n" +
"</body>\n" +
"</html>\n";
viewer.setText(text);
sourcePane.setContentType("text/plain");
sourcePane.setEditable(false);
showTree();
// Zone scrollee au centre avec le document
JScrollPane scrollPane = new JScrollPane(viewer);
JScrollPane scrollPane2 = new JScrollPane(sourcePane);
JPanel panel2 = new JPanel();
panel2.add(scrollPane, null);
panel2.add(scrollPane2, null);
panel2.setSize(200, 200);
// Ajout des composants a la fenetre
setJMenuBar(createMenuBar());
panel.add(createToolBar(), BorderLayout.SOUTH);
getContentPane().add(scrollPane2, BorderLayout.SOUTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.NORTH);
}
public JMenuBar createMenuBar() {
JMenuBar menubar = new JMenuBar();
JMenu color = new JMenu("Color");
color.add(new StyledEditorKit.ForegroundAction("Noir", Color.black));
color.add(new StyledEditorKit.ForegroundAction("Bleu", Color.blue));
color.add(new StyledEditorKit.ForegroundAction("Rouge", Color.red));
color.add(new StyledEditorKit.ForegroundAction("Jaune", Color.yellow));
menubar.add(color);
return menubar;
}
public JToolBar createToolBar() {
JToolBar bar = new JToolBar();
JButton boldButton = new JButton();
JButton italicButton = new JButton();
JButton underlineButton = new JButton();
JButton colorButton = new JButton();
JButton cutButton = new JButton();
JButton copyButton = new JButton();
JButton pasteButton = new JButton();
JButton leftButton = new JButton();
JButton centerButton = new JButton();
JButton rightButton = new JButton();
JButton testButton = new JButton();
JButton linkButton = new JButton();
JButton imageButton = new JButton();
JButton h1Button = new JButton();
JButton h2Button = new JButton();
JButton h3Button = new JButton();
Action a = viewer.getActionMap().get("font-bold");
if (a != null) {
boldButton = bar.add(a);
boldButton.setText("G");
}
a = viewer.getActionMap().get("font-italic");
if (a != null) {
italicButton = bar.add(a);
italicButton.setText("I");
}
a = viewer.getActionMap().get("font-underline");
if (a != null) {
underlineButton = bar.add(a);
underlineButton.setText("S");
}
bar.addSeparator();
a = viewer.getActionMap().get(StyledEditorKit.cutAction);
if (a != null) {
cutButton = bar.add(a);
cutButton.setText("X");
}
a = viewer.getActionMap().get(StyledEditorKit.copyAction);
if (a != null) {
copyButton = bar.add(a);
copyButton.setText("C");
}
a = viewer.getActionMap().get(StyledEditorKit.pasteAction);
if (a != null) {
pasteButton = bar.add(a);
pasteButton.setText("V");
}
bar.addSeparator();
a = new StyledEditorKit.AlignmentAction("left", 0);
if (a != null) {
leftButton = bar.add(a);
leftButton.setText("<");
}
a = new StyledEditorKit.AlignmentAction("center", 1);
if (a != null) {
centerButton = bar.add(a);
centerButton.setText("-");
}
a = new StyledEditorKit.AlignmentAction("right", 2);
if (a != null) {
rightButton = bar.add(a);
rightButton.setText(">");
}
testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showTree();
}
});
bar.add(testButton);
testButton.setText("Show Tree");
return bar;
}
@Override
public void init() {
setSize(600, 600);
setVisible(Boolean.TRUE);
}
public void showTree() {
Reader reader = null;
char[] chars = getSource();
reader = new CharArrayReader(chars);
Document docum = new PlainDocument();
try {
sourcePane.getEditorKit().read(reader, docum, 0);
} catch (Exception ignoredForNow) {
}
sourcePane.setDocument(docum);
}
public char[] getSource() {
CharArrayWriter writer = null;
writer = new CharArrayWriter();
try {
viewer.getEditorKit().write(writer, viewer.getDocument(), 0, viewer.getDocument().getLength());
} catch (Exception ignoredForNow) {
}
return writer.toCharArray();
}
public String getTree() {
return sourcePane.getText();
}
}[/INDENT] |
Partager