Package model

Source Code of model.ATMCModel

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package model;

import framework.IAirport;
import framework.INavaidCoord;
import framework.IRadarColor;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import messages.RadarPlaneObject;
import enums.ServiceTags;
import events.AirportEvent;
import events.ColorEvent;
import events.HeightEvent;
import events.NavaidEvent;
import events.ServerEvent;
import framework.IPlaneDataObject;
import java.util.Map;
import logic.*;
import framework.IATMCModel;
import framework.IATMClient;
import framework.IAirportXML;
import framework.IConnection;
import framework.IILS;
import framework.INavaidXML;
import framework.IPlaneUpdateObject;
import framework.IRadarTab;
import framework.IRadarPlaneObject;
import framework.IReceive;
import framework.IServer;
import framework.ISettingsXML;
import gui.windows.FlightList;
import java.io.File;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.TreeMap;
import javax.swing.JPanel;
import messages.ServiceObject;

/**
*
* @author m0ng85
*/
public class ATMCModel extends Observable implements IATMCModel {

    private IATMClient atmClient;
    private IConnection connection;
    private IRadarTab radar;
    private IILS ILS;
    private IReceive receive;
    private String nickName;
    private String server;
    private int port;
    private String function;
    private String[] functions;
    private TreeMap<String, IRadarPlaneObject> radarPlaneList = new TreeMap<String, IRadarPlaneObject>();
    private List<IPlaneUpdateObject> pul = new LinkedList<IPlaneUpdateObject>();
    private FlightList assignedList = new FlightList(this, true);
    private FlightList unassignedList = new FlightList(this, false);
    private File settingsData = new File("data/settings.xml");
    private File airportData = new File("data/map.xml");
    private ISettingsXML settingsXML;
    private INavaidXML navaidXML;
    private IAirportXML airportXML;
    private LinkedList<IServer> servers = new LinkedList<IServer>();
    private HashMap<String, IAirport> airports = new HashMap<String, IAirport>();
    private HashMap<String, INavaidCoord> navaids = new HashMap<String, INavaidCoord>();
    private TreeMap<String, IRadarColor> colors = new TreeMap<String, IRadarColor>();
    private int maxHeight = 99999;
    private int minHeight = 0;

    public ATMCModel(IATMClient atmc) {
        atmClient = atmc;
        connection = new Connection(this);
        initServers();
        initColors();
        initNavaids();
        initAirports();
    }

    public void initServers() {
        try {
            settingsXML = new SettingsXML(settingsData, XMLSettingsAccess.GenerateDocument(settingsData));
            servers = settingsXML.getServers();
            setChanged();
            notifyObservers(new ServerEvent());
        } catch (IOException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setNick(String nick) {
        this.nickName = nick;
    }

    public void saveSettings(String server, int port, String nickName) {
        this.addServer(new Server(server, port));
        this.server = server;
        this.port = port;
    }

    public boolean connect(String server, int port, String nickName) {
        if (connection.connect(server, port, nickName)) {
            return true;
        } else {
            return false;
        }
    }

    public void setStatus(final String message) {
        atmClient.setStatus(message);
    }

    public boolean isConnected() {
        return connection.isConnected();
    }

    public void disconnect() {
        connection.disconnect();
    }

    public void sendObject(Object o) {
        connection.sendObject(o);
    }

    public void sendChatMessage(String message) {
        connection.sendChatMessage(message, server);
    }

    public void addChatMessage(String message) {
        atmClient.addChatMessage(message);
    }

    public void addStatusMessage(String message) {
        atmClient.addStatusMessage(message);
    }

    public JPanel getRadarScreen() {
        return radar.getRadarScreen();
    }

    public String getServer() {
        return server;
    }

    public String getNickname() {
        return nickName;
    }

    public int getPort() {
        return port;
    }

    public TreeMap<String, IRadarPlaneObject> getPlanes() {
        return radarPlaneList;
    }

    public String[] getFunctions() {
        return functions;
    }

    public void setFunctions(String[] func) {
        functions = func;
        atmClient.setFunctions(func);
    }

    public String getFunction() {
        return function;
    }

    public void setFunction(String func) {
        function = func;
        if (function.equals("Pilot")) {
            atmClient.startXPlaneClient();
            assignedList.clear();
            unassignedList.clear();
            assignedList.setVisible(false);
            unassignedList.setVisible(false);
        } else if (function.equals("-no function-")) {
            atmClient.stopXPlaneClient();
            assignedList.clear();
            unassignedList.clear();
            assignedList.setVisible(false);
            unassignedList.setVisible(false);
        } else {
            atmClient.stopXPlaneClient();
            assignedList.clear();
            unassignedList.clear();
            assignedList.setVisible(true);
            unassignedList.setVisible(true);
            updateFlightLists();
        }

        ServiceObject sout = new ServiceObject(nickName, ServiceTags.FunctionChoosen);
        sout.setValue(func);
        connection.sendObject(sout);
    }

    public void updateFlightLists() {
        unassignedList.clear();
        assignedList.clear();
        for (IRadarPlaneObject r : radarPlaneList.values()) {
            handlePlane(r);
        }
    }

    public void updatePlaneData(IPlaneUpdateObject puo) {
        IRadarPlaneObject tmp = radarPlaneList.get(puo.getIp());
        handlePlaneUpdate(tmp, tmp.getActiveController(), puo.getActiveController());
        tmp.update(puo);
    }

    /**
     * Adds a new plane.
     *
     * @param puo the planeupdateobject of the new plane
     */
    private void addPlane(IPlaneUpdateObject puo) {
        IRadarPlaneObject tmp = new RadarPlaneObject(puo);
        radarPlaneList.put(puo.getIp(), tmp);
        handlePlane(tmp);
    }

    /**
     * Adds a new plane.
     *
     * @param pdo the planedataobject of the new plane
     */
    private void addPlane(IPlaneDataObject pdo) {
        IRadarPlaneObject tmp = new RadarPlaneObject(pdo);
        radarPlaneList.put(pdo.getIP(), tmp);
        handlePlane(tmp);
    }

    /**
     * Removes a plane.
     *
     * @param puo the planeupdateobject of the plane to be removed
     */
    private void removePlane(IPlaneUpdateObject puo) {
        IRadarPlaneObject tmp = new RadarPlaneObject(puo);
        radarPlaneList.remove(puo.getIp());
        assignedList.removePlane(tmp);
        unassignedList.removePlane(tmp);
    }

    public void handlePlaneUpdate(IRadarPlaneObject rpo, String oC, String nC) {

        if (!oC.equals(nC)) {
            if (oC.equals(function)) {
                assignedList.removePlane(rpo);
            } else if (oC.equals("???")) {
                unassignedList.removePlane(rpo);
            }

            if (nC.equals(function)) {
                assignedList.addPlane(rpo);
            } else if (nC.equals("???")) {
                unassignedList.addPlane(rpo);
            }
        }
    }

    /**
     * Handles a newly added plane for the flightlists.
     *
     * @param rpo the new plane
     */
    private void handlePlane(IRadarPlaneObject rpo) {
        if (rpo.getActiveController() != null && rpo.getActiveController().equals(function)) {
            assignedList.addPlane(rpo);
        } else if (rpo.getActiveController().equals("???")) {
            unassignedList.addPlane(rpo);
        }
    }

    public IATMClient getClient() {
        return atmClient;
    }

    public void addPlaneUpdate(IPlaneUpdateObject puo) {
        try {
            pul.add(puo);
            if (puo.getIp().equals(InetAddress.getLocalHost().toString())) {
                atmClient.setPilotData(puo);
            }
        } catch (UnknownHostException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addPlaneData(IPlaneDataObject pdo) {
        if (radarPlaneList.containsKey(pdo.getIP())) {
            IRadarPlaneObject current = radarPlaneList.get(pdo.getIP());
            current.addPosition(pdo);
        } else {
            this.addPlane(pdo);
        }

    }

    public void setUpdates() {
        Iterator<IPlaneUpdateObject> uit = pul.iterator();
        while (uit.hasNext()) {
            IPlaneUpdateObject puo = uit.next();
            if (radarPlaneList.containsKey(puo.getIp())) {
                IRadarPlaneObject current = radarPlaneList.get(puo.getIp());
                handlePlaneUpdate(current, current.getActiveController(), puo.getActiveController());
                current.update(puo);

            } else {
                this.addPlane(puo);
            }
            uit.remove();
        }
    }

    public String getUser() {
        return nickName;
    }

    public Map getRadarPlaneObjects() {
        return radarPlaneList;
    }

    public void updateRadarPlaneList(String[] userList) {
        boolean found = false;
        Iterator<IRadarPlaneObject> it = radarPlaneList.values().iterator();
        while (it.hasNext()) {
            IRadarPlaneObject rpo = it.next();
            String nick = rpo.getNick();
            System.out.print(nick);
            for (int i = 0; i < userList.length && !found; i++) {
                System.out.println(" - " + userList[i]);
                if (nick.equals(userList[i])) {
                    found = true;
                }
            }
            if (!found) {
                it.remove();
            }
        }
    }

    public void addColor(RadarColor radarColor) {
        try {
            settingsXML.addColor(radarColor);
            initColors();
        } catch (IOException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void addServer(Server server) {
        try {
            settingsXML.addServer(server);
            initServers();
        } catch (IOException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public TreeMap<String, IRadarColor> getRadarColors() {
        return colors;
    }

    public LinkedList<IServer> getServers() {
        return servers;
    }

    public void removeServer(Server server) {
        try {
            settingsXML.removeServer(server);
            initServers();
        } catch (IOException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public HashMap<String, IAirport> getAirports() {
        return airports;
    }

    public HashMap<String, INavaidCoord> getNavaids() {
        return navaids;
    }

    public void addNewObserver(Observer o) {
        super.addObserver(o);
    }

    public int getMinHeight() {
        return minHeight;
    }

    public int getMaxHeight() {
        return maxHeight;
    }

    public void setMinHeight(int height) {
        if (height > 99999) {
            height = 99999;
        } else if (height < 0) {
            height = 0;
        }

        minHeight = height;
        atmClient.addStatusMessage("New Minimum Height: " + minHeight);
        setChanged();
        notifyObservers(new HeightEvent());
    }

    public void setMaxHeight(int height) {
        if (height > 99999) {
            height = 99999;
        } else if (height < 0) {
            height = 0;
        }

        maxHeight = height;
        atmClient.addStatusMessage("New Maximum Height: " + maxHeight);
        setChanged();
        notifyObservers(new HeightEvent());
    }

    public void initColors() {
        try {
            settingsXML = new SettingsXML(settingsData, XMLSettingsAccess.GenerateDocument(settingsData));
            colors = settingsXML.getColors();
            setChanged();
            notifyObservers(new ColorEvent());
        } catch (IOException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void initAirports() {
        try {
            airportXML = new AirportXML(airportData, XMLMapAccess.GenerateDocument(airportData));
            airports = (HashMap<String, IAirport>) airportXML.getAirports();
            setChanged();
            notifyObservers(new AirportEvent());
        } catch (IOException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void initNavaids() {
        try {
            navaidXML = new NavaidXML(airportData, XMLMapAccess.GenerateDocument(airportData));
            navaids = (HashMap<String, INavaidCoord>) navaidXML.getNavaidData();
            setChanged();
            notifyObservers(new NavaidEvent());
        } catch (IOException ex) {
            Logger.getLogger(ATMCModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
TOP

Related Classes of model.ATMCModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.