KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > confirmation > ConfirmationAC


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

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 /**
34  * The confirmation aspect implementation (allows the user to add
35  * confirmation popups before committing. */

36
37 public class ConfirmationAC
38     extends AspectComponent
39     implements ConfirmationConf
40 {
41     static final Logger logger = Logger.getLogger("confirmation");
42
43     static String JavaDoc cancellationMessage = "invocation was cancelled by the user";
44     public void confirm(String JavaDoc classes, String JavaDoc methods, String JavaDoc objects) {
45         pointcut(
46             objects,
47             classes,
48             methods,
49             new ConfirmationWrapper(this),
50             "catchCancellation");
51     }
52
53     public void confirm(String JavaDoc classes, String JavaDoc methods, String JavaDoc objects, String JavaDoc message) {
54         pointcut(
55             objects,
56             classes,
57             methods,
58             new ConfirmationWrapper(this,message),
59             "catchCancellation");
60     }
61
62     /**
63      * A confirmation wrapper that wraps methods to show a confirmation
64      * message box before actually performing the call. */

65
66     public class ConfirmationWrapper extends Wrapper {
67
68         String JavaDoc message;
69
70         public ConfirmationWrapper(AspectComponent ac, String JavaDoc 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 JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
81             return confirm((Interaction) invocation);
82         }
83
84         public Object JavaDoc construct(ConstructorInvocation invocation)
85             throws Throwable JavaDoc {
86             throw new Exception JavaDoc("This wrapper does not support constructor wrapping");
87         }
88
89         /** The wrapping method. */
90         public Object JavaDoc 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 JavaDoc 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         /** The exception handler. */
138         public void catchCancellation(OperationCanceledException e) {
139             System.out.println("Catching exception: " + e);
140         }
141
142     }
143
144 }
145
146 class OperationCanceledException extends Exception JavaDoc {
147     public OperationCanceledException(Interaction interaction) {
148         super("Operation cancelled: " + interaction.method);
149     }
150 }
151
Popular Tags