Skip to content

Commit 1d3cd6b

Browse files
authored
Merge pull request #132 from UCSDOalads/changeFieldsInputWindowByXYG
Change the Method Input Frame
2 parents 1c126e4 + da27d99 commit 1d3cd6b

5 files changed

Lines changed: 315 additions & 25 deletions

File tree

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/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+
}
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)