📄 telephone.java
字号:
//// The contents of this file are subject to the Mozilla Public// License Version 1.1 (the "License"); you may not use this file// except in compliance with the License. You may obtain a copy// of the License at http://www.mozilla.org/MPL/// // Software distributed under the License is distributed on an// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or// implied. See the License for the specific language governing// rights and limitations under the License.// // The Original Code is State Machine Compiler (SMC).// // The Initial Developer of the Original Code is Charles W. Rapp.// Portions created by Charles W. Rapp are// Copyright (C) 2000 - 2003 Charles W. Rapp.// All Rights Reserved.// // Contributor(s): //// Name// Telephone.java//// Description// A simulation of an old fashioned touch-tone telephone.//// RCS ID// $Id: Telephone.java,v 1.2 2007/08/05 13:23:17 cwrapp Exp $//// CHANGE LOG// $Log: Telephone.java,v $// Revision 1.2 2007/08/05 13:23:17 cwrapp// Version 5.0.1 check-in. See net/sf/smc/CODE_README.txt for more information.//// Revision 1.1 2005/05/28 12:49:22 cwrapp// Added Ant examples 1 - 7.//// Revision 1.1 2004/09/06 15:22:14 charlesr// Updated for SMC v. 3.1.0. Using new -d option.//// Revision 1.0 2004/05/31 13:36:18 charlesr// Initial revision//package smc_ex7;import java.applet.Applet;import java.applet.AudioClip;import java.awt.Container;import java.awt.Font;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.MalformedURLException;import java.net.URL;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.LinkedList;import java.util.ListIterator;import java.util.Map;import java.util.Timer;import java.util.TimerTask;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;import javax.swing.UIManager;public final class Telephone{// Member methods. // Display the "telephone" user interface and run until // the user quits the window. public static void main(String[] args) { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) {} // Create the top-level container and add contents to it. JFrame frame = new JFrame("Telephone Demo"); Telephone telephone = new Telephone(frame.getContentPane()); // Finish up setting the frame and show it. frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { // The window is going away NOW. Just exit. System.exit(0); } } ); frame.pack(); frame.setVisible(true); } public Telephone(Container pane) { _areaCode = new String(); _exchange = new String(); _local = new String(); _display = new String(); _receiverButton = null; _dialButtons = new JButton[12]; _timerMap = new HashMap(); _playbackThread = null; _loadSounds(); _loadUI(pane); // Create the state machine to drive this object. _fsm = new TelephoneContext(this); // DEBUG // _fsm.setDebugFlag(true); } public void issueTimeout(String timer) { TelephoneTimer task = (TelephoneTimer) _timerMap.remove(timer); if (task != null && _fsm != null) { try { Method transition = (Method) _timerTransitionMap.get(timer); Object[] args; if (transition != null) { args = new Object[0]; transition.invoke(_fsm, args); } } catch (IllegalAccessException accex) {} catch (IllegalArgumentException argex) {} catch (InvocationTargetException targetex) {} } return; } //----------------------------------------------------------- // State Machine Actions. // // Convert a string to a number. Return -1 if the parse // fails. public int parseInt(String n) { int retval; try { retval = Integer.parseInt(n); } catch (NumberFormatException formex) { retval = -1; } return (retval); } // Return the current area code. public String getAreaCode() { return (_areaCode); } // Return the exchange. public String getExchange() { return (_exchange); } // Return the local number. public String getLocal() { return (_local); } // Use a separate thread to route the call asynchronously. public void routeCall(int callType, String areaCode, String exchange, String local) { CallRoutingThread thread = new CallRoutingThread(callType, areaCode, exchange, local, this); thread.start(); return; } public void startTimer(String name, long delay) { TelephoneTimer task = new TelephoneTimer(name, delay, this); _timerMap.put(name, task); _timer.schedule(task, delay); return; } public void resetTimer(String name) { TelephoneTimer task = (TelephoneTimer) _timerMap.get(name); long delay; if (task != null) { delay = task.getDelay(); task.cancel(); startTimer(name, delay); } return; } public void stopTimer(String name) { TelephoneTimer task = (TelephoneTimer) _timerMap.remove(name); if (task != null) { task.cancel(); } return; } public void play(String name) { AudioData audioData = (AudioData) _audioMap.get(name); if (audioData != null) { try { audioData.play(); } catch (InterruptedException interrupt) {} } else { System.err.println("There is no audio clip named \"" + name + "\"."); } return; } public void playTT(String name) { int n; try { n = Integer.parseInt(name); if (_dtmf[n] != null) { _dtmf[n].play(); } else { System.err.println("There is no audio clip named \"dtmf_" + name + "\"."); } } catch (NumberFormatException formex) {} return; } public void loop(String name) { AudioData audioData = (AudioData) _audioMap.get(name); if (audioData != null) { audioData.loop(); } else { System.err.println("There is no audio clip named \"" + name + "\"."); } return; } public void stopLoop(String name) { AudioData audioData = (AudioData) _audioMap.get(name); if (audioData != null) { audioData.stop(); } else { System.err.println("There is no audio clip named \"" + name + "\"."); } return; } public void stopPlayback() { if (_playbackThread != null) { _playbackThread.halt(); _playbackThread = null; } return; } public void playEmergency() { AudioData audioData; LinkedList audioList = new LinkedList(); audioData = (AudioData) _audioMap.get("911"); audioList.add(audioData); _playbackThread = new PlaybackThread(audioList, this); _playbackThread.setDaemon(true); _playbackThread.start(); return; } public void playNYCTemp() { AudioData audioData; LinkedList audioList = new LinkedList(); audioData = (AudioData) _audioMap.get("NYC_temp"); audioList.add(audioData); _playbackThread = new PlaybackThread(audioList, this); _playbackThread.setDaemon(true); _playbackThread.start(); return; } public void playDepositMoney() { AudioData audioData; LinkedList audioList = new LinkedList(); audioData = (AudioData) _audioMap.get("50_cents_please"); audioList.add(audioData); _playbackThread = new PlaybackThread(audioList, this); _playbackThread.setDaemon(true); _playbackThread.start(); return; } public void playTime() { GregorianCalendar calendar = new GregorianCalendar(); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int seconds = calendar.get(Calendar.SECOND); int am_pm = calendar.get(Calendar.AM_PM); AudioData clip; LinkedList clipList = new LinkedList(); clip = (AudioData) _audioMap.get("the_time_is"); clipList.add(clip); // 1. Read the hour. clip = (AudioData) _audioMap.get(Integer.toString(hour)); clipList.add(clip); // Is this on the hour exactly? if (minute == 0 && seconds == 0) { clip = (AudioData) _audioMap.get("oclock"); clipList.add(clip); _soundMeridian(am_pm, clipList); clip = (AudioData) _audioMap.get("exactly"); clipList.add(clip); } else { // 2. Read the minute. _soundNumber(minute, true, clipList); _soundMeridian(am_pm, clipList); // 3. Read the seconds. if (seconds == 0) { clip = (AudioData) _audioMap.get("exactly"); clipList.add(clip); } else { clip = (AudioData) _audioMap.get("and"); clipList.add(clip); _soundNumber(seconds, false, clipList); if (seconds == 1) { clip = (AudioData) _audioMap.get("second"); } else { clip = (AudioData) _audioMap.get("seconds"); } clipList.add(clip); } } _playbackThread = new PlaybackThread(clipList, this); _playbackThread.setDaemon(true); _playbackThread.start(); return; } public void playInvalidNumber() { AudioData audioData; LinkedList audioList = new LinkedList(); audioData = (AudioData) _audioMap.get("you_dialed"); audioList.add(audioData); _soundPhoneNumber(audioList); audioData = (AudioData) _audioMap.get("could_not_be_completed"); audioList.add(audioData); // Play the message in a separate thread. _playbackThread = new PlaybackThread(audioList, this); _playbackThread.setDaemon(true); _playbackThread.start(); return; } public int getType() { return (_callType); } public void setType(int type) { _callType = type; return; } public void saveAreaCode(String n) { _areaCode += n; addDisplay(n); return; } public void saveExchange(String n) { _exchange += n; addDisplay(n); return; } public void saveLocal(String n) { _local += n; addDisplay(n); return; } public void addDisplay(String character) { _display += character;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -