Skip to content

Commit 4c3e5d3

Browse files
authored
Merge pull request #39 from UCSDOalads/addCDEFbykaic
Add delete clear revert
2 parents 66881ad + 4610907 commit 4c3e5d3

4 files changed

Lines changed: 125 additions & 18 deletions

File tree

src/ui/helper/classsearch/ClassSearchFrame.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public void run() {
140140
public void actionPerformed(ActionEvent e) {
141141

142142
// User cancel the class search
143+
143144
if (e.getSource() == btnCancel) {
144145
setVisible(false);
145146
dispose();

src/ui/helper/historyui/HistoryDataObject.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package ui.helper.historyui;
22

3+
4+
/**
5+
*
6+
* HistoryDataOjbect
7+
*
8+
*/
39
public class HistoryDataObject {
410

511
String data;

src/ui/helper/historyui/HistoryUI.java

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.awt.BorderLayout;
44
import java.awt.event.ActionEvent;
55
import 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

811
import javax.swing.JFrame;
912
import javax.swing.JPanel;
@@ -16,6 +19,13 @@
1619
import javax.swing.DefaultListSelectionModel;
1720
import javax.swing.JButton;
1821

22+
23+
24+
25+
/**
26+
* create historyUI
27+
*
28+
*/
1929
public 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

src/ui/helper/historyui/HistoryUITest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ public class HistoryUITest {
1616

1717
@Before
1818
public void setUp(){
19-
String[] arr = {"cacel","revert","confirm"};
19+
String[] arr = {"delete","exit","clear","revert","confirm"};
2020
historyUI = new HistoryUI(arr);
2121
historyUI.insert(new HistoryDataObject("AAAA"));
2222
historyUI.insert(new HistoryDataObject("BBB"));
23+
historyUI.insert(new HistoryDataObject("Test 1"));
24+
historyUI.insert(new HistoryDataObject("This is a item"));
25+
historyUI.insert(new HistoryDataObject("turn right"));
26+
historyUI.insert(new HistoryDataObject("112345"));
27+
2328
}
2429

2530
@Test
@@ -41,7 +46,7 @@ public void didPressButton(String buttonName, int selectedRow) {
4146

4247

4348
historyUI.setVisible(true);
44-
historyUI.setSize(new Dimension(300, 200));
49+
historyUI.setSize(new Dimension(500, 400));
4550

4651

4752
/* Comment below to test the framework */

0 commit comments

Comments
 (0)