diff --git a/src/CallCommand.java b/src/CallCommand.java new file mode 100644 index 0000000..0f8de45 --- /dev/null +++ b/src/CallCommand.java @@ -0,0 +1,17 @@ +public class CallCommand extends Command { + + String nick; + + public CallCommand(){ + super(CommandType.CALL); + } + + public CallCommand(String nick) { + super(CommandType.CALL); + this.nick = nick; + } + + public String getNick() { + return nick; + } +} diff --git a/src/CallListener.java b/src/CallListener.java new file mode 100644 index 0000000..c65a7b3 --- /dev/null +++ b/src/CallListener.java @@ -0,0 +1,46 @@ +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +public class CallListener { + private String localNick,remoteNick; + private boolean isBusy,isOnline; + private String remoteIP; + private int remotePort; + private ServerSocket serverSocket; + private Command lastCommand; + private NickCommand nickCommand; + + public CallListener(String localNick){ + try { + serverSocket = new ServerSocket(Protocol.PORT_NUMBER); + } catch (IOException e) { + //asd + } + this.localNick=localNick; + this.isBusy= false; + this.isOnline=true; + } + + public Connection getConnection() throws IOException { + Connection connection = new Connection(serverSocket.accept()); + return connection; + } + + public void setBusy(boolean isBusy) { + this.isBusy = isBusy; + } + + public void setNick(String nick){ + localNick=nick; + } + + public String getRemoteNick(){ + return remoteNick; + } + + public void setOnline(Boolean online){ + isOnline=online; + } +} diff --git a/src/CallListenerThread.java b/src/CallListenerThread.java new file mode 100644 index 0000000..191b2b4 --- /dev/null +++ b/src/CallListenerThread.java @@ -0,0 +1,62 @@ +import java.io.IOException; + +public class CallListenerThread { + /* + + private String localNick; + private CallListener callListener; + private boolean stop; + private String remoteNick; + private Connection remoteConnection; + private IncomingCallForm form; + + + + + public CallListenerThread(String localNick,Boolean isBusy){ + callListener = new CallListener(localNick); + + } + + public void run() { + try { + while (!stop) { + + remoteConnection = callListener.getConnection(); + if (remoteConnection == null) continue; + remoteNick=callListener.getRemoteNick(); + form = new IncomingCallForm(remoteConnection,remoteNick); + System.out.println("AZAZA"); + + } + } + catch(IOException e){ + e.printStackTrace(); + } + + + } + + + public Connection getRemoteConnection(){ + return remoteConnection; + } + + public void setBusy(Boolean isBusy){ + callListener.setBusy(isBusy); + } + + public void setOnline(Boolean online){ + callListener.setOnline(online); + } + + public void setNick(String nick){ + localNick=nick; + callListener.setNick(localNick); + } + + + public void kill() { + stop = true; + }*/ +} diff --git a/src/Caller.java b/src/Caller.java new file mode 100644 index 0000000..7570c8c --- /dev/null +++ b/src/Caller.java @@ -0,0 +1,63 @@ +import java.io.IOException; +import java.net.Socket; + +public class Caller { + private String localNick; + private String remoteIP; + private String remoteNick; + private Command lastCommand; + private NickCommand nickCommand; + private String lastError; + + + public Caller(String localNick,String remoteIP){ + this.localNick=localNick; + this.remoteIP=remoteIP; + + } + + public Connection call() throws IOException { + Connection connection = new Connection(new Socket(remoteIP,Protocol.PORT_NUMBER)); + //Проверка, к тому ли мы подключились. + lastCommand = connection.recieve(); + if (lastCommand.type==CommandType.NICK) { + nickCommand = (NickCommand) lastCommand; + } + else{ + connection.disconnect(); + lastError="Wrong IP"; + UltimateGUI ultimateGUI = new UltimateGUI("Failed to connect"); + return null; + } + + //Проверка, занят ли тот пользователь + remoteNick = nickCommand.getNick(); + if (nickCommand.isBusy()){ + lastCommand = connection.recieve(); + connection.disconnect(); + UltimateGUI busyGUI = new UltimateGUI(remoteNick+" is busy"); + lastError="User is busy"; + remoteNick=null; + + return null; + } + + //Если не занят, то отсылаем ник и ждём подтверждения + + lastCommand = connection.recieve(); + + //Если accept - вернуть connection + if (lastCommand.type==CommandType.ACCEPT) return connection; + else{ + UltimateGUI ultimateGUI = new UltimateGUI(remoteNick+" rejected your call"); + lastError="User rejected your call"; + + connection.disconnect(); + return null; + } + + } + public String getRemoteNick(){ + return remoteNick; + } +} diff --git a/src/Colors.java b/src/Colors.java new file mode 100644 index 0000000..a8c3da5 --- /dev/null +++ b/src/Colors.java @@ -0,0 +1,10 @@ +import java.awt.*; + +public class Colors { + public static final Color mainGreen = new Color(139, 194, 73); + public static final Color darkGreen = new Color(54, 107, 52); + public static final Color midGreen = new Color(103, 159, 66); + public static final Color softGreen = new Color(242, 248, 233); + public static final Color darkGrey = new Color(67, 67, 67); + public static final Color lightGreen = new Color(0xDCECC8); +} diff --git a/src/Command.java b/src/Command.java new file mode 100644 index 0000000..e40beff --- /dev/null +++ b/src/Command.java @@ -0,0 +1,75 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Command { + protected CommandType type ; + + public Command(){} + + public Command(CommandType type){ + this.type=type; + } + + public static Command getCommand(Scanner in){ + String string =""; + if (in.hasNextLine()) { + string = in.nextLine(); + } + else{ + return null; + } + if (string.startsWith(Protocol.ACCEPTED)) return new Command(CommandType.ACCEPT); + if (string.startsWith(Protocol.DISCONNECT)) return new Command(CommandType.DISCONNECT); + if (string.startsWith(Protocol.REJECTED)) return new Command(CommandType.REJECT); + if (string.startsWith(Protocol.GREETING)) return new NickCommand(string); + if (string.startsWith(Protocol.MESSAGE)) return new MessageCommand(Protocol.decode(in.nextLine())); + if (string.startsWith(Protocol.HELLO_SERVER)) return new Command(CommandType.HELLO_SERVER); + if (string.startsWith(Protocol.HELLO_CLIENT)) return new Command(CommandType.HELLO_CLIENT); + + if (string.startsWith(Protocol.CONTACTS)){ + string = string.replace(Protocol.CONTACTS+" ",""); + ArrayList result = new ArrayList(); + String[] tmp = string.split(" "); + for (int i=0;i result = new ArrayList(); + String[] tmp = string.split(" "); + for (int i=0;i onlines = new ArrayList(); + for (String s : tmp){ + onlines.add(Boolean.parseBoolean(s)); + } + return new OnlineCommand(onlines); + } + if (string.startsWith(Protocol.LOGOUT)) return new Command(CommandType.LOGOUT); + + return null; + } + +} + diff --git a/src/CommandType.java b/src/CommandType.java new file mode 100644 index 0000000..a6ca64e --- /dev/null +++ b/src/CommandType.java @@ -0,0 +1,4 @@ +public enum CommandType { + ACCEPT, REJECT, DISCONNECT, NICK, MESSAGE, HELLO_SERVER, HELLO_CLIENT, CALL, LOGIN, SIGNUP, DISCONNECT_FROM_USER, + CONTACTS, CONTACT, ONLINE_CONTACTS, LOGOUT, EMPTYCONTACTS, EMPTYMYCONTACTS, GET_CONTACTS, BUSY, OFFLINE, MY_CONTACTS, GET_MY_CONTACTS +} diff --git a/src/Connection.java b/src/Connection.java new file mode 100644 index 0000000..1369bf0 --- /dev/null +++ b/src/Connection.java @@ -0,0 +1,158 @@ +import java.io.*; +import java.net.Socket; +import java.util.Scanner; + +class Connection{ + private Socket socket; + private Scanner in; + private DataOutputStream out; + private CommandType lastCommand; + + public Connection(Socket socket){ + this.socket=socket; + try { + in = new Scanner(socket.getInputStream()); + out = new DataOutputStream(socket.getOutputStream()); + } catch (IOException e) { + e.printStackTrace(); + //ошибка при попытке создать Connection, как-то показать + } + } + + public void sendServerHello(){ + try { + out.write(new StringBuilder(Protocol.HELLO_SERVER).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.HELLO_CLIENT; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendCall(String nick){ + try { + out.write(new StringBuilder(Protocol.CALL).append(" ").append(nick).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.CALL; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendLogin(String nick,String password){ + try { + out.write(new StringBuilder(Protocol.LOGIN).append(" ").append(nick).append(" ").append(password.hashCode()).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.LOGIN; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendSignUp(String nick,String password){ + try { + out.write(new StringBuilder(Protocol.SIGNUP).append(" ").append(nick).append(" ").append(password.hashCode()).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.SIGNUP; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void accept(){ + try { + out.write(new StringBuilder(Protocol.ACCEPTED).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.ACCEPT; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void reject(){ + try { + + out.write(new StringBuilder(Protocol.REJECTED).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.REJECT; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendMessage(String message){ + try { + out.write(new StringBuilder(Protocol.MESSAGE).append("\n").append(message).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.MESSAGE; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void disconnect(){ + try{ + out.write(new StringBuilder(Protocol.DISCONNECT).append("\n").toString().getBytes("UTF-8")); + socket.close(); + in.close(); + out.close(); + lastCommand=CommandType.DISCONNECT; + }catch (Exception e){ + e.printStackTrace(); + } + } + + public void logout(){ + try { + out.write(new StringBuilder(Protocol.LOGOUT).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.LOGOUT; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void disconnectFromUser(){ + try{ + out.write(new StringBuilder(Protocol.DISCONNECT_FROM_USER).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.DISCONNECT_FROM_USER; + }catch (Exception e){ + e.printStackTrace(); + } + } + + public void getContacts(String regex){ + try { + out.write(new StringBuilder(Protocol.GET_CONTACTS).append(" ").append(regex).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.GET_CONTACTS; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void getMyContacts(){ + try { + out.write(new StringBuilder(Protocol.GET_MY_CONTACTS).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.GET_MY_CONTACTS; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendContacts(String contacts){ + try { + out.write(new StringBuilder(Protocol.CONTACTS).append(contacts).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.CONTACTS; + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void getOnline(String s){ + try { + out.write(new StringBuilder(Protocol.ONLINE_CONTACTS).append(s).append("\n").toString().getBytes("UTF-8")); + lastCommand=CommandType.ONLINE_CONTACTS; + } catch (IOException e) { + e.printStackTrace(); + } + } + + + + + public Command recieve(){ + return Command.getCommand(in); + } +} diff --git a/src/ConnectionObserver.java b/src/ConnectionObserver.java new file mode 100644 index 0000000..433868a --- /dev/null +++ b/src/ConnectionObserver.java @@ -0,0 +1,7 @@ +public abstract class ConnectionObserver { + + public void update(Connection connection){ + + } + +} diff --git a/src/Contact.java b/src/Contact.java new file mode 100644 index 0000000..b639a0b --- /dev/null +++ b/src/Contact.java @@ -0,0 +1,79 @@ + +public class Contact { + private boolean isFav,isOnline; + private String nick; + private ContactsViewModel contactsViewModel; + private Main main; + + + public Contact(ContactsViewModel cvm,String nick){ + this.nick=nick; + isFav=false; + contactsViewModel=cvm; + isOnline=false; + } + + public void setContactsViewModel(ContactsViewModel contactsViewModel) { + this.contactsViewModel = contactsViewModel; + } + + public boolean isFav() { + return isFav; + } + + public void setMain(Main main) { + this.main = main; + } + + public boolean isOnline() { + return isOnline; + } + + public void setOnline(boolean isOnline) { + this.isOnline = isOnline; + } + + public String getNick() { + return nick; + } + + public void setNick(String nick) { + this.nick = nick; + } + + public void changeFav(ContactPanel contactPanel){ + if (isFav){ + contactsViewModel.removeContact(this,contactPanel); + isFav=false; + contactsViewModel.add(this); + } + else { + contactsViewModel.removeContact(this,contactPanel); + isFav=true; + contactsViewModel.add(this); + } + } + + public boolean equals(Object object){ + Contact tmp = (Contact) object; + if (nick.equals(tmp.nick)) return true; + else return false; + } + + public void remove(ContactPanel tmp){ + contactsViewModel.removeContact(this,tmp); + } + + @Override + public int hashCode() { + return nick.hashCode(); + } + + public void setFav(Boolean b){ + isFav=b; + } + + public void sendCall() { + main.call(nick); + } +} diff --git a/src/ContactPanel.java b/src/ContactPanel.java new file mode 100644 index 0000000..d12bd62 --- /dev/null +++ b/src/ContactPanel.java @@ -0,0 +1,101 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.File; +import java.io.IOException; + +public class ContactPanel extends JPanel{ + private Contact contact; + private JLabel label = new JLabel(); + private Font font; + + public ContactPanel(){ + createGUI(); + } + + public ContactPanel(Contact contact){ + this.contact=contact; + createGUI(); + } + + private void createGUI(){ + try { + font = Font.createFont(Font.TRUETYPE_FONT,Main.class.getClassLoader().getResourceAsStream("roboto-thin.ttf")).deriveFont(15f); + } catch (FontFormatException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + setLayout(null); + setBackground(Colors.softGreen); + setMinimumSize(new Dimension(200, 25)); + setPreferredSize(new Dimension(200,25)); + setMaximumSize(new Dimension(200, 25)); + label.setBounds(5, 3, 200, 20); + label.setFont(font); + label.setText(contact.getNick()); + if (contact.isOnline()) label.setIcon(new ImageIcon(Main.class.getResource("/on.png"))); + else label.setIcon(new ImageIcon(Main.class.getResource("/off.png"))); + add(label); + + addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + PopUp popUp = new PopUp(getThis()); + popUp.show(label,e.getX(),e.getY()); + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + setBackground(Colors.mainGreen); + } + + @Override + public void mouseExited(MouseEvent e) { + setBackground(Colors.softGreen); + } + }); + } + + public boolean isFav(){ + return contact.isFav(); + } + + public void update(){ + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + if (contact.isOnline()) label.setIcon(new ImageIcon(Main.class.getResource("/on.png"))); + else label.setIcon(new ImageIcon(Main.class.getResource("/off.png"))); + label.updateUI(); + } + }); + + } + + public String getNick(){ + return contact.getNick(); + } + + public Contact getContact(){ + return contact; + } + + private ContactPanel getThis(){ + return this; + } + + +} diff --git a/src/ContactsCommand.java b/src/ContactsCommand.java new file mode 100644 index 0000000..9991b28 --- /dev/null +++ b/src/ContactsCommand.java @@ -0,0 +1,19 @@ +import java.util.ArrayList; + +public class ContactsCommand extends Command { + + ArrayList arrayList; + + public ContactsCommand(){ + super(CommandType.CONTACTS); + } + + public ContactsCommand(ArrayList tmp){ + super(CommandType.CONTACTS); + arrayList=tmp; + } + + public ArrayList getArrayList() { + return arrayList; + } +} diff --git a/src/ContactsView.java b/src/ContactsView.java new file mode 100644 index 0000000..3168c0c --- /dev/null +++ b/src/ContactsView.java @@ -0,0 +1,73 @@ +import javax.swing.*; +import java.awt.*; +import java.util.*; +import java.util.Timer; + +public class ContactsView extends JPanel{ + + private JPanel contactsP,favouritesP; + + JLabel contactsL = new JLabel("Contacts"); + JLabel favouritesL = new JLabel("Favourites"); + + private ArrayList list = new ArrayList(); + + public ContactsView(){ + createGUI(); + } + + private void createGUI(){ + setBackground(Colors.softGreen); + setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); + + setAlignmentX(Component.LEFT_ALIGNMENT); + add(favouritesL); + add(Box.createVerticalStrut(10)); + + favouritesP = new JPanel(); + favouritesP.setLayout(new BoxLayout(favouritesP,BoxLayout.Y_AXIS)); + favouritesP.setBackground(Colors.softGreen); + favouritesP.setAlignmentX(Component.LEFT_ALIGNMENT); + favouritesP.setAutoscrolls(true); + add(favouritesP); + + + add(Box.createVerticalStrut(10)); + add(contactsL); + add(Box.createVerticalStrut(10)); + + contactsP = new JPanel(); + contactsP.setAutoscrolls(true); + contactsP.setLayout(new BoxLayout(contactsP,BoxLayout.Y_AXIS)); + contactsP.setBackground(Colors.softGreen); + contactsP.setAlignmentX(Component.LEFT_ALIGNMENT); + add(contactsP); + } + + public void addContact(Contact contact){ + ContactPanel tmp = new ContactPanel(contact); + list.add(tmp); + if (contact.isFav()) favouritesP.add(tmp); + else contactsP.add(tmp); + updateUI(); + } + + public void delete(ContactPanel contact){ + if (contact.isFav()) favouritesP.remove(contact); + else contactsP.remove(contact); + list.remove(contact); + updateUI(); + } + + public void setLabelFont(Font font){ + contactsL.setFont(font); + favouritesL.setFont(font); + } + + public void onlineUpdate() { + for (ContactPanel cp : list){ + cp.update(); + } + updateUI(); + } +} diff --git a/src/ContactsViewModel.java b/src/ContactsViewModel.java new file mode 100644 index 0000000..968c094 --- /dev/null +++ b/src/ContactsViewModel.java @@ -0,0 +1,57 @@ +import java.util.*; + + +public class ContactsViewModel{ + ContactsView contactsView; + ArrayList contactList = new ArrayList(); + + + public ContactsViewModel(ContactsView contactsView){ + this.contactsView=contactsView; + } + + public void setContactsView(ContactsView cv){ + contactsView=cv; + } + + public void removeContact(Contact contact, ContactPanel contactPanel){ + contactList.remove(contact); + contactsView.delete(contactPanel); + } + + public Collection getList(){ + return contactList; + } + + + public void add(Contact contact){ + if (contactList.contains(contact)) return; + contactList.add(contact); + contactsView.addContact(contact); + + } + + + public String getContacts() { + StringBuilder stringBuilder = new StringBuilder(); + for (Contact contact : contactList){ + stringBuilder.append(" ").append(contact.getNick()).append(" ").append(contact.isFav()); + } + return stringBuilder.toString(); + } + + public String getOnlineCintacts(){ + StringBuilder stringBuilder = new StringBuilder(); + for (Contact contact : contactList){ + stringBuilder.append(" ").append(contact.getNick()); + } + return stringBuilder.toString(); + } + + public void updateOnline(ArrayList onlines){ + for (int i=0;i Messagelist = new ArrayList(); + JTextArea historyView; + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("[kk:mm:ss dd.MM.yy]"); + + + public HistoryViewModel(JTextArea historyView){ + this.historyView = historyView; + } + + private String composeString(String message){ + System.out.println(message); + return new StringBuilder(simpleDateFormat.format(new Date(System.currentTimeMillis()))).append(" ").append(message).toString(); + } + + public void addRemoteMessage(String message){ + System.out.println(remoteNick); + String tmp = new StringBuilder(remoteNick).append(" ").append(composeString(message)).toString(); + Messagelist.add(tmp); + historyView.append(tmp+"\n"); + + } + + public void addLocalMessage(String message){ + System.out.println(localNick); + String tmp = new StringBuilder(localNick).append(" ").append(composeString(message)).toString(); + Messagelist.add(tmp); + historyView.append(tmp+"\n"); + + } + + public void addSystemMessage(String message){ + Messagelist.add(composeString(message)); + historyView.append(composeString(message)+"\n"); + } + + + public String get(int i){ + return Messagelist.get(i); + } + + public void writeHistoryFile(){ + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("[kk.mm.ss dd.MM.yy]"); + FileWriter out = null; + try { + out = new FileWriter(new StringBuilder(simpleDateFormat.format( + new Date(System.currentTimeMillis()))).append(remoteNick).append(".txt").toString()); + for (String string : Messagelist){ + out.write(string+"\n"); + out.flush(); + } + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public void setHistoryView(JTextArea historyView) { + this.historyView = historyView; + } + + public void clearView(){ + Messagelist = new ArrayList(); + historyView.setText(""); + } + + public void setLocalNick(String localNick) { + this.localNick = localNick; + } + + public void setRemoteNick(String remoteNick) { + this.remoteNick = remoteNick; + } +} diff --git a/src/LogInWindow.java b/src/LogInWindow.java new file mode 100644 index 0000000..2dbb896 --- /dev/null +++ b/src/LogInWindow.java @@ -0,0 +1,198 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.File; +import java.io.IOException; + +public class LogInWindow extends JFrame { + JTextArea login; + JPasswordField password; + + JLabel signupBtn, signinBtn; + + public LogInWindow(){ + + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + setLocationRelativeTo(null); + setSize(new Dimension(300,200)); + setLocation(getX()-150,getY()-100); + setAlwaysOnTop(true); + setResizable(false); + createGUI(); + setVisible(true); + } + + private void createGUI(){ + JPanel panel = new JPanel(); + panel.setLayout(null); + panel.setBackground(Colors.softGreen); + panel.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Colors.midGreen)); + add(panel); + + Font font = null; + try { + font = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("roboto-thin.ttf")).deriveFont(15f); + } catch (FontFormatException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + login = new JTextArea(); + login.setFont(font); + login.setForeground(Color.lightGray); + login.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Colors.midGreen)); + login.setBackground(Color.white); + login.setText("Login"); + login.setBounds(55, 30, 182, 30); + login.getDocument().putProperty("filterNewlines", Boolean.TRUE); + + login.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + if (login.getText().equals("Login")) { + login.setForeground(Color.black); + login.setText(""); + password.setForeground(Color.black); + password.setText(""); + signinBtn.setEnabled(true); + } + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } + }); + login.addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent e) { + if (login.getText().equals("Login")) { + login.setForeground(Color.black); + login.setText(""); + password.setForeground(Color.black); + password.setText(""); + signinBtn.setEnabled(true); + } + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyReleased(KeyEvent e) { + + } + }); + + panel.add(login); + + password = new JPasswordField(); + password.setFont(font); + password.setBounds(55,65,182,30); + password.setForeground(Color.lightGray); + password.setBackground(Color.white); + password.setText("password"); + password.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Colors.midGreen)); + panel.add(password); + + password.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + String tmp =""; + for ( char c : password.getPassword() ){ + tmp=tmp+c; + } + System.out.println(tmp); + if (tmp.equals("password")){ + password.setForeground(Color.black); + password.setText(""); + login.setForeground(Color.black); + login.setText(""); + signinBtn.setEnabled(true); + } + + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } + }); + + signinBtn = new JLabel(""); + signinBtn.setIcon(new ImageIcon(Main.class.getResource("/signingN.png"))); + signinBtn.setDisabledIcon(new ImageIcon(Main.class.getResource("/signingD.png"))); + signinBtn.setBounds(40,105,97,27); + signinBtn.setEnabled(false); + panel.add(signinBtn); + + signupBtn = new JLabel(""); + signupBtn.setIcon(new ImageIcon(Main.class.getResource("/signuptN.png"))); + signupBtn.setDisabledIcon(new ImageIcon(Main.class.getResource("/signuptD.png"))); + signupBtn.setBounds(157,105,97,27); + signupBtn.setEnabled(true); + panel.add(signupBtn); + + } + + public JLabel getSignupBtn() { + return signupBtn; + } + + public JLabel getSigninBtn() { + return signinBtn; + } + + public JTextArea getLogin() { + return login; + } + + public JPasswordField getPassword() { + return password; + } + + + public static void main(String[] args) { + LogInWindow asd = new LogInWindow(); + } + + + +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..92fb873 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,987 @@ +import javax.swing.*; +import javax.swing.Timer; +import java.awt.*; +import java.awt.event.*; +import java.io.IOException; +import java.util.*; + +public class Main extends JFrame { + + MainGUI mainFrame; + LogInWindow logInWindow; + SignUpWindow signUpWindow; + HistoryViewModel historyViewModel; + ContactsViewModel contactsViewModel; + Connection connection; + String login; + public static boolean isConnected=false; + + private String remoteNick=null; + + public Main() { + try { + connection = ServerCaller.call(); + } catch (IOException e) { + e.printStackTrace(); + } + + if (connection==null) { + UltimateGUI ultimateGUI = new UltimateGUI("Could not connect to main server"); + } + else { + createMainFrame(); + createLogInWindow(); + createSignUpWindow(); + } + + } + + public Main getThis(){ + return this; + } + + private void createLogInWindow(){ + logInWindow = new LogInWindow(); + + logInWindow.getSigninBtn().addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + logInWindow.getSigninBtn().setIcon(new ImageIcon(Main.class.getResource("/signingP.png"))); + String tmp = ""; + for (char c : logInWindow.getPassword().getPassword()){ + tmp+=c; + } + tmp = "" + tmp.hashCode(); + connection.sendLogin(logInWindow.getLogin().getText(),tmp); + Command command = connection.recieve(); + System.out.println(command.type); + if (command.type==CommandType.ACCEPT) { + mainFrame.setLocalNick(logInWindow.getLogin().getText()); + historyViewModel = new HistoryViewModel(mainFrame.getHistoryView()); + contactsViewModel = new ContactsViewModel(mainFrame.getContactsView()); + historyViewModel.setLocalNick(logInWindow.getLogin().getText()); + logInWindow.setVisible(false); + signUpWindow.dispose(); + mainFrame.setVisible(true); + CommandListenerThread clt = new CommandListenerThread(connection); + clt.start(); + + } + else{ + UltimateGUI ultimateGUI = new UltimateGUI("Wrong login\\password or such user is already online"); + } + } + + @Override + public void mouseReleased(MouseEvent e) { + logInWindow.getSigninBtn().setIcon(new ImageIcon(Main.class.getResource("/signingN.png"))); + } + + @Override + public void mouseEntered(MouseEvent e) { + logInWindow.getSigninBtn().setIcon(new ImageIcon(Main.class.getResource("/signingH.png"))); + } + + @Override + public void mouseExited(MouseEvent e) { + logInWindow.getSigninBtn().setIcon(new ImageIcon(Main.class.getResource("/signingN.png"))); + } + }); + + logInWindow.getSignupBtn().addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + logInWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptP.png"))); + logInWindow.dispose(); + signUpWindow.setVisible(true); + } + + @Override + public void mouseReleased(MouseEvent e) { + logInWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptN.png"))); + } + + @Override + public void mouseEntered(MouseEvent e) { + logInWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptH.png"))); + } + + @Override + public void mouseExited(MouseEvent e) { + logInWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptN.png"))); + } + }); + + logInWindow.addWindowListener(new WindowListener() { + @Override + public void windowOpened(WindowEvent e) { + + } + + @Override + public void windowClosing(WindowEvent e) { + connection.disconnect(); + System.exit(0); + } + + @Override + public void windowClosed(WindowEvent e) { + + } + + @Override + public void windowIconified(WindowEvent e) { + + } + + @Override + public void windowDeiconified(WindowEvent e) { + + } + + @Override + public void windowActivated(WindowEvent e) { + + } + + @Override + public void windowDeactivated(WindowEvent e) { + + } + }); + } + + private void createSignUpWindow(){ + signUpWindow = new SignUpWindow(); + + signUpWindow.getSignupBtn().addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + signUpWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptP.png"))); + String tmp = "",tmp1 =""; + for (char c : signUpWindow.getPassword().getPassword()){ + tmp+=c; + } + for (char c : signUpWindow.getPasswordC().getPassword()){ + tmp1+=c; + } + if (tmp.equals(tmp1)) { + tmp = "" + tmp.hashCode(); + connection.sendSignUp(signUpWindow.getLogin().getText(), tmp); + Command command = connection.recieve(); + System.out.println("Getting accept"); + if (command.type == CommandType.ACCEPT) { + System.out.println("Got accept"); + mainFrame.setLocalNick(signUpWindow.getLogin().getText()); + historyViewModel.setLocalNick(signUpWindow.getLogin().getText()); + signUpWindow.setVisible(false); + signUpWindow.dispose(); + mainFrame.setVisible(true); + CommandListenerThread clt = new CommandListenerThread(connection); + clt.start(); + } else { + UltimateGUI ultimateGUI = new UltimateGUI("User with such login already exists"); + } + + } + else{ + UltimateGUI ultimateGUI = new UltimateGUI("Passwords must match!"); + } + } + + @Override + public void mouseReleased(MouseEvent e) { + signUpWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptN.png"))); + } + + @Override + public void mouseEntered(MouseEvent e) { + signUpWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptH.png"))); + } + + @Override + public void mouseExited(MouseEvent e) { + signUpWindow.getSignupBtn().setIcon(new ImageIcon(Main.class.getResource("/signuptN.png"))); + } + }); + + signUpWindow.addWindowListener(new WindowListener() { + @Override + public void windowOpened(WindowEvent e) { + + } + + @Override + public void windowClosing(WindowEvent e) { + connection.disconnect(); + System.exit(0); + } + + @Override + public void windowClosed(WindowEvent e) { + + } + + @Override + public void windowIconified(WindowEvent e) { + + } + + @Override + public void windowDeiconified(WindowEvent e) { + + } + + @Override + public void windowActivated(WindowEvent e) { + + } + + @Override + public void windowDeactivated(WindowEvent e) { + + } + }); + } + + private void createMainFrame(){ + mainFrame = new MainGUI(); + + mainFrame.getDisconnectBtn().addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getDisconnectBtn().setIcon(new ImageIcon(Main.class.getResource("/disconP.png"))); + mainFrame.setDisconnected(); + connection.disconnectFromUser(); + mainFrame.setRemoteNick(""); + historyViewModel.addSystemMessage("Disconnected"); + isConnected=false; + } + }); + } + + @Override + public void mousePressed(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getDisconnectBtn().setIcon(new ImageIcon(Main.class.getResource("/disconP.png"))); + } + }); + } + + @Override + public void mouseReleased(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getDisconnectBtn().setIcon(new ImageIcon(Main.class.getResource("/disconN.png"))); + } + }); + } + + @Override + public void mouseEntered(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getDisconnectBtn().setIcon(new ImageIcon(Main.class.getResource("/disconH.png"))); + } + }); + } + + @Override + public void mouseExited(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getDisconnectBtn().setIcon(new ImageIcon(Main.class.getResource("/disconN.png"))); + } + }); + } + }); + + mainFrame.getLogoutBtn().addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getLogoutBtn().setIcon(new ImageIcon(Main.class.getResource("/logoutP.png"))); + } + }); + } + + @Override + public void mousePressed(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getLogoutBtn().setIcon(new ImageIcon(Main.class.getResource("/logoutP.png"))); + connection.logout(); + connection.sendContacts(contactsViewModel.getContacts()); + mainFrame.dispose(); + createMainFrame(); + createLogInWindow(); + createSignUpWindow(); + } + }); + } + + @Override + public void mouseReleased(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getLogoutBtn().setIcon(new ImageIcon(Main.class.getResource("/logoutN.png"))); + } + }); + } + + @Override + public void mouseEntered(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getLogoutBtn().setIcon(new ImageIcon(Main.class.getResource("/logoutH.png"))); + } + }); + } + + @Override + public void mouseExited(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getLogoutBtn().setIcon(new ImageIcon(Main.class.getResource("/logoutN.png"))); + } + }); + } + }); + + mainFrame.getSendBtn().addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getSendBtn().setIcon(new ImageIcon(Main.class.getResource("/sendP.png"))); + if (Protocol.isMessageValid(mainFrame.getMessageField().getText())) { + connection.sendMessage(mainFrame.getMessageField().getText()); + historyViewModel.addLocalMessage(mainFrame.getMessageField().getText()); + mainFrame.getMessageField().setText(""); + } + } + }); + } + + @Override + public void mousePressed(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getSendBtn().setIcon(new ImageIcon(Main.class.getResource("/sendP.png"))); + } + }); + } + + @Override + public void mouseReleased(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getSendBtn().setIcon(new ImageIcon(Main.class.getResource("/sendN.png"))); + } + }); + + } + + @Override + public void mouseEntered(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getSendBtn().setIcon(new ImageIcon(Main.class.getResource("/sendH.png"))); + } + }); + + } + + @Override + public void mouseExited(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + mainFrame.getSendBtn().setIcon(new ImageIcon(Main.class.getResource("/sendN.png"))); + } + }); + + } + }); + + mainFrame.getMessageField().addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent e) { + if (e.getKeyCode()==KeyEvent.VK_ENTER && Protocol.isMessageValid(mainFrame.getMessageField().getText())) { + connection.sendMessage(mainFrame.getMessageField().getText()); + historyViewModel.addLocalMessage(mainFrame.getMessageField().getText()); + mainFrame.getMessageField().setText(""); + } + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode()==KeyEvent.VK_ENTER && Protocol.isMessageValid(mainFrame.getMessageField().getText())) { + connection.sendMessage(mainFrame.getMessageField().getText()); + historyViewModel.addLocalMessage(mainFrame.getMessageField().getText()); + mainFrame.getMessageField().setText(""); + } + } + + @Override + public void keyReleased(KeyEvent e) { + if (e.getKeyCode()==KeyEvent.VK_ENTER && Protocol.isMessageValid(mainFrame.getMessageField().getText())) { + connection.sendMessage(mainFrame.getMessageField().getText()); + historyViewModel.addLocalMessage(mainFrame.getMessageField().getText()); + mainFrame.getMessageField().setText(""); + } + } + }); + + mainFrame.setDisconnected(); + + mainFrame.getFindButton().addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + mainFrame.getFindButton().setIcon(new ImageIcon(Main.class.getResource("/findP.png"))); + connection.getContacts(mainFrame.getFindContacts().getText()); + mainFrame.getFindContacts().setText(""); + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + mainFrame.getFindButton().setIcon(new ImageIcon(Main.class.getResource("/findN.png"))); + } + + @Override + public void mouseEntered(MouseEvent e) { + mainFrame.getFindButton().setIcon(new ImageIcon(Main.class.getResource("/findH.png"))); + } + + @Override + public void mouseExited(MouseEvent e) { + mainFrame.getFindButton().setIcon(new ImageIcon(Main.class.getResource("/findN.png"))); + } + }); + + mainFrame.addWindowListener(new WindowListener() { + @Override + public void windowOpened(WindowEvent e) { + + } + + @Override + public void windowClosing(WindowEvent e) { + exit(); + + } + + @Override + public void windowClosed(WindowEvent e) { + + } + + @Override + public void windowIconified(WindowEvent e) { + + } + + @Override + public void windowDeiconified(WindowEvent e) { + + } + + @Override + public void windowActivated(WindowEvent e) { + + } + + @Override + public void windowDeactivated(WindowEvent e) { + + } + }); + + } + + private void exit(){ + connection.sendContacts(contactsViewModel.getContacts()); + connection.disconnect(); + System.exit(0); + + } + + public void call(String nick) { + connection.sendCall(nick); + remoteNick = nick; + isConnected=true; + historyViewModel.setRemoteNick(nick); + historyViewModel.clearView(); + } + + private class CommandListenerThread extends Thread{ + + private Command lastCommand; + private Connection connection; + private boolean stop; + + + public CommandListenerThread(Connection connection){ + this.connection=connection; + + } + + + + public void run() { + + connection.getMyContacts(); + java.util.Timer onlineTimer = new java.util.Timer(); + onlineTimer.schedule(new TimerTask() { + @Override + public void run() { + if (contactsViewModel.contactList.size()!=0) connection.getOnline(contactsViewModel.getOnlineCintacts()); + } + },1000,10000); + + + while (!stop) { + System.out.println("waiting for a new command"); + lastCommand = connection.recieve(); + if (lastCommand==null) continue; + switch (lastCommand.type){ + case MY_CONTACTS:{ + MyContactsCommand tmp = (MyContactsCommand) lastCommand; + ArrayList tmpArr = tmp.getArrayList(); + System.out.println("Got contacts"); + for (Contact contact : tmpArr){ + contact.setContactsViewModel(contactsViewModel); + contact.setMain(getThis()); + contactsViewModel.add(contact); + } + break; + } + case LOGOUT:{ + System.out.println("got logout"); + onlineTimer.cancel(); + return; + } + case BUSY:{ + UltimateGUI ultimateGUI = new UltimateGUI(remoteNick + " is busy"); + + break; + } + case OFFLINE:{ + UltimateGUI ultimateGUI = new UltimateGUI(remoteNick + " is offline"); + mainFrame.setEnabled(true); + break; + } + case CONTACTS:{ + ArrayList tmp = ((ContactsCommand) lastCommand).getArrayList(); + for (Contact contact : tmp){ + contact.setContactsViewModel(contactsViewModel); + contact.setMain(getThis()); + } + ContactsFindView contactsFindView = new ContactsFindView(tmp); + break; + } + case DISCONNECT:{ + System.out.println("Got disconnect"); + System.exit(0); + break; + } + case DISCONNECT_FROM_USER:{ + System.out.println("got Disconnect from"); + mainFrame.setDisconnected(); + historyViewModel.addSystemMessage("Disconnected"); + mainFrame.setRemoteNick(""); + isConnected=false; + break; + } + case CALL:{ + CallCommand callCommand = (CallCommand) lastCommand; + contactsViewModel.add(new Contact(contactsViewModel,callCommand.getNick())); + historyViewModel.setRemoteNick(callCommand.getNick()); + IncomingCallForm incomingCallForm = new IncomingCallForm(callCommand.getNick()); + break; + } + case MESSAGE:{ + historyViewModel.addRemoteMessage(((MessageCommand) lastCommand).message); + System.out.println("got "+((MessageCommand) lastCommand).message); + break; + } + case ACCEPT:{ + mainFrame.setConnected(); + mainFrame.setRemoteNick(remoteNick); + break; + } + case REJECT:{ + UltimateGUI ultimateGUI = new UltimateGUI("User rejected your call"); + isConnected=false; + break; + } + case EMPTYCONTACTS:{ + UltimateGUI ultimateGUI = new UltimateGUI("None contacts found"); + break; + } + case ONLINE_CONTACTS:{ + contactsViewModel.updateOnline(((OnlineCommand) lastCommand).getArrayList()); + break; + } + } + } + } + + public void kill(){ + stop = true; + } + } + + private class ContactsFindView extends JPanel{ + private JPanel contactsP; + + JLabel contactsL = new JLabel("Contacts"); + + private ArrayList list = new ArrayList(); + + public ContactsFindView(ArrayList contacts){ + JFrame frame = new JFrame(); + createGUI(); + for (Contact contact : contacts){ + addContact(contact); + } + + frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + frame.setSize(290, 500); + frame.setResizable(false); + frame.setLocationRelativeTo(mainFrame); + frame.add(this); + frame.setVisible(true); + } + + private void createGUI(){ + setBackground(Colors.softGreen); + setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); + + setAlignmentX(Component.LEFT_ALIGNMENT); + add(Box.createVerticalStrut(10)); + add(contactsL); + contactsL.setFont(mainFrame.getBigFont()); + add(Box.createVerticalStrut(10)); + + contactsP = new JPanel(); + JScrollPane scrollPane = new JScrollPane(contactsP); + scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI()); + scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(15, Integer.MAX_VALUE)); + scrollPane.setBorder(null); + contactsP.setAutoscrolls(true); + contactsP.setLayout(new BoxLayout(contactsP,BoxLayout.Y_AXIS)); + contactsP.setBackground(Colors.softGreen); + contactsP.setAlignmentX(Component.LEFT_ALIGNMENT); + add(scrollPane); + } + + public void addContact(Contact contact){ + ContactFindPanel tmp = new ContactFindPanel(contact); + contactsP.add(tmp); + } + + public void delete(ContactFindPanel contact){ + contactsP.remove(contact); + list.remove(contact); + updateUI(); + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + + private class ContactFindPanel extends JPanel { + private Contact contact; + private JLabel label = new JLabel(); + + public ContactFindPanel(){ + createGUI(); + } + + public ContactFindPanel(Contact contact){ + this.contact=contact; + createGUI(); + } + + private void createGUI(){ + + + setLayout(null); + setBackground(Colors.softGreen); + setMinimumSize(new Dimension(200, 25)); + setPreferredSize(new Dimension(200,25)); + setMaximumSize(new Dimension(200, 25)); + label.setBounds(5, 3, 200, 20); + label.setFont(mainFrame.getSmallFont()); + label.setText(contact.getNick()); + if (contact.isOnline()) label.setIcon(new ImageIcon(Main.class.getResource("/on.png"))); + else label.setIcon(new ImageIcon(Main.class.getResource("/off.png"))); + add(label); + + addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + setBackground(Colors.midGreen); + delete(getThis()); + contactsViewModel.add(contact); + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + setBackground(Colors.softGreen); + } + }); + } + + @Override + public void mouseEntered(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + setBackground(Colors.mainGreen); + } + }); + + } + + @Override + public void mouseExited(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + setBackground(Colors.softGreen); + } + }); + } + }); + } + + public boolean isFav(){ + return contact.isFav(); + } + + public void update(){ + if (contact.isOnline()) label.setIcon(new ImageIcon(Main.class.getResource("/on.png"))); + else label.setIcon(new ImageIcon(Main.class.getResource("/off.png"))); + } + + public String getNick(){ + return contact.getNick(); + } + + public Contact getContact(){ + return contact; + } + + private ContactFindPanel getThis(){ + return this; + } + + } + + + } + + private class IncomingCallForm extends JFrame { + + + private static final int Height = 150; + private static final int Widht = 300; + + JPanel panel = new JPanel(); + + JLabel accept; + JLabel dismiss; + JLabel nickName; + + IncomingCallForm(final String username){ + + setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + setSize(Widht, Height); + setResizable(false); + setUndecorated(true); + setAlwaysOnTop(true); + setLocationRelativeTo(null); + + JPanel panel = new JPanel(); + panel.setLayout(null); + panel.setBackground(Color.white); + panel.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Colors.midGreen)); + + nickName = new JLabel(username+ " is calling"); + nickName.setLocation(80, 10); + nickName.setFont(mainFrame.getSmallFont()); + nickName.setSize(200, 54); + + final java.util.Timer timer = new java.util.Timer(); + + timer.schedule(new TimerTask() { + @Override + public void run() { + connection.reject(); + dispose(); + } + },10000); + + accept = new JLabel(""); + accept.setLocation(48, 80); + accept.setSize(97, 27); + accept.setIcon(new ImageIcon(Main.class.getResource("/acceptN.png"))); + accept.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + connection.accept(); + System.out.println("Sending Accept!"); + mainFrame.setConnected(); + mainFrame.setRemoteNick(username); + historyViewModel.remoteNick = username; + historyViewModel.clearView(); + + timer.cancel(); + dispose(); + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + accept.setIcon(new ImageIcon(Main.class.getResource("/acceptH.png"))); + } + }); + } + + @Override + public void mouseExited(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + accept.setIcon(new ImageIcon(Main.class.getResource("/acceptN.png"))); + } + }); + } + }); + + + + dismiss = new JLabel(""); + dismiss.setLocation(170, 80); + dismiss.setSize(97, 27); + dismiss.setIcon(new ImageIcon(Main.class.getResource("/rejectN.png"))); + dismiss.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + connection.reject(); + System.out.println("Sending Reject!"); + timer.cancel(); + dispose(); + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + + } + + @Override + public void mouseEntered(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + dismiss.setIcon(new ImageIcon(Main.class.getResource("/rejectH.png"))); + } + }); + + } + + @Override + public void mouseExited(MouseEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + dismiss.setIcon(new ImageIcon(Main.class.getResource("/rejectN.png"))); + } + }); + + } + }); + + + panel.add(accept); + panel.add(dismiss); + panel.add(nickName); + + + + this.add(panel); + setVisible(true); + } + + } + + public static void main(String[] args) { + Main main = new Main(); + } +} \ No newline at end of file diff --git a/src/MainGUI.java b/src/MainGUI.java new file mode 100644 index 0000000..bd115b9 --- /dev/null +++ b/src/MainGUI.java @@ -0,0 +1,270 @@ +import javax.swing.*; +import javax.swing.plaf.metal.MetalScrollBarUI; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.WindowEvent; +import java.awt.event.WindowStateListener; +import java.awt.geom.AffineTransform; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +public class MainGUI extends JFrame { + + final byte BTN_WIDTH = 97; + final byte BTN_HEIGHT = 27; + + JLabel logged = new JLabel("Logged as"); + JLabel talking = new JLabel("Talking to"); + JLabel localNick = new JLabel("EvGe22"); + JLabel remoteNick = new JLabel(""); + + JTextArea messageField = new JTextArea(); + JTextArea historyView = new JTextArea(); + JTextArea findContacts = new JTextArea(); + + JLabel logoutBtn = new JLabel(""); + JLabel disconnectBtn = new JLabel(""); + //JLabel optionsBtn = new JLabel(""); + JLabel sendBtn = new JLabel(""); + JLabel findButton = new JLabel(""); + + ContactsView contactsView = new ContactsView(); + + JScrollPane contactsPane = new JScrollPane(contactsView); + JScrollPane historyPane = new JScrollPane(historyView); + + JPanel leftTopPanel = new JPanel(); + JPanel leftBottomPanel = new JPanel(); + + Font bigFont,smallFont; + + + public MainGUI(){ + super("ChatApp 2015"); + createGUI(); + } + + private void createGUI(){ + + setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + setMinimumSize(new Dimension(860,550)); + setLayout(null); + getContentPane().setBackground(Color.white); + + try { + smallFont = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("roboto-thin.ttf")).deriveFont(15f); + bigFont = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("roboto-thin.ttf")).deriveFont(18f); + + } catch (FontFormatException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + leftTopPanel.setBackground(Colors.softGreen); + leftTopPanel.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Colors.midGreen)); + leftTopPanel.setBounds(-1,-1,280,123); + leftTopPanel.setLayout(null); + add(leftTopPanel); + + leftBottomPanel.setBackground(Colors.softGreen); + leftBottomPanel.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Colors.midGreen)); + leftBottomPanel.setBounds(-1,121,280,getHeight()-150); + leftBottomPanel.setLayout(null); + add(leftBottomPanel); + + logged.setBounds(25,10,150,40); + logged.setFont(bigFont); + leftTopPanel.add(logged); + + localNick.setBounds(25,32,150,40); + localNick.setFont(bigFont); + leftTopPanel.add(localNick); + + talking.setBounds(153,10,150,40); + talking.setFont(bigFont); + leftTopPanel.add(talking); + + remoteNick.setBounds(153,32,150,40); + remoteNick.setFont(bigFont); + leftTopPanel.add(remoteNick); + + + + logoutBtn.setIcon(new ImageIcon(Main.class.getResource("/logoutN.png"))); + logoutBtn.setDisabledIcon(new ImageIcon(Main.class.getResource("/logoutD.png"))); + logoutBtn.setBounds(20,80,BTN_WIDTH,BTN_HEIGHT); + leftTopPanel.add(logoutBtn); + + disconnectBtn.setIcon(new ImageIcon(Main.class.getResource("/disconN.png"))); + disconnectBtn.setDisabledIcon(new ImageIcon(Main.class.getResource("/disconD.png"))); + disconnectBtn.setEnabled(false); + disconnectBtn.setBounds(155,80,BTN_WIDTH,BTN_HEIGHT); + leftTopPanel.add(disconnectBtn); + + messageField.setBounds(getWidth()-544,getHeight()-122,430,50); + messageField.setFont(smallFont); + messageField.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Colors.lightGreen)); + messageField.setBackground(Colors.softGreen); + messageField.getDocument().putProperty("filterNewlines", Boolean.TRUE); + add(messageField); + messageField.setEnabled(false); + messageField.setLineWrap(true); + + sendBtn.setIcon(new ImageIcon(Main.class.getResource("/sendN.png"))); + sendBtn.setDisabledIcon(new ImageIcon(Main.class.getResource("/sendD.png"))); + sendBtn.setEnabled(false); + sendBtn.setBounds(getWidth()-96,getHeight()-124, 51,51); + add(sendBtn); + + /*optionsBtn.setIcon(new ImageIcon(Main.class.getResource("/optionsN.png"))); + optionsBtn.setBounds(getWidth()-47,getHeight()-70,24,24); + add(optionsBtn);*/ + + historyPane.getVerticalScrollBar().setPreferredSize(new Dimension(15, Integer.MAX_VALUE)); + historyPane.getVerticalScrollBar().setUI(new MyScrollBarUI()); + historyPane.setBackground(Color.white); + historyPane.setBorder(null); + + add(historyPane); + + findContacts.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Colors.midGreen)); + findContacts.setBounds(25,14,200,25); + findButton.setFont(smallFont); + leftBottomPanel.add(findContacts); + + findButton.setBounds(237,14,24,24); + findButton.setIcon(new ImageIcon(Main.class.getResource("/findN.png"))); + leftBottomPanel.add(findButton); + + + contactsPane.getVerticalScrollBar().setPreferredSize(new Dimension(15,Integer.MAX_VALUE)); + contactsPane.getVerticalScrollBar().setUI(new MyScrollBarUI()); + contactsPane.setBounds(25, 48, 240, leftBottomPanel.getHeight() - 80); + contactsPane.setBorder(null); + contactsPane.setBackground(Colors.softGreen); + contactsView.setLabelFont(bigFont); + leftBottomPanel.add(contactsPane); + + + historyView.setFont(smallFont); + historyView.setEditable(false); + historyView.setText(""); + historyView.setLineWrap(true); + + + resize(); + addWindowStateListener(new WindowStateListener() { + @Override + public void windowStateChanged(WindowEvent e) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + resize(); + } + }); + } + }); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + resize(); + } + }); + } + }); + } + + public void resize(){ + leftBottomPanel.setBounds(-1,121,280,getHeight()-150); + contactsPane.setBounds(25, 48, 240, leftBottomPanel.getHeight() - 80); + historyPane.setBounds(321,40,getWidth()-370,getHeight()-195); + sendBtn.setBounds(getWidth()-96,getHeight()-124, 51,51); + //optionsBtn.setBounds(getWidth()-47,getHeight()-70,24,24); + messageField.setBounds(320,getHeight()-122,getWidth()-430 /*430*/,50); + + } + + /* public JLabel getOptionsBtn() { + return optionsBtn; + } */ + + public JLabel getDisconnectBtn() { + return disconnectBtn; + } + + public JLabel getLogoutBtn() { + return logoutBtn; + } + + public JTextArea getHistoryView() { + return historyView; + } + + public JTextArea getMessageField() { + return messageField; + } + + public JLabel getRemoteNick() { + return remoteNick; + } + + public JLabel getLocalNick() { + return localNick; + } + + public JLabel getSendBtn() { + return sendBtn; + } + + public ContactsView getContactsView() { + return contactsView; + } + + public JLabel getFindButton() { + return findButton; + } + + public JTextArea getFindContacts() { + return findContacts; + } + + public void setConnected(){ + disconnectBtn.setEnabled(true); + sendBtn.setEnabled(true); + messageField.setEnabled(true); + logoutBtn.setEnabled(false); + messageField.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Colors.midGreen)); + } + + public void setDisconnected(){ + disconnectBtn.setEnabled(false); + sendBtn.setEnabled(false); + messageField.setEnabled(false); + logoutBtn.setEnabled(true); + messageField.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Colors.lightGreen)); + } + + public void setLocalNick(String nick){ + localNick.setText(nick); + } + + public Font getSmallFont() { + return smallFont; + } + + public Font getBigFont(){ + return bigFont; + } + + public void setRemoteNick(String remoteNick) { + this.remoteNick.setText(remoteNick); + } +} diff --git a/src/MessageCommand.java b/src/MessageCommand.java new file mode 100644 index 0000000..e33f78e --- /dev/null +++ b/src/MessageCommand.java @@ -0,0 +1,12 @@ +public class MessageCommand extends Command{ + String message; + + public MessageCommand(){ + super(CommandType.MESSAGE); + } + + public MessageCommand(String message){ + super(CommandType.MESSAGE); + this.message=message; + } +} diff --git a/src/MyContactsCommand.java b/src/MyContactsCommand.java new file mode 100644 index 0000000..0cad591 --- /dev/null +++ b/src/MyContactsCommand.java @@ -0,0 +1,19 @@ +import java.util.ArrayList; + +public class MyContactsCommand extends Command { + + ArrayList arrayList; + + public MyContactsCommand(){ + super(CommandType.MY_CONTACTS); + } + + public MyContactsCommand(ArrayList result) { + super(CommandType.MY_CONTACTS); + arrayList = result; + } + + public ArrayList getArrayList() { + return arrayList; + } +} diff --git a/src/MyScrollBarUI.java b/src/MyScrollBarUI.java new file mode 100644 index 0000000..fce2c83 --- /dev/null +++ b/src/MyScrollBarUI.java @@ -0,0 +1,63 @@ +import javax.swing.*; +import javax.swing.plaf.metal.MetalScrollBarUI; +import java.awt.*; +import java.awt.geom.AffineTransform; +import java.awt.image.BufferedImage; + +public class MyScrollBarUI extends MetalScrollBarUI { + + private Image imageThumb, imageTrack; + private JButton b = new JButton() { + + @Override + public Dimension getPreferredSize() { + return new Dimension(0, 0); + } + }; + + public MyScrollBarUI() { + imageThumb = PlainImage.create(15, 15, Colors.mainGreen); + imageTrack = PlainImage.create(15, 15, Colors.darkGreen); + } + + @Override + protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { + g.translate(thumbBounds.x, thumbBounds.y); + g.setColor( Color.red ); + g.drawRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 ); + AffineTransform transform = AffineTransform.getScaleInstance((double)thumbBounds.width/imageThumb.getWidth(null),(double)thumbBounds.height/imageThumb.getHeight(null)); + ((Graphics2D)g).drawImage(imageThumb, transform, null); + g.translate(-thumbBounds.x, -thumbBounds.y ); + } + + @Override + protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) { + g.translate(trackBounds.x, trackBounds.y); + ((Graphics2D)g).drawImage(imageTrack,AffineTransform.getScaleInstance(1,(double)trackBounds.height/imageTrack.getHeight(null)),null); + g.translate(-trackBounds.x, -trackBounds.y ); + } + + @Override + protected JButton createDecreaseButton(int orientation) { + return b; + } + + @Override + protected JButton createIncreaseButton(int orientation) { + return b; + } + + + private static class PlainImage { + + static public Image create(int w, int h, Color c) { + BufferedImage bi = new BufferedImage( + w, h, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = bi.createGraphics(); + g2d.setPaint(c); + g2d.fillRect(0, 0, w, h); + g2d.dispose(); + return bi; + } + } +} \ No newline at end of file diff --git a/src/NewContactFrame.java b/src/NewContactFrame.java new file mode 100644 index 0000000..ad212df --- /dev/null +++ b/src/NewContactFrame.java @@ -0,0 +1,82 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class NewContactFrame extends JFrame{ + /* + + JButton Cancel; + JButton accept; + + JTextArea nickArea; + JTextArea ipArea; + + + public NewContactFrame(ContactsViewModel cvm){ + super("Create new contact"); + createGUI(cvm); + } + + private void createGUI(final ContactsViewModel cvm){ + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setSize(300,150); + setResizable(false); + setAlwaysOnTop(true); + setLocationRelativeTo(null); + + + + + + JPanel panel = new JPanel(); + panel.setLayout(null); + + + nickArea = new JTextArea("Nick"); + nickArea.setLocation(18, 15); + nickArea.setSize(250,20); + ipArea = new JTextArea("IP"); + ipArea.setLocation(18, 46); + ipArea.setSize(250,20); + + + accept = new JButton("accept"); + accept.setLocation(18, 80); + accept.setSize(130,30); + accept.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Contact c = new Contact(cvm,nickArea.getText(),ipArea.getText()); + cvm.add(c); + close(); + } + }); + + Cancel = new JButton("Cancel"); + Cancel.setLocation(150, 80); + Cancel.setSize(130, 30); + Cancel.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + close(); + } + }); + + + panel.add(accept); + panel.add(Cancel); + panel.add(nickArea); + panel.add(ipArea); + + + + this.add(panel); + setVisible(true); + } + + public void close(){ + dispose(); + } + */ + +} diff --git a/src/NickCommand.java b/src/NickCommand.java new file mode 100644 index 0000000..0014e09 --- /dev/null +++ b/src/NickCommand.java @@ -0,0 +1,26 @@ +public class NickCommand extends Command{ + String nick; + Boolean busy=false; + + public NickCommand(){ + super(CommandType.NICK); + } + + public NickCommand(String string){ + super(CommandType.NICK); + if (string.contains(" busy")){ + busy = true; + string=string.replace(" busy",""); + } + string = string.replace(Protocol.GREETING,""); + nick = string; + } + + public boolean isBusy(){ + return busy; + } + + public String getNick() { + return nick; + } +} diff --git a/src/OnlineCommand.java b/src/OnlineCommand.java new file mode 100644 index 0000000..928ed48 --- /dev/null +++ b/src/OnlineCommand.java @@ -0,0 +1,19 @@ +import java.util.ArrayList; + +public class OnlineCommand extends Command { + + ArrayList arrayList; + + public OnlineCommand() { + super(CommandType.ONLINE_CONTACTS); + } + + public OnlineCommand(ArrayList onlines) { + super(CommandType.ONLINE_CONTACTS); + arrayList = onlines; + } + + public ArrayList getArrayList() { + return arrayList; + } +} diff --git a/src/Options.java b/src/Options.java new file mode 100644 index 0000000..c391a34 --- /dev/null +++ b/src/Options.java @@ -0,0 +1,6 @@ +public class Options { + public static boolean saveNick = true; + public static boolean saveHistory = true; + public static boolean saveContacts = true; + public static boolean autoSaveContacts = true; +} diff --git a/src/OptionsFrame.java b/src/OptionsFrame.java new file mode 100644 index 0000000..44f0bde --- /dev/null +++ b/src/OptionsFrame.java @@ -0,0 +1,119 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.io.File; +import java.io.IOException; + +public class OptionsFrame extends JFrame { + + JCheckBox autoAddContacts; + JCheckBox contacts; + JCheckBox history; + JCheckBox nick; + static Font font; + + static { + try { + font = Font.createFont(Font.TRUETYPE_FONT, new File("src/font/roboto-thin.ttf")).deriveFont(15f); + } catch (FontFormatException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public OptionsFrame(){ + super("Options"); + createGUI(); + } + + private void createGUI(){ + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setSize(300, 140); + setResizable(false); + setAlwaysOnTop(true); + setLocationRelativeTo(null); + + JPanel panel = new JPanel(); + panel.setLayout(null); + panel.setBackground(Colors.softGreen); + + autoAddContacts = new JCheckBox("Add Contacts automatically"); + contacts = new JCheckBox("Save Contacts"); + history = new JCheckBox("Save History"); + nick = new JCheckBox("Save Nick"); + + setStyle(autoAddContacts); + setStyle(contacts); + setStyle(history); + setStyle(nick); + + if (Options.saveContacts) contacts.setSelected(true); + if (Options.saveHistory) history.setSelected(true); + if (Options.saveNick) nick.setSelected(true); + if (Options.autoSaveContacts) autoAddContacts.setSelected(true); + + contacts.setLocation(10,10); + contacts.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (contacts.isSelected()) Options.saveContacts = true; + else Options.saveContacts = false; + } + }); + + history.setLocation(10, 30); + history.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (history.isSelected()) Options.saveHistory=true; + else Options.saveHistory=false; + } + }); + + nick.setLocation(10,50); + nick.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (nick.isSelected()) Options.saveNick=true; + else Options.saveNick=false; + } + }); + + autoAddContacts.setLocation(10,70); + autoAddContacts.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (autoAddContacts.isSelected()) Options.autoSaveContacts=true; + else Options.autoSaveContacts=false; + } + }); + + panel.add(contacts); + panel.add(history); + panel.add(nick); + panel.add(autoAddContacts); + + add(panel); + setVisible(true); + } + + private static void setStyle(JCheckBox checkBox){ + checkBox.setIcon(new ImageIcon("src/images/notchecked.png")); + checkBox.setSelectedIcon(new ImageIcon("src/images/checked.png")); + checkBox.setRolloverIcon(new ImageIcon("src/images/notcheckedH.png")); + checkBox.setRolloverSelectedIcon(new ImageIcon("src/images/checkedH.png")); + checkBox.setFont(font.deriveFont(15f)); + checkBox.setBackground(Colors.softGreen); + checkBox.setIconTextGap(8); + checkBox.setSize(250,15); + checkBox.setBorderPainted(false); + } + + public static void main(String[] args) { + OptionsFrame op = new OptionsFrame(); + } +} diff --git a/src/PopUp.java b/src/PopUp.java new file mode 100644 index 0000000..c70eb2d --- /dev/null +++ b/src/PopUp.java @@ -0,0 +1,56 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class PopUp extends JPopupMenu { + + JMenuItem delete,fav,call; + Contact contact; + ContactPanel contactPanel; + + + public PopUp(ContactPanel contact){ + contactPanel=contact; + this.contact = contact.getContact(); + createGUI(); + + } + + private void createGUI(){ + delete = new JMenuItem("Delete"); + fav = new JMenuItem(); + if (contact.isFav()){ + fav.setText("Remove from favourites"); + } + else{ + fav.setText("Add to favourites"); + } + call = new JMenuItem("Call"); + if (Main.isConnected) call.setEnabled(false); + + add(call); + add(fav); + add(delete); + + call.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + contact.sendCall(); + } + }); + + fav.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + contact.changeFav(contactPanel); + } + }); + delete.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + contact.remove(contactPanel); + } + }); + } + +} \ No newline at end of file diff --git a/src/Protocol.java b/src/Protocol.java new file mode 100644 index 0000000..0d7bc78 --- /dev/null +++ b/src/Protocol.java @@ -0,0 +1,48 @@ +public class Protocol { + public static final int PORT_NUMBER =28411; + public static final String GREETING = "ChatApp 2015 user "; + public static final String ACCEPTED = "Accepted"; + public static final String REJECTED = "Rejected"; + public static final String MESSAGE = "Message"; + public static final String DISCONNECT = "Disconnect"; + public static final String DISCONNECT_FROM_USER = "Dscnnct from user"; + public static final String SERVER_IP = "178.151.143.46"; + public static final String HELLO_SERVER = "Hello server"; + public static final String HELLO_CLIENT = "Hello ChatApp2015"; + public static final String CALL = "Call"; + public static final String LOGIN = "Login"; + public static final String SIGNUP = "Signup"; + public static final String LOGOUT = "Logout"; + public static final String CONTACTS = "Contacts"; + public static final String GET_CONTACTS = "Get contacts"; + public static final String GET_MY_CONTACTS = "Get my contacts"; + public static final String MY_CONTACTS = "Mycntcts"; + public static final String EMPTYCONTACTS = "EmptyCntcts"; + public static final String EMPTYMYCONTACTS = "EmptyMyCntcts"; + public static final String BUSY = "Busy"; + public static final String OFFLINE = "Offline"; + public static final String ONLINE_CONTACTS = "Online Cntcts"; + + + public static String encode(String string){ + return string.replace("\n",":&:"); + } + + public static boolean isNickValid(String nick){ + if (nick.matches(".*[a-zA-Z0-9_\\-].*")) { + return true; + } + return false; + } + + public static boolean isMessageValid(String message){ + if (message.matches(".*\\S.*")) { + return true; + } + return false; + } + + public static String decode(String string){ + return string.replace(":&:","\n"); + } +} diff --git a/src/ServerCaller.java b/src/ServerCaller.java new file mode 100644 index 0000000..be1d384 --- /dev/null +++ b/src/ServerCaller.java @@ -0,0 +1,17 @@ +import java.io.IOException; +import java.net.Socket; + +public class ServerCaller { + + public static Connection call() throws IOException { + Command lastCommand; + Connection connection = new Connection(new Socket(Protocol.SERVER_IP,Protocol.PORT_NUMBER)); + //Проверка есть ли подключение и туда ли мы присоединились. + lastCommand = connection.recieve(); + if (lastCommand.type==CommandType.HELLO_CLIENT){ + connection.sendServerHello(); + return connection; + } + return null; + } +} diff --git a/src/SignUpWindow.java b/src/SignUpWindow.java new file mode 100644 index 0000000..5487d45 --- /dev/null +++ b/src/SignUpWindow.java @@ -0,0 +1,246 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.File; +import java.io.IOException; + +public class SignUpWindow extends JFrame { + JTextArea login; + JPasswordField password,passwordC; + + JLabel signupBtn; + + public SignUpWindow(){ + + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + setSize(new Dimension(300,225)); + setLocation(getX()-150,getY()-100); + setAlwaysOnTop(true); + setResizable(false); + createGUI(); + } + + private void createGUI(){ + JPanel panel = new JPanel(); + panel.setLayout(null); + panel.setBackground(Colors.softGreen); + panel.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Colors.midGreen)); + add(panel); + + Font font = null; + try { + font = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("roboto-thin.ttf")).deriveFont(15f); + } catch (FontFormatException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + login = new JTextArea(); + login.setFont(font); + login.setForeground(Color.lightGray); + login.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Colors.midGreen)); + login.setBackground(Color.white); + login.setText("Login"); + login.setBounds(55, 30, 182, 30); + login.getDocument().putProperty("filterNewlines", Boolean.TRUE); + + login.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + if (login.getText().equals("Login")) { + login.setForeground(Color.black); + login.setText(""); + password.setForeground(Color.black); + password.setText(""); + passwordC.setForeground(Color.black); + passwordC.setText(""); + signupBtn.setEnabled(true); + } + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } + }); + login.addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent e) { + if (login.getText().equals("Login")) { + login.setForeground(Color.black); + login.setText(""); + password.setForeground(Color.black); + password.setText(""); + signupBtn.setEnabled(true); + passwordC.setForeground(Color.black); + passwordC.setText(""); + } + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyReleased(KeyEvent e) { + + } + }); + + panel.add(login); + + password = new JPasswordField(); + password.setFont(font); + password.setBounds(55,65,182,30); + password.setForeground(Color.lightGray); + password.setBackground(Color.white); + password.setText("password"); + password.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Colors.midGreen)); + panel.add(password); + + password.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + String tmp =""; + for ( char c : password.getPassword() ){ + tmp=tmp+c; + } + System.out.println(tmp); + if (tmp.equals("password")){ + passwordC.setForeground(Color.black); + passwordC.setText(""); + password.setForeground(Color.black); + password.setText(""); + login.setForeground(Color.black); + login.setText(""); + signupBtn.setEnabled(true); + } + + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } + }); + + passwordC = new JPasswordField(); + passwordC.setFont(font); + passwordC.setBounds(55,100,182,30); + passwordC.setForeground(Color.lightGray); + passwordC.setBackground(Color.white); + passwordC.setText("password"); + passwordC.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Colors.midGreen)); + panel.add(passwordC); + + passwordC.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + String tmp =""; + for ( char c : passwordC.getPassword() ){ + tmp=tmp+c; + } + System.out.println(tmp); + if (tmp.equals("password")){ + passwordC.setForeground(Color.black); + passwordC.setText(""); + password.setForeground(Color.black); + password.setText(""); + login.setForeground(Color.black); + login.setText(""); + signupBtn.setEnabled(true); + } + + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } + }); + + signupBtn = new JLabel(""); + signupBtn.setIcon(new ImageIcon(Main.class.getResource("/signuptN.png"))); + signupBtn.setDisabledIcon(new ImageIcon(Main.class.getResource("/signuptD.png"))); + signupBtn.setBounds(97,145,97,27); + signupBtn.setEnabled(false); + panel.add(signupBtn); + + } + + public JLabel getSignupBtn() { + return signupBtn; + } + + public JTextArea getLogin() { + return login; + } + + public JPasswordField getPassword() { + return password; + } + + public JPasswordField getPasswordC() { + return passwordC; + } + + public static void main(String[] args) { + SignUpWindow asd = new SignUpWindow(); + } + + + +} \ No newline at end of file diff --git a/src/UltimateGUI.java b/src/UltimateGUI.java new file mode 100644 index 0000000..eb6ac2d --- /dev/null +++ b/src/UltimateGUI.java @@ -0,0 +1,118 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.io.File; +import java.io.IOException; + +public class UltimateGUI extends JFrame{ + + JLabel okButton; + Font font; + + public UltimateGUI(String string){ + super("Error"); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(450, 100); + setResizable(false); + setAlwaysOnTop(true); + setLocationRelativeTo(null); + createGui(string); + setUndecorated(true); + setVisible(true); + + + } + + public void createGui(String string){ + JPanel panel = new JPanel(); + panel.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Colors.midGreen)); + panel.setBackground(Colors.softGreen); + panel.setLayout(null); + panel.setSize(450,150); + + try { + font = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("roboto-thin.ttf")).deriveFont(15f); + } catch (FontFormatException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + JLabel label = new JLabel(string); + label.setLocation(0,0); + label.setSize(450,50); + label.setHorizontalAlignment(0); + label.setFont(font); + + + + okButton = new JLabel(""); + okButton.setIcon(new ImageIcon(Main.class.getResource("/okN.png"))); + okButton.setLocation(175,50); + okButton.setSize(97,27); + okButton.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + okButton.setIcon(new ImageIcon(Main.class.getResource("/okP.png"))); + } + + @Override + public void mouseReleased(MouseEvent e) { + dispose(); + } + + @Override + public void mouseEntered(MouseEvent e) { + okButton.setIcon(new ImageIcon(Main.class.getResource("/okH.png"))); + } + + @Override + public void mouseExited(MouseEvent e) { + okButton.setIcon(new ImageIcon(Main.class.getResource("/okN.png"))); + } + }); + + addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + okButton.setIcon(new ImageIcon(Main.class.getResource("/okP.png"))); + + } + } + + @Override + public void keyReleased(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + dispose(); + } + } + }); + + panel.add(label); + panel.add(okButton); + add(panel); + + + + } + + public static void main(String[] args) { + UltimateGUI asd = new UltimateGUI("asd"); + } + + private void exit(){ + dispose(); + } +} diff --git a/src/font/roboto-thin.ttf b/src/font/roboto-thin.ttf new file mode 100644 index 0000000..d43e943 Binary files /dev/null and b/src/font/roboto-thin.ttf differ diff --git a/src/font/roboto-thin9.ttf b/src/font/roboto-thin9.ttf new file mode 100644 index 0000000..861d63a Binary files /dev/null and b/src/font/roboto-thin9.ttf differ diff --git a/src/images/ButtonPattern.psd b/src/images/ButtonPattern.psd new file mode 100644 index 0000000..4d71212 Binary files /dev/null and b/src/images/ButtonPattern.psd differ diff --git a/src/images/acceptH.png b/src/images/acceptH.png new file mode 100644 index 0000000..6103428 Binary files /dev/null and b/src/images/acceptH.png differ diff --git a/src/images/acceptN.png b/src/images/acceptN.png new file mode 100644 index 0000000..1b5f03e Binary files /dev/null and b/src/images/acceptN.png differ diff --git a/src/images/acceptP.png b/src/images/acceptP.png new file mode 100644 index 0000000..2376e52 Binary files /dev/null and b/src/images/acceptP.png differ diff --git a/src/images/checked.png b/src/images/checked.png new file mode 100644 index 0000000..adf5e92 Binary files /dev/null and b/src/images/checked.png differ diff --git a/src/images/checkedH.png b/src/images/checkedH.png new file mode 100644 index 0000000..ed7196a Binary files /dev/null and b/src/images/checkedH.png differ diff --git a/src/images/disconD.png b/src/images/disconD.png new file mode 100644 index 0000000..3574afb Binary files /dev/null and b/src/images/disconD.png differ diff --git a/src/images/disconH.png b/src/images/disconH.png new file mode 100644 index 0000000..fea9b83 Binary files /dev/null and b/src/images/disconH.png differ diff --git a/src/images/disconN.png b/src/images/disconN.png new file mode 100644 index 0000000..3c9f83b Binary files /dev/null and b/src/images/disconN.png differ diff --git a/src/images/disconP.png b/src/images/disconP.png new file mode 100644 index 0000000..ce0ce67 Binary files /dev/null and b/src/images/disconP.png differ diff --git a/src/images/logoutD.png b/src/images/logoutD.png new file mode 100644 index 0000000..dfd6183 Binary files /dev/null and b/src/images/logoutD.png differ diff --git a/src/images/logoutH.png b/src/images/logoutH.png new file mode 100644 index 0000000..36a7b0b Binary files /dev/null and b/src/images/logoutH.png differ diff --git a/src/images/logoutN.png b/src/images/logoutN.png new file mode 100644 index 0000000..279e5a4 Binary files /dev/null and b/src/images/logoutN.png differ diff --git a/src/images/logoutP.png b/src/images/logoutP.png new file mode 100644 index 0000000..f65f50b Binary files /dev/null and b/src/images/logoutP.png differ diff --git a/src/images/noH.png b/src/images/noH.png new file mode 100644 index 0000000..180de56 Binary files /dev/null and b/src/images/noH.png differ diff --git a/src/images/noN.png b/src/images/noN.png new file mode 100644 index 0000000..2875caf Binary files /dev/null and b/src/images/noN.png differ diff --git a/src/images/noP.png b/src/images/noP.png new file mode 100644 index 0000000..c4a685b Binary files /dev/null and b/src/images/noP.png differ diff --git a/src/images/notchecked.png b/src/images/notchecked.png new file mode 100644 index 0000000..1441e35 Binary files /dev/null and b/src/images/notchecked.png differ diff --git a/src/images/notcheckedH.png b/src/images/notcheckedH.png new file mode 100644 index 0000000..644759d Binary files /dev/null and b/src/images/notcheckedH.png differ diff --git a/src/images/off.png b/src/images/off.png new file mode 100644 index 0000000..bede8db Binary files /dev/null and b/src/images/off.png differ diff --git a/src/images/okH.png b/src/images/okH.png new file mode 100644 index 0000000..7d776bc Binary files /dev/null and b/src/images/okH.png differ diff --git a/src/images/okN.png b/src/images/okN.png new file mode 100644 index 0000000..a8f38c5 Binary files /dev/null and b/src/images/okN.png differ diff --git a/src/images/okP.png b/src/images/okP.png new file mode 100644 index 0000000..cdae71d Binary files /dev/null and b/src/images/okP.png differ diff --git a/src/images/on.png b/src/images/on.png new file mode 100644 index 0000000..8c8b955 Binary files /dev/null and b/src/images/on.png differ diff --git a/src/images/optionsH.png b/src/images/optionsH.png new file mode 100644 index 0000000..3d12cd8 Binary files /dev/null and b/src/images/optionsH.png differ diff --git a/src/images/optionsN.png b/src/images/optionsN.png new file mode 100644 index 0000000..3c0ea6a Binary files /dev/null and b/src/images/optionsN.png differ diff --git a/src/images/optionsP.png b/src/images/optionsP.png new file mode 100644 index 0000000..22bdf61 Binary files /dev/null and b/src/images/optionsP.png differ diff --git a/src/images/rejectH.png b/src/images/rejectH.png new file mode 100644 index 0000000..45e2a73 Binary files /dev/null and b/src/images/rejectH.png differ diff --git a/src/images/rejectN.png b/src/images/rejectN.png new file mode 100644 index 0000000..8dbb876 Binary files /dev/null and b/src/images/rejectN.png differ diff --git a/src/images/rejectP.png b/src/images/rejectP.png new file mode 100644 index 0000000..e515413 Binary files /dev/null and b/src/images/rejectP.png differ diff --git a/src/images/sendD.png b/src/images/sendD.png new file mode 100644 index 0000000..96eecc4 Binary files /dev/null and b/src/images/sendD.png differ diff --git a/src/images/sendH.png b/src/images/sendH.png new file mode 100644 index 0000000..4bbb569 Binary files /dev/null and b/src/images/sendH.png differ diff --git a/src/images/sendN.png b/src/images/sendN.png new file mode 100644 index 0000000..8165882 Binary files /dev/null and b/src/images/sendN.png differ diff --git a/src/images/sendP.png b/src/images/sendP.png new file mode 100644 index 0000000..7ca926a Binary files /dev/null and b/src/images/sendP.png differ diff --git a/src/images/signingD.png b/src/images/signingD.png new file mode 100644 index 0000000..d3e2294 Binary files /dev/null and b/src/images/signingD.png differ diff --git a/src/images/signingH.png b/src/images/signingH.png new file mode 100644 index 0000000..ab79125 Binary files /dev/null and b/src/images/signingH.png differ diff --git a/src/images/signingN.png b/src/images/signingN.png new file mode 100644 index 0000000..ff7349d Binary files /dev/null and b/src/images/signingN.png differ diff --git a/src/images/signingP.png b/src/images/signingP.png new file mode 100644 index 0000000..ab79125 Binary files /dev/null and b/src/images/signingP.png differ diff --git a/src/images/signuptD.png b/src/images/signuptD.png new file mode 100644 index 0000000..abf42ff Binary files /dev/null and b/src/images/signuptD.png differ diff --git a/src/images/signuptH.png b/src/images/signuptH.png new file mode 100644 index 0000000..097dfbe Binary files /dev/null and b/src/images/signuptH.png differ diff --git a/src/images/signuptN.png b/src/images/signuptN.png new file mode 100644 index 0000000..6aafb6d Binary files /dev/null and b/src/images/signuptN.png differ diff --git a/src/images/signuptP.png b/src/images/signuptP.png new file mode 100644 index 0000000..4584f56 Binary files /dev/null and b/src/images/signuptP.png differ diff --git a/src/images/yesH.png b/src/images/yesH.png new file mode 100644 index 0000000..6018854 Binary files /dev/null and b/src/images/yesH.png differ diff --git a/src/images/yesN.png b/src/images/yesN.png new file mode 100644 index 0000000..0a97030 Binary files /dev/null and b/src/images/yesN.png differ diff --git a/src/images/yesP.png b/src/images/yesP.png new file mode 100644 index 0000000..5412974 Binary files /dev/null and b/src/images/yesP.png differ