001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui;
034:
035: import java.awt.Dimension;
036: import java.awt.Font;
037: import java.awt.GraphicsEnvironment;
038: import java.awt.GridBagConstraints;
039: import java.awt.Insets;
040: import java.awt.Point;
041: import java.awt.event.KeyEvent;
042: import java.util.StringTokenizer;
043:
044: import javax.swing.DefaultListModel;
045: import javax.swing.JDialog;
046: import javax.swing.JList;
047: import javax.swing.JScrollPane;
048:
049: import com.vividsolutions.jump.I18N;
050:
051: /**
052: * Based on FontChooser by Janos Szatmary. Posted on the Sun Java forums.
053: *
054: * Szatmary, Janos. "A FontChooser Class (source included)." April 2001.
055: * Available from http://forum.java.sun.com/thread.jsp?forum=57&thread=124810.
056: * Internet; accessed 6 November 2002.
057: *
058: */
059: public class FontChooser extends javax.swing.JDialog {
060: private String sampleText = "The quick brown fox jumped over the lazy dog.";
061: String[] styleList = new String[] { "Plain", "Bold", "Italic" };
062: String[] sizeList = new String[] { "2", "4", "6", "8", "10", "12",
063: "14", "16", "18", "20", "22", "24", "30", "36", "48", "72" };
064: String currentFont = null;
065: int currentStyle = -1;
066: int currentSize = -1;
067: public boolean ok = false;
068:
069: // Variables declaration - do not modify
070: private javax.swing.JPanel jPanel3;
071: private javax.swing.JTextField jFont;
072: private javax.swing.JScrollPane jScrollPane1;
073: private javax.swing.JList jFontList;
074: private javax.swing.JPanel jPanel4;
075: private javax.swing.JTextField jStyle;
076: private javax.swing.JScrollPane jScrollPane2;
077: private javax.swing.JList jStyleList;
078: private javax.swing.JPanel jPanel5;
079: private javax.swing.JTextField jSize;
080: private javax.swing.JScrollPane jScrollPane3;
081: private javax.swing.JList jSizeList;
082: private javax.swing.JPanel jPanel1;
083: private javax.swing.JScrollPane jScrollPane4;
084: private javax.swing.JTextArea jSample;
085: private javax.swing.JPanel jButtons;
086: private javax.swing.JButton jOk;
087: private javax.swing.JButton jCancel;
088: private javax.swing.JLabel jLabel6;
089:
090: /* ------------------------------------------------------------- */
091: private FontChooser(JDialog parent, boolean modal) {
092: super (parent, modal);
093: jbInit();
094: setListValues(jFontList, GraphicsEnvironment
095: .getLocalGraphicsEnvironment()
096: .getAvailableFontFamilyNames());
097: setListValues(jStyleList, styleList);
098: setListValues(jSizeList, sizeList);
099: setCurrentFont(jSample.getFont());
100: pack();
101: }
102:
103: private FontChooser(JDialog parent, boolean modal, Font font) {
104: this (parent, modal);
105: setCurrentFont(font);
106: }
107:
108: /* ------------------------------------------------------------- */
109: private void setListValues(JList list, String[] values) {
110: if (list.getModel() instanceof DefaultListModel) {
111: DefaultListModel model = (DefaultListModel) list.getModel();
112: model.removeAllElements();
113:
114: for (int j = 0; j < values.length; j++) {
115: model.addElement(values[j]);
116: }
117: }
118: }
119:
120: /* ------------------------------------------------------------- */
121: private void setSampleFont() {
122: if ((currentFont != null) && (currentStyle >= 0)
123: && (currentSize > 0)) {
124: jSample.setFont(new Font(currentFont, currentStyle,
125: currentSize));
126: }
127: }
128:
129: private String styleToString(int style) {
130: String str = "";
131:
132: if ((style & Font.BOLD) == Font.BOLD) {
133: if (str.length() > 0) {
134: str += ",";
135: }
136:
137: str += "Bold";
138: }
139:
140: if ((style & Font.ITALIC) == Font.ITALIC) {
141: if (str.length() > 0) {
142: str += ",";
143: }
144:
145: str += "Italic";
146: }
147:
148: if ((str.length() <= 0) && ((style & Font.PLAIN) == Font.PLAIN)) {
149: str = "Plain";
150: }
151:
152: return str;
153: }
154:
155: /* ------------------------------------------------------------- */
156: public Font getCurrentFont() {
157: return jSample.getFont();
158: }
159:
160: /* ------------------------------------------------------------- */
161: public void setCurrentFont(Font font) {
162: if (font == null) {
163: font = jSample.getFont();
164: }
165:
166: jFont.setText(font.getName());
167: jFontActionPerformed(null);
168:
169: jStyle.setText(styleToString(font.getStyle()));
170: jStyleActionPerformed(null);
171:
172: jSize.setText(Integer.toString(font.getSize()));
173: jSizeActionPerformed(null);
174: }
175:
176: /* ------------------------------------------------------------- */
177: public static Font showDialog(JDialog parent, String title,
178: Font font) {
179: FontChooser dialog = new FontChooser(parent, true, font);
180:
181: Point p1 = parent.getLocation();
182: Dimension d1 = parent.getSize();
183: Dimension d2 = dialog.getSize();
184:
185: int x = p1.x + ((d1.width - d2.width) / 2);
186: int y = p1.y + ((d1.height - d2.height) / 2);
187:
188: if (x < 0) {
189: x = 0;
190: }
191:
192: if (y < 0) {
193: y = 0;
194: }
195:
196: if (title != null) {
197: dialog.setTitle(title);
198: }
199:
200: dialog.setLocation(x, y);
201: dialog.setVisible(true);
202:
203: Font newfont = null;
204:
205: if (dialog.ok) {
206: newfont = dialog.getCurrentFont();
207: }
208:
209: dialog.dispose();
210:
211: return newfont;
212: }
213:
214: /** This method is called from within the constructor to
215: * initialize the form.
216: * WARNING: Do NOT modify this code. The content of this
217: method is
218: * always regenerated by the FormEditor.
219: */
220: private void jbInit() {
221: jPanel3 = new javax.swing.JPanel();
222: jFont = new javax.swing.JTextField();
223: jScrollPane1 = new javax.swing.JScrollPane();
224: jFontList = new javax.swing.JList();
225: jPanel4 = new javax.swing.JPanel();
226: jStyle = new javax.swing.JTextField();
227: jScrollPane2 = new javax.swing.JScrollPane();
228: jStyleList = new javax.swing.JList();
229: jPanel5 = new javax.swing.JPanel();
230: jSize = new javax.swing.JTextField();
231: jScrollPane3 = new javax.swing.JScrollPane();
232: jSizeList = new javax.swing.JList();
233: jPanel1 = new javax.swing.JPanel();
234: jScrollPane4 = new javax.swing.JScrollPane();
235: jScrollPane4
236: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
237: jScrollPane4
238: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
239: jSample = new javax.swing.JTextArea();
240: jSample.setEditable(false);
241: jButtons = new javax.swing.JPanel();
242: jOk = new javax.swing.JButton();
243: jCancel = new javax.swing.JButton();
244: jLabel6 = new javax.swing.JLabel();
245: getContentPane().setLayout(new java.awt.GridBagLayout());
246:
247: java.awt.GridBagConstraints gridBagConstraints1;
248: setTitle(I18N.get("ui.FontChooser.font-chooser"));
249: addWindowListener(new java.awt.event.WindowAdapter() {
250: public void windowClosing(java.awt.event.WindowEvent evt) {
251: closeDialog(evt);
252: }
253: });
254:
255: jPanel3.setLayout(new java.awt.GridBagLayout());
256:
257: java.awt.GridBagConstraints gridBagConstraints2;
258: jPanel3.setBorder(new javax.swing.border.TitledBorder(
259: new javax.swing.border.EtchedBorder(), " "
260: + I18N.get("ui.FontChooser.font") + " "));
261:
262: jFont.setEditable(false);
263: jFont.setColumns(24);
264: jFont.addActionListener(new java.awt.event.ActionListener() {
265: public void actionPerformed(java.awt.event.ActionEvent evt) {
266: jFontActionPerformed(evt);
267: }
268: });
269: gridBagConstraints2 = new java.awt.GridBagConstraints();
270: gridBagConstraints2.gridwidth = 0;
271: gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
272: gridBagConstraints2.insets = new java.awt.Insets(0, 3, 0, 3);
273: gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST;
274: gridBagConstraints2.weightx = 1.0;
275: jStyle.setEditable(false);
276: jPanel3.add(jFont, gridBagConstraints2);
277:
278: jFontList.setModel(new DefaultListModel());
279: jFontList
280: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
281: jFontList
282: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
283: public void valueChanged(
284: javax.swing.event.ListSelectionEvent evt) {
285: jFontListValueChanged(evt);
286: }
287: });
288: jScrollPane1.setViewportView(jFontList);
289:
290: gridBagConstraints2 = new java.awt.GridBagConstraints();
291: gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
292: gridBagConstraints2.insets = new java.awt.Insets(3, 3, 3, 3);
293: gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST;
294: gridBagConstraints2.weightx = 1.0;
295: gridBagConstraints2.weighty = 1.0;
296: jPanel3.add(jScrollPane1, gridBagConstraints2);
297:
298: gridBagConstraints1 = new java.awt.GridBagConstraints();
299: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
300: gridBagConstraints1.insets = new java.awt.Insets(5, 5, 0, 0);
301: gridBagConstraints1.weightx = 0.5;
302: gridBagConstraints1.weighty = 1.0;
303: getContentPane().add(jPanel3, gridBagConstraints1);
304:
305: jPanel4.setLayout(new java.awt.GridBagLayout());
306:
307: java.awt.GridBagConstraints gridBagConstraints3;
308: jPanel4.setBorder(new javax.swing.border.TitledBorder(
309: new javax.swing.border.EtchedBorder(), " "
310: + I18N.get("ui.FontChooser.style") + " "));
311:
312: jStyle.setColumns(18);
313: jStyle.addActionListener(new java.awt.event.ActionListener() {
314: public void actionPerformed(java.awt.event.ActionEvent evt) {
315: jStyleActionPerformed(evt);
316: }
317: });
318: gridBagConstraints3 = new java.awt.GridBagConstraints();
319: gridBagConstraints3.gridwidth = 0;
320: gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
321: gridBagConstraints3.insets = new java.awt.Insets(0, 3, 0, 3);
322: gridBagConstraints3.anchor = java.awt.GridBagConstraints.NORTHWEST;
323: gridBagConstraints3.weightx = 1.0;
324: jPanel4.add(jStyle, gridBagConstraints3);
325:
326: jStyleList.setModel(new DefaultListModel());
327: jStyleList.setVisibleRowCount(4);
328: jStyleList
329: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
330: public void valueChanged(
331: javax.swing.event.ListSelectionEvent evt) {
332: jStyleListValueChanged(evt);
333: }
334: });
335: jScrollPane2.setViewportView(jStyleList);
336:
337: gridBagConstraints3 = new java.awt.GridBagConstraints();
338: gridBagConstraints3.fill = java.awt.GridBagConstraints.BOTH;
339: gridBagConstraints3.insets = new java.awt.Insets(3, 3, 3, 3);
340: gridBagConstraints3.anchor = java.awt.GridBagConstraints.NORTHWEST;
341: gridBagConstraints3.weightx = 0.5;
342: gridBagConstraints3.weighty = 1.0;
343: jPanel4.add(jScrollPane2, gridBagConstraints3);
344:
345: gridBagConstraints1 = new java.awt.GridBagConstraints();
346: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
347: gridBagConstraints1.insets = new java.awt.Insets(5, 5, 0, 0);
348: gridBagConstraints1.weightx = 0.375;
349: gridBagConstraints1.weighty = 1.0;
350: getContentPane().add(jPanel4, gridBagConstraints1);
351:
352: jPanel5.setLayout(new java.awt.GridBagLayout());
353:
354: java.awt.GridBagConstraints gridBagConstraints4;
355: jPanel5.setBorder(new javax.swing.border.TitledBorder(
356: new javax.swing.border.EtchedBorder(), " "
357: + I18N.get("ui.FontChooser.size") + " "));
358:
359: jSize.setColumns(6);
360: jSize.addActionListener(new java.awt.event.ActionListener() {
361: public void actionPerformed(java.awt.event.ActionEvent evt) {
362: jSizeActionPerformed(evt);
363: }
364: });
365: gridBagConstraints4 = new java.awt.GridBagConstraints();
366: gridBagConstraints4.gridwidth = 0;
367: gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
368: gridBagConstraints4.insets = new java.awt.Insets(0, 3, 0, 3);
369: gridBagConstraints4.anchor = java.awt.GridBagConstraints.NORTHWEST;
370: gridBagConstraints4.weightx = 1.0;
371: jPanel5.add(jSize, gridBagConstraints4);
372:
373: jSizeList.setModel(new DefaultListModel());
374: jSizeList.setVisibleRowCount(4);
375: jSizeList
376: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
377: jSizeList
378: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
379: public void valueChanged(
380: javax.swing.event.ListSelectionEvent evt) {
381: jSizeListValueChanged(evt);
382: }
383: });
384: jScrollPane3.setViewportView(jSizeList);
385:
386: gridBagConstraints4 = new java.awt.GridBagConstraints();
387: gridBagConstraints4.fill = java.awt.GridBagConstraints.BOTH;
388: gridBagConstraints4.insets = new java.awt.Insets(3, 3, 3, 3);
389: gridBagConstraints4.anchor = java.awt.GridBagConstraints.NORTHWEST;
390: gridBagConstraints4.weightx = 0.25;
391: gridBagConstraints4.weighty = 1.0;
392: jPanel5.add(jScrollPane3, gridBagConstraints4);
393:
394: gridBagConstraints1 = new java.awt.GridBagConstraints();
395: gridBagConstraints1.gridwidth = 0;
396: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
397: gridBagConstraints1.insets = new java.awt.Insets(5, 5, 0, 5);
398: gridBagConstraints1.weightx = 0.125;
399: gridBagConstraints1.weighty = 1.0;
400:
401: // getContentPane().add(jPanel5, gridBagConstraints1);
402: jPanel1.setLayout(new java.awt.GridBagLayout());
403:
404: java.awt.GridBagConstraints gridBagConstraints5;
405: jPanel1.setBorder(new javax.swing.border.TitledBorder(
406: new javax.swing.border.EtchedBorder(), " "
407: + I18N.get("ui.FontChooser.sample") + " "));
408:
409: jSample.setWrapStyleWord(true);
410: jSample.setLineWrap(true);
411: jSample.setColumns(20);
412: jSample.setRows(3);
413: jSample.setText(sampleText);
414: jScrollPane4.setViewportView(jSample);
415:
416: gridBagConstraints5 = new java.awt.GridBagConstraints();
417: gridBagConstraints5.fill = java.awt.GridBagConstraints.BOTH;
418: gridBagConstraints5.insets = new java.awt.Insets(0, 3, 3, 3);
419: gridBagConstraints5.weightx = 1.0;
420: gridBagConstraints5.weighty = 1.0;
421: jPanel1.add(jScrollPane4, gridBagConstraints5);
422:
423: gridBagConstraints1 = new java.awt.GridBagConstraints();
424: gridBagConstraints1.gridwidth = 0;
425: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
426: gridBagConstraints1.insets = new java.awt.Insets(0, 5, 0, 5);
427: gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTHWEST;
428: gridBagConstraints1.weightx = 1.0;
429: getContentPane().add(
430: jPanel1,
431: new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0,
432: GridBagConstraints.CENTER,
433: GridBagConstraints.BOTH,
434: new Insets(0, 3, 3, 3), 0, 0));
435:
436: jButtons.setLayout(new java.awt.GridBagLayout());
437:
438: jOk.setMnemonic(KeyEvent.VK_O);
439: jOk.setText(I18N.get("ui.FontChooser.ok"));
440: jOk.setRequestFocusEnabled(false);
441: jOk.addActionListener(new java.awt.event.ActionListener() {
442: public void actionPerformed(java.awt.event.ActionEvent evt) {
443: jOkActionPerformed(evt);
444: }
445: });
446: jButtons.add(jOk, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
447: GridBagConstraints.WEST, GridBagConstraints.NONE,
448: new Insets(5, 5, 5, 0), 0, 0));
449:
450: jCancel.setMnemonic(KeyEvent.VK_C);
451: jCancel.setText(I18N.get("ui.FontChooser.cancel"));
452: jCancel.setRequestFocusEnabled(false);
453: jCancel.addActionListener(new java.awt.event.ActionListener() {
454: public void actionPerformed(java.awt.event.ActionEvent evt) {
455: jCancelActionPerformed(evt);
456: }
457: });
458:
459: jButtons.add(jCancel, new GridBagConstraints(1, 0, 1, 1, 0.0,
460: 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
461: new Insets(5, 5, 5, 5), 0, 0));
462:
463: gridBagConstraints1 = new java.awt.GridBagConstraints();
464: gridBagConstraints1.gridwidth = 0;
465: gridBagConstraints1.anchor = java.awt.GridBagConstraints.SOUTHWEST;
466: gridBagConstraints1.weightx = 1.0;
467: getContentPane().add(
468: jButtons,
469: new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0,
470: GridBagConstraints.SOUTHWEST,
471: GridBagConstraints.NONE,
472: new Insets(0, 0, 0, 0), 0, 0));
473: }
474:
475: private void jCancelActionPerformed(java.awt.event.ActionEvent evt) {
476: // Add your handling code here:
477: setVisible(false);
478: }
479:
480: private void jOkActionPerformed(java.awt.event.ActionEvent evt) {
481: // Add your handling code here:
482: ok = true;
483: setVisible(false);
484: }
485:
486: private void jSizeActionPerformed(java.awt.event.ActionEvent evt) {
487: // Add your handling code here:
488: int size = 0;
489:
490: try {
491: size = Integer.parseInt(jSize.getText());
492: } catch (Exception e) {
493: }
494:
495: if (size > 0) {
496: currentSize = size;
497: setSampleFont();
498: }
499: }
500:
501: private void jStyleActionPerformed(java.awt.event.ActionEvent evt) {
502: // Add your handling code here:
503: StringTokenizer st = new StringTokenizer(jStyle.getText(), ",");
504: int style = 0;
505:
506: while (st.hasMoreTokens()) {
507: String str = st.nextToken().trim();
508:
509: if (str.equalsIgnoreCase("Plain")) {
510: style |= Font.PLAIN;
511: } else if (str.equalsIgnoreCase("Bold")) {
512: style |= Font.BOLD;
513: } else if (str.equalsIgnoreCase("Italic")) {
514: style |= Font.ITALIC;
515: }
516: }
517:
518: if (style >= 0) {
519: currentStyle = style;
520: setSampleFont();
521: }
522: }
523:
524: private void jFontActionPerformed(java.awt.event.ActionEvent evt) {
525: // Add your handling code here:
526: DefaultListModel model = (DefaultListModel) jFontList
527: .getModel();
528:
529: if (model.indexOf(jFont.getText()) >= 0) {
530: currentFont = jFont.getText();
531: setSampleFont();
532: }
533: }
534:
535: private void jStyleListValueChanged(
536: javax.swing.event.ListSelectionEvent evt) {
537: // Add your handling code here:
538: String str = new String();
539: Object[] values = jStyleList.getSelectedValues();
540:
541: if (values.length > 0) {
542: int j;
543:
544: for (j = 0; j < values.length; j++) {
545: String s = (String) values[j];
546:
547: if (s.equalsIgnoreCase("Plain")) {
548: str = "Plain";
549:
550: break;
551: }
552:
553: if (str.length() > 0) {
554: str += ",";
555: }
556:
557: str += (String) values[j];
558: }
559: } else {
560: str = styleToString(currentStyle);
561: }
562:
563: jStyle.setText(str);
564: jStyleActionPerformed(null);
565: }
566:
567: private void jSizeListValueChanged(
568: javax.swing.event.ListSelectionEvent evt) {
569: // Add your handling code here:
570: String str = (String) jSizeList.getSelectedValue();
571:
572: if ((str == null) || (str.length() <= 0)) {
573: str = Integer.toString(currentSize);
574: }
575:
576: jSize.setText(str);
577: jSizeActionPerformed(null);
578: }
579:
580: private void jFontListValueChanged(
581: javax.swing.event.ListSelectionEvent evt) {
582: // Add your handling code here:
583: String str = (String) jFontList.getSelectedValue();
584:
585: if ((str == null) || (str.length() <= 0)) {
586: str = currentFont;
587: }
588:
589: jFont.setText(str);
590: jFontActionPerformed(null);
591: }
592:
593: /** Closes the dialog */
594: private void closeDialog(java.awt.event.WindowEvent evt) {
595: setVisible(false);
596: }
597:
598: // End of variables declaration
599: }
|