Ich hatte mir einen kleinen texteditor, jedoch ohne berücksichtigung von mvc geschrieben. ich wollte dies nun nachträglich einbauen, da ich das theoretische prinzip nun verstanden habe. jedoch weiß ich nicht genau, wie ich es praktisch umsetzen soll. bei meinem texteditor habe ich den text nun mal als model genommen. jedoch funtkioniert das ganze nicht. vieleicht kann mir einer mal meinen fehler zeigen und lösungsvorschläge geben. danke
sourcecode:
model:
view:
+ TxtFileFilter.java + LineNumber.java
controller:
sourcecode:
model:
Code:
package jpad.model;
public class Text
{
private String text = "";
/**
* @return Returns the text.
*/
public String getText()
{
return text;
}
/**
* @param text The text to set.
*/
public void setText(String text)
{
this.text = text;
}
}
view:
Code:
package jpad.view;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import jpad.controll.*;
import jpad.model.*;
public class MainFrame extends JFrame implements Observer
{
private Action newAct;
private Action openAct;
private Action saveAct;
private Action saveAsAct;
private Action quitAct;
private Action cutAct;
private Action copyAct;
private Action pasteAct;
private Action selectAllAct;
private Action aboutAct;
private Controller controller;
private File file;
private JLabel statusLabel;
private JMenu fileMenu;
private JMenu editMenu;
private JMenu helpMenu;
private JMenuBar menuBar;
private JScrollPane scrollPane;
private JTextArea textArea;
public MainFrame(Controller controller)
{
setTitle("Unbenannt - jPad-Lite");
setSize(460, 360);
setIconImage(new ImageIcon(getClass().getResource("images/txt.png")).getImage());
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
quit();
}
});
createActions();
setJMenuBar(createMenuBar());
textArea = new JTextArea();
//textArea.setLineWrap(true);
scrollPane = new JScrollPane(textArea);
scrollPane.setRowHeaderView(new LineNumber(textArea));
statusLabel = new JLabel();
statusLabel.setText("Bereit");
getContentPane().add(createToolBar(), BorderLayout.NORTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(statusLabel, BorderLayout.SOUTH);
this.controller = controller;
file = null;
setVisible(true);
}
private void createActions()
{
newAct = new AbstractAction("Neu", new ImageIcon(getClass().getResource("images/new.png")))
{
public void actionPerformed(ActionEvent e)
{
newFile();
}
};
newAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_N));
newAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
newAct.putValue(Action.SHORT_DESCRIPTION, "Neu");
openAct = new AbstractAction("Öffnen", new ImageIcon(getClass().getResource("images/open.png")))
{
public void actionPerformed(ActionEvent e)
{
open();
}
};
openAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_F));
openAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
openAct.putValue(Action.SHORT_DESCRIPTION, "Öffnen");
saveAct = new AbstractAction("Speichern", new ImageIcon(getClass().getResource("images/save.png")))
{
public void actionPerformed(ActionEvent e)
{
save();
}
};
saveAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_P));
saveAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
saveAct.putValue(Action.SHORT_DESCRIPTION, "Speichern");
saveAsAct = new AbstractAction("Speichern unter ...", new ImageIcon(getClass().getResource("images/saveas.png")))
{
public void actionPerformed(ActionEvent e)
{
saveAs();
}
};
saveAsAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_U));
saveAsAct.putValue(Action.SHORT_DESCRIPTION, "Speichern unter");
quitAct = new AbstractAction("Beenden", new ImageIcon(getClass().getResource("images/exit.png")))
{
public void actionPerformed(ActionEvent e)
{
quit();
}
};
quitAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_E));
quitAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
quitAct.putValue(Action.SHORT_DESCRIPTION, "Beenden");
cutAct = new AbstractAction("Ausschneiden", new ImageIcon(getClass().getResource("images/cut.png")))
{
public void actionPerformed(ActionEvent e)
{
textArea.cut();
}
};
cutAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
cutAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
cutAct.putValue(Action.SHORT_DESCRIPTION, "Ausschneiden");
copyAct = new AbstractAction("Kopieren", new ImageIcon(getClass().getResource("images/copy.png")))
{
public void actionPerformed(ActionEvent e)
{
textArea.copy();
}
};
copyAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_K));
copyAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
copyAct.putValue(Action.SHORT_DESCRIPTION, "Kopieren");
pasteAct = new AbstractAction("Einfügen", new ImageIcon(getClass().getResource("images/paste.png")))
{
public void actionPerformed(ActionEvent e)
{
textArea.paste();
}
};
pasteAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_F));
pasteAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
pasteAct.putValue(Action.SHORT_DESCRIPTION, "Einfügen");
selectAllAct = new AbstractAction("Alles auswählen")
{
public void actionPerformed(ActionEvent e)
{
textArea.selectAll();
}
};
selectAllAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_L));
selectAllAct.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
selectAllAct.putValue(Action.SHORT_DESCRIPTION, "Alles auswählen");
aboutAct = new AbstractAction("Über", new ImageIcon(getClass().getResource("images/about.png")))
{
public void actionPerformed(ActionEvent e)
{
about();
}
};
aboutAct.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_B));
aboutAct.putValue(Action.SHORT_DESCRIPTION, "Über");
}
private JMenuBar createMenuBar()
{
menuBar = new JMenuBar();
// file-menu
fileMenu = new JMenu("Datei");
fileMenu.setMnemonic(KeyEvent.VK_D);
fileMenu.add(new JMenuItem(newAct));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem(openAct));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem(saveAct));
fileMenu.add(new JMenuItem(saveAsAct));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem(quitAct));
menuBar.add(fileMenu);
// edit-menu
editMenu = new JMenu("Bearbeiten");
editMenu.setMnemonic(KeyEvent.VK_B);
editMenu.add(new JMenuItem(cutAct));
editMenu.add(new JMenuItem(copyAct));
editMenu.add(new JMenuItem(pasteAct));
editMenu.addSeparator();
editMenu.add(new JMenuItem(selectAllAct));
menuBar.add(editMenu);
// help-menu
helpMenu = new JMenu("Hilfe");
helpMenu.setMnemonic(KeyEvent.VK_H);
helpMenu.add(new JMenuItem(aboutAct));
menuBar.add(helpMenu);
return menuBar;
}
private JToolBar createToolBar()
{
JToolBar toolBar = new JToolBar("Tool bar");
JButton newButton = new JButton();
newButton.setAction(newAct);
newButton.setText("");
toolBar.add(newButton);
JButton openButton = new JButton();
openButton.setAction(openAct);
openButton.setText("");
toolBar.add(openButton);
toolBar.addSeparator();
JButton saveButton = new JButton();
saveButton.setAction(saveAct);
saveButton.setText("");
toolBar.add(saveButton);
JButton saveAsButton = new JButton();
saveAsButton.setAction(saveAsAct);
saveAsButton.setText("");
toolBar.add(saveAsButton);
toolBar.addSeparator();
JButton cutButton = new JButton();
cutButton.setAction(cutAct);
cutButton.setText("");
toolBar.add(cutButton);
JButton copyButton = new JButton();
copyButton.setAction(copyAct);
copyButton.setText("");
toolBar.add(copyButton);
JButton pasteButton = new JButton();
pasteButton.setAction(pasteAct);
pasteButton.setText("");
toolBar.add(pasteButton);
return toolBar;
}
private void newFile()
{
file = null;
controller.newFile();
statusLabel.setText("Bereit");
setTitle("Unbenannt - jPad-Lite");
}
private void open()
{
JFileChooser fileChooser = new JFileChooser(".");
// Titel des Dialogs
fileChooser.setDialogTitle("Öffnen");
fileChooser.addChoosableFileFilter(new TxtFileFilter());
fileChooser.setMultiSelectionEnabled(false);
// Wenn OK gedrueckt wurde, fuehre aus...
int ret = fileChooser.showOpenDialog(this);
if(ret == JFileChooser.APPROVE_OPTION)
{
// der OK-Button des FileChooser-Dialogs wurde gedrueckt
file = fileChooser.getSelectedFile();
controller.loadFile(file);
statusLabel.setText(file.getName() + " erfolgreich geöffnet");
setTitle(file.getName() + " - jPad-Lite");
}
else
{
statusLabel.setText("Öffnen abgebrochen");
}
}
private void save()
{
if(file != null)
{
controller.saveFile(file);
statusLabel.setText(file.getName() + " erfolgreich gespeichert");
}
else
{
saveAs();
}
}
private void saveAs()
{
JFileChooser fileChooser = new JFileChooser(".");
// Titel des Dialogs
fileChooser.setDialogTitle("Speichern unter ...");
fileChooser.addChoosableFileFilter(new TxtFileFilter());
fileChooser.setMultiSelectionEnabled(false);
// Wenn OK gedrueckt wurde, fuehre aus...
int ret = fileChooser.showSaveDialog(this);
if(ret == JFileChooser.APPROVE_OPTION)
{
// der OK-Button des FileChooser-Dialogs wurde gedrueckt
file = fileChooser.getSelectedFile();
controller.saveFile(file);
statusLabel.setText(file.getName() + " erfolgreich gespeichert");
}
else
{
statusLabel.setText("Speichern abgebrochen");
}
}
private void quit()
{
if(JOptionPane.showConfirmDialog(this, "Wollen Sie das Programm wirklich beenden?", "Beenden",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
== JOptionPane.YES_OPTION)
{
setVisible(false);
dispose();
System.exit(0);
}
}
private void about()
{
JOptionPane.showMessageDialog(this, "jPad-Lite 0.1");
}
public void update(Observable arg0, Object arg1)
{
Text text = (Text)arg1;
textArea.setText(text.getText());
}
}
+ TxtFileFilter.java + LineNumber.java
controller:
Code:
package jpad.controll;
import java.io.*;
import java.util.Observable;
import javax.swing.UIManager;
import jpad.model.*;
import jpad.view.*;
public class Controller extends Observable
{
private Text text;
public Controller()
{
MainFrame frame = new MainFrame(this);
addObserver(frame);
text = new Text();
}
public void newFile()
{
text.setText("");
setChanged();
notifyObservers(text);
}
public void loadFile(File file)
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
String string = "";
while((line = reader.readLine()) != null)
{
string.concat(line + System.getProperty("line.separator"));
}
reader.close();
string = string.substring(0, string.lastIndexOf(System.getProperty("line.separator")));
text.setText(string);
setChanged();
notifyObservers(text);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void saveFile(File file)
{
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String string = text.getText();
writer.write(string);
writer.close();
text.setText(string);
setChanged();
notifyObservers(text);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
new Controller();
}
}