1 /* 2 * JaLingo, http://jalingo.sourceforge.net/ 3 * 4 * Copyright (c) 2002-2006 Oleksandr Shyshko 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 package ja.lingo.application.util; 22 23 import ja.centre.gui.resources.Resources; 24 25 import javax.swing.*; 26 import java.awt.event.ActionEvent; 27 import java.awt.event.ActionListener; 28 import java.awt.event.KeyEvent; 29 30 public class Buttons { 31 private static final Resources resources = Resources.forProperties( Buttons.class ); 32 Buttons()33 private Buttons() { 34 } 35 36 // various (text) cancel()37 public static JButton cancel() { return text( "cancel" ); } close()38 public static JButton close() { return text( "close" ); } continue1()39 public static JButton continue1() { return text( "continue" ); } 40 stop()41 public static JButton stop() { return textAcc( "stop" ); } search()42 public static JButton search() { return textAcc( "search" ); } searchNew()43 public static JButton searchNew() { return textAcc( "searchNew"); } 44 45 // various exportAllToHtml()46 public static JButton exportAllToHtml() { return toolBar( "exportAllToHtml" ); } gc()47 public static JButton gc() { return toolBar( "gc" ); } 48 closeCross()49 public static JButton closeCross() { return toolBar( "closeCross" ); } 50 51 52 // find findOptions()53 public static JButton findOptions() { return toolBar( "findOptions" ); } previous()54 public static JButton previous() { return toolBar( "previous" ); } next()55 public static JButton next() { return toolBar( "next" ); } 56 57 // edit add()58 public static JButton add() { return toolBar( "add" ); } remove()59 public static JButton remove() { return toolBar( "remove" ); } moveDown()60 public static JButton moveDown() { return toolBar( "moveDown" ); } moveUp()61 public static JButton moveUp() { return toolBar( "moveUp" ); } 62 text( String key )63 private static JButton text( String key ) { 64 JButton button = new JButton(); 65 button.setText( resources.text( key ) ); 66 return button; 67 } textAcc( String key )68 private static JButton textAcc( String key ) { 69 JButton button = text( key ); 70 button.setMnemonic( resources.stroke( key ).getKeyCode() ); 71 return button; 72 } 73 toolBar( String key )74 private static JButton toolBar( String key ) { 75 final JButton button = new JButton(); 76 button.setFocusable( false ); 77 button.setIcon( resources.icon( key ) ); 78 button.setToolTipText( resources.text( key ) 79 + calculateStroke( resources.stroke( key ) ) ); 80 81 // TODO refactor with Actions.initializeButton 82 button.registerKeyboardAction( new ActionListener() { 83 public void actionPerformed( ActionEvent e ) { 84 // spread event 85 for ( ActionListener listener : button.getActionListeners() ) { 86 listener.actionPerformed( new ActionEvent( button, 0, "" ) ); 87 } 88 } 89 }, resources.stroke( key ), JComponent.WHEN_IN_FOCUSED_WINDOW ); 90 91 return button; 92 } 93 calculateStroke( KeyStroke keyStroke )94 public static String calculateStroke( KeyStroke keyStroke ) { 95 String accText = KeyEvent.getKeyModifiersText( keyStroke.getModifiers() ); 96 if ( accText.length() > 0 ) { 97 accText += "+"; 98 } 99 accText = " (" + accText + KeyEvent.getKeyText( keyStroke.getKeyCode() ) + ")"; 100 return accText; 101 } 102 } 103