001package bradleyross.library.applets;
002//  colorsSample.java
003//  A simple Java applet
004//
005import java.awt.*;
006import java.applet.Applet;
007import java.applet.AppletContext;
008import java.awt.Button;
009import java.awt.event.ActionListener;
010import java.awt.event.ActionEvent;
011import bradleyross.library.helpers.FileHelpers;
012import java.io.IOException;
013import java.io.StringReader;
014import java.io.LineNumberReader;
015import java.net.MalformedURLException;
016import java.net.URL;
017import java.util.Vector;
018//
019/**
020 * Demonstrates using a Java applet to display web pages in windows
021 *    on the browser.
022 * <p>This Java file can be compiled using
023 *    <code>-source 1.3 -target 1.3</code>
024 *    so that it will run with browsers using Java Run-Time
025 *    Environments as early as Java 1.3.  However, the standard
026 *    script now uses version 1.5 as the standard.</p>
027 * @see java.awt.Button
028 * @see java.awt.Canvas
029 * @see java.awt.Panel
030 * @see java.awt.GridBagConstraints
031 * @see java.awt.GridBagLayout
032 * @author Bradley Ross
033 */
034public class WebSample  extends Applet implements ActionListener 
035{
036        /**
037         * Inserted to satisfy Serializable interface.
038         */
039        private static final long serialVersionUID = 1L;
040        protected AppletContext context = null;
041        protected String listUrl = null;
042        protected String listUrlArray[] = null;
043        protected URL rootUrl = null;
044        protected URL url = null;
045        /** 
046         * The inner class colorSet represents the buttons in the panel 
047         * which the user sees.
048         * <p>Each of the buttons is defined as a public field in the inner class, with each 
049         *    field belonging to the class java.awt.Button.  The entire panel containing
050         *    the buttons is also defined as a public field, with the field of type
051         *    java.awt.Panel.</p>
052         */
053        class colorSet extends Component
054        {
055
056                /**
057                 * 
058                 */
059                private static final long serialVersionUID = 1L;
060                /**
061                 * Integer value representing the button that
062                 * was depressed.
063                 */
064                public int ColorChoice;
065                /**
066                 * Object defining the panel of buttons.
067                 */
068                Panel colorPanel = new Panel();
069                /**
070                 * Object for the "black" button.
071                 *<p>The other buttons are defined in the same manner.</p>
072                 */
073                public Button blackButton = new Button("black");
074                /**
075                 * Object for the "blue" button.
076                 *<p>The actions in the constructor for blueButton
077                 *   differ from the other in that the label is 
078                 *   changed to " Blue " while the message returned
079                 *   to the listener is still "blue".</p>
080                 */
081                public Button blueButton = new Button("blue");
082                /** See the description of blackButton */
083                public Button cyanButton = new Button("cyan");
084                /** See the description of blackButton */
085                public Button darkGrayButton = new Button("darkGray");
086                /** See the description of blackButton */
087                public Button grayButton = new Button("gray");
088                /** See the description of blackButton */
089                public Button lightGrayButton = new Button("lightGray");
090                /** See the description of blackButton */
091                public Button  magentaButton = new Button("magenta");
092                /** See the description of blackButton */
093                public Button orangeButton = new Button("orange");
094                /** See the description of blackButton */
095                public Button pinkButton = new Button("pink");
096                /** See the description of blackButton */
097                public Button redButton = new Button("red");
098                /** See the description of blackButton */
099                public Button whiteButton = new Button("white");
100                /** See the description of blackButton */
101                public Button yellowButton = new Button("yellow");
102                /** See the description of blackButton */
103                public Button greenButton = new Button("green");
104                /**
105                 * This is the constructor for the colorSet class
106                 * which defines the buttons.
107                 *<p>For blackButton, the setLabel and setActionCommand
108                 *   methods are used to set the values.</p>
109                 *<p>For the other buttons, label for the button and
110                 *   the message returned when the button is depressed
111                 *   are set equal to the string used in the constructor
112                 *   for the java.awt.Button object.</p>
113                 *<p>The add method for the colorPanel object places the
114                 *   various buttons in the panel according to the
115                 *   GridBagLayout and GridBagConstraints classes.</p>
116                 */
117                public colorSet()
118                {
119                        ColorChoice = 1;
120                        setBackground(Color.white);
121                        GridBagLayout layout = new GridBagLayout();
122                        GridBagConstraints constraints = new GridBagConstraints();
123                        colorPanel.setLayout(layout);
124                        constraints.insets = new Insets (5, 5, 5, 5);
125                        constraints.fill = GridBagConstraints.BOTH;
126                        constraints.anchor = GridBagConstraints.SOUTHWEST;
127                        constraints.weightx = 0; constraints.weighty = 0;
128                        constraints.gridwidth = 1; constraints.gridheight = 1;
129                        //
130                        constraints.gridx = 1; constraints.gridy = 1;
131                        colorPanel.add(blackButton, constraints);
132                        //
133                        constraints.gridx = 2; constraints.gridy = 1;
134                        colorPanel.add(blueButton, constraints);
135                        blueButton.setLabel("  Blue  ");
136                        blueButton.setActionCommand("blue");
137                        //
138                        constraints.gridx = 3; constraints.gridy = 1;
139                        colorPanel.add(cyanButton, constraints);
140                        //
141                        constraints.gridx = 4; constraints.gridy = 1;
142                        colorPanel.add(darkGrayButton, constraints);
143                        //
144                        constraints.gridx = 5; constraints.gridy = 1;
145                        colorPanel.add(grayButton, constraints);
146                        //
147                        constraints.gridx = 1; constraints.gridy = 2;
148                        colorPanel.add(greenButton, constraints);
149                        //
150                        constraints.gridx = 2; constraints.gridy = 2;
151                        colorPanel.add(lightGrayButton, constraints);
152                        //
153                        constraints.gridx = 3; constraints.gridy = 2;
154                        colorPanel.add(magentaButton, constraints);
155                        //
156                        constraints.gridx = 4; constraints.gridy = 2;
157                        colorPanel.add(orangeButton, constraints);
158                        //
159                        constraints.gridx = 5; constraints.gridy = 2;
160                        colorPanel.add(pinkButton, constraints);
161                        //
162                        constraints.gridx = 1; constraints.gridy = 3;
163                        colorPanel.add(redButton, constraints);
164                        //
165                        constraints.gridx = 2; constraints.gridy = 3;
166                        colorPanel.add(whiteButton, constraints);
167                        //
168                        constraints.gridx = 3; constraints.gridy = 3;
169                        colorPanel.add(yellowButton, constraints);
170                }
171        } // This is the end of the inner class
172        static final String message = "Hello World!";
173        static final String errorMessage = "Illegal Code";
174
175        /**
176         * The buttons object is the instantiation of the inner class colorSet.
177         */
178        colorSet buttons = new colorSet();
179        /**
180         * Initialize the applet.
181         *<p>The class colorsSample implements the interface
182         *   java.awt.ActionListener as well as extending the
183         *   class java.applet.Applet.</p>
184         *<p>The addActionListener methods arrange for the
185         *   colorsSample class to receive the messages
186         *   when the buttons are depressed.  This means that
187         *   that the ActionPerformed method for the class
188         *   is executed whenever a button is depressed.</p>
189         * @see java.awt.event.ActionListener
190         * @see java.applet.Applet
191         */
192        public void init() 
193        {
194                System.out.println("Starting program");
195
196
197                listUrl = getParameter("ImageList");
198                rootUrl = getDocumentBase();
199                if (listUrl != null)
200                {
201                        try 
202                        {
203                                String urlFile = FileHelpers.readTextFile(new URL(rootUrl, listUrl));
204                                LineNumberReader reader = new LineNumberReader(new StringReader(urlFile));
205                                Vector<String> urlSet = new Vector<String>();
206                                while (true)
207                                {
208                                        String temp = reader.readLine();
209                                        if (temp == null) { break; }
210                                        if (temp.length() == 0) { continue; }
211                                        urlSet.add(temp);
212                                }
213                        } 
214                        catch (MalformedURLException e) 
215                        {
216                                showStatus(e.getClass().getName());
217                        } 
218                        catch (IOException e) 
219                        {
220                                showStatus(e.getClass().getName());
221                        }
222                }
223                context = this.getAppletContext();
224                FlowLayout layout = new FlowLayout();
225                setLayout(layout);
226                setBackground(Color.white);
227                add(buttons.colorPanel);
228                buttons.blackButton.addActionListener(this);
229                buttons.blueButton.addActionListener(this);
230                buttons.cyanButton.addActionListener(this);
231                buttons.darkGrayButton.addActionListener(this);
232                buttons.grayButton.addActionListener(this);
233                buttons.greenButton.addActionListener(this);
234                buttons.lightGrayButton.addActionListener(this);
235                buttons.magentaButton.addActionListener(this);
236                buttons.orangeButton.addActionListener(this);
237                buttons.pinkButton.addActionListener(this);
238                buttons.redButton.addActionListener(this);
239                buttons.whiteButton.addActionListener(this);
240                buttons.yellowButton.addActionListener(this);
241                doLayout();
242                System.out.println("Layout complete");
243        } // End of init
244        /**
245         * The paint method is called whenever it is necessary to
246         * redraw the contents of the applet.
247         * <p>The paint method is called whenever it is necessary
248         *    redraw the image.  It can be called by the window manager
249         *    when a window is resized, covered, or uncovered or when it
250         *    detects that another method has executed the repaint method.</p>
251         * @param g The graphics item which is to be redrawn.
252         */
253        /**
254         * The actionPerformed method is executed whenever
255         *    an action is taken by the user.
256         *<p>The event created by depressing the
257         *   the button is passed as argument of
258         *   the method and the getActionCommand method
259         *   obtains the character string associated
260         *   with the button as defined by the constructor
261         *   and other methods for the button.</p>
262         *<p>This method then sets the ColorChoice field
263         *   in the buttons object according to which 
264         *   button was depressed and then uses the
265         *   repaint method to cause the graphics on the
266         *   applet to be redrawn.</p>
267         */
268        public void actionPerformed (ActionEvent e)
269        {
270                String command = new String();
271                command = e.getActionCommand();
272                System.out.println("ActionEvent **" + command + "**");
273                URL test = null;
274                try
275                {
276                        test = new URL(rootUrl, "colors/" + command.toLowerCase() + ".html");
277                }
278                catch (MalformedURLException e1)
279                {
280                        System.out.println(e1.getClass().getName() + " " + e1.getMessage());
281                }
282                context.showDocument(test, "pages");     
283        } // end of actionPerformed
284}