1 | |
package org.jtheque.core.managers.view.impl.components; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jdesktop.jxlayer.JXLayer; |
20 | |
import org.jdesktop.jxlayer.plaf.BufferedLayerUI; |
21 | |
import org.jtheque.core.managers.Managers; |
22 | |
import org.jtheque.core.managers.language.ILanguageManager; |
23 | |
import org.jtheque.core.managers.view.ViewComponent; |
24 | |
|
25 | |
import javax.swing.JComponent; |
26 | |
import javax.swing.JTabbedPane; |
27 | |
import javax.swing.Timer; |
28 | |
import javax.swing.event.ChangeEvent; |
29 | |
import javax.swing.event.ChangeListener; |
30 | |
import java.awt.Component; |
31 | |
import java.awt.Graphics; |
32 | |
import java.awt.Graphics2D; |
33 | |
import java.awt.event.ActionEvent; |
34 | |
import java.awt.event.ActionListener; |
35 | |
import java.awt.image.BufferedImage; |
36 | |
import java.util.ArrayList; |
37 | |
import java.util.List; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class LayerTabbedPane extends JTabbedPane implements ViewComponent { |
45 | |
private final List<JXLayer<JComponent>> components; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public LayerTabbedPane() { |
51 | 0 | super(); |
52 | |
|
53 | 0 | components = new ArrayList<JXLayer<JComponent>>(5); |
54 | |
|
55 | 0 | addChangeListener(new TabbedAnimatingChangeListener()); |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public final void addInternationalizedTab(String key, JComponent component) { |
65 | 0 | addLayeredTab(Managers.getManager(ILanguageManager.class).getMessage(key), component); |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public final void addLayeredTab(String title, JComponent component) { |
75 | 0 | JXLayer<JComponent> layerComponent = new JXLayer<JComponent>(component); |
76 | |
|
77 | 0 | components.add(layerComponent); |
78 | 0 | addTab(title, layerComponent); |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public final JComponent getSelectedComponent() { |
83 | 0 | if (getSelectedIndex() != -1) { |
84 | 0 | return components.get(getSelectedIndex()).getView(); |
85 | |
} |
86 | |
|
87 | 0 | return null; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
final JXLayer<JComponent> getSelectedLayer() { |
96 | 0 | JXLayer<JComponent> layer = null; |
97 | |
|
98 | 0 | if (getSelectedIndex() != -1) { |
99 | 0 | layer = components.get(getSelectedIndex()); |
100 | |
} |
101 | |
|
102 | 0 | return layer; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
final JXLayer<JComponent> getLayerAt(int index) { |
112 | 0 | return components.get(index); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | 0 | private static final class TabbedAnimatingChangeListener implements ChangeListener { |
121 | |
private int index; |
122 | |
private final Timer timer; |
123 | |
private final AnimationLayerUI layerUI; |
124 | |
private float delta; |
125 | |
|
126 | |
private static final int ANIMATION_DELAY = 50; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
private TabbedAnimatingChangeListener() { |
132 | 0 | super(); |
133 | |
|
134 | 0 | delta = .1f; |
135 | 0 | layerUI = new AnimationLayerUI(); |
136 | |
|
137 | 0 | timer = new Timer(ANIMATION_DELAY, new ActionListener() { |
138 | |
@Override |
139 | |
public void actionPerformed(ActionEvent e) { |
140 | |
|
141 | 0 | if (layerUI.getAlpha() <= delta) { |
142 | 0 | layerUI.setAlpha(0); |
143 | 0 | timer.stop(); |
144 | 0 | return; |
145 | |
} |
146 | |
|
147 | 0 | layerUI.setAlpha(layerUI.getAlpha() - delta); |
148 | 0 | } |
149 | |
}); |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public float getDelta() { |
158 | 0 | return delta; |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public void setDelta(float delta) { |
167 | 0 | if (delta <= 0 || delta > 1) { |
168 | 0 | throw new IllegalArgumentException(); |
169 | |
} |
170 | 0 | this.delta = delta; |
171 | 0 | } |
172 | |
|
173 | |
@Override |
174 | |
public void stateChanged(ChangeEvent e) { |
175 | 0 | LayerTabbedPane pane = (LayerTabbedPane) e.getSource(); |
176 | 0 | JXLayer<JComponent> layer = pane.getSelectedLayer(); |
177 | 0 | JXLayer<JComponent> oldLayer = pane.getLayerAt(index); |
178 | |
|
179 | 0 | if (oldLayer != null && layer != null) { |
180 | 0 | layerUI.setAlpha(1 - layerUI.getAlpha()); |
181 | |
|
182 | 0 | layerUI.setComponent(oldLayer); |
183 | |
|
184 | 0 | oldLayer.setUI(layer.getUI()); |
185 | 0 | layer.setUI(layerUI); |
186 | |
|
187 | |
|
188 | 0 | timer.start(); |
189 | 0 | index = pane.getSelectedIndex(); |
190 | |
} |
191 | 0 | } |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public Object getImpl() { |
196 | 0 | return this; |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | 0 | private static final class AnimationLayerUI extends BufferedLayerUI<JComponent> { |
205 | |
private BufferedImage componentImage; |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | 0 | private AnimationLayerUI() { |
211 | 0 | setAlpha(0); |
212 | 0 | setIncrementalUpdate(false); |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public void setComponent(Component component) { |
221 | 0 | if (component == null |
222 | |
|| component.getWidth() <= 0 || component.getHeight() <= 0) { |
223 | 0 | componentImage = null; |
224 | |
} else { |
225 | 0 | if (componentImage == null |
226 | |
|| componentImage.getWidth() != component.getWidth() |
227 | |
|| componentImage.getHeight() != component.getHeight()) { |
228 | 0 | componentImage = createBuffer(component.getWidth(), component.getHeight()); |
229 | |
} |
230 | 0 | Graphics g = componentImage.getGraphics(); |
231 | 0 | component.paint(g); |
232 | 0 | g.dispose(); |
233 | |
} |
234 | 0 | } |
235 | |
|
236 | |
@Override |
237 | |
public void paint(Graphics g, JComponent c) { |
238 | |
|
239 | 0 | c.paint(g); |
240 | 0 | super.paint(g, c); |
241 | 0 | } |
242 | |
|
243 | |
@Override |
244 | |
protected void paintLayer(Graphics2D graphics2D, JXLayer<? extends JComponent> jxLayer) { |
245 | 0 | if (componentImage != null) { |
246 | |
|
247 | 0 | graphics2D.drawImage(componentImage, 0, 0, null); |
248 | |
} |
249 | 0 | } |
250 | |
} |
251 | |
} |