Merge lp:~tapaal-contributor/tapaal/add-undo-batch-export-1938790 into lp:tapaal

Proposed by Lena Ernstsen
Status: Merged
Approved by: Jiri Srba
Approved revision: 1132
Merged at revision: 1132
Proposed branch: lp:~tapaal-contributor/tapaal/add-undo-batch-export-1938790
Merge into: lp:tapaal
Diff against target: 241 lines (+126/-32)
3 files modified
src/dk/aau/cs/gui/undo/AddFileExportBatchCommand.java (+36/-0)
src/dk/aau/cs/gui/undo/RemoveFileExportBatchCommand.java (+36/-0)
src/pipe/gui/ExportBatchDialog.java (+54/-32)
To merge this branch: bzr merge lp:~tapaal-contributor/tapaal/add-undo-batch-export-1938790
Reviewer Review Type Date Requested Status
Jiri Srba Approve
Kenneth Yrke Jørgensen code Approve
Review via email: mp+406649@code.launchpad.net

Commit message

Added undo/redo feature in export batch dialog

Description of the change

It is possible to undo/redo adding models, removing models, and clearing the entire list of models.

To post a comment you must log in.
Revision history for this message
Kenneth Yrke Jørgensen (yrke) :
review: Approve (code)
Revision history for this message
Jiri Srba (srba) wrote :

Tested and works.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'src/dk/aau/cs/gui/undo/AddFileExportBatchCommand.java'
2--- src/dk/aau/cs/gui/undo/AddFileExportBatchCommand.java 1970-01-01 00:00:00 +0000
3+++ src/dk/aau/cs/gui/undo/AddFileExportBatchCommand.java 2021-08-04 11:51:12 +0000
4@@ -0,0 +1,36 @@
5+package dk.aau.cs.gui.undo;
6+
7+import dk.aau.cs.gui.BatchProcessingDialog;
8+import pipe.gui.ExportBatchDialog;
9+
10+import javax.swing.*;
11+import java.io.File;
12+import java.util.List;
13+
14+public class AddFileExportBatchCommand extends Command{
15+ private final DefaultListModel<File> listModel;
16+ final List<File> files;
17+ final File file;
18+ private final ExportBatchDialog dialog;
19+
20+ public AddFileExportBatchCommand(DefaultListModel<File> listModel, File file, List<File> files, ExportBatchDialog dialog){
21+ this.listModel = listModel;
22+ this.file = file;
23+ this.files = files;
24+ this.dialog = dialog;
25+ }
26+
27+ @Override
28+ public void redo() {
29+ files.add(file);
30+ listModel.addElement(file);
31+ dialog.enableButtons();
32+ }
33+
34+ @Override
35+ public void undo() {
36+ files.remove(file);
37+ listModel.removeElement(file);
38+ dialog.enableButtons();
39+ }
40+}
41
42=== added file 'src/dk/aau/cs/gui/undo/RemoveFileExportBatchCommand.java'
43--- src/dk/aau/cs/gui/undo/RemoveFileExportBatchCommand.java 1970-01-01 00:00:00 +0000
44+++ src/dk/aau/cs/gui/undo/RemoveFileExportBatchCommand.java 2021-08-04 11:51:12 +0000
45@@ -0,0 +1,36 @@
46+package dk.aau.cs.gui.undo;
47+
48+import dk.aau.cs.gui.BatchProcessingDialog;
49+import pipe.gui.ExportBatchDialog;
50+
51+import javax.swing.*;
52+import java.io.File;
53+import java.util.List;
54+
55+public class RemoveFileExportBatchCommand extends Command {
56+ private final DefaultListModel<File> listModel;
57+ final List<File> files;
58+ final File file;
59+ private final ExportBatchDialog dialog;
60+
61+ public RemoveFileExportBatchCommand(DefaultListModel<File> listModel, File file, List<File> files, ExportBatchDialog dialog){
62+ this.listModel = listModel;
63+ this.file = file;
64+ this.files = files;
65+ this.dialog = dialog;
66+ }
67+
68+ @Override
69+ public void redo() {
70+ files.remove(file);
71+ listModel.removeElement(file);
72+ dialog.enableButtons();
73+ }
74+
75+ @Override
76+ public void undo() {
77+ files.add(file);
78+ listModel.addElement(file);
79+ dialog.enableButtons();
80+ }
81+}
82
83=== modified file 'src/pipe/gui/ExportBatchDialog.java'
84--- src/pipe/gui/ExportBatchDialog.java 2020-12-14 17:13:30 +0000
85+++ src/pipe/gui/ExportBatchDialog.java 2021-08-04 11:51:12 +0000
86@@ -1,16 +1,7 @@
87 package pipe.gui;
88
89-import java.awt.BorderLayout;
90-import java.awt.Color;
91-import java.awt.Component;
92-import java.awt.Dimension;
93-import java.awt.FlowLayout;
94-import java.awt.Frame;
95-import java.awt.GridBagConstraints;
96-import java.awt.GridBagLayout;
97-import java.awt.Insets;
98-import java.awt.Point;
99-import java.awt.Rectangle;
100+import java.awt.*;
101+import java.awt.event.ActionEvent;
102 import java.awt.event.KeyAdapter;
103 import java.awt.event.KeyEvent;
104 import java.awt.event.MouseEvent;
105@@ -35,6 +26,7 @@
106 import javax.xml.transform.TransformerConfigurationException;
107 import javax.xml.transform.TransformerException;
108
109+import dk.aau.cs.gui.undo.*;
110 import org.w3c.dom.DOMException;
111 import dk.aau.cs.gui.FileNameCellRenderer;
112 import dk.aau.cs.gui.components.ExportBatchResultTableModel;
113@@ -45,6 +37,7 @@
114 import dk.aau.cs.util.StringComparator;
115 import pipe.dataLayer.DataLayer;
116 import pipe.dataLayer.TAPNQuery;
117+import pipe.gui.undo.UndoManager;
118 import pipe.gui.widgets.filebrowser.FileBrowser;
119
120 public class ExportBatchDialog extends JDialog {
121@@ -60,8 +53,7 @@
122 private final static String NAME_FailStringFolderExists = "Failed as the subfolder already exists";
123 private final static String NAME_FailStringParseError = "Failed due to net/query parsing error";
124
125-
126- private JPanel filesButtonsPanel;
127+ private JPanel filesButtonsPanel;
128 private JPanel mainPanel;
129 private JButton addFilesButton;
130 private JButton clearFilesButton;
131@@ -84,7 +76,9 @@
132 private JDialog progressBarContainer;
133 static boolean noOrphanTransitions = false;
134
135- static ExportBatchDialog exportBatchDialog;
136+ private final UndoManager undoManager = new UndoManager();
137+
138+ static ExportBatchDialog exportBatchDialog;
139 ModelLoader loader = new ModelLoader();
140
141 public static boolean isDialogVisible() {
142@@ -114,7 +108,8 @@
143 private ExportBatchDialog(Frame frame, String title, boolean modal) {
144 super(frame, title, modal);
145 initComponents();
146- }
147+ makeShortcuts();
148+ }
149
150 private void initComponents() {
151 setLayout(new FlowLayout());
152@@ -412,28 +407,29 @@
153
154 File[] filesArray = browser.openFiles();
155 if (filesArray.length>0) {
156- for (File file : filesArray) {
157+ undoManager.newEdit();
158+ for (File file : filesArray) {
159 lastSelectPath = file.getParent();
160- if (!files.contains(file)) {
161- files.add(file);
162- listModel.addElement(file);
163- }
164+ if (!files.contains(file)) {
165+ Command c = new AddFileExportBatchCommand(listModel, file, files, this);
166+ c.redo();
167+ undoManager.addEdit(c);
168+ }
169 }
170-
171- enableButtons();
172 }
173 }
174
175 private void removeSelectedFiles() {
176- for (Object o : fileList.getSelectedValuesList()) {
177- File file = (File) o;
178- files.remove(file);
179- listModel.removeElement(file);
180- }
181- enableButtons();
182+ undoManager.newEdit();
183+ for (Object o : fileList.getSelectedValuesList()) {
184+ File file = (File) o;
185+ Command c = new RemoveFileExportBatchCommand(listModel, file, files, this);
186+ c.redo();
187+ undoManager.addEdit(c);
188+ }
189 }
190
191- private void enableButtons() {
192+ public void enableButtons() {
193 fileList.setEnabled(true);
194 uniqueQueryNames.setEnabled(true);
195 addFilesButton.setEnabled(true);
196@@ -450,11 +446,17 @@
197 }
198 else
199 exportFilesButton.setEnabled(false);
200+ }
201
202- }
203 private void clearFiles() {
204- files.clear();
205- listModel.removeAllElements();
206+ undoManager.newEdit();
207+
208+ for (Object o : listModel.toArray()) {
209+ File file = (File)o;
210+ Command c = new RemoveFileExportBatchCommand(listModel, file, files, this);
211+ c.redo();
212+ undoManager.addEdit(c);
213+ }
214 }
215
216 private void selectDestinationPath() {
217@@ -643,6 +645,26 @@
218 return result[1];
219 }
220 }
221+
222+ private void makeShortcuts(){
223+ int shortcutkey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
224+ ActionMap am = mainPanel.getActionMap();
225+ am.put("undo", new AbstractAction() {
226+ @Override
227+ public void actionPerformed(ActionEvent e) {
228+ undoManager.undo();
229+ }
230+ });
231+ am.put("redo", new AbstractAction() {
232+ @Override
233+ public void actionPerformed(ActionEvent e) {
234+ undoManager.redo();
235+ }
236+ });
237+ InputMap im = mainPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
238+ im.put(KeyStroke.getKeyStroke('Z', shortcutkey), "undo");
239+ im.put(KeyStroke.getKeyStroke('Y', shortcutkey), "redo");
240+ }
241 }
242
243

Subscribers

People subscribed via source and target branches