Skip to content

Commit 4a0e2bc

Browse files
author
Xiangyi Gong
committed
Merge branch 'develop' into XYGBranch
2 parents 825010a + 49744e5 commit 4a0e2bc

File tree

9 files changed

+218
-21
lines changed

9 files changed

+218
-21
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: java
2+
jdk:
3+
- oraclejdk8
4+
5+
before_script:
6+
- "export DISPLAY=:99.0"
7+
- "sh -e /etc/init.d/xvfb start"
8+
- sleep 3 # give xvfb some time to start
9+
10+
notifications:
11+
email: false

build.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<project name="JavaSketchPad" default="run" basedir=".">
2+
<property name="build.dir" location="ant_build" />
3+
<property name="src.dir" location="src" />
4+
5+
<target name="init" description="Creating directory">
6+
<mkdir dir="${build.dir}" />
7+
</target>
8+
9+
<target name="compile" depends="init" description="Compiling Sources">
10+
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false">
11+
<classpath refid="classpath.test" />
12+
</javac>
13+
</target>
14+
15+
<target name="run" depends="compile" description="Running the program">
16+
</target>
17+
18+
<target name="clean">
19+
<delete dir="${build.dir}" />
20+
</target>
21+
22+
23+
<path id="classpath.test">
24+
<pathelement location="lib/junit-4.12.jar" />
25+
<pathelement location="lib/hamcrest-core-1.3.jar" />
26+
<pathelement location="${build.dir}" />
27+
</path>
28+
29+
30+
<target name="test" depends="compile">
31+
<junit printsummary="on" haltonfailure="yes" fork="true">
32+
<classpath>
33+
<path refid="classpath.test" />
34+
<pathelement location="${test.build.dir}" />
35+
</classpath>
36+
<formatter type="brief" usefile="false" />
37+
<batchtest>
38+
<fileset dir="${src.dir}" includes="**/*Test.java" />
39+
</batchtest>
40+
</junit>
41+
</target>
42+
</project>

lib/hamcrest-core-1.3.jar

44 KB
Binary file not shown.

lib/junit-4.12.jar

308 KB
Binary file not shown.

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/classsearch/ClassSearchFrameTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ClassSearchFrameTest {
1212

1313
@Test
1414
public void test() throws InterruptedException {
15-
SwingUtilities.invokeLater(new Runnable() {
15+
Runnable t = (new Runnable() {
1616

1717
@Override
1818
public void run() {
@@ -29,10 +29,29 @@ public void didSelectClass(String classname) {
2929
classSearchFrame.setVisible(true);
3030
classSearchFrame.setSize(new Dimension(300, 200));
3131

32+
/* Comment below to test the framework */
33+
/* Added for the purpose of continuous integration */
34+
new Thread(new Runnable() {
35+
36+
@Override
37+
public void run() {
38+
try {
39+
Thread.sleep(5000);
40+
classSearchFrame.setVisible(false);
41+
classSearchFrame.dispose();
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
46+
}
47+
}).start();
3248

3349
}
3450
});
35-
Thread.sleep(Long.MAX_VALUE);
51+
SwingUtilities.invokeLater(t);
52+
Thread.sleep(10000);
53+
54+
3655
}
3756

3857
}

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: 26 additions & 3 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,12 +46,30 @@ 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));
50+
51+
52+
/* Comment below to test the framework */
53+
/* Added for the purpose of continuous integration */
54+
new Thread(new Runnable() {
55+
56+
@Override
57+
public void run() {
58+
59+
try {
60+
Thread.sleep(5000);
61+
historyUI.dispose();
62+
} catch (InterruptedException e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
}).start();
67+
4568

4669

4770
}
4871
});
49-
Thread.sleep(Long.MAX_VALUE);
72+
Thread.sleep(10000);
5073
}
5174

5275

0 commit comments

Comments
 (0)