//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : FileSelectorView.java
// @ Date : 2016/9/5
// @ Author :
//
//
/** * This interface represents the View component in the Model-View-Presenter * pattern.It can be implemented by either the GUI components,or by the Stub. * */
public interface FileSelectorView {
public void open();
public void close();
public boolean isOpened();
public void setPresenter(FileSelectorPresenter presenter);
public FileSelectorPresenter getPresenter();
public void setFileName(String name);
public String getFileName();
public void showMessage(String message);
public void displayData(String str);
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : FileSelectorJFrame.java
// @ Date : 2016/9/5
// @ Author :
//
//
@SuppressWarnings("serial")
public class FileSelectorJFrame extends JFrame implements FileSelectorView,ActionListener {
public JButton OK;
public JButton cancel;
public JLabel info;
public JLabel contents;
public JTextField input;
public JTextArea area;
public JPanel panel;
private FileSelectorPresenter presenter;
public String fileName;
public FileSelectorJFrame() {
super("File Loader");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
// this.setLayout(null);
this.setLayout(new BorderLayout());
this.setBounds(100,100,500,200);
//Add the panel.
this.panel = new JPanel();
this.add(panel,BorderLayout.CENTER);
this.panel.setLayout(new GridLayout(2, 2));
// panel.setLayout(null);
panel.setBounds(0,0,500,200);
panel.setBackground(Color.LIGHT_GRAY);
//Add the info label.
this.info = new JLabel("File Name:");
this.panel.add(info);
info.setBounds(30,10,100,30);
//Add the text field.
this.input = new JTextField(100);
this.panel.add(input);
this.input.setBounds(150,15,200,20);
this.contents = new JLabel("File contents :");
this.panel.add(contents);
this.contents.setBounds(30, 100, 120, 30);
//Add the text area
this.area = new JTextArea(100,100);
JScrollPane pane = new JScrollPane(area);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.panel.add(pane);
this.area.setEditable(false);
pane.setBounds(150,100,250,80);
//Add the OK button.
JPanel south = new JPanel();
south.setLayout(new GridLayout(1,2));
this.OK = new JButton("OK");
// this.panel.add(OK);
this.OK.setBounds(250,50,100,25);
this.OK.addActionListener(this);
south.add(OK);
//Add the cancel button.
this.cancel = new JButton("Cancel");
// this.panel.add(this.cancel);
this.cancel.setBounds(380,50,100,25);
this.cancel.addActionListener(this);
south.add(cancel);
this.add(south,BorderLayout.SOUTH);
// this.pack();
this.setSize(740, 390);
this.presenter = null;
this.fileName = null;
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == this.OK){
this.fileName = this.input.getText();
presenter.fileNameChanged();
presenter.confirmed();
} else if(event.getSource() == this.cancel) {
presenter.cancelled();
}
}
public void open() {
this.setVisible(true);
}
public void close() {
this.dispose();
}
public boolean isOpened() {
return this.isVisible();
}
public void setPresenter(FileSelectorPresenter presenter) {
this.presenter = presenter;
}
public FileSelectorPresenter getPresenter() {
return presenter;
}
public void setFileName(String name) {
this.fileName = name;
}
public String getFileName() {
return this.fileName;
}
public void showMessage(String msg) {
JOptionPane.showMessageDialog(null,msg);
}
public void displayData(String data) {
this.area.setText(data);
}
}
package com.think.in.java.design.pattern.lesson36;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : FileSelectorPresenter.java
// @ Date : 2016/9/5
// @ Author :
//
//
public class FileSelectorPresenter {
private FileSelectorView view;
private FileLoader loader;
public FileSelectorPresenter(FileSelectorView view) {
this.view = view;
}
public void setLoader(FileLoader loader) {
this.loader = loader;
}
public void start() {
view.setPresenter(this);
view.open();
}
public void fileNameChanged() {
loader.setFileName(view.getFileName());
}
public void confirmed() {
if(loader.getFileName() == null ||
loader.getFileName().equals("")){
view.showMessage("Please give the name of the file first!");
return;
}
if(loader.fileExists()){
String data = loader.loadData();
view.displayData(data);
}
else {
view.showMessage("The file specified does not exist.");
}
}
public void cancelled() {
view.close();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : FileLoader.java
// @ Date : 2016/9/5
// @ Author :
//
//
public class FileLoader {
public boolean loaded = false;
public String fileName;
public FileLoader() {
}
public String loadData() {
try {
BufferedReader br = new BufferedReader(new FileReader(
new File(this.fileName)
));
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null)
{
sb.append(line).append("\n");
}
this.loaded = true;
br.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void setFileName(String name) {
this.fileName = name;
}
public String getFileName() {
return this.fileName;
}
public boolean fileExists() {
return new File(this.fileName).exists();
}
public boolean isLoaded() {
return this.loaded;
}
}
public class App {
public static void main(String[] args) {
FileLoader loader = new FileLoader();
FileSelectorJFrame jFrame = new FileSelectorJFrame();
FileSelectorPresenter presenter = new FileSelectorPresenter(jFrame);
presenter.setLoader(loader);
presenter.start();
}
}