Skip to content

Commit d0910b5

Browse files
author
x1gong
committed
change the method input interface
1 parent 0ff3ff4 commit d0910b5

5 files changed

Lines changed: 230 additions & 49 deletions

File tree

src/actions/AddLazyJavaMethodComponentAction.java

Lines changed: 18 additions & 21 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,21 +38,21 @@ public boolean canPerformAction() {
4138
@Override
4239
public void performAction() {
4340
ClassPaintComponent comp = (ClassPaintComponent) panel.getSelectTool().getSelectedComponents().get(0);
44-
Method[] methods = comp.getDisplayingClass().getMethods();
41+
42+
InputManager im = new InputManager();
43+
im.askForMethod(panel, new InputManagerDelegate<Method>() {
4544

46-
String desiaredConstructorIndex = JOptionPane.showInputDialog(
47-
"Please enter the index of the constructor you would like to use: \n\n\n"
45+
@Override
46+
public void didFinishInput(Method input) {
47+
AddLazyJavaMethodComponentGlobalAction assiciatedAction = (AddLazyJavaMethodComponentGlobalAction)
48+
ActionName.ADD_LAZY_JAVA_METHOD_ACTION.getAssiciatedAction();
49+
assiciatedAction.setMethodComponent(comp);
50+
assiciatedAction.setMethod(input);
51+
GlobalPaintActionExecuter.getSharedInstance().execute(assiciatedAction, panel);
52+
}
53+
}, comp.getDisplayingClass());
4854

49-
+ getMethodsSelectionUI(methods));
50-
//call DialogInputChecker to check input
51-
DialogInputChecker inputChecker = new DialogInputChecker();
52-
if(inputChecker.isValidNumber(desiaredConstructorIndex, 0, methods.length -1)){
53-
AddLazyJavaMethodComponentGlobalAction assiciatedAction
54-
= (AddLazyJavaMethodComponentGlobalAction) ActionName.ADD_LAZY_JAVA_METHOD_ACTION.getAssiciatedAction();
55-
assiciatedAction.setMethodComponent(comp);
56-
assiciatedAction.setMethod(comp.getDisplayingClass().getMethods()[Integer.parseInt(desiaredConstructorIndex)]);
57-
GlobalPaintActionExecuter.getSharedInstance().execute(assiciatedAction, panel);
58-
}
55+
5956
}
6057

6158
public String getMethodsSelectionUI(Method[] methods) {

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
}

src/ui/helper/methodinput/MethodInputFrame.java

Lines changed: 115 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,37 @@
44
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
66
import java.lang.reflect.Method;
7+
import java.util.ArrayList;
78

89
import javax.swing.DefaultListSelectionModel;
910
import javax.swing.JButton;
1011
import javax.swing.JFrame;
12+
import javax.swing.JOptionPane;
1113
import javax.swing.JPanel;
1214
import javax.swing.JScrollPane;
1315
import javax.swing.JTable;
1416
import javax.swing.JTextField;
1517
import javax.swing.ListSelectionModel;
18+
import javax.swing.SwingUtilities;
19+
import javax.swing.event.DocumentEvent;
20+
import javax.swing.event.DocumentListener;
1621
import javax.swing.table.DefaultTableModel;
1722

18-
19-
public class MethodInputFrame extends JFrame {
23+
/**
24+
* similar implementation as class search frame
25+
*/
26+
public class MethodInputFrame extends JFrame implements ActionListener, DocumentListener {
2027

2128
private JTextField searchingTextField;
2229
private JTable resultsTable;
30+
private MethodSearch searchUtil;
31+
private DefaultTableModel defaultTableModel;
32+
private MethodSearchFrameDelegateInterface delegate;
33+
34+
private Thread runningThread;
2335
private JButton btnCancel, btnConfirm;
2436
private JPanel panel;
2537
private JScrollPane scrollPane;
26-
private DefaultTableModel defaultTableModel;
27-
28-
private Class mtdClass;
29-
private Method[] methods;
3038

3139

3240
public MethodInputFrame(Class c) {
@@ -38,44 +46,42 @@ public boolean isCellEditable(int row, int column) {
3846
}
3947
};
4048

41-
this.mtdClass = c;
42-
this.methods = this.mtdClass.getMethods();
43-
4449
// big JPanel window
4550
panel = new JPanel();
4651
getContentPane().add(panel, BorderLayout.CENTER);
4752
panel.setLayout(new BorderLayout(0, 0));
48-
53+
54+
// search text field on top
55+
searchUtil = new MethodSearch(c);
4956
searchingTextField = new JTextField();
5057
getContentPane().add(searchingTextField, BorderLayout.NORTH);
5158
searchingTextField.setColumns(10);
52-
59+
5360
// show result
5461
resultsTable = new JTable();
5562
resultsTable.setModel(defaultTableModel);
5663
resultsTable.setSelectionModel(new ForcedListSelectionModel());
57-
64+
5865
// scroll option
5966
scrollPane = new JScrollPane(resultsTable);
6067
panel.add(scrollPane, BorderLayout.CENTER);
6168

6269
// add small JPanel for Cancel and Confirm buttons
6370
JPanel panel_1 = new JPanel();
6471
getContentPane().add(panel_1, BorderLayout.SOUTH);
65-
66-
ButtonListener listener = new ButtonListener();
67-
72+
6873
btnCancel = new JButton("Cancel");
6974
panel_1.add(btnCancel);
70-
btnCancel.addActionListener(listener);
75+
btnCancel.addActionListener(this);
7176

7277
btnConfirm = new JButton("Confirm");
7378
panel_1.add(btnConfirm);
74-
btnConfirm.addActionListener(listener);
79+
btnConfirm.addActionListener(this);
7580

7681
// set up connections
77-
// searchingTextField.addActionListener(this);
78-
// searchingTextField.getDocument().addDocumentListener(this);
82+
searchingTextField.addActionListener(this);
83+
searchingTextField.getDocument().addDocumentListener(this);
84+
7985
}
8086

8187

@@ -99,16 +105,97 @@ public ForcedListSelectionModel() {
99105
*/
100106
}
101107

102-
private class ButtonListener implements ActionListener {
103-
104-
@Override
105-
public void actionPerformed(ActionEvent e) {
106-
if (e.getSource() == btnCancel) {
107-
108-
} else if (e.getSource() == btnConfirm) {
109-
110-
}
108+
private void updateMethodList() {
109+
if (runningThread != null) {
110+
runningThread.interrupt();
111111
}
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
112159

113160
}
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+
}
114201
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ui.helper.methodinput;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.ArrayList;
5+
6+
public class MethodSearch {
7+
8+
private ArrayList<String> allMethods;
9+
10+
/**
11+
* Constructs a MethodSearch Instance
12+
* The method
13+
*/
14+
public MethodSearch(Class mtdClass) {
15+
//load classes
16+
allMethods = new ArrayList<>();
17+
18+
Method[] methods = mtdClass.getMethods();
19+
20+
for (Method mtd : methods) {
21+
allMethods.add(mtd.toString());
22+
}
23+
24+
}
25+
26+
public ArrayList<String> methodsForName(String name) {
27+
28+
ArrayList<String> result = new ArrayList<>();
29+
for (String string : allMethods) {
30+
if(string.toUpperCase().contains(name.toUpperCase())){
31+
result.add(string);
32+
}
33+
}
34+
35+
return sortAccordingToPrecedence(result, name);
36+
}
37+
38+
private ArrayList<String> sortAccordingToPrecedence(ArrayList<String> result,
39+
String name) {
40+
ArrayList<String> ret = new ArrayList<>();
41+
42+
43+
for (String string : result) {
44+
String[] comps = string.split("\\.");
45+
if(comps.length == 0) continue;
46+
47+
if(comps[comps.length - 1].startsWith(name)){
48+
ret.add(0, string);
49+
} else {
50+
ret.add(string);
51+
}
52+
}
53+
54+
return ret;
55+
}
56+
57+
58+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ui.helper.methodinput;
2+
3+
public interface MethodSearchFrameDelegateInterface {
4+
public void didSelectMethod(String methodName);
5+
}

0 commit comments

Comments
 (0)