View Javadoc

1   package nl.tudelft.simulation.introspection.mapping;
2   
3   import java.awt.Component;
4   import java.awt.Dialog;
5   import java.awt.Font;
6   
7   import javax.swing.AbstractCellEditor;
8   import javax.swing.JLabel;
9   import javax.swing.JTable;
10  import javax.swing.SwingUtilities;
11  import javax.swing.table.TableCellEditor;
12  
13  import org.jfree.ui.FontChooserDialog;
14  
15  /***
16   * An editor for fonts.
17   * <p>
18   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
19   * University of Technology </a>, the Netherlands. <br>
20   * See for project information <a
21   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
22   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
23   * General Public License (LGPL) </a>, no warranty.
24   * 
25   * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
26   * @version 1.2 Apr 15, 2004
27   * @since 1.5
28   */
29  public class MyFontEditor extends AbstractCellEditor implements TableCellEditor
30  {
31      /*** the value */
32      protected Font value;
33  
34      /*** the cellPanel */
35      protected JLabel cellPanel = new JLabel();
36  
37      /***
38       * @see javax.swing.CellEditor#getCellEditorValue()
39       */
40      public Object getCellEditorValue()
41      {
42          return this.value;
43      }
44  
45      /***
46       * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(JTable,
47       *      Object, boolean, int, int)
48       */
49      public Component getTableCellEditorComponent(final JTable table,
50              final Object value, final boolean isSelected, final int row,
51              final int column)
52      {
53          this.cellPanel.setBackground(this.cellPanel.getBackground().darker());
54          this.value = (Font) value;
55          MyFontChooserDialog chooser = new MyFontChooserDialog(
56                  (Dialog) SwingUtilities.getWindowAncestor(table),
57                  "Font selection", true, (Font) value);
58  
59          chooser.addUserListener(new UserListener(chooser));
60          chooser.pack();
61          chooser.setVisible(true);
62          return this.cellPanel;
63      }
64  
65      /*** the listener */
66      private class UserListener implements
67              MyFontChooserDialog.UserListenerInterface
68      {
69          /*** the fileChooser */
70          private FontChooserDialog chooser;
71  
72          /***
73           * constructs a new UserListener
74           * 
75           * @param chooser the chooser we listen at
76           */
77          public UserListener(FontChooserDialog chooser)
78          {
79              this.chooser = chooser;
80          }
81  
82          /***
83           * @see nl.tudelft.simulation.introspection.mapping.MyFontChooserDialog.UserListenerInterface
84           *      #okActionPerformed()
85           */
86          public void okActionPerformed()
87          {
88              MyFontEditor.this.value = this.chooser.getSelectedFont();
89              MyFontEditor.this.cellPanel.setText("" + MyFontEditor.this.value);
90              MyFontEditor.this.cellPanel
91                      .paintImmediately(MyFontEditor.this.cellPanel.getBounds());
92              MyFontEditor.this.stopCellEditing();
93          }
94  
95          /***
96           * @see nl.tudelft.simulation.introspection.mapping.MyFontChooserDialog.UserListenerInterface
97           *      #cancelActionPerformed()
98           */
99          public void cancelActionPerformed()
100         {
101             MyFontEditor.this.cancelCellEditing();
102         }
103     }
104 }