smsbaseview.java

来自「手机无线网络纸牌游戏源代码。非常适合学习使用」· Java 代码 · 共 173 行

JAVA
173
字号
// SMSBaseView.java
//
// Copyright (c) 2000-2001 Symbian Ltd. All rights reserved

package com.symbian.devnet.whist.awt;

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.Image.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import javax.pim.database.*;
import javax.pim.addressbook.*;
import javax.net.datagram.*;
import com.symbian.devnet.whist.game.*;
import com.symbian.devnet.quartz.awt.*;

/** 
 * Class to control the view used to set up the SMS mode of operation.
 * @author Symbian Devnet
 */
public class SMSBaseView
{
	/** A vector containing all the cell phone numbers */
	public final Vector phoneNos = new Vector(); 
	/** The <code>Panel</code> used to display the information on. */
	private Panel panel;
	/** The reference to the Whist game. */
	private Whist whistRef;
	/** A reference to the <code>ContactDatabase</code> */
	private ContactDatabase contacts;
	/** An array of <code>Label</code>s */
	private Label[] labels = new Label[4];
	/** A <code>Label</code> containing the SMS address of this device */
	private Label mySMSAddress;
	/** 
	 * An array of <code>Label</code>s used for creating space in the 
	 * <code>Panel</code> 
	 */
	private Label[] space = new Label[9];
	/** An array of <code>Choice</code> boxes for the names of possible players */
	private Choice[] addresses = new Choice[3];
	/** A <code>Button</code> for acknowledging the choice of players */
	private Button OKbtn = new Button("OK");
	/** <code>GameDialog</code> used for displaying information to the user */
	private GameDialog gameDialog;
			
	/**
	 * Constructor which sets the layout of the Panel
	 * @param p the <code>Panel</code> used to display the information on
	 * @param w a reference to the Whist game.
	 */
	public SMSBaseView(Panel p, Whist w)
	{
		panel = p;
		panel.setLayout(new GridLayout(9,2));
		whistRef = w;
		labels[0] = new Label("Player 1", Label.LEFT);
		labels[1] = new Label("Player 2", Label.LEFT);
		labels[2] = new Label("Player 3", Label.LEFT);
		labels[3] = new Label("Player 4", Label.LEFT);	
		mySMSAddress = new Label(whistRef.mySMSAddress);		
		panel.add(labels[0]);			
		panel.add(mySMSAddress);
		try
		{
			contacts = ContactDatabase.openDatabase();
		}
		catch (Exception ex) 
		{				
			System.out.println("problem opening default address book: " + ex.getMessage());
		}
		addresses[0] = new Choice();
		addresses[1] = new Choice();
		addresses[2] = new Choice();	
		populateChoice();								
		for (int i = 0; i < 3; i++)
		{			
			panel.add(labels[i+1]);			
			panel.add(addresses[i]);
		}		
		for (int i = 0; i < 9; i++)
		{
			space[i] = new Label("");
			panel.add(space[i]);
		}

		panel.add(OKbtn);
		
		OKbtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{			
				// check that user hasn't selected same person (compare indices of
				// choice)
								
				if((addresses[0].getSelectedIndex() != addresses[1].getSelectedIndex()) && (addresses[1].getSelectedIndex() != addresses[2].getSelectedIndex()) && (addresses[0].getSelectedIndex() != addresses[2].getSelectedIndex()))
				{
					String[] s = new String[4];
					String[] names = new String[4];
					s[0] = "wdpsms://" + mySMSAddress.getText() + ":1010";
					for (int i = 0; i < 3; i++)
					{
						names[i+1] = addresses[i].getSelectedItem();
						try
						{
							s[i+1] = "wdpsms://" + phoneNos.elementAt(addresses[i].getSelectedIndex()) + ":1010";
						}
						catch (Exception ex)
						{
						}
					}					
					whistRef.setUpSMS();
					whistRef.startGame(names, s);	
				}
				else // user has selected same person twice	
				{
					String[] array = new String[2];
					array[0] = ("Please select three different people "); 
					array[1] = ("to invite to join the Whist game.");
					gameDialog = new GameDialog(whistRef, "Information", array);
					gameDialog.setVisible(true);
				}						
			}		
		});		
	}
	
	/** 
	 * Fills the Choice boxes with cell phone numbers extracted from the 
	 * <code>ContactDatabase</code>.
	 */
	private void populateChoice()
	{	
		String currentName;
		final Iterator cards;
		try
		{
			cards = contacts.cardItems();
			while (cards.hasNext())
			{	
				ContactCard currentCard = (ContactCard)cards.next();
				String[] cellPhoneNumbers = currentCard.getPhoneNumbers(ContactDatabase.CELL);
				if (0 != cellPhoneNumbers.length)
				{
					phoneNos.addElement(cellPhoneNumbers[0]);		
					currentName = FormattedNameBuilder.buildName(currentCard);					
					currentCard.setFormattedName(currentName);					
					contacts.updateItem(currentCard);
					
					if (0 != currentName.length())
					{				
						addresses[0].add(currentName);
						addresses[1].add(currentName);
						addresses[2].add(currentName);
					}				
				}
			}
		}
		catch(Exception ex)
		{
			System.out.println("exception: " + ex.getMessage());
		}
		try 
		{
			contacts.close();
			System.out.println("Address book closed");
		}
		catch (Exception e) 
		{
		}	
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?