Coverage Report - org.argouml.ui.ArgoJFontChooser
 
Classes in this File Line Coverage Branch Coverage Complexity
ArgoJFontChooser
100%
128/128
58%
7/12
1.533
ArgoJFontChooser$1
20%
1/5
0%
0/2
1.533
ArgoJFontChooser$2
20%
1/5
0%
0/2
1.533
ArgoJFontChooser$3
100%
5/5
N/A
1.533
ArgoJFontChooser$4
100%
5/5
N/A
1.533
 
 1  
 /* $Id: ArgoJFontChooser.java 17841 2010-01-12 19:17:52Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2007 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.ui;
 40  
 
 41  
 import java.awt.Dimension;
 42  
 import java.awt.Font;
 43  
 import java.awt.Frame;
 44  
 import java.awt.GraphicsEnvironment;
 45  
 import java.awt.GridBagConstraints;
 46  
 import java.awt.GridBagLayout;
 47  
 import java.awt.Insets;
 48  
 import java.awt.event.ActionEvent;
 49  
 import java.awt.event.ActionListener;
 50  
 
 51  
 import javax.swing.DefaultListModel;
 52  
 import javax.swing.JButton;
 53  
 import javax.swing.JComponent;
 54  
 import javax.swing.JDialog;
 55  
 import javax.swing.JLabel;
 56  
 import javax.swing.JList;
 57  
 import javax.swing.JPanel;
 58  
 import javax.swing.JScrollPane;
 59  
 import javax.swing.event.ListSelectionEvent;
 60  
 import javax.swing.event.ListSelectionListener;
 61  
 
 62  
 import org.argouml.i18n.Translator;
 63  
 
 64  
 /**
 65  
  * A font chooser dialog for selecting font family, size and style.
 66  
  *
 67  
  * @author Aleksandar
 68  
  */
 69  10
 public class ArgoJFontChooser extends JDialog {
 70  
 
 71  11
     private JPanel jContentPane = null;
 72  
 
 73  11
     private JList jlstFamilies = null;
 74  
 
 75  11
     private JList jlstSizes = null;
 76  
 
 77  
 //    private JCheckBox jchbBold = null;
 78  
 
 79  
 //    private JCheckBox jchbItalic = null;
 80  
 
 81  11
     private JLabel jlblFamilies = null;
 82  
 
 83  11
     private JLabel jlblSize = null;
 84  
 
 85  11
     private JLabel jlblPreview = null;
 86  
 
 87  11
     private JButton jbtnOk = null;
 88  
 
 89  11
     private JButton jbtnCancel = null;
 90  
 
 91  
     private int resultSize;
 92  
 
 93  
     private String resultName;
 94  
 
 95  11
     private boolean isOk = false;
 96  
 
 97  
     /**
 98  
      * @param owner the <code>Frame</code> from which the dialog is displayed
 99  
      * @param parent determines the position of the chooser
 100  
      * @param name the initial font name value
 101  
      * @param size the initial font size value
 102  
      */
 103  
     public ArgoJFontChooser(Frame owner, JComponent parent, String name,
 104  
             int size) {
 105  11
         super(owner, true);
 106  11
         setLocationRelativeTo(parent);
 107  
 
 108  11
         this.resultName = name;
 109  11
         this.resultSize = size;
 110  
 
 111  11
         initialize();
 112  11
     }
 113  
 
 114  
     /**
 115  
      * This method initializes this
 116  
      */
 117  
     private void initialize() {
 118  11
         this.setSize(299, 400);
 119  11
         this.setTitle(Translator.localize("dialog.fontchooser"));
 120  11
         this.setContentPane(getJContentPane());
 121  
 
 122  11
         updatePreview();
 123  11
     }
 124  
 
 125  
     /**
 126  
      * This method initializes jContentPane
 127  
      *
 128  
      * @return javax.swing.JPanel
 129  
      */
 130  
     private JPanel getJContentPane() {
 131  11
         if (jContentPane == null) {
 132  11
             GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
 133  11
             gridBagConstraints8.gridx = 4;
 134  11
             gridBagConstraints8.anchor = GridBagConstraints.NORTHEAST;
 135  11
             gridBagConstraints8.insets = new Insets(0, 0, 5, 5);
 136  11
             gridBagConstraints8.weightx = 0.0;
 137  11
             gridBagConstraints8.ipadx = 0;
 138  11
             gridBagConstraints8.gridy = 5;
 139  11
             GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
 140  11
             gridBagConstraints7.gridx = 3;
 141  11
             gridBagConstraints7.fill = GridBagConstraints.NONE;
 142  11
             gridBagConstraints7.weightx = 1.0;
 143  11
             gridBagConstraints7.anchor = GridBagConstraints.NORTHEAST;
 144  11
             gridBagConstraints7.insets = new Insets(0, 0, 5, 5);
 145  11
             gridBagConstraints7.weighty = 0.0;
 146  11
             gridBagConstraints7.gridwidth = 1;
 147  11
             gridBagConstraints7.ipadx = 0;
 148  11
             gridBagConstraints7.gridy = 5;
 149  11
             GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
 150  11
             gridBagConstraints6.gridx = 0;
 151  11
             gridBagConstraints6.gridwidth = 5;
 152  11
             gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
 153  11
             gridBagConstraints6.weightx = 1.0;
 154  11
             gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
 155  11
             gridBagConstraints6.anchor = GridBagConstraints.NORTHWEST;
 156  11
             gridBagConstraints6.gridy = 4;
 157  11
             jlblPreview = new JLabel();
 158  11
             jlblPreview.setText(Translator
 159  
                     .localize("label.diagramappearance.preview"));
 160  11
             jlblPreview.setPreferredSize(new Dimension(52, 50));
 161  11
             GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
 162  11
             gridBagConstraints5.gridx = 4;
 163  11
             gridBagConstraints5.anchor = GridBagConstraints.NORTHWEST;
 164  11
             gridBagConstraints5.insets = new Insets(5, 5, 0, 0);
 165  11
             gridBagConstraints5.gridy = 0;
 166  11
             jlblSize = new JLabel();
 167  11
             jlblSize.setText(Translator
 168  
                     .localize("label.diagramappearance.fontsize"));
 169  11
             GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
 170  11
             gridBagConstraints4.gridx = 0;
 171  11
             gridBagConstraints4.anchor = GridBagConstraints.NORTHWEST;
 172  11
             gridBagConstraints4.insets = new Insets(5, 5, 0, 0);
 173  11
             gridBagConstraints4.gridy = 0;
 174  11
             jlblFamilies = new JLabel();
 175  11
             jlblFamilies.setText(Translator
 176  
                     .localize("label.diagramappearance.fontlist"));
 177  
 //            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
 178  
 //            gridBagConstraints3.gridx = 2;
 179  
 //            gridBagConstraints3.anchor = GridBagConstraints.NORTHWEST;
 180  
 //            gridBagConstraints3.insets = new Insets(5, 5, 0, 0);
 181  
 //            gridBagConstraints3.gridy = 3;
 182  
 //            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
 183  
 //            gridBagConstraints2.gridx = 0;
 184  
 //            gridBagConstraints2.anchor = GridBagConstraints.NORTHWEST;
 185  
 //            gridBagConstraints2.insets = new Insets(5, 5, 0, 0);
 186  
 //            gridBagConstraints2.gridy = 3;
 187  11
             GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
 188  11
             gridBagConstraints1.fill = GridBagConstraints.BOTH;
 189  11
             gridBagConstraints1.gridy = 2;
 190  11
             gridBagConstraints1.weightx = 0.0;
 191  11
             gridBagConstraints1.weighty = 1.0;
 192  11
             gridBagConstraints1.insets = new Insets(5, 0, 0, 5);
 193  11
             gridBagConstraints1.anchor = GridBagConstraints.NORTHWEST;
 194  11
             gridBagConstraints1.gridwidth = 2;
 195  11
             gridBagConstraints1.gridx = 4;
 196  11
             GridBagConstraints gridBagConstraints = new GridBagConstraints();
 197  11
             gridBagConstraints.fill = GridBagConstraints.BOTH;
 198  11
             gridBagConstraints.gridy = 2;
 199  11
             gridBagConstraints.weightx = 1.0;
 200  11
             gridBagConstraints.weighty = 1.0;
 201  11
             gridBagConstraints.insets = new Insets(5, 5, 0, 5);
 202  11
             gridBagConstraints.gridwidth = 4;
 203  11
             gridBagConstraints.gridheight = 1;
 204  11
             gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
 205  11
             gridBagConstraints.gridx = 0;
 206  11
             jContentPane = new JPanel();
 207  11
             jContentPane.setLayout(new GridBagLayout());
 208  
 
 209  11
             JScrollPane jscpFamilies = new JScrollPane();
 210  11
             jscpFamilies.setViewportView(getJlstFamilies());
 211  11
             JScrollPane jscpSizes = new JScrollPane();
 212  11
             jscpSizes.setViewportView(getJlstSizes());
 213  11
             jContentPane.add(jscpFamilies, gridBagConstraints);
 214  11
             jContentPane.add(jscpSizes, gridBagConstraints1);
 215  
 //            jContentPane.add(getJchbBold(), gridBagConstraints2);
 216  
 //            jContentPane.add(getJchbItalic(), gridBagConstraints3);
 217  11
             jContentPane.add(jlblFamilies, gridBagConstraints4);
 218  11
             jContentPane.add(jlblSize, gridBagConstraints5);
 219  11
             jContentPane.add(jlblPreview, gridBagConstraints6);
 220  11
             jContentPane.add(getJbtnOk(), gridBagConstraints7);
 221  11
             jContentPane.add(getJbtnCancel(), gridBagConstraints8);
 222  
         }
 223  11
         return jContentPane;
 224  
     }
 225  
 
 226  
     /**
 227  
      * This method initializes jlstFamilies
 228  
      *
 229  
      * @return javax.swing.JList
 230  
      */
 231  
     private JList getJlstFamilies() {
 232  11
         if (jlstFamilies == null) {
 233  11
             jlstFamilies = new JList();
 234  11
             jlstFamilies.setModel(new DefaultListModel());
 235  
 
 236  11
             String[] fontNames = GraphicsEnvironment
 237  
                     .getLocalGraphicsEnvironment()
 238  
                     .getAvailableFontFamilyNames();
 239  792
             for (String fontName : fontNames) {
 240  781
                 ((DefaultListModel) jlstFamilies.getModel())
 241  
                         .addElement(fontName);
 242  
             }
 243  11
             jlstFamilies.setSelectedValue(resultName, true);
 244  
 
 245  11
             jlstFamilies.getSelectionModel().addListSelectionListener(
 246  11
                     new ListSelectionListener() {
 247  
                         public void valueChanged(ListSelectionEvent e) {
 248  0
                             if (jlstFamilies.getSelectedValue() != null) {
 249  0
                                 resultName = (String) jlstFamilies
 250  
                                         .getSelectedValue();
 251  0
                                 updatePreview();
 252  
                             }
 253  0
                         }
 254  
                     });
 255  
         }
 256  11
         return jlstFamilies;
 257  
     }
 258  
 
 259  
     /**
 260  
      * This method initializes jlstSizes
 261  
      *
 262  
      * @return javax.swing.JList
 263  
      */
 264  
     private JList getJlstSizes() {
 265  11
         if (jlstSizes == null) {
 266  11
             jlstSizes = new JList(new Integer[] {Integer.valueOf(8),
 267  
                 Integer.valueOf(9), Integer.valueOf(10), Integer.valueOf(11),
 268  
                 Integer.valueOf(12), Integer.valueOf(14), Integer.valueOf(16),
 269  
                 Integer.valueOf(18), Integer.valueOf(20), Integer.valueOf(22),
 270  
                 Integer.valueOf(24), Integer.valueOf(26), Integer.valueOf(28),
 271  
                 Integer.valueOf(36), Integer.valueOf(48), Integer.valueOf(72)
 272  
             });
 273  11
             jlstSizes.setSelectedValue(resultSize, true);
 274  
 
 275  11
             jlstSizes.getSelectionModel().addListSelectionListener(
 276  11
                     new ListSelectionListener() {
 277  
                         public void valueChanged(ListSelectionEvent e) {
 278  0
                             if (jlstSizes.getSelectedValue() != null) {
 279  0
                                 resultSize = (Integer) jlstSizes
 280  
                                         .getSelectedValue();
 281  0
                                 updatePreview();
 282  
                             }
 283  0
                         }
 284  
                     });
 285  
         }
 286  11
         return jlstSizes;
 287  
     }
 288  
 
 289  
 
 290  
     /**
 291  
      * Updates preview label.
 292  
      */
 293  
     private void updatePreview() {
 294  11
         int style = 0;
 295  
 
 296  11
         Font previewFont = new Font(resultName, style, resultSize);
 297  11
         jlblPreview.setFont(previewFont);
 298  11
     }
 299  
 
 300  
     /**
 301  
      * This method initializes jbtnOk
 302  
      *
 303  
      * @return javax.swing.JButton
 304  
      */
 305  
     private JButton getJbtnOk() {
 306  11
         if (jbtnOk == null) {
 307  11
             jbtnOk = new JButton();
 308  11
             jbtnOk.setText(Translator.localize("button.ok"));
 309  
 
 310  11
             jbtnOk.addActionListener(new ActionListener() {
 311  
                 public void actionPerformed(ActionEvent e) {
 312  9
                     isOk = true;
 313  9
                     dispose();
 314  9
                     setVisible(false);
 315  9
                 }
 316  
             });
 317  
         }
 318  11
         return jbtnOk;
 319  
     }
 320  
 
 321  
     /**
 322  
      * This method initializes jbtnCancel
 323  
      *
 324  
      * @return javax.swing.JButton
 325  
      */
 326  
     private JButton getJbtnCancel() {
 327  11
         if (jbtnCancel == null) {
 328  11
             jbtnCancel = new JButton();
 329  11
             jbtnCancel.setText(Translator.localize("button.cancel"));
 330  
 
 331  11
             jbtnCancel.addActionListener(new ActionListener() {
 332  
                 public void actionPerformed(ActionEvent e) {
 333  1
                     isOk = false;
 334  1
                     dispose();
 335  1
                     setVisible(false);
 336  1
                 }
 337  
             });
 338  
         }
 339  11
         return jbtnCancel;
 340  
     }
 341  
 
 342  
     /**
 343  
      * Returns true if dilaog result is OK.
 344  
      *
 345  
      * @return dialog result
 346  
      */
 347  
     public boolean isOk() {
 348  10
         return isOk;
 349  
     }
 350  
 
 351  
     /**
 352  
      * Result.
 353  
      *
 354  
      * @return result
 355  
      */
 356  
     public String getResultName() {
 357  9
         return resultName;
 358  
     }
 359  
 
 360  
     /**
 361  
      * Result.
 362  
      *
 363  
      * @return result
 364  
      */
 365  
     public int getResultSize() {
 366  9
         return resultSize;
 367  
     }
 368  
 }