33import java .awt .BorderLayout ;
44import java .awt .event .ActionEvent ;
55import java .awt .event .ActionListener ;
6-
6+ import java .util .ArrayList ;
7+ import java .util .Arrays ;
8+ import java .util .Stack ;
9+ import java .util .stream .IntStream ;
710
811import javax .swing .JFrame ;
912import javax .swing .JPanel ;
1619import javax .swing .DefaultListSelectionModel ;
1720import javax .swing .JButton ;
1821
22+
23+
24+
25+ /**
26+ * create historyUI
27+ *
28+ */
1929public class HistoryUI extends JFrame
2030 {
2131 private static final long serialVersionUID = 8141494344180865577L ;
@@ -27,9 +37,13 @@ public class HistoryUI extends JFrame
2737 private JPanel panel ;
2838 private JScrollPane scrollPane ;
2939
40+ @ SuppressWarnings ("rawtypes" )
41+ private Stack <ArrayList > delete_history ;
3042
31- /*
32- * Setup class finders
43+ /**
44+ * Setup historyUI
45+ *
46+ * param: receive a list of button names
3347 */
3448 public HistoryUI (String [] titles ) {
3549
@@ -53,51 +67,132 @@ public boolean isCellEditable(int row, int column) {
5367 resultsTable = new JTable ();
5468 resultsTable .setModel (defaultTableModel );
5569 resultsTable .setSelectionModel (new ForcedListSelectionModel ());
56-
70+
5771 // scroll option
5872 scrollPane = new JScrollPane (resultsTable );
5973 panel .add (scrollPane , BorderLayout .CENTER );
6074
61- // add small JPanel for Cancel and Confirm buttons
62- JPanel buttom_panel = new JPanel ();
63- getContentPane ().add (buttom_panel , BorderLayout .SOUTH );
75+ // add small JPanel for buttons
76+ JPanel button_panel = new JPanel ();
77+ getContentPane ().add (button_panel , BorderLayout .SOUTH );
6478
65- //create buttons from array list
79+ //initialize buttons
80+ createButtons (titles , button_panel );
81+
82+ //stack, used to stored deleted rows
83+ delete_history = new Stack <>();
84+ }
85+
86+ /**
87+ * create buttons
88+ * param: a list of buttons' name and button panel
89+ */
90+ public void createButtons (String [] titles , JPanel button_panel ){
91+ //loop through arr and add buttons
6692 for (String title : titles ){
6793 JButton button = new JButton (title );
68- buttom_panel .add (button );
94+ button_panel .add (button );
6995 button .addActionListener (new ActionListener () {
70-
96+
7197 @ Override
7298 public void actionPerformed (ActionEvent e ) {
99+ //if delete button is pressed, delete selected rows
100+ if (title == "delete" ){
101+ int [] indices = resultsTable .getSelectedRows ();
102+ if (indices .length != 0 )
103+ removeSeveralRows (indices );
104+ }
105+ //if exit button pressed, close window
106+ else if (title == "exit" ){
107+ System .exit (EXIT_ON_CLOSE );
108+ }
109+
110+ //if revert button pressed, revert to last version
111+ else if (title =="revert" ){
112+ revert ();
113+ }
114+
115+ //if clear button pressed, delete all rows in table
116+ else if (title =="clear" ){
117+ int num_rows = resultsTable .getRowCount ();
118+ int [] indices = IntStream .range (0 , num_rows ).toArray ();
119+ removeSeveralRows (indices );
120+ }
73121 delegate .didPressButton (title , resultsTable .getSelectedRow ());
74122
75123 }
76124 });
77125 }
78126 }
79127
80- //add a row to the end of table
81- public void insert (HistoryDataObject e ){
82- defaultTableModel .addRow (new Object [] {e .toString ()});
128+
129+ /**
130+ * revert deleted rows
131+ */
132+ @ SuppressWarnings ("rawtypes" )
133+ public void revert (){
134+
135+ if (!delete_history .isEmpty ()){
136+ ArrayList arr = delete_history .pop ();
137+ //get the deleted rows and corresponding data
138+ int [] indices = (int []) arr .get (0 );
139+ HistoryDataObject [] objects = (HistoryDataObject []) arr .get (1 );
140+
141+ //insert data to its row
142+ for (int i = 0 ; i <indices .length ; i ++){
143+ defaultTableModel .insertRow (indices [i ], new Object [] {objects [i ]} );
144+ }
145+ }
83146 }
84147
85148
149+
150+ /**
151+ * add a row to the end of table
152+ * receive a HistoryDataObject
153+ */
154+ public void insert (HistoryDataObject e ){
155+ defaultTableModel .addRow (new Object [] {e });
156+ }
157+
158+
159+ /**
160+ * remove several selected rows from table and stores them into stack
161+ * param: an array of rows number to be removed
162+ */
163+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
164+ public void removeSeveralRows (int [] indices ){
165+ //create a arr to hold the deleted rows and its data
166+ ArrayList arr = new ArrayList <>();
167+ HistoryDataObject [] objects = new HistoryDataObject [indices .length ];
168+ Arrays .sort (indices );
169+
170+ //loop through indices and delete
171+ for (int i =indices .length -1 ;i >=0 ;i --){
172+ objects [i ] = (HistoryDataObject ) resultsTable .getModel ().getValueAt (indices [i ], 0 );
173+ defaultTableModel .removeRow (indices [i ]);
174+ }
175+
176+ //stores deleted rows and its data to delete_history stack
177+ arr .add (indices );
178+ arr .add (objects );
179+ delete_history .push (arr );
180+ }
86181
87182 public void setDelegate (HistoryUIInterface delegate ) {
88183 this .delegate = delegate ;
89184 }
90185
91- /*
92- * Forcedly remove single selection in JTable
186+ /**
187+ * Forcedly multiple interval selection in JTable
93188 */
94189 private static class ForcedListSelectionModel
95190 extends
96191 DefaultListSelectionModel {
97192 private static final long serialVersionUID = 1L ;
98193
99194 public ForcedListSelectionModel () {
100- this .setSelectionMode (ListSelectionModel .SINGLE_SELECTION );
195+ this .setSelectionMode (ListSelectionModel .MULTIPLE_INTERVAL_SELECTION );
101196 };
102197 }
103198
0 commit comments