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+
1926public class HistoryUI extends JFrame
2027 {
2128 private static final long serialVersionUID = 8141494344180865577L ;
@@ -27,6 +34,8 @@ public class HistoryUI extends JFrame
2734 private JPanel panel ;
2835 private JScrollPane scrollPane ;
2936
37+ @ SuppressWarnings ("rawtypes" )
38+ private Stack <ArrayList > delete_history ;
3039
3140 /*
3241 * Setup class finders
@@ -53,36 +62,110 @@ public boolean isCellEditable(int row, int column) {
5362 resultsTable = new JTable ();
5463 resultsTable .setModel (defaultTableModel );
5564 resultsTable .setSelectionModel (new ForcedListSelectionModel ());
56-
65+
5766 // scroll option
5867 scrollPane = new JScrollPane (resultsTable );
5968 panel .add (scrollPane , BorderLayout .CENTER );
6069
6170 // add small JPanel for Cancel and Confirm buttons
62- JPanel buttom_panel = new JPanel ();
63- getContentPane ().add (buttom_panel , BorderLayout .SOUTH );
71+ JPanel button_panel = new JPanel ();
72+ getContentPane ().add (button_panel , BorderLayout .SOUTH );
6473
65- //create buttons from array list
74+ //initialize buttons
75+ createButtons (titles , button_panel );
76+
77+ delete_history = new Stack <>();
78+ }
79+
80+ /*
81+ * create buttons
82+ * param: a list of buttons' name and button panel
83+ */
84+ public void createButtons (String [] titles , JPanel button_panel ){
6685 for (String title : titles ){
6786 JButton button = new JButton (title );
68- buttom_panel .add (button );
87+ button_panel .add (button );
6988 button .addActionListener (new ActionListener () {
70-
89+
7190 @ Override
7291 public void actionPerformed (ActionEvent e ) {
92+ //if delete button is pressed, delete selected rows
93+ if (title == "delete" ){
94+ int [] indices = resultsTable .getSelectedRows ();
95+ if (indices .length != 0 )
96+ removeSeveralRows (indices );
97+ }
98+ //if exit button pressed, close window
99+ else if (title == "exit" ){
100+ System .exit (EXIT_ON_CLOSE );
101+ }
102+
103+ //if revert button pressed
104+ else if (title =="revert" ){
105+ revert ();
106+ }
107+
108+ //if clear button pressed
109+ else if (title =="clear" ){
110+ int num_rows = resultsTable .getRowCount ();
111+ int [] indices = IntStream .range (0 , num_rows ).toArray ();
112+ removeSeveralRows (indices );
113+ }
73114 delegate .didPressButton (title , resultsTable .getSelectedRow ());
74115
75116 }
76117 });
77118 }
78119 }
79120
80- //add a row to the end of table
121+
122+ /*
123+ * revert deleted rows
124+ */
125+ @ SuppressWarnings ("rawtypes" )
126+ public void revert (){
127+ if (!delete_history .isEmpty ()){
128+ ArrayList arr = delete_history .pop ();
129+ int [] indices = (int []) arr .get (0 );
130+ String [] objects = (String []) arr .get (1 );
131+ for (int i = 0 ; i <indices .length ; i ++){
132+ defaultTableModel .insertRow (indices [i ], new Object [] {objects [i ]} );
133+ }
134+ }
135+ }
136+
137+
138+
139+ /*
140+ * add a row to the end of table
141+ * receive a HistoryDataObject
142+ */
81143 public void insert (HistoryDataObject e ){
82144 defaultTableModel .addRow (new Object [] {e .toString ()});
83145 }
84146
85-
147+
148+ /*
149+ * remove several selected rows from table
150+ * param: an array of rows number to be removed
151+ * return an ArrayList contains a list of indices and corresponding data
152+ */
153+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
154+ public void removeSeveralRows (int [] indices ){
155+ //create a arr to hold the deleted data and rows
156+ ArrayList arr = new ArrayList <>();
157+ String [] objects = new String [indices .length ];
158+ Arrays .sort (indices );
159+
160+ for (int i =indices .length -1 ;i >=0 ;i --){
161+ objects [i ] = (String ) resultsTable .getModel ().getValueAt (indices [i ], 0 );
162+ defaultTableModel .removeRow (indices [i ]);
163+ }
164+
165+ arr .add (indices );
166+ arr .add (objects );
167+ delete_history .push (arr );
168+ }
86169
87170 public void setDelegate (HistoryUIInterface delegate ) {
88171 this .delegate = delegate ;
@@ -97,7 +180,7 @@ private static class ForcedListSelectionModel
97180 private static final long serialVersionUID = 1L ;
98181
99182 public ForcedListSelectionModel () {
100- this .setSelectionMode (ListSelectionModel .SINGLE_SELECTION );
183+ this .setSelectionMode (ListSelectionModel .MULTIPLE_INTERVAL_SELECTION );
101184 };
102185 }
103186
0 commit comments