Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/chatGUI/ChatController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chatGUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javaChat.ClientConnection;

/**
*
* @author cleanrun
*/
public class ChatController implements ActionListener {

private ChatView view;
private ClientConnection client = null;

public ChatController() {
view = new ChatView();
view.setVisible(true);
view.addListener(this);
client = null;
}

public class WriteOutput extends Thread {

public void run() {
try {
String inputan;

while ((inputan = client.readStream()) != null) {
view.setTxAreaChat(inputan);
}
} catch (Exception e) {
System.out.println("Error : " + e.getMessage());
}
}
}


public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == view.getTxFieldChat()) {
if (client == null) {
try {
client = new ClientConnection();
String ip = view.getStringChat();
client.connect(ip);

WriteOutput w = new WriteOutput();
w.start();
} catch (Exception e) {
System.out.println("Error : " + e.getMessage());
}
} else {
String input = view.getStringChat();
client.writeStream(input);
}
view.setTxFieldChat("");
}
}
}
69 changes: 69 additions & 0 deletions src/chatGUI/ChatView.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>

<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jScrollPane1" pref="380" max="32767" attributes="0"/>
<Component id="txFieldChat" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="243" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="txFieldChat" pref="29" max="32767" attributes="0"/>
<EmptySpace min="-2" pref="6" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextArea" name="txAreaChat">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="columns" type="int" value="20"/>
<Property name="rows" type="int" value="5"/>
<Property name="text" type="java.lang.String" value="Input Server IP Address : "/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JTextField" name="txFieldChat">
</Component>
</SubComponents>
</Form>
130 changes: 130 additions & 0 deletions src/chatGUI/ChatView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chatGUI;

import java.awt.event.ActionListener;
import javax.swing.JTextArea;

/**
*
* @author cleanrun
*/
public class ChatView extends javax.swing.JFrame {

/**
* Creates new form ChatView
*/
public ChatView() {
initComponents();
}

public void setTxAreaChat(String message) {
txAreaChat.append(message + "\n");
}

public Object getTxFieldChat() {
return txFieldChat;
}

public String getStringChat() {
return txFieldChat.getText();
}


public void setTxFieldChat(String message) {
this.txFieldChat.setText(message);
}

public void addListener(ActionListener e){
txFieldChat.addActionListener(e);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
txAreaChat = new javax.swing.JTextArea();
txFieldChat = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

txAreaChat.setEditable(false);
txAreaChat.setColumns(20);
txAreaChat.setRows(5);
txAreaChat.setText("Input Server IP Address : ");
jScrollPane1.setViewportView(txAreaChat);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addComponent(txFieldChat))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txFieldChat, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
.addGap(6, 6, 6))
);

pack();
}// </editor-fold>//GEN-END:initComponents

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChatView().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txAreaChat;
private javax.swing.JTextField txFieldChat;
// End of variables declaration//GEN-END:variables
}
58 changes: 58 additions & 0 deletions src/consoleApp/ConsoleApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package consoleApp;

import javaChat.ClientConnection;

public class ConsoleApplication extends Thread {

private ClientConnection client;

public class ReadInput extends Thread {

public void run() {
try {
String inputKeyboard;
do {
System.out.println(">> ");
inputKeyboard = client.inputString();
client.writeStream(inputKeyboard);
} while (!inputKeyboard.equals("quit"));
client.disconnect();
} catch (Exception e) {
System.out.println(" Error :" + e.getMessage());
}

}
}

public class WriteInput extends Thread {

public void run() {
try {
String inputan;
while ((inputan = client.readStream()) != null) {
System.out.println(inputan);
System.out.println(">> ");
}
} catch (Exception e) {
System.out.println(" Error :" + e.getMessage());
}
}
}

public void startChat() {
try {
client = new ClientConnection();
System.out.println("input server IP : ");
String ip = client.inputString();
client.connect(ip);

ReadInput in = new ReadInput();
WriteInput out = new WriteInput();
in.start();
out.start();
} catch (Exception e) {
System.out.println("Error : " + e.getMessage());
}
}

}
41 changes: 41 additions & 0 deletions src/consoleApp/connectionthread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package consoleApp;

import java.io.IOException;
import java.net.Socket;
import javaChat.Connection;

public class connectionthread extends Thread{
private Socket client;
private Connection connection;

public connectionthread(Socket newClient) throws IOException{
this.client = newClient;
connection = new Connection(client);
}

public void run(){
try{
System.out.println("--------------");
System.out.println("new client connected");
System.out.println("client information : ");
System.out.println(connection.getClientInformation());

String inputan;
String message;
while ((inputan = connection.readStream()) != null && !inputan.equals("quit")){
message = "Client " + connection.getIpClient()
+ "said : " + inputan;
System.out.println(message);
connection.sendToAll(message);
}
message = "Clien from IP : " + connection.getIpClient()
+ "Quit the chat room";
System.out.println(message);
connection.sendToAll(message);
connection.disconnect();
} catch (IOException ex){
System.out.println("Error : " + ex);}
}
}


11 changes: 11 additions & 0 deletions src/driver/DriverClientConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package driver;

import consoleApp.ConsoleApplication;

public class DriverClientConsole {
public static void main(String[] args) {
ConsoleApplication cpp = new ConsoleApplication();
cpp.startChat();
}

}
9 changes: 9 additions & 0 deletions src/driver/DriverClientGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package driver;

import chatGUI.ChatController;

public class DriverClientGui {
public static void main(String[] args) {
ChatController cc = new ChatController();
}
}
Loading