001    /*
002     * LAPIS lightweight structured text processing system
003     *
004     * Copyright (C) 1998-2002 Carnegie Mellon University,
005     * Copyright (C) 2003 Massachusetts Institute of Technology.
006     * All rights reserved.
007     *
008     * This library is free software; you can redistribute it
009     * and/or modify it under the terms of the GNU General
010     * Public License as published by the Free Software
011     * Foundation, version 2.
012     *
013     * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014     */
015    
016    package lapisx.swing;
017    
018    import java.awt.*;
019    import javax.swing.*;
020    
021    public abstract class ActionAdapters {
022    
023        public static Action toMenuAction (Action action) {
024            return new MaskedAction (Action.SMALL_ICON, action);
025        }
026    
027        public static Action toToolbarAction (Action action) {
028            return new MaskedAction (Action.NAME, action);
029        }
030    
031        public static JToggleButton toToggleButton (Action action,
032                                                      ButtonGroup group) {
033            JToggleButton button = 
034                new JToggleButton ((Icon) action.getValue (Action.SMALL_ICON));
035    
036            button.addActionListener (action);
037    
038            String tooltip = (String) action.getValue (Action.SHORT_DESCRIPTION);
039            if (tooltip != null)
040                button.setToolTipText (tooltip);
041            
042            if (group != null)
043                group.add (button);
044            return button;
045        }
046    
047        public static JRadioButtonMenuItem toRadioButtonMenuItem (Action action,
048                                                                  ButtonGroup group) {
049            JRadioButtonMenuItem item = 
050                new JRadioButtonMenuItem ((String) action.getValue (Action.NAME));
051    
052            item.addActionListener (action);
053    
054            Object mn = action.getValue (AcceleratedAction.MNEMONIC);
055            if (mn != null)
056                item.setMnemonic (((Character)mn).charValue ());
057    
058            Object acc = action.getValue (AcceleratedAction.ACCELERATOR);
059            if (acc != null)
060                item.setAccelerator ((KeyStroke) acc);
061    
062            if (group != null)
063                group.add (item);
064    
065            return item;
066        }
067        public static JRadioButton toRadioButton (Action action, ButtonGroup group) {
068            JRadioButton item = 
069                new JRadioButton ((String) action.getValue (Action.NAME));
070    
071            item.addActionListener (action);
072    
073            Object mn = action.getValue (AcceleratedAction.MNEMONIC);
074            if (mn != null)
075                item.setMnemonic (((Character)mn).charValue ());
076    
077            if (group != null)
078                group.add (item);
079    
080            return item;
081        }
082        /** 
083         * constructs a new JButton attached to an Action
084         * @returns a new JButton
085         * 
086         **/
087        public static JButton toButton (Action a) {
088            JButton b = new JButton ((String) a.getValue (Action.NAME));
089            b.addActionListener (a);
090            return b;
091        }
092    
093        /** 
094         * constructs a new JCheckBox attached to an Action
095         * @returns a new JCheckBox
096         * 
097         **/
098        public static JCheckBox toCheckBox (Action a) {
099            JCheckBox b = new JCheckBox ((String) a.getValue (Action.NAME));
100            b.addActionListener (a);
101            return b;
102        }
103    
104    
105        /** 
106         * Make a new JButton attached to an Action.  This JButton
107         * is shorter than a default JButtons, and uses a smaller font.
108         * @param action Action to trigger when button is pressed.
109         * @returns a new tiny JButton
110         **/
111        public static JButton toTinyButton (Action a) {
112            int height = new JLabel ("X").getPreferredSize ().height;
113    
114            JButton button = new JButton ((String) a.getValue (Action.NAME));
115            button.addActionListener (a);
116    
117            Font oldFont = button.getFont ();
118            Font font = new Font (oldFont.getName (), 
119                                  oldFont.getStyle (),
120                                  oldFont.getSize () - 2);
121            button.setFont (font);
122    
123            Dimension d = new Dimension (button.getPreferredSize ().width,
124                                         height);
125            button.setPreferredSize (d);
126            button.setMaximumSize (d);
127    
128            return button;
129        }
130    
131    
132    }