summaryrefslogtreecommitdiff
path: root/prototypes/miglayout/net/miginfocom/demo/CallbackDemo.java
blob: 693271e4dc521b0917535d1111136ac2ec333988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package net.miginfocom.demo;

import net.miginfocom.swing.MigLayout;
import net.miginfocom.layout.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.IdentityHashMap;

public class CallbackDemo extends JFrame implements ActionListener, MouseMotionListener, MouseListener
{
	private final Timer repaintTimer = new Timer(20, new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			((JPanel) getContentPane()).revalidate();
		}
	});
	private final IdentityHashMap<Object, Long> pressMap = new IdentityHashMap<Object, Long>();
	private Point mousePos = null;

	public CallbackDemo()
	{
		super("MiG Layout Callback Demo");

		MigLayout migLayout = new MigLayout("align center bottom, insets 30");
		final JPanel panel = new JPanel(migLayout) {
			protected void paintComponent(Graphics g)
			{
				((Graphics2D) g).setPaint(new GradientPaint(0, getHeight() / 2, Color.WHITE, 0, getHeight(), new Color(240, 238, 235)));
				g.fillRect(0, 0, getWidth(), getHeight());
			}
		};
		setContentPane(panel);

		// This callback methods will be called for every layout cycle and let you make correction before and after the calculations.
		migLayout.addLayoutCallback(new LayoutCallback() {

			// This is the size change part
			public BoundSize[] getSize(ComponentWrapper comp)
			{
				if (comp.getComponent() instanceof JButton) {
					Component c = (Component) comp.getComponent();
					Point p = mousePos != null ? SwingUtilities.convertPoint(panel, mousePos, c) : new Point(-1000, -1000);

					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));
					fact = Math.max(2 - (fact / 200), 1);

					return new BoundSize[] {new BoundSize(new UnitValue(60 * fact), ""), new BoundSize(new UnitValue(60 * fact), "")};
				}
				return null;
			}

			// This is the jumping part
			public void correctBounds(ComponentWrapper c)
			{
				Long pressedNanos = pressMap.get(c.getComponent());
				if (pressedNanos != null) {
					long duration = System.nanoTime() - pressedNanos;
					double maxHeight = 100.0 - (duration / 100000000.0);
					int deltaY = (int) Math.round(Math.abs(Math.sin((duration) / 300000000.0) * maxHeight));
					c.setBounds(c.getX(), c.getY() - deltaY, c.getWidth(), c.getHeight());

					if (maxHeight < 0.5) {
						pressMap.remove(c.getComponent());
						if (pressMap.size() == 0)
							repaintTimer.stop();
					}
				}
			}
		});

		for (int j = 0; j < 10; j++)
			panel.add(createButton(j), "aligny 0.8al");

		JLabel label = new JLabel("Can't you just feel the urge to press one of those Swing JButtons?");
		label.setFont(new Font("verdana", Font.PLAIN, 24));
		label.setForeground(new Color(150, 150, 150));
		panel.add(label, "pos 0.5al 0.2al");

		panel.addMouseMotionListener(this);
		panel.addMouseListener(this);
	}

	private static Font[] FONTS = new Font[120];
	private JButton createButton(int i)
	{
		JButton button = new JButton(String.valueOf("MIG LAYOUT".charAt(i))) {
			public Font getFont()
			{
				if (FONTS[0] == null) {
					for (int i = 0; i < FONTS.length; i++)
						FONTS[i] = new Font("tahoma", Font.PLAIN, i);
				}

				return FONTS[getWidth() >> 1];
			}
		};

		button.setForeground(new Color(100, 100, 100));
		button.setFocusPainted(false);
		button.addMouseMotionListener(this);
		button.addActionListener(this);
		button.setMargin(new Insets(0, 0, 0, 0));
		return button;
	}

	public void mouseDragged(MouseEvent e) {}
	public void mouseMoved(MouseEvent e)
	{
		if (e.getSource() instanceof JButton) {
			mousePos = SwingUtilities.convertPoint((Component) e.getSource(), e.getPoint(), getContentPane());
		} else {
			mousePos = e.getPoint();
		}
		((JPanel) getContentPane()).revalidate();
	}

	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e)
	{
		mousePos = null;
		((JPanel) getContentPane()).revalidate();
	}

	public void actionPerformed(ActionEvent e)
	{
		pressMap.put(e.getSource(), System.nanoTime());
		repaintTimer.start();
	}

	public static void main(String args[])
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception ex) {}

		CallbackDemo demoFrame = new CallbackDemo();
		demoFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		demoFrame.setSize(970, 500);
		demoFrame.setLocationRelativeTo(null);
		demoFrame.setVisible(true);
	}
}