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
|
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import java.net.*;
class TC{
JTextPane zonedetexte1,zonedetexte2;
JButton boutton;
JPanel window;
Action boldAction = new StyledEditorKit.BoldAction();
Action underlineAction = new StyledEditorKit.UnderlineAction();
Action italicAction = new StyledEditorKit.ItalicAction();
String name;
String myname;
Socket s;
public TC(Socket _s,String my,String a){
s=_s;
name=a;
myname=my;
createButton();
createWindow();
}
public void createButton(){
boutton = new JButton(name);
boutton.setMargin(new Insets(2,0,0,0));
boutton.setBackground(Color.green);
boutton.setFocusPainted(false);
boutton.setPreferredSize(new Dimension(80,25));
boutton.setMaximumSize(new Dimension(80,25));
boutton.setMinimumSize(new Dimension(80,25));
}
public void createWindow(){
window =new JPanel();
window.setLayout(new BoxLayout(window,BoxLayout.Y_AXIS));
window.add(Box.createRigidArea(new Dimension(0, 10)));
Border loweredbevel;
loweredbevel = BorderFactory.createLoweredBevelBorder();
JScrollPane scroll1,scroll2;
zonedetexte1=new JTextPane();
scroll1=new JScrollPane(zonedetexte1);
scroll1.setPreferredSize(new Dimension(250, 180));
scroll1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll1.setAutoscrolls(true);
zonedetexte1.setEditable(false);
zonedetexte1.setContentType("text/html");
zonedetexte2=new JTextPane();
zonedetexte2.setContentType("text/html");
zonedetexte2.addKeyListener(new KeyListener()
{
int marqueur=0;
public void keyPressed(KeyEvent e){
if(marqueur==1)
{
if(e.getKeyChar()=='e')
{
marqueur=2;
}
else
marqueur=0;
}
else
{
if(e.getKeyChar()==':')
marqueur=1;
else
marqueur=0;
}
}
public void keyReleased(KeyEvent e){
if(marqueur==2)
{
String message=decoupe(zonedetexte2.getText());
zonedetexte2.setText(message);
marqueur=0;
}
//System.out.println(zonedetexte2.getText());
}
public void keyTyped(KeyEvent e){
}
}
);
scroll2=new JScrollPane(zonedetexte2);
scroll2.setPreferredSize(new Dimension(250, 80));
scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll2.setAutoscrolls(true);
scroll1.setBorder(loweredbevel);
scroll2.setBorder(loweredbevel);
window.add(scroll1);
window.add(Box.createRigidArea(new Dimension(0, 4)));
window.add(createPanelchoix());
window.add(Box.createRigidArea(new Dimension(0, 2)));
window.add(scroll2);
JButton envoyer=new JButton("envoyer");
envoyer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(zonedetexte2.getDocument().getLength()!=0)
sendText(RecupereText());
}
}
);
window.add(Box.createRigidArea(new Dimension(100, 5)));
window.add(envoyer);
}
public JComponent createPanelchoix(){
JPanel pan=new JPanel();
String p[]={"Sherif","Comic"};
pan.setLayout(new BoxLayout(pan,BoxLayout.X_AXIS));
JToggleButton italique,gras,souligne;
italique=new JToggleButton(italicAction);
italique.setText("i");
//italique.setIcon(new ImageIcon());
italique.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
zonedetexte2.select(0, zonedetexte2.getDocument().getLength());
}
}
);
italique.setBorderPainted(false);
italique.setFocusPainted(false);
italique.setToolTipText("Ecrire en italique");
italique.setAlignmentX(Component.LEFT_ALIGNMENT);
gras=new JToggleButton(boldAction);
gras.setText(null);
gras.setIcon(new ImageIcon("etoile1.gif"));
gras.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
zonedetexte2.selectAll(); }
}
);
gras.setBorderPainted(false);
gras.setFocusPainted(false);
gras.setToolTipText("Ecrire en gras");
souligne=new JToggleButton(underlineAction);
souligne.setText("S");
//souligne.setIcon(new ImageIcon());
souligne.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
zonedetexte2.select(0, zonedetexte2.getDocument().getLength());
}
}
);
souligne.setBorderPainted(false);
souligne.setFocusPainted(false);
souligne.setToolTipText("Ecrire en souligne");
pan.add(italique);
pan.add(Box.createRigidArea(new Dimension(3, 0)));
pan.add(gras);
pan.add(Box.createRigidArea(new Dimension(3, 0)));
pan.add(souligne);
pan.add(Box.createRigidArea(new Dimension(3, 0)));
JComboBox pol = new JComboBox(p);
pol.setEditable(false);
pan.add(pol);
return pan;
}
public String HtmlParse(String mes){
return mes.substring(mes.indexOf("<body>")+6,mes.indexOf("</body>"));
}
public String decoupe(String message){
String retour= message.replaceAll( (":e"),("<IMG SRC=\"file:///home/jeje/Site/msn/etoile1.gif\">"));
//http://perso.orange.fr/skwi6/etoile1.gif
return retour;
}
dois je mettre zonedetexte2.setText("");
public String RecupereText(){
String text=zonedetexte2.getText();
text+="\n";
zonedetexte2.setText("");
System.out.println(zonedetexte2.getText());
return text;
} |