View Javadoc
1 /*** 2 * Copyright (c) 2003 held jointly by the individual authors. 3 * 4 * This library is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU Lesser General Public License as published 6 * by the Free Software Foundation; either version 2.1 of the License, or 7 * (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; with out even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this library; if not, write to the Free Software Foundation, 16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 17 * 18 * > http://www.gnu.org/copyleft/lesser.html 19 * > http://www.opensource.org/licenses/lgpl-license.php 20 */ 21 package net.mlw.gfw.client; 22 23 import java.awt.BorderLayout; 24 import java.util.logging.Logger; 25 26 import javax.swing.JFrame; 27 import javax.swing.JPanel; 28 29 import net.mlw.gfw.event.EventHandlerTypeDelegator; 30 import net.mlw.gfw.event.impl.ConnectEvent; 31 import net.mlw.gfw.event.impl.DisconnectEvent; 32 import net.mlw.gfw.ext.basic.client.component.ChatPanel; 33 import net.mlw.gfw.ext.basic.client.component.PlayerListPanel; 34 import net.mlw.gfw.ext.basic.client.component.StatusDialog; 35 import net.mlw.gfw.ext.basic.client.event.LoggerEventHandler; 36 import net.mlw.gfw.ext.basic.event.ChatEvent; 37 import net.mlw.gfw.ext.basic.event.ErrorEvent; 38 import net.mlw.gfw.server.impl.DefaultServerDecorator; 39 40 /*** This abstract class can be used as a starting point for a swing 41 * client interface. <b>The method initialize(ClientContext) 42 * must be called before this class can be used.</b> 43 * 44 * @author Matthew L. Wilson 45 * @version $Revision: 1.3 $ $Date: 2004/07/01 14:31:41 $ 46 */ 47 public abstract class DefaultClient extends JFrame 48 { 49 /*** Logger * */ 50 protected static Logger LOGGER = Logger.getLogger(DefaultClient.class.getName()); 51 52 /*** 53 * The server, the decorator alows the furture implementation of the 54 * Server to be set any time, and passed by reference. 55 */ 56 protected final DefaultServerDecorator server = new DefaultServerDecorator(); 57 58 /*** The glue that holds the clients communication together. * */ 59 protected final EventHandlerTypeDelegator clientEventHandler = new EventHandlerTypeDelegator(new LoggerEventHandler("Event not processed", LOGGER)); 60 61 /*** The component used to chat with other players. **/ 62 final ChatPanel chatPanel = new ChatPanel(); 63 /*** The component used see who is in the room. **/ 64 final PlayerListPanel playerList = new PlayerListPanel(); 65 66 /*** This method must be called after creation of this object. This 67 * method in turn calls: 68 * <ol> 69 * <li>preInit(final ClientContext clientContext)</li> 70 * <li>init(final ClientContext clientContext)</li> 71 * <li>postInit(final ClientContext clientContext)</li> 72 * </ol> 73 * 74 * @param clientContext The ClientContext object that can pass the 75 * username by reference. 76 */ 77 public void initialize(final ClientContext clientContext) 78 { 79 preInit(clientContext); 80 init(clientContext); 81 postInit(clientContext); 82 } 83 84 /*** The first method called during the initialzation. This 85 * method does the following: 86 * <ol> 87 * <li>Sets the layout to BorderLayout.</li> 88 * <li>Adds the ChatPanel and registers it with the server.</li> 89 * <li>Adds the PlayerListPanel and registers it with the server.</li> 90 * </ol> 91 * 92 * @param clientContext The ClientContext object that can pass the 93 * username by reference. 94 */ 95 private final void preInit(final ClientContext clientContext) 96 { 97 getContentPane().setLayout(new BorderLayout()); 98 99 chatPanel.init(clientContext); 100 chatPanel.addEventHandler(server); 101 102 playerList.init(clientContext); 103 playerList.addEventHandler(server); 104 105 JPanel southPanel = new JPanel(new BorderLayout()); 106 southPanel.add(playerList, BorderLayout.EAST); 107 southPanel.add(chatPanel, BorderLayout.CENTER); 108 109 getContentPane().add(southPanel, BorderLayout.SOUTH); 110 } 111 112 /*** This method is called by the initialize method between the 113 * preInit and postInit. This is where all setup should 114 * take place. Things such as component building and listener 115 * registering should take plave here. 116 * 117 * @param clientContext The ClientContext object that can pass the 118 * username by reference. 119 */ 120 public abstract void init(final ClientContext clientContext); 121 122 /*** The last method called before initialzation is complete. 123 * This method does the following: 124 * <ol> 125 * <li>Sets a listner for ErrorEvent.</li> 126 * <li>Sets a listner for ConnectEvent.</li> 127 * <li>Sets a listner for DisconnectEvent.</li> 128 * <li>Sets a listner for ChatEvent.</li> 129 * </ol> 130 * 131 * @param clientContext The ClientContext object that can pass the 132 * username by reference. 133 */ 134 private final void postInit(final ClientContext clientContext) 135 { 136 clientEventHandler.addEventHandler(ErrorEvent.class, new StatusDialog(this)); 137 clientEventHandler.addEventHandler(ConnectEvent.class, playerList); 138 clientEventHandler.addEventHandler(DisconnectEvent.class, playerList); 139 clientEventHandler.addEventHandler(ChatEvent.class, chatPanel); 140 } 141 142 /*** Convience method to get the JFrame inside a inlince class. 143 * 144 * @return The this instance. 145 */ 146 public final JFrame getJFrame() 147 { 148 return this; 149 } 150 151 }

This page was automatically generated by Maven