Skip to content

Commit d3fbc2e

Browse files
committed
Merge pull request iluwatar#3 from pitsios-s/master
Model-View-Presenter pattern
2 parents 106b0b9 + 028aa39 commit d3fbc2e

File tree

12 files changed

+702
-1
lines changed

12 files changed

+702
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@
189189
* a snapshot of an object's state must be saved so that it can be restored to that state later, and
190190
* a direct interface to obtaining the state would expose implementation details and break the object's encapsulation
191191

192+
##Model-View-Presenter
193+
**Intent:** Apply a "Separation of Concerns" principle in a way that allows developers to build and test user interfaces.
194+
195+
![alt text](https://github.com/pitsios-s/java-design-patterns/blob/master/model-view-presenter/etc/model-view-presenter.jpg "Model-View-Presenter")
196+
197+
**Applicability:** Use the Model-View-Presenter in any of the following situations
198+
* when you want to improve the "Separation of Concerns" principle in presentation logic
199+
* when a user interface development and testing is necessary.
200+
192201
##Observer
193202
**Intent:** Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
194203

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

0 commit comments

Comments
 (0)