|
| 1 | +package actions; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | + |
| 5 | +import javax.swing.JOptionPane; |
| 6 | + |
| 7 | +import actions.menu.ActionsMenuBarTitles; |
| 8 | +import paintcomponents.java.lazy.ClassConstructorPaintComponent; |
| 9 | +import paintcomponents.java.lazy.ClassPaintComponent; |
| 10 | +import paintcomponents.java.lazy.MethodPaintComponent; |
| 11 | +import ui.PaintPanel; |
| 12 | + |
| 13 | +public class AddLazyJavaMethodComponentAction extends PaintAction { |
| 14 | + |
| 15 | + public AddLazyJavaMethodComponentAction(PaintPanel panel) { |
| 16 | + super(panel); |
| 17 | + } |
| 18 | + |
| 19 | + //TODO |
| 20 | + //NOTE: I am copying from Constructor, consider refinement |
| 21 | + @Override |
| 22 | + public boolean canPerformAction() { |
| 23 | + if (panel.getSelectTool().getSelectedComponents().size() != 1) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + if (panel.getSelectTool().getSelectedComponents() |
| 27 | + .get(0) instanceof ClassPaintComponent) { |
| 28 | + return true; |
| 29 | + } |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void performAction() { |
| 35 | + ClassPaintComponent comp = (ClassPaintComponent) panel.getSelectTool() |
| 36 | + .getSelectedComponents().get(0); |
| 37 | + Method[] methods = comp.getDisplayingClass().getMethods(); |
| 38 | + |
| 39 | + int desiaredConstructorIndex = Integer |
| 40 | + .parseInt(JOptionPane.showInputDialog( |
| 41 | + "Please enter the index of the constructor you would like to use: \n\n\n" |
| 42 | + + getMethodsSelectionUI(methods))); |
| 43 | + MethodPaintComponent methodComp = new MethodPaintComponent( |
| 44 | + methods[desiaredConstructorIndex], panel.getWidth() / 2, |
| 45 | + panel.getHeight() / 2); |
| 46 | + panel.addPaintComponent(methodComp); |
| 47 | + panel.repaint(); |
| 48 | + } |
| 49 | + public String getMethodsSelectionUI(Method[] methods) { |
| 50 | + StringBuilder builder = new StringBuilder(); |
| 51 | + for (int i = 0; i < methods.length; i++) { |
| 52 | + Method constructor = methods[i]; |
| 53 | + builder.append(i + " : " + constructor.toString() + "\n"); |
| 54 | + } |
| 55 | + return builder.toString(); |
| 56 | + |
| 57 | + } |
| 58 | + @Override |
| 59 | + public String locationString() { |
| 60 | + return ActionsMenuBarTitles.Lazy().Add().Java_Method().toString(); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments