|
| 1 | +package actions; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | + |
| 5 | +import javax.swing.JOptionPane; |
| 6 | + |
| 7 | +import actions.edit.undoredo.SharedUndoRedoActionManager; |
| 8 | +import actions.edit.undoredo.UndoRedoableInterface; |
| 9 | +import actions.menu.ActionsMenuBarTitles; |
| 10 | + |
| 11 | +import paintcomponents.java.interactive.ClassConstructorPaintComponent; |
| 12 | +import paintcomponents.java.interactive.MethodPaintComponent; |
| 13 | +import ui.PaintPanel; |
| 14 | + |
| 15 | +public class AddInteractiveJavaMethodComponentAction extends PaintAction { |
| 16 | + |
| 17 | + public AddInteractiveJavaMethodComponentAction(PaintPanel panel) { |
| 18 | + super(panel); |
| 19 | + } |
| 20 | + |
| 21 | + //TODO |
| 22 | + //NOTE: I am copying from Constructor, consider refinement |
| 23 | + |
| 24 | + @Override |
| 25 | + public boolean canPerformAction() { |
| 26 | + if (panel.getSelectTool().getSelectedComponents().size() != 1) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + if (panel.getSelectTool().getSelectedComponents() |
| 30 | + .get(0) instanceof ClassConstructorPaintComponent) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + @Override |
| 38 | + public void performAction() { |
| 39 | + ClassConstructorPaintComponent comp = (ClassConstructorPaintComponent) panel.getSelectTool() |
| 40 | + .getSelectedComponents().get(0); |
| 41 | + Method[] methods = comp.getDisplayingClass().getMethods(); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + int desiaredConstructorIndex = Integer |
| 47 | + .parseInt(JOptionPane.showInputDialog( |
| 48 | + "Please enter the index of the constructor you would like to use: \n\n\n" |
| 49 | + + getMethodsSelectionUI(methods))); |
| 50 | + MethodPaintComponent methodComp = new MethodPaintComponent( |
| 51 | + methods[desiaredConstructorIndex], comp.getInstance(), panel.getWidth() / 2, |
| 52 | + panel.getHeight() / 2); |
| 53 | + panel.addPaintComponent(methodComp); |
| 54 | + // add action to undo redo manager |
| 55 | + SharedUndoRedoActionManager.getSharedInstance().pushUndoableAction(new UndoRedoableInterface() { |
| 56 | + |
| 57 | + @Override |
| 58 | + public void undoAction() { |
| 59 | + methodComp.remove(panel); |
| 60 | + panel.repaint(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void redoAction() { |
| 65 | + panel.addPaintComponent(methodComp); |
| 66 | + panel.repaint(); |
| 67 | + } |
| 68 | + }); |
| 69 | + panel.repaint(); |
| 70 | + } |
| 71 | + public String getMethodsSelectionUI(Method[] methods) { |
| 72 | + StringBuilder builder = new StringBuilder(); |
| 73 | + for (int i = 0; i < methods.length; i++) { |
| 74 | + Method constructor = methods[i]; |
| 75 | + builder.append(i + " : " + constructor.toString() + "\n"); |
| 76 | + } |
| 77 | + return builder.toString(); |
| 78 | + |
| 79 | + } |
| 80 | + @Override |
| 81 | + public String locationString() { |
| 82 | + return ActionsMenuBarTitles.Developer("Interactive/Method").toString(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments