1 14 package org.compiere.plaf; 15 16 import java.awt.BorderLayout ; 17 import java.awt.Dialog ; 18 import java.awt.Dimension ; 19 import java.awt.FlowLayout ; 20 import java.awt.Font ; 21 import java.awt.GraphicsEnvironment ; 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.util.Arrays ; 28 import java.util.ResourceBundle ; 29 30 import javax.swing.BorderFactory ; 31 import javax.swing.JDialog ; 32 import javax.swing.JTextArea ; 33 34 import org.compiere.swing.CButton; 35 import org.compiere.swing.CComboBox; 36 import org.compiere.swing.CLabel; 37 import org.compiere.swing.CPanel; 38 39 45 public class FontChooser extends JDialog implements ActionListener  46 { 47 54 public static Font showDialog (Dialog owner, String title, Font initFont) 55 { 56 Font retValue = initFont; 57 FontChooser fc = new FontChooser(owner, title, initFont); 58 retValue = fc.getFont(); 59 fc = null; 60 return retValue; 61 } 63 64 65 72 public FontChooser(Dialog owner, String title, Font initFont) 73 { 74 super(owner, title, true); 75 try 76 { 77 jbInit(); 78 dynInit(); 79 setFont(initFont); 80 CompierePLAF.showCenterScreen(this); 81 } 82 catch(Exception ex) 83 { 84 System.err.println ("FontChooser"); 85 ex.printStackTrace(); 86 } 87 } 89 92 public FontChooser() 93 { 94 this (null, s_res.getString("FontChooser"), null); 95 } 97 static ResourceBundle s_res = ResourceBundle.getBundle("org.compiere.plaf.PlafRes"); 98 99 100 public static FontStyle[] s_list = { 101 new FontStyle(s_res.getString("Plain"), Font.PLAIN), 102 new FontStyle(s_res.getString("Italic"), Font.ITALIC), 103 new FontStyle(s_res.getString("Bold"), Font.BOLD), 104 new FontStyle(s_res.getString("BoldItalic"), Font.BOLD|Font.ITALIC)}; 105 106 private Font m_font = super.getFont(); 107 private Font m_retFont = null; 108 109 private boolean m_setting = false; 110 111 private CPanel mainPanel = new CPanel(); 112 private BorderLayout mainLayout = new BorderLayout (); 113 private CPanel selectPanel = new CPanel(); 114 private CLabel nameLabel = new CLabel(); 115 private CComboBox fontName = new CComboBox(); 116 private CLabel sizeLabel = new CLabel(); 117 private CLabel styleLabel = new CLabel(); 118 private CComboBox fontStyle = new CComboBox(); 119 private CComboBox fontSize = new CComboBox(); 120 private JTextArea fontTest = new JTextArea (); 121 private JTextArea fontInfo = new JTextArea (); 122 private GridBagLayout selectLayout = new GridBagLayout (); 123 private CPanel confirmPanel = new CPanel(); 124 private CButton bCancel = CompierePLAF.getCancelButton(); 125 private CButton bOK = CompierePLAF.getOKButton(); 126 private FlowLayout confirmLayout = new FlowLayout (); 127 128 132 private void jbInit() throws Exception  133 { 134 CompiereColor.setBackground(this); 135 mainPanel.setLayout(mainLayout); 136 nameLabel.setText(s_res.getString("Name")); 137 selectPanel.setLayout(selectLayout); 138 sizeLabel.setText(s_res.getString("Size")); 139 styleLabel.setText(s_res.getString("Style")); 140 fontTest.setText(s_res.getString("TestString")); 141 fontTest.setLineWrap(true); 142 fontTest.setWrapStyleWord(true); 143 fontTest.setBackground(CompierePLAF.getFieldBackground_Inactive()); 144 fontTest.setBorder(BorderFactory.createLoweredBevelBorder()); 145 fontTest.setPreferredSize(new Dimension (220, 100)); 146 fontInfo.setText(s_res.getString("FontString")); 147 fontInfo.setLineWrap(true); 148 fontInfo.setWrapStyleWord(true); 149 fontInfo.setBackground(CompierePLAF.getFieldBackground_Inactive()); 150 fontInfo.setOpaque(false); 151 fontInfo.setEditable(false); 152 confirmPanel.setLayout(confirmLayout); 153 confirmLayout.setAlignment(FlowLayout.RIGHT); 154 confirmPanel.setOpaque(false); 155 selectPanel.setOpaque(false); 156 getContentPane().add(mainPanel); 157 mainPanel.add(selectPanel, BorderLayout.CENTER); 158 selectPanel.add(nameLabel, new GridBagConstraints (0, 0, 1, 1, 0.0, 0.0 159 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 160 selectPanel.add(fontName, new GridBagConstraints (1, 0, 1, 1, 0.0, 0.0 161 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 162 selectPanel.add(sizeLabel, new GridBagConstraints (0, 1, 1, 1, 0.0, 0.0 163 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 164 selectPanel.add(styleLabel, new GridBagConstraints (0, 2, 1, 1, 0.0, 0.0 165 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 166 selectPanel.add(fontStyle, new GridBagConstraints (1, 2, 2, 1, 0.0, 0.0 167 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 168 selectPanel.add(fontSize, new GridBagConstraints (1, 1, 2, 1, 0.0, 0.0 169 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 5, 5, 5), 0, 0)); 170 selectPanel.add(fontTest, new GridBagConstraints (0, 3, 2, 1, 0.0, 0.0 171 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets (20, 5, 5, 5), 0, 0)); 172 selectPanel.add(fontInfo, new GridBagConstraints (0, 4, 2, 1, 0.0, 0.0 173 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets (10, 5, 10, 5), 0, 0)); 174 mainPanel.add(confirmPanel, BorderLayout.SOUTH); 176 confirmPanel.add(bCancel, null); 177 confirmPanel.add(bOK, null); 178 bCancel.addActionListener(this); 179 bOK.addActionListener(this); 180 } 182 185 private void dynInit() 186 { 187 String [] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 188 Arrays.sort(names); 189 for (int i = 0; i < names.length; i++) 190 fontName.addItem(names[i]); 191 fontName.addActionListener(this); 192 for (int i = 6; i < 32; i++) 194 fontSize.addItem(String.valueOf(i)); 195 fontSize.addActionListener(this); 196 for (int i = 0; i < s_list.length; i++) 198 fontStyle.addItem(s_list[i]); 199 fontStyle.addActionListener(this); 200 } 202 206 public void setFont(Font font) 207 { 208 if (font == null) 209 return; 210 if (m_retFont == null) 212 m_retFont = font; 213 fontTest.setFont(font); 215 fontInfo.setFont(font); 216 fontInfo.setText(font.toString()); 217 m_setting = true; 219 fontName.setSelectedItem(font.getName()); 220 if (!fontName.getSelectedItem().equals(font.getName())) 221 System.err.println("FontChooser.setFont" + fontName.getSelectedItem().toString() + " <> " + font.getName()); 222 fontSize.setSelectedItem(String.valueOf(font.getSize())); 224 if (!fontSize.getSelectedItem().equals(String.valueOf(font.getSize()))) 225 System.err.println("FontChooser.setFont" + fontSize.getSelectedItem() + " <> " + font.getSize()); 226 for (int i = 0; i < s_list.length; i++) 228 if (s_list[i].getID() == font.getStyle()) 229 fontStyle.setSelectedItem(s_list[i]); 230 if (((FontStyle)fontStyle.getSelectedItem()).getID() != font.getStyle()) 231 System.err.println("FontChooser.setFont" + ((FontStyle)fontStyle.getSelectedItem()).getID() + " <> " + font.getStyle()); 232 m_font = font; 234 this.pack(); 235 m_setting = false; 236 } 238 242 public Font getFont() 243 { 244 return m_retFont; 245 } 247 251 public void actionPerformed(ActionEvent e) 252 { 253 if (m_setting) 254 return; 255 256 if (e.getSource() == bOK) 257 { 258 m_retFont = m_font; 259 dispose(); 260 } 261 262 else if (e.getSource() == bCancel) 263 dispose(); 264 265 else if (e.getSource() == fontName) 266 { 267 String s = fontName.getSelectedItem().toString(); 268 m_font = new Font (s, m_font.getStyle(), m_font.getSize()); 269 } 270 else if (e.getSource() == fontSize) 271 { 272 String s = fontSize.getSelectedItem().toString(); 273 m_font = new Font (m_font.getName(), m_font.getStyle(), Integer.parseInt(s)); 274 } 275 else if (e.getSource() == fontStyle) 276 { 277 FontStyle fs = (FontStyle)fontStyle.getSelectedItem(); 278 m_font = new Font (m_font.getName(), fs.getID(), m_font.getSize()); 279 } 280 setFont(m_font); 282 } } 285 288 class FontStyle 289 { 290 295 public FontStyle(String name, int id) 296 { 297 m_name = name; 298 m_id = id; 299 } 301 private String m_name; 302 private int m_id; 303 304 308 public String toString() 309 { 310 return m_name; 311 } 313 317 public int getID() 318 { 319 return m_id; 320 } } | Popular Tags |