Coverage Report - net.miginfocom.demo.CallbackDemo
 
Classes in this File Line Coverage Branch Coverage Complexity
CallbackDemo
N/A
N/A
1.688
CallbackDemo$1
N/A
N/A
1.688
CallbackDemo$2
N/A
N/A
1.688
CallbackDemo$3
N/A
N/A
1.688
CallbackDemo$4
N/A
N/A
1.688
 
 1  
 package net.miginfocom.demo;
 2  
 
 3  
 import net.miginfocom.layout.BoundSize;
 4  
 import net.miginfocom.layout.ComponentWrapper;
 5  
 import net.miginfocom.layout.LayoutCallback;
 6  
 import net.miginfocom.layout.UnitValue;
 7  
 import net.miginfocom.swing.MigLayout;
 8  
 
 9  
 import javax.swing.*;
 10  
 import java.awt.*;
 11  
 import java.awt.event.*;
 12  
 import java.util.IdentityHashMap;
 13  
 
 14  
 /*
 15  
  * License (BSD):
 16  
  * ==============
 17  
  *
 18  
  * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
 19  
  * All rights reserved.
 20  
  *
 21  
  * Redistribution and use in source and binary forms, with or without modification,
 22  
  * are permitted provided that the following conditions are met:
 23  
  * Redistributions of source code must retain the above copyright notice, this list
 24  
  * of conditions and the following disclaimer.
 25  
  * Redistributions in binary form must reproduce the above copyright notice, this
 26  
  * list of conditions and the following disclaimer in the documentation and/or other
 27  
  * materials provided with the distribution.
 28  
  * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
 29  
  * used to endorse or promote products derived from this software without specific
 30  
  * prior written permission.
 31  
  *
 32  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 33  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 34  
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 35  
  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 36  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 37  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 38  
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 39  
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 40  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 41  
  * OF SUCH DAMAGE.
 42  
  *
 43  
  * @version 1.0
 44  
  * @author Mikael Grev, MiG InfoCom AB
 45  
  *         Date: 2006-sep-08
 46  
  */
 47  
 
 48  
 public class CallbackDemo extends JFrame implements ActionListener, MouseMotionListener, MouseListener
 49  
 {
 50  
         private final Timer repaintTimer = new Timer(20, new ActionListener() {
 51  
                 public void actionPerformed(ActionEvent e) {
 52  
                         ((JPanel) getContentPane()).revalidate();
 53  
                 }
 54  
         });
 55  
         private final IdentityHashMap<Object, Long> pressMap = new IdentityHashMap<Object, Long>();
 56  
         private Point mousePos = null;
 57  
 
 58  
         public CallbackDemo()
 59  
         {
 60  
                 super("MiG Layout Callback Demo");
 61  
 
 62  
                 MigLayout migLayout = new MigLayout("align center bottom, insets 30");
 63  
                 final JPanel panel = new JPanel(migLayout) {
 64  
                         protected void paintComponent(Graphics g)
 65  
                         {
 66  
                                 ((Graphics2D) g).setPaint(new GradientPaint(0, getHeight() / 2, Color.WHITE, 0, getHeight(), new Color(240, 238, 235)));
 67  
                                 g.fillRect(0, 0, getWidth(), getHeight());
 68  
                         }
 69  
                 };
 70  
                 setContentPane(panel);
 71  
 
 72  
                 // This callback methods will be called for every layout cycle and let you make correction before and after the calculations.
 73  
                 migLayout.addLayoutCallback(new LayoutCallback() {
 74  
 
 75  
                         // This is the size change part
 76  
                         public BoundSize[] getSize(ComponentWrapper comp)
 77  
                         {
 78  
                                 if (comp.getComponent() instanceof JButton) {
 79  
                                         Component c = (Component) comp.getComponent();
 80  
                                         Point p = mousePos != null ? SwingUtilities.convertPoint(panel, mousePos, c) : new Point(-1000, -1000);
 81  
 
 82  
                                         float fact = (float) Math.sqrt(Math.pow(Math.abs(p.x - c.getWidth() / 2f), 2) + Math.pow(Math.abs(p.y - c.getHeight() / 2f), 2));
 83  
                                         fact = Math.max(2 - (fact / 200), 1);
 84  
 
 85  
                                         return new BoundSize[] {new BoundSize(new UnitValue(70 * fact), ""), new BoundSize(new UnitValue(70 * fact), "")};
 86  
                                 }
 87  
                                 return null;
 88  
                         }
 89  
 
 90  
                         // This is the jumping part
 91  
                         public void correctBounds(ComponentWrapper c)
 92  
                         {
 93  
                                 Long pressedNanos = pressMap.get(c.getComponent());
 94  
                                 if (pressedNanos != null) {
 95  
                                         long duration = System.nanoTime() - pressedNanos;
 96  
                                         double maxHeight = 100.0 - (duration / 100000000.0);
 97  
                                         int deltaY = (int) Math.round(Math.abs(Math.sin((duration) / 300000000.0) * maxHeight));
 98  
                                         c.setBounds(c.getX(), c.getY() - deltaY, c.getWidth(), c.getHeight());
 99  
                                         if (maxHeight < 0.5) {
 100  
                                                 pressMap.remove(c.getComponent());
 101  
                                                 if (pressMap.size() == 0)
 102  
                                                         repaintTimer.stop();
 103  
                                         }
 104  
                                 }
 105  
                         }
 106  
                 });
 107  
 
 108  
                 for (int j = 0; j < 10; j++)
 109  
                         panel.add(createButton(j), "aligny 0.8al");
 110  
 
 111  
                 JLabel label = new JLabel("Can't you just feel the urge to press one of those Swing JButtons?");
 112  
                 label.setFont(new Font("verdana", Font.PLAIN, 24));
 113  
                 label.setForeground(new Color(150, 150, 150));
 114  
                 panel.add(label, "pos 0.5al 0.2al");
 115  
 
 116  
                 panel.addMouseMotionListener(this);
 117  
                 panel.addMouseListener(this);
 118  
         }
 119  
 
 120  
         private static Font[] FONTS = new Font[120];
 121  
         private JButton createButton(int i)
 122  
         {
 123  
                 JButton button = new JButton(String.valueOf("MIG LAYOUT".charAt(i))) {
 124  
                         public Font getFont()
 125  
                         {
 126  
                                 if (FONTS[0] == null) {
 127  
                                         for (int i = 0; i < FONTS.length; i++)
 128  
                                                 FONTS[i] = new Font("tahoma", Font.PLAIN, i);
 129  
                                 }
 130  
 
 131  
                                 return FONTS[getWidth() >> 1];
 132  
                         }
 133  
                 };
 134  
 
 135  
                 button.setForeground(new Color(100, 100, 100));
 136  
                 button.setFocusPainted(false);
 137  
                 button.addMouseMotionListener(this);
 138  
                 button.addActionListener(this);
 139  
                 button.setMargin(new Insets(0, 0, 0, 0));
 140  
                 return button;
 141  
         }
 142  
 
 143  
         public void mouseDragged(MouseEvent e) {}
 144  
         public void mouseMoved(MouseEvent e)
 145  
         {
 146  
                 if (e.getSource() instanceof JButton) {
 147  
                         mousePos = SwingUtilities.convertPoint((Component) e.getSource(), e.getPoint(), getContentPane());
 148  
                 } else {
 149  
                         mousePos = e.getPoint();
 150  
                 }
 151  
                 ((JPanel) getContentPane()).revalidate();
 152  
         }
 153  
 
 154  
         public void mousePressed(MouseEvent e) {}
 155  
         public void mouseReleased(MouseEvent e) {}
 156  
         public void mouseClicked(MouseEvent e) {}
 157  
         public void mouseEntered(MouseEvent e) {}
 158  
         public void mouseExited(MouseEvent e)
 159  
         {
 160  
                 mousePos = null;
 161  
                 ((JPanel) getContentPane()).revalidate();
 162  
         }
 163  
 
 164  
         public void actionPerformed(ActionEvent e)
 165  
         {
 166  
                 pressMap.put(e.getSource(), System.nanoTime());
 167  
                 repaintTimer.start();
 168  
         }
 169  
 
 170  
         public static void main(String args[])
 171  
         {
 172  
                 try {
 173  
                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 174  
                 } catch (Exception ex) {}
 175  
 
 176  
                 CallbackDemo demoFrame = new CallbackDemo();
 177  
                 demoFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 178  
                 demoFrame.setSize(970, 500);
 179  
                 demoFrame.setLocationRelativeTo(null);
 180  
                 demoFrame.setVisible(true);
 181  
         }
 182  
 }