/*
 * FontChooser.java
 *
 */

package uk.ac.reload.editor.style;


/**
 * Esta clase es un JDialog que permite elegir fuente
 * @author  raul
 */
public class FontChooser extends javax.swing.JDialog {
    
    
    /**
     * La fuente elegida por el usuario
     */
    private java.awt.Font oUserFont;
    
    /**
     * Los nombres de las fuentes que se presentar�n al usuario
     */
    private String[] oFontNames;
    
    
    /**
     * Constructor de la clase
     */
    public FontChooser(java.awt.Frame parent,java.awt.Font oFont) {
        super(parent, true);
        initComponents();
        
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void  windowClosing(java.awt.event.WindowEvent oE) {
                close();
            }
        });
        oJLabel2.setText("Parameter");
        oJLabel3.setText("Operator");
        oJLabel4.setText("Value");
        oJButton1.setText("OK");
        oJButton2.setText("Cancel");
        oJLabel1.setBorder(new javax.swing.border.TitledBorder("Preview"));
        
        
        
        oFontNames= java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        
        
        oJList1.setListData(oFontNames);
        
        
        oJList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Plain", "Bold", "Italic", "Bold+Italic"};
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        
        
        oJList3.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "3","5","8","10","12","14","18","24","36","48"};
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        
        oJList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        oJList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        oJList3.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        
        //Fuente por defecto por si no hay ninguna fuente seleccionada
        
        //Mostramos en este font chooser la fuente actualmente seleccionada (si no es null, claro
        if (oFont!=null) {
            oJList3.clearSelection();
            oJList1.setSelectedValue(oFont.getFamily(),true);
            oJTextField1.setText(oFont.getName());
            oJList2.setSelectedIndex(oFont.getStyle());
            oJTextField2.setText((String) oJList2.getSelectedValue());
            oJList3.setSelectedValue(String.valueOf(oFont.getSize()),true);
            oJFormattedTextField1.setValue(oFont.getSize());
            oUserFont = oFont;
            oJLabel1.setFont(oUserFont);
            oJLabel1.setText(oUserFont.getFamily()+" "+oJList2.getSelectedValue()+" "+oUserFont.getSize());
            
        } else { //Si no mostramos la fuente por defecto
              
            oJList1.setSelectedIndex(0);
            oJList2.setSelectedIndex(0);
            oJList3.setSelectedIndex(4);
            oJTextField1.setText((String) oJList1.getSelectedValue());
            oJTextField2.setText((String) oJList2.getSelectedValue());
            oJFormattedTextField1.setValue(Integer.parseInt((String) oJList3.getSelectedValue()));
            
            readFont();
            oJLabel1.setFont(oUserFont);
            oJLabel1.setText(oUserFont.getFamily()+" "+oJList2.getSelectedValue()+" "+oUserFont.getSize());
        }
        
        
        oJList1.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent oE) {
                list1Changed(oE);
            }
        });
        
        oJList2.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent oE) {
                list2Changed(oE);
            }
        });
        
        oJList3.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent oE) {
                list3Changed(oE);
            }
        });
        
        oJFormattedTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent oE) {
                textChanged(oE);
            }
        });
        
        
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        oJTextField1 = new javax.swing.JTextField();
        oJTextField2 = new javax.swing.JTextField();
        oJFormattedTextField1 = new javax.swing.JFormattedTextField(java.text.NumberFormat.getIntegerInstance());
        oJScrollPane1 = new javax.swing.JScrollPane();
        oJList1 = new javax.swing.JList();
        oJScrollPane2 = new javax.swing.JScrollPane();
        oJList2 = new javax.swing.JList();
        oJScrollPane3 = new javax.swing.JScrollPane();
        oJList3 = new javax.swing.JList();
        oJLabel1 = new javax.swing.JLabel();
        oJPanel1 = new javax.swing.JPanel();
        oJButton1 = new javax.swing.JButton();
        oJButton2 = new javax.swing.JButton();
        oJLabel2 = new javax.swing.JLabel();
        oJLabel3 = new javax.swing.JLabel();
        oJLabel4 = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        oJTextField1.setEditable(false);
        oJTextField1.setPreferredSize(new java.awt.Dimension(100, 19));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(oJTextField1, gridBagConstraints);

        oJTextField2.setEditable(false);
        oJTextField2.setPreferredSize(new java.awt.Dimension(100, 19));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(oJTextField2, gridBagConstraints);

        oJFormattedTextField1.setMinimumSize(new java.awt.Dimension(11, 19));
        oJFormattedTextField1.setPreferredSize(new java.awt.Dimension(100, 19));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(oJFormattedTextField1, gridBagConstraints);

        oJScrollPane1.setMinimumSize(new java.awt.Dimension(150, 152));
        oJScrollPane1.setPreferredSize(new java.awt.Dimension(150, 152));
        oJScrollPane1.setViewportView(oJList1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
        getContentPane().add(oJScrollPane1, gridBagConstraints);

        oJScrollPane2.setMinimumSize(new java.awt.Dimension(150, 152));
        oJScrollPane2.setPreferredSize(new java.awt.Dimension(150, 152));
        oJScrollPane2.setViewportView(oJList2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
        getContentPane().add(oJScrollPane2, gridBagConstraints);

        oJScrollPane3.setMinimumSize(new java.awt.Dimension(150, 152));
        oJScrollPane3.setPreferredSize(new java.awt.Dimension(150, 152));
        oJScrollPane3.setViewportView(oJList3);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
        getContentPane().add(oJScrollPane3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(oJLabel1, gridBagConstraints);

        oJButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oJButton1ActionPerformed(evt);
            }
        });

        oJPanel1.add(oJButton1);

        oJButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oJButton2ActionPerformed(evt);
            }
        });

        oJPanel1.add(oJButton2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(oJPanel1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_END;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
        getContentPane().add(oJLabel2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_END;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
        getContentPane().add(oJLabel3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_END;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
        getContentPane().add(oJLabel4, gridBagConstraints);

        pack();
    }
    // </editor-fold>//GEN-END:initComponents
    
    
    /**
     * Funci�n llamada cuando el usuario pulsa Cancel
     */
    private void oJButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oJButton2ActionPerformed
        close();
    }//GEN-LAST:event_oJButton2ActionPerformed
    
    /**
     * Cierra la ventana y pone a null el valor de selecci�n de fuente.
     */
    private void close() {
        oUserFont=null;
        dispose();
    }
    
    
    /**
     * Funci�n llamada cuando el usuario pulsa OK
     */
    private void oJButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oJButton1ActionPerformed
        readFont();
        dispose();
    }//GEN-LAST:event_oJButton1ActionPerformed
    
    /**
     * Esta funci�n lee los par�metros seleccionados por el usuario y crea la fuente
     */
    private void readFont() {
        java.awt.Font oFont = java.awt.Font.decode(oFontNames[oJList1.getSelectedIndex()]);
        int iStyle=oJList2.getSelectedIndex();
        if (oJFormattedTextField1.getValue() instanceof Integer)  {
            int iSize =(Integer) oJFormattedTextField1.getValue();
            oUserFont = oFont.deriveFont(iStyle,iSize);
        } else {
            long lSize = (Long) oJFormattedTextField1.getValue();
            oUserFont = oFont.deriveFont(iStyle,new Long(lSize).intValue());
        }
    }
    
    /**
     * Funci�n llamada cuando el usuario selecciona un nombre de fuente
     * en el JList destinado al efecto
     */
    private void list1Changed(javax.swing.event.ListSelectionEvent oE) {
        if (oE.getValueIsAdjusting()) return;
        javax.swing.ListSelectionModel oLSM = (javax.swing.ListSelectionModel) oE.getSource();
        int iSelectedRow = oLSM.getMinSelectionIndex();
        oJTextField1.setText((String) oJList1.getSelectedValue());
        oJTextField1.setCaretPosition(0);
        readFont();
        oJLabel1.setFont(oUserFont);
        oJLabel1.setText(oUserFont.getFamily()+" "+oJList2.getSelectedValue()+" "+oUserFont.getSize());
        
    }
    
    /**
     * Funci�n llamada cuando el usuario selecciona un estilo de fuente en el
     * JList destinado al efecto
     */
    private void list2Changed(javax.swing.event.ListSelectionEvent oE) {
        if (oE.getValueIsAdjusting()) return;
        javax.swing.ListSelectionModel oLSM = (javax.swing.ListSelectionModel) oE.getSource();
        int iSelectedRow = oLSM.getMinSelectionIndex();
        oJTextField2.setText((String) oJList2.getSelectedValue());
        readFont();
        oJLabel1.setFont(oUserFont);
        oJLabel1.setText(oUserFont.getFamily()+" "+oJList2.getSelectedValue()+" "+oUserFont.getSize());
    }
    
    /**
     * Funci�n llamada cuando el usuario selecciona un tama�o de fuente
     * en el JList destinado al efecto
     */
    private void list3Changed(javax.swing.event.ListSelectionEvent oE) {
        if (oE.getValueIsAdjusting()) return;
        if (oJList3.isSelectionEmpty()) return;
        javax.swing.ListSelectionModel oLSM = (javax.swing.ListSelectionModel) oE.getSource();
        int iSelectedRow = oLSM.getMinSelectionIndex();
        oJFormattedTextField1.setValue(Integer.parseInt((String) oJList3.getSelectedValue()));
        readFont();
        oJLabel1.setFont(oUserFont);
        oJLabel1.setText(oUserFont.getFamily()+" "+oJList2.getSelectedValue()+" "+oUserFont.getSize());
    }
    
    /**
     * Funci�n llamada cuando el usuario introduce un tama�o de fuente 
     * en el JFormattedTextField destinado al efecto
     */
    private void textChanged(java.awt.event.ActionEvent oE) {
        readFont();
        //Leemos el contenido del TextField y vemos si el valor introducido
        //coincide con alguno de los valores de la JList3, si es as�, seleccionamos el elemento en la lista
        //En caso contrario limpiamos la lista
        oJList3.clearSelection();
        oJList3.setSelectedValue(oJFormattedTextField1.getText(),true);
        oJLabel1.setFont(oUserFont);
        oJLabel1.setText(oUserFont.getFamily()+" "+oJList2.getSelectedValue()+" "+oUserFont.getSize());
    }
    
    /**
     * Devuelve la fuente seleccionada por el usuario en el cuadro de di�logo
     * @param La fuente
     */
    public java.awt.Font getFont() {
        return oUserFont;
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton oJButton1;
    private javax.swing.JButton oJButton2;
    private javax.swing.JFormattedTextField oJFormattedTextField1;
    private javax.swing.JLabel oJLabel1;
    private javax.swing.JLabel oJLabel2;
    private javax.swing.JLabel oJLabel3;
    private javax.swing.JLabel oJLabel4;
    private javax.swing.JList oJList1;
    private javax.swing.JList oJList2;
    private javax.swing.JList oJList3;
    private javax.swing.JPanel oJPanel1;
    private javax.swing.JScrollPane oJScrollPane1;
    private javax.swing.JScrollPane oJScrollPane2;
    private javax.swing.JScrollPane oJScrollPane3;
    private javax.swing.JTextField oJTextField1;
    private javax.swing.JTextField oJTextField2;
    // End of variables declaration//GEN-END:variables
    
}
