⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 telephone.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//// 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 - 2007. 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.5 2007/02/21 13:45:08 cwrapp Exp $//// CHANGE LOG// $Log: Telephone.java,v $// Revision 1.5  2007/02/21 13:45:08  cwrapp// Moved Java code to release 1.5.0//// Revision 1.4  2005/05/28 13:51:24  cwrapp// Update Java examples 1 - 7.//// Revision 1.0  2003/12/14 20:22:40  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.Iterator;import java.util.LinkedList;import java.util.List;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<String, TelephoneTimer>();        _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 = _timerMap.remove(timer);        if (task != null && _fsm != null)        {            try            {                Method transition =                    _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 = _timerMap.get(name);        long delay;        if (task != null)        {            delay = task.getDelay();            task.cancel();            startTimer(name, delay);        }        return;    }    public void stopTimer(String name)    {        TelephoneTimer task = _timerMap.remove(name);        if (task != null)        {            task.cancel();        }        return;    }    public void play(String name)    {        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 = _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 = _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;        List<AudioData> 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;        List<AudioData> 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;        List<AudioData> 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;        List<AudioData> clipList = new LinkedList<AudioData>();        clip = _audioMap.get("the_time_is");        clipList.add(clip);        // 1. Read the hour.        clip = _audioMap.get(Integer.toString(hour));        clipList.add(clip);        // Is this on the hour exactly?        if (minute == 0 && seconds == 0)        {            clip = _audioMap.get("oclock");            clipList.add(clip);            _soundMeridian(am_pm, clipList);            clip = _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 = _audioMap.get("exactly");                clipList.add(clip);            }            else            {                clip = _audioMap.get("and");                clipList.add(clip);                _soundNumber(seconds, false, clipList);                if (seconds == 1)                {                    clip = _audioMap.get("second");                }                else                {                    clip = _audioMap.get("seconds");                }                clipList.add(clip);            }        }        _playbackThread = new PlaybackThread(clipList, this);        _playbackThread.setDaemon(true);        _playbackThread.start();        return;    }    public void playInvalidNumber()    {        AudioData audioData;        List<AudioData> audioList = new LinkedList<AudioData>();        audioData = _audioMap.get("you_dialed");        audioList.add(audioData);        _soundPhoneNumber(audioList);        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;        _numberDisplay.setText(_display);

⌨️ 快捷键说明

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