4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.subtitlesadjuster
;
27 import static net
.sourceforge
.aprog
.i18n
.Messages
.*;
28 import static net
.sourceforge
.aprog
.tools
.Tools
.*;
30 import java
.lang
.reflect
.InvocationHandler
;
31 import java
.lang
.reflect
.Method
;
32 import java
.lang
.reflect
.Proxy
;
34 import javax
.swing
.JMenuItem
;
35 import javax
.swing
.KeyStroke
;
37 import net
.sourceforge
.aprog
.context
.Context
;
38 import net
.sourceforge
.aprog
.events
.Variable
;
39 import net
.sourceforge
.aprog
.events
.Variable
.Listener
;
40 import net
.sourceforge
.aprog
.swing
.SwingTools
;
41 import net
.sourceforge
.aprog
.tools
.Tools
;
42 import net
.sourceforge
.jmacadapter
.MacAdapterTools
;
46 * <br>If it turns out that some methods defined here are reused in other projects,
47 * then they could be moved directly into Aprog.
49 * @author codistmonk (creation 2010-06-27)
51 public final class SubtitlesAdjusterTools
{
54 * Private default constructor to prevent instantiation.
56 private SubtitlesAdjusterTools() {
60 public static final String META
= MacAdapterTools
.isMacOSX() ?
"meta" : "control";
68 * @param objectOrClass
78 @SuppressWarnings("unchecked")
79 public static final void invokeOnVariableChanged(final Context context
, final String variableName
,
80 final Object objectOrClass
, final String methodName
, final Object
... arguments
) {
81 final Variable
<Object
> variable
= context
.getVariable(variableName
);
83 variable
.addListener(newListener(Listener
.class, "valueChanged",
84 Tools
.class, "invoke", objectOrClass
, methodName
, arguments
));
86 Tools
.invoke(objectOrClass
, methodName
, arguments
);
91 * @param translationKey
104 public static final JMenuItem
item(final String translationKey
,
105 final String methodName
, final Object
... arguments
) {
106 return translate(new JMenuItem(
107 SwingTools
.action(Actions
.class, methodName
, arguments
)
108 .setName(translationKey
)));
113 * @param translationKey
129 public static final JMenuItem
item(final String translationKey
, final KeyStroke shortcut
,
130 final String methodName
, final Object
... arguments
) {
131 return translate(new JMenuItem(
132 SwingTools
.action(Actions
.class, methodName
, arguments
)
133 .setName(translationKey
)
134 .setShortcut(shortcut
)));
139 * @param <L> The listener type
140 * @param listenerClass
142 * @param listenerMethodName
144 * @param objectOrClass
154 @SuppressWarnings("unchecked")
155 public static final <L
> L
newListener(final Class
<L
> listenerClass
, final String listenerMethodName
,
156 final Object objectOrClass
, final String methodName
, final Object
... arguments
) {
157 return (L
) Proxy
.newProxyInstance(getCallerClass().getClassLoader(), array(listenerClass
),
158 new ListenerInvocationHandler(listenerMethodName
, objectOrClass
, methodName
, arguments
));
163 * @author codistmonk (creation 2010-07-03)
165 public static final class ListenerInvocationHandler
implements InvocationHandler
{
167 private final String listenerMethodName
;
169 private final Object objectOrClass
;
171 private final String methodName
;
173 private final Object
[] arguments
;
177 * @param listenerMethodName
180 * @param objectOrClass
190 public ListenerInvocationHandler(final String listenerMethodName
,
191 final Object objectOrClass
, final String methodName
, final Object
... arguments
) {
192 this.listenerMethodName
= listenerMethodName
;
193 this.objectOrClass
= objectOrClass
;
194 this.methodName
= methodName
;
195 this.arguments
= arguments
;
199 public final Object
invoke(final Object proxy
, final Method method
, final Object
[] arguments
) throws Throwable
{
202 // "\n", method.getDeclaringClass(),
203 // "\n", method.getDeclaringClass().isAssignableFrom(this.getClass()));
205 if (method
.getName().equals(this.listenerMethodName
)) {
206 return Tools
.invoke(this.objectOrClass
, this.methodName
, this.arguments
);
207 } else if (method
.getDeclaringClass().isAssignableFrom(this.getClass())) {
208 return method
.invoke(this, arguments
);
215 public final boolean equals(final Object object
) {
216 return this == object
||
218 Proxy
.isProxyClass(object
.getClass()) &&
219 this == Proxy
.getInvocationHandler(object
);
223 public final int hashCode() {
224 return super.hashCode();