Skip to content

Commit 1b895be

Browse files
committed
ajuste
1 parent 70afef1 commit 1b895be

File tree

1 file changed

+79
-89
lines changed

1 file changed

+79
-89
lines changed

src/br/feevale/TelaChat.java

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -17,175 +17,165 @@
1717
import org.json.JSONException;
1818
import org.json.JSONObject;
1919

20+
2021
public class TelaChat extends JFrame {
2122

2223
/**
2324
*
2425
*/
2526
private static final long serialVersionUID = 1L;
2627

27-
public enum TpTela {
28-
SERVIDOR, CLIENTE
29-
}
30-
31-
private JTextArea timeline;
28+
public enum TpTela { SERVIDOR, CLIENTE }
29+
private JTextArea log;
3230
private JTextField mensagem;
33-
31+
3432
private Socket socket;
3533
private String meuNome;
3634
private String outroNome;
37-
38-
public TelaChat(Socket socket, TpTela tipo, String meuNome)
39-
throws JSONException {
40-
35+
36+
public TelaChat( Socket socket, TpTela tipo, String meuNome ) throws JSONException {
37+
4138
this.socket = socket;
4239
this.meuNome = meuNome;
43-
44-
if (tipo == TpTela.SERVIDOR) {
45-
setBounds(10, 80, 500, 400);
46-
setTitle("Servidor");
40+
41+
if( tipo == TpTela.SERVIDOR ) {
42+
setBounds( 10, 80, 500, 400 );
43+
setTitle( "Servidor" );
4744
} else {
48-
setBounds(515, 80, 500, 400);
49-
setTitle("Cliente");
45+
setBounds( 515, 80, 500, 400 );
46+
setTitle( "Cliente" );
5047
}
48+
49+
setLayout( null );
5150

52-
setLayout(null);
53-
54-
timeline = new JTextArea();
55-
timeline.setLineWrap(true);
56-
timeline.setEditable(false);
57-
58-
JScrollPane sp = new JScrollPane(timeline);
59-
sp.setBounds(10, 10, 470, 300);
60-
getContentPane().add(sp);
61-
51+
log = new JTextArea();
52+
log.setLineWrap( true );
53+
log.setEditable( false );
54+
55+
JScrollPane sp = new JScrollPane( log );
56+
sp.setBounds( 10, 10, 470, 300 );
57+
getContentPane().add( sp );
58+
6259
mensagem = new JTextField();
63-
mensagem.setBounds(10, 320, 470, 23);
64-
getContentPane().add(mensagem);
65-
66-
mensagem.addKeyListener(new KeyListener() {
60+
mensagem.setBounds( 10, 320, 470, 23 );
61+
getContentPane().add( mensagem );
62+
63+
mensagem.addKeyListener( new KeyListener() {
64+
6765
@Override
6866
public void keyTyped(KeyEvent e) {
69-
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
67+
68+
if( e.getKeyChar() == KeyEvent.VK_ENTER ) {
7069
enviaMensagem();
7170
}
7271
}
73-
72+
7473
@Override
75-
public void keyReleased(KeyEvent e) {
76-
}
77-
74+
public void keyReleased(KeyEvent e) {}
75+
7876
@Override
79-
public void keyPressed(KeyEvent e) {
80-
}
81-
});
82-
83-
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
84-
setVisible(true);
85-
86-
System.out.println("Iniciando...");
77+
public void keyPressed(KeyEvent e) {}
78+
} );
8779

88-
new ReadSocket().start();
80+
setDefaultCloseOperation( DISPOSE_ON_CLOSE );
81+
setVisible( true );
8982

83+
System.out.println( "Iniciando...");
84+
new LeitorDeSocket().start();
85+
9086
try {
91-
Thread.sleep(100);
92-
} catch (InterruptedException e1) {
93-
}
94-
87+
Thread.sleep( 100 );
88+
} catch (InterruptedException e1) {}
89+
9590
enviaHandshake();
9691
}
9792

9893
private void enviaHandshake() throws JSONException {
9994

10095
JSONObject obj = new JSONObject();
101-
obj.put("tpTransacao", 1);
102-
obj.put("meuNome", meuNome);
103-
104-
enviaTransacao(obj);
96+
obj.put( "tpTransacao", 1 );
97+
obj.put( "meuNome", meuNome );
98+
99+
enviaTransacao( obj );
105100
}
106101

107102
private void enviaTransacao(JSONObject obj) {
108103

109104
try {
110105
OutputStream os = socket.getOutputStream();
111-
DataOutputStream dos = new DataOutputStream(os);
106+
DataOutputStream dos = new DataOutputStream( os );
112107

113-
dos.writeUTF(obj.toString());
108+
dos.writeUTF( obj.toString() );
114109
os.flush();
115110
} catch (IOException e) {
116111
e.printStackTrace();
117112
}
118-
113+
119114
}
120115

121-
122116
protected void enviaMensagem() {
123117

124118
String mensagemAEnviar = mensagem.getText();
125-
126-
mensagem.setText(null);
119+
120+
mensagem.setText( null );
127121
mensagem.requestFocusInWindow();
128-
122+
129123
try {
130124
JSONObject obj = new JSONObject();
131-
obj.put("tpTransacao", 2);
132-
obj.put("msg", mensagemAEnviar);
125+
obj.put( "tpTransacao", 2 );
126+
obj.put( "msg", mensagemAEnviar );
133127

134-
enviaTransacao(obj);
135-
136-
escreveNoLog(meuNome + ": " + mensagemAEnviar);
137-
} catch (Exception e) {
128+
enviaTransacao( obj );
129+
130+
escreveNoLog( meuNome + ": " + mensagemAEnviar );
131+
} catch ( Exception e) {
138132
e.printStackTrace();
139133
}
140134
}
141135

142136

143-
144-
private class ReadSocket extends Thread {
145-
137+
private class LeitorDeSocket extends Thread {
138+
146139
@Override
147140
public void run() {
148141
try {
149142
InputStream is = socket.getInputStream();
150-
DataInputStream dis = new DataInputStream(is);
151-
152-
while (isVisible()) {
143+
DataInputStream dis = new DataInputStream( is );
144+
145+
while( isVisible() ) {
153146

154147
String mensagem = dis.readUTF();
155148

156-
JSONObject obj = new JSONObject(mensagem);
157-
158-
int tpTransacao = obj.getInt("tpTransacao");
149+
JSONObject obj = new JSONObject( mensagem );
159150

160-
switch (tpTransacao) {
151+
int tpTransacao = obj.getInt( "tpTransacao" );
152+
153+
switch( tpTransacao ) {
161154

162-
case 1:
163-
trataHandshake(obj);
164-
break;
165-
case 2:
166-
trataMensagem(obj);
167-
break;
155+
case 1: trataHandshake( obj ); break;
156+
case 2: trataMensagem( obj ); break;
168157
}
169158
}
170-
} catch (Exception e) {
159+
} catch ( Exception e) {
171160
e.printStackTrace();
172161
}
173-
}
162+
}
174163
}
175164

176165

177-
private void escreveNoLog(String msg) {
178-
timeline.append(msg);
179-
timeline.append("\n");
166+
167+
private void escreveNoLog( String msg ) {
168+
log.append( msg );
169+
log.append( "\n" );
180170
}
181171

182172
public void trataHandshake(JSONObject obj) throws JSONException {
183-
outroNome = obj.getString("meuNome");
173+
outroNome = obj.getString( "meuNome" );
184174
}
185175

186176
public void trataMensagem(JSONObject obj) throws JSONException {
187177

188-
String mensagem = obj.getString("msg");
189-
escreveNoLog(outroNome + ": " + mensagem);
178+
String mensagem = obj.getString( "msg" );
179+
escreveNoLog( outroNome + ": " + mensagem );
190180
}
191181
}

0 commit comments

Comments
 (0)