1 17 18 package org.objectweb.jac.aspects.confirmation; 19 20 import org.aopalliance.intercept.ConstructorInvocation; 21 import org.aopalliance.intercept.MethodInvocation; 22 import org.apache.log4j.Logger; 23 import org.objectweb.jac.aspects.gui.DisplayContext; 24 import org.objectweb.jac.aspects.gui.GuiAC; 25 import org.objectweb.jac.core.AspectComponent; 26 import org.objectweb.jac.core.Display; 27 import org.objectweb.jac.core.Interaction; 28 import org.objectweb.jac.core.Wrapper; 29 import org.objectweb.jac.core.rtti.AbstractMethodItem; 30 import org.objectweb.jac.core.rtti.ClassRepository; 31 import org.objectweb.jac.core.rtti.NamingConventions; 32 33 36 37 public class ConfirmationAC 38 extends AspectComponent 39 implements ConfirmationConf 40 { 41 static final Logger logger = Logger.getLogger("confirmation"); 42 43 static String cancellationMessage = "invocation was cancelled by the user"; 44 public void confirm(String classes, String methods, String objects) { 45 pointcut( 46 objects, 47 classes, 48 methods, 49 new ConfirmationWrapper(this), 50 "catchCancellation"); 51 } 52 53 public void confirm(String classes, String methods, String objects, String message) { 54 pointcut( 55 objects, 56 classes, 57 methods, 58 new ConfirmationWrapper(this,message), 59 "catchCancellation"); 60 } 61 62 65 66 public class ConfirmationWrapper extends Wrapper { 67 68 String message; 69 70 public ConfirmationWrapper(AspectComponent ac, String message) { 71 super(ac); 72 this.message = message; 73 } 74 75 public ConfirmationWrapper(AspectComponent ac) { 76 super(ac); 77 this.message = null; 78 } 79 80 public Object invoke(MethodInvocation invocation) throws Throwable { 81 return confirm((Interaction) invocation); 82 } 83 84 public Object construct(ConstructorInvocation invocation) 85 throws Throwable { 86 throw new Exception ("This wrapper does not support constructor wrapping"); 87 } 88 89 90 public Object confirm(Interaction interaction) 91 throws OperationCanceledException 92 { 93 logger.debug("confirm " + interaction); 94 DisplayContext context = 95 (DisplayContext) this.attr(GuiAC.DISPLAY_CONTEXT); 96 logger.debug(" context=" + context); 97 AbstractMethodItem method = interaction.method; 98 if (context != null) { 99 Display display = context.getDisplay(); 100 String actualMessage = message; 101 if (actualMessage == null) { 102 if (method.isRemover()) { 103 actualMessage = 104 "Do you really want to remove " 105 + NamingConventions.textForName( 106 ClassRepository.get().getClass(interaction.args[0]).getShortName()) 107 + " '" 108 + GuiAC.toString(interaction.args[0]) 109 + "' from " 110 + NamingConventions.textForName( 111 method.getRemovedCollections()[0].getName()) 112 + " of " 113 + NamingConventions.textForName( 114 interaction.getClassItem().getShortName()) 115 + " '" 116 + GuiAC.toString(interaction.wrappee) 117 + "' ?"; 118 } else { 119 actualMessage = 120 "Do you really want to " 121 + NamingConventions.textForName( 122 interaction.method.getName()) 123 + (interaction.method.isStatic() 124 ? "" 125 : (" for '" + GuiAC.toString(interaction.wrappee)) + "'") 126 + " ?"; 127 } 128 } 129 130 if (!display.showMessage(actualMessage, "Confirmation", true, true, false)) { 131 throw new OperationCanceledException(interaction); 132 } 133 } 134 return interaction.proceed(); 135 } 136 137 138 public void catchCancellation(OperationCanceledException e) { 139 System.out.println("Catching exception: " + e); 140 } 141 142 } 143 144 } 145 146 class OperationCanceledException extends Exception { 147 public OperationCanceledException(Interaction interaction) { 148 super("Operation cancelled: " + interaction.method); 149 } 150 } 151 | Popular Tags |