Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.GradientPaint;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.beans.PropertyChangeEvent;
- import java.io.File;
- import java.util.ArrayList;
- import javax.swing.BoxLayout;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFileChooser;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JRadioButton;
- import javax.swing.JSlider;
- import javax.swing.JSpinner;
- import javax.swing.SpinnerNumberModel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- import javax.swing.filechooser.FileFilter;
- import abstractMVC.AbstractView;
- /**
- * Controls View (the panel with the controls to control the system)
- *
- * @author JamesAcres
- *
- */
- public class ControlsView extends AbstractView {
- private static final long serialVersionUID = 1632885958396420224L;
- private ControlsController controller;
- private JFileChooser fileChooser;
- private JButton importAirport;
- private JSpinner orientationBaring;
- private JPanel dayNightPanel;
- private ButtonGroup dayNightView;
- private JRadioButton dayView;
- private JRadioButton nightView;
- private static Font controlFont;
- private JLabel airportName;
- private JComboBox runways;
- private ActionListener runwaysActionListener;
- private boolean setByModel = false;
- private int fireCount = 0;
- /**
- Nicks changes
- */
- private JLabel airportNameHolder;
- private JLabel airportNameLabel;
- private JComboBox categoryChoser;
- private JLabel categoryLabel;
- private JLabel dayNightLabel;
- private JButton importButton;
- private JLabel importLabel;
- private JSlider orientationSlider;
- private JLabel orientationLabel;
- private JLabel runwayLabel;
- private JComboBox runwaySelector;
- /**
- * Constructs the Control Panel
- */
- public ControlsView(final ControlsController controller) {
- // The controller for this view
- this.controller = controller;
- // Set background colour to light gray
- this.setBackground(Color.LIGHT_GRAY);
- //this.setBorder(new EmptyBorder(15, 15, 15, 15));
- //this.setLayout(new GridLayout(9, 2));
- //this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
- ControlsView.controlFont = new Font("Verdana", 0, 14);
- // Setup the file chooser
- this.setupFileChooser();
- /*// Add Import Airport Button
- // Setup import crossword
- importAirport = new JButton("Import Airport");
- importAirport.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- // Loop until get file that exists
- while (true) {
- int returnVal = fileChooser.showOpenDialog(null);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- if (fileChooser.getSelectedFile().exists()) {
- // Hide display while rendering
- controller.changeAirportDisplayAirport(new Boolean(false));
- // Change the airport file path
- controller.changeAirportAirportFilePath(file.getPath());
- break;
- }
- } else if (returnVal == JFileChooser.CANCEL_OPTION) {
- // Don't show box again if cancelling
- break;
- }
- }
- }
- });
- this.runways = new JComboBox();
- this.runwaysActionListener = new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- if (runways.getSelectedItem().getClass().getName().equals("Runway")) {
- JOptionPane.showMessageDialog(null, "Selected Runway " + ((Runway) runways.getSelectedItem()).getRunwayID() + ".", "Selected Runway", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- };
- this.runways.addActionListener(this.runwaysActionListener);
- //NICK DO SOME GUI STUFF HERE
- this.orientationBaring = new JSpinner(new SpinnerNumberModel(0,-1,360,1));
- this.orientationBaring.addChangeListener(new ChangeListener(){
- @Override
- public void stateChanged(ChangeEvent arg0) {
- // TODO Auto-generated method stub
- if (orientationBaring.getValue().equals(360)) orientationBaring.setValue(0);
- if (orientationBaring.getValue().equals(-1)) orientationBaring.setValue(359);
- if(setByModel)
- {
- setByModel = false;
- fireCount = 0;
- }
- else
- {
- orientationBaring.setValue((Integer) orientationBaring.getValue()%360);
- controller.changeAirportOrientationBaring((Integer) orientationBaring.getValue());
- }
- }
- });
- this.dayNightPanel = new JPanel();
- this.dayView = new JRadioButton("Day");
- this.dayView.setActionCommand("Day");
- this.dayView.setSelected(true);
- this.nightView = new JRadioButton("Night");
- this.nightView.setActionCommand("Night");
- this.dayNightView = new ButtonGroup();
- this.dayNightPanel.setBackground(Color.LIGHT_GRAY);
- setupDayNightRadioButtons(this.dayView);
- setupDayNightRadioButtons(this.nightView);
- JLabel importLabel = new JLabel("Import:");
- importLabel.setFont(ControlsView.controlFont);
- this.add(importLabel);
- this.add(importAirport);
- this.add(new JLabel(""));
- this.add(new JLabel(""));
- JLabel airportLabel = new JLabel("Airport:");
- airportLabel.setFont(ControlsView.controlFont);
- this.add(airportLabel);
- this.airportName = new JLabel("");
- airportName.setFont(ControlsView.controlFont);
- this.add(this.airportName);
- JLabel categoryLabel = new JLabel("Category:");
- categoryLabel.setFont(ControlsView.controlFont);
- this.add(categoryLabel);
- this.add(new JLabel(""));
- JLabel runwayLabel = new JLabel("Runway:");
- runwayLabel.setFont(ControlsView.controlFont);
- this.add(runwayLabel);
- this.add(this.runways);
- JLabel weatherLabel = new JLabel("Weather:");
- weatherLabel.setFont(ControlsView.controlFont);
- this.add(weatherLabel);
- this.add(new JLabel(""));
- JLabel directionLabel = new JLabel("Orientation:");
- directionLabel.setFont(ControlsView.controlFont);
- this.add(directionLabel);
- this.add(this.orientationBaring);
- JLabel daynightLabel = new JLabel("Day/Night:");
- daynightLabel.setFont(ControlsView.controlFont);
- this.add(daynightLabel);
- this.add(dayNightPanel);
- //this.add(nightView);
- //this.add(new JLabel(""));
- this.add(new JLabel(""));
- this.add(new JLabel(""));
- */
- initComponents();
- }
- /*protected void paintComponent(Graphics g) {
- if (!isOpaque()) {
- super.paintComponent(g);
- return;
- }
- Graphics2D g2d = (Graphics2D) g;
- // Paint a gradient from top to bottom
- GradientPaint gp = new GradientPaint(0, 0, Color.LIGHT_GRAY, this.getWidth(), 0, Color.WHITE);
- g2d.setPaint(gp);
- g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
- setOpaque(false);
- super.paintComponent(g);
- setOpaque(true);
- }*/
- public void setupDayNightRadioButtons(JRadioButton rb){
- this.dayNightView.add(rb);
- this.dayNightPanel.add(rb);
- rb.setBackground(Color.LIGHT_GRAY);
- rb.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- boolean day;
- if(dayNightView.getSelection().getActionCommand().equals("Day")){
- day = true;
- } else {
- day = false;
- }
- controller.changeAirportDayNightView(day);
- }
- });
- }
- private void setupFileChooser() {
- // Setup the file dialogue
- this.fileChooser = new JFileChooser();
- // Here the files are restricted to type PNG only
- this.fileChooser.setFileFilter(new FileFilter() {
- public boolean accept(File file) {
- // Show all directories
- if (file.isDirectory()) {
- return true;
- } else {
- // Show file paths which end in .apx
- String path = file.getAbsolutePath().toLowerCase();
- if (((path.endsWith("apx") && (path.charAt(path.length() - 3 - 1)) == '.'))) {
- return true;
- }
- }
- return false;
- }
- public String getDescription() {
- // Shows txt file in the type dropdown
- return "Airport files (.apx)";
- }
- });
- }
- private void initComponents() {
- importLabel = new javax.swing.JLabel();
- importButton = new javax.swing.JButton();
- airportNameLabel = new javax.swing.JLabel();
- airportNameHolder = new javax.swing.JLabel();
- categoryLabel = new javax.swing.JLabel();
- categoryChoser = new javax.swing.JComboBox();
- runwayLabel = new javax.swing.JLabel();
- runwaySelector = new javax.swing.JComboBox();
- orientationLabel = new javax.swing.JLabel();
- orientationSlider = new javax.swing.JSlider();
- dayNightLabel = new javax.swing.JLabel();
- this.setBackground(new java.awt.Color(191, 191, 191));
- importLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
- importLabel.setText("Import:");
- importButton.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
- importButton.setText("Import Airport");
- importButton.setMinimumSize(new java.awt.Dimension(135, 27));
- importButton.setPreferredSize(new java.awt.Dimension(135, 27));
- importButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- // /importButtonActionPerformed(evt);
- }
- });
- airportNameLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
- airportNameLabel.setText("Airport Name:");
- airportNameHolder.setText("jLabel1");
- categoryLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
- categoryLabel.setText("Category:");
- categoryChoser.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
- runwayLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
- runwayLabel.setText("Runway:");
- runwaySelector.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
- orientationLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
- orientationLabel.setText("Orientation:");
- dayNightLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
- dayNightLabel.setText("Day/Night:");
- javax.swing.GroupLayout controlsPanelLayout = new javax.swing.GroupLayout(this);
- this.setLayout(controlsPanelLayout);
- controlsPanelLayout.setHorizontalGroup(
- controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(controlsPanelLayout.createSequentialGroup()
- .addContainerGap()
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(controlsPanelLayout.createSequentialGroup()
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(airportNameLabel)
- .addComponent(categoryLabel)
- .addComponent(importLabel)
- .addComponent(runwayLabel)
- .addComponent(orientationLabel))
- .addGap(18, 18, 18)
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(airportNameHolder, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
- .addComponent(importButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(runwaySelector, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(categoryChoser, 0, 134, Short.MAX_VALUE)
- .addComponent(orientationSlider, 0, 0, Short.MAX_VALUE)))
- .addComponent(dayNightLabel))
- .addContainerGap(20, Short.MAX_VALUE))
- );
- controlsPanelLayout.setVerticalGroup(
- controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(controlsPanelLayout.createSequentialGroup()
- .addContainerGap()
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(importLabel)
- .addComponent(importButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(airportNameLabel)
- .addComponent(airportNameHolder))
- .addGap(18, 18, 18)
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(categoryLabel)
- .addComponent(categoryChoser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(runwayLabel)
- .addComponent(runwaySelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(orientationLabel)
- .addComponent(orientationSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addComponent(dayNightLabel)
- .addContainerGap(350, Short.MAX_VALUE))
- );
- }
- /**
- * Called by the controller when the model changes
- */
- public void modelPropertyChange(PropertyChangeEvent evt) {
- // Put all the GUI elements to update here
- if (evt.getPropertyName().equals(AirportController.AIRPORT_AIRPORTNAME_PROPERTY)) {
- this.airportName.setText((String) evt.getNewValue());
- } else if (evt.getPropertyName().equals(AirportController.AIRPORT_RUNWAYS_PROPERTY)) {
- @SuppressWarnings("unchecked")
- ArrayList<Runway> runwayArrayList = ((ArrayList<Runway>) evt.getNewValue());
- this.runways.removeActionListener(this.runwaysActionListener);
- this.runways.removeAllItems();
- this.runways.addItem("Select a runway");
- for (Runway thisRunway : runwayArrayList) {
- this.runways.addItem(thisRunway);
- }
- this.runways.addActionListener(this.runwaysActionListener);
- } else if (evt.getPropertyName().equals(AirportController.AIRPORT_ROTATE_PROPERTY)) {
- fireCount++;
- if(fireCount % 2 == 0){
- setByModel = true;
- System.err.println("Hello buddys");
- if((Double) evt.getNewValue()-(Double) evt.getOldValue() < 0){
- orientationBaring.setValue((360+(Integer)orientationBaring.getValue()+(int)Math.round(Math.toDegrees(((Double)evt.getNewValue()-(Double)evt.getOldValue())))) %360);
- } else {
- orientationBaring.setValue((Integer)orientationBaring.getValue()+(int)Math.round(Math.toDegrees(((Double)evt.getNewValue()-(Double)evt.getOldValue()))) %360);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement