Jump to content

Applying a factory to a text editor. Rookie in JAVA

- - - - -

  • Please log in to reply
No replies to this topic

#1
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hi i wonder how these factories work. I have googled and looked on many examples but i think its still as hard to take that and implement it with my creation.

I have made en text editor using the MVC-pattern.
Our assignment is to make a factory that creates the textfile and re-do the model.

My code til now:

MODEL
 package model;

import java.io.*;


public class Model {

	private File currentFile;

	private String textFileText;


	public Model() 

	{

		this.currentFile = null;

		this.textFileText = "";

	}


	public void open(File f) 

	{

		this.currentFile = f;


		BufferedReader br = null;

		try 

		{

			br = new BufferedReader(new FileReader(f));

		} catch (FileNotFoundException e) 

		{

			System.err.println("Error: " + e.getMessage());

			e.printStackTrace();

		}

		textFileText = "";

		String st = "";

		try 

		{

			while((st = br.readLine()) != null)

			{

				textFileText += st;

			}


		} catch (IOException e) 

		{

			System.err.println("Error: " + e.getMessage());

			e.printStackTrace();

		}

	}


	public void save() 

	{

		try 

		{

			FileWriter fw = new FileWriter(this.currentFile);

			BufferedWriter out = new BufferedWriter(fw);

			out.write(this.textFileText);

			out.close();

		}catch (Exception e)

		{ 

			System.err.println("Error: " + e.getMessage());

		}	

	}


	public void setCurrentFile(File currentFile) 

	{

		this.currentFile = currentFile;

	}


	public File getCurrentFile() 

	{

		return currentFile;

	}


	public void setFileText(String s) 

	{

		this.textFileText = s;

	}


	public String getFileText() 

	{

		return textFileText;

	}

}

VIEW
package view;

import controller.*;


import java.awt.*;

import javax.swing.*;


import model.Model;


@SuppressWarnings("serial")

public class View extends javax.swing.JFrame 

{

	private Model m;


	private JMenuItem copy;

	private JMenuItem cut;

	private JMenu edit;

	private JMenuItem exit;

	private JMenu file;

	private JMenuBar menuBar;

	private JMenuItem newFile;

	private JMenuItem open;

	private JMenuItem paste;

	private JMenuItem save;

	private JMenuItem saveAs;

	private JScrollPane scrollPane;

	private JTextArea textArea;


	public View() 

	{

		m = new Model();

		initComponents();

	}


	private void initComponents() 

	{

		this.scrollPane = new JScrollPane();

		this.textArea = new JTextArea();

		this.menuBar = new JMenuBar();

		this.file = new JMenu();

		this.newFile = new JMenuItem();

		this.open = new JMenuItem();

		this.save = new JMenuItem();

		this.saveAs = new JMenuItem();

		this.exit = new JMenuItem();

		this.edit = new JMenu();

		this.copy = new JMenuItem();

		this.cut = new JMenuItem();

		this.paste = new JMenuItem();


		this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		this.setTitle("Notepad");


		this.textArea.setColumns(28);

		this.textArea.setRows(22);


		this.scrollPane.setViewportView(textArea);

		this.add(scrollPane, BorderLayout.CENTER);


		this.file.setText("File");


		this.newFile.setMnemonic('N');

		this.newFile.setText("New");

		this.newFile.addActionListener(new NewFileListener(this.m, this));

		this.file.add(newFile);


		this.open.setMnemonic('O');

		this.open.setText("Open");

		this.open.addActionListener(new OpenListener(this.m, this));

		this.file.add(open);


		this.save.setMnemonic('S');

		this.save.setText("Save");

		this.save.addActionListener(new SaveListener(this.m, this));

		this.file.add(save);


		this.saveAs.setMnemonic('A');

		this.saveAs.setText("Save As...");

		this.saveAs.addActionListener(new SaveAsListener(this.m, this));

		this.file.add(saveAs);


		this.exit.setMnemonic('E');

		this.exit.setText("Exit");

		this.exit.addActionListener(new ExitListener(this.m, this));

		this.file.add(exit);


		this.menuBar.add(file);


		this.edit.setText("Edit");


		this.copy.setMnemonic('C');

		this.copy.setText("Copy");

		this.edit.add(copy);


		this.cut.setMnemonic('U');

		this.cut.setText("Cut");

		this.edit.add(cut);


		this.paste.setMnemonic('P');

		this.paste.setText("Paste");

		this.edit.add(paste);


		this.menuBar.add(edit);


		this.setJMenuBar(menuBar);


		int width = 350;

		int height = 500;

		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

		int x = (screen.width - width) / 2;

		int y = (screen.height - height) / 2;

		setBounds(x, y, width, height);


		this.pack();

		this.setVisible(true);

	}


	public void setTextArea(JTextArea textArea) 

	{

		this.textArea = textArea;

	}


	public JTextArea getTextArea() 

	{

		return textArea;

	}


	public static void main(String args[]) 

	{

		new View();

	}

}

CONTROLLER(S)
package controller;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import model.Model;

import view.View;


public class ExitListener extends SaveAsListener implements ActionListener 

{


	public ExitListener(Model model, View view) 

	{

		super(model, view);

	}


	public void actionPerformed(ActionEvent e) 

	{

		super.actionPerformed(e);

		System.exit(0);

	}

}


package controller;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import model.Model;

import view.View;


public class NewFileListener implements ActionListener 

{

	private View v;

	private Model m;

	

	public NewFileListener(Model model, View view) 

	{

		this.v = view;

		this.m = model;

	}


	public void actionPerformed(ActionEvent e) 

	{

		this.m.setCurrentFile(null);

		this.m.setFileText("");

		this.v.getTextArea().setText(this.m.getFileText());

	}

}


package controller;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.JFileChooser;

import view.View;

import model.Model;


public class OpenListener implements ActionListener 

{

	private Model m;

	private View v;


	public OpenListener(Model m, View v) 

	{

		this.m = m;

		this.v = v;

	}


	public void actionPerformed(ActionEvent e) 

	{

		JFileChooser fc;

		fc = new JFileChooser();

		fc.setDialogTitle("Open File");


		int value = fc.showOpenDialog(null);

		if(value == JFileChooser.APPROVE_OPTION)

		{

			File f = fc.getSelectedFile();

			this.m.open(f);

			String textFromFile = this.m.getFileText();

			this.v.getTextArea().setText(textFromFile);

		}

	}

}


package controller;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

import model.Model;

import view.View;


public class SaveAsListener implements ActionListener 

{

	protected View v;

	protected Model m;



	public SaveAsListener(Model model, View view) 

	{

		v = view;

		m = model;

	}


	public void actionPerformed(ActionEvent e) 

	{

		String textArea = this.v.getTextArea().getText();

		

		if (textArea.equals(m.getFileText()))

				{

					JOptionPane.showMessageDialog(null, "The current file is not altered");

				}

		

		else if (!textArea.equals("") && !textArea.equals(m.getFileText()) && JOptionPane.showConfirmDialog(null,"Do you want to save the file?") == 0)

		{

			JFileChooser fc = new JFileChooser();

			int value = fc.showSaveDialog(null);

			if(value == JFileChooser.APPROVE_OPTION) 

			{

				this.m.setCurrentFile(fc.getSelectedFile().getAbsoluteFile());

				this.m.setFileText(textArea);

				this.m.save();

			}

		}	

	}

}



package controller;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import view.View;

import model.Model;


public class SaveListener extends SaveAsListener implements ActionListener 

{

	

	public SaveListener(Model model, View view) 

	{

		super(model, view);

	}


	public void actionPerformed(ActionEvent e) 

	{

		String textArea = this.v.getTextArea().getText();

		

		if (textArea.equals(m.getFileText()))

		{

			JOptionPane.showMessageDialog(null, "The current file is not altered");

		}

		else if (this.m.getCurrentFile() == null)

		{

			super.actionPerformed(e);

		}

		else 

		{

			String text = this.v.getTextArea().getText();

			this.m.setFileText(text);

			this.m.save();

		}

	}

}

So how am i supposed to think? Do i create a new factoryclass that gets the currentFile and textFileText and based upon what action is performed on the menu calls that method, for example saveFile()

I mean basically u need the things you can have done (my listeners), like save & open file to be subclasses of Model then?

some input would be awesome..!!

/Manne

Edited by Darkone85, 12 May 2011 - 10:24 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users