2008-04-20 16:03:19 +00:00
package net.sourceforge.tuned.ui ;
2011-11-28 10:24:46 +00:00
import static java.util.Collections.* ;
2011-11-26 09:50:31 +00:00
import static javax.swing.JOptionPane.* ;
2008-10-21 17:49:08 +00:00
import java.awt.Color ;
2009-04-04 19:36:12 +00:00
import java.awt.Component ;
2008-04-20 16:03:19 +00:00
import java.awt.Dimension ;
2010-01-26 19:08:09 +00:00
import java.awt.Frame ;
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 ;
2009-02-11 18:42:29 +00:00
import java.awt.dnd.DnDConstants ;
2008-04-27 17:36:27 +00:00
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
2012-10-28 03:36:36 +00:00
import java.awt.event.KeyEvent ;
2009-02-11 18:42:29 +00:00
import java.awt.event.MouseEvent ;
2008-06-21 19:24:18 +00:00
import java.awt.image.BufferedImage ;
2011-11-26 09:50:31 +00:00
import java.lang.reflect.InvocationTargetException ;
2011-11-28 10:24:46 +00:00
import java.util.ArrayList ;
import java.util.List ;
2014-01-10 10:55:57 +00:00
import java.util.regex.Pattern ;
2008-04-20 16:03:19 +00:00
2009-05-16 11:58:28 +00:00
import javax.swing.AbstractAction ;
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-07-30 22:37:01 +00:00
import javax.swing.ImageIcon ;
2009-08-03 23:51:31 +00:00
import javax.swing.JButton ;
2008-04-20 16:03:19 +00:00
import javax.swing.JComponent ;
2011-11-26 09:50:31 +00:00
import javax.swing.JOptionPane ;
2008-04-20 16:03:19 +00:00
import javax.swing.KeyStroke ;
2009-02-11 18:42:29 +00:00
import javax.swing.ListSelectionModel ;
2008-07-30 22:37:01 +00:00
import javax.swing.SwingUtilities ;
2008-04-27 17:36:27 +00:00
import javax.swing.Timer ;
2009-02-11 18:42:29 +00:00
import javax.swing.event.MouseInputListener ;
import javax.swing.plaf.basic.BasicTableUI ;
2009-05-16 11:58:28 +00:00
import javax.swing.text.JTextComponent ;
import javax.swing.undo.UndoManager ;
2009-03-14 10:20:59 +00:00
2009-01-25 00:08:57 +00:00
public final class TunedUtilities {
2013-09-21 07:29:57 +00:00
2008-10-21 17:49:08 +00:00
public static final Color TRANSLUCENT = new Color ( 255 , 255 , 255 , 0 ) ;
2013-09-21 07:29:57 +00:00
2008-07-30 22:37:01 +00: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 20:43:15 +00:00
}
2013-09-21 07:29:57 +00:00
2012-07-14 19:51:46 +00:00
public static Color interpolateHSB ( Color c1 , Color c2 , float f ) {
float [ ] hsb1 = Color . RGBtoHSB ( c1 . getRed ( ) , c1 . getGreen ( ) , c1 . getBlue ( ) , null ) ;
float [ ] hsb2 = Color . RGBtoHSB ( c2 . getRed ( ) , c2 . getGreen ( ) , c2 . getBlue ( ) , null ) ;
float [ ] hsb = new float [ 3 ] ;
2013-09-21 07:29:57 +00:00
2012-07-14 19:51:46 +00:00
for ( int i = 0 ; i < hsb . length ; i + + ) {
hsb [ i ] = hsb1 [ i ] + ( ( hsb2 [ i ] - hsb1 [ i ] ) * f ) ;
}
2013-09-21 07:29:57 +00:00
2012-07-14 19:51:46 +00:00
return Color . getHSBColor ( hsb [ 0 ] , hsb [ 1 ] , hsb [ 2 ] ) ;
}
2013-09-21 07:29:57 +00:00
2012-07-14 19:51:46 +00:00
public static String escapeHTML ( String s ) {
char [ ] sc = new char [ ] { '&' , '<' , '>' , '"' , '\'' } ;
for ( char c : sc ) {
s = s . replace ( Character . toString ( c ) , String . format ( " &#%d; " , ( int ) c ) ) ; // e.g. &
}
return s ;
}
2013-09-21 07:29:57 +00:00
2009-02-15 17:58:32 +00:00
public static Color derive ( Color color , float alpha ) {
return new Color ( ( ( int ) ( ( alpha * 255 ) ) < < 24 ) | ( color . getRGB ( ) & 0x00FFFFFF ) , true ) ;
}
2013-09-21 07:29:57 +00:00
2014-01-10 07:31:50 +00:00
public static String toHex ( Color c ) {
return String . format ( " #%02x%02x%02x " , c . getRed ( ) , c . getGreen ( ) , c . getBlue ( ) ) ;
}
2013-04-06 14:43:48 +00:00
public static boolean isShiftOrAltDown ( ActionEvent evt ) {
return checkModifiers ( evt . getModifiers ( ) , ActionEvent . SHIFT_MASK ) | | checkModifiers ( evt . getModifiers ( ) , ActionEvent . ALT_MASK ) ;
2011-08-22 03:43:22 +00:00
}
2013-09-21 07:29:57 +00:00
2011-08-22 03:43:22 +00:00
public static boolean checkModifiers ( int modifiers , int mask ) {
return ( ( modifiers & mask ) = = mask ) ;
}
2013-09-21 07:29:57 +00:00
2009-08-03 23:51:31 +00:00
public static JButton createImageButton ( Action action ) {
JButton button = new JButton ( action ) ;
button . setHideActionText ( true ) ;
button . setOpaque ( false ) ;
2013-09-21 07:29:57 +00:00
2009-08-03 23:51:31 +00:00
return button ;
}
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
public static void installAction ( JComponent component , KeyStroke keystroke , Action action ) {
Object key = action . getValue ( Action . NAME ) ;
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
if ( key = = null )
throw new IllegalArgumentException ( " Action must have a name " ) ;
2013-09-21 07:29:57 +00:00
2008-04-20 16:03:19 +00:00
component . getInputMap ( JComponent . WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ) . put ( keystroke , key ) ;
component . getActionMap ( ) . put ( key , action ) ;
}
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
public static UndoManager installUndoSupport ( JTextComponent component ) {
final UndoManager undoSupport = new UndoManager ( ) ;
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
// install undo listener
component . getDocument ( ) . addUndoableEditListener ( undoSupport ) ;
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
// install undo action
2012-10-28 03:36:36 +00:00
installAction ( component , KeyStroke . getKeyStroke ( KeyEvent . VK_Z , KeyEvent . CTRL_MASK ) , new AbstractAction ( " Undo " ) {
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
@Override
public void actionPerformed ( ActionEvent e ) {
if ( undoSupport . canUndo ( ) )
undoSupport . undo ( ) ;
}
} ) ;
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
// install redo action
2012-10-28 03:36:36 +00:00
installAction ( component , KeyStroke . getKeyStroke ( KeyEvent . VK_Y , KeyEvent . CTRL_MASK ) , new AbstractAction ( " Redo " ) {
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
@Override
public void actionPerformed ( ActionEvent e ) {
if ( undoSupport . canRedo ( ) )
undoSupport . redo ( ) ;
}
} ) ;
2013-09-21 07:29:57 +00:00
2009-05-16 11:58:28 +00:00
return undoSupport ;
}
2013-09-21 07:29:57 +00:00
2010-01-26 19:08:09 +00:00
public static boolean isMaximized ( Frame frame ) {
return ( frame . getExtendedState ( ) & Frame . MAXIMIZED_BOTH ) ! = 0 ;
}
2013-09-21 07:29:57 +00:00
2011-11-28 10:24:46 +00:00
public static List < String > showMultiValueInputDialog ( final String text , final String initialValue , final String title , final Component parent ) throws InvocationTargetException , InterruptedException {
String input = showInputDialog ( text , initialValue , title , parent ) ;
if ( input = = null | | input . isEmpty ( ) ) {
return emptyList ( ) ;
}
2013-09-21 07:29:57 +00:00
2014-01-10 10:55:57 +00:00
for ( char separator : new char [ ] { '|' , ';' } ) {
2011-11-28 10:24:46 +00:00
if ( input . indexOf ( separator ) > = 0 ) {
List < String > values = new ArrayList < String > ( ) ;
2014-01-10 10:55:57 +00:00
for ( String field : input . split ( Pattern . quote ( Character . toString ( separator ) ) ) ) {
field = field . trim ( ) ;
2011-11-28 10:24:46 +00:00
if ( field . length ( ) > 0 ) {
values . add ( field ) ;
}
}
2013-09-21 07:29:57 +00:00
2011-11-28 10:24:46 +00:00
if ( values . size ( ) > 0 ) {
return values ;
}
}
}
2013-09-21 07:29:57 +00:00
2011-11-28 10:24:46 +00:00
return singletonList ( input ) ;
}
2013-09-21 07:29:57 +00:00
2011-11-28 10:24:46 +00:00
public static String showInputDialog ( final String text , final String initialValue , final String title , final Component parent ) throws InvocationTargetException , InterruptedException {
2011-11-26 09:50:31 +00:00
final StringBuilder buffer = new StringBuilder ( ) ;
2013-09-21 07:29:57 +00:00
Runnable runnable = new Runnable ( ) {
2011-11-26 09:50:31 +00:00
@Override
public void run ( ) {
Object value = JOptionPane . showInputDialog ( parent , text , title , PLAIN_MESSAGE , null , null , initialValue ) ;
if ( value ! = null ) {
2014-01-10 10:55:57 +00:00
buffer . append ( value . toString ( ) . trim ( ) ) ;
2011-11-26 09:50:31 +00:00
}
}
2013-09-21 07:29:57 +00:00
} ;
if ( SwingUtilities . isEventDispatchThread ( ) ) {
runnable . run ( ) ;
} else {
SwingUtilities . invokeAndWait ( runnable ) ;
}
2011-11-26 09:50:31 +00:00
return buffer . length ( ) = = 0 ? null : buffer . toString ( ) ;
}
2013-09-21 07:29:57 +00:00
2009-05-17 15:09:09 +00:00
public static Window getWindow ( Object component ) {
2009-04-04 19:36:12 +00:00
if ( component instanceof Window )
return ( Window ) component ;
2013-09-21 07:29:57 +00:00
2009-05-17 15:09:09 +00:00
if ( component instanceof Component )
return SwingUtilities . getWindowAncestor ( ( Component ) component ) ;
2013-09-21 07:29:57 +00:00
2009-05-17 15:09:09 +00:00
return null ;
2009-04-04 19:36:12 +00:00
}
2013-09-21 07:29:57 +00:00
2009-08-02 11:48:45 +00:00
public static Point getOffsetLocation ( Window owner ) {
2009-07-15 09:22:40 +00:00
if ( owner = = null ) {
Window [ ] toplevel = Window . getOwnerlessWindows ( ) ;
2013-09-21 07:29:57 +00:00
2009-07-15 09:22:40 +00:00
if ( toplevel . length = = 0 )
return new Point ( 120 , 80 ) ;
2013-09-21 07:29:57 +00:00
2009-07-15 09:22:40 +00:00
// assume first top-level window as point of reference
owner = toplevel [ 0 ] ;
}
2013-09-21 07:29:57 +00:00
2008-04-20 16:03:19 +00:00
Point p = owner . getLocation ( ) ;
Dimension d = owner . getSize ( ) ;
2013-09-21 07:29:57 +00:00
2008-04-20 16:03:19 +00:00
return new Point ( p . x + d . width / 4 , p . y + d . height / 7 ) ;
}
2013-09-21 07:29:57 +00:00
2008-06-21 19:24:18 +00:00
public static Image getImage ( Icon icon ) {
2009-01-25 00:08:57 +00:00
if ( icon = = null )
return null ;
2013-09-21 07:29:57 +00:00
2009-01-25 00:08:57 +00:00
if ( icon instanceof ImageIcon )
2008-07-30 22:37:01 +00:00
return ( ( ImageIcon ) icon ) . getImage ( ) ;
2013-09-21 07:29:57 +00:00
2009-01-25 00:08:57 +00:00
// draw icon into a new image
2008-06-21 19:24:18 +00:00
BufferedImage image = new BufferedImage ( icon . getIconWidth ( ) , icon . getIconHeight ( ) , BufferedImage . TYPE_INT_ARGB ) ;
2013-09-21 07:29:57 +00:00
2008-06-21 19:24:18 +00:00
Graphics2D g2d = image . createGraphics ( ) ;
icon . paintIcon ( null , g2d , 0 , 0 ) ;
g2d . dispose ( ) ;
2013-09-21 07:29:57 +00:00
2008-06-21 19:24:18 +00:00
return image ;
}
2013-09-21 07:29:57 +00:00
2008-12-31 11:54:44 +00:00
public static Dimension getDimension ( Icon icon ) {
return new Dimension ( icon . getIconWidth ( ) , icon . getIconHeight ( ) ) ;
}
2013-09-21 07:29:57 +00:00
2008-04-27 17:36:27 +00:00
public static Timer invokeLater ( int delay , final Runnable runnable ) {
Timer timer = new Timer ( delay , new ActionListener ( ) {
2013-09-21 07:29:57 +00:00
2008-04-27 17:36:27 +00:00
@Override
public void actionPerformed ( ActionEvent e ) {
runnable . run ( ) ;
}
} ) ;
2013-09-21 07:29:57 +00:00
2008-04-27 17:36:27 +00:00
timer . setRepeats ( false ) ;
timer . start ( ) ;
2013-09-21 07:29:57 +00:00
2008-04-27 17:36:27 +00:00
return timer ;
}
2013-09-21 07:29:57 +00:00
2009-02-11 18:42:29 +00:00
/ * *
2012-10-28 03:36:36 +00: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 .
2009-02-11 18:42:29 +00:00
* /
public static class DragDropRowTableUI extends BasicTableUI {
2013-09-21 07:29:57 +00:00
2009-02-11 18:42:29 +00:00
@Override
protected MouseInputListener createMouseInputListener ( ) {
return new DragDropRowMouseInputHandler ( ) ;
}
2013-09-21 07:29:57 +00:00
2009-02-11 18:42:29 +00:00
protected class DragDropRowMouseInputHandler extends MouseInputHandler {
2013-09-21 07:29:57 +00:00
2009-02-11 18:42:29 +00:00
@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 ) ;
}
}
}
}
2013-09-21 07:29:57 +00:00
2008-10-10 19:20:37 +00:00
/ * *
* Dummy constructor to prevent instantiation .
* /
2009-01-25 00:08:57 +00:00
private TunedUtilities ( ) {
2008-10-10 19:20:37 +00:00
throw new UnsupportedOperationException ( ) ;
2008-07-30 22:37:01 +00:00
}
2013-09-21 07:29:57 +00:00
2008-04-20 16:03:19 +00:00
}