Skip to content

Commit f970a76

Browse files
author
pitsios-s
committed
Added new design pattern.
Implementation for the Model-View-Presenter pattern is now available.
1 parent 106b0b9 commit f970a76

File tree

12 files changed

+816
-0
lines changed

12 files changed

+816
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Test line 1
2+
Test line 2
130 KB
Loading

model-view-presenter/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>model-view-presenter</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>model-view-presenter</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>

model-view-presenter/pom.xml~

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>observer</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>observer</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.io.BufferedReader;
2+
import java.io.File;
3+
import java.io.FileReader;
4+
5+
/**
6+
* Every instance of this class represents the Model component
7+
* in the Model-View-Presenter architectural pattern.
8+
*
9+
* It is responsible for reading and loading the contents of a given file.
10+
*/
11+
public class FileLoader {
12+
13+
/**
14+
* Indicates if the file is loaded or not.
15+
*/
16+
private boolean loaded = false;
17+
18+
/**
19+
* The name of the file that we want to load.
20+
*/
21+
private String fileName;
22+
23+
/**
24+
* Loads the data of the file specified.
25+
*/
26+
public String loadData() {
27+
try {
28+
BufferedReader br = new BufferedReader(new FileReader(new File(this.fileName)));
29+
String text = "";
30+
String line = "";
31+
32+
while( (line = br.readLine()) != null ) {
33+
text += line + "\n";
34+
}
35+
36+
this.loaded = true;
37+
br.close();
38+
39+
return text;
40+
}
41+
42+
catch(Exception e) {
43+
e.printStackTrace();
44+
}
45+
46+
return null;
47+
}
48+
49+
/**
50+
* Sets the path of the file to be loaded, to the given value.
51+
*
52+
* @param fileName The path of the file to be loaded.
53+
*/
54+
public void setFileName(String fileName) {
55+
this.fileName = fileName;
56+
}
57+
58+
/**
59+
* @return fileName The path of the file to be loaded.
60+
*/
61+
public String getFileName() {
62+
return this.fileName;
63+
}
64+
65+
/**
66+
* @return True, if the file given exists, false otherwise.
67+
*/
68+
public boolean fileExists() {
69+
return new File(this.fileName).exists();
70+
}
71+
72+
/**
73+
* @return True, if the file is loaded, false otherwise.
74+
*/
75+
public boolean isLoaded() {
76+
return this.loaded;
77+
}
78+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
import java.awt.Color;
2+
import java.awt.event.ActionEvent;
3+
import java.awt.event.ActionListener;
4+
5+
import javax.swing.JButton;
6+
import javax.swing.JFrame;
7+
import javax.swing.JLabel;
8+
import javax.swing.JOptionPane;
9+
import javax.swing.JPanel;
10+
import javax.swing.JScrollPane;
11+
import javax.swing.JTextArea;
12+
import javax.swing.JTextField;
13+
14+
/**
15+
* This class is the GUI implementation of the View component
16+
* In the Model-View-Presenter pattern.
17+
*/
18+
public class FileSelectorJFrame extends JFrame implements FileSelectorView, ActionListener {
19+
20+
/**
21+
* Default serial version ID.
22+
*/
23+
private static final long serialVersionUID = 1L;
24+
25+
/**
26+
* The "OK" button for loading the file.
27+
*/
28+
private JButton OK;
29+
30+
/**
31+
* The cancel button.
32+
*/
33+
private JButton cancel;
34+
35+
/**
36+
* The information label.
37+
*/
38+
private JLabel info;
39+
40+
/**
41+
* The contents label.
42+
*/
43+
private JLabel contents;
44+
45+
/**
46+
* The text field for giving the name of the file
47+
* that we want to open.
48+
*/
49+
private JTextField input;
50+
51+
/**
52+
* A text area that will keep the contents of the file opened.
53+
*/
54+
private JTextArea area;
55+
56+
/**
57+
* The panel that will hold our widgets.
58+
*/
59+
private JPanel panel;
60+
61+
/**
62+
* The Presenter component that the frame will interact with
63+
*/
64+
private FileSelectorPresenter presenter;
65+
66+
/**
67+
* The name of the file that we want to read it's contents.
68+
*/
69+
private String fileName;
70+
71+
/**
72+
* Constructor.
73+
*/
74+
public FileSelectorJFrame() {
75+
super("File Loader");
76+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
77+
this.setLayout(null);
78+
this.setBounds(100, 100, 500, 200);
79+
80+
/*
81+
* Add the panel.
82+
*/
83+
this.panel = new JPanel();
84+
panel.setLayout(null);
85+
this.add(panel);
86+
panel.setBounds(0, 0, 500, 200);
87+
panel.setBackground(Color.LIGHT_GRAY);
88+
89+
/*
90+
* Add the info label.
91+
*/
92+
this.info = new JLabel("File Name :");
93+
this.panel.add(info);
94+
info.setBounds(30, 10, 100, 30);
95+
96+
/*
97+
* Add the contents label.
98+
*/
99+
this.contents = new JLabel("File contents :");
100+
this.panel.add(contents);
101+
this.contents.setBounds(30, 100, 120, 30);
102+
103+
/*
104+
* Add the text field.
105+
*/
106+
this.input = new JTextField(100);
107+
this.panel.add(input);
108+
this.input.setBounds(150, 15, 200, 20);
109+
110+
/*
111+
* Add the text area.
112+
*/
113+
this.area = new JTextArea(100, 100);
114+
JScrollPane pane = new JScrollPane(area);
115+
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
116+
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
117+
this.panel.add(pane);
118+
this.area.setEditable(false);
119+
pane.setBounds(150, 100, 250, 80);
120+
121+
/*
122+
* Add the OK button.
123+
*/
124+
this.OK = new JButton("OK");
125+
this.panel.add(OK);
126+
this.OK.setBounds(250, 50, 100, 25);
127+
this.OK.addActionListener(this);
128+
129+
/*
130+
* Add the cancel button.
131+
*/
132+
this.cancel = new JButton("Cancel");
133+
this.panel.add(this.cancel);
134+
this.cancel.setBounds(380, 50, 100, 25);
135+
this.cancel.addActionListener(this);
136+
137+
this.presenter = null;
138+
this.fileName = null;
139+
}
140+
141+
@Override
142+
public void actionPerformed(ActionEvent e) {
143+
if(e.getSource() == this.OK) {
144+
this.fileName = this.input.getText();
145+
presenter.fileNameChanged();
146+
presenter.confirmed();
147+
}
148+
149+
else if(e.getSource() == this.cancel) {
150+
presenter.cancelled();
151+
}
152+
}
153+
154+
@Override
155+
public void open() {
156+
this.setVisible(true);
157+
}
158+
159+
@Override
160+
public void close() {
161+
this.dispose();
162+
}
163+
164+
@Override
165+
public boolean isOpened() {
166+
return this.isVisible();
167+
}
168+
169+
@Override
170+
public void setPresenter(FileSelectorPresenter presenter) {
171+
this.presenter = presenter;
172+
}
173+
174+
@Override
175+
public FileSelectorPresenter getPresenter() {
176+
return this.presenter;
177+
}
178+
179+
@Override
180+
public void setFileName(String name) {
181+
this.fileName = name;
182+
}
183+
184+
@Override
185+
public String getFileName() {
186+
return this.fileName;
187+
}
188+
189+
@Override
190+
public void showMessage(String message) {
191+
JOptionPane.showMessageDialog(null, message);
192+
}
193+
194+
@Override
195+
public void displayData(String data) {
196+
this.area.setText(data);
197+
}
198+
}

0 commit comments

Comments
 (0)