| 1 |
/* |
| 2 |
* Copyright (c) 2005-2010 Flamingo / Substance Kirill Grouchnikov. All Rights Reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* o Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* |
| 10 |
* o Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* this list of conditions and the following disclaimer in the documentation |
| 12 |
* and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* o Neither the name of Flamingo Kirill Grouchnikov nor the names of |
| 15 |
* its contributors may be used to endorse or promote products derived |
| 16 |
* from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
package org.pushingpixels.substance.flamingo.common.ui; |
| 31 |
|
| 32 |
import java.awt.Dimension; |
| 33 |
import java.awt.Graphics; |
| 34 |
import java.util.EnumSet; |
| 35 |
|
| 36 |
import javax.swing.*; |
| 37 |
import javax.swing.plaf.ComponentUI; |
| 38 |
|
| 39 |
import org.pushingpixels.flamingo.api.common.JCommandButton; |
| 40 |
import org.pushingpixels.flamingo.api.common.JScrollablePanel; |
| 41 |
import org.pushingpixels.flamingo.api.common.JScrollablePanel.ScrollType; |
| 42 |
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon; |
| 43 |
import org.pushingpixels.flamingo.internal.ui.common.BasicScrollablePanelUI; |
| 44 |
import org.pushingpixels.substance.api.*; |
| 45 |
import org.pushingpixels.substance.flamingo.common.TransitionAwareResizableIcon; |
| 46 |
import org.pushingpixels.substance.internal.animation.StateTransitionTracker; |
| 47 |
import org.pushingpixels.substance.internal.painter.BackgroundPaintingUtils; |
| 48 |
import org.pushingpixels.substance.internal.utils.*; |
| 49 |
|
| 50 |
/** |
| 51 |
* UI for {@link JScrollablePanel} components in <b>Substance</b> look and feel. |
| 52 |
* |
| 53 |
* @author Kirill Grouchnikov |
| 54 |
*/ |
| 55 |
public class SubstanceScrollablePanelUI extends BasicScrollablePanelUI { |
| 56 |
/* |
| 57 |
* (non-Javadoc) |
| 58 |
* |
| 59 |
* @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent) |
| 60 |
*/ |
| 61 |
public static ComponentUI createUI(JComponent comp) { |
| 62 |
SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp); |
| 63 |
return new SubstanceScrollablePanelUI(); |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* (non-Javadoc) |
| 68 |
* |
| 69 |
* @see javax.swing.plaf.ComponentUI#update(java.awt.Graphics, |
| 70 |
* javax.swing.JComponent) |
| 71 |
*/ |
| 72 |
@Override |
| 73 |
public void update(Graphics g, JComponent c) { |
| 74 |
if (!c.isShowing()) { |
| 75 |
return; |
| 76 |
} |
| 77 |
synchronized (c) { |
| 78 |
if (c.isOpaque()) { |
| 79 |
BackgroundPaintingUtils.update(g, c, false); |
| 80 |
super.paint(g, c); |
| 81 |
} else { |
| 82 |
super.paint(g, c); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public JCommandButton createLeadingScroller() { |
| 89 |
final JCommandButton result = super.createLeadingScroller(); |
| 90 |
int fontSize = SubstanceSizeUtils.getComponentFontSize(result); |
| 91 |
int arrowIconHeight = (int) SubstanceSizeUtils |
| 92 |
.getSmallArrowIconHeight(fontSize) + 3; |
| 93 |
int arrowIconWidth = (int) SubstanceSizeUtils |
| 94 |
.getSmallArrowIconWidth(fontSize); |
| 95 |
if (arrowIconHeight % 2 == 0) |
| 96 |
arrowIconHeight++; |
| 97 |
ResizableIcon arrowIcon = new TransitionAwareResizableIcon( |
| 98 |
result, |
| 99 |
new TransitionAwareResizableIcon.StateTransitionTrackerDelegate() { |
| 100 |
@Override |
| 101 |
public StateTransitionTracker getStateTransitionTracker() { |
| 102 |
return ((ActionPopupTransitionAwareUI) result.getUI()) |
| 103 |
.getActionTransitionTracker(); |
| 104 |
} |
| 105 |
}, new TransitionAwareResizableIcon.Delegate() { |
| 106 |
@Override |
| 107 |
public Icon getColorSchemeIcon(SubstanceColorScheme scheme, |
| 108 |
int width, int height) { |
| 109 |
int fontSize = SubstanceSizeUtils |
| 110 |
.getComponentFontSize(result); |
| 111 |
float arrowStrokeWidth = SubstanceSizeUtils |
| 112 |
.getDoubleArrowStrokeWidth(fontSize) - 0.3f; |
| 113 |
if (scrollablePanel.getScrollType() == ScrollType.HORIZONTALLY) { |
| 114 |
width--; |
| 115 |
} |
| 116 |
Icon doubleArrowIcon = SubstanceImageCreator |
| 117 |
.getDoubleArrowIcon( |
| 118 |
SubstanceSizeUtils |
| 119 |
.getComponentFontSize(result), |
| 120 |
width, |
| 121 |
height, |
| 122 |
arrowStrokeWidth, |
| 123 |
(scrollablePanel.getScrollType() == ScrollType.HORIZONTALLY) ? SwingUtilities.WEST |
| 124 |
: SwingUtilities.NORTH, scheme); |
| 125 |
return doubleArrowIcon; |
| 126 |
} |
| 127 |
}, new Dimension(arrowIconHeight, arrowIconWidth)); |
| 128 |
result.setIcon(arrowIcon); |
| 129 |
result |
| 130 |
.putClientProperty( |
| 131 |
SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY, |
| 132 |
(scrollablePanel.getScrollType() == ScrollType.HORIZONTALLY) ? EnumSet |
| 133 |
.of(SubstanceConstants.Side.RIGHT) |
| 134 |
: EnumSet.of(SubstanceConstants.Side.BOTTOM)); |
| 135 |
result.setHorizontalAlignment(SwingConstants.CENTER); |
| 136 |
result.setFlat(true); |
| 137 |
return result; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public JCommandButton createTrailingScroller() { |
| 142 |
final JCommandButton result = super.createTrailingScroller(); |
| 143 |
int fontSize = SubstanceSizeUtils.getComponentFontSize(result); |
| 144 |
int arrowIconHeight = (int) SubstanceSizeUtils |
| 145 |
.getSmallArrowIconHeight(fontSize) + 3; |
| 146 |
int arrowIconWidth = (int) SubstanceSizeUtils |
| 147 |
.getSmallArrowIconWidth(fontSize); |
| 148 |
if (arrowIconHeight % 2 == 0) |
| 149 |
arrowIconHeight++; |
| 150 |
ResizableIcon arrowIcon = new TransitionAwareResizableIcon( |
| 151 |
result, |
| 152 |
new TransitionAwareResizableIcon.StateTransitionTrackerDelegate() { |
| 153 |
@Override |
| 154 |
public StateTransitionTracker getStateTransitionTracker() { |
| 155 |
return ((ActionPopupTransitionAwareUI) result.getUI()) |
| 156 |
.getActionTransitionTracker(); |
| 157 |
} |
| 158 |
}, new TransitionAwareResizableIcon.Delegate() { |
| 159 |
@Override |
| 160 |
public Icon getColorSchemeIcon(SubstanceColorScheme scheme, |
| 161 |
int width, int height) { |
| 162 |
int fontSize = SubstanceSizeUtils |
| 163 |
.getComponentFontSize(result); |
| 164 |
float arrowStrokeWidth = SubstanceSizeUtils |
| 165 |
.getDoubleArrowStrokeWidth(fontSize) - 0.3f; |
| 166 |
if (scrollablePanel.getScrollType() == ScrollType.HORIZONTALLY) { |
| 167 |
width--; |
| 168 |
} |
| 169 |
Icon doubleArrowIcon = SubstanceImageCreator |
| 170 |
.getDoubleArrowIcon( |
| 171 |
SubstanceSizeUtils |
| 172 |
.getComponentFontSize(result), |
| 173 |
width, |
| 174 |
height, |
| 175 |
arrowStrokeWidth, |
| 176 |
(scrollablePanel.getScrollType() == ScrollType.HORIZONTALLY) ? SwingUtilities.EAST |
| 177 |
: SwingUtilities.SOUTH, scheme); |
| 178 |
return doubleArrowIcon; |
| 179 |
} |
| 180 |
}, new Dimension(arrowIconHeight, arrowIconWidth)); |
| 181 |
result.setIcon(arrowIcon); |
| 182 |
result |
| 183 |
.putClientProperty( |
| 184 |
SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY, |
| 185 |
(scrollablePanel.getScrollType() == ScrollType.HORIZONTALLY) ? EnumSet |
| 186 |
.of(SubstanceConstants.Side.LEFT) |
| 187 |
: EnumSet.of(SubstanceConstants.Side.TOP)); |
| 188 |
result.setHorizontalAlignment(SwingConstants.CENTER); |
| 189 |
result.setFlat(true); |
| 190 |
return result; |
| 191 |
} |
| 192 |
} |