1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui.component;
20
21 import java.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.lang.reflect.InvocationTargetException;
27
28 import javax.swing.Action;
29 import javax.swing.JButton;
30 import javax.swing.JComponent;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33
34 import net.sf.magicproject.clickable.targetable.card.MCard;
35 import net.sf.magicproject.tools.MToolKit;
36 import net.sf.magicproject.ui.component.task.TaskAction;
37 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
38
39 import org.apache.commons.lang.StringUtils;
40
41 import com.l2fprod.common.swing.JCollapsiblePane;
42 import com.l2fprod.common.swing.JTaskPane;
43 import com.l2fprod.common.swing.JTaskPaneGroup;
44
45 /***
46 * Panel containing the card properties. This panel use the lf2prod task
47 * component. The layout of this component is defined into the tbs XML
48 * definition.<br>
49 * For manual modification of TaskPane properties :<br>
50 * Please see http://common.l2fprod.com <br>
51 * UIManager.put("TaskPane.useGradient", Boolean.TRUE); <br>
52 * UIManager.put("TaskPane.backgroundGradientStart", Color.green.darker()); <br>
53 * UIManager.put("TaskPane.backgroundGradientEnd",
54 * Color.green.darker().darker())<br>;
55 * TaskPaneGroup.titleBackgroundGradientStart; <br>
56 * TaskPaneGroup.specialTitleForeground; <br>
57 * TaskPaneGroup.titleBackgroundGradientEnd; <br>
58 * TaskPaneGroup.background; <br>
59 * TaskPaneGroup.titleForeground; <br>
60 *
61 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
62 * @since 0.90
63 */
64 public class CardPropertiesPanel extends JPanel {
65
66 /***
67 * Nested element type.
68 */
69 public static final int NESTED_ELEMENT = 0;
70
71 /***
72 * Attribute element type.
73 */
74 public static final int ATTRIBUTE = 1;
75
76 /***
77 * Create an empty task panel with a main title.
78 */
79 public CardPropertiesPanel() {
80 super();
81 setLayout(new BorderLayout());
82 taskPane = new JTaskPane();
83 add("Center", new JScrollPane(taskPane));
84
85
86
87
88
89
90 }
91
92 /***
93 * Read and add from the given input stream the content of this panel.
94 * <p>
95 * <ul>
96 * Structure of Stream : Data[size]
97 * <li>nb nodes [1]</li>
98 * <li>node type i [1]</li>
99 * <li>element/attribute i [...]</li>
100 * </ul>
101 * <p>
102 * <ul>
103 * Structure of Element : Data[size]
104 * <li>element title [1]</li>
105 * <li>nb nodes [1]</li>
106 * <li>node type i [1]</li>
107 * <li>element/attribute i [...]</li>
108 * </ul>
109 * <p>
110 * <ul>
111 * Structure of Attribute : Data[size]
112 * <li>attribute name + '\0' [...]</li>
113 * <li>nb nodes [1]</li>
114 * <li>node type i [1]</li>
115 * <li>element/attribute i [...]</li>
116 * </ul>
117 * <p>
118 *
119 * @param dbStream
120 * the source srteam.
121 */
122 public void init(InputStream dbStream) {
123 try {
124 fillTaskPane(taskPane, dbStream);
125 } catch (Exception e) {
126 throw new RuntimeException("Error reading taskPanel properties", e);
127 }
128 taskPane.revalidate();
129 }
130
131 /***
132 * Read and add from the given input stream the content of the given panel.
133 *
134 * @param parent
135 * the panel were the read content would be added.
136 * @param dbStream
137 * the source srteam.
138 * @throws Exception
139 * If some I/O or reflection error occurs.
140 */
141 private void fillTaskPane(final JComponent parent, final InputStream dbStream)
142 throws IOException, InstantiationException, IllegalAccessException,
143 InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
144 final int count = dbStream.read();
145 for (int i = 0; i < count; i++) {
146 final int elementType = dbStream.read();
147 switch (elementType) {
148 case NESTED_ELEMENT:
149 final JTaskPaneGroup nested = new JTaskPaneGroup();
150 nested.setTitle(LanguageManagerMDB.getString(MToolKit
151 .readString(dbStream)));
152 fillTaskPane(nested, dbStream);
153 parent.add(nested);
154 break;
155 case ATTRIBUTE:
156 final Action action = (Action) Class.forName(
157 new StringBuilder(TaskAction.class.getPackage().getName()).append(
158 ".").append(
159 StringUtils.capitalize(MToolKit.readString(dbStream))).append(
160 "Action").toString()).getConstructor(
161 new Class[] { InputStream.class }).newInstance(
162 new Object[] { dbStream });
163 ((JTaskPaneGroup) parent).add(action);
164 break;
165 default:
166 throw new RuntimeException("unknown element type : " + elementType);
167 }
168 }
169
170 }
171
172 /***
173 * Revalidate this task Pane with the given card.
174 *
175 * @param card
176 * the card used to revalidate the content of thise task Pane.
177 */
178 public void revalidate(MCard card) {
179 if (card != this.card) {
180 final Component[] nesteds = this.taskPane.getComponents();
181 for (Object nested : nesteds) {
182 if (nested instanceof JTaskPaneGroup) {
183 revalidate(card, (JTaskPaneGroup) nested);
184 }
185 }
186 }
187 }
188
189 /***
190 * Revalidate the given taskPane with the given card.
191 *
192 * @param card
193 * the card used to revalidate the content of the
194 * <param>taskPaneGroup</param>.
195 * @param taskPaneGroup
196 * the task pane to revalidate.
197 */
198 private void revalidate(MCard card, JTaskPaneGroup taskPaneGroup) {
199 final Component[] nesteds = ((Container) ((Container) ((JCollapsiblePane) taskPaneGroup
200 .getComponent(0)).getComponent(0)).getComponent(0)).getComponents();
201 for (int i = nesteds.length; i-- > 0;) {
202 if (nesteds[i] instanceof JTaskPaneGroup) {
203 revalidate(card, (JTaskPaneGroup) nesteds[i]);
204 } else if (nesteds[i] instanceof JButton) {
205 ((TaskAction) ((JButton) nesteds[i]).getAction()).revalidate(card);
206 }
207 ((JComponent) nesteds[i]).revalidate();
208 }
209 this.card = card;
210 }
211
212 /***
213 * Return the last card used to display the content of this task pane.
214 *
215 * @return the last card used to display the content of this task pane.
216 */
217 public MCard getCard() {
218 return card;
219 }
220
221 /***
222 * The task pane root.
223 */
224 private JTaskPane taskPane;
225
226 /***
227 * The last card used to display the content of this task pane.
228 */
229 private MCard card = null;
230
231 }