Browse Source

Implement Undo

master
Markus Bauer 6 years ago
parent
commit
fb1460a3d7

+ 12
- 0
src/kontakte/controller/CommandInvoker.java View File



import java.awt.Component; import java.awt.Component;
import java.util.HashMap; import java.util.HashMap;
import java.util.Stack;


/** /**
* *
public class CommandInvoker public class CommandInvoker
{ {
private HashMap<Component, CommandInterface> commands; private HashMap<Component, CommandInterface> commands;
private Stack<CommandInterface> undoStack;
public CommandInvoker() public CommandInvoker()
{ {
commands = new HashMap<>(); commands = new HashMap<>();
undoStack = new Stack<CommandInterface>();
} }
public void addCommand(Component key, CommandInterface value) public void addCommand(Component key, CommandInterface value)
public void executeCommand(Component key) public void executeCommand(Component key)
{ {
commands.get(key).execute(); commands.get(key).execute();
undoStack.add(commands.get(key));
}
public void undoCommand()
{
if (!undoStack.empty())
{
undoStack.pop().undo();
}
} }
} }

+ 40
- 0
src/kontakte/controller/CommandNewKontaktCancel.java View File

/*
* 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()
{
}
}

+ 27
- 1
src/kontakte/controller/CommandNewKontaktOk.java View File



package kontakte.controller; package kontakte.controller;


import java.util.Map;
import java.util.Stack;
import kontakte.model.Kontakt; import kontakte.model.Kontakt;
import kontakte.model.Model; import kontakte.model.Model;
import kontakte.view.View; import kontakte.view.View;
{ {
private View view; private View view;
private Model model; private Model model;
private Stack<Kontakt> undoStack;
public CommandNewKontaktOk(View view, Model model) public CommandNewKontaktOk(View view, Model model)
{ {
this.view = view; this.view = view;
this.model = model; this.model = model;
view.getAddEntryFrame().pack();
undoStack = new Stack<Kontakt>();
} }


@Override @Override
String name = view.getTextName().getText(); String name = view.getTextName().getText();
String wohnort = view.getTextCity().getText(); String wohnort = view.getTextCity().getText();
String strasse = view.getTextStreet().getText(); String strasse = view.getTextStreet().getText();
view.getTextName().setText("");
view.getTextCity().setText("");
view.getTextStreet().setText("");
Kontakt neuKon = new Kontakt(name, wohnort, strasse); Kontakt neuKon = new Kontakt(name, wohnort, strasse);
model.addKontakt(neuKon); model.addKontakt(neuKon);
view.getAddEntryFrame().setVisible(false);
undoStack.add(neuKon);
redrawTextbox();
view.getAddEntryFrame().dispose();
} }


@Override @Override
public void undo() 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");
}
} }
} }

+ 13
- 1
src/kontakte/controller/Controller.java View File

view.getBtnSave().addActionListener(this); view.getBtnSave().addActionListener(this);
view.getMenuSave().addActionListener(this); view.getMenuSave().addActionListener(this);
view.getBtnNewEntry().addActionListener(this); view.getBtnNewEntry().addActionListener(this);
view.getBtnOk().addActionListener(this);
view.getBtnCancel().addActionListener(this);
view.getMenuUndo().addActionListener(this);
} }
public void registerCommands() public void registerCommands()
invoker.addCommand(view.getBtnSave(), cmdSave); invoker.addCommand(view.getBtnSave(), cmdSave);
invoker.addCommand(view.getMenuSave(), cmdSave); invoker.addCommand(view.getMenuSave(), cmdSave);
invoker.addCommand(view.getBtnNewEntry(), new CommandNewKontaktVisible(view, model)); 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 @Override
public void actionPerformed(ActionEvent e) public void actionPerformed(ActionEvent e)
{ {
Component key = (Component)e.getSource(); Component key = (Component)e.getSource();
invoker.executeCommand(key);
if(key != view.getMenuUndo())
{
invoker.executeCommand(key);
}
else
{
invoker.undoCommand();
}
} }
} }

+ 30
- 26
src/kontakte/view/View.form View File

<Property name="text" type="java.lang.String" value="Delete Entry"/> <Property name="text" type="java.lang.String" value="Delete Entry"/>
</Properties> </Properties>
</MenuItem> </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> </SubComponents>
</Menu> </Menu>
</SubComponents> </SubComponents>


<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
<SubComponents> <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"> <Container class="javax.swing.JLayeredPane" name="jLayeredPane2">
<Constraints> <Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
</Component> </Component>
</SubComponents> </SubComponents>
</Container> </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> </SubComponents>
</Container> </Container>
</SubComponents> </SubComponents>

+ 34
- 29
src/kontakte/view/View.java View File

return menuSave; return menuSave;
} }


/**
* @return the tableContacts
*/
public javax.swing.JTable getTableContacts()
{
return tableContacts;
}

/** /**
* @return the textCity * @return the textCity
*/ */
btnClose = new javax.swing.JButton(); btnClose = new javax.swing.JButton();
textFileAdress = new javax.swing.JTextField(); textFileAdress = new javax.swing.JTextField();
jLayeredPane1 = new javax.swing.JLayeredPane(); jLayeredPane1 = new javax.swing.JLayeredPane();
jScrollPane1 = new javax.swing.JScrollPane();
tableContacts = new javax.swing.JTable();
jLayeredPane2 = new javax.swing.JLayeredPane(); jLayeredPane2 = new javax.swing.JLayeredPane();
btnNewEntry = new javax.swing.JButton(); btnNewEntry = new javax.swing.JButton();
jButton2 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton();
jLayeredPane3 = new javax.swing.JLayeredPane(); jLayeredPane3 = new javax.swing.JLayeredPane();
textFindTip = new javax.swing.JTextField(); textFindTip = new javax.swing.JTextField();
textFind = new javax.swing.JTextField(); textFind = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
txtKontakte = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar(); jMenuBar1 = new javax.swing.JMenuBar();
menuFile = new javax.swing.JMenu(); menuFile = new javax.swing.JMenu();
menuOpen = new javax.swing.JMenuItem(); menuOpen = new javax.swing.JMenuItem();
menuEdit = new javax.swing.JMenu(); menuEdit = new javax.swing.JMenu();
menuAddEntry = new javax.swing.JMenuItem(); menuAddEntry = new javax.swing.JMenuItem();
menuDeleteEntry = 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)); addEntryFrame.getContentPane().setLayout(new javax.swing.BoxLayout(addEntryFrame.getContentPane(), javax.swing.BoxLayout.Y_AXIS));




jLayeredPane1.setLayout(new java.awt.BorderLayout()); 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()); jLayeredPane2.setLayout(new java.awt.FlowLayout());


btnNewEntry.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kontakte/view/img/Add24.gif"))); // NOI18N btnNewEntry.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kontakte/view/img/Add24.gif"))); // NOI18N


jLayeredPane1.add(jLayeredPane3, java.awt.BorderLayout.PAGE_START); 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); getContentPane().add(jLayeredPane1, java.awt.BorderLayout.CENTER);


menuFile.setMnemonic('F'); menuFile.setMnemonic('F');
menuDeleteEntry.setText("Delete Entry"); menuDeleteEntry.setText("Delete Entry");
menuEdit.add(menuDeleteEntry); 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); jMenuBar1.add(menuEdit);


setJMenuBar(jMenuBar1); setJMenuBar(jMenuBar1);
private javax.swing.JLayeredPane jLayeredPane7; private javax.swing.JLayeredPane jLayeredPane7;
private javax.swing.JLayeredPane jLayeredPane8; private javax.swing.JLayeredPane jLayeredPane8;
private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField5; private javax.swing.JTextField jTextField5;
private javax.swing.JMenu menuFile; private javax.swing.JMenu menuFile;
private javax.swing.JMenuItem menuOpen; private javax.swing.JMenuItem menuOpen;
private javax.swing.JMenuItem menuSave; private javax.swing.JMenuItem menuSave;
private javax.swing.JTable tableContacts;
private javax.swing.JMenuItem menuUndo;
private javax.swing.JTextField textCity; private javax.swing.JTextField textCity;
private javax.swing.JTextField textFileAdress; private javax.swing.JTextField textFileAdress;
private javax.swing.JTextField textFind; private javax.swing.JTextField textFind;
private javax.swing.JTextField textFindTip; private javax.swing.JTextField textFindTip;
private javax.swing.JTextField textName; private javax.swing.JTextField textName;
private javax.swing.JTextField textStreet; private javax.swing.JTextField textStreet;
private javax.swing.JTextArea txtKontakte;
// End of variables declaration//GEN-END:variables // 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;
}
} }

Loading…
Cancel
Save