Skip to content

Commit a4f951b

Browse files
committed
revert delete clear
1 parent b5815e6 commit a4f951b

3 files changed

Lines changed: 101 additions & 12 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/HistoryUI.java

Lines changed: 93 additions & 10 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,10 @@
1619
import javax.swing.DefaultListSelectionModel;
1720
import javax.swing.JButton;
1821

22+
23+
24+
25+
1926
public 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

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
}

0 commit comments

Comments
 (0)