001    /*
002     * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: FontSelectionDialog.java,v 503f7e4f22db 2008/04/28 12:43:00 maxberger $ */
018    
019    package net.sourceforge.jeuclid.app.mathviewer;
020    
021    import java.awt.BorderLayout;
022    import java.awt.Dialog;
023    import java.awt.FlowLayout;
024    import java.awt.Font;
025    import java.awt.Frame;
026    import java.awt.event.ActionEvent;
027    import java.awt.event.ActionListener;
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.List;
031    import java.util.Vector;
032    
033    import javax.swing.JButton;
034    import javax.swing.JDialog;
035    import javax.swing.JLabel;
036    import javax.swing.JList;
037    import javax.swing.JPanel;
038    import javax.swing.JScrollPane;
039    import javax.swing.JTextArea;
040    import javax.swing.ScrollPaneConstants;
041    import javax.swing.WindowConstants;
042    import javax.swing.event.ListSelectionEvent;
043    import javax.swing.event.ListSelectionListener;
044    
045    import net.sourceforge.jeuclid.font.FontFactory;
046    
047    /**
048     * Dialog that lets user select one or more fonts available in the system.
049     * 
050     * @version $Revision: 503f7e4f22db $
051     */
052    // CHECKSTYLE:OFF
053    public class FontSelectionDialog extends JDialog {
054        // CHECKSTYLE:ON
055    
056        private static final int SAMPLE_FONTSIZE = 14;
057    
058        /**
059         * 
060         */
061        private static final long serialVersionUID = 1L;
062    
063        private static final String PREVIEW_TEXT = "The quick brown fox jumps over the lazy dog. 123456790";
064    
065        private final List<String> fontNames = new ArrayList<String>();
066    
067        private JList list;
068    
069        /**
070         * Default Constructor.
071         * 
072         * @param parent
073         *            parent dialog
074         * @param currentFontNames
075         *            font names to select initially
076         */
077        public FontSelectionDialog(final Dialog parent,
078                final List<String> currentFontNames) {
079            super(parent);
080            this.init(currentFontNames);
081        }
082    
083        /**
084         * Default Constructor.
085         * 
086         * @param parent
087         *            parent frame
088         * @param currentFontNames
089         *            font names to select initially
090         */
091        public FontSelectionDialog(final Frame parent,
092                final List<String> currentFontNames) {
093            super(parent);
094            this.init(currentFontNames);
095        }
096    
097        private void init(final List<String> currentFontNames) {
098            this.setTitle(Messages
099                    .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    }