BasicSpinnerUI.java [plain text]
package javax.swing.plaf.basic;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JSpinner;
import javax.swing.Timer;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.SpinnerUI;
public class BasicSpinnerUI extends SpinnerUI
{
public static ComponentUI createUI(JComponent c)
{
return new BasicSpinnerUI();
}
protected JComponent createEditor()
{
return spinner.getEditor();
}
protected LayoutManager createLayout()
{
return new DefaultLayoutManager();
}
protected Component createNextButton()
{
JButton button = new BasicArrowButton(BasicArrowButton.NORTH);
return button;
}
protected Component createPreviousButton()
{
JButton button = new BasicArrowButton(BasicArrowButton.SOUTH);
return button;
}
protected PropertyChangeListener createPropertyChangeListener()
{
return new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
if ("editor".equals(evt.getPropertyName()))
BasicSpinnerUI.this.replaceEditor((JComponent) evt.getOldValue(),
(JComponent) evt.getNewValue());
}
};
}
protected void installDefaults()
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
spinner.setLayout(createLayout());
}
protected void installListeners()
{
spinner.addPropertyChangeListener(listener);
}
protected void installNextButtonListeners(Component c)
{
c.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
if (! spinner.isEnabled())
return;
increment();
timer.setInitialDelay(500);
timer.start();
}
public void mouseReleased(MouseEvent evt)
{
timer.stop();
}
void increment()
{
Object next = BasicSpinnerUI.this.spinner.getNextValue();
if (next != null)
BasicSpinnerUI.this.spinner.getModel().setValue(next);
}
volatile boolean mouseDown = false;
Timer timer = new Timer(50,
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
increment();
}
});
});
}
protected void installPreviousButtonListeners(Component c)
{
c.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
if (! spinner.isEnabled())
return;
decrement();
timer.setInitialDelay(500);
timer.start();
}
public void mouseReleased(MouseEvent evt)
{
timer.stop();
}
void decrement()
{
Object prev = BasicSpinnerUI.this.spinner.getPreviousValue();
if (prev != null)
BasicSpinnerUI.this.spinner.getModel().setValue(prev);
}
volatile boolean mouseDown = false;
Timer timer = new Timer(50,
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
decrement();
}
});
});
}
public void installUI(JComponent c)
{
super.installUI(c);
spinner = (JSpinner) c;
installDefaults();
installListeners();
Component next = createNextButton();
Component previous = createPreviousButton();
installNextButtonListeners(next);
installPreviousButtonListeners(previous);
c.add(createEditor(), "Editor");
c.add(next, "Next");
c.add(previous, "Previous");
}
protected void replaceEditor(JComponent oldEditor, JComponent newEditor)
{
spinner.remove(oldEditor);
spinner.add(newEditor);
}
protected void uninstallDefaults()
{
spinner.setLayout(null);
}
protected void uninstallListeners()
{
spinner.removePropertyChangeListener(listener);
}
public void uninstallUI(JComponent c)
{
super.uninstallUI(c);
uninstallDefaults();
uninstallListeners();
c.removeAll();
}
protected JSpinner spinner;
private PropertyChangeListener listener = createPropertyChangeListener();
private class DefaultLayoutManager implements LayoutManager
{
public void layoutContainer(Container parent)
{
synchronized (parent.getTreeLock())
{
Insets i = parent.getInsets();
boolean l2r = parent.getComponentOrientation().isLeftToRight();
Dimension e = minSize(editor);
Dimension n = minSize(next);
Dimension p = minSize(previous);
Dimension s = spinner.getPreferredSize();
int x = l2r ? i.left : i.right;
int y = i.top;
int w = Math.max(p.width, n.width);
int h = Math.max(p.height, n.height);
h = Math.max(h, e.height / 2);
int e_width = s.width - w;
if (l2r)
{
setBounds(editor, x, y + (s.height - e.height) / 2, e_width,
e.height);
x += e_width;
setBounds(next, x, y, w, h);
y += h;
setBounds(previous, x, y, w, h);
}
else
{
setBounds(next, x, y + (s.height - e.height) / 2, w, h);
y += h;
setBounds(previous, x, y, w, h);
x += w;
y -= h;
setBounds(editor, x, y, e_width, e.height);
}
}
}
public Dimension minimumLayoutSize(Container parent)
{
Dimension d = new Dimension();
if (editor != null)
{
Dimension tmp = editor.getMinimumSize();
d.width += tmp.width;
d.height = tmp.height;
}
int nextWidth = 0;
int previousWidth = 0;
int otherHeight = 0;
if (next != null)
{
Dimension tmp = next.getMinimumSize();
nextWidth = tmp.width;
otherHeight += tmp.height;
}
if (previous != null)
{
Dimension tmp = previous.getMinimumSize();
previousWidth = tmp.width;
otherHeight += tmp.height;
}
d.height = Math.max(d.height, otherHeight);
d.width += Math.max(nextWidth, previousWidth);
return d;
}
public Dimension preferredLayoutSize(Container parent)
{
Dimension d = new Dimension();
if (editor != null)
{
Dimension tmp = editor.getPreferredSize();
d.width += Math.max(tmp.width, 40);
d.height = tmp.height;
}
int nextWidth = 0;
int previousWidth = 0;
int otherHeight = 0;
if (next != null)
{
Dimension tmp = next.getPreferredSize();
nextWidth = tmp.width;
otherHeight += tmp.height;
}
if (previous != null)
{
Dimension tmp = previous.getPreferredSize();
previousWidth = tmp.width;
otherHeight += tmp.height;
}
d.height = Math.max(d.height, otherHeight);
d.width += Math.max(nextWidth, previousWidth);
return d;
}
public void removeLayoutComponent(Component child)
{
if (child == editor)
editor = null;
else if (child == next)
next = null;
else if (previous == child)
previous = null;
}
public void addLayoutComponent(String name, Component child)
{
if ("Editor".equals(name))
editor = child;
else if ("Next".equals(name))
next = child;
else if ("Previous".equals(name))
previous = child;
}
private Dimension minSize(Component c)
{
if (c == null)
return new Dimension();
else
return c.getMinimumSize();
}
private void setBounds(Component c, int x, int y, int w, int h)
{
if (c != null)
c.setBounds(x, y, w, h);
}
private Component editor;
private Component next;
private Component previous;
}
}