/*
* FlightList.java
*
* Created on 28. Februar 2008, 21:03
*/
package gui.windows;
import enums.PlaneStates;
import framework.IATMCModel;
import framework.IFlightList;
import framework.IPlaneUpdateObject;
import framework.IRadarPlaneObject;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.border.LineBorder;
import logic.FlightListModel;
import logic.FlightListRenderer;
import messages.PlaneUpdateObject;
/**
*
* @author m0ng85
*/
public class FlightList extends javax.swing.JFrame implements IFlightList {
private static int UPDATE_INTERVALL = 1000;
private FlightList localCopy;
private IATMCModel atmcModel;
private FlightListModel flightListModel = new FlightListModel();
private FlightListRenderer flightListRenderer;
private boolean isAssigned;
private IRadarPlaneObject selectedPlane;
private String[] functions;
private String name;
private Thread update;
/** Creates new form FlightList */
public FlightList(IATMCModel model, boolean assigned) {
atmcModel = model;
isAssigned = assigned;
flightListRenderer = new FlightListRenderer(this);
localCopy = this;
initComponents();
initTable();
name = "Assigned";
if (!isAssigned) {
name = "Unassigned";
}
this.setTitle(name);
update = new FlightListUpdate();
update.setDaemon(true);
update.start();
}
public void initTable() {
planeTable.setRowHeight(70);
planeTable.setModel(flightListModel);
planeTable.setDefaultRenderer(Object.class, flightListRenderer);
planeTable.getColumnModel().getColumn(0).setHeaderValue(name);
planeTable.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
identifyPlane(e);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
scrollPane = new javax.swing.JScrollPane();
planeTable = new javax.swing.JTable();
planeTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
scrollPane.setViewportView(planeTable);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void identifyPlane(MouseEvent e) {
int mouseY = e.getY();
int planeNr = 0;
while (mouseY > 70) {
mouseY -= 70;
planeNr++;
}
if (planeNr >= 0 && planeNr < flightListModel.getRowCount()) {
planeClicked(e, planeNr);
}
}
private void planeClicked(MouseEvent e, int planeNr) {
IRadarPlaneObject clickedPlane =
(IRadarPlaneObject) flightListModel.getValueAt(planeNr, 0);
if (e.getButton() == 3) {
planeRightClicked(e, clickedPlane);
} else {
planeLeftClicked(clickedPlane);
}
}
private void planeRightClicked(MouseEvent e, IRadarPlaneObject plane) {
selectedPlane = plane;
repaint();
ContextMenu cm = new ContextMenu();
cm.show(planeTable, e.getX(), e.getY());
}
private void planeLeftClicked(IRadarPlaneObject plane) {
if (selectedPlane != null && selectedPlane.equals(plane)) {
selectedPlane = null;
repaint();
} else if (selectedPlane != null && !selectedPlane.equals(plane) ||
selectedPlane == null) {
selectedPlane = plane;
repaint();
}
}
public String getFunction() {
return atmcModel.getFunction();
}
public void removePlane(IRadarPlaneObject rpo) {
flightListModel.removeRow(rpo);
}
public void addPlane(IRadarPlaneObject rpo) {
flightListModel.addRow(rpo);
flightListModel.fireTableDataChanged();
}
public IRadarPlaneObject getSelectedPlane() {
return selectedPlane;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTable planeTable;
private javax.swing.JScrollPane scrollPane;
// End of variables declaration//GEN-END:variables
private final class ContextMenu extends JPopupMenu {
private JMenuItem details = new JMenuItem("Details");
private JMenuItem assignHeader = new JMenuItem("Assign to");
private JMenuItem stateHeader = new JMenuItem("State");
private JMenu stateMenu = new JMenu(selectedPlane.getPlaneState());
/**
* Initialisiert das Menu
*/
private ContextMenu() {
super();
this.setBorder(new LineBorder(Color.BLACK));
this.setBorderPainted(true);
initMenuItems();
this.add(details);
initUserList();
initControllers();
this.add(stateHeader);
initStateMenu();
this.add(stateMenu);
}
/**
* Initialisiert den State teil des rechtsklick menues.
*/
private void initStateMenu() {
for (int i = 0; i < PlaneStates.values().length; i++) {
JMenuItem tmp = new JMenuItem(PlaneStates.values()[i].toString());
final int k = i;
tmp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedPlane.setPlaneState(PlaneStates.values()[k].toString());
atmcModel.sendObject(new PlaneUpdateObject(selectedPlane));
flightListModel.fireTableDataChanged();
}
});
stateMenu.add(tmp);
}
}
/**
* Initialisiert die aktuelle Liste aller verbundenen Fluglotsen
*/
private void initUserList() {
functions = atmcModel.getFunctions().clone();
}
/**
* Initialisiert die Menuunterpunkte zur Controllerzuteilung basierend
* auf der aktuelle Userlist.
*/
private void initControllers() {
this.add(assignHeader);
if (isAssigned) {
JMenuItem tmp = new JMenuItem("Unassign");
tmp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oC = selectedPlane.getActiveController();
String nC = "???";
selectedPlane.setActiveController(nC);
IPlaneUpdateObject tmp = new PlaneUpdateObject(selectedPlane);
atmcModel.handlePlaneUpdate(selectedPlane, oC, nC);
atmcModel.sendObject(tmp);
flightListModel.fireTableDataChanged();
}
});
this.add(tmp);
for (int i = 0; i < functions.length; i++) {
if (!functions[i].equals("Pilot") && !functions[i].equals("-no function-")) {
final int k = i;
tmp = new JMenuItem(functions[i]);
tmp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oC = selectedPlane.getActiveController();
String nC = functions[k];
selectedPlane.setActiveController(nC);
IPlaneUpdateObject tmp = new PlaneUpdateObject(selectedPlane);
atmcModel.handlePlaneUpdate(selectedPlane, oC, nC);
atmcModel.sendObject(tmp);
flightListModel.fireTableDataChanged();
}
});
this.add(tmp);
}
}
} else {
JMenuItem tmp = new JMenuItem("Myself");
tmp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oC = selectedPlane.getActiveController();
String nC = atmcModel.getFunction();
selectedPlane.setActiveController(nC);
IPlaneUpdateObject tmp = new PlaneUpdateObject(selectedPlane);
atmcModel.handlePlaneUpdate(selectedPlane, oC, nC);
atmcModel.sendObject(tmp);
flightListModel.fireTableDataChanged();
}
});
this.add(tmp);
}
}
/**
* Initialisiert die Menu Überschriften.
*/
private void initMenuItems() {
assignHeader.setEnabled(false);
assignHeader.setBackground(Color.BLACK);
stateHeader.setEnabled(false);
stateHeader.setBackground(Color.BLACK);
details.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PlaneEditor tmp = new PlaneEditor(atmcModel, localCopy);
}
});
}
}
public class FlightListUpdate extends Thread {
@SuppressWarnings("static-access")
@Override
public void run() {
while (true) {
try {
if(localCopy.isVisible()){
flightListModel.fireTableDataChanged();
}
this.sleep(UPDATE_INTERVALL);
} catch (InterruptedException ex) {
Logger.getLogger(FlightList.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void clear() {
flightListModel.removeAll();
flightListModel.fireTableDataChanged();
}
public void fireChange() {
flightListModel.fireTableDataChanged();
}
}