Mercurial > hg > release > thermostat-0.4
changeset 560:e7e68d924181
Refactor thread view (toggle switch)
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002890.html
reviewed-by: omajid
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Wed, 22 Aug 2012 19:22:24 +0200 |
parents | f4701a486325 |
children | 995b2dd560b0 |
files | client/swing-components/src/main/java/com/redhat/thermostat/swing/GraphicsUtils.java client/swing-components/src/main/java/com/redhat/thermostat/swing/ToggleButton.java client/swing-components/src/main/resources/icons/scale-slider-vert-backdrop.png client/swing-components/src/main/resources/icons/scale-slider-vert.png |
diffstat | 4 files changed, 189 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/client/swing-components/src/main/java/com/redhat/thermostat/swing/GraphicsUtils.java Wed Aug 22 14:55:41 2012 +0200 +++ b/client/swing-components/src/main/java/com/redhat/thermostat/swing/GraphicsUtils.java Wed Aug 22 19:22:24 2012 +0200 @@ -39,9 +39,13 @@ import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; +import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Paint; import java.awt.RenderingHints; +import java.awt.Shape; +import java.awt.geom.RoundRectangle2D; import javax.swing.JComponent; @@ -74,4 +78,13 @@ public FontMetrics getFontMetrics(JComponent component, Font font) { return SwingUtilities2.getFontMetrics(component, font); } + + public Shape getRoundShape(int width, int height) { + return new RoundRectangle2D.Double(0, 0, width - 2, height - 1, 4, 4); + } + + public void setGradientPaint(Graphics2D g, int x, int height, Color start, Color stop) { + Paint paint = new GradientPaint(x, 0, start, 0, height, stop); + g.setPaint(paint); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing-components/src/main/java/com/redhat/thermostat/swing/ToggleButton.java Wed Aug 22 19:22:24 2012 +0200 @@ -0,0 +1,176 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.swing; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.beans.Transient; + +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JToggleButton; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; + +/** + * A simple On/Off switch. + */ +@SuppressWarnings("serial") +public class ToggleButton extends JToggleButton { + + private static final int WIDTH = 40; + private static final int HEIGHT = 24; + + private ImageIcon toggle; + private ImageIcon selectedToggle; + + public ToggleButton() { + + toggle = new ImageIcon(getClass().getResource("/icons/scale-slider-vert.png")); + selectedToggle = new ImageIcon(getClass().getResource("/icons/scale-slider-vert-backdrop.png")); + + setBorder(null); + } + + @Override + protected void paintComponent(Graphics g) { + GraphicsUtils utils = GraphicsUtils.getInstance(); + + Graphics2D graphics = utils.createAAGraphics(g); + graphics.clearRect(0, 0, getWidth(), getHeight()); + + graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + + Color selectedcolor = null; + Color shadow = new Color(0xE0E0E0); + if (model.isSelected()) { + selectedcolor = new Color(0x4A90D9); + } else { + selectedcolor = new Color(0xaeaeae); + } + + Color bgColor = null; + Container parent = getParent(); + if (parent != null) { + bgColor = parent.getBackground(); + } else { + bgColor = getBackground(); + } + + // external shape and main fill + utils.setGradientPaint(graphics, 0, 10, shadow, bgColor); + graphics.fill(utils.getRoundShape(getWidth(), getHeight())); + + utils.setGradientPaint(graphics, 0, getWidth() / 2, selectedcolor, bgColor); + graphics.draw(utils.getRoundShape(getWidth(), getHeight())); + + // slider + int x = (toggle.getIconWidth() / 2); + int y = (getHeight()/2); + int width = getWidth() - (toggle.getIconWidth()/2) - 2; + + graphics.setColor(selectedcolor); + graphics.setStroke(new BasicStroke(1.2f)); + graphics.drawLine(x, y, width, y); + + graphics.setStroke(new BasicStroke()); + + // toggle + x = 2; + y = (getHeight()/2) - (toggle.getIconHeight()/2); + ImageIcon toggle = this.toggle; + if (model.isRollover()) { + toggle = this.selectedToggle; + } + + if (model.isSelected()) { + toggle = this.selectedToggle; + x = getWidth() - toggle.getIconWidth() - 3; + } + graphics.drawImage(toggle.getImage(), x, y, null); + + graphics.dispose(); + } + + @Override + @Transient + public Dimension getMinimumSize() { + if (isMinimumSizeSet()) { + return super.getMinimumSize(); + } + return new Dimension(WIDTH, HEIGHT); + } + + @Override + @Transient + public Dimension getPreferredSize() { + if (isPreferredSizeSet()) { + return super.getPreferredSize(); + } + return new Dimension(WIDTH, HEIGHT); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + HeaderPanel header = new HeaderPanel(); + header.setHeader("Test"); + + JPanel buttonPanel = new JPanel(); + ToggleButton toggle = new ToggleButton(); + + buttonPanel.add(toggle); + header.setContent(buttonPanel); + + frame.add(header); + frame.setSize(500, 500); + frame.setVisible(true); + } + }); + } +}