瀏覽代碼

Merge remote-tracking branch 'origin/master'

Conflicts:
	src/command/PrivateMessage.java
Frans 9 年之前
父節點
當前提交
6a694787db

+ 14 - 11
src/client/ChatWindow.java

@@ -23,6 +23,7 @@ import javax.swing.*;
 import javax.swing.border.EmptyBorder;
 
 import common.Message;
+import common.Message.MessageType;
 
 @SuppressWarnings("serial")
 public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyListener{
@@ -104,13 +105,13 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
 		if (so.isConnected() && !so.isClosed())
 			out.println(text);
 		else {
-			chat.log("Not connected to server!");
+			chat.log(new Message("Not connected to server!", MessageType.WARNING, false));
 			write.setEnabled(false);
 		}
 	}
 	
 	void connect(String address, int port, String username) {
-		chat.log("Connecting to " + address + " on port " + port + ".");
+		chat.log(new Message("Connecting to " + address + " on port " + port + ".", MessageType.INFO, false));
 		if (getMessages != null)
 			getMessages.interrupt();
 		
@@ -121,7 +122,7 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
 		} catch (NullPointerException ex) {
 			//Nothing
 		} catch (IOException ex) {
-			chat.log(ex.toString());
+			chat.log(new Message(ex.toString(), MessageType.ERROR, false));
 		}
 		
 		try {
@@ -130,10 +131,10 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
 			objIn =		new ObjectInputStream(so.getInputStream());
 			out =		new PrintWriter(so.getOutputStream(), true);
 		} catch (SocketTimeoutException ex) {
-			chat.log("Could not connect to server. (Connection timed out!)");
+			chat.log(new Message("Could not connect to server. (Connection timed out!)", MessageType.ERROR, false));
 			return;
 		} catch (IOException e) {
-			chat.log(e.toString());
+			chat.log(new Message(e.toString(), MessageType.ERROR, false));
 			return;
 		}
 		
@@ -156,11 +157,11 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
 		try {
 			getMessages();
 		} catch (EOFException eof) {
-			chat.log(eof.toString() + " Disconnected from host.");
+			chat.log(new Message(eof.toString() + " Disconnected from host.", MessageType.ERROR, false));
 		} catch (ClassNotFoundException cnf) {
-			chat.log("The message recieved from the server could not be understood. Are you using the right version?");
+			chat.log(new Message("The message recieved from the server could not be understood. Are you using the right version?", MessageType.ERROR, false));
 		} catch (IOException e) {
-			chat.log(e.toString());
+			chat.log(new Message(e.toString(), MessageType.ERROR, false));
 		}
 	}
 	
@@ -172,12 +173,14 @@ public class ChatWindow extends JFrame implements ActionListener, Runnable, KeyL
 				chat.log(mess);
 				
 				model = new DefaultListModel<String>();
-				for (int i = 0; i < mess.usersOnline.length; i++)
-					model.addElement(mess.usersOnline[i]);
+				for (String user: mess.usersOnline)
+					model.addElement(user);
 				
 				userList.setModel(model);
+			} else if (fromServer instanceof String) {
+				chat.log(new Message((String)fromServer, MessageType.NORMAL, false));
 			} else
-				chat.log(fromServer.toString());
+				throw new ClassNotFoundException();
 		}
 	}
 

+ 0 - 10
src/client/Console.java

@@ -1,7 +1,5 @@
 package client;
 
-import java.awt.Color;
-
 import javax.swing.*;
 import javax.swing.text.*;
 
@@ -14,14 +12,6 @@ public class Console extends JTextPane {
 		setEditable(false);
 	}
 	
-	void log(String str) {
-		SimpleAttributeSet style = new SimpleAttributeSet();
-		StyleConstants.setForeground(style, Color.RED);
-		StyleConstants.setBold(style, true);
-		
-		SwingUtilities.invokeLater(new AppendThread(str, style, this.getStyledDocument()));
-	}
-	
 	void log(Message mess) {
 		SwingUtilities.invokeLater(new AppendThread(mess.toString(), mess.style, this.getStyledDocument()));
 	}

+ 4 - 4
src/command/Ban.java

@@ -1,8 +1,9 @@
 package command;
 
 import java.util.InputMismatchException;
-import java.util.Scanner;
 
+import common.Message;
+import common.Message.MessageType;
 import server.Client;
 import server.CommandHandler;
 import server.Server;
@@ -19,20 +20,19 @@ public class Ban extends Command {
 		try {
 			victim = Server.getUserByName(args[0]);
 		}	catch (NullPointerException e) {
-			caller.send("No such user!");
+			caller.send(new Message("No such user!", MessageType.WARNING, false));
 			return;
 		}
 		
 		IP = victim.sock.getInetAddress().toString();
 		
-		
 		BanNote bn = new BanNote(IP);
 		
 		if (args.length == 1)
 			bn = new BanNote(IP);
 		else
 			try {
-				duration = new Scanner(args[1]).nextInt();
+				duration = Server.CInt(args[1]);
 				
 				if (args.length >= 3)
 					bn = new BanNote(IP, duration, CommandHandler.stringArrayToString(CommandHandler.removeFirst(CommandHandler.removeFirst(args))));

+ 3 - 1
src/command/Help.java

@@ -1,5 +1,7 @@
 package command;
 
+import common.Message;
+import common.Message.MessageType;
 import server.Client;
 import server.CommandHandler;
 
@@ -15,7 +17,7 @@ public class Help extends Command {
 			if (i + 1 < CommandHandler.commands.length)
 				help += "\n";
 		}
-		caller.send(help);
+		caller.send(new Message(help, MessageType.COMMAND, false));
 	}
 
 	@Override

+ 4 - 2
src/command/JoinChannel.java

@@ -1,5 +1,7 @@
 package command;
 
+import common.Message;
+import common.Message.MessageType;
 import server.Client;
 import server.Server;
 
@@ -15,9 +17,9 @@ public class JoinChannel extends Command {
 		try {
 			Server.getChannelByName(args[0]).add(caller);
 			caller.primaryChannel = Server.getChannelByName(args[0]);
-			caller.send("You are now speaking in channel " + args[0] + ".");
+			caller.send(new Message("You are now speaking in channel " + args[0] + ".", MessageType.COMMAND, false));
 		} catch (NullPointerException ex) {
-			caller.send("No such channel!");
+			caller.send(new Message("No such channel!", MessageType.ERROR, false));
 		}
 	}
 

+ 3 - 1
src/command/Kick.java

@@ -1,5 +1,7 @@
 package command;
 
+import common.Message;
+import common.Message.MessageType;
 import server.Client;
 import server.Server;
 
@@ -10,7 +12,7 @@ public class Kick extends Command {
 		try {
 			Server.getUserByName(args[0]).disconnect(false);
 		} catch (NullPointerException ex) {
-			caller.send("No user called " + args[0] + "!");
+			caller.send(new Message("No user called " + args[0] + "!", MessageType.ERROR, false));
 		}
 	}
 

+ 5 - 3
src/command/LeaveChannel.java

@@ -1,5 +1,7 @@
 package command;
 
+import common.Message;
+import common.Message.MessageType;
 import server.Client;
 import server.Server;
 
@@ -16,10 +18,10 @@ public class LeaveChannel extends Command{
 			Server.getChannelByName(args[0]).remove(caller);
 			if (caller.primaryChannel.equals(Server.getChannelByName(args[0])))
 				caller.primaryChannel = Server.channels.get(0);
-			caller.send("You left channel " + args[0] + ".");
-			caller.send("You are now speaking in channel " + caller.primaryChannel.name + ".");
+			caller.send(new Message("You left channel " + args[0] + ".", MessageType.COMMAND, false));
+			caller.send(new Message("You are now speaking in channel " + caller.primaryChannel.name + ".", MessageType.COMMAND, false));
 		} catch(NullPointerException ex) {
-			caller.send("No channel named " + args[0] + ".");
+			caller.send(new Message("No channel named " + args[0] + ".", MessageType.ERROR, false));
 			return;
 		}
 	}

+ 5 - 3
src/command/List.java

@@ -1,5 +1,7 @@
 package command;
 
+import common.Message;
+import common.Message.MessageType;
 import server.Client;
 import server.Server;
 
@@ -14,16 +16,16 @@ public class List extends Command {
 			try {
 				arr = Server.getChannelByName(channelName).listClients();
 			} catch (NullPointerException ex) {
-				caller.send("No channel named " + channelName + ".");
+				caller.send(new Message("No channel named " + channelName + ".", MessageType.ERROR, false));
 				return;
 			}
 		} else
 			arr =  Server.listClients();
 		
 		if (channelName == null)
-			caller.send("Users online are:" + "\n" + arr);
+			caller.send(new Message("Users online are:" + "\n" + arr, MessageType.COMMAND, false));
 		else
-			caller.send("Users in channel " + channelName + " are:" + "\n" + arr);
+			caller.send(new Message("Users in channel " + channelName + " are:" + "\n" + arr, MessageType.COMMAND, false));
 	}
 
 	@Override

+ 4 - 4
src/command/PrivateMessage.java

@@ -10,7 +10,7 @@ public class PrivateMessage extends Command {
 	@Override
 	public void execute(String[] args, Client caller) {
 		Client reciever = Server.getUserByName(args[0]);
-			
+
 		if (caller.equals(reciever)) {
 			caller.send("Please don't speak with yourself.");
 			return;
@@ -19,10 +19,10 @@ public class PrivateMessage extends Command {
 			return;
 		}
 		
-		Message mess = new Message("PM", caller.username, CommandHandler.stringArrayToString(CommandHandler.removeFirst(args)));
-		
+		Message mess = new Message("PM", caller.username, CommandHandler.stringArrayToString(CommandHandler.removeFirst(args)), Message.MessageType.PM);
+
 		reciever.send(mess); caller.send(mess);
-		
+
 	}
 
 	@Override

+ 44 - 12
src/common/Message.java

@@ -12,23 +12,37 @@ import server.Server;
 
 @SuppressWarnings("serial")
 public class Message implements java.io.Serializable {
-	public String content, channel, sender;
+	
+	public enum MessageType {
+		PM, NORMAL, WARNING, ERROR, COMMAND, INFO
+	}
+	
+	public MessageType messType = MessageType.NORMAL;
+	public String content = "", channel = "", sender = "";
 	public SimpleAttributeSet style = new SimpleAttributeSet();
 	public String[] usersOnline;
+	public boolean preInfo = true;
 	
-	public Message(String channel, String send, String con) {
+	public Message(String channel, String send, String con, MessageType messType) {
 		this.sender = send;
 		this.channel = channel;
 		this.content = con;
+		this.messType = messType;
 		usersOnline = Server.getUsersOnline();
 	}
 	
-	public Message(String send, String con) {
-		this("Info", send, con);
+	public Message(String sender, String con) {
+		this("", sender, con, MessageType.NORMAL);
 	}
 	
 	public Message(String con) {
-		this("SERVER", con);
+		this("Info", "SERVER", con, MessageType.INFO);
+	}
+	
+	public Message(String con, MessageType messType, boolean preInfo) { //TODO Needs to include Server.getUsersOnline() to prevent NullPointerException
+		this.content = con;
+		this.preInfo = preInfo; 
+		this.messType = messType;
 	}
 	
 	public boolean validate() {
@@ -40,19 +54,37 @@ public class Message implements java.io.Serializable {
 	
 	@Override
 	public String toString() {
-		if (channel.equals("PM"))
-			StyleConstants.setForeground(style, Color.GRAY);
-		else if (channel.equals("Info"))
+		switch (messType) {
+		case COMMAND:
+			StyleConstants.setForeground(style, Color.GREEN);
+			break;
+		case ERROR:
+			StyleConstants.setForeground(style, Color.RED);
+			break;
+		case INFO:
 			StyleConstants.setForeground(style, Color.BLUE);
+			break;
+		case NORMAL:
+			break;
+		case PM:
+			StyleConstants.setForeground(style, Color.GRAY);
+			break;
+		case WARNING:
+			StyleConstants.setForeground(style, Color.YELLOW);
+			break;
+		default:
+			break;
+		}	
 		
 		DateFormat dateFormat = new SimpleDateFormat("[HH:mm:ss]");
 		Date time = new Date();
 		String timestamp = dateFormat.format(time);
 		
-		String messEntry;
-		
-		messEntry =  timestamp + "<" + channel + ">" + sender + ": " + content;
+		String preInfoStr = timestamp + "<" + channel + ">" + sender + ": ";
 		
-		return messEntry;
+		if (preInfo)
+			return preInfoStr + content;
+		else
+			return content;
 	}
 }

+ 1 - 1
src/server/Client.java

@@ -133,7 +133,7 @@ public class Client implements Runnable, ActionListener {
 			while (!readuser.isInterrupted() && ((lastMess = in.readLine()) != null)) {
 				if (lastMess.startsWith("/")) //Command handling
 				{
-					String[] commandarray = lastMess.toLowerCase().substring(1).split(" ");
+					String[] commandarray = lastMess.substring(1).split(" ");
 					CommandHandler.executeCommand(commandarray, this);
 				}
 				else //Normal message handling

+ 1 - 1
src/server/Server.java

@@ -132,7 +132,7 @@ public class Server {
 		System.exit(0);
 	}
 	
-	static int CInt(String str) {
+	public static int CInt(String str) {
 		int i;
 		Scanner sc = new Scanner(str);
 		i = sc.nextInt();