assign3.client.WaypointClient.java Source code

Java tutorial

Introduction

Here is the source code for assign3.client.WaypointClient.java

Source

package assign3.client;

import assign3.server.*;
import assign3.client.*;
import javax.swing.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.rmi.*;
import java.text.NumberFormat;
import org.json.*;

/**
 * Copyright (c) 2014 Tim Lindquist,
 * Software Engineering,
 * Arizona State University at the Polytechnic campus
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 2
 * of the License.
 * <p/>
 * This program is distributed in the hope that it will be useful,
 * but without any warranty or fitness for a particular purpose.
 * <p/>
 * Please review the GNU General Public License at:
 * http://www.gnu.org/licenses/gpl-2.0.html
 * see also: https://www.gnu.org/licenses/gpl-faq.html
 * so you are aware of the terms and your rights with regard to this software.
 * Or, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
 * <p/>
 * Purpose: Java client UI for Waypoint management.
 * This class creates Gui components for a UI to manage waypoints.
 * This software is meant to run on OSX, and Windows Cygwin using g++.
 * WaypointClient demonstrates use of the WaypointGUI class
 * for solving cst420 assignments.
 * WaypointGUI class uses Swing components JTextField, JComboBox, and
 * JTextArea to realize a GUI for Waypoint management.
 * <p/>
 * Ser321 Principles of Distributed Software Systems.
 * see http://pooh.poly.asu.edu/Cst420
 * @author Tim Lindquist (Tim.Lindquist@asu.edu) CIDSE - Software Engineering
 *                       Ira Fulton Schools of Engineering, ASU Polytechnic
 * @file    WaypointGUI.java
 * @date    September, 2014
 * @license See above
 */
public class WaypointClient extends WaypointGUI implements ActionListener, ItemListener, java.io.Serializable {

    private static final boolean debugOn = true;
    private Waypoint aWp;
    private WaypointServer server = null;
    private String hostId;
    private String port;

    public WaypointClient(String base, String hostId, String port) throws RemoteException {
        super(base);
        this.hostId = hostId;
        this.port = port;
        removeWPButt.addActionListener(this);
        addWPButt.addActionListener(this);
        modWPButt.addActionListener(this);
        exportToJSON.addActionListener(this);
        distBearButt.addActionListener(this);
        frWps.addItemListener(this);
        toWps.addItemListener(this);

        try {
            server = (WaypointServer) Naming.lookup("rmi://" + hostId + ":" + port + "/WaypointServer");
            System.out.println("Client obtained remote object reference to" + " the WaypointServer at:\n" + "rmi://"
                    + hostId + ":" + port + "/WaypointServer");
        } catch (Exception e) {
            System.out.println("Could not find server.");
        }
        System.out.println("Getting Waypoints From Server.");
        String[] names = server.getNames();
        for (int i = 0; i < names.length; i++) {
            Waypoint aWp = server.get(names[i]);
            frWps.addItem(names[i]);
            toWps.addItem(names[i]);
            System.out.println(names[i]);
        }

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                debug("you clicked X");
                System.exit(0);
            }
        });

        setVisible(true);
    }

    public void itemStateChanged(ItemEvent event) {
        if (event.getStateChange() == ItemEvent.SELECTED) {
            Object comp = event.getSource();
            debug("Selection event generated by " + ((comp == frWps) ? "from " : "to ") + "combobox. "
                    + "Selected waypoint is: " + (String) event.getItem());
            String To = "to waypoint";
            String From = "from waypoint";
            String currentSelection;
            if (comp == frWps) {
                currentSelection = frWps.getSelectedItem().toString();
            } else {
                currentSelection = toWps.getSelectedItem().toString();
            }
            if ((currentSelection == "from waypoint") || (currentSelection == "to waypoint")) {
                latIn.setText("null");
                lonIn.setText("null");
                eleIn.setText("null");
                nameIn.setText("null");
                addrIn.setText("null");
            } else {
                try {
                    Waypoint currentWaypoint = server.get(currentSelection);
                    //System.out.println("Description Changed: "+server.get(currentSelection));
                    latIn.setText(String.valueOf(currentWaypoint.getLat()));
                    lonIn.setText(String.valueOf(currentWaypoint.getLon()));
                    eleIn.setText(String.valueOf(currentWaypoint.getEle()));
                    nameIn.setText(currentWaypoint.getName());
                    addrIn.setText(currentWaypoint.getAddr());
                } catch (RemoteException e) {
                    System.out.println("Error retrieving name from server.");
                }
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        try {
            if (e.getActionCommand().equals("Remove")) {
                debug("you clicked Remove Waypoint");
                frWps.removeItem(frWps.getSelectedItem());
                toWps.removeItem(toWps.getSelectedItem());
            } else if (e.getActionCommand().equals("Add")) {
                debug("you clicked Add Waypoint");
                addWaypoint();
                distBearIn.setText("Added: " + nameIn.getText());
            } else if (e.getActionCommand().equals("Modify")) {
                debug("you clicked Modify Waypoint");
            } else if (e.getActionCommand().equals("GetLatLon")) {
                debug("you clicked Get Lat/Lon");
            } else if (e.getActionCommand().equals("exportToJSON")) {
                debug("you clicked export to JSON");
                String[] names = server.getNames(); //Check this
                JSONObject obj = new JSONObject();
                for (int i = 0; i < names.length; i++) {
                    Waypoint wp = server.get(names[i]);
                    obj.put(names[i], wp.toJson());
                }
            } else if (e.getActionCommand().equals("Distance")) {
                debug("you clicked Distance and Bearing");
                String wpFrom = frWps.getSelectedItem().toString();
                String wpTo = toWps.getSelectedItem().toString();
                NumberFormat nf = NumberFormat.getInstance();
                nf.setMaximumFractionDigits(4);
                String distCalc = nf.format(server.distGC(wpFrom, wpTo));
                String dirCalc = nf.format(server.directionGC(wpFrom, wpTo));
                server.distGC(frWps.getName(), toWps.getName());
                server.directionGC(frWps.getName(), toWps.getName());
                distBearIn.setText("Distance from " + wpFrom + " to " + wpTo + " is " + distCalc
                        + " miles, bearing " + dirCalc + ".");
            }
        } catch (RemoteException ex) {
            System.out.println("Server connection error.");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    private void addWaypoint() throws RemoteException {
        frWps.addItem(nameIn.getText());
        double newLat = Double.parseDouble(latIn.getText());
        double newLon = Double.parseDouble(lonIn.getText());
        double newEle = Double.parseDouble(eleIn.getText());
        aWp = new Waypoint(newLat, newLon, newEle, nameIn.getText(), addrIn.getText());
        System.out.println("Name=" + nameIn.getText());
        System.out.println("Lat=" + newLat);
        System.out.println("Lon=" + newLon);
        System.out.println("Ele=" + newEle);
        System.out.println("Addr=" + addrIn.getText());
        server.add(nameIn.getText(), aWp);
        toWps.addItem(nameIn.getText());
    }

    private void debug(String message) {
        if (debugOn)
            System.out.println("debug: " + message);
    }

    public static void main(String args[]) {
        try {
            String name = "Cst420";
            String hostId = "localhost";
            String port = "2222";
            if (args.length >= 3) {
                name = args[0];
                hostId = args[1];
                port = args[2];
            }
            WaypointClient sa2 = new WaypointClient(name, hostId, port);

            /*BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                
            String inStr;
            Waypoint waypnt;
                
            do
            {
            inStr = stdin.readLine();
            if (inStr.equalsIgnoreCase("End"))
               break;
            // get the (serializable) employee object from the waypoint
            // server
            waypnt = server.get(inStr);
            // Note that getWaypoint() runs on server; getName() runs in
            // client
            System.out.println(waypnt.name);
            }
            while(true);*/
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}