2008-04-20 16:03:19 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned.ui;
|
|
|
|
|
|
|
|
|
|
|
|
import java.awt.Dimension;
|
2008-06-21 19:24:18 +00:00
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import java.awt.Image;
|
2008-04-20 16:03:19 +00:00
|
|
|
import java.awt.Point;
|
|
|
|
import java.awt.Window;
|
2008-04-27 17:36:27 +00:00
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
2008-06-21 19:24:18 +00:00
|
|
|
import java.awt.image.BufferedImage;
|
2008-04-20 16:03:19 +00:00
|
|
|
|
|
|
|
import javax.swing.Action;
|
2008-06-21 19:24:18 +00:00
|
|
|
import javax.swing.Icon;
|
2008-04-20 16:03:19 +00:00
|
|
|
import javax.swing.JComponent;
|
|
|
|
import javax.swing.JDialog;
|
|
|
|
import javax.swing.KeyStroke;
|
2008-04-27 17:36:27 +00:00
|
|
|
import javax.swing.Timer;
|
2008-04-20 16:03:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
public class TunedUtil {
|
|
|
|
|
2008-05-03 20:43:15 +00:00
|
|
|
private TunedUtil() {
|
|
|
|
// hide constructor
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-20 16:03:19 +00:00
|
|
|
public static void registerActionForKeystroke(JComponent component, KeyStroke keystroke, Action action) {
|
|
|
|
Integer key = action.hashCode();
|
|
|
|
component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keystroke, key);
|
|
|
|
component.getActionMap().put(key, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Point getPreferredLocation(JDialog dialog) {
|
|
|
|
Window owner = dialog.getOwner();
|
|
|
|
|
|
|
|
if (owner == null)
|
|
|
|
return new Point(120, 80);
|
|
|
|
|
|
|
|
Point p = owner.getLocation();
|
|
|
|
Dimension d = owner.getSize();
|
|
|
|
|
|
|
|
return new Point(p.x + d.width / 4, p.y + d.height / 7);
|
|
|
|
}
|
2008-04-27 17:36:27 +00:00
|
|
|
|
|
|
|
|
2008-06-21 19:24:18 +00:00
|
|
|
public static Image getImage(Icon icon) {
|
|
|
|
//TODO uncomment
|
|
|
|
// if (icon instanceof ImageIcon) {
|
|
|
|
// return ((ImageIcon) icon).getImage();
|
|
|
|
// }
|
|
|
|
|
|
|
|
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
|
|
|
|
|
|
Graphics2D g2d = image.createGraphics();
|
|
|
|
icon.paintIcon(null, g2d, 0, 0);
|
|
|
|
g2d.dispose();
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-27 17:36:27 +00:00
|
|
|
public static Timer invokeLater(int delay, final Runnable runnable) {
|
|
|
|
Timer timer = new Timer(delay, new ActionListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
runnable.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
timer.setRepeats(false);
|
|
|
|
timer.start();
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2008-04-20 16:03:19 +00:00
|
|
|
}
|