001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 005 package graphlab.ui.components.gpropertyeditor.utils; 006 007 import javax.swing.*; 008 import javax.swing.event.ListSelectionEvent; 009 import javax.swing.event.ListSelectionListener; 010 import java.awt.*; 011 import java.awt.event.ActionEvent; 012 import java.awt.event.ActionListener; 013 014 public class JFontChooser extends JComponent { 015 /** 016 * 017 */ 018 private static final long serialVersionUID = 660143495474577287L; 019 public static int OK_OPTION = 0; 020 public static int CANCEL_OPTION = 1; 021 022 private JList fontList, sizeList; 023 private JCheckBox cbBold, cbItalic; 024 private JTextArea txtSample; 025 026 private String[] sizes = new String[] 027 {"2", "4", "6", "8", "10", "12", "13", "14", "16", "18", "20", "22", "24", "30", "36", "48", "72"}; 028 029 public JFontChooser() { 030 // create all components 031 032 fontList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment(). 033 getAvailableFontFamilyNames()) { 034 /** 035 * 036 */ 037 private static final long serialVersionUID = 2307765155498619149L; 038 039 public Dimension getPreferredScrollableViewportSize() { 040 return new Dimension(150, 144); 041 } 042 }; 043 fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 044 045 046 sizeList = new JList(sizes) { 047 /** 048 * 049 */ 050 private static final long serialVersionUID = -2474666139561694389L; 051 052 public Dimension getPreferredScrollableViewportSize() { 053 return new Dimension(25, 144); 054 } 055 }; 056 sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 057 058 059 cbBold = new JCheckBox("Bold"); 060 061 cbItalic = new JCheckBox("Italic"); 062 063 064 txtSample = new JTextArea() { 065 /** 066 * 067 */ 068 private static final long serialVersionUID = 1805024865116989603L; 069 070 public Dimension getPreferredScrollableViewportSize() { 071 return new Dimension(385, 80); 072 } 073 074 public void paint(Graphics g) { 075 ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 076 super.paint(g); 077 } 078 }; 079 txtSample.setText("The quick brown fox jumped over the fence"); 080 081 // set the default font 082 083 setFont(null); 084 085 // add the listeners 086 087 ListSelectionListener listListener = new ListSelectionListener() { 088 public void valueChanged(ListSelectionEvent e) { 089 txtSample.setFont(getCurrentFont()); 090 } 091 }; 092 093 fontList.addListSelectionListener(listListener); 094 sizeList.addListSelectionListener(listListener); 095 096 097 ActionListener cbListener = new ActionListener() { 098 public void actionPerformed(ActionEvent e) { 099 txtSample.setFont(getCurrentFont()); 100 } 101 }; 102 103 cbBold.addActionListener(cbListener); 104 cbItalic.addActionListener(cbListener); 105 106 // build the container 107 108 setLayout(new java.awt.BorderLayout()); 109 110 JPanel leftPanel = new JPanel(); 111 leftPanel.setLayout(new java.awt.BorderLayout()); 112 113 leftPanel.add(new JScrollPane(fontList), java.awt.BorderLayout.CENTER); 114 leftPanel.add(new JScrollPane(sizeList), java.awt.BorderLayout.EAST); 115 116 add(leftPanel, java.awt.BorderLayout.CENTER); 117 118 119 JPanel rightPanel = new JPanel(); 120 rightPanel.setLayout(new java.awt.BorderLayout()); 121 122 JPanel rightPanelSub1 = new JPanel(); 123 rightPanelSub1.setLayout(new java.awt.FlowLayout()); 124 125 rightPanelSub1.add(cbBold); 126 rightPanelSub1.add(cbItalic); 127 128 rightPanel.add(rightPanelSub1, java.awt.BorderLayout.NORTH); 129 130 JPanel rightPanelSub2 = new JPanel(); 131 rightPanelSub2.setLayout(new java.awt.GridLayout(2, 1)); 132 133 rightPanel.add(rightPanelSub2, java.awt.BorderLayout.SOUTH); 134 135 add(rightPanel, java.awt.BorderLayout.EAST); 136 137 add(new JScrollPane(txtSample), java.awt.BorderLayout.SOUTH); 138 139 setSize(200, 200); 140 } 141 142 public void setFont(Font font) { 143 if (font == null) font = txtSample.getFont(); 144 145 fontList.setSelectedValue(font.getName(), true); 146 fontList.ensureIndexIsVisible(fontList.getSelectedIndex()); 147 sizeList.setSelectedValue("" + font.getSize(), true); 148 sizeList.ensureIndexIsVisible(sizeList.getSelectedIndex()); 149 150 cbBold.setSelected(font.isBold()); 151 cbItalic.setSelected(font.isItalic()); 152 } 153 154 public Font getFont() { 155 return getCurrentFont(); 156 } 157 158 private Font getCurrentFont() { 159 String fontFamily = (String) fontList.getSelectedValue(); 160 int fontSize = Integer.parseInt((String) sizeList.getSelectedValue()); 161 162 int fontType = Font.PLAIN; 163 164 if (cbBold.isSelected()) fontType += Font.BOLD; 165 if (cbItalic.isSelected()) fontType += Font.ITALIC; 166 167 return new Font(fontFamily, fontType, fontSize); 168 } 169 }