001: package org.millstone.examples.gogame;
002:
003: import org.millstone.base.Application;
004: import org.millstone.base.data.Item;
005: import org.millstone.base.data.Property;
006: import org.millstone.base.data.util.IndexedContainer;
007: import org.millstone.base.event.*;
008: import org.millstone.base.ui.*;
009:
010: /** The classic game 'Go' as an example for the Millstone framework.
011: *
012: * @author IT Mill Ltd.
013: * @see org.millstone.base.Application
014: */
015: public class Go extends Application implements Action.Handler,
016: Property.ValueChangeListener {
017:
018: /* An IndexedContainer will hold the list of players - it can be
019: * displayed directly by the 'Table' ui component. */
020: private static IndexedContainer players = new IndexedContainer();
021:
022: // This action will be triggered when a player challenges another.
023: private static Action challengeAction = new Action("Challenge",
024: null);
025:
026: // The players can have a current game.
027: static {
028: players.addContainerProperty("Current game", Game.class, null);
029: }
030:
031: // The layout
032: private CustomLayout layout = new CustomLayout("goroom");
033:
034: // Label to be displayed in the login window.
035: private TextField loginName = new TextField(
036: "Who are you stranger?", "");
037:
038: // Button for leaving the game
039: private Button leaveButton = new Button("Leave game", this , "close");
040:
041: // Button for logging in
042: private Button loginButton = new Button("Enter game", this , "login");
043:
044: // A 'Table' ui component will be used to show the list of players.
045: private Table playersTable;
046:
047: // Our Go-board ('Board') component.
048: private Board board = null;
049:
050: /** The initialization method that is the only requirement for
051: * inheriting the org.millstone.base.service.Application class. It will
052: * be automatically called by the framework when a user accesses the
053: * application.
054: * We'll initialize our components here.
055: */
056: public void init() {
057:
058: // Use the GO theme that includes support for goboard component
059: // and goroom layout
060: setTheme("gogame");
061:
062: // Initialize main window with created layout
063: addWindow(new Window("Game of GO", layout));
064:
065: // Xreate a table for showing the players in the IndexedContainer 'players'.
066: playersTable = new Table("Players", players);
067: playersTable.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
068: playersTable.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_ID);
069: playersTable.addActionHandler(this );
070: playersTable.setPageBufferingEnabled(false);
071:
072: // Add the Table and a Button to the main window.
073: layout.addComponent(playersTable, "players");
074: layout.addComponent(leaveButton, "logoutbutton");
075:
076: // Hide game components
077: leaveButton.setVisible(false);
078: playersTable.setVisible(false);
079:
080: // Add login functionality
081: layout.addComponent(loginName, "loginname");
082: loginButton.dependsOn(loginName);
083: layout.addComponent(loginButton, "loginbutton");
084: }
085:
086: /** This function is called when a player tries to log in.
087: */
088: public void login() {
089: String name = loginName.toString();
090: if (name.length() > 0 && !players.containsId(name)) {
091:
092: // Login successful
093: setUser(name);
094:
095: // Add user to player list.
096: Item user = players.addItem(name);
097: ((Property.ValueChangeNotifier) user
098: .getItemProperty("Current game")).addListener(this );
099:
100: // Update visible components
101: layout.removeComponent(loginName);
102: layout.removeComponent(loginButton);
103: leaveButton.setVisible(true);
104: playersTable.setVisible(true);
105: }
106: }
107:
108: // On logout, remove user from the player list
109: public void close() {
110: if (getUser() != null) {
111:
112: // Remove user from the player list.
113: players.removeItem(getUser());
114: }
115: super .close();
116: }
117:
118: /** Implementing the Action.Handler interface - this function returns
119: * the available actions.
120: * @see org.millstone.base.event.Action.Handler
121: */
122: public Action[] getActions(Object target, Object source) {
123: Property p = players.getContainerProperty(target,
124: "Current game");
125: if (p != null && target != null && !target.equals(getUser())) {
126: Game game = (Game) p.getValue();
127: if (game == null) {
128: return new Action[] { challengeAction };
129: }
130: }
131:
132: return new Action[] {};
133: }
134:
135: /** Implementing the Action.Handler interface - this function handles
136: * the specified action.
137: * @see org.millstone.base.event.Action.Handler
138: */
139: public void handleAction(Action action, Object sender, Object target) {
140: if (action == challengeAction) {
141: Property p = players.getContainerProperty(target,
142: "Current game");
143: if (p != null && target != null
144: && !target.equals(getUser())) {
145: Game game = (Game) p.getValue();
146: if (game == null) {
147: game = new Game(9, (String) getUser(),
148: (String) target);
149: p.setValue(game);
150: players.getContainerProperty(getUser(),
151: "Current game").setValue(game);
152: }
153: }
154: }
155: }
156:
157: /** Implementing the Property.ValueChangeListener interface - this function
158: * is called when a value change event occurs.
159: * @see org.millstone.base.data.Property.ValueChangeListener
160: */
161: public void valueChange(Property.ValueChangeEvent event) {
162: if (board != null)
163: layout.removeComponent(board);
164: Game game = (Game) event.getProperty().getValue();
165: if (game != null) {
166: board = new Board(game, game.getBlackPlayer() == getUser());
167: layout.addComponent(board, "board");
168: }
169: }
170: }
171:
172: /* This Millstone sample code is public domain. *
173: * For more information see www.millstone.org. */
|