001package net.sf.logdistiller.gui;
002
003/*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.awt.event.ActionEvent;
018import java.awt.event.ActionListener;
019import java.lang.reflect.*;
020import java.awt.event.ItemListener;
021import java.awt.event.ItemEvent;
022
023public class SwingAdapter
024{
025    private SwingAdapter()
026    {
027    }
028
029    public static ActionListener getActionListener( Object obj, String methodName )
030    {
031        try
032        {
033            Class[] paramTypes = { ActionEvent.class };
034            Method method = obj.getClass().getMethod( methodName, paramTypes );
035            return new ActionAdapter( obj, method );
036        }
037        catch ( NoSuchMethodException nsme )
038        {
039            throw new RuntimeException( "method 'public void " + methodName + "(ActionEvent ae)' not found in "
040                + obj.getClass().getName(), nsme );
041        }
042    }
043
044    public static Runnable getRunnable( Object obj, String methodName )
045    {
046        try
047        {
048            Class[] paramTypes = {};
049            Method method = obj.getClass().getMethod( methodName, paramTypes );
050            return new RunnableAdapter( obj, method );
051        }
052        catch ( NoSuchMethodException nsme )
053        {
054            throw new RuntimeException( "method 'public void " + methodName + "()' not found in "
055                + obj.getClass().getName(), nsme );
056        }
057    }
058
059    public static ItemListener getItemListener( Object obj, String methodName )
060    {
061        try
062        {
063            Class[] paramTypes = { ItemEvent.class };
064            Method method = obj.getClass().getMethod( methodName, paramTypes );
065            return new ItemAdapter( obj, method );
066        }
067        catch ( NoSuchMethodException nsme )
068        {
069            throw new RuntimeException( "method 'public void " + methodName + "(ItemEvent ie)' not found in "
070                + obj.getClass().getName(), nsme );
071        }
072    }
073
074    private static class ActionAdapter
075        implements ActionListener
076    {
077        private final Object target;
078
079        private final Method method;
080
081        public ActionAdapter( Object target, Method method )
082        {
083            this.target = target;
084            this.method = method;
085        }
086
087        public void actionPerformed( ActionEvent ae )
088        {
089            try
090            {
091                method.invoke( target, new Object[] { ae } );
092            }
093            catch ( InvocationTargetException ite )
094            {
095                throw new RunException( ite );
096            }
097            catch ( IllegalArgumentException iae )
098            {
099                throw new RuntimeException( "IllegalArgumentException invoking method '" + method.getName() + "'", iae );
100            }
101            catch ( IllegalAccessException iae )
102            {
103                throw new RuntimeException( "IllegalAccessException invoking method '" + method.getName() + "'", iae );
104            }
105        }
106    }
107
108    private static class RunnableAdapter
109        implements Runnable
110    {
111        private final Object target;
112
113        private final Method method;
114
115        public RunnableAdapter( Object target, Method method )
116        {
117            this.target = target;
118            this.method = method;
119        }
120
121        public void run()
122        {
123            try
124            {
125                method.invoke( target, new Object[] {} );
126            }
127            catch ( InvocationTargetException ite )
128            {
129                throw new RunException( ite );
130            }
131            catch ( IllegalArgumentException iae )
132            {
133                throw new RuntimeException( "IllegalArgumentException invoking method '" + method.getName() + "'", iae );
134            }
135            catch ( IllegalAccessException iae )
136            {
137                throw new RuntimeException( "IllegalAccessException invoking method '" + method.getName() + "'", iae );
138            }
139        }
140    }
141
142    private static class ItemAdapter
143        implements ItemListener
144    {
145        private final Object target;
146
147        private final Method method;
148
149        public ItemAdapter( Object target, Method method )
150        {
151            this.target = target;
152            this.method = method;
153        }
154
155        public void itemStateChanged( ItemEvent ie )
156        {
157            try
158            {
159                method.invoke( target, new Object[] { ie } );
160            }
161            catch ( InvocationTargetException ite )
162            {
163                throw new RunException( ite );
164            }
165            catch ( IllegalArgumentException iae )
166            {
167                throw new RuntimeException( "IllegalArgumentException invoking method '" + method.getName() + "'", iae );
168            }
169            catch ( IllegalAccessException iae )
170            {
171                throw new RuntimeException( "IllegalAccessException invoking method '" + method.getName() + "'", iae );
172            }
173        }
174    }
175
176    public static class RunException
177        extends RuntimeException
178    {
179        private static final long serialVersionUID = 4688792600015649872L;
180
181        private RunException( Exception nestedException )
182        {
183            super( nestedException );
184        }
185    }
186}