|
| 1 | +package actions.singleinstanceoperations; |
| 2 | + |
| 3 | +import actions.PaintAction; |
| 4 | +import paintcomponents.PaintComponent; |
| 5 | +import ui.PaintPanel; |
| 6 | + |
| 7 | +/** |
| 8 | + * Single Instance Operation defines operation on a single instance. |
| 9 | + * |
| 10 | + * i.e., |
| 11 | + * 1. |
| 12 | + * One and Only One Instance must be selected to perform operaion. |
| 13 | + * |
| 14 | + * 2. The |
| 15 | + * operation only changes the instance itself but nothing else |
| 16 | + * |
| 17 | + * |
| 18 | + * After updating, the subclass do not need to call paintcomponent.repaint method |
| 19 | + * |
| 20 | + * |
| 21 | + * @author chenzb |
| 22 | + * |
| 23 | + */ |
| 24 | +public abstract class SingleInstanceOperation<T> extends PaintAction { |
| 25 | + |
| 26 | + public SingleInstanceOperation(PaintPanel panel) { |
| 27 | + super(panel); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean canPerformAction() { |
| 32 | + if (panel.getSelectTool().getSelectedComponents().size() != 1) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + PaintComponent paintComponent = panel.getSelectTool() |
| 36 | + .getSelectedComponents().get(0); |
| 37 | + |
| 38 | + |
| 39 | + //return if the object is kind of the desired class |
| 40 | + return getGenericClassType().isInstance(paintComponent); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void performAction() { |
| 46 | + // TODO Auto-generated method stub |
| 47 | + @SuppressWarnings("unchecked") |
| 48 | + T comp = (T) panel.getSelectTool().getSelectedComponents().get(0); |
| 49 | + performActionOnInstance(comp); |
| 50 | + panel.repaint(); |
| 51 | + } |
| 52 | + |
| 53 | + protected abstract void performActionOnInstance(T instance); |
| 54 | + |
| 55 | + /** |
| 56 | + * Due to constraint in Java API, subclass MUST override this method |
| 57 | + * |
| 58 | + * with method body :: <code>return T.class;</code> |
| 59 | + * @return |
| 60 | + */ |
| 61 | + protected abstract Class<T> getGenericClassType(); |
| 62 | + |
| 63 | + |
| 64 | +} |
0 commit comments