Debian Codegraph @

substance-swingx/src/main/java/org/pushingpixels/substance/swingx/SubstanceTaskPaneUI.java

raw symbols SCIP
1 /*
2 * Copyright 2005-2010 Kirill Grouchnikov, based on work by
3 * Sun Microsystems, Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 package org.pushingpixels.substance.swingx;
20
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.Insets;
26 import java.awt.Rectangle;
27 import java.awt.event.MouseEvent;
28 import java.util.Map;
29
30 import javax.swing.ButtonModel;
31 import javax.swing.DefaultButtonModel;
32 import javax.swing.Icon;
33 import javax.swing.JComponent;
34 import javax.swing.SwingConstants;
35 import javax.swing.border.Border;
36 import javax.swing.border.CompoundBorder;
37 import javax.swing.border.EmptyBorder;
38 import javax.swing.plaf.ComponentUI;
39
40 import org.jdesktop.swingx.JXTaskPane;
41 import org.jdesktop.swingx.icon.EmptyIcon;
42 import org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI;
43 import org.pushingpixels.lafwidget.LafWidgetUtilities;
44 import org.pushingpixels.substance.api.ColorSchemeAssociationKind;
45 import org.pushingpixels.substance.api.ComponentState;
46 import org.pushingpixels.substance.api.DecorationAreaType;
47 import org.pushingpixels.substance.api.SubstanceColorScheme;
48 import org.pushingpixels.substance.api.SubstanceLookAndFeel;
49 import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
50 import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
51 import org.pushingpixels.substance.internal.painter.DecorationPainterUtils;
52 import org.pushingpixels.substance.internal.painter.HighlightPainterUtils;
53 import org.pushingpixels.substance.internal.utils.RolloverControlListener;
54 import org.pushingpixels.substance.internal.utils.SubstanceColorSchemeUtilities;
55 import org.pushingpixels.substance.internal.utils.SubstanceColorUtilities;
56 import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
57 import org.pushingpixels.substance.internal.utils.SubstanceImageCreator;
58 import org.pushingpixels.substance.internal.utils.SubstanceSizeUtils;
59 import org.pushingpixels.substance.internal.utils.SubstanceTextUtilities;
60 import org.pushingpixels.substance.internal.utils.icon.TransitionAwareIcon;
61
62 /**
63 * Substance-consistent UI delegate for {@link JXTaskPane}.
64 *
65 * @author Kirill Grouchnikov
66 */
67 public class SubstanceTaskPaneUI extends BasicTaskPaneUI implements
68 TransitionAwareUI {
69 /**
70 * Background delegate.
71 */
72 private SubstanceSwingxFillBackgroundDelegate bgDelegate;
73
74 /**
75 * Listener for thumb transition animations.
76 */
77 private RolloverControlListener substanceRolloverListener;
78
79 /**
80 * Listener for transition animations.
81 */
82 protected StateTransitionTracker stateTransitionTracker;
83
84 /**
85 * Surrogate model for the fade effects on the title pane border.
86 */
87 protected ButtonModel taskPaneModel;
88
89 public static ComponentUI createUI(JComponent comp) {
90 SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
91 return new SubstanceTaskPaneUI((JXTaskPane) comp);
92 }
93
94 /**
95 * Creates a new UI delegate.
96 */
97 public SubstanceTaskPaneUI(JXTaskPane taskPane) {
98 super();
99 this.bgDelegate = new SubstanceSwingxFillBackgroundDelegate();
100
101 this.taskPaneModel = new DefaultButtonModel();
102 this.taskPaneModel.setArmed(false);
103 this.taskPaneModel.setSelected(false);
104 this.taskPaneModel.setPressed(false);
105 this.taskPaneModel.setRollover(false);
106
107 this.stateTransitionTracker = new StateTransitionTracker(taskPane,
108 this.taskPaneModel);
109 }
110
111 /*
112 * (non-Javadoc)
113 *
114 * @see org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI#installListeners()
115 */
116 @Override
117 protected void installListeners() {
118 super.installListeners();
119
120 this.substanceRolloverListener = new RolloverControlListener(this,
121 this.taskPaneModel);
122 this.group.addMouseListener(this.substanceRolloverListener);
123 this.group.addMouseMotionListener(this.substanceRolloverListener);
124
125 this.stateTransitionTracker.registerModelListeners();
126 }
127
128 /*
129 * (non-Javadoc)
130 *
131 * @see org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI#uninstallListeners()
132 */
133 @Override
134 protected void uninstallListeners() {
135 this.group.removeMouseListener(this.substanceRolloverListener);
136 this.group.removeMouseMotionListener(this.substanceRolloverListener);
137 this.substanceRolloverListener = null;
138
139 this.stateTransitionTracker.unregisterModelListeners();
140
141 super.uninstallListeners();
142 }
143
144 /*
145 * (non-Javadoc)
146 *
147 * @see org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI#installDefaults()
148 */
149 @Override
150 protected void installDefaults() {
151 SubstanceLookAndFeel.setDecorationType(this.group,
152 DecorationAreaType.GENERAL);
153
154 super.installDefaults();
155 }
156
157 /*
158 * (non-Javadoc)
159 *
160 * @see
161 * org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI#uninstallUI(javax.swing
162 * .JComponent)
163 */
164 @Override
165 public void uninstallUI(JComponent c) {
166 DecorationPainterUtils.clearDecorationType(this.group);
167 super.uninstallUI(c);
168 }
169
170 /*
171 * (non-Javadoc)
172 *
173 * @see org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI#createPaneBorder()
174 */
175 @Override
176 protected Border createPaneBorder() {
177 return new SubstancePaneBorder();
178 }
179
180 /*
181 * (non-Javadoc)
182 *
183 * @see
184 * org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI#createContentPaneBorder()
185 */
186 @Override
187 protected Border createContentPaneBorder() {
188 SubstanceColorScheme colorScheme = SubstanceColorSchemeUtilities
189 .getColorScheme(group, ColorSchemeAssociationKind.BORDER,
190 ComponentState.ENABLED);
191 // the code below is the same as for the separators
192 final Color borderColor = colorScheme.isDark() ? colorScheme
193 .getDarkColor() : SubstanceColorUtilities.getInterpolatedColor(
194 colorScheme.getMidColor(), colorScheme.getDarkColor(), 0.4);
195 Border outer = new Border() {
196 @Override
197 public void paintBorder(Component c, Graphics g, int x, int y,
198 int width, int height) {
199 Graphics2D g2d = (Graphics2D) g.create();
200
201 g2d.setColor(borderColor);
202 // left
203 g2d.drawLine(x, y, x, y + height - 1);
204 // right
205 g2d.drawLine(x + width - 1, y, x + width - 1, y + height - 1);
206 // bottom
207 g2d.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
208
209 g2d.dispose();
210 }
211
212 @Override
213 public boolean isBorderOpaque() {
214 return true;
215 }
216
217 @Override
218 public Insets getBorderInsets(Component c) {
219 // don't paint at the top
220 return new Insets(0, 1, 1, 1);
221 }
222 };
223 return new CompoundBorder(outer, new EmptyBorder(1, 2, 2, 2));
224 }
225
226 /**
227 * Pane border for task pane.
228 *
229 * @author Kirill Grouchnikov
230 */
231 protected class SubstancePaneBorder extends PaneBorder {
232 protected Icon expandedIcon;
233
234 protected Icon collapsedIcon;
235
236 /**
237 * Creates a new pane border.
238 */
239 public SubstancePaneBorder() {
240 this.borderColor = SubstanceColorSchemeUtilities.getColorScheme(
241 group, ColorSchemeAssociationKind.BORDER,
242 ComponentState.ENABLED).getMidColor();
243 TransitionAwareIcon.ColorSchemeAssociationKindDelegate colorSchemeAssociationDelegate = new TransitionAwareIcon.ColorSchemeAssociationKindDelegate() {
244 @Override
245 public ColorSchemeAssociationKind getColorSchemeAssociationKind(
246 ComponentState state) {
247 if (!state.isDisabled()
248 && (state != ComponentState.ENABLED)) {
249 // use HIGHLIGHT
250 return ColorSchemeAssociationKind.HIGHLIGHT;
251 }
252 return ColorSchemeAssociationKind.MARK;
253 }
254 };
255 this.expandedIcon = new TransitionAwareIcon(group,
256 new TransitionAwareIcon.TransitionAwareUIDelegate() {
257 @Override
258 public TransitionAwareUI getTransitionAwareUI() {
259 return (TransitionAwareUI) group.getUI();
260 }
261 }, new TransitionAwareIcon.Delegate() {
262 public Icon getColorSchemeIcon(
263 SubstanceColorScheme scheme) {
264 return SubstanceImageCreator
265 .getDoubleArrowIconDelta(SubstanceSizeUtils
266 .getComponentFontSize(group), 0, 3,
267 0, SwingConstants.NORTH, scheme);
268 }
269 }, colorSchemeAssociationDelegate,
270 "substance.swingx.taskpane.expanded");
271 this.collapsedIcon = new TransitionAwareIcon(group,
272 new TransitionAwareIcon.TransitionAwareUIDelegate() {
273 @Override
274 public TransitionAwareUI getTransitionAwareUI() {
275 return (TransitionAwareUI) group.getUI();
276 }
277 }, new TransitionAwareIcon.Delegate() {
278 public Icon getColorSchemeIcon(
279 SubstanceColorScheme scheme) {
280 return SubstanceImageCreator
281 .getDoubleArrowIconDelta(SubstanceSizeUtils
282 .getComponentFontSize(group), 0, 3,
283 0, SwingConstants.SOUTH, scheme);
284 }
285 }, colorSchemeAssociationDelegate,
286 "substance.swingx.taskpane.collapsed");
287
288 // since the label is never added to this component, we need
289 // to explicitly mark it as DecorationAreaType.GENERAL
290 SubstanceLookAndFeel.setDecorationType(this.label,
291 DecorationAreaType.GENERAL);
292 // and enforce the foreground color computed in #getPaintColor
293 this.label.putClientProperty(
294 SubstanceTextUtilities.ENFORCE_FG_COLOR, Boolean.TRUE);
295 }
296
297 /*
298 * (non-Javadoc)
299 *
300 * @seeorg.jdesktop.swingx.plaf.basic.BasicTaskPaneUI.PaneBorder#
301 * paintTitleBackground(org.jdesktop.swingx.JXTaskPane,
302 * java.awt.Graphics)
303 */
304 @Override
305 protected void paintTitleBackground(JXTaskPane group, Graphics g) {
306 Graphics2D graphics = (Graphics2D) g.create();
307
308 StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker
309 .getModelStateInfo();
310 Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo
311 .getStateContributionMap();
312 ComponentState currState = stateTransitionTracker
313 .getModelStateInfo().getCurrModelState();
314
315 SubstanceColorScheme baseFillScheme = SubstanceColorSchemeUtilities
316 .getColorScheme(group,
317 ColorSchemeAssociationKind.HIGHLIGHT, currState);
318 SubstanceColorScheme baseBorderScheme = SubstanceColorSchemeUtilities
319 .getColorScheme(group,
320 ColorSchemeAssociationKind.HIGHLIGHT_BORDER,
321 currState);
322
323 HighlightPainterUtils
324 .paintHighlight(graphics, null, group, new Rectangle(0, 0,
325 group.getWidth(), getTitleHeight(group)), 0.5f,
326 null, baseFillScheme, baseBorderScheme);
327
328 for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> stateEntry : activeStates
329 .entrySet()) {
330 ComponentState activeState = stateEntry.getKey();
331 if (activeState == currState)
332 continue;
333
334 float contribution = stateEntry.getValue().getContribution();
335 if (contribution == 0.0f)
336 continue;
337
338 SubstanceColorScheme fillScheme = SubstanceColorSchemeUtilities
339 .getColorScheme(group,
340 ColorSchemeAssociationKind.HIGHLIGHT,
341 activeState);
342 SubstanceColorScheme borderScheme = SubstanceColorSchemeUtilities
343 .getColorScheme(group,
344 ColorSchemeAssociationKind.HIGHLIGHT_BORDER,
345 activeState);
346
347 graphics.setComposite(LafWidgetUtilities.getAlphaComposite(
348 group, contribution, g));
349 HighlightPainterUtils.paintHighlight(graphics, null, group,
350 new Rectangle(0, 0, group.getWidth(),
351 getTitleHeight(group)), 0.5f, null, fillScheme,
352 borderScheme);
353 }
354 graphics.dispose();
355 }
356
357 /*
358 * (non-Javadoc)
359 *
360 * @see
361 * org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI$PaneBorder#getPaintColor
362 * (org.jdesktop.swingx.JXTaskPane)
363 */
364 @Override
365 protected Color getPaintColor(JXTaskPane group) {
366 StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker
367 .getModelStateInfo();
368 Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo
369 .getStateContributionMap();
370 ComponentState currState = stateTransitionTracker
371 .getModelStateInfo().getCurrModelState();
372
373 if (currState.isDisabled() || (activeStates.size() == 1))
374 return getColorSchemeForState(group, currState)
375 .getForegroundColor();
376
377 float aggrRed = 0;
378 float aggrGreen = 0;
379 float aggrBlue = 0;
380
381 for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : modelStateInfo
382 .getStateContributionMap().entrySet()) {
383 ComponentState activeState = activeEntry.getKey();
384 SubstanceColorScheme scheme = getColorSchemeForState(group,
385 activeState);
386 Color schemeFg = scheme.getForegroundColor();
387 float contribution = activeEntry.getValue().getContribution();
388 aggrRed += schemeFg.getRed() * contribution;
389 aggrGreen += schemeFg.getGreen() * contribution;
390 aggrBlue += schemeFg.getBlue() * contribution;
391 }
392 return new Color((int) aggrRed, (int) aggrGreen, (int) aggrBlue);
393 }
394
395 private SubstanceColorScheme getColorSchemeForState(JXTaskPane group,
396 ComponentState state) {
397 ColorSchemeAssociationKind assocKind = ColorSchemeAssociationKind.FILL;
398 if (!state.isDisabled() && (state != ComponentState.ENABLED)) {
399 // use HIGHLIGHT
400 assocKind = ColorSchemeAssociationKind.HIGHLIGHT;
401 }
402 SubstanceColorScheme currScheme = SubstanceColorSchemeUtilities
403 .getColorScheme(group, assocKind, state);
404 return currScheme;
405 }
406
407 /*
408 * (non-Javadoc)
409 *
410 * @seeorg.jdesktop.swingx.plaf.basic.BasicTaskPaneUI$PaneBorder#
411 * paintExpandedControls(org.jdesktop.swingx.JXTaskPane,
412 * java.awt.Graphics, int, int, int, int)
413 */
414 @Override
415 protected void paintExpandedControls(JXTaskPane group, Graphics g,
416 int x, int y, int width, int height) {
417 Icon arrowIcon = group.isCollapsed() ? collapsedIcon : expandedIcon;
418 int dx = (width - arrowIcon.getIconWidth()) / 2;
419 int dy = 1 + (height - arrowIcon.getIconHeight()) / 2;
420 arrowIcon.paintIcon(group, g, x + dx, y + dy);
421 // g.setColor(Color.red);
422 // g.drawRect(x, y, width, height);
423 // g.setColor(Color.green);
424 // g.drawRect(x + dx, y + dy, arrowIcon.getIconWidth(), arrowIcon
425 // .getIconHeight());
426 }
427
428 /*
429 * (non-Javadoc)
430 *
431 * @seeorg.jdesktop.swingx.plaf.basic.BasicTaskPaneUI$PaneBorder#
432 * isMouseOverBorder()
433 */
434 @Override
435 protected boolean isMouseOverBorder() {
436 return true;
437 }
438
439 /*
440 * (non-Javadoc)
441 *
442 * @see
443 * org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI$PaneBorder#paintFocus
444 * (java.awt.Graphics, java.awt.Color, int, int, int, int)
445 */
446 @Override
447 protected void paintFocus(Graphics g, Color paintColor, int x, int y,
448 int width, int height) {
449 SubstanceCoreUtilities.paintFocus(g, group, group,
450 SubstanceTaskPaneUI.this, new Rectangle(x, y, width - 1,
451 height - 1), label.getBounds(), 1.0f, 0);
452 }
453
454 /*
455 * (non-Javadoc)
456 *
457 * @see
458 * org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI$PaneBorder#configureLabel
459 * (org.jdesktop.swingx.JXTaskPane)
460 */
461 @Override
462 protected void configureLabel(JXTaskPane group) {
463 label.applyComponentOrientation(group.getComponentOrientation());
464 label.setFont(group.getFont());
465 label.setText(group.getTitle());
466 label.setIcon(group.getIcon() == null ? new EmptyIcon() : group
467 .getIcon());
468 }
469 }
470
471 /*
472 * (non-Javadoc)
473 *
474 * @see javax.swing.plaf.ComponentUI#paint(java.awt.Graphics,
475 * javax.swing.JComponent)
476 */
477 @Override
478 public void paint(Graphics g, JComponent c) {
479 this.bgDelegate.paint(c, (Graphics2D) g, false);
480 }
481
482 @Override
483 public boolean isInside(MouseEvent me) {
484 return this.isInBorder(me);
485 }
486
487 @Override
488 public StateTransitionTracker getTransitionTracker() {
489 return this.stateTransitionTracker;
490 }
491
492 /*
493 * (non-Javadoc)
494 *
495 * @see
496 * org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI#getTitleHeight(java.awt
497 * .Component)
498 */
499 @Override
500 protected int getTitleHeight(Component c) {
501 return SubstanceSizeUtils.getAdjustedSize(SubstanceSizeUtils
502 .getComponentFontSize(c), super.getTitleHeight(c), 2, 3, false);
503 }
504 }