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