001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test;
031:
032: import java.awt.*;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.image.BufferedImage;
036: import java.util.HashMap;
037: import java.util.Map;
038:
039: import javax.swing.*;
040: import javax.swing.event.ChangeEvent;
041: import javax.swing.event.ChangeListener;
042:
043: import org.jvnet.lafwidget.LafWidget;
044: import org.jvnet.lafwidget.LafWidgetUtilities2;
045: import org.jvnet.lafwidget.layout.TransitionLayout;
046: import org.jvnet.lafwidget.preview.DefaultPreviewPainter;
047: import org.jvnet.substance.SubstanceLookAndFeel;
048: import org.jvnet.substance.grip.DragBumpsGripPainter;
049: import org.jvnet.substance.painter.AlphaControlBackgroundComposite;
050: import org.jvnet.substance.utils.SubstanceCoreUtilities;
051: import org.jvnet.substance.utils.SubstanceConstants.ScrollPaneButtonPolicyKind;
052:
053: public class ScrollDemo extends JFrame {
054: public class ScrollPanel extends JPanel {
055: private JScrollPane sp;
056:
057: private JPanel panel;
058:
059: private Map<String, BufferedImage> shots;
060:
061: public ScrollPanel() {
062: this .shots = new HashMap<String, BufferedImage>();
063:
064: this .panel = new JPanel() {
065: @Override
066: protected void paintComponent(Graphics g) {
067: Graphics2D graphics = (Graphics2D) g.create();
068: graphics.setComposite(TransitionLayout
069: .getAlphaComposite(panel));
070:
071: BufferedImage shot = getShot(this .getWidth(), this
072: .getHeight());
073: graphics.drawImage(shot, 0, 0, null);
074: graphics.dispose();
075: }
076: };
077: // this.panel.putClientProperty(LafWidget.COMPONENT_PREVIEW_PAINTER,
078: // new DefaultPreviewPainter());
079: this .panel.setPreferredSize(new Dimension(1200, 800));
080: this .panel.setSize(this .getPreferredSize());
081: this .panel.setMinimumSize(this .getPreferredSize());
082: this .sp = new JScrollPane(this .panel,
083: ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
084: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
085: this .sp
086: .putClientProperty(
087: SubstanceLookAndFeel.OVERLAY_PROPERTY,
088: Boolean.TRUE);
089: this .sp.putClientProperty(
090: SubstanceLookAndFeel.PAINT_ACTIVE_PROPERTY,
091: Boolean.TRUE);
092: this .sp.putClientProperty(
093: SubstanceLookAndFeel.GRIP_PAINTER,
094: new DragBumpsGripPainter());
095: this .sp.putClientProperty(
096: LafWidget.COMPONENT_PREVIEW_PAINTER,
097: new DefaultPreviewPainter());
098: // this.sp.putClientProperty(
099: // SubstanceLookAndFeel.PAINT_ACTIVE_PROPERTY, Boolean.FALSE);
100: this .setLayout(new BorderLayout());
101: this .add(this .sp, BorderLayout.CENTER);
102:
103: JPanel controlsSliders = new JPanel(new GridLayout(2, 1));
104:
105: final JSlider sliderInactive = new JSlider(0, 100, 50);
106: sliderInactive.setMajorTickSpacing(20);
107: sliderInactive.setMinorTickSpacing(5);
108: sliderInactive.setPaintLabels(true);
109: sliderInactive.setPaintTicks(true);
110: sliderInactive.setSnapToTicks(true);
111:
112: final JSlider sliderActive = new JSlider(0, 100, 80);
113: sliderActive.setMajorTickSpacing(20);
114: sliderActive.setMinorTickSpacing(5);
115: sliderActive.setPaintLabels(true);
116: sliderActive.setPaintTicks(true);
117: sliderActive.setSnapToTicks(true);
118:
119: sliderInactive.addChangeListener(new ChangeListener() {
120: public void stateChanged(ChangeEvent e) {
121: int valInactive = sliderInactive.getValue();
122: int valActive = sliderActive.getValue();
123: sp.putClientProperty(
124: SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
125: new AlphaControlBackgroundComposite(
126: valInactive / 100.0f,
127: valActive / 100.0f));
128: sp.repaint();
129: }
130: });
131: sliderActive.addChangeListener(new ChangeListener() {
132: public void stateChanged(ChangeEvent e) {
133: int valInactive = sliderInactive.getValue();
134: int valActive = sliderActive.getValue();
135: sp.putClientProperty(
136: SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
137: new AlphaControlBackgroundComposite(
138: valInactive / 100.0f,
139: valActive / 100.0f));
140: sp.repaint();
141: }
142: });
143: int valInactive = sliderInactive.getValue();
144: int valActive = sliderActive.getValue();
145: sp.putClientProperty(
146: SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
147: new AlphaControlBackgroundComposite(
148: valInactive / 100.0f, valActive / 100.0f));
149:
150: controlsSliders.add(sliderActive);
151: controlsSliders.add(sliderInactive);
152: this .add(controlsSliders, BorderLayout.SOUTH);
153:
154: JPanel controls = new JPanel();
155: controls.setLayout(new FlowLayout(FlowLayout.CENTER));
156: final JCheckBox isEnabled = new JCheckBox("enabled");
157: isEnabled.setSelected(true);
158: isEnabled.addActionListener(new ActionListener() {
159: public void actionPerformed(ActionEvent e) {
160: boolean toEnable = isEnabled.isSelected();
161: sp.setEnabled(toEnable);
162: updateEnabledState(sp, toEnable);
163: Check.out("Scroll pane is " + toEnable);
164: }
165: });
166: // controls.add(isEnabled);
167:
168: final JCheckBox hasCustomPreview = new JCheckBox("preview");
169: hasCustomPreview.setSelected(LafWidgetUtilities2
170: .getComponentPreviewPainter(sp) != null);
171: hasCustomPreview.addActionListener(new ActionListener() {
172: public void actionPerformed(ActionEvent e) {
173: sp
174: .putClientProperty(
175: LafWidget.COMPONENT_PREVIEW_PAINTER,
176: hasCustomPreview.isSelected() ? new DefaultPreviewPainter()
177: : null);
178: sp.repaint();
179: }
180: });
181: controls.add(hasCustomPreview);
182:
183: final JComboBox buttonPolicyCombo = new JComboBox(
184: new Object[] { ScrollPaneButtonPolicyKind.NONE,
185: ScrollPaneButtonPolicyKind.OPPOSITE,
186: ScrollPaneButtonPolicyKind.ADJACENT,
187: ScrollPaneButtonPolicyKind.MULTIPLE,
188: ScrollPaneButtonPolicyKind.MULTIPLE_BOTH });
189: buttonPolicyCombo
190: .setSelectedItem(ScrollPaneButtonPolicyKind.OPPOSITE);
191: buttonPolicyCombo.addActionListener(new ActionListener() {
192: public void actionPerformed(ActionEvent e) {
193: ScrollPaneButtonPolicyKind buttonPolicy = (ScrollPaneButtonPolicyKind) buttonPolicyCombo
194: .getSelectedItem();
195: sp
196: .putClientProperty(
197: SubstanceLookAndFeel.SCROLL_PANE_BUTTONS_POLICY,
198: buttonPolicy);
199: sp.repaint();
200: }
201: });
202: // controls.add(new JLabel("buttons"));
203: controls.add(buttonPolicyCombo);
204:
205: final JCheckBox isNever = new JCheckBox("never");
206: isNever.addActionListener(new ActionListener() {
207: public void actionPerformed(ActionEvent e) {
208: sp
209: .putClientProperty(
210: SubstanceLookAndFeel.BUTTON_PAINT_NEVER_PROPERTY,
211: isNever.isSelected() ? Boolean.TRUE
212: : null);
213: sp.repaint();
214: }
215: });
216: controls.add(isNever);
217:
218: final JCheckBox isFlat = new JCheckBox("flat");
219: isFlat.addActionListener(new ActionListener() {
220: public void actionPerformed(ActionEvent e) {
221: sp
222: .putClientProperty(
223: SubstanceLookAndFeel.FLAT_PROPERTY,
224: isFlat.isSelected() ? Boolean.TRUE
225: : null);
226: sp.repaint();
227: }
228: });
229: controls.add(isFlat);
230:
231: final JCheckBox isActive = new JCheckBox("active");
232: isActive
233: .setSelected(Boolean.TRUE
234: .equals(sp
235: .getClientProperty(SubstanceLookAndFeel.PAINT_ACTIVE_PROPERTY)));
236: isActive.addActionListener(new ActionListener() {
237: public void actionPerformed(ActionEvent e) {
238: sp
239: .putClientProperty(
240: SubstanceLookAndFeel.PAINT_ACTIVE_PROPERTY,
241: isActive.isSelected() ? Boolean.TRUE
242: : null);
243: sp.repaint();
244: }
245: });
246: controls.add(isActive);
247:
248: final JCheckBox isOverlay = new JCheckBox("overlay");
249: isOverlay
250: .setSelected(Boolean.TRUE
251: .equals(sp
252: .getClientProperty(SubstanceLookAndFeel.OVERLAY_PROPERTY)));
253: isOverlay.addActionListener(new ActionListener() {
254: public void actionPerformed(ActionEvent e) {
255: sp.putClientProperty(
256: SubstanceLookAndFeel.OVERLAY_PROPERTY,
257: isOverlay.isSelected() ? Boolean.TRUE
258: : null);
259: sp.repaint();
260: }
261: });
262: controls.add(isOverlay);
263:
264: this .add(controls, BorderLayout.NORTH);
265: }
266:
267: private synchronized BufferedImage getShot(int width, int height) {
268: String key = width + ":" + height;
269: BufferedImage result = this .shots.get(key);
270: if (result == null) {
271: result = SubstanceCoreUtilities.getBlankImage(width,
272: height);
273:
274: Graphics2D graphics = result.createGraphics();
275: int cols = 1 + width / 10;
276: int rows = 1 + height / 10;
277: if (SubstanceCoreUtilities
278: .isThemeDark(SubstanceLookAndFeel.getTheme()))
279: graphics.setColor(Color.black);
280: else
281: graphics.setColor(Color.white);
282: graphics.fillRect(0, 0, width, height);
283: for (int i = 0; i < cols; i++) {
284: for (int j = 0; j < rows; j++) {
285: if (((i + j) % 2) == 0) {
286: float val = (i + j) / 100.f;
287: val -= Math.floor(val);
288: boolean isDark = SubstanceCoreUtilities
289: .isThemeDark(SubstanceLookAndFeel
290: .getTheme());
291: float brightness = isDark ? 0.1f : 0.9f;
292: float saturation = 0.2f;
293: graphics.setColor(new Color(Color.HSBtoRGB(
294: val, saturation, brightness)));
295: graphics.fillRect(i * 10, j * 10, 10, 10);
296: }
297: }
298: }
299:
300: graphics.setColor(Color.gray);
301: graphics.setFont(new Font("Arial", Font.PLAIN, 11));
302: rows = 1 + height / 25;
303: for (int i = 0; i < rows; i++) {
304: for (int j = 0; j < width / 25; j++) {
305: graphics.drawString("" + (i + j), j * 25,
306: 25 * i);
307: }
308: }
309:
310: this .shots.put(key, result);
311: }
312: return result;
313: }
314: }
315:
316: public ScrollDemo() {
317: super ("Scroll overlay demo");
318: this .setLayout(new BorderLayout());
319: this .add(new ScrollPanel(), BorderLayout.CENTER);
320: this .setSize(450, 400);
321: this .setLocationRelativeTo(null);
322: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
323: }
324:
325: void updateEnabledState(Container c, boolean enabled) {
326: for (int counter = c.getComponentCount() - 1; counter >= 0; counter--) {
327: Component child = c.getComponent(counter);
328:
329: child.setEnabled(enabled);
330: if (child instanceof Container) {
331: updateEnabledState((Container) child, enabled);
332: }
333: }
334: }
335:
336: public static void main(String[] args) throws Exception {
337: // UIManager.setLookAndFeel(new SubstanceModerateLookAndFeel());
338: UIManager.setLookAndFeel(new SubstanceLookAndFeel());
339: SwingUtilities.invokeLater(new Runnable() {
340: public void run() {
341: new ScrollDemo().setVisible(true);
342: }
343: });
344: }
345: }
|