upstream version 1.2.2
[debian/freetts] / com / sun / speech / engine / synthesis / SynthesizerSelector.java
1 /**
2  * Copyright 1998-2001 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 package com.sun.speech.engine.synthesis;
9
10 import java.awt.Component;
11 import java.util.Enumeration;
12 import java.util.List;
13
14 import javax.speech.Central;
15 import javax.speech.EngineList;
16 import javax.speech.synthesis.SynthesizerModeDesc;
17 import javax.swing.JOptionPane;
18
19 /**
20  * GUI that displays a list of <code>Synthesizer</code> names in a
21  * dialog box.  When the user makes a selection, the selected
22  * <code>SynthesizerModeDesc</code> is returned to the caller.
23  */
24 public class SynthesizerSelector {
25     /**
26      * Asks <code>Central</code> for a list of available synthesizers.
27      * If there are none, returns <code>null</code>.  If there is only
28      * one, returns it.  Otherwise, pops up an input dialog that gives
29      * the user a choice.
30      *
31      * @param component the component for JOptionPane.showInputDialog
32      * @param appName the title for the input dialog
33      *
34      * @return a <code>SynthesizerModeDesc</code> representing the
35      *   synthesizer to use.
36      */
37     static public SynthesizerModeDesc getSynthesizerModeDesc(
38         Component component,
39         String appName) {
40         List synths = new java.util.ArrayList();
41         List synthNames = new java.util.ArrayList();
42         EngineList list = Central.availableSynthesizers(null); 
43         Enumeration e = list.elements();
44         while (e.hasMoreElements()) {
45             synths.add(((SynthesizerModeDesc) e.nextElement()));
46             synthNames.add(
47                 ((SynthesizerModeDesc)
48                  synths.get(synths.size() - 1)).getEngineName());
49         }
50         Object[] synthNamesArray = synthNames.toArray();
51
52         if (synths.size() == 0) {
53             return null;
54         } else if (synths.size() == 1) {
55             return (SynthesizerModeDesc) synths.get(0);
56         }
57         
58         String synthName = (String)JOptionPane.showInputDialog(
59             component,
60             "Select the Synthesizer to use:",
61             appName + ": Select Synthesizer",
62             JOptionPane.QUESTION_MESSAGE,
63             null,
64             synthNamesArray,
65             synthNamesArray[0]);
66
67         int index = synthNames.indexOf(synthName);
68         if (index == -1) {
69             return null;
70         }
71         return (SynthesizerModeDesc) synths.get(index);
72     }
73 }