2008-04-20 12:03:19 -04:00
package net.sourceforge.tuned.ui ;
2008-10-21 13:49:08 -04:00
import java.awt.Color ;
2009-04-04 15:36:12 -04:00
import java.awt.Component ;
2008-04-20 12:03:19 -04:00
import java.awt.Dimension ;
2008-06-21 15:24:18 -04:00
import java.awt.Graphics2D ;
import java.awt.Image ;
2008-04-20 12:03:19 -04:00
import java.awt.Point ;
import java.awt.Window ;
2009-02-11 13:42:29 -05:00
import java.awt.dnd.DnDConstants ;
2008-04-27 13:36:27 -04:00
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
2009-02-11 13:42:29 -05:00
import java.awt.event.MouseEvent ;
2008-06-21 15:24:18 -04:00
import java.awt.image.BufferedImage ;
2008-04-20 12:03:19 -04:00
2009-05-16 07:58:28 -04:00
import javax.swing.AbstractAction ;
2008-04-20 12:03:19 -04:00
import javax.swing.Action ;
2008-06-21 15:24:18 -04:00
import javax.swing.Icon ;
2008-07-30 18:37:01 -04:00
import javax.swing.ImageIcon ;
2009-08-03 19:51:31 -04:00
import javax.swing.JButton ;
2008-04-20 12:03:19 -04:00
import javax.swing.JComponent ;
import javax.swing.KeyStroke ;
2009-02-11 13:42:29 -05:00
import javax.swing.ListSelectionModel ;
2008-07-30 18:37:01 -04:00
import javax.swing.SwingUtilities ;
2008-04-27 13:36:27 -04:00
import javax.swing.Timer ;
2009-02-11 13:42:29 -05:00
import javax.swing.event.MouseInputListener ;
import javax.swing.plaf.basic.BasicTableUI ;
2009-05-16 07:58:28 -04:00
import javax.swing.text.JTextComponent ;
import javax.swing.undo.UndoManager ;
2009-03-14 06:20:59 -04:00
2009-01-24 19:08:57 -05:00
public final class TunedUtilities {
2008-04-20 12:03:19 -04:00
2008-10-21 13:49:08 -04:00
public static final Color TRANSLUCENT = new Color ( 255 , 255 , 255 , 0 ) ;
2009-06-13 05:53:48 -04:00
2008-07-30 18:37:01 -04:00
public static void checkEventDispatchThread ( ) {
if ( ! SwingUtilities . isEventDispatchThread ( ) ) {
throw new IllegalStateException ( " Method must be accessed from the Swing Event Dispatch Thread, but was called on Thread \" " + Thread . currentThread ( ) . getName ( ) + " \" " ) ;
}
2008-05-03 16:43:15 -04:00
}
2009-02-15 12:58:32 -05:00
public static Color derive ( Color color , float alpha ) {
return new Color ( ( ( int ) ( ( alpha * 255 ) ) < < 24 ) | ( color . getRGB ( ) & 0x00FFFFFF ) , true ) ;
}
2009-08-03 19:51:31 -04:00
public static JButton createImageButton ( Action action ) {
JButton button = new JButton ( action ) ;
button . setHideActionText ( true ) ;
button . setOpaque ( false ) ;
return button ;
}
2009-05-16 07:58:28 -04:00
public static void installAction ( JComponent component , KeyStroke keystroke , Action action ) {
Object key = action . getValue ( Action . NAME ) ;
if ( key = = null )
throw new IllegalArgumentException ( " Action must have a name " ) ;
2008-04-20 12:03:19 -04:00
component . getInputMap ( JComponent . WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ) . put ( keystroke , key ) ;
component . getActionMap ( ) . put ( key , action ) ;
}
2009-05-16 07:58:28 -04:00
public static UndoManager installUndoSupport ( JTextComponent component ) {
final UndoManager undoSupport = new UndoManager ( ) ;
// install undo listener
component . getDocument ( ) . addUndoableEditListener ( undoSupport ) ;
// install undo action
installAction ( component , KeyStroke . getKeyStroke ( " control Z " ) , new AbstractAction ( " Undo " ) {
@Override
public void actionPerformed ( ActionEvent e ) {
if ( undoSupport . canUndo ( ) )
undoSupport . undo ( ) ;
}
} ) ;
// install redo action
installAction ( component , KeyStroke . getKeyStroke ( " control Y " ) , new AbstractAction ( " Redo " ) {
@Override
public void actionPerformed ( ActionEvent e ) {
if ( undoSupport . canRedo ( ) )
undoSupport . redo ( ) ;
}
} ) ;
return undoSupport ;
}
2009-05-17 11:09:09 -04:00
public static Window getWindow ( Object component ) {
2009-04-04 15:36:12 -04:00
if ( component instanceof Window )
return ( Window ) component ;
2009-05-17 11:09:09 -04:00
if ( component instanceof Component )
return SwingUtilities . getWindowAncestor ( ( Component ) component ) ;
return null ;
2009-04-04 15:36:12 -04:00
}
2009-08-02 07:48:45 -04:00
public static Point getOffsetLocation ( Window owner ) {
2009-07-15 05:22:40 -04:00
if ( owner = = null ) {
Window [ ] toplevel = Window . getOwnerlessWindows ( ) ;
if ( toplevel . length = = 0 )
return new Point ( 120 , 80 ) ;
// assume first top-level window as point of reference
owner = toplevel [ 0 ] ;
}
2008-04-20 12:03:19 -04:00
Point p = owner . getLocation ( ) ;
Dimension d = owner . getSize ( ) ;
return new Point ( p . x + d . width / 4 , p . y + d . height / 7 ) ;
}
2008-04-27 13:36:27 -04:00
2008-06-21 15:24:18 -04:00
public static Image getImage ( Icon icon ) {
2009-01-24 19:08:57 -05:00
if ( icon = = null )
return null ;
if ( icon instanceof ImageIcon )
2008-07-30 18:37:01 -04:00
return ( ( ImageIcon ) icon ) . getImage ( ) ;
2008-06-21 15:24:18 -04:00
2009-01-24 19:08:57 -05:00
// draw icon into a new image
2008-06-21 15:24:18 -04:00
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-12-31 06:54:44 -05:00
public static Dimension getDimension ( Icon icon ) {
return new Dimension ( icon . getIconWidth ( ) , icon . getIconHeight ( ) ) ;
}
2008-04-27 13:36:27 -04: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-07-30 18:37:01 -04:00
2009-02-11 13:42:29 -05:00
/ * *
* When trying to drag a row of a multi - select JTable , it will start selecting rows instead
* of initiating a drag . This TableUI will give the JTable proper dnd behaviour .
* /
public static class DragDropRowTableUI extends BasicTableUI {
@Override
protected MouseInputListener createMouseInputListener ( ) {
return new DragDropRowMouseInputHandler ( ) ;
}
2009-06-13 05:53:48 -04:00
2009-02-11 13:42:29 -05:00
protected class DragDropRowMouseInputHandler extends MouseInputHandler {
@Override
public void mouseDragged ( MouseEvent e ) {
// Only do special handling if we are drag enabled with multiple selection
if ( table . getDragEnabled ( ) & & table . getSelectionModel ( ) . getSelectionMode ( ) = = ListSelectionModel . MULTIPLE_INTERVAL_SELECTION ) {
table . getTransferHandler ( ) . exportAsDrag ( table , e , DnDConstants . ACTION_COPY ) ;
} else {
super . mouseDragged ( e ) ;
}
}
}
}
2009-06-13 05:53:48 -04:00
2008-10-10 15:20:37 -04:00
/ * *
* Dummy constructor to prevent instantiation .
* /
2009-01-24 19:08:57 -05:00
private TunedUtilities ( ) {
2008-10-10 15:20:37 -04:00
throw new UnsupportedOperationException ( ) ;
2008-07-30 18:37:01 -04:00
}
2008-04-20 12:03:19 -04:00
}