001    package stringtransformers;
002    
003    import cs101.util.gamecontrol.Initializable;
004    import java.awt.*;
005    import java.awt.event.*;
006    import java.lang.reflect.*;
007    
008    public class ControlPanel extends Panel
009    {
010      public ControlPanel()
011      {
012        super();
013    
014        final Class[] implementations = { StringInputter.class,
015                                          StringOutputter.class,
016                                          Broadcaster.class,
017                                          WordSplitter.class,
018                                          PigLatinTransformer.class,
019                                          UbbyDubbyTransformer.class,
020                                          NoopTransformer.class,
021                                          LazyEchoer.class,
022                                          UpperCaser.class,
023                                          LowerCaser.class,
024                                          SentenceCaser.class,
025                                          NameDropper.class,
026                                          Pedant.class,
027                                          Accumulator.class,
028                                          AlternatingOutputter.class
029                                        };
030                                    
031        final String[] labels = { "Inputter",
032                                  "Terminator",
033                                  "Broadcaster",
034                                  "Word Splitter",
035                                  "Pig Latin Transformer",
036                                  "Ubby Dubby Transformer",
037                                  "Echoer",
038                                  "Lazy Echoer",
039                                  "Upper Caser",
040                                  "Lower Caser",
041                                  "Sentence Caser",
042                                  "Name Dropper",
043                                  "Pedant",
044                                  "Accumulator (Terminator)",
045                                  "(Output) Alternater"
046                                };
047                            
048    
049        if ( implementations.length != labels.length ) 
050          {
051            throw new RuntimeException( "Wrong number of button labels for implementations!" );
052          }
053    
054        //    this.layout = new GridLayout( 4 );
055    
056        for ( int i = 0; i < labels.length; i++ )
057          {
058            Button b = createButton( labels[i], implementations[i] );
059            this.add( b );
060          }
061            
062        /*
063          Button sourceButton = new Button( "New Inputter" ),
064          sinkButton = new Button( "New Terminator" ),
065          broadcastButton = new Button( "New Broadcaster" ),
066          wordButton = new Button( "New Word Splitter" ),
067          noopButton = new Button( "New (Noop) Transformer" ),
068          plButton = new Button( "New Pig Latin Transformer" );
069        
070          sourceButton.addActionListener( new ActionListener() {
071          public void actionPerformed ( ActionEvent ae )
072          {
073          new StringInputter().init();
074          }
075          });
076    
077          noopButton.addActionListener( new ActionListener() {
078          public void actionPerformed ( ActionEvent ae )
079          {
080          new NoopTransformer();
081          }
082          });
083    
084          plButton.addActionListener( new ActionListener() {
085          public void actionPerformed ( ActionEvent ae )
086          {
087          new PigLatinTransformer();
088          }
089          });
090    
091          sinkButton.addActionListener( new ActionListener() {
092          public void actionPerformed ( ActionEvent ae )
093          {
094          new StringOutputter().init();
095          }
096          });
097    
098          this.add (sourceButton);
099          this.add (noopButton);
100          this.add (plButton);
101          this.add (sinkButton);
102          */
103    
104        new SimpleFrame( this ).init();
105      }
106    
107      private Button createButton( String label, final Class whatKind )
108      {
109        Button b = new Button( label );
110    
111        b.addActionListener( new ActionListener() {
112          public void actionPerformed ( ActionEvent ae )
113            {
114              try
115                {
116                  Object transformer = whatKind.newInstance();
117    
118                  final Class[] noArgs = {};
119        
120                  Method init = whatKind.getMethod( "init", noArgs );
121                  init.invoke( (Initializable) transformer, noArgs );
122                }
123              catch ( InstantiationException e )
124                {
125                  System.err.println( "Uh oh, instantiation exception!!" + 
126                                      e +
127                                      e.getMessage() );
128                }
129              catch ( IllegalAccessException e )
130                {
131                  System.err.println( "Uh oh, illegal access exception!!" + 
132                                      e +
133                                      e.getMessage() );
134                }
135              catch ( NoSuchMethodException e )
136                {
137                  System.err.println( "Uh oh, no such method!!" + 
138                                      e +
139                                      e.getMessage() );
140                }
141              catch ( IllegalArgumentException e )
142                {
143                  System.err.println( "Uh oh, illegal argument!!" + 
144                                      e +
145                                      e.getMessage() );
146                }
147              catch ( InvocationTargetException e )
148                {
149                  System.err.println( "Uh oh, bad invocation target!!" + 
150                                      e +
151                                      e.getMessage() );
152                }
153            }
154        });
155    
156        return b;
157      }
158    
159      public Dimension getPreferredSize()
160      {
161        return new Dimension( 400, 140 );
162      }
163    
164      public static void main ( String[] args )
165      {
166        new ControlPanel();
167      }
168    
169    
170    
171    }