001: /*
002: * FontChooser.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019:
020: package net.sf.jdec.ui.util.highlight;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Color;
024: import java.awt.GraphicsEnvironment;
025: import java.awt.GridLayout;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028:
029: import javax.swing.BoxLayout;
030: import javax.swing.JButton;
031: import javax.swing.JComboBox;
032: import javax.swing.JDialog;
033: import javax.swing.JFrame;
034: import javax.swing.JLabel;
035: import javax.swing.JPanel;
036: import javax.swing.JSeparator;
037: import javax.swing.border.EtchedBorder;
038: import javax.swing.border.TitledBorder;
039:
040: import net.sf.jdec.ui.main.UILauncher;
041:
042: public class FontChooser extends JDialog implements ActionListener {
043:
044: String fonts[] = null;
045: String sizes[] = { "8", "10", "12", "14", "16", "18", "20" };
046: JLabel curfmly = new JLabel("");
047: JLabel cursz = new JLabel("");
048: JButton apply = new JButton("Apply");
049: JButton prev = new JButton("Preview");
050: JComboBox fmly = null;
051: JComboBox fsizes = null;
052: JPanel prevP = null;
053: JLabel sample = null;
054:
055: public FontChooser(JFrame frame) {
056: super (frame);
057: setTitle("Jdec Font Chooser");
058:
059: }
060:
061: private JLabel name = null;
062: private JLabel sz = null;
063:
064: public void createAllComponents() {
065:
066: GraphicsEnvironment ge = GraphicsEnvironment
067: .getLocalGraphicsEnvironment();
068: fonts = ge.getAvailableFontFamilyNames();
069: JPanel panel = new JPanel();//new GridLayout(3,1,1,1));
070: name = new JLabel("Current User Selected Font Family:");
071: name.setText("Current User Selected Font Family:");
072: name.setForeground(Color.RED);
073: sz = new JLabel("Current User Selected Font Size:");
074: sz.setText("Current User Selected Font Size:");
075: sz.setForeground(Color.RED);
076: JLabel sel1 = new JLabel("Select Font Family:");
077: JLabel sel2 = new JLabel(
078: "Select Font Size: ");
079: sel1.setForeground(Color.blue);
080: sel2.setForeground(Color.blue);
081: fmly = new JComboBox(fonts);
082:
083: fsizes = new JComboBox(sizes);
084: //panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
085: JPanel nameP = new JPanel();
086: //nameP.setLayout(new BoxLayout(nameP,BoxLayout.X_AXIS));
087: nameP.add(name, BorderLayout.CENTER);
088: nameP.add(curfmly, BorderLayout.SOUTH);
089:
090: JPanel szP = new JPanel();
091: //szP.setLayout(new BoxLayout(szP,BoxLayout.X_AXIS));
092: szP.add(sz);
093: szP.add(cursz);
094:
095: JPanel curSel = new JPanel();
096: curSel.setLayout(new BoxLayout(curSel, BoxLayout.Y_AXIS));
097: curSel.add(nameP);
098: curSel.add(szP);
099: curSel.setBorder(new EtchedBorder());
100:
101: panel.add(curSel);//,BorderLayout.NORTH);
102:
103: JPanel selFml = new JPanel(new GridLayout(1, 2, 10, 2));
104: //selFml.setLayout(new BoxLayout(selFml,BoxLayout.X_AXIS));
105: selFml.add(sel1);
106: selFml.add(fmly);
107:
108: JPanel selSize = new JPanel(new GridLayout(1, 2, 10, 2));
109:
110: //selSize.setLayout(new BoxLayout(selSize,BoxLayout.X_AXIS));
111: selSize.add(sel2);
112: selSize.add(fsizes);
113: panel.add(new JSeparator());
114: panel.add(selFml);//,BorderLayout.CENTER);
115: panel.add(selSize);//,BorderLayout.SOUTH);
116: panel.setBorder(new TitledBorder("Select Font"));
117:
118: prevP = new JPanel();
119: sample = new JLabel("Text To Be Previewed");
120: //sample.setForeground(Color.blue);
121: //prevP.add(sample);
122: prevP.add(apply);
123: JButton cl = new JButton("Close");
124: cl.addActionListener(new ActionListener() {
125: public void actionPerformed(ActionEvent ae) {
126: FontChooser.this .setVisible(false);
127: FontChooser.this .dispose();
128: }
129: });
130: prevP.add(cl);
131: //prevP.add(prev);
132: getContentPane().add(panel, BorderLayout.CENTER);
133: getContentPane().add(prevP, BorderLayout.SOUTH);
134: apply.addActionListener(this );
135: prev.addActionListener(this );
136: showAnyUserFonts();
137: }
138:
139: private void showAnyUserFonts() {
140: name.setText("Current User Selected Font Family:");
141: sz.setText("Current User Selected Font Size:");
142: if (cat != null && cat.indexOf("Keyword") != -1) {
143: String s1 = UILauncher.getUIConfigRef().getFontFamilykwd();
144: String s2 = UILauncher.getUIConfigRef().getFontSizekwd();
145: if (name.getText() == null)
146: name.setText("");
147: if (sz.getText() == null)
148: sz.setText("");
149: if (s1 == null)
150: s1 = "";
151: if (s2 == null)
152: s2 = "";
153: name.setText(name.getText() + " (keyword) " + s1);
154: sz.setText(sz.getText() + " " + s2);
155: }
156: if (cat != null && cat.indexOf("Number") != -1) {
157: String s1 = UILauncher.getUIConfigRef().getFontFamilynum();
158: String s2 = UILauncher.getUIConfigRef().getFontSizenum();
159: if (name.getText() == null)
160: name.setText("");
161: if (sz.getText() == null)
162: sz.setText("");
163: if (s1 == null)
164: s1 = "";
165: if (s2 == null)
166: s2 = "";
167: name.setText(name.getText() + " (Number) " + s1);
168: sz.setText(sz.getText() + " " + s2);
169: }
170: if (cat != null && cat.indexOf("Operator") != -1) {
171: String s1 = UILauncher.getUIConfigRef().getFontFamilyop();
172: String s2 = UILauncher.getUIConfigRef().getFontSizeop();
173: if (name.getText() == null)
174: name.setText("");
175: if (sz.getText() == null)
176: sz.setText("");
177: if (s1 == null)
178: s1 = "";
179: if (s2 == null)
180: s2 = "";
181: name.setText(name.getText() + " (Operator) " + s1);
182: sz.setText(sz.getText() + " " + s2);
183: }
184: if (cat != null && cat.indexOf("String") != -1) {
185: String s1 = UILauncher.getUIConfigRef().getFontFamilystr();
186: String s2 = UILauncher.getUIConfigRef().getFontSizestr();
187: if (name.getText() == null)
188: name.setText("");
189: if (sz.getText() == null)
190: sz.setText("");
191: if (s1 == null)
192: s1 = "";
193: if (s2 == null)
194: s2 = "";
195: name.setText(name.getText() + " (String) " + s1);
196: sz.setText(sz.getText() + " " + s2);
197: }
198: }
199:
200: public void showFontWindow() {
201: setSize(500, 400);
202: setVisible(true);
203: //pack();
204: }
205:
206: public static void main(String[] args) {
207: FontChooser fc = new FontChooser(new JFrame());
208: fc.createAllComponents();
209: fc.showFontWindow();
210:
211: }
212:
213: public void actionPerformed(ActionEvent ae) {
214: if (ae.getSource() == apply) {
215: Object o = fmly.getSelectedItem();
216: name.setText("Current User Selected Font Family:");
217: sz.setText("Current User Selected Font Size:");
218: if (o != null) {
219: String fmName = o.toString();
220: if (cat.indexOf("Keyword") != -1) {
221: UILauncher.getUIConfigRef()
222: .setFontFamilykwd(fmName);
223: UILauncher.getUIConfigRef().addPref(
224: "font_family_kwd", fmName);
225: name.setText(name.getText() + " (Keyword) "
226: + fmName);
227:
228: } else if (cat.indexOf("Number") != -1) {
229: UILauncher.getUIConfigRef()
230: .setFontFamilynum(fmName);
231: UILauncher.getUIConfigRef().addPref(
232: "font_family_num", fmName);
233: name.setText(name.getText() + " (Number) "
234: + fmName);
235: } else if (cat.indexOf("Operator") != -1) {
236: UILauncher.getUIConfigRef().setFontFamilyop(fmName);
237: UILauncher.getUIConfigRef().addPref(
238: "font_family_op", fmName);
239: name.setText(name.getText() + " (Operator) "
240: + fmName);
241: } else if (cat.indexOf("String") != -1) {
242: UILauncher.getUIConfigRef()
243: .setFontFamilystr(fmName);
244: UILauncher.getUIConfigRef().addPref(
245: "font_family_str", fmName);
246: name.setText(name.getText() + " (String) "
247: + fmName);
248: }
249:
250: }
251: Object o2 = fsizes.getSelectedItem();
252: if (o2 != null) {
253: String size = o2.toString();
254: if (cat.indexOf("Keyword") != -1) {
255: UILauncher.getUIConfigRef().setFontSizekwd(size);
256: UILauncher.getUIConfigRef().addPref(
257: "font_size_kwd", size);
258: sz.setText(sz.getText() + " " + size);
259:
260: } else if (cat.indexOf("Number") != -1) {
261: UILauncher.getUIConfigRef().setFontSizenum(size);
262: UILauncher.getUIConfigRef().addPref(
263: "font_size_num", size);
264: sz.setText(sz.getText() + " " + size);
265: } else if (cat.indexOf("Operator") != -1) {
266: UILauncher.getUIConfigRef().setFontSizeop(size);
267: UILauncher.getUIConfigRef().addPref("font_size_op",
268: size);
269: sz.setText(sz.getText() + " " + size);
270: } else if (cat.indexOf("String") != -1) {
271: UILauncher.getUIConfigRef().setFontSizestr(size);
272: UILauncher.getUIConfigRef().addPref(
273: "font_size_str", size);
274: sz.setText(sz.getText() + " " + size);
275: }
276: }
277: } else if (ae.getSource() == prev) {
278:
279: }
280: }
281:
282: private String cat = "";
283:
284: public void setCategory(String cat) {
285: this.cat = cat;
286: }
287: }
|