Skip to content

Commit c1d76d1

Browse files
committed
undo/redo multiple rows
1 parent 880d947 commit c1d76d1

9 files changed

Lines changed: 138 additions & 55 deletions

File tree

src/actions/edit/undoredo/UndoRedoableInterface.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package actions.edit.undoredo;
22

3-
import ui.helper.historyui.HistoryDataObject;
3+
import ui.helper.historyui.TableUIDataObject;
44

5-
public abstract class UndoRedoableInterface extends HistoryDataObject {
5+
public abstract class UndoRedoableInterface extends TableUIDataObject {
66

77
public abstract void undoAction();
88
public abstract void redoAction();

src/ui/MainFrame.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.awt.Toolkit;
88
import java.util.ArrayList;
99

10+
import javax.swing.BorderFactory;
1011
import javax.swing.JFrame;
1112
import javax.swing.JPanel;
1213

@@ -15,7 +16,8 @@
1516
import ui.helper.historyui.undoredoLog.UndoredoDialog;
1617

1718
public class MainFrame extends JFrame {
18-
Color color = new Color(150, 150, 150);
19+
Color backgroundColor = new Color(200, 200, 200);
20+
1921
ArrayList<JPanel> panelList;
2022

2123
public MainFrame() {
@@ -60,6 +62,7 @@ public MainFrame() {
6062
// tool bar
6163
toolBar.getSelectTool().addSelectionToolListener(menuBar);
6264
toolBar.addToolBarListener(paintPanel);
65+
toolBar.setBorder(BorderFactory.createLineBorder(Color.black));
6366

6467
// change backgrounds setting
6568
changeTheme();
@@ -74,10 +77,10 @@ public static void main(String[] args) {
7477

7578
/* change the background setting for JPanels */
7679
public void changeTheme() {
77-
setForeground(color);
78-
setBackground(color);
80+
setForeground(backgroundColor);
81+
setBackground(backgroundColor);
7982
for (JPanel panel : panelList) {
80-
panel.setBackground(color);
83+
panel.setBackground(backgroundColor);
8184
}
8285
}
8386

src/ui/PaintPanel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ui;
22

33
import java.awt.BorderLayout;
4+
import java.awt.Color;
45
import java.awt.Cursor;
56
import java.awt.Graphics;
67
import java.awt.Point;
@@ -241,4 +242,5 @@ public void setDefaultSelectTool(){
241242
selectTool.getButton().doClick();
242243
}
243244

245+
244246
}

src/ui/helper/historyui/HistoryUIInterface.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/ui/helper/historyui/HistoryDataObject.java renamed to src/ui/helper/historyui/TableUIDataObject.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
* HistoryDataOjbect
88
*
99
*/
10-
public class HistoryDataObject {
10+
public class TableUIDataObject {
1111

1212
String data;
1313
int row;
1414
boolean untraced;
15-
public HistoryDataObject(){
15+
public TableUIDataObject(){
1616

1717
}
1818

19-
public HistoryDataObject(String data){
19+
public TableUIDataObject(String data){
2020
this.data = data;
2121
untraced = false;
2222
}

src/ui/helper/historyui/HistoryUI.java renamed to src/ui/helper/historyui/TableUITemplate.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import javax.swing.BorderFactory;
1414
import javax.swing.JButton;
15+
import javax.swing.JComponent;
1516
import javax.swing.JPanel;
1617
import javax.swing.JScrollPane;
1718
import javax.swing.JTable;
@@ -28,21 +29,22 @@
2829
* use getter and setter
2930
*
3031
*/
31-
public class HistoryUI extends JPanel {
32+
public class TableUITemplate extends JPanel {
3233

3334
private JTable resultsTable;
3435
private DefaultTableModel defaultTableModel;
3536
private JScrollPane scrollPane;
3637
private JPanel button_panel;
3738
private ArrayList<JButton> buttonArray = new ArrayList<JButton>();
38-
private Color color = new Color(150,150,150);
39-
protected HistoryUIInterface delegate;
39+
private Color color;
40+
protected TableUITemplateInterface delegate;
4041
private int index = -1; //value of last item that is not untraced
42+
protected ArrayList<JComponent> jcomponents = new ArrayList<JComponent>();
4143

4244
/**
4345
* Setup historyUI
4446
*/
45-
public HistoryUI(String[] titles){
47+
public TableUITemplate(String[] titles){
4648

4749
// set the defaultTableModel to non editable by user clicking around
4850
this.setDefaultTableModel((new DefaultTableModel(0, 1) {
@@ -54,7 +56,6 @@ public boolean isCellEditable(int row, int column) {
5456
}
5557
}));
5658

57-
5859
setLayout(new BorderLayout(0, 0));
5960

6061
//set size
@@ -72,22 +73,20 @@ public boolean isCellEditable(int row, int column) {
7273
//create panel for buttons
7374
setButtonPanel();
7475

75-
setForeground(getColor());
76-
setBackground(getColor());
7776
}
7877

7978

8079
protected void setTable() {
81-
this.setResultsTable((new JTable()));
80+
resultsTable = new JTable();
8281
getResultsTable().setTableHeader(null);
83-
getResultsTable().setFont(new Font("Apple LiSung", Font.PLAIN, 16));
8482
getResultsTable().setShowHorizontalLines(false);
8583
getResultsTable().setShowVerticalLines(false);
8684
getResultsTable().setShowGrid(false);
8785
getResultsTable().setBorder(null);
8886
getResultsTable().setBackground(getColor());
89-
getResultsTable().setForeground(Color.WHITE);
87+
getResultsTable().setForeground(Color.BLACK);
9088
getResultsTable().setModel(getDefaultTableModel());
89+
jcomponents.add(resultsTable);
9190
}
9291

9392
/**
@@ -112,14 +111,16 @@ private void setScrollPane(){
112111
scrollPane.setBackground(getColor());
113112
scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
114113
add(scrollPane, BorderLayout.CENTER);
114+
jcomponents.add(scrollPane);
115115
}
116116

117117
private void setButtonPanel() {
118118
FlowLayout fl_button_panel = new FlowLayout();
119-
setButton_panel(new JPanel(fl_button_panel));
119+
button_panel = new JPanel(fl_button_panel);
120120
getButton_panel().setBorder(BorderFactory.createLineBorder(Color.black));
121121
getButton_panel().setBackground(getColor());
122122
add(getButton_panel(), BorderLayout.SOUTH);
123+
jcomponents.add(button_panel);
123124
}
124125

125126
/**
@@ -133,10 +134,11 @@ public void redo(){
133134
* set redo for specific row
134135
*/
135136
public void redo(int r,int c){
136-
HistoryDataObject o = (HistoryDataObject) getDefaultTableModel().getValueAt(r,c );
137+
TableUIDataObject o = (TableUIDataObject) getDefaultTableModel().getValueAt(r,c );
137138
o.setUntraced(false);
138139
getDefaultTableModel().fireTableDataChanged();
139140
}
141+
140142

141143
/**
142144
* set undo for last row, last col
@@ -150,17 +152,18 @@ public void undo(){
150152
* @param r
151153
*/
152154
public void undo(int r, int c){
153-
HistoryDataObject o = (HistoryDataObject) getDefaultTableModel().getValueAt(r, c);
155+
TableUIDataObject o = (TableUIDataObject) getDefaultTableModel().getValueAt(r, c);
154156
o.setUntraced(true);
155157
getDefaultTableModel().fireTableDataChanged();
156158
index--;
157159
}
158160

161+
159162
/**
160163
* add a row to the end of table
161164
* receive a HistoryDataObject
162165
*/
163-
public void insert(HistoryDataObject e){
166+
public void insert(TableUIDataObject e){
164167
clearUntraced();
165168
int r = getNumRow();
166169
insert(e,r);
@@ -169,7 +172,7 @@ public void insert(HistoryDataObject e){
169172
/**
170173
* insert a row into specific row
171174
*/
172-
public void insert(HistoryDataObject e, int r){
175+
public void insert(TableUIDataObject e, int r){
173176
clearUntraced();
174177
getDefaultTableModel().insertRow(r,new Object[] {e});
175178
getDefaultTableModel().fireTableDataChanged();
@@ -246,7 +249,7 @@ public void clear(){
246249
getDefaultTableModel().fireTableDataChanged();
247250
}
248251

249-
public void setDelegate(HistoryUIInterface delegate) {
252+
public void setDelegate(TableUITemplateInterface delegate) {
250253
this.delegate = delegate;
251254
}
252255

@@ -346,5 +349,17 @@ public Color getColor() {
346349
public void setColor(Color color) {
347350
this.color = color;
348351
}
352+
353+
@Override
354+
public void setBackground(Color c){
355+
super.setBackground(c);
356+
color = c;
357+
358+
}
359+
360+
361+
public int getSelectedRow(){
362+
return resultsTable.getSelectedRow();
363+
}
349364
}
350365

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ui.helper.historyui;
2+
3+
public interface TableUITemplateInterface {
4+
5+
/**
6+
* Called when a button is pressed.
7+
* @param buttonName The name of the button
8+
* @param selectedRow the index of selected row. -1 if no row selected
9+
*/
10+
11+
public void didPressButton(String buttonName, int selectedRow);
12+
13+
14+
/**
15+
* implement addButtons method to add buttons to the button_panel
16+
*/
17+
public abstract void addButtons();
18+
19+
/**
20+
* implement updateButtonStatus to update button when button actions performanced
21+
*/
22+
public abstract void updateButtonStatus();
23+
24+
25+
}

src/ui/helper/historyui/HistoryUITest.java renamed to src/ui/helper/historyui/TableUITest.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
import org.junit.Before;
1010
import org.junit.Test;
1111

12-
public class HistoryUITest {
12+
public class TableUITest {
1313

14-
HistoryUI historyUI;
14+
TableUITemplate historyUI;
1515

1616

1717
@Before
1818
public void setUp(){
1919
String[] arr = {"delete","exit","clear","revert","confirm"};
20-
historyUI = new HistoryUI(arr);
21-
historyUI.insert(new HistoryDataObject("AAAA"));
22-
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"));
20+
historyUI = new TableUITemplate(arr);
21+
historyUI.insert(new TableUIDataObject("AAAA"));
22+
historyUI.insert(new TableUIDataObject("BBB"));
23+
historyUI.insert(new TableUIDataObject("Test 1"));
24+
historyUI.insert(new TableUIDataObject("This is a item"));
25+
historyUI.insert(new TableUIDataObject("turn right"));
26+
historyUI.insert(new TableUIDataObject("112345"));
2727

2828
}
2929

@@ -35,13 +35,25 @@ public void test() throws InterruptedException {
3535
public void run() {
3636
//ClassSearchFrame classSearchFrame = new ClassSearchFrame();
3737

38-
historyUI.setDelegate(new HistoryUIInterface() {
38+
historyUI.setDelegate(new TableUITemplateInterface() {
3939

4040
@Override
4141
public void didPressButton(String buttonName, int selectedRow) {
4242
System.out.println("buttonname = " + buttonName + "; selectedRow = " + selectedRow);
4343

4444
}
45+
46+
@Override
47+
public void addButtons() {
48+
// TODO Auto-generated method stub
49+
50+
}
51+
52+
@Override
53+
public void updateButtonStatus() {
54+
// TODO Auto-generated method stub
55+
56+
}
4557
});
4658

4759

0 commit comments

Comments
 (0)