@@ -8,6 +8,7 @@ package kontakte.controller; | |||
import java.awt.Component; | |||
import java.util.HashMap; | |||
import java.util.Stack; | |||
/** | |||
* | |||
@@ -16,10 +17,12 @@ import java.util.HashMap; | |||
public class CommandInvoker | |||
{ | |||
private HashMap<Component, CommandInterface> commands; | |||
private Stack<CommandInterface> undoStack; | |||
public CommandInvoker() | |||
{ | |||
commands = new HashMap<>(); | |||
undoStack = new Stack<CommandInterface>(); | |||
} | |||
public void addCommand(Component key, CommandInterface value) | |||
@@ -30,5 +33,14 @@ public class CommandInvoker | |||
public void executeCommand(Component key) | |||
{ | |||
commands.get(key).execute(); | |||
undoStack.add(commands.get(key)); | |||
} | |||
public void undoCommand() | |||
{ | |||
if (!undoStack.empty()) | |||
{ | |||
undoStack.pop().undo(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,40 @@ | |||
/* | |||
* To change this license header, choose License Headers in Project Properties. | |||
* To change this template file, choose Tools | Templates | |||
* and open the template in the editor. | |||
*/ | |||
package kontakte.controller; | |||
import kontakte.model.Model; | |||
import kontakte.view.View; | |||
/** | |||
* | |||
* @author nobody | |||
*/ | |||
public class CommandNewKontaktCancel implements CommandInterface | |||
{ | |||
private View view; | |||
private Model model; | |||
public CommandNewKontaktCancel(View view, Model model) | |||
{ | |||
this.view = view; | |||
this.model = model; | |||
} | |||
@Override | |||
public void execute() | |||
{ | |||
view.getTextName().setText(""); | |||
view.getTextCity().setText(""); | |||
view.getTextStreet().setText(""); | |||
view.getAddEntryFrame().dispose(); | |||
} | |||
@Override | |||
public void undo() | |||
{ | |||
} | |||
} |
@@ -6,6 +6,8 @@ | |||
package kontakte.controller; | |||
import java.util.Map; | |||
import java.util.Stack; | |||
import kontakte.model.Kontakt; | |||
import kontakte.model.Model; | |||
import kontakte.view.View; | |||
@@ -18,11 +20,14 @@ public class CommandNewKontaktOk implements CommandInterface | |||
{ | |||
private View view; | |||
private Model model; | |||
private Stack<Kontakt> undoStack; | |||
public CommandNewKontaktOk(View view, Model model) | |||
{ | |||
this.view = view; | |||
this.model = model; | |||
view.getAddEntryFrame().pack(); | |||
undoStack = new Stack<Kontakt>(); | |||
} | |||
@Override | |||
@@ -31,13 +36,34 @@ public class CommandNewKontaktOk implements CommandInterface | |||
String name = view.getTextName().getText(); | |||
String wohnort = view.getTextCity().getText(); | |||
String strasse = view.getTextStreet().getText(); | |||
view.getTextName().setText(""); | |||
view.getTextCity().setText(""); | |||
view.getTextStreet().setText(""); | |||
Kontakt neuKon = new Kontakt(name, wohnort, strasse); | |||
model.addKontakt(neuKon); | |||
view.getAddEntryFrame().setVisible(false); | |||
undoStack.add(neuKon); | |||
redrawTextbox(); | |||
view.getAddEntryFrame().dispose(); | |||
} | |||
@Override | |||
public void undo() | |||
{ | |||
if(!undoStack.empty()) | |||
{ | |||
model.deleteKontakt(undoStack.pop()); | |||
redrawTextbox(); | |||
} | |||
} | |||
private void redrawTextbox() | |||
{ | |||
view.getTxtKontakte().setText(""); | |||
for (Map.Entry<String, Kontakt> k: model.kontaktliste.entrySet()) | |||
{ | |||
view.getTxtKontakte().append(k.getValue().name + " "); | |||
view.getTxtKontakte().append(k.getValue().wohnort + " "); | |||
view.getTxtKontakte().append(k.getValue().strasse + "\n"); | |||
} | |||
} | |||
} |
@@ -36,6 +36,9 @@ public class Controller implements ActionListener | |||
view.getBtnSave().addActionListener(this); | |||
view.getMenuSave().addActionListener(this); | |||
view.getBtnNewEntry().addActionListener(this); | |||
view.getBtnOk().addActionListener(this); | |||
view.getBtnCancel().addActionListener(this); | |||
view.getMenuUndo().addActionListener(this); | |||
} | |||
public void registerCommands() | |||
@@ -47,13 +50,22 @@ public class Controller implements ActionListener | |||
invoker.addCommand(view.getBtnSave(), cmdSave); | |||
invoker.addCommand(view.getMenuSave(), cmdSave); | |||
invoker.addCommand(view.getBtnNewEntry(), new CommandNewKontaktVisible(view, model)); | |||
invoker.addCommand(view.getBtnOk(),new CommandNewKontaktOk(view,model)); | |||
invoker.addCommand(view.getBtnCancel(),new CommandNewKontaktCancel(view,model)); | |||
} | |||
@Override | |||
public void actionPerformed(ActionEvent e) | |||
{ | |||
Component key = (Component)e.getSource(); | |||
invoker.executeCommand(key); | |||
if(key != view.getMenuUndo()) | |||
{ | |||
invoker.executeCommand(key); | |||
} | |||
else | |||
{ | |||
invoker.undoCommand(); | |||
} | |||
} | |||
} |
@@ -168,6 +168,16 @@ | |||
<Property name="text" type="java.lang.String" value="Delete Entry"/> | |||
</Properties> | |||
</MenuItem> | |||
<MenuItem class="javax.swing.JMenuItem" name="menuUndo"> | |||
<Properties> | |||
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor"> | |||
<KeyStroke key="Ctrl+Z"/> | |||
</Property> | |||
<Property name="mnemonic" type="int" value="85"/> | |||
<Property name="text" type="java.lang.String" value="Undo"/> | |||
<Property name="toolTipText" type="java.lang.String" value=""/> | |||
</Properties> | |||
</MenuItem> | |||
</SubComponents> | |||
</Menu> | |||
</SubComponents> | |||
@@ -264,32 +274,6 @@ | |||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> | |||
<SubComponents> | |||
<Container class="javax.swing.JScrollPane" name="jScrollPane1"> | |||
<AuxValues> | |||
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> | |||
</AuxValues> | |||
<Constraints> | |||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | |||
<BorderConstraints direction="Center"/> | |||
</Constraint> | |||
</Constraints> | |||
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> | |||
<SubComponents> | |||
<Component class="javax.swing.JTable" name="tableContacts"> | |||
<Properties> | |||
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> | |||
<Table columnCount="4" rowCount="4"> | |||
<Column editable="true" title="Title 1" type="java.lang.Object"/> | |||
<Column editable="true" title="Title 2" type="java.lang.Object"/> | |||
<Column editable="true" title="Title 3" type="java.lang.Object"/> | |||
<Column editable="true" title="Title 4" type="java.lang.Object"/> | |||
</Table> | |||
</Property> | |||
</Properties> | |||
</Component> | |||
</SubComponents> | |||
</Container> | |||
<Container class="javax.swing.JLayeredPane" name="jLayeredPane2"> | |||
<Constraints> | |||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | |||
@@ -342,6 +326,26 @@ | |||
</Component> | |||
</SubComponents> | |||
</Container> | |||
<Container class="javax.swing.JScrollPane" name="jScrollPane2"> | |||
<AuxValues> | |||
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> | |||
</AuxValues> | |||
<Constraints> | |||
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> | |||
<BorderConstraints direction="Center"/> | |||
</Constraint> | |||
</Constraints> | |||
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> | |||
<SubComponents> | |||
<Component class="javax.swing.JTextArea" name="txtKontakte"> | |||
<Properties> | |||
<Property name="columns" type="int" value="20"/> | |||
<Property name="rows" type="int" value="5"/> | |||
</Properties> | |||
</Component> | |||
</SubComponents> | |||
</Container> | |||
</SubComponents> | |||
</Container> | |||
</SubComponents> |
@@ -131,14 +131,6 @@ public class View extends javax.swing.JFrame | |||
return menuSave; | |||
} | |||
/** | |||
* @return the tableContacts | |||
*/ | |||
public javax.swing.JTable getTableContacts() | |||
{ | |||
return tableContacts; | |||
} | |||
/** | |||
* @return the textCity | |||
*/ | |||
@@ -226,14 +218,14 @@ public class View extends javax.swing.JFrame | |||
btnClose = new javax.swing.JButton(); | |||
textFileAdress = new javax.swing.JTextField(); | |||
jLayeredPane1 = new javax.swing.JLayeredPane(); | |||
jScrollPane1 = new javax.swing.JScrollPane(); | |||
tableContacts = new javax.swing.JTable(); | |||
jLayeredPane2 = new javax.swing.JLayeredPane(); | |||
btnNewEntry = new javax.swing.JButton(); | |||
jButton2 = new javax.swing.JButton(); | |||
jLayeredPane3 = new javax.swing.JLayeredPane(); | |||
textFindTip = new javax.swing.JTextField(); | |||
textFind = new javax.swing.JTextField(); | |||
jScrollPane2 = new javax.swing.JScrollPane(); | |||
txtKontakte = new javax.swing.JTextArea(); | |||
jMenuBar1 = new javax.swing.JMenuBar(); | |||
menuFile = new javax.swing.JMenu(); | |||
menuOpen = new javax.swing.JMenuItem(); | |||
@@ -242,6 +234,7 @@ public class View extends javax.swing.JFrame | |||
menuEdit = new javax.swing.JMenu(); | |||
menuAddEntry = new javax.swing.JMenuItem(); | |||
menuDeleteEntry = new javax.swing.JMenuItem(); | |||
menuUndo = new javax.swing.JMenuItem(); | |||
addEntryFrame.getContentPane().setLayout(new javax.swing.BoxLayout(addEntryFrame.getContentPane(), javax.swing.BoxLayout.Y_AXIS)); | |||
@@ -326,23 +319,6 @@ public class View extends javax.swing.JFrame | |||
jLayeredPane1.setLayout(new java.awt.BorderLayout()); | |||
tableContacts.setModel(new javax.swing.table.DefaultTableModel( | |||
new Object [][] | |||
{ | |||
{null, null, null, null}, | |||
{null, null, null, null}, | |||
{null, null, null, null}, | |||
{null, null, null, null} | |||
}, | |||
new String [] | |||
{ | |||
"Title 1", "Title 2", "Title 3", "Title 4" | |||
} | |||
)); | |||
jScrollPane1.setViewportView(tableContacts); | |||
jLayeredPane1.add(jScrollPane1, java.awt.BorderLayout.CENTER); | |||
jLayeredPane2.setLayout(new java.awt.FlowLayout()); | |||
btnNewEntry.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kontakte/view/img/Add24.gif"))); // NOI18N | |||
@@ -366,6 +342,12 @@ public class View extends javax.swing.JFrame | |||
jLayeredPane1.add(jLayeredPane3, java.awt.BorderLayout.PAGE_START); | |||
txtKontakte.setColumns(20); | |||
txtKontakte.setRows(5); | |||
jScrollPane2.setViewportView(txtKontakte); | |||
jLayeredPane1.add(jScrollPane2, java.awt.BorderLayout.CENTER); | |||
getContentPane().add(jLayeredPane1, java.awt.BorderLayout.CENTER); | |||
menuFile.setMnemonic('F'); | |||
@@ -407,6 +389,12 @@ public class View extends javax.swing.JFrame | |||
menuDeleteEntry.setText("Delete Entry"); | |||
menuEdit.add(menuDeleteEntry); | |||
menuUndo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK)); | |||
menuUndo.setMnemonic('U'); | |||
menuUndo.setText("Undo"); | |||
menuUndo.setToolTipText(""); | |||
menuEdit.add(menuUndo); | |||
jMenuBar1.add(menuEdit); | |||
setJMenuBar(jMenuBar1); | |||
@@ -483,7 +471,7 @@ public class View extends javax.swing.JFrame | |||
private javax.swing.JLayeredPane jLayeredPane7; | |||
private javax.swing.JLayeredPane jLayeredPane8; | |||
private javax.swing.JMenuBar jMenuBar1; | |||
private javax.swing.JScrollPane jScrollPane1; | |||
private javax.swing.JScrollPane jScrollPane2; | |||
private javax.swing.JTextField jTextField1; | |||
private javax.swing.JTextField jTextField3; | |||
private javax.swing.JTextField jTextField5; | |||
@@ -495,12 +483,29 @@ public class View extends javax.swing.JFrame | |||
private javax.swing.JMenu menuFile; | |||
private javax.swing.JMenuItem menuOpen; | |||
private javax.swing.JMenuItem menuSave; | |||
private javax.swing.JTable tableContacts; | |||
private javax.swing.JMenuItem menuUndo; | |||
private javax.swing.JTextField textCity; | |||
private javax.swing.JTextField textFileAdress; | |||
private javax.swing.JTextField textFind; | |||
private javax.swing.JTextField textFindTip; | |||
private javax.swing.JTextField textName; | |||
private javax.swing.JTextField textStreet; | |||
private javax.swing.JTextArea txtKontakte; | |||
// End of variables declaration//GEN-END:variables | |||
/** | |||
* @return the txtKontakte | |||
*/ | |||
public javax.swing.JTextArea getTxtKontakte() | |||
{ | |||
return txtKontakte; | |||
} | |||
/** | |||
* @return the menuUndo | |||
*/ | |||
public javax.swing.JMenuItem getMenuUndo() | |||
{ | |||
return menuUndo; | |||
} | |||
} |