View Javadoc

1   /*
2    * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: FontSelectionDialog.java,v 503f7e4f22db 2008/04/28 12:43:00 maxberger $ */
18  
19  package net.sourceforge.jeuclid.app.mathviewer;
20  
21  import java.awt.BorderLayout;
22  import java.awt.Dialog;
23  import java.awt.FlowLayout;
24  import java.awt.Font;
25  import java.awt.Frame;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.Vector;
32  
33  import javax.swing.JButton;
34  import javax.swing.JDialog;
35  import javax.swing.JLabel;
36  import javax.swing.JList;
37  import javax.swing.JPanel;
38  import javax.swing.JScrollPane;
39  import javax.swing.JTextArea;
40  import javax.swing.ScrollPaneConstants;
41  import javax.swing.WindowConstants;
42  import javax.swing.event.ListSelectionEvent;
43  import javax.swing.event.ListSelectionListener;
44  
45  import net.sourceforge.jeuclid.font.FontFactory;
46  
47  /**
48   * Dialog that lets user select one or more fonts available in the system.
49   * 
50   * @version $Revision: 503f7e4f22db $
51   */
52  // CHECKSTYLE:OFF
53  public class FontSelectionDialog extends JDialog {
54      // CHECKSTYLE:ON
55  
56      private static final int SAMPLE_FONTSIZE = 14;
57  
58      /**
59       * 
60       */
61      private static final long serialVersionUID = 1L;
62  
63      private static final String PREVIEW_TEXT = "The quick brown fox jumps over the lazy dog. 123456790";
64  
65      private final List<String> fontNames = new ArrayList<String>();
66  
67      private JList list;
68  
69      /**
70       * Default Constructor.
71       * 
72       * @param parent
73       *            parent dialog
74       * @param currentFontNames
75       *            font names to select initially
76       */
77      public FontSelectionDialog(final Dialog parent,
78              final List<String> currentFontNames) {
79          super(parent);
80          this.init(currentFontNames);
81      }
82  
83      /**
84       * Default Constructor.
85       * 
86       * @param parent
87       *            parent frame
88       * @param currentFontNames
89       *            font names to select initially
90       */
91      public FontSelectionDialog(final Frame parent,
92              final List<String> currentFontNames) {
93          super(parent);
94          this.init(currentFontNames);
95      }
96  
97      private void init(final List<String> currentFontNames) {
98          this.setTitle(Messages
99                  .getString("MathViewer.FontSelectionDialog.title"));
100         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
101         this
102                 .getContentPane()
103                 .add(
104                         new JLabel(
105                                 Messages
106                                         .getString("MathViewer.FontSelectionDialog.topLabel")),
107                         BorderLayout.NORTH);
108         this.setupFontsList(currentFontNames);
109         this.setupButtons();
110         this.pack();
111         this.setLocationByPlatform(true);
112         this.setModal(true);
113     }
114 
115     /**
116      * @return selected font names if OK was pressed to close the dialog
117      */
118     public List<String> getFontNames() {
119         return this.fontNames;
120     }
121 
122     private void setupFontsList(final List<String> currentFontNames) {
123         // CHECKSTYLE:OFF
124         // Vector is a must because JList requires it.
125         final Vector<String> allFonts = new Vector<String>(FontFactory
126                 .getInstance().listFontNames());
127         // CHECKSTYLE:ON
128         Collections.sort(allFonts);
129         this.list = new JList(allFonts);
130         final List<Integer> selectedIndicies = new ArrayList<Integer>();
131         if (currentFontNames != null && !currentFontNames.isEmpty()) {
132             for (final String value : currentFontNames) {
133                 final int i = Collections.binarySearch(allFonts, value);
134                 if (i > -1) {
135                     selectedIndicies.add(i);
136                 }
137             }
138         }
139         final int[] selectedIndiciesArr = new int[selectedIndicies.size()];
140         for (int i = 0; i < selectedIndiciesArr.length; i++) {
141             selectedIndiciesArr[i] = selectedIndicies.get(i);
142         }
143         this.list.setSelectedIndices(selectedIndiciesArr);
144         final JScrollPane scrollPane = new JScrollPane(this.list,
145                 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
146                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
147         final JTextArea preview = new JTextArea(
148                 FontSelectionDialog.PREVIEW_TEXT);
149         this.list.addListSelectionListener(new ListSelectionListener() {
150             public void valueChanged(final ListSelectionEvent e) {
151                 preview.setFont(FontFactory.getInstance().getFont(
152                         (String) FontSelectionDialog.this.list
153                                 .getSelectedValue(), Font.PLAIN,
154                         FontSelectionDialog.SAMPLE_FONTSIZE));
155                 preview.revalidate();
156             }
157         });
158         this.getContentPane().add(scrollPane, BorderLayout.WEST);
159         this.getContentPane().add(preview, BorderLayout.CENTER);
160     }
161 
162     private void setupButtons() {
163         final JPanel buttonsPanel = new JPanel(new FlowLayout(
164                 FlowLayout.CENTER));
165         final JButton btnOK = new JButton(Messages.getString("MathViewer.ok"));
166         btnOK.setMnemonic('O');
167         final ActionListener actionListener = new ActionListener() {
168             public void actionPerformed(final ActionEvent e) {
169                 if (e.getSource() == btnOK) {
170                     for (Object val : FontSelectionDialog.this.list
171                             .getSelectedValues()) {
172                         FontSelectionDialog.this.fontNames.add((String) val);
173                     }
174                 }
175                 FontSelectionDialog.this.dispose();
176             }
177         };
178         btnOK.addActionListener(actionListener);
179         buttonsPanel.add(btnOK);
180         final JButton btnCancel = new JButton(Messages
181                 .getString("MathViewer.cancel"));
182         btnCancel.setMnemonic('C');
183         btnCancel.addActionListener(actionListener);
184         buttonsPanel.add(btnCancel);
185         this.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
186     }
187 
188 }