Java 11 migraiton: model-view-presenter
This commit is contained in:
		| @@ -44,9 +44,9 @@ public class App { | ||||
|    * @param args command line args | ||||
|    */ | ||||
|   public static void main(String[] args) { | ||||
|     FileLoader loader = new FileLoader(); | ||||
|     FileSelectorJFrame frame = new FileSelectorJFrame(); | ||||
|     FileSelectorPresenter presenter = new FileSelectorPresenter(frame); | ||||
|     var loader = new FileLoader(); | ||||
|     var frame = new FileSelectorJFrame(); | ||||
|     var presenter = new FileSelectorPresenter(frame); | ||||
|     presenter.setLoader(loader); | ||||
|     presenter.start(); | ||||
|   } | ||||
|   | ||||
| @@ -27,6 +27,7 @@ import java.io.BufferedReader; | ||||
| import java.io.File; | ||||
| import java.io.FileReader; | ||||
| import java.io.Serializable; | ||||
| import java.util.stream.Collectors; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| @@ -59,18 +60,11 @@ public class FileLoader implements Serializable { | ||||
|    * Loads the data of the file specified. | ||||
|    */ | ||||
|   public String loadData() { | ||||
|     String dataFileName = this.fileName; | ||||
|     try (BufferedReader br = new BufferedReader(new FileReader(new File(dataFileName)))) { | ||||
|       StringBuilder sb = new StringBuilder(); | ||||
|       String line; | ||||
|  | ||||
|       while ((line = br.readLine()) != null) { | ||||
|         sb.append(line).append('\n'); | ||||
|       } | ||||
|  | ||||
|     var dataFileName = this.fileName; | ||||
|     try (var br = new BufferedReader(new FileReader(new File(dataFileName)))) { | ||||
|       var result = br.lines().collect(Collectors.joining("\n")); | ||||
|       this.loaded = true; | ||||
|  | ||||
|       return sb.toString(); | ||||
|       return result; | ||||
|     } catch (Exception e) { | ||||
|       LOGGER.error("File {} does not exist", dataFileName); | ||||
|     } | ||||
|   | ||||
| @@ -48,37 +48,22 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti | ||||
|   /** | ||||
|    * The "OK" button for loading the file. | ||||
|    */ | ||||
|   private JButton ok; | ||||
|   private final JButton ok; | ||||
|  | ||||
|   /** | ||||
|    * The cancel button. | ||||
|    */ | ||||
|   private JButton cancel; | ||||
|  | ||||
|   /** | ||||
|    * The information label. | ||||
|    */ | ||||
|   private JLabel info; | ||||
|  | ||||
|   /** | ||||
|    * The contents label. | ||||
|    */ | ||||
|   private JLabel contents; | ||||
|   private final JButton cancel; | ||||
|  | ||||
|   /** | ||||
|    * The text field for giving the name of the file that we want to open. | ||||
|    */ | ||||
|   private JTextField input; | ||||
|   private final JTextField input; | ||||
|  | ||||
|   /** | ||||
|    * A text area that will keep the contents of the file opened. | ||||
|    */ | ||||
|   private JTextArea area; | ||||
|  | ||||
|   /** | ||||
|    * The panel that will hold our widgets. | ||||
|    */ | ||||
|   private JPanel panel; | ||||
|   private final JTextArea area; | ||||
|  | ||||
|   /** | ||||
|    * The Presenter component that the frame will interact with. | ||||
| @@ -102,7 +87,7 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti | ||||
|     /* | ||||
|      * Add the panel. | ||||
|      */ | ||||
|     this.panel = new JPanel(); | ||||
|     var panel = new JPanel(); | ||||
|     panel.setLayout(null); | ||||
|     this.add(panel); | ||||
|     panel.setBounds(0, 0, 500, 200); | ||||
| @@ -111,32 +96,32 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti | ||||
|     /* | ||||
|      * Add the info label. | ||||
|      */ | ||||
|     this.info = new JLabel("File Name :"); | ||||
|     this.panel.add(info); | ||||
|     var info = new JLabel("File Name :"); | ||||
|     panel.add(info); | ||||
|     info.setBounds(30, 10, 100, 30); | ||||
|  | ||||
|     /* | ||||
|      * Add the contents label. | ||||
|      */ | ||||
|     this.contents = new JLabel("File contents :"); | ||||
|     this.panel.add(contents); | ||||
|     this.contents.setBounds(30, 100, 120, 30); | ||||
|     var contents = new JLabel("File contents :"); | ||||
|     panel.add(contents); | ||||
|     contents.setBounds(30, 100, 120, 30); | ||||
|  | ||||
|     /* | ||||
|      * Add the text field. | ||||
|      */ | ||||
|     this.input = new JTextField(100); | ||||
|     this.panel.add(input); | ||||
|     panel.add(input); | ||||
|     this.input.setBounds(150, 15, 200, 20); | ||||
|  | ||||
|     /* | ||||
|      * Add the text area. | ||||
|      */ | ||||
|     this.area = new JTextArea(100, 100); | ||||
|     JScrollPane pane = new JScrollPane(area); | ||||
|     var pane = new JScrollPane(area); | ||||
|     pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | ||||
|     pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); | ||||
|     this.panel.add(pane); | ||||
|     panel.add(pane); | ||||
|     this.area.setEditable(false); | ||||
|     pane.setBounds(150, 100, 250, 80); | ||||
|  | ||||
| @@ -144,7 +129,7 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti | ||||
|      * Add the OK button. | ||||
|      */ | ||||
|     this.ok = new JButton("OK"); | ||||
|     this.panel.add(ok); | ||||
|     panel.add(ok); | ||||
|     this.ok.setBounds(250, 50, 100, 25); | ||||
|     this.ok.addActionListener(this); | ||||
|  | ||||
| @@ -152,7 +137,7 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti | ||||
|      * Add the cancel button. | ||||
|      */ | ||||
|     this.cancel = new JButton("Cancel"); | ||||
|     this.panel.add(this.cancel); | ||||
|     panel.add(this.cancel); | ||||
|     this.cancel.setBounds(380, 50, 100, 25); | ||||
|     this.cancel.addActionListener(this); | ||||
|  | ||||
|   | ||||
| @@ -41,7 +41,7 @@ public class FileSelectorPresenter implements Serializable { | ||||
|   /** | ||||
|    * The View component that the presenter interacts with. | ||||
|    */ | ||||
|   private FileSelectorView view; | ||||
|   private final FileSelectorView view; | ||||
|  | ||||
|   /** | ||||
|    * The Model component that the presenter interacts with. | ||||
| @@ -91,7 +91,7 @@ public class FileSelectorPresenter implements Serializable { | ||||
|     } | ||||
|  | ||||
|     if (loader.fileExists()) { | ||||
|       String data = loader.loadData(); | ||||
|       var data = loader.loadData(); | ||||
|       view.displayData(data); | ||||
|     } else { | ||||
|       view.showMessage("The file specified does not exist."); | ||||
|   | ||||
| @@ -26,16 +26,13 @@ package com.iluwatar.model.view.presenter; | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Application test | ||||
|  * | ||||
|  */ | ||||
| public class AppTest { | ||||
|  | ||||
|   @Test | ||||
|   public void test() { | ||||
|     String[] args = {}; | ||||
|     App.main(args); | ||||
|     App.main(new String[]{}); | ||||
|   } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -23,10 +23,10 @@ | ||||
|  | ||||
| package com.iluwatar.model.view.presenter; | ||||
|  | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||||
|  | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| /** | ||||
|  * Date: 12/21/15 - 12:12 PM | ||||
|  * | ||||
| @@ -35,8 +35,8 @@ import static org.junit.jupiter.api.Assertions.assertNull; | ||||
| public class FileLoaderTest { | ||||
|  | ||||
|   @Test | ||||
|   public void testLoadData() throws Exception { | ||||
|     final FileLoader fileLoader = new FileLoader(); | ||||
|   public void testLoadData() { | ||||
|     final var fileLoader = new FileLoader(); | ||||
|     fileLoader.setFileName("non-existing-file"); | ||||
|     assertNull(fileLoader.loadData()); | ||||
|   } | ||||
|   | ||||
| @@ -23,14 +23,14 @@ | ||||
|  | ||||
| package com.iluwatar.model.view.presenter; | ||||
|  | ||||
| import org.junit.jupiter.api.BeforeEach; | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
|  | ||||
| import org.junit.jupiter.api.BeforeEach; | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| /** | ||||
|  * This test case is responsible for testing our application by taking advantage of the | ||||
|  * Model-View-Controller architectural pattern. | ||||
| @@ -79,7 +79,7 @@ public class FileSelectorPresenterTest { | ||||
|    */ | ||||
|   @Test | ||||
|   public void updateFileNameToLoader() { | ||||
|     String expectedFile = "Stamatis"; | ||||
|     var expectedFile = "Stamatis"; | ||||
|     stub.setFileName(expectedFile); | ||||
|  | ||||
|     presenter.start(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user