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. |