001    import java.awt.*;
002    import java.awt.event.*;
003    import java.sql.ResultSet;
004    import javax.swing.*;
005    
006    /** 
007     * <h2>WeatherForm15</h2>
008     *
009     * @author Matthew Hough
010     * @version .01, 6/2/07
011     * @assignment.number A19015
012     * @prgm.usage This program builds the WindsAloft form
013     *
014     */
015    
016    public class WeatherForm15 extends JPanel {
017    
018            //****************************************************************************//
019            //************************ Variables below ***********************************//
020            //****************************************************************************//
021    
022            /** the station ID */
023            private String strStaID;
024    
025            /** Combo box variable */
026            private JComboBox boxStaLocations;
027    
028            /** Array of Airport Labels for the combo box */
029            private String[] strComboBoxAirports = new String[180];
030    
031            /** Width of JFrame */
032            private int WIDTH = 525;
033    
034            /** Height of JFrame */
035            private int HEIGHT = 525;
036    
037            /** Main Window to hold form */
038            private JFrame frame;
039    
040            /** The form within the frame */
041            private JPanel panel;
042    
043            /** Main title on the form */
044            private JLabel lblTitle;
045    
046            /** Label to hold the name of the programmer */
047            private JLabel lblProgrammer;
048    
049            /** Label at top of airport names */
050            private JLabel lblAirport;
051    
052            /** Label at top of nine altitudes */
053            private JLabel lblAltitude;
054    
055            /** Label to hold the Wind Direction */
056            private JLabel lblDir;
057    
058            /** Label to hold the Wind Speed */
059            private JLabel lblSp;
060    
061            /** Label to hold the Wind Temperature */
062            private JLabel lblTemp;
063    
064            /** Static label at left of Wind Direction */
065            private JLabel lblPDir;
066    
067            /** Static label at left of Wind Speed */
068            private JLabel lblPSp;
069    
070            /** Static label at left of Wind Temperature */
071            private JLabel lblPTemp;
072    
073            /** The Clear Database Button */
074            private JButton btnClrDB;
075    
076            /** The Load Winds Aloft Button */
077            private JButton btnLoadWinds;
078    
079            /** The Get Station Details Button */
080            private JButton btnStaDetails;
081    
082            /** The Load Combo Box Button */
083            private JButton btnLoadCombo;
084    
085            /** The Exit Button */
086            private JButton btnExit;
087    
088            /** Radio buttons for the ten altitudes */
089            private JRadioButton[] Alt = new JRadioButton[10];
090    
091            /** Object containing the code for decoding the Nat Weather Svc FD report */
092            private NWSFB06[] wea;
093    
094            /** Temporary variable to hold Airport Name */
095            private String strAirport;
096    
097            /** Temporary variable to hold a number indicating which Airport radio button has been selected */
098            private int intAirport;
099    
100            /** Array of Altitude Labels */
101            private String[] strAlt = { "SUR", "3", "6", "9", "12", "18", "24", "30", "34", "39" };
102    
103            /** Temporary variable to hold Altitude Labels */
104            private String strAltitude = "3";
105    
106            /** Temporary variable to hold a number indicating which altitude radio button has been selected */
107            private int intAltitude;
108    
109            private JTextArea TA;
110    
111            //****************************************************************************//
112            //************************ Main Contructor below *****************************//
113            //****************************************************************************//
114    
115            /**
116             * Weather Form Constructor
117             */
118            public WeatherForm15() // constructor
119            {
120                    intAirport = 0; // initialize Airport
121                    intAltitude = 3; // initialize Altitude
122                    strAltitude = "3"; // initialize Altitude
123    
124                    frame = new JFrame("Form 15");
125                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
126    
127                    panel = new JPanel(null);
128                    panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
129                    panel.setBackground(Color.lightGray);
130    
131                    lblTitle = new JLabel("Black Mountain Aviation Center");
132                    lblTitle.setFont(new Font("Helvetica", Font.BOLD, 24));
133                    lblTitle.setSize(410, 20);
134                    lblTitle.setLocation(15, 25);
135                    lblTitle.setForeground(Color.blue);
136                    panel.add(lblTitle);
137    
138                    lblProgrammer = new JLabel("programmed by Matthew T Hough");
139                    lblProgrammer.setSize(200, 20);
140                    lblProgrammer.setLocation(100, 50); // 100 from Left, 50 from Top
141                    panel.add(lblProgrammer);
142    
143                    //****************************************************************************//
144                    //************************** Labels below ************************************//
145                    //****************************************************************************//
146    
147                    /* Label for the airports */
148                    lblAirport = new JLabel("Airport");
149                    lblAirport.setSize(200, 20);
150                    lblAirport.setLocation(15, 100);
151                    panel.add(lblAirport);
152    
153                    //****************************************************************//
154                    //****************      Combo Box Below       ********************//
155                    //****************************************************************//
156    
157                    /* Variable for the ComboListener */
158                    ComboListener comboListen = new ComboListener();
159    
160                    /* Setup combo box here for airport locations */
161                    boxStaLocations = new JComboBox();
162                    boxStaLocations.addActionListener(comboListen);
163                    boxStaLocations.setSize(350, 25);
164                    boxStaLocations.setLocation(15, 125);
165                    panel.add(boxStaLocations);
166    
167                    /* Label for the Altitude */
168                    lblAltitude = new JLabel("Altitude in Thousands");
169                    lblAltitude.setSize(200, 20);
170                    lblAltitude.setLocation(15, 175);
171                    panel.add(lblAltitude);
172    
173                    /* Radio buttons for Altitude here */
174                    RBListener altListener = new RBListener();
175                    ButtonGroup groupAlt = new ButtonGroup();
176                    for (int count = 0; count < 10; count++) {
177                            Alt[count] = new JRadioButton(strAlt[count], true);
178                            groupAlt.add(Alt[count]);
179                            Alt[count].addActionListener(altListener);
180                            Alt[count].setSize(52, 20); // wide, high
181                            int x = 200; // pixels from top
182                            int y = (count * 49) + 15; // pixels from left
183                            Alt[count].setLocation(y, x); // left,top
184                            panel.add(Alt[count]);
185                    }
186    
187                    /* Labels for the Answers */
188                    lblPDir = new JLabel("Direction: ");
189                    lblPDir.setSize(100, 20);
190                    lblPDir.setLocation(15, 225);
191                    panel.add(lblPDir);
192    
193                    lblDir = new JLabel("-----");
194                    lblDir.setSize(100, 20);
195                    lblDir.setLocation(115, 225);
196                    panel.add(lblDir);
197    
198                    lblPSp = new JLabel("Speed: ");
199                    lblPSp.setSize(100, 20);
200                    lblPSp.setLocation(15, 250);
201                    panel.add(lblPSp);
202    
203                    lblSp = new JLabel("-----");
204                    lblSp.setSize(100, 20);
205                    lblSp.setLocation(115, 250);
206                    panel.add(lblSp);
207    
208                    lblPTemp = new JLabel("Temp C: ");
209                    lblPTemp.setSize(100, 20);
210                    lblPTemp.setLocation(15, 275);
211                    panel.add(lblPTemp);
212    
213                    lblTemp = new JLabel("-----");
214                    lblTemp.setSize(100, 20);
215                    lblTemp.setLocation(115, 275);
216                    panel.add(lblTemp);
217    
218                    //****************************************************************************//
219                    //*************************** Buttons below **********************************//
220                    //****************************************************************************//
221    
222                    /** Clear database Button */
223                    btnClrDB = new JButton("Clear Database");
224                    btnClrDB.addActionListener(new ButtonListener());
225                    btnClrDB.setSize(350, 30);
226                    btnClrDB.setLocation(15, 300);
227                    panel.add(btnClrDB);
228    
229                    /** Load Winds Aloft Button */
230                    btnLoadWinds = new JButton("Load Winds Aloft");
231                    btnLoadWinds.addActionListener(new ButtonListener());
232                    btnLoadWinds.setSize(350, 30);
233                    btnLoadWinds.setLocation(15, 340);
234                    panel.add(btnLoadWinds);
235    
236                    /** Get Station Details Button */
237                    btnStaDetails = new JButton("Get Station Details");
238                    btnStaDetails.addActionListener(new ButtonListener());
239                    btnStaDetails.setSize(350, 30);
240                    btnStaDetails.setLocation(15, 380);
241                    panel.add(btnStaDetails);
242    
243                    /** Load Combo Box Button */
244                    btnLoadCombo = new JButton("Load Combo Box");
245                    btnLoadCombo.addActionListener(new ButtonListener());
246                    btnLoadCombo.setSize(350, 30);
247                    btnLoadCombo.setLocation(15, 420);
248                    panel.add(btnLoadCombo);
249    
250                    /** Exit Button */
251                    btnExit = new JButton("Exit");
252                    btnExit.addActionListener(new ButtonListener());
253                    btnExit.setSize(350, 30);
254                    btnExit.setLocation(15, 460);
255                    panel.add(btnExit);
256    
257                    frame.getContentPane().add(panel);
258    
259            } // end of DemoForm07
260    
261            //****************************************************************************//
262            //******************************* Methods below ******************************//
263            //****************************************************************************//
264    
265            /** This method deletes all of the database data */
266            public void deleteAll() {
267                    /* Instatiate a new DBUPdt1 object and execute the deleteAll() method */
268                    DBUpdt15 deleteDB = new DBUpdt15();
269                    try {
270                            deleteDB.open("BMAC", "", ""); //open the database connection
271                            deleteDB.deleteAll("station"); //delete all fields
272                            deleteDB.close(); //close the database connection
273                    } // end of try block
274                    catch (Exception e) {
275                            System.out.println(e);
276                    } // end of catch block
277            }
278    
279            /** This method calls the loadWindsAloft() method from a DBUpdt15 object */
280            public void loadWindsAloft() {
281                    /* Instatiate a new DBUPdt15 object and execute the loadWindsAloft() method */
282                    DBUpdt15 loadWinds = new DBUpdt15();
283                    loadWinds.loadWindsAloft();
284            }
285    
286            /** This method calls the getStationDetails() method from a DBUpdt15 object */
287            public void getStationDetails() {
288                    /* Instatiate a new DBUPdt15 object and execute the getStationDetails() method */
289                    DBUpdt15 StaDetails = new DBUpdt15();
290                    StaDetails.getStationDetails();
291            }
292    
293            /** This method downloads the winds aloft report and saves the data to the database */
294            public void loadComboBox() {
295                    /* Instantiate a new DBUpdt15 object */
296                    DBUpdt15 dbComboBox = new DBUpdt15();
297    
298                    /* Reset the combo box in the case it was already filled */
299                    boxStaLocations.removeAllItems();
300    
301                    try {
302                            /* Open the database */
303                            dbComboBox.open("BMAC", "", "");
304                            /* Get the recordset */
305                            ResultSet s = dbComboBox.getField("stationName");
306                            /* Loop through the ResultSet */
307                            while (s.next()) {
308                                    /* Add the records to the combo box */
309                                    String j = s.getString("stationName");
310                                    if (j!=null){
311                                            int x = 0;
312                                            strComboBoxAirports[x] = j;
313                                            boxStaLocations.addItem(strComboBoxAirports[x]);
314                                            x++;
315                                    } //end of if block 
316                            } //end of while block
317                            /* Close the database */
318                            dbComboBox.close();
319                    } //end of try block
320                    catch (Exception e) {
321                            System.out.println(e);
322                    }
323            }
324    
325            public void display() {
326                    frame.pack();
327                    frame.setVisible(true);
328            }
329    
330            /** This method selects the airport from the combo box and then displays the appropriate weather based on the altitude from the radio button */
331            public void updateScreen(String strStaID, String strAltitude) {
332    
333                    /* Connect to the database */
334                    DBUpdt15 dbComboBox = new DBUpdt15();
335                    try {
336                            dbComboBox.open("BMAC", "", "");
337                    }
338                    catch (Exception e) {
339                            System.out.println("Problems connecting");
340                    }
341                    /* Search for the station ID */
342                    try {
343                            ResultSet rs = dbComboBox.query("SELECT * FROM station WHERE stationName ='" + strStaID + "'");
344                            if (rs!=null){
345                                    while (rs.next()){
346                                            String strStation = rs.getString("windsaloft").toString(); //get the station data from the database
347    
348                                            /* Use an NWSFB06 object to extract the appropriate data based on the altitude */
349                                            NWSFB06 getWeather = new NWSFB06(strStation);
350                                            lblDir.setText(getWeather.getWindDir(strAltitude));
351                                            lblSp.setText(getWeather.getWindSpeed(strAltitude));
352                                            lblTemp.setText(getWeather.getWindTemp(strAltitude));
353                                    } //end of while block
354                            } //end of if block
355                    } //end of try block
356                    catch (Exception e) {
357                            System.out.println(e);
358                    } 
359            }
360    
361            /**
362             * Code for what to do when user clicks on one of the Radio Buttons
363             */
364            private class RBListener implements ActionListener {
365                    public void actionPerformed(ActionEvent event) {
366    
367                            Object source = event.getSource();
368                            for (int count=1; count<10; count++)
369                            {
370                                    /* handle the SUR button here */
371                                    if (source == Alt[0])
372                                    {
373                                            String strAltitude = strAlt[1];
374                                            updateScreen(strStaID, strAltitude);
375                                            return;
376                                    }
377                                    /* handle the rest of the altitude buttons here */
378                                    if (source == Alt[count])
379                                    {
380                                            String strAltitude = strAlt[count];
381                                            updateScreen(strStaID, strAltitude);
382                                    }
383                            } //end of for block
384                    } // end of action performed
385            }// end of RBListener
386    
387            /** Code for the combo box */
388            private class ComboListener implements ActionListener
389            {
390                    public void actionPerformed(ActionEvent event)
391                    {
392                            /* Count the combo box */
393                            int k = boxStaLocations.getMaximumRowCount();
394                            /* if the combo box didn't load more than 2, don't fire */
395                            if (k<2){
396                                    //do nothing
397                            } //end of if block
398                            else {
399                                    strStaID = boxStaLocations.getSelectedItem().toString(); // get the station from the combo box
400                                    updateScreen(strStaID, strAltitude); //call the updateScreen function
401                            } //end of else
402                    } 
403            } //end of combolistener
404    
405            /** Code for what to do when user presses one of the main buttons */
406            private class ButtonListener implements ActionListener {
407                    public void actionPerformed(ActionEvent event) {
408                            if (event.getSource() == btnClrDB) {
409                                    deleteAll(); // delete all records in the database
410                            }
411                            if (event.getSource() == btnLoadWinds) {
412                                    loadWindsAloft(); // download the weather info and save it in a text file
413                            }
414                            if (event.getSource() == btnStaDetails) {
415                                    getStationDetails(); // download the XML file details
416                            }
417                            if (event.getSource() == btnLoadCombo) {
418                                    loadComboBox(); // load the combo box
419                            }
420                            if (event.getSource() == btnExit) {
421                                    System.exit(0); // return to operating system
422                            }
423                    }
424            } // end of ButtonListener
425    
426    }