From: <le...@us...> - 2007-10-16 12:11:28
|
Revision: 5 http://rochat.svn.sourceforge.net/rochat/?rev=5&view=rev Author: levia Date: 2007-10-16 05:11:22 -0700 (Tue, 16 Oct 2007) Log Message: ----------- - Made a small mistake. Added Paths: ----------- trunk/src/net/sensiva/rochat/ui/ trunk/src/net/sensiva/rochat/ui/init/ trunk/src/net/sensiva/rochat/ui/init/Initializer.java Removed Paths: ------------- trunk/src/net/sensiva/rochat/init/ Added: trunk/src/net/sensiva/rochat/ui/init/Initializer.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/init/Initializer.java (rev 0) +++ trunk/src/net/sensiva/rochat/ui/init/Initializer.java 2007-10-16 12:11:22 UTC (rev 5) @@ -0,0 +1,20 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.init; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-16 13:26:33
|
Revision: 9 http://rochat.svn.sourceforge.net/rochat/?rev=9&view=rev Author: levia Date: 2007-10-16 06:26:31 -0700 (Tue, 16 Oct 2007) Log Message: ----------- - Added logging, still working on redirecting error stream. - Changed look and feel to be system native. Modified Paths: -------------- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java Added Paths: ----------- trunk/src/net/sensiva/rochat/core/ trunk/src/net/sensiva/rochat/core/logging/ trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java trunk/src/net/sensiva/rochat/core/logging/LogListener.java trunk/src/net/sensiva/rochat/core/logging/Logger.java Added: trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java 2007-10-16 13:26:31 UTC (rev 9) @@ -0,0 +1,60 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.core.logging; + +import java.util.*; +import java.io.*; + +/** + * This class is a thread that runs to listen for errors (exceptions, etc). +*/ +public class ErrorThread extends Thread +{ + private PipedInputStream m_ErrInput; + + public ErrorThread(PipedInputStream errorInput) + { + m_ErrInput = errorInput; + } + + public void run() + { + final byte[] buf = new byte[1024]; + try + { + while (true) + { + final int len = m_ErrInput.read(buf); + if (len == -1) + { + break; + } + String message = new String(buf, 0, len); + System.out.println(message); + Logger.getInstance().log(Logger.LOG_ERROR, message); + } + } + catch (IOException e) + { + } + } + +} + Added: trunk/src/net/sensiva/rochat/core/logging/LogListener.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/LogListener.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/logging/LogListener.java 2007-10-16 13:26:31 UTC (rev 9) @@ -0,0 +1,37 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.core.logging; + +import java.util.Date; + +/** + * The listener interface. +*/ +public interface LogListener +{ + /** + * The logEvent function handles the logged events. + * + * @param severity The severity of this message. + * @param message The actual message. + * @param timeStamp The timestamp of the event. + */ + public void logEvent(String severity, String message, Date timeStamp); +} \ No newline at end of file Added: trunk/src/net/sensiva/rochat/core/logging/Logger.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/Logger.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/logging/Logger.java 2007-10-16 13:26:31 UTC (rev 9) @@ -0,0 +1,139 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.core.logging; + +import java.util.*; +import java.io.*; + +/** + * The Logger class, here you can listen for log messages, and log them + * yourself, or you can let the standard logger take over. +*/ +public class Logger +{ + /** Severity used for information - nothings wrong, just info. */ + public static final String LOG_INFORMATION = "INFORMATION"; + + /** Severity used for warnings - there was something wrong, but we can + continue without hassle. */ + public static final String LOG_WARNING = "WARNING"; + + /** Severity used for error - there was an error, and we needed to stop our + current execution. */ + public static final String LOG_ERROR = "ERROR"; + + private List<LogListener> m_LogListeners = new ArrayList<LogListener>(); + private static Logger m_Instance = new Logger(); + + private PipedInputStream m_ErrInput; + private PipedOutputStream m_ErrOutput; + + /** + * The Logger constructor. + */ + public Logger() + { + try + { + m_ErrInput = new PipedInputStream(); + m_ErrOutput = new PipedOutputStream(m_ErrInput); + System.setErr(new PrintStream(m_ErrOutput, true)); + } + catch (Exception e) + { + e.printStackTrace(); + } + System.out.println("Blaat"); + new ErrorThread(m_ErrInput).start(); + throw new RuntimeException(); + } + + /** + * This functions logs a message. + * + * @param severity The severity of the event. + * @param message The message. + */ + public void log(String severity, String message) + { + if (m_LogListeners.isEmpty()) + { + // error + System.out.println(severity + " : " + message); + } + Date timeStamp = new Date(); + for (int i = 0; i < m_LogListeners.size(); i++) + { + LogListener listener = (LogListener)m_LogListeners.get(i); + listener.logEvent(severity, message, timeStamp); + } + + } + + /** + * This function logs a message. + * + * @param message The message. + */ + public void log(String message) + { + if (m_LogListeners.isEmpty()) + { + // error + } + Date timeStamp = new Date(); + for (int i = 0; i < m_LogListeners.size(); i++) + { + LogListener listener = (LogListener)m_LogListeners.get(i); + listener.logEvent(LOG_INFORMATION, message, timeStamp); + } + } + + /** + * Method to register a listener for the log. + * + * @param listener The listener. + */ + public void addListener(LogListener listener) + { + m_LogListeners.add(listener); + } + + /** + * Method to unregister a listener for the log. + * + * @param listener The listener to remove. + */ + public void removeListener(LogListener listener) + { + m_LogListeners.remove(listener); + } + + /** + * Get the instance (singleton) + * + * @return This class. + */ + public static Logger getInstance() + { + return m_Instance; + } + +} Modified: trunk/src/net/sensiva/rochat/ui/main/MainFrame.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-16 13:21:24 UTC (rev 8) +++ trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-16 13:26:31 UTC (rev 9) @@ -37,6 +37,16 @@ */ public void initialize() { + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception e) + { + e.printStackTrace(); + } + + m_MainFrame = new JFrame("ROChat"); m_Tabs = new JTabbedPane(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-16 14:06:30
|
Revision: 10 http://rochat.svn.sourceforge.net/rochat/?rev=10&view=rev Author: levia Date: 2007-10-16 07:06:27 -0700 (Tue, 16 Oct 2007) Log Message: ----------- - Removed ErrorThread, redirecting of error stream failed since when exceptions get there, the app closes anyway. - Changed the Logger a bit. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/logging/LogListener.java trunk/src/net/sensiva/rochat/core/logging/Logger.java trunk/src/net/sensiva/rochat/ui/main/MainFrame.java Removed Paths: ------------- trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java Deleted: trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java 2007-10-16 13:26:31 UTC (rev 9) +++ trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java 2007-10-16 14:06:27 UTC (rev 10) @@ -1,60 +0,0 @@ -/* - ROChat - Copyright (C) 2007 The ROChat team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - ---------------------------------------------------------------------------- -*/ -package net.sensiva.rochat.ui.core.logging; - -import java.util.*; -import java.io.*; - -/** - * This class is a thread that runs to listen for errors (exceptions, etc). -*/ -public class ErrorThread extends Thread -{ - private PipedInputStream m_ErrInput; - - public ErrorThread(PipedInputStream errorInput) - { - m_ErrInput = errorInput; - } - - public void run() - { - final byte[] buf = new byte[1024]; - try - { - while (true) - { - final int len = m_ErrInput.read(buf); - if (len == -1) - { - break; - } - String message = new String(buf, 0, len); - System.out.println(message); - Logger.getInstance().log(Logger.LOG_ERROR, message); - } - } - catch (IOException e) - { - } - } - -} - Modified: trunk/src/net/sensiva/rochat/core/logging/LogListener.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/LogListener.java 2007-10-16 13:26:31 UTC (rev 9) +++ trunk/src/net/sensiva/rochat/core/logging/LogListener.java 2007-10-16 14:06:27 UTC (rev 10) @@ -17,7 +17,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ---------------------------------------------------------------------------- */ -package net.sensiva.rochat.ui.core.logging; +package net.sensiva.rochat.core.logging; import java.util.Date; Modified: trunk/src/net/sensiva/rochat/core/logging/Logger.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/Logger.java 2007-10-16 13:26:31 UTC (rev 9) +++ trunk/src/net/sensiva/rochat/core/logging/Logger.java 2007-10-16 14:06:27 UTC (rev 10) @@ -17,7 +17,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ---------------------------------------------------------------------------- */ -package net.sensiva.rochat.ui.core.logging; +package net.sensiva.rochat.core.logging; import java.util.*; import java.io.*; @@ -42,57 +42,60 @@ private List<LogListener> m_LogListeners = new ArrayList<LogListener>(); private static Logger m_Instance = new Logger(); - private PipedInputStream m_ErrInput; - private PipedOutputStream m_ErrOutput; - /** * The Logger constructor. */ public Logger() { - try + } + + /** + * Logs a information message. + * + * @param message The message to log. + * \return void. + */ + public void info(String message) + { + if (m_LogListeners.isEmpty()) { - m_ErrInput = new PipedInputStream(); - m_ErrOutput = new PipedOutputStream(m_ErrInput); - System.setErr(new PrintStream(m_ErrOutput, true)); + // error } - catch (Exception e) + Date timeStamp = new Date(); + for (int i = 0; i < m_LogListeners.size(); i++) { - e.printStackTrace(); + LogListener listener = (LogListener)m_LogListeners.get(i); + listener.logEvent(LOG_INFORMATION, message, timeStamp); } - System.out.println("Blaat"); - new ErrorThread(m_ErrInput).start(); - throw new RuntimeException(); } /** - * This functions logs a message. + * Logs a warning message. * - * @param severity The severity of the event. - * @param message The message. + * @param message The warning message to log. + * \return void. */ - public void log(String severity, String message) + public void warning(String message) { if (m_LogListeners.isEmpty()) { // error - System.out.println(severity + " : " + message); } Date timeStamp = new Date(); for (int i = 0; i < m_LogListeners.size(); i++) { LogListener listener = (LogListener)m_LogListeners.get(i); - listener.logEvent(severity, message, timeStamp); + listener.logEvent(LOG_WARNING, message, timeStamp); } - } /** - * This function logs a message. + * Logs a error message. * - * @param message The message. + * @param message The error message to log. + * \return void. */ - public void log(String message) + public void error(String message) { if (m_LogListeners.isEmpty()) { @@ -102,7 +105,7 @@ for (int i = 0; i < m_LogListeners.size(); i++) { LogListener listener = (LogListener)m_LogListeners.get(i); - listener.logEvent(LOG_INFORMATION, message, timeStamp); + listener.logEvent(LOG_ERROR, message, timeStamp); } } Modified: trunk/src/net/sensiva/rochat/ui/main/MainFrame.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-16 13:26:31 UTC (rev 9) +++ trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-16 14:06:27 UTC (rev 10) @@ -19,6 +19,7 @@ */ package net.sensiva.rochat.ui.main; +import net.sensiva.rochat.core.logging.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; @@ -46,7 +47,6 @@ e.printStackTrace(); } - m_MainFrame = new JFrame("ROChat"); m_Tabs = new JTabbedPane(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-17 14:57:20
|
Revision: 15 http://rochat.svn.sourceforge.net/rochat/?rev=15&view=rev Author: levia Date: 2007-10-17 07:55:46 -0700 (Wed, 17 Oct 2007) Log Message: ----------- - Completed configuration manager, now parses and writes a XML file. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java trunk/src/net/sensiva/rochat/ui/main/MainFrame.java Modified: trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java =================================================================== --- trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java 2007-10-17 14:23:58 UTC (rev 14) +++ trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java 2007-10-17 14:55:46 UTC (rev 15) @@ -53,11 +53,13 @@ { public void run() { + Logger.getInstance().info("ROChat shutting down, executing listeners.."); for (Iterator<ShutdownListener> it = m_Listeners.iterator(); it.hasNext();) { ShutdownListener entry = it.next(); entry.onShutdown(); } + Logger.getInstance().info("Listeners execution completed."); saveToFile(); } }); @@ -111,38 +113,53 @@ */ public boolean loadFile(String file) { + Logger logger = Logger.getInstance(); try { if (!m_KeyMap.isEmpty()) { m_KeyMap.clear(); } - Properties p = new Properties(); + m_File = new File(file); if (!m_File.exists()) { m_File.createNewFile(); return false; } - p.load(new FileInputStream(file)); - Iterator<Map.Entry<Object, Object>> it = p.entrySet().iterator(); - for (; it.hasNext();) + logger.info("Settings file parsing started: " + file); + + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document doc = builder.parse(file); + NodeList nodes = doc.getElementsByTagName("Setting"); + + for (int i = 0; i < nodes.getLength(); i++) { - Map.Entry<Object, Object> entry = it.next(); - m_KeyMap.put((String)entry.getKey(), (String)entry.getValue()); + Element element = (Element)nodes.item(i); + + String key = element.getAttribute("key"); + if (key.isEmpty()) + { + logger.warning("Found a setting entry, but couldn't find a key. Skipping."); + continue; + } + + String value = element.getTextContent(); + value = value.trim(); + + System.out.println(key + " : " + value); + + m_KeyMap.put(key, value); } + logger.info("Successfully parsed settings file."); } - catch (FileNotFoundException fnfe) + catch (Exception ioe) { - Logger.getInstance().error(fnfe.getMessage()); + logger.error("Parsing of settings file failed."); + logger.error(ioe.getMessage()); return false; } - catch (IOException ioe) - { - Logger.getInstance().error(ioe.getMessage()); - return false; - } return true; } @@ -241,24 +258,54 @@ */ public boolean saveToFile(String file) { + Logger logger = Logger.getInstance(); try { File propertyFile = new File(file); - BufferedWriter bf = new BufferedWriter(new FileWriter(propertyFile)); + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + DOMImplementation impl = builder.getDOMImplementation(); + Document doc = impl.createDocument(null, null, null); + + logger.info("Saving settings file: " + file); - Iterator<Map.Entry<String, String>> it = m_KeyMap.entrySet().iterator(); - - for (; it.hasNext();) + Element root = doc.createElement("Settings"); + doc.appendChild(root); + + for (Iterator<Map.Entry<String, String>> it = m_KeyMap.entrySet().iterator(); it.hasNext();) { - Map.Entry<String, String> entry = it.next(); - bf.write(entry.getKey() + " = " + entry.getValue() + "\n"); + Map.Entry entry = it.next(); + String key = (String)entry.getKey(); + String value = (String)entry.getValue(); + + Element settingNode = doc.createElement("Setting"); + root.appendChild(settingNode); + settingNode.setAttribute("key", key); + + settingNode.setTextContent(value); } - bf.close(); + + FileOutputStream fos; + Transformer transformer; + fos = new FileOutputStream(file); + + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + transformer = transformerFactory.newTransformer(); + + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); + transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(fos); + transformer.transform(source, result); + logger.info("Saving settings file completed successfully."); return true; } - catch (IOException ioe) + catch (Exception ioe) { - ioe.printStackTrace(); + logger.error("Saving of settings file failed."); + logger.error(ioe.getMessage()); return false; } } Modified: trunk/src/net/sensiva/rochat/ui/main/MainFrame.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-17 14:23:58 UTC (rev 14) +++ trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-17 14:55:46 UTC (rev 15) @@ -21,6 +21,7 @@ import net.sensiva.rochat.core.logging.*; import net.sensiva.rochat.ui.tabs.*; +import net.sensiva.rochat.core.config.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; @@ -48,6 +49,8 @@ Logger.getInstance().error(e.getMessage()); } + ConfigurationManager.getInstance().loadFile("settings.xml"); + m_MainFrame = new JFrame("ROChat"); m_Tabs = new JTabbedPane(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-19 14:31:11
|
Revision: 24 http://rochat.svn.sourceforge.net/rochat/?rev=24&view=rev Author: levia Date: 2007-10-19 07:30:57 -0700 (Fri, 19 Oct 2007) Log Message: ----------- - Added UI Util. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/util/Constants.java Added Paths: ----------- trunk/src/net/sensiva/rochat/ui/util/ trunk/src/net/sensiva/rochat/ui/util/Util.java Modified: trunk/src/net/sensiva/rochat/core/util/Constants.java =================================================================== --- trunk/src/net/sensiva/rochat/core/util/Constants.java 2007-10-19 14:20:13 UTC (rev 23) +++ trunk/src/net/sensiva/rochat/core/util/Constants.java 2007-10-19 14:30:57 UTC (rev 24) @@ -30,8 +30,6 @@ "Joshua 'Spike1506' Luckers"; public static final String THANKS_TO = ""; - public static final String BUG_EMAIL = "bu...@op..."; - public static final String ROCHAT_VERSION = "0.1.0"; public static final String OS_NAME = System.getProperty("os.name"); Added: trunk/src/net/sensiva/rochat/ui/util/Util.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/util/Util.java (rev 0) +++ trunk/src/net/sensiva/rochat/ui/util/Util.java 2007-10-19 14:30:57 UTC (rev 24) @@ -0,0 +1,95 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.util; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import javax.swing.border.*; + +/** + * The Util class. +*/ +public class Util +{ + private static Timer m_Timer; + + /** + * Linkifies a label. This method sets the color, and changes it's cursor + * to the standard link cursor. + * + * @param label The label to linkify. + * @param underline Whether to underline the link or not. + */ + public static void linkify(JLabel label, boolean underline) + { + label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + label.setForeground(Color.blue); + if (underline) + { + label.setText("<html><u>" + label.getText() + "</u></html>"); + } + } + + /** + * Flashes a component in given color. The border of the component flashes + * to the color given and is then set back to it's old border. It does + * this 7 times, and changes it's state every 150 miliseconds. + * + * @param comp The component to flash. + * @param color The color to use when flashing. + */ + public static void flashComponent(final JComponent comp, final Color color) + { + final Border oldBorder = comp.getBorder(); + m_Timer = new Timer(150, new ActionListener() + { + private int count = 0; + public void actionPerformed(ActionEvent evt) + { + if (count != 7) + { + if (!comp.getBorder().equals(oldBorder)) + { + ++count; + comp.setBorder(oldBorder); + } + else + { + comp.setBorder(BorderFactory.createLineBorder(color)); + } + } + else + { + m_Timer.stop(); + } + } + } + + ); + m_Timer.start(); + } +} + + + + + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-21 15:44:34
|
Revision: 27 http://rochat.svn.sourceforge.net/rochat/?rev=27&view=rev Author: levia Date: 2007-10-21 08:43:44 -0700 (Sun, 21 Oct 2007) Log Message: ----------- - Added IRCConnection. - Added listener system for it. - You can now connect to irc servers, hardcoded, and hacky. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/connection/Connection.java trunk/src/net/sensiva/rochat/ui/main/MainFrame.java trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java Added Paths: ----------- trunk/src/net/sensiva/rochat/core/connection/ConnectionListener.java trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java Modified: trunk/src/net/sensiva/rochat/core/connection/Connection.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/Connection.java 2007-10-21 13:55:30 UTC (rev 26) +++ trunk/src/net/sensiva/rochat/core/connection/Connection.java 2007-10-21 15:43:44 UTC (rev 27) @@ -23,11 +23,59 @@ //import java.io.*; /** - * The interface for connections made. + * The abstract class for connections made. */ -public interface Connection +public abstract class Connection extends Thread { - public void connect(String host, int port); - public void disconnect(); + protected List<ConnectionListener> m_ConnectionListener = new ArrayList<ConnectionListener>(); + + public abstract void connect(String host, int port); + public abstract void disconnect(); + + public void addConnectionListener(ConnectionListener listener) + { + m_ConnectionListener.add(listener); + } + + public void removeConnectionListener(ConnectionListener listener) + { + m_ConnectionListener.remove(listener); + } + + protected void fireConnected() + { + for (Iterator it = m_ConnectionListener.iterator(); it.hasNext();) + { + ConnectionListener listener = (ConnectionListener)it.next(); + listener.connected(); + } + } + + protected void fireDisconnected() + { + for (Iterator it = m_ConnectionListener.iterator(); it.hasNext();) + { + ConnectionListener listener = (ConnectionListener)it.next(); + listener.disconnected(); + } + } + + protected void fireServerMessageReceived(String message) + { + for (Iterator it = m_ConnectionListener.iterator(); it.hasNext();) + { + ConnectionListener listener = (ConnectionListener)it.next(); + listener.serverMessageReceived(message); + } + } + + protected void fireMessageReceived(String message) + { + for (Iterator it = m_ConnectionListener.iterator(); it.hasNext();) + { + ConnectionListener listener = (ConnectionListener)it.next(); + listener.messageReceived(message); + } + } } Added: trunk/src/net/sensiva/rochat/core/connection/ConnectionListener.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/ConnectionListener.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/connection/ConnectionListener.java 2007-10-21 15:43:44 UTC (rev 27) @@ -0,0 +1,55 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.core.connection; + +import java.util.*; +//import java.io.*; + +/** + * A listener for connections. +*/ +public interface ConnectionListener +{ + /** + * Called when the connection connected to a server. + */ + public void connected(); + + /** + * Called when the connection disconnected from the server. + */ + public void disconnected(); + + /** + * Called when a message has been received from the server. + * + * @param message The message that has been received. + */ + public void serverMessageReceived(String message); + + /** + * Called when a message should be shown to the user. + * + * @param message The message that should be shown. + */ + public void messageReceived(String message); +} + + Added: trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-21 15:43:44 UTC (rev 27) @@ -0,0 +1,204 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.core.connection; + +import net.sensiva.rochat.core.logging.*; +import net.sensiva.rochat.core.message.*; +import java.util.*; +import java.net.*; +import java.io.*; + +/** + * The class that is able to connect to a server. +*/ +public class IRCConnection extends Connection//, Thread +{ + private Socket m_Socket; + + private BufferedReader m_In; + private PrintWriter m_Out; + + private String m_NickName = null; + private String m_UserName = null; + private String m_RealName = null; + private String m_Password = null; + + private String m_Host; + private int m_Port; + + + /** + * ServerConnection constructor. + */ + public IRCConnection(String nickName, String userName, String realName, String password) + { + m_NickName = nickName; + m_UserName = userName; + m_RealName = realName; + m_Password = password; + } + + /** + * Connects to a given server. + * + * @param host The host or IP address. + * @param port The port of the server. + */ + public void connect(String host, int port) + { + try + { + fireMessageReceived("Attempting to connect to " + host + " on port " + port); + if (m_Socket != null && m_Socket.isConnected()) + { + disconnect(); + } + m_Socket = new Socket(host, port); + m_Socket.setKeepAlive(true); + fireConnected(); + fireMessageReceived("Successfully connected."); + m_Host = host; + m_Port = port; + + initialize(); + start(); + register(); + } + catch (UnknownHostException uhe) + { + Logger.getInstance().warning(uhe.getMessage()); + fireMessageReceived("Could not find host: " + host); + m_Socket = null; + } + catch (IOException ioe) + { + Logger.getInstance().warning(ioe.getMessage()); + fireMessageReceived("Could not connect to the server:" + ioe.getMessage()); + m_Socket = null; + } + } + + /** + * Initializes the socket, and makes it ready for use. + */ + private void initialize() + { + try + { + m_Socket.setSoTimeout(5000); + + + m_In = new BufferedReader(new InputStreamReader(m_Socket.getInputStream())); + m_Out = new PrintWriter(new OutputStreamWriter(m_Socket.getOutputStream())); + } + catch (Exception e) + { + Logger.getInstance().warning(e.getMessage()); + } + } + + /** + * Disconnects from the server. + */ + public synchronized void disconnect() + { + //System.out.println(":D"); + try + { + if (!m_Socket.isClosed()) + { + m_Socket.close(); + fireDisconnected(); + } + } + catch (IOException ioe) + { + Logger.getInstance().warning(ioe.getMessage()); + } + } + + /** + * Starts the thread, causing it to continuously read data. + */ + public void run() + { + try + { + String line; + while (!isInterrupted()) + { + line = m_In.readLine(); + if (line != null) + { + // Parse IRC messages here. + fireServerMessageReceived(line); + + if (line.startsWith("PING")) + { + send("PONG " + line.substring(4, line.length())); + } + } + else + { + disconnect(); + } + } + } + catch (IOException exc) + { + System.out.println(":D"); + disconnect(); + } + } + + /** + * Send a message to the server. + * + * @param message The message to send. + */ + public synchronized void send(String message) + { + try + { + m_Out.write(message + "\r\n"); + m_Out.flush(); + } + catch (Exception exc) + { + Logger.getInstance().warning(exc.getMessage()); + } + } + + /** + * Register to the server. + */ + public void register() + { + if (m_Password != null) + { + send("PASS " + m_Password); + } + send("NICK "+ m_NickName); + send("USER "+ m_UserName + " " + m_Socket.getLocalAddress() + " " + m_Host + " :" + m_RealName); + } + + + +} Modified: trunk/src/net/sensiva/rochat/ui/main/MainFrame.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-21 13:55:30 UTC (rev 26) +++ trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-21 15:43:44 UTC (rev 27) @@ -20,6 +20,7 @@ package net.sensiva.rochat.ui.main; import net.sensiva.rochat.core.logging.*; +import net.sensiva.rochat.core.connection.*; import net.sensiva.rochat.ui.tabs.*; import net.sensiva.rochat.core.config.*; import javax.swing.*; @@ -70,6 +71,7 @@ contentPane.setLayout(bl); contentPane.add(m_Tabs); m_MainFrame.setContentPane(contentPane); + //Creates a statustab when the program initializes all components. StatusTab statusTab = new StatusTab(this); @@ -78,7 +80,14 @@ ChannelTab channelTab = new ChannelTab(this); channelTab.initialize("#Channel", "#Channel information!"); + IRCConnection irc = new IRCConnection("Levia_", "Levia_", "Levia_", "lars"); + statusTab.setIRCConnection(irc); + irc.connect("127.0.0.1", 6667); + //irc.send("RPL_MOTDSTART"); + //irc.send("RPL_MOTD"); + //irc.send("RPL_MOTDEND"); + } /** Modified: trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java 2007-10-21 13:55:30 UTC (rev 26) +++ trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java 2007-10-21 15:43:44 UTC (rev 27) @@ -20,13 +20,15 @@ package net.sensiva.rochat.ui.tabs; import net.sensiva.rochat.ui.main.*; +import net.sensiva.rochat.core.connection.*; import javax.swing.*; import java.awt.*; +import java.awt.event.*; /** * The tab that shows server status replies and all. */ -public class StatusTab extends JPanel implements Tab +public class StatusTab extends JPanel implements Tab, ConnectionListener { private MainFrame m_MainFrame; private int m_TabIndex; @@ -34,6 +36,8 @@ private JEditorPane m_MessageList; private JTextField m_TextField; + private IRCConnection m_Connection = null; + public StatusTab(MainFrame mainFrame) { m_MainFrame = mainFrame; @@ -58,22 +62,65 @@ setBorder(BorderFactory.createEmptyBorder(3,3,3,3)); m_MessageList = new JEditorPane(); + m_MessageList.setAutoscrolls(true); JScrollPane messagePane = new JScrollPane(m_MessageList); m_TextField = new JTextField(); m_TextField.setMaximumSize(new Dimension(Integer.MAX_VALUE,20)); + m_TextField.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent event) + { + String e = event.getActionCommand(); + + m_Connection.send(m_TextField.getText()); + m_TextField.setText(""); + } + } + ); + add(messagePane); add(Box.createRigidArea(new Dimension(0,3))); add(m_TextField); } + public void setIRCConnection(IRCConnection con) + { + if (m_Connection != null) + { + m_Connection.removeConnectionListener(this); + } + m_Connection = con; + m_Connection.addConnectionListener(this); + } + public int getTabIndex() { return m_TabIndex; } + public void connected() + { + + } + + public void disconnected() + { + + } + + public void serverMessageReceived(String message) + { + m_MessageList.setText(m_MessageList.getText() + message + "\r\n"); + } + + public void messageReceived(String message) + { + m_MessageList.setText(m_MessageList.getText() + message + "\r\n"); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-21 17:39:35
|
Revision: 29 http://rochat.svn.sourceforge.net/rochat/?rev=29&view=rev Author: levia Date: 2007-10-21 10:39:29 -0700 (Sun, 21 Oct 2007) Log Message: ----------- - Fixed menu. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java trunk/src/net/sensiva/rochat/ui/main/MenuBar.java trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java Modified: trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-21 16:11:40 UTC (rev 28) +++ trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-21 17:39:29 UTC (rev 29) @@ -20,7 +20,6 @@ package net.sensiva.rochat.core.connection; import net.sensiva.rochat.core.logging.*; -import net.sensiva.rochat.core.message.*; import java.util.*; import java.net.*; import java.io.*; @@ -102,7 +101,6 @@ { try { - //m_Socket.setSoTimeout(5000); //Socket timeout, 0 for infinite; m_Socket.setSoTimeout(0); Modified: trunk/src/net/sensiva/rochat/ui/main/MenuBar.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MenuBar.java 2007-10-21 16:11:40 UTC (rev 28) +++ trunk/src/net/sensiva/rochat/ui/main/MenuBar.java 2007-10-21 17:39:29 UTC (rev 29) @@ -45,19 +45,20 @@ menu.setToolTipText("The file menu!"); //File --> Exit - JMenuItem menuItemConn = new JMenuItem("Connect",KeyEvent.VK_C); - menuItemConn.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK)); - menuItemConn.setToolTipText("Quick connect to a server"); - menuItemConn.addActionListener(this); + JMenuItem menuItem = new JMenuItem("Connect",KeyEvent.VK_C); + menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK)); + menuItem.setToolTipText("Quick connect to a server"); + menuItem.addActionListener(this); - menu.add(menuItemConn); + menu.add(menuItem); + menu.addSeparator(); - JMenuItem menuItemExit = new JMenuItem("Exit",KeyEvent.VK_Q); - menuItemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK)); - menuItemExit.setToolTipText("Quit the application"); - menuItemExit.addActionListener(this); + menuItem = new JMenuItem("Exit",KeyEvent.VK_Q); + menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK)); + menuItem.setToolTipText("Quit the application"); + menuItem.addActionListener(this); - menu.add(menuItemExit); + menu.add(menuItem); add(menu); @@ -66,14 +67,14 @@ menu.setMnemonic(KeyEvent.VK_H); menu.setToolTipText("The help menu!"); -// //Help --> About -// menuItem = new JMenuItem("About",KeyEvent.VK_A); -// menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK)); -// menuItem.setToolTipText("Shows the about menu"); -// menuItem.addActionListener(this); -// -// menu.add(menuItem); -// add(menu); + //Help --> About + menuItem = new JMenuItem("About",KeyEvent.VK_A); + menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK)); + menuItem.setToolTipText("Shows the about menu"); + menuItem.addActionListener(this); + + menu.add(menuItem); + add(menu); } public void actionPerformed(ActionEvent event) Modified: trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java 2007-10-21 16:11:40 UTC (rev 28) +++ trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java 2007-10-21 17:39:29 UTC (rev 29) @@ -24,16 +24,17 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; +import javax.swing.text.*; /** * The tab that shows server status replies and all. */ -public class StatusTab extends JPanel implements Tab, ConnectionListener +public class StatusTab extends JPanel implements Tab, ConnectionListener, ActionListener { private MainFrame m_MainFrame; private int m_TabIndex; private JPanel m_ContentPane; - private JEditorPane m_MessageList; + private JTextPane m_MessageList; private JTextField m_TextField; private IRCConnection m_Connection = null; @@ -61,26 +62,16 @@ setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); setBorder(BorderFactory.createEmptyBorder(3,3,3,3)); - m_MessageList = new JEditorPane(); - m_MessageList.setAutoscrolls(true); + m_MessageList = new JTextPane(); + m_MessageList.setBackground(Color.WHITE); + m_MessageList.setEditable(false); JScrollPane messagePane = new JScrollPane(m_MessageList); m_TextField = new JTextField(); m_TextField.setMaximumSize(new Dimension(Integer.MAX_VALUE,20)); - m_TextField.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent event) - { - String e = event.getActionCommand(); - - m_Connection.send(m_TextField.getText()); - m_TextField.setText(""); - } - } + m_TextField.addActionListener(this); - ); - add(messagePane); add(Box.createRigidArea(new Dimension(0,3))); add(m_TextField); @@ -114,13 +105,47 @@ public void serverMessageReceived(String message) { - m_MessageList.setText(m_MessageList.getText() + message + "\r\n"); + //m_MessageList.setText(m_MessageList.getText() + message + "\r\n"); + try + { + Document doc = m_MessageList.getStyledDocument(); + + SimpleAttributeSet set = new SimpleAttributeSet(); + StyleConstants.setForeground(set, Color.BLACK); + + m_MessageList.setCaretPosition(doc.getLength()); + doc.insertString(doc.getLength(), m_TextField.getText() + "\n", set); + } + catch (Exception e) + { + e.printStackTrace(); + } } public void messageReceived(String message) { - m_MessageList.setText(m_MessageList.getText() + message + "\r\n"); + //m_MessageList.setText(m_MessageList.getText() + message + "\r\n"); + + try + { + Document doc = m_MessageList.getStyledDocument(); + + SimpleAttributeSet set = new SimpleAttributeSet(); + StyleConstants.setForeground(set, Color.BLACK); + + m_MessageList.setCaretPosition(doc.getLength()); + doc.insertString(doc.getLength(), m_TextField.getText() + "\n", set); + } + catch (Exception e) + { + e.printStackTrace(); + } } + public void actionPerformed(ActionEvent event) + { + m_Connection.send(m_TextField.getText()); + m_TextField.setText(""); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-21 18:31:20
|
Revision: 31 http://rochat.svn.sourceforge.net/rochat/?rev=31&view=rev Author: levia Date: 2007-10-21 11:31:08 -0700 (Sun, 21 Oct 2007) Log Message: ----------- - Added ServerMessageParser, but needs to be rewritten, it is copied. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java trunk/src/net/sensiva/rochat/ui/main/MainFrame.java Added Paths: ----------- trunk/src/net/sensiva/rochat/core/connection/ServerMessageParser.java Modified: trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java =================================================================== --- trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java 2007-10-21 17:44:42 UTC (rev 30) +++ trunk/src/net/sensiva/rochat/core/config/ConfigurationManager.java 2007-10-21 18:31:08 UTC (rev 31) @@ -148,8 +148,6 @@ String value = element.getTextContent(); value = value.trim(); - System.out.println(key + " : " + value); - m_KeyMap.put(key, value); } logger.info("Successfully parsed settings file."); Modified: trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-21 17:44:42 UTC (rev 30) +++ trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-21 18:31:08 UTC (rev 31) @@ -118,13 +118,13 @@ */ public synchronized void disconnect() { - //System.out.println(":D"); try { if (!m_Socket.isClosed()) { m_Socket.close(); fireDisconnected(); + fireMessageReceived("Disconnected from server."); } } catch (IOException ioe) @@ -147,11 +147,13 @@ if (line != null) { // Parse IRC messages here. + + ServerMessageParser par = new ServerMessageParser(line); fireServerMessageReceived(line); - if (line.startsWith("PING")) + if (par.getCommand().equals("PING")) { - send("PONG " + line.substring(4, line.length())); + send("PONG " + par.getTrailing()); } } else @@ -176,6 +178,7 @@ { try { + System.out.println("Sending: " + message); m_Out.write(message + "\r\n"); m_Out.flush(); } Added: trunk/src/net/sensiva/rochat/core/connection/ServerMessageParser.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/ServerMessageParser.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/connection/ServerMessageParser.java 2007-10-21 18:31:08 UTC (rev 31) @@ -0,0 +1,166 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.core.connection; + +import net.sensiva.rochat.core.logging.*; +import java.util.*; +import java.net.*; +import java.io.*; + +/** + * The class that parses the server messages. + * + * TODO: REWRITE! +*/ +public class ServerMessageParser +{ + private String m_Message; + private String m_Command; + private String m_Middle; + private String m_Trailing; + private String m_Prefix; + + public ServerMessageParser(String message) + { + m_Message = message; + parse(message); + } + + protected void parse(String message) + { + int index = 0; + int trail; + + String prefix = null, command = null, trailing = null, middle = null; + + StringBuffer buf = new StringBuffer(message); + int len = buf.length(); + + // prefix + if (buf.charAt(0) == ':') { + prefix = buf.substring(1, (index = indexOf(buf, len, ' ', index))); + index++; + } + + while (buf.charAt(index) == ' ') + index++; + + // command + command = buf.substring(index, ((index = indexOf(buf, len, ' ', index)) != -1) + ? index : (index = len)); + + while (index < len && buf.charAt(index) == ' ') + index++; + index--; + + // middle & trailing + if ((trail = indexOf(buf, len, " :", index)) != -1) + trailing = buf.substring(trail + 2, len); + else if ((trail = lastIndexOf(buf, len, ' ')) != -1 && trail >= index) + trailing = buf.substring(trail + 1, len); + middle = (index < trail) ? buf.substring(index + 1, trail) : ""; + + // save + m_Prefix = (prefix != null) ? prefix : ""; + m_Command = (command != null) ? command : ""; + m_Middle = (middle != null) ? middle : ""; + m_Trailing = (trailing != null) ? trailing : ""; + + } + + private int indexOf(StringBuffer buf, int len, int c, int i) + { + while (i < len) + { + if (buf.charAt(i++) == c) + { + return --i; + } + } + return -1; + } + + + private int indexOf(StringBuffer buf, int len, String str, int i) + { + int sublen = str.length(); + int index = -1; + int j; + for ( ; i < len; i++) + { + for (index = i, j = 0; i < len && j < sublen; i++, j++) + { + if (buf.charAt(i) != str.charAt(j)) + { + break; + } + else if (j + 1 == sublen) + { + return index; + } + } + } + return -1; + } + + private int lastIndexOf(StringBuffer buf, int len, int c) + { + int i = len; + boolean ok = false; + while (i > 0) + { + if (buf.charAt(--i) != c) + { + ok = true; + } + else if (ok) + { + return i; + } + } + return -1; + } + + public String getCommand() + { + return m_Command; + } + + public String getMiddle() + { + return m_Middle; + } + + public String getTrailing() + { + return m_Trailing; + } + + public String getPrefix() + { + return m_Prefix; + } + + public String getMessage() + { + return m_Message; + } +} + Modified: trunk/src/net/sensiva/rochat/ui/main/MainFrame.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-21 17:44:42 UTC (rev 30) +++ trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-21 18:31:08 UTC (rev 31) @@ -83,9 +83,6 @@ IRCConnection irc = new IRCConnection("Levia_", "Levia_", "Levia_", "lars"); statusTab.setIRCConnection(irc); irc.connect("127.0.0.1", 6667); - //irc.send("RPL_MOTDSTART"); - //irc.send("RPL_MOTD"); - //irc.send("RPL_MOTDEND"); } @@ -99,6 +96,7 @@ { m_MainFrame.setVisible(visible); } + /** * Adds a tab to the main window. * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-22 12:59:45
|
Revision: 34 http://rochat.svn.sourceforge.net/rochat/?rev=34&view=rev Author: levia Date: 2007-10-22 05:59:42 -0700 (Mon, 22 Oct 2007) Log Message: ----------- - Added Channel. - Changed lots of stuff in preparation for channels. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/connection/CommandParser.java trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java trunk/src/net/sensiva/rochat/ui/main/MainFrame.java trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java Added Paths: ----------- trunk/src/net/sensiva/rochat/core/connection/Channel.java Added: trunk/src/net/sensiva/rochat/core/connection/Channel.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/Channel.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/connection/Channel.java 2007-10-22 12:59:42 UTC (rev 34) @@ -0,0 +1,68 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.core.connection; + +import net.sensiva.rochat.core.logging.*; +import net.sensiva.rochat.ui.tabs.*; +import java.util.*; +import java.net.*; +import java.io.*; + +/** + * The class that is able to handle channels. +*/ +public class Channel extends Thread +{ + private String m_Channel; + private IRCConnection m_Server; + + /** + * Channels constructor. + * + * @param channel The channel this channel is handling. + * @Param server The server. + */ + public Channel(String channel, IRCConnection server) + { + m_Channel = channel; + m_Server = server; + } + + /** + * Get the channels name. + * + * \return The name of the channel. + */ + public String getChannelName() + { + return m_Channel; + } + + /** + * Get the server this channel is on. + * + * \return The server. + */ + public IRCConnection getServer() + { + return m_Server; + } +} + Modified: trunk/src/net/sensiva/rochat/core/connection/CommandParser.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/CommandParser.java 2007-10-22 10:53:05 UTC (rev 33) +++ trunk/src/net/sensiva/rochat/core/connection/CommandParser.java 2007-10-22 12:59:42 UTC (rev 34) @@ -49,16 +49,35 @@ */ public String doCommand(String command) { - String finalCommand = null; - if (command.startsWith("/")) + Tab selectedTab = m_MainFrame.getSelectedTab(); + + if (selectedTab instanceof StatusTab) { - finalCommand = command.substring(1, command.length()); + StatusTab serverTab = (StatusTab)selectedTab; + + if (command.startsWith("/")) + { + serverTab.getIRCConnection().send(command.substring(1, command.length())); + } + else + { + serverTab.addMessage("You cannot talk in a server tab."); + } } - else + else if (selectedTab instanceof ChannelTab) { - // Say in current channel. + ChannelTab channelTab = (ChannelTab)selectedTab; + + if (command.startsWith("/")) + { + channelTab.getServer().send(command.substring(1, command.length())); + } + else + { + // Talk + } } - return finalCommand; + return ""; } } Modified: trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-22 10:53:05 UTC (rev 33) +++ trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-22 12:59:42 UTC (rev 34) @@ -43,7 +43,9 @@ private String m_Host; private int m_Port; + private Vector<Channel> m_JoinedChannels = new Vector<Channel>(); + /** * ServerConnection constructor. */ @@ -202,11 +204,167 @@ send("USER "+ m_UserName + " " + m_Socket.getLocalAddress() + " " + m_Host + " :" + m_RealName); } + /** + * Join a channel. + * + * @param channel The channel to join. + * \return A reference to the channel. + */ + public Channel joinChannel(String channel) + { + if (hasJoinedChannel(channel)) + { + fireMessageReceived("You have already joined " + channel); + return null; + } + Channel chan = new Channel(channel, this); + send("JOIN " + channel); + m_JoinedChannels.add(chan); + return chan; + } + /** + * Join channels. + * + * @param channels The channels to join. + * \return A array with the channels joined. + */ + public Channel[] joinChannels(String[] channels) + { + Channel[] chans = new Channel[channels.length]; + String joinCommand = "JOIN "; + for (int i = 0; i != channels.length; ++i) + { + if (hasJoinedChannel(channels[i])) + { + fireMessageReceived("You have already joined " + channels[i]); + continue; + } + Channel chan = new Channel(channels[i], this); + joinCommand += channels[i] + ","; + m_JoinedChannels.add(chan); + chans[i] = chan; + } + send(joinCommand); + return chans; + } + /** + * Part a channel. + * + * @param channelName The channel to part. + */ + public void partChannel(String channelName) + { + for (Enumeration entries = m_JoinedChannels.elements(); entries.hasMoreElements();) + { + Channel channel = (Channel)entries.nextElement(); + + if (channel.getChannelName().equals(channelName)) + { + send("PART " + channelName); + m_JoinedChannels.remove(channel); + return; + } + } + fireMessageReceived("You are not in channel " + channelName); + } + /** + * Part a channel. + * + * @param channel The channel to part. + */ + public void partChannel(Channel channel) + { + if (m_JoinedChannels.contains(channel)) + { + send("PART " + channel.getChannelName()); + m_JoinedChannels.remove(channel); + return; + } + fireMessageReceived("You are not in channel " + channel.getChannelName()); + } /** + * Part multiple channels. + * + * @param channels The channels to part. + */ + public void partChannels(Channel[] channels) + { + String partCommand = "PART "; + for (int i = 0; i != channels.length; ++i) + { + Channel chan = channels[i]; + if (m_JoinedChannels.contains(chan)) + { + partCommand += chan.getChannelName() + ","; + m_JoinedChannels.remove(chan); + } + fireMessageReceived("You are not in channel " + chan.getChannelName()); + } + send(partCommand); + } + + /** + * Part all channels. + */ + public void partAllChannels() + { + if (!m_JoinedChannels.isEmpty()) + { + String partCommand = "PART "; + for (Enumeration entries = m_JoinedChannels.elements(); entries.hasMoreElements();) + { + Channel chan = (Channel)entries.nextElement(); + + partCommand += chan.getChannelName() + ","; + m_JoinedChannels.remove(chan); + } + send(partCommand); + } + } + + /** + * Checks whether a channel has been joined. + * + * @param channel The channel to check for. + * \return True if so, false if not. + */ + public boolean hasJoinedChannel(String channel) + { + for (Enumeration entries = m_JoinedChannels.elements(); entries.hasMoreElements();) + { + Channel chan = (Channel)entries.nextElement(); + if (chan.getChannelName().equals(channel)) + { + return true; + } + } + return false; + } + + /** + * Checks whether a channel has been joined. + * + * @param channel The channel to check for. + * \return True if so, false if not. + */ + public boolean hasJoinedChannel(Channel channel) + { + if (m_JoinedChannels.contains(channel)) + { + return true; + } + return false; + } + + + + + + /** * Gets the host it connects or is connected to. * * \return The host. Modified: trunk/src/net/sensiva/rochat/ui/main/MainFrame.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-22 10:53:05 UTC (rev 33) +++ trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-22 12:59:42 UTC (rev 34) @@ -34,6 +34,7 @@ { private JFrame m_MainFrame; private TabManager m_TabManager; + private CommandParser m_Parser; /** * Creates our frame, then initializes all components. @@ -53,9 +54,9 @@ m_MainFrame = new JFrame("ROChat"); m_TabManager = new TabManager(this); + m_Parser = new CommandParser(this); - m_MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* * Don't use setLocationRelativeTo(null) because it will give some problems with dual-screens. @@ -134,6 +135,17 @@ { return (Tab)m_TabManager.getSelectedComponent(); } + + /** + * Execute a command entered by the user. + * + * @param command The phrase the user entered. + * \return void. + */ + public void executeCommand(String command) + { + String finalCommand = m_Parser.doCommand(command); + } } Modified: trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java 2007-10-22 10:53:05 UTC (rev 33) +++ trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java 2007-10-22 12:59:42 UTC (rev 34) @@ -31,27 +31,35 @@ public class ChannelTab extends JPanel implements Tab { private MainFrame m_MainFrame; - private int m_TabIndex; + public int m_TabIndex; private JPanel m_ContentPane; - private JEditorPane m_MessageList; + private JTextPane m_MessageList; private JTextField m_TextField; private JList m_NickList; private JSplitPane m_SplitPane; - //private Channel m_Channel; + private Channel m_Channel; private IRCConnection m_Connection; + /** + * ChannelTab constructor. + * + * @param mainFrame The Mainframe. + * @param channel The channel to join. + * @param server The server to join on. + */ public ChannelTab(MainFrame mainFrame, String channel, IRCConnection server) { m_MainFrame = mainFrame; m_Connection = server; - //m_Connection.joinChannel(channel); - + m_Channel = m_Connection.joinChannel(channel); } - - public void initialize() + /** + * Initializes this tab. + */ + public void initialize() { setOpaque(false); setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); @@ -61,7 +69,9 @@ leftPane.setOpaque(false); leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.PAGE_AXIS)); - m_MessageList = new JEditorPane(); + m_MessageList = new JTextPane(); + m_MessageList.setBackground(Color.WHITE); + m_MessageList.setEditable(false); JScrollPane messagePane = new JScrollPane(m_MessageList); @@ -87,12 +97,26 @@ m_SplitPane.setBorder(BorderFactory.createEmptyBorder(3,3,3,3)); m_SplitPane.setOpaque(false); add(m_SplitPane); - } - public int getTabIndex() + /** + * Get the channel. + * + * \return The channel. + */ + public Channel getChannel() { - return m_TabIndex; + return m_Channel; } + /** + * Get the server. + * + * \return The server. + */ + public IRCConnection getServer() + { + return m_Connection; + } + } Modified: trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java 2007-10-22 10:53:05 UTC (rev 33) +++ trunk/src/net/sensiva/rochat/ui/tabs/StatusTab.java 2007-10-22 12:59:42 UTC (rev 34) @@ -36,14 +36,12 @@ private JPanel m_ContentPane; private JTextPane m_MessageList; private JTextField m_TextField; - private CommandParser m_Parser; private IRCConnection m_Connection = null; public StatusTab(MainFrame mainFrame) { m_MainFrame = mainFrame; - m_Parser = new CommandParser(mainFrame); } public void initialize() @@ -115,8 +113,8 @@ SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setForeground(set, Color.BLACK); - m_MessageList.setCaretPosition(doc.getLength()); doc.insertString(doc.getLength(), message + "\n", set); + m_MessageList.setCaretPosition(doc.getLength()); } catch (Exception e) { @@ -126,15 +124,7 @@ public void actionPerformed(ActionEvent event) { - String command = m_Parser.doCommand(m_TextField.getText()); - if (command == null) - { - addMessage("You can not talk in a server tab."); - } - else - { - m_Connection.send(command); - } + m_MainFrame.executeCommand(m_TextField.getText()); m_TextField.setText(""); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <le...@us...> - 2007-10-29 10:56:08
|
Revision: 37 http://rochat.svn.sourceforge.net/rochat/?rev=37&view=rev Author: levia Date: 2007-10-29 03:56:04 -0700 (Mon, 29 Oct 2007) Log Message: ----------- - Fixed stuff. - Added ChannelListener. Modified Paths: -------------- trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java Added Paths: ----------- trunk/src/net/sensiva/rochat/core/connection/ChannelListener.java Added: trunk/src/net/sensiva/rochat/core/connection/ChannelListener.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/ChannelListener.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/connection/ChannelListener.java 2007-10-29 10:56:04 UTC (rev 37) @@ -0,0 +1,8 @@ + +package net.sensiva.rochat.core.connection; + +public interface ChannelListener +{ + public void messageReceived(String message, String userName, String host, String nickName); + public void topicReceived(String topic); +} Modified: trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java =================================================================== --- trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-23 08:10:35 UTC (rev 36) +++ trunk/src/net/sensiva/rochat/core/connection/IRCConnection.java 2007-10-29 10:56:04 UTC (rev 37) @@ -125,6 +125,7 @@ { if (!m_Socket.isClosed()) { + send("QUIT :slet");. m_Socket.close(); fireDisconnected(); fireMessageReceived("Disconnected from server."); Modified: trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java 2007-10-23 08:10:35 UTC (rev 36) +++ trunk/src/net/sensiva/rochat/ui/tabs/ChannelTab.java 2007-10-29 10:56:04 UTC (rev 37) @@ -144,4 +144,9 @@ addMessage(nickName + " | " + message); } + public void topicReceived(String topic) + { + addMessage(topic); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |