View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.ui.component.task;
20  
21  import java.awt.event.ActionEvent;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.lang.reflect.InvocationTargetException;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  
29  import net.sf.magicproject.clickable.targetable.card.MCard;
30  import net.sf.magicproject.tools.MToolKit;
31  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
32  
33  import org.apache.commons.lang.WordUtils;
34  
35  /***
36   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37   * @since 0.90
38   */
39  public abstract class TaskAction extends AbstractAction {
40  
41  	/***
42  	 * The action title. The title will be translated with
43  	 * {@link net.sf.magicproject.ui.i18n.LanguageManagerMDB }.
44  	 */
45  	protected String title;
46  
47  	/***
48  	 * The associated propety value. This property will interpreted by a JavaBean.
49  	 */
50  	private String propertyKey;
51  
52  	/***
53  	 * The card currently used by this action.
54  	 */
55  	private MCard card;
56  
57  	/***
58  	 * @param dbStream
59  	 *          the stream containg the definition of this action.
60  	 * @throws IOException
61  	 *           if error occurred during the reading process from the specified
62  	 *           input stream
63  	 */
64  	protected TaskAction(final InputStream dbStream) throws IOException {
65  		super();
66  		this.title = LanguageManagerMDB.getString(MToolKit.readString(dbStream));
67  		super.putValue(Action.NAME, "<html><b>" + title + "<b> : -");
68  		this.propertyKey = MToolKit.readString(dbStream);
69  	}
70  
71  	public abstract void actionPerformed(ActionEvent e);
72  
73  	/***
74  	 * Revalidate this action with the given card.
75  	 * 
76  	 * @param card
77  	 *          the new card used to revalidate content of this action.
78  	 */
79  	public final void revalidate(MCard card) {
80  		this.card = card;
81  		try {
82  			setValue(processJavaBean(this, propertyKey));
83  		} catch (Exception e) {
84  			e.printStackTrace();
85  		}
86  	}
87  
88  	private static String processJavaBean(Object object, String propertyKey)
89  			throws IllegalAccessException, InvocationTargetException,
90  			NoSuchMethodException {
91  		if (object == null) {
92  			return "-";
93  		}
94  		int indexSep = propertyKey.indexOf('.');
95  		if (propertyKey.length() > 0) {
96  			if (indexSep == -1) {
97  				indexSep = propertyKey.length();
98  			}
99  			final int indexParam = propertyKey.indexOf('[');
100 
101 			if (indexParam != -1 && indexSep > indexParam) {
102 				// at least on parameter
103 				final int indexParam2 = propertyKey.indexOf(']');
104 				final String parameter = propertyKey.substring(indexParam + 1,
105 						indexParam2);
106 				return processJavaBean(object.getClass().getMethod(
107 						getGetter(propertyKey.substring(0, indexParam)), String.class)
108 						.invoke(object, parameter), propertyKey.substring(indexParam2 + 1));
109 			}
110 
111 			// no parameter
112 			return processJavaBean(object.getClass().getMethod(
113 					getGetter(propertyKey.substring(0, indexSep))).invoke(object),
114 					propertyKey.substring(indexSep >= propertyKey.length() ? indexSep
115 							: indexSep + 1));
116 		}
117 		return object.toString();
118 	}
119 
120 	/***
121 	 * Return a getter method : <br>
122 	 * getGetter(&quot;&quot;) = &quot;get&quot;<br>
123 	 * getGetter(*) = &quot;get*&quot;<br>
124 	 * getGetter(&quot;i-aM-fine&quot;) = &quot;getIAmFine&quot;<br>
125 	 * 
126 	 * @param property
127 	 * @return return a getter method
128 	 */
129 	private static String getGetter(String property) {
130 		return "get"
131 				+ WordUtils.capitalizeFully(property, new char[] { '-' }).replace("-",
132 						"");
133 	}
134 
135 	/***
136 	 * Return the card currently used by this action.<br>
137 	 * Unreferenced method, but called with reflection.
138 	 * 
139 	 * @return the card currently used by this action.
140 	 */
141 	public MCard getCard() {
142 		return card;
143 	}
144 
145 	/***
146 	 * Set a new value to this action. This action will be isplayed as
147 	 * <code>title : <param>htmlValue</param></code>
148 	 * 
149 	 * @param htmlValue
150 	 *          the new value to set to this action.
151 	 */
152 	protected void setValue(String htmlValue) {
153 		putValue(Action.NAME, "<html><b>" + title + " : </b>" + htmlValue);
154 	}
155 }