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