diff --git a/.gitignore b/.gitignore index f53fb92..e35ea9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /nbproject/private/ +/build/ diff --git a/src/mvcgrafik/Start.java b/src/mvcgrafik/Start.java index c44625e..f12e437 100644 --- a/src/mvcgrafik/Start.java +++ b/src/mvcgrafik/Start.java @@ -7,6 +7,7 @@ package mvcgrafik; import javax.swing.JOptionPane; import javax.swing.UIManager; +import mvcgrafik.controller.CommandController; import mvcgrafik.controller.GrafikController; import mvcgrafik.model.GrafikModel; import mvcgrafik.view.GrafikFrame; @@ -25,7 +26,10 @@ public class Start GrafikView view = frm.getgZeichenflaeche(); view.setModel(model); GrafikController controller = new GrafikController(view, model); + CommandController controller_commands = new CommandController(frm, model); controller.registerEvents(); + controller_commands.registerEvents(); + controller_commands.registerCommands(); frm.setVisible(true); } diff --git a/src/mvcgrafik/controller/CommandController.java b/src/mvcgrafik/controller/CommandController.java new file mode 100644 index 0000000..2f06b4a --- /dev/null +++ b/src/mvcgrafik/controller/CommandController.java @@ -0,0 +1,58 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package mvcgrafik.controller; + +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import mvcgrafik.controller.commands.CommandInvoker; +import mvcgrafik.controller.commands.OpenCommand; +import mvcgrafik.controller.commands.SaveCommand; +import mvcgrafik.model.GrafikModel; +import mvcgrafik.view.GrafikFrame; + +/** + * + * @author ahren + */ +public class CommandController implements ActionListener +{ + private GrafikFrame view; + private GrafikModel model; + private CommandInvoker invoker; + + public CommandController(GrafikFrame view, GrafikModel model) + { + this.view = view; + this.model = model; + this.invoker = new CommandInvoker(); + } + + public void registerEvents() + { + view.getBtnFileOpen().addActionListener(this); + view.getBtnFileSave().addActionListener(this); + } + + public void registerCommands() + { + invoker.addCommand(view.getBtnFileOpen(), new OpenCommand(view, model)); + invoker.addCommand(view.getBtnFileSave(), new SaveCommand(view,model)); + } + + /** + * Ausführen des jeweiligen Kommandos + * @param e Referenz auf das Event + */ + @Override + public void actionPerformed(ActionEvent e) { + Component key = (Component)e.getSource(); + invoker.executeCommand(key); + if(key == view.getBtnFileOpen()){ + invoker.deleteStack(); + } + } +} diff --git a/src/mvcgrafik/controller/commands/CommandInterface.java b/src/mvcgrafik/controller/commands/CommandInterface.java new file mode 100644 index 0000000..c021d6d --- /dev/null +++ b/src/mvcgrafik/controller/commands/CommandInterface.java @@ -0,0 +1,17 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template + */ +package mvcgrafik.controller.commands; + +/** + * + * @author ahren + */ +public interface CommandInterface +{ + public void execute(); + public void undo(); + public void redo(); + public boolean isUndoable(); +} diff --git a/src/mvcgrafik/controller/commands/CommandInvoker.java b/src/mvcgrafik/controller/commands/CommandInvoker.java new file mode 100644 index 0000000..e572c50 --- /dev/null +++ b/src/mvcgrafik/controller/commands/CommandInvoker.java @@ -0,0 +1,73 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package mvcgrafik.controller.commands; + +import java.awt.Component; +import java.util.HashMap; +import java.util.Stack; + +/** + * Dient zur Realisierung des Polymorphismus + * für das COmmand-Design-Pattern + * Die Commands werden in einer HashMap gespeichert + * @author ahren + * @see CommandInterface + */ +public class CommandInvoker { + + private HashMap commands; + private Stack undoStack; + + public CommandInvoker(){ + commands = new HashMap<>(); + undoStack = new Stack(); + } + + /** + * Fügt ein Kommando zur Kommando-"Datenbank" = HashMap hinzu + * @param key Quelle des Events + * @param value Referenz auf das zugehörige Kommando-Objekt + */ + public void addCommand(Component key, CommandInterface value){ + commands.put(key, value); + } + + /** + * Führt Kommando der Eventquelle "key" aus und legt die Referenz + * des Kommando in den Undo-Stack + * @param key Referenz auf die Eventquelle + */ + public void executeCommand(Component key){ + CommandInterface command = commands.get(key); + command.execute(); + if (command.isUndoable()) + { + undoStack.push(command); + } + } + /** + * Falls der Stack Elemente enthält, wird das oberste Element geholt + * und die Methode "undo" des Commands aufgerufen + */ + public void undoCommand() + { + if (!undoStack.isEmpty()) + { + undoStack.pop().undo(); + } + } + + /** + * Löscht bei Öffnen einer neuen Datei den Stack + */ + public void deleteStack() + { + while(!undoStack.isEmpty()) + undoStack.pop(); + } + +} + diff --git a/src/mvcgrafik/controller/commands/OpenCommand.java b/src/mvcgrafik/controller/commands/OpenCommand.java new file mode 100644 index 0000000..c81955c --- /dev/null +++ b/src/mvcgrafik/controller/commands/OpenCommand.java @@ -0,0 +1,78 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package mvcgrafik.controller.commands; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JFileChooser; +import mvcgrafik.model.GrafikModel; +import mvcgrafik.view.GrafikFrame; + + +/** + * + * @author ahren + */ +public class OpenCommand implements CommandInterface +{ + private GrafikFrame view; + private GrafikModel model; + + public OpenCommand(GrafikFrame view, GrafikModel model) + { + this.view = view; + this.model = model; + } + + @Override + public void execute() { + JFileChooser fc = view.getFcFileChooser(); + String lastDirectory = model.getPref(); + if (lastDirectory != null) { + fc.setCurrentDirectory(new File(lastDirectory)); + } + int choice = fc.showOpenDialog(view); + if (choice == JFileChooser.APPROVE_OPTION) + { + File selectedFile = fc.getSelectedFile(); + String filename = selectedFile.getAbsolutePath(); + model.putPref("lastDirectory", selectedFile.getParent()); + + try + { + model.lesePunkte(filename); + } + catch (FileNotFoundException ex) + { + Logger.getLogger(OpenCommand.class.getName()).log(Level.SEVERE, null, ex); + } + catch (ClassNotFoundException | IOException ex) + { + Logger.getLogger(OpenCommand.class.getName()).log(Level.SEVERE, null, ex); + } + model.deleteArrays(); + } + } + + @Override + public void undo() { + throw new UnsupportedOperationException("Not undoable."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void redo() { + throw new UnsupportedOperationException("Not redoable."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public boolean isUndoable() + { + return false; + } +} diff --git a/src/mvcgrafik/controller/commands/SaveCommand.java b/src/mvcgrafik/controller/commands/SaveCommand.java new file mode 100644 index 0000000..397591e --- /dev/null +++ b/src/mvcgrafik/controller/commands/SaveCommand.java @@ -0,0 +1,80 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package mvcgrafik.controller.commands; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JFileChooser; +import mvcgrafik.model.GrafikModel; +import mvcgrafik.view.GrafikFrame; + +/** + * + * @author ahren + */ +public class SaveCommand implements CommandInterface +{ + private GrafikFrame view; + private GrafikModel model; + + public SaveCommand(GrafikFrame view, GrafikModel model){ + this.view = view; + this.model = model; + } + + /** + * Aufrufen des File Choosers in zuletzt verwendeten Ordner, versuchen die + * Tabellendatei zu speichern + */ + @Override + public void execute() { + JFileChooser fc = view.getFcFileChooser(); + String lastDirectory = model.getPref(); + if (lastDirectory != null) { + fc.setCurrentDirectory(new File(lastDirectory)); + } + int choice = fc.showSaveDialog(view); + if (choice == JFileChooser.APPROVE_OPTION) + { + File selectedFile = fc.getSelectedFile(); + String filename = selectedFile.getAbsolutePath(); + model.putPref("lastDirectory", selectedFile.getParent()); + + try + { + model.speicherePunkte(filename); + } + catch (FileNotFoundException ex) + { + Logger.getLogger(SaveCommand.class.getName()).log(Level.SEVERE, null, ex); + } + catch (IOException ex) + { + Logger.getLogger(SaveCommand.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + + @Override + public void undo() { + throw new UnsupportedOperationException("Not undoable."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void redo() { + throw new UnsupportedOperationException("Not redoable."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public boolean isUndoable() { + return false; + } + +} diff --git a/src/mvcgrafik/model/GrafikModel.java b/src/mvcgrafik/model/GrafikModel.java index 371807c..fcfd926 100644 --- a/src/mvcgrafik/model/GrafikModel.java +++ b/src/mvcgrafik/model/GrafikModel.java @@ -17,6 +17,7 @@ import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.prefs.Preferences; /** * @@ -26,6 +27,7 @@ public class GrafikModel { private ArrayList punkte; private ArrayList figuren; + private Preferences pref; public GrafikModel() { @@ -93,6 +95,30 @@ public class GrafikModel punkte.clear(); } - + /** + * Bestimmt die Adresse des zuletzt besuchten Ordners + * @return letzter Ordner + */ + public String getPref() + { + pref = Preferences.userNodeForPackage(getClass()); + return pref.get("lastDirectory", null); + } + + /** + * Setzt die Preferenz zu dem zuletzt besuchten Ordner + * @param lastDirectory Bezeichner "lastDirectory" + * @param lastAdress Adresse des zuletzt besuchten Ordners + */ + public void putPref(String lastDirectory, String lastAdress) + { + pref.put(lastDirectory, lastAdress); + } + + public void deleteArrays() + { + figuren.clear(); + punkte.clear(); + } } diff --git a/src/mvcgrafik/view/GrafikFrame.form b/src/mvcgrafik/view/GrafikFrame.form index 70c71e8..f2cd990 100644 --- a/src/mvcgrafik/view/GrafikFrame.form +++ b/src/mvcgrafik/view/GrafikFrame.form @@ -1,6 +1,10 @@
+ + + + @@ -35,5 +39,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mvcgrafik/view/GrafikFrame.java b/src/mvcgrafik/view/GrafikFrame.java index e0c31a1..ae256b9 100644 --- a/src/mvcgrafik/view/GrafikFrame.java +++ b/src/mvcgrafik/view/GrafikFrame.java @@ -10,6 +10,30 @@ package mvcgrafik.view; */ public class GrafikFrame extends javax.swing.JFrame { + + /** + * @return the FcFileChooser + */ + public javax.swing.JFileChooser getFcFileChooser() + { + return FcFileChooser; + } + + /** + * @return the btnFileOpen + */ + public javax.swing.JButton getBtnFileOpen() + { + return btnFileOpen; + } + + /** + * @return the btnFileSave + */ + public javax.swing.JButton getBtnFileSave() + { + return btnFileSave; + } /** * @return the gZeichenflaeche */ @@ -36,11 +60,32 @@ public class GrafikFrame extends javax.swing.JFrame private void initComponents() { + FcFileChooser = new javax.swing.JFileChooser(); gZeichenflaeche = new mvcgrafik.view.GrafikView(); + jToolBar1 = new javax.swing.JToolBar(); + btnFileOpen = new javax.swing.JButton(); + btnFileSave = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().add(gZeichenflaeche, java.awt.BorderLayout.CENTER); + jToolBar1.setRollover(true); + + btnFileOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mvcgrafik/view/Open24.gif"))); // NOI18N + btnFileOpen.setToolTipText("Open File"); + btnFileOpen.setFocusable(false); + btnFileOpen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); + btnFileOpen.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); + jToolBar1.add(btnFileOpen); + + btnFileSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mvcgrafik/view/Save24.gif"))); // NOI18N + btnFileSave.setToolTipText("Safe File"); + btnFileSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); + btnFileSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); + jToolBar1.add(btnFileSave); + + getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START); + setSize(new java.awt.Dimension(540, 412)); setLocationRelativeTo(null); }// //GEN-END:initComponents @@ -95,6 +140,10 @@ public class GrafikFrame extends javax.swing.JFrame } // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JFileChooser FcFileChooser; + private javax.swing.JButton btnFileOpen; + private javax.swing.JButton btnFileSave; private mvcgrafik.view.GrafikView gZeichenflaeche; + private javax.swing.JToolBar jToolBar1; // End of variables declaration//GEN-END:variables } diff --git a/src/mvcgrafik/view/Open24.gif b/src/mvcgrafik/view/Open24.gif new file mode 100644 index 0000000..2086bc2 Binary files /dev/null and b/src/mvcgrafik/view/Open24.gif differ diff --git a/src/mvcgrafik/view/Save24.gif b/src/mvcgrafik/view/Save24.gif new file mode 100644 index 0000000..bfa98a8 Binary files /dev/null and b/src/mvcgrafik/view/Save24.gif differ