Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.63 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.GradientPaint;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.GridLayout;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.beans.PropertyChangeEvent;
  11. import java.io.File;
  12. import java.util.ArrayList;
  13.  
  14. import javax.swing.BoxLayout;
  15. import javax.swing.ButtonGroup;
  16. import javax.swing.JButton;
  17. import javax.swing.JComboBox;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JPanel;
  22. import javax.swing.JRadioButton;
  23. import javax.swing.JSlider;
  24. import javax.swing.JSpinner;
  25. import javax.swing.SpinnerNumberModel;
  26. import javax.swing.border.EmptyBorder;
  27. import javax.swing.event.ChangeEvent;
  28. import javax.swing.event.ChangeListener;
  29. import javax.swing.filechooser.FileFilter;
  30.  
  31. import abstractMVC.AbstractView;
  32.  
  33. /**
  34. * Controls View (the panel with the controls to control the system)
  35. *
  36. * @author JamesAcres
  37. *
  38. */
  39. public class ControlsView extends AbstractView {
  40.  
  41. private static final long serialVersionUID = 1632885958396420224L;
  42.  
  43. private ControlsController controller;
  44.  
  45. private JFileChooser fileChooser;
  46.  
  47. private JButton importAirport;
  48.  
  49. private JSpinner orientationBaring;
  50.  
  51. private JPanel dayNightPanel;
  52. private ButtonGroup dayNightView;
  53. private JRadioButton dayView;
  54. private JRadioButton nightView;
  55.  
  56. private static Font controlFont;
  57. private JLabel airportName;
  58. private JComboBox runways;
  59. private ActionListener runwaysActionListener;
  60.  
  61. private boolean setByModel = false;
  62. private int fireCount = 0;
  63.  
  64.  
  65. /**
  66. Nicks changes
  67. */
  68. private JLabel airportNameHolder;
  69. private JLabel airportNameLabel;
  70. private JComboBox categoryChoser;
  71. private JLabel categoryLabel;
  72. private JLabel dayNightLabel;
  73. private JButton importButton;
  74. private JLabel importLabel;
  75. private JSlider orientationSlider;
  76. private JLabel orientationLabel;
  77. private JLabel runwayLabel;
  78. private JComboBox runwaySelector;
  79.  
  80. /**
  81. * Constructs the Control Panel
  82. */
  83. public ControlsView(final ControlsController controller) {
  84. // The controller for this view
  85. this.controller = controller;
  86.  
  87. // Set background colour to light gray
  88. this.setBackground(Color.LIGHT_GRAY);
  89.  
  90. //this.setBorder(new EmptyBorder(15, 15, 15, 15));
  91.  
  92. //this.setLayout(new GridLayout(9, 2));
  93. //this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  94.  
  95. ControlsView.controlFont = new Font("Verdana", 0, 14);
  96.  
  97. // Setup the file chooser
  98. this.setupFileChooser();
  99.  
  100. /*// Add Import Airport Button
  101. // Setup import crossword
  102. importAirport = new JButton("Import Airport");
  103.  
  104. importAirport.addActionListener(new ActionListener() {
  105. public void actionPerformed(ActionEvent arg0) {
  106. // Loop until get file that exists
  107. while (true) {
  108. int returnVal = fileChooser.showOpenDialog(null);
  109. if (returnVal == JFileChooser.APPROVE_OPTION) {
  110. File file = fileChooser.getSelectedFile();
  111. if (fileChooser.getSelectedFile().exists()) {
  112. // Hide display while rendering
  113. controller.changeAirportDisplayAirport(new Boolean(false));
  114. // Change the airport file path
  115. controller.changeAirportAirportFilePath(file.getPath());
  116. break;
  117. }
  118. } else if (returnVal == JFileChooser.CANCEL_OPTION) {
  119. // Don't show box again if cancelling
  120. break;
  121. }
  122. }
  123. }
  124. });
  125.  
  126. this.runways = new JComboBox();
  127. this.runwaysActionListener = new ActionListener() {
  128. public void actionPerformed(ActionEvent arg0) {
  129. if (runways.getSelectedItem().getClass().getName().equals("Runway")) {
  130. JOptionPane.showMessageDialog(null, "Selected Runway " + ((Runway) runways.getSelectedItem()).getRunwayID() + ".", "Selected Runway", JOptionPane.INFORMATION_MESSAGE);
  131. }
  132. }
  133. };
  134. this.runways.addActionListener(this.runwaysActionListener);
  135.  
  136.  
  137. //NICK DO SOME GUI STUFF HERE
  138. this.orientationBaring = new JSpinner(new SpinnerNumberModel(0,-1,360,1));
  139. this.orientationBaring.addChangeListener(new ChangeListener(){
  140.  
  141. @Override
  142. public void stateChanged(ChangeEvent arg0) {
  143. // TODO Auto-generated method stub
  144. if (orientationBaring.getValue().equals(360)) orientationBaring.setValue(0);
  145. if (orientationBaring.getValue().equals(-1)) orientationBaring.setValue(359);
  146. if(setByModel)
  147. {
  148. setByModel = false;
  149. fireCount = 0;
  150. }
  151. else
  152. {
  153. orientationBaring.setValue((Integer) orientationBaring.getValue()%360);
  154. controller.changeAirportOrientationBaring((Integer) orientationBaring.getValue());
  155. }
  156. }
  157.  
  158. });
  159.  
  160.  
  161. this.dayNightPanel = new JPanel();
  162. this.dayView = new JRadioButton("Day");
  163. this.dayView.setActionCommand("Day");
  164. this.dayView.setSelected(true);
  165. this.nightView = new JRadioButton("Night");
  166. this.nightView.setActionCommand("Night");
  167. this.dayNightView = new ButtonGroup();
  168. this.dayNightPanel.setBackground(Color.LIGHT_GRAY);
  169. setupDayNightRadioButtons(this.dayView);
  170. setupDayNightRadioButtons(this.nightView);
  171.  
  172. JLabel importLabel = new JLabel("Import:");
  173.  
  174. importLabel.setFont(ControlsView.controlFont);
  175. this.add(importLabel);
  176. this.add(importAirport);
  177. this.add(new JLabel(""));
  178. this.add(new JLabel(""));
  179. JLabel airportLabel = new JLabel("Airport:");
  180. airportLabel.setFont(ControlsView.controlFont);
  181. this.add(airportLabel);
  182. this.airportName = new JLabel("");
  183. airportName.setFont(ControlsView.controlFont);
  184. this.add(this.airportName);
  185. JLabel categoryLabel = new JLabel("Category:");
  186. categoryLabel.setFont(ControlsView.controlFont);
  187. this.add(categoryLabel);
  188. this.add(new JLabel(""));
  189. JLabel runwayLabel = new JLabel("Runway:");
  190. runwayLabel.setFont(ControlsView.controlFont);
  191. this.add(runwayLabel);
  192. this.add(this.runways);
  193. JLabel weatherLabel = new JLabel("Weather:");
  194. weatherLabel.setFont(ControlsView.controlFont);
  195. this.add(weatherLabel);
  196. this.add(new JLabel(""));
  197. JLabel directionLabel = new JLabel("Orientation:");
  198. directionLabel.setFont(ControlsView.controlFont);
  199. this.add(directionLabel);
  200. this.add(this.orientationBaring);
  201. JLabel daynightLabel = new JLabel("Day/Night:");
  202. daynightLabel.setFont(ControlsView.controlFont);
  203. this.add(daynightLabel);
  204. this.add(dayNightPanel);
  205. //this.add(nightView);
  206. //this.add(new JLabel(""));
  207. this.add(new JLabel(""));
  208. this.add(new JLabel(""));
  209. */
  210. initComponents();
  211.  
  212. }
  213.  
  214. /*protected void paintComponent(Graphics g) {
  215. if (!isOpaque()) {
  216. super.paintComponent(g);
  217. return;
  218. }
  219.  
  220. Graphics2D g2d = (Graphics2D) g;
  221.  
  222. // Paint a gradient from top to bottom
  223. GradientPaint gp = new GradientPaint(0, 0, Color.LIGHT_GRAY, this.getWidth(), 0, Color.WHITE);
  224.  
  225. g2d.setPaint(gp);
  226. g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
  227.  
  228. setOpaque(false);
  229. super.paintComponent(g);
  230. setOpaque(true);
  231. }*/
  232.  
  233. public void setupDayNightRadioButtons(JRadioButton rb){
  234. this.dayNightView.add(rb);
  235. this.dayNightPanel.add(rb);
  236. rb.setBackground(Color.LIGHT_GRAY);
  237. rb.addActionListener(new ActionListener(){
  238.  
  239. @Override
  240. public void actionPerformed(ActionEvent arg0) {
  241. // TODO Auto-generated method stub
  242. boolean day;
  243. if(dayNightView.getSelection().getActionCommand().equals("Day")){
  244. day = true;
  245. } else {
  246. day = false;
  247. }
  248. controller.changeAirportDayNightView(day);
  249. }
  250.  
  251. });
  252. }
  253.  
  254. private void setupFileChooser() {
  255. // Setup the file dialogue
  256. this.fileChooser = new JFileChooser();
  257. // Here the files are restricted to type PNG only
  258. this.fileChooser.setFileFilter(new FileFilter() {
  259. public boolean accept(File file) {
  260. // Show all directories
  261. if (file.isDirectory()) {
  262. return true;
  263. } else {
  264. // Show file paths which end in .apx
  265. String path = file.getAbsolutePath().toLowerCase();
  266. if (((path.endsWith("apx") && (path.charAt(path.length() - 3 - 1)) == '.'))) {
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272.  
  273. public String getDescription() {
  274. // Shows txt file in the type dropdown
  275. return "Airport files (.apx)";
  276. }
  277.  
  278. });
  279. }
  280.  
  281. private void initComponents() {
  282. importLabel = new javax.swing.JLabel();
  283. importButton = new javax.swing.JButton();
  284. airportNameLabel = new javax.swing.JLabel();
  285. airportNameHolder = new javax.swing.JLabel();
  286. categoryLabel = new javax.swing.JLabel();
  287. categoryChoser = new javax.swing.JComboBox();
  288. runwayLabel = new javax.swing.JLabel();
  289. runwaySelector = new javax.swing.JComboBox();
  290. orientationLabel = new javax.swing.JLabel();
  291. orientationSlider = new javax.swing.JSlider();
  292. dayNightLabel = new javax.swing.JLabel();
  293.  
  294.  
  295. this.setBackground(new java.awt.Color(191, 191, 191));
  296.  
  297. importLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
  298. importLabel.setText("Import:");
  299.  
  300. importButton.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
  301. importButton.setText("Import Airport");
  302. importButton.setMinimumSize(new java.awt.Dimension(135, 27));
  303. importButton.setPreferredSize(new java.awt.Dimension(135, 27));
  304. importButton.addActionListener(new java.awt.event.ActionListener() {
  305. public void actionPerformed(java.awt.event.ActionEvent evt) {
  306. // /importButtonActionPerformed(evt);
  307. }
  308. });
  309.  
  310. airportNameLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
  311. airportNameLabel.setText("Airport Name:");
  312.  
  313. airportNameHolder.setText("jLabel1");
  314.  
  315. categoryLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
  316. categoryLabel.setText("Category:");
  317.  
  318. categoryChoser.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
  319.  
  320. runwayLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
  321. runwayLabel.setText("Runway:");
  322.  
  323. runwaySelector.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
  324.  
  325. orientationLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
  326. orientationLabel.setText("Orientation:");
  327.  
  328. dayNightLabel.setFont(new java.awt.Font("Verdana", 2, 18)); // NOI18N
  329. dayNightLabel.setText("Day/Night:");
  330.  
  331. javax.swing.GroupLayout controlsPanelLayout = new javax.swing.GroupLayout(this);
  332. this.setLayout(controlsPanelLayout);
  333. controlsPanelLayout.setHorizontalGroup(
  334. controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  335. .addGroup(controlsPanelLayout.createSequentialGroup()
  336. .addContainerGap()
  337. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  338. .addGroup(controlsPanelLayout.createSequentialGroup()
  339. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  340. .addComponent(airportNameLabel)
  341. .addComponent(categoryLabel)
  342. .addComponent(importLabel)
  343. .addComponent(runwayLabel)
  344. .addComponent(orientationLabel))
  345. .addGap(18, 18, 18)
  346. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  347. .addComponent(airportNameHolder, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
  348. .addComponent(importButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  349. .addComponent(runwaySelector, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  350. .addComponent(categoryChoser, 0, 134, Short.MAX_VALUE)
  351. .addComponent(orientationSlider, 0, 0, Short.MAX_VALUE)))
  352. .addComponent(dayNightLabel))
  353. .addContainerGap(20, Short.MAX_VALUE))
  354. );
  355. controlsPanelLayout.setVerticalGroup(
  356. controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  357. .addGroup(controlsPanelLayout.createSequentialGroup()
  358. .addContainerGap()
  359. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  360. .addComponent(importLabel)
  361. .addComponent(importButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  362. .addGap(18, 18, 18)
  363. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  364. .addComponent(airportNameLabel)
  365. .addComponent(airportNameHolder))
  366. .addGap(18, 18, 18)
  367. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  368. .addComponent(categoryLabel)
  369. .addComponent(categoryChoser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  370. .addGap(18, 18, 18)
  371. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  372. .addComponent(runwayLabel)
  373. .addComponent(runwaySelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  374. .addGap(18, 18, 18)
  375. .addGroup(controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  376. .addComponent(orientationLabel)
  377. .addComponent(orientationSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  378. .addGap(18, 18, 18)
  379. .addComponent(dayNightLabel)
  380. .addContainerGap(350, Short.MAX_VALUE))
  381. );
  382.  
  383. }
  384.  
  385.  
  386. /**
  387. * Called by the controller when the model changes
  388. */
  389. public void modelPropertyChange(PropertyChangeEvent evt) {
  390. // Put all the GUI elements to update here
  391. if (evt.getPropertyName().equals(AirportController.AIRPORT_AIRPORTNAME_PROPERTY)) {
  392. this.airportName.setText((String) evt.getNewValue());
  393. } else if (evt.getPropertyName().equals(AirportController.AIRPORT_RUNWAYS_PROPERTY)) {
  394.  
  395. @SuppressWarnings("unchecked")
  396. ArrayList<Runway> runwayArrayList = ((ArrayList<Runway>) evt.getNewValue());
  397. this.runways.removeActionListener(this.runwaysActionListener);
  398. this.runways.removeAllItems();
  399. this.runways.addItem("Select a runway");
  400. for (Runway thisRunway : runwayArrayList) {
  401. this.runways.addItem(thisRunway);
  402. }
  403. this.runways.addActionListener(this.runwaysActionListener);
  404.  
  405. } else if (evt.getPropertyName().equals(AirportController.AIRPORT_ROTATE_PROPERTY)) {
  406. fireCount++;
  407. if(fireCount % 2 == 0){
  408. setByModel = true;
  409. System.err.println("Hello buddys");
  410.  
  411. if((Double) evt.getNewValue()-(Double) evt.getOldValue() < 0){
  412. orientationBaring.setValue((360+(Integer)orientationBaring.getValue()+(int)Math.round(Math.toDegrees(((Double)evt.getNewValue()-(Double)evt.getOldValue())))) %360);
  413. } else {
  414. orientationBaring.setValue((Integer)orientationBaring.getValue()+(int)Math.round(Math.toDegrees(((Double)evt.getNewValue()-(Double)evt.getOldValue()))) %360);
  415. }
  416. }
  417. }
  418. }
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement