Advertisement
Guest User

Untitled

a guest
May 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. /**
  2.  * The MIT License
  3.  * Copyright (c) 2014-2016 Ilkka Seppälä
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  6.  * of this software and associated documentation files (the "Software"), to deal
  7.  * in the Software without restriction, including without limitation the rights
  8.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9.  * copies of the Software, and to permit persons to whom the Software is
  10.  * furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice shall be included in
  13.  * all copies or substantial portions of the Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21.  * THE SOFTWARE.
  22.  */
  23. package com.iluwatar.model.view.presenter;
  24.  
  25. import java.awt.Color;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28.  
  29. import javax.swing.JButton;
  30. import javax.swing.JFrame;
  31. import javax.swing.JLabel;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.JPanel;
  34. import javax.swing.JScrollPane;
  35. import javax.swing.JTextArea;
  36. import javax.swing.JTextField;
  37.  
  38. /**
  39.  * This class is the GUI implementation of the View component in the Model-View-Presenter pattern.
  40.  */
  41. public class FileSelectorJFrame extends JFrame implements FileSelectorView, ActionListener {
  42.  
  43.   /**
  44.    * Default serial version ID.
  45.    */
  46.   private static final long serialVersionUID = 1L;
  47.  
  48.   /**
  49.    * The "OK" button for loading the file.
  50.    */
  51.   private JButton ok;
  52.  
  53.   /**
  54.    * The cancel button.
  55.    */
  56.   private JButton cancel;
  57.  
  58.   /**
  59.    * The information label.
  60.    */
  61.   private JLabel info;
  62.  
  63.   /**
  64.    * The contents label.
  65.    */
  66.   private JLabel contents;
  67.  
  68.   /**
  69.    * The text field for giving the name of the file that we want to open.
  70.    */
  71.   private JTextField input;
  72.  
  73.   /**
  74.    * A text area that will keep the contents of the file opened.
  75.    */
  76.   private JTextArea area;
  77.  
  78.   /**
  79.    * The panel that will hold our widgets.
  80.    */
  81.   private JPanel panel;
  82.  
  83.   /**
  84.    * The Presenter component that the frame will interact with
  85.    */
  86.   private FileSelectorPresenter presenter;
  87.  
  88.   /**
  89.    * The name of the file that we want to read it's contents.
  90.    */
  91.   private String fileName;
  92.  
  93.   /**
  94.    * Constructor.
  95.    */
  96.   public FileSelectorJFrame() {
  97.     super("File Loader");
  98.     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  99.     this.setLayout(null);
  100.     this.setBounds(100, 100, 500, 200);
  101.  
  102.     /*
  103.      * Add the panel.
  104.      */
  105.     this.panel = new JPanel();
  106.     panel.setLayout(null);
  107.     this.add(panel);
  108.     panel.setBounds(0, 0, 500, 200);
  109.     panel.setBackground(Color.LIGHT_GRAY);
  110.  
  111.     /*
  112.      * Add the info label.
  113.      */
  114.     this.info = new JLabel("File Name :");
  115.     this.panel.add(info);
  116.     info.setBounds(30, 10, 100, 30);
  117.  
  118.     /*
  119.      * Add the contents label.
  120.      */
  121.     this.contents = new JLabel("File contents :");
  122.     this.panel.add(contents);
  123.     this.contents.setBounds(30, 100, 120, 30);
  124.  
  125.     /*
  126.      * Add the text field.
  127.      */
  128.     this.input = new JTextField(100);
  129.     this.panel.add(input);
  130.     this.input.setBounds(150, 15, 200, 20);
  131.  
  132.     /*
  133.      * Add the text area.
  134.      */
  135.     this.area = new JTextArea(100, 100);
  136.     JScrollPane pane = new JScrollPane(area);
  137.     pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  138.     pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  139.     this.panel.add(pane);
  140.     this.area.setEditable(false);
  141.     pane.setBounds(150, 100, 250, 80);
  142.  
  143.     /*
  144.      * Add the OK button.
  145.      */
  146.     this.ok = new JButton("OK");
  147.     this.panel.add(ok);
  148.     this.ok.setBounds(250, 50, 100, 25);
  149.     this.ok.addActionListener(this);
  150.  
  151.     /*
  152.      * Add the cancel button.
  153.      */
  154.     this.cancel = new JButton("Cancel");
  155.     this.panel.add(this.cancel);
  156.     this.cancel.setBounds(380, 50, 100, 25);
  157.     this.cancel.addActionListener(this);
  158.  
  159.     this.presenter = null;
  160.     this.fileName = null;
  161.   }
  162.  
  163.   @Override
  164.   public void actionPerformed(ActionEvent e) {
  165.     if (this.ok.equals(e.getSource())) {
  166.       this.fileName = this.input.getText();
  167.       presenter.fileNameChanged();
  168.       presenter.confirmed();
  169.     } else if (this.cancel.equals(e.getSource())) {
  170.       presenter.cancelled();
  171.     }
  172.   }
  173.  
  174.   @Override
  175.   public void open() {
  176.     this.setVisible(true);
  177.   }
  178.  
  179.   @Override
  180.   public void close() {
  181.     this.dispose();
  182.   }
  183.  
  184.   @Override
  185.   public boolean isOpened() {
  186.     return this.isVisible();
  187.   }
  188.  
  189.   @Override
  190.   public void setPresenter(FileSelectorPresenter presenter) {
  191.     this.presenter = presenter;
  192.   }
  193.  
  194.   @Override
  195.   public FileSelectorPresenter getPresenter() {
  196.     return this.presenter;
  197.   }
  198.  
  199.   @Override
  200.   public void setFileName(String name) {
  201.     this.fileName = name;
  202.   }
  203.  
  204.   @Override
  205.   public String getFileName() {
  206.     return this.fileName;
  207.   }
  208.  
  209.   @Override
  210.   public void showMessage(String message) {
  211.     JOptionPane.showMessageDialog(null, message);
  212.   }
  213.  
  214.   @Override
  215.   public void displayData(String data) {
  216.     this.area.setText(data);
  217.   }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement