Skip to content

Commit ffa2db6

Browse files
committed
Merge branch 'develop' into fixedBugsbyTanSu
Fixed selectionTool highlighting and double click input bugs and modified so that right click in some tools perform operation without returning to selectionTool
2 parents e18d37f + 1d3cd6b commit ffa2db6

8 files changed

Lines changed: 339 additions & 29 deletions

src/actions/AddLazyJavaMethodComponentAction.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44

55
import javax.swing.JOptionPane;
66

7-
import actions.edit.undoredo.SharedUndoRedoActionManager;
8-
import actions.edit.undoredo.UndoRedoableInterface;
7+
import paintcomponents.java.lazy.ClassPaintComponent;
8+
import ui.PaintPanel;
9+
import ui.general.InputManager;
10+
import ui.general.InputManagerDelegate;
11+
import ui.helper.methodinput.MethodInputFrame;
912
import actions.global.ActionName;
1013
import actions.global.GlobalPaintActionExecuter;
1114
import actions.global.globalactions.AddLazyJavaClassGlobalAction;
1215
import actions.global.globalactions.AddLazyJavaMethodComponentGlobalAction;
1316
import actions.menu.ActionsMenuBarTitles;
14-
import paintcomponents.java.lazy.ClassConstructorPaintComponent;
15-
import paintcomponents.java.lazy.ClassPaintComponent;
16-
import paintcomponents.java.lazy.MethodPaintComponent;
17-
import ui.PaintPanel;
18-
import ui.general.InputManager;
19-
import ui.general.InputManagerDelegate;
2017

2118
public class AddLazyJavaMethodComponentAction extends MenuBarPaintAction {
2219

@@ -41,24 +38,19 @@ public boolean canPerformAction() {
4138
@Override
4239
public void performAction() {
4340
ClassPaintComponent comp = (ClassPaintComponent) panel.getSelectTool().getSelectedComponents().get(0);
44-
Method[] methods = comp.getDisplayingClass().getMethods();
45-
46-
String desiaredConstructorIndex = JOptionPane.showInputDialog(
47-
"Please enter the index of the constructor you would like to use: \n\n\n"
48-
49-
50-
+ getMethodsSelectionUI(methods));
51-
//call DialogInputChecker to check input
52-
DialogInputChecker inputChecker = new DialogInputChecker();
53-
if(inputChecker.isValidNumber(desiaredConstructorIndex, 0, methods.length -1)){
54-
55-
AddLazyJavaMethodComponentGlobalAction associatedAction
56-
= (AddLazyJavaMethodComponentGlobalAction) ActionName.ADD_LAZY_JAVA_METHOD_ACTION.getAssociatedAction();
57-
associatedAction.setMethodComponent(comp);
58-
associatedAction.setMethod(comp.getDisplayingClass().getMethods()[Integer.parseInt(desiaredConstructorIndex)]);
59-
GlobalPaintActionExecuter.getSharedInstance().execute(associatedAction, panel);
41+
42+
InputManager im = new InputManager();
43+
im.askForMethod(panel, new InputManagerDelegate<Method>() {
6044

61-
}
45+
@Override
46+
public void didFinishInput(Method input) {
47+
AddLazyJavaMethodComponentGlobalAction associatedAction = (AddLazyJavaMethodComponentGlobalAction)
48+
ActionName.ADD_LAZY_JAVA_METHOD_ACTION.getAssociatedAction();
49+
associatedAction.setMethodComponent(comp);
50+
associatedAction.setMethod(input);
51+
GlobalPaintActionExecuter.getSharedInstance().execute(associatedAction, panel);
52+
}
53+
}, comp.getDisplayingClass());
6254
}
6355

6456
public String getMethodsSelectionUI(Method[] methods) {

src/actions/InputDataForDataInputBoxAction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ public InputDataForDataInputBoxAction(PaintPanel panel) {
2121
public boolean canPerformAction() {
2222
ArrayList<PaintComponent> comps = panel.getSelectTool().getSelectedComponents();
2323
if(comps.size()!= 1) return false;
24-
if (comps.get(0) instanceof DataInputTextfieldPaintComponent) {
25-
return true;
24+
Object comp = comps.get(0);
25+
if (comp instanceof DataInputTextfieldPaintComponent) {
26+
//Make sure this is not a class box
27+
if( ((DataInputTextfieldPaintComponent) comp).canUpdate()){
28+
return true;
29+
}
2630
}
2731
return false;
2832
}

src/actions/global/globalactions/AddLazyJavaClassGlobalAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public void setClassToCreate(Class classToCreate) {
2323
@Override
2424
protected void execute(PaintPanel panel) {
2525
ClassPaintComponent comp = new ClassPaintComponent(classToCreate, x, y);
26+
//The class input box shouldn't be updatable.
27+
comp.setCannotUpdate();
2628
panel.addPaintComponent(comp);
2729

2830
if (panel.getSelectTool() != null) {

src/paintcomponents/data/DataInputTextfieldPaintComponent.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class DataInputTextfieldPaintComponent extends DataTextIOPaintComponent
1616
implements DataFromPointDataProvider{
1717

1818

19+
//Protect the class box from being updated
20+
boolean canUpdate = true;
21+
1922
public DataInputTextfieldPaintComponent(String displayingText, int x,
2023
int y) {
2124
super(displayingText, x, y);
@@ -52,8 +55,19 @@ public DataInputTextfieldPaintComponent(Element rootElement, PaintPanel panel) {
5255
linkPoints(rootElement);
5356
}
5457

58+
/*
59+
* Set this input box to be "in-updatable"
60+
* Now this should only be used by the class input box
61+
*/
62+
public void setCannotUpdate(){
63+
this.canUpdate = false;
64+
}
5565

56-
66+
/*
67+
* Getter for whether it's updatable.
68+
*/
69+
public boolean canUpdate(){
70+
return this.canUpdate;
71+
}
5772

58-
5973
}

src/ui/general/InputManager.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package ui.general;
22

33
import java.awt.Dimension;
4+
import java.lang.reflect.Method;
45

56
import javax.swing.JOptionPane;
67

78
import ui.PaintPanel;
89
import ui.helper.classsearch.ClassSearchFrame;
910
import ui.helper.classsearch.ClassSearchFrameDelegateInterface;
11+
import ui.helper.methodinput.MethodInputFrame;
12+
import ui.helper.methodinput.MethodSearchFrameDelegateInterface;
1013

1114
/**
1215
* Ask for things from user.
@@ -98,5 +101,36 @@ public void didSelectClass(String classname) {
98101
}
99102

100103

104+
public void askForMethod(PaintPanel panel, InputManagerDelegate<Method> delegate, Class c) {
105+
MethodInputFrame methodSearchFrame = new MethodInputFrame(c);
106+
107+
methodSearchFrame.setTitle("Add a Method");
108+
109+
methodSearchFrame.setDelegate(new MethodSearchFrameDelegateInterface() {
110+
111+
@Override
112+
public void didSelectMethod(String methodName) {
113+
114+
for (Method method : c.getMethods()) {
115+
if (methodName.equals(method.toString())) {
116+
delegate.didFinishInput(method);
117+
return;
118+
}
119+
}
120+
121+
JOptionPane.showMessageDialog(panel, methodName + " :: Method Not Found");
122+
123+
}
124+
});
125+
126+
127+
methodSearchFrame.setVisible(true);
128+
methodSearchFrame.setSize(new Dimension(600, 400));
129+
methodSearchFrame.setLocation(panel.getX() + panel.getWidth() / 2 - 300,
130+
panel.getY() + panel.getHeight() / 2 - 200);
131+
132+
}
133+
134+
101135

102136
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package ui.helper.methodinput;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
import java.lang.reflect.Method;
7+
import java.util.ArrayList;
8+
9+
import javax.swing.DefaultListSelectionModel;
10+
import javax.swing.JButton;
11+
import javax.swing.JFrame;
12+
import javax.swing.JOptionPane;
13+
import javax.swing.JPanel;
14+
import javax.swing.JScrollPane;
15+
import javax.swing.JTable;
16+
import javax.swing.JTextField;
17+
import javax.swing.ListSelectionModel;
18+
import javax.swing.SwingUtilities;
19+
import javax.swing.event.DocumentEvent;
20+
import javax.swing.event.DocumentListener;
21+
import javax.swing.table.DefaultTableModel;
22+
23+
/**
24+
* similar implementation as class search frame
25+
*/
26+
public class MethodInputFrame extends JFrame implements ActionListener, DocumentListener {
27+
28+
private JTextField searchingTextField;
29+
private JTable resultsTable;
30+
private MethodSearch searchUtil;
31+
private DefaultTableModel defaultTableModel;
32+
private MethodSearchFrameDelegateInterface delegate;
33+
34+
private Thread runningThread;
35+
private JButton btnCancel, btnConfirm;
36+
private JPanel panel;
37+
private JScrollPane scrollPane;
38+
39+
40+
public MethodInputFrame(Class c) {
41+
// set the defaultTableModel to non editable by user clicking around
42+
defaultTableModel = new DefaultTableModel(0, 1) {
43+
@Override
44+
public boolean isCellEditable(int row, int column) {
45+
return false;
46+
}
47+
};
48+
49+
// big JPanel window
50+
panel = new JPanel();
51+
getContentPane().add(panel, BorderLayout.CENTER);
52+
panel.setLayout(new BorderLayout(0, 0));
53+
54+
// search text field on top
55+
searchUtil = new MethodSearch(c);
56+
searchingTextField = new JTextField();
57+
getContentPane().add(searchingTextField, BorderLayout.NORTH);
58+
searchingTextField.setColumns(10);
59+
60+
// show result
61+
resultsTable = new JTable();
62+
resultsTable.setModel(defaultTableModel);
63+
resultsTable.setSelectionModel(new ForcedListSelectionModel());
64+
65+
// scroll option
66+
scrollPane = new JScrollPane(resultsTable);
67+
panel.add(scrollPane, BorderLayout.CENTER);
68+
69+
// add small JPanel for Cancel and Confirm buttons
70+
JPanel panel_1 = new JPanel();
71+
getContentPane().add(panel_1, BorderLayout.SOUTH);
72+
73+
btnCancel = new JButton("Cancel");
74+
panel_1.add(btnCancel);
75+
btnCancel.addActionListener(this);
76+
77+
btnConfirm = new JButton("Confirm");
78+
panel_1.add(btnConfirm);
79+
btnConfirm.addActionListener(this);
80+
81+
// set up connections
82+
searchingTextField.addActionListener(this);
83+
searchingTextField.getDocument().addDocumentListener(this);
84+
85+
}
86+
87+
88+
/*
89+
* Forcely remove single selection in Jtable
90+
*/
91+
private static class ForcedListSelectionModel
92+
extends
93+
DefaultListSelectionModel {
94+
public ForcedListSelectionModel() {
95+
this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
96+
};
97+
/*
98+
* potentially better solve the selection problem, but not needed at the
99+
* moment
100+
*
101+
* @Override public void clearSelection() { }
102+
*
103+
* @Override public void removeSelectionInterval(int index0, int index1)
104+
* { }
105+
*/
106+
}
107+
108+
private void updateMethodList() {
109+
if (runningThread != null) {
110+
runningThread.interrupt();
111+
}
112+
runningThread = new Thread(new Runnable() {
113+
114+
@Override
115+
public void run() {
116+
117+
String searchText;
118+
synchronized (searchingTextField) {
119+
searchText = searchingTextField.getText();
120+
}
121+
// start Search
122+
ArrayList<String> classesForName = searchUtil.methodsForName(searchText);
123+
124+
boolean equals;
125+
synchronized (searchingTextField) {
126+
equals =
127+
// if text has not changed
128+
searchingTextField.getText().equals(searchText);
129+
}
130+
131+
if (equals) {
132+
SwingUtilities.invokeLater(new Runnable() {
133+
134+
@Override
135+
public void run() {
136+
defaultTableModel.setRowCount(0);
137+
for (String string : classesForName) {
138+
defaultTableModel.addRow(new String[]{string});
139+
}
140+
defaultTableModel.fireTableDataChanged();
141+
}
142+
});
143+
}
144+
145+
// It is buggy with the synchronized searching
146+
// resultsTable.setRowSelectionInterval(0, 0);
147+
}
148+
});
149+
runningThread.start();
150+
}
151+
152+
public void setDelegate(MethodSearchFrameDelegateInterface delegate) {
153+
this.delegate = delegate;
154+
}
155+
156+
@Override
157+
public void changedUpdate(DocumentEvent e) {
158+
// TODO Auto-generated method stub
159+
160+
}
161+
162+
@Override
163+
public void insertUpdate(DocumentEvent e) {
164+
// TODO Auto-generated method stub
165+
updateMethodList();
166+
}
167+
168+
@Override
169+
public void removeUpdate(DocumentEvent e) {
170+
// TODO Auto-generated method stub
171+
updateMethodList();
172+
}
173+
174+
@Override
175+
public void actionPerformed(ActionEvent e) {
176+
// User cancel the class search
177+
178+
if (e.getSource() == btnCancel) {
179+
setVisible(false);
180+
dispose();
181+
} else if (e.getSource() == btnConfirm) {
182+
183+
// return the selected (or the first if no selected) class if the
184+
// table is not empty
185+
if (resultsTable.getRowCount() != 0) {
186+
187+
int selectedRow = resultsTable.getSelectedRow();
188+
if (selectedRow == -1) {
189+
selectedRow = 0;
190+
}
191+
// TODO get selected/highlighted class, not just the first one
192+
delegate.didSelectMethod(
193+
(String) resultsTable.getValueAt(selectedRow, 0));
194+
195+
}
196+
197+
setVisible(false);
198+
dispose();
199+
}
200+
}
201+
}

0 commit comments

Comments
 (0)