/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot.jaxodraw;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.ArrayList;
import java.util.ResourceBundle;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


/** Brings up a dialog that allows the inclusion of custom Latex packages
 * in the Latex output.
 */
public class JaxoLatexPackage implements ListSelectionListener {
    private ResourceBundle language =
        ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
    private ArrayList listVector;
    private JList list;
    private DefaultListModel listModel;
    private JButton removeButton;
    private JTextField packageName;
    private JDialog dialog;

    /** Constructor: initializes the dialog with the current list of
     * included Latex packages (at least axodraw and color).
     * @param vector The list of currently included Latex packages
     */
    public JaxoLatexPackage(ArrayList vector) {
        this.listVector = vector;
        listModel = new DefaultListModel();

        for (int i = 0; i < listVector.size(); i++) {
            listModel.add(i, listVector.get(i));
        }

        initComponents();
    }

    private void initComponents() {
        //Create the list and put it in a scroll pane
        list = new JList(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.addListSelectionListener(this);

        JScrollPane listScrollPane = new JScrollPane(list);

        JButton addButton = new JButton(language.getString("Add"));
        addButton.addActionListener(new AddPackageListener());

        removeButton = new JButton(language.getString("Remove"));
        removeButton.addMouseListener(new MouseAdapter() {
                public final void mouseClicked(MouseEvent evt) {
                    removeButtonClicked();
                }
            });
        removeButton.setEnabled(false);

        JButton okButton = new JButton(language.getString("OK"));

        //okButton.setToolTipText();
        okButton.addMouseListener(new MouseAdapter() {
                public final void mouseClicked(MouseEvent evt) {
                    okButtonClicked();
                }
            });

        JButton cancelButton = new JButton(language.getString("Cancel"));

        //okButton.setToolTipText();
        cancelButton.addMouseListener(new MouseAdapter() {
                public final void mouseClicked(MouseEvent evt) {
                    cancelButtonClicked();
                }
            });

        packageName = new JTextField(10);
        packageName.addActionListener(new AddPackageListener());

        //         String name =
        //             listModel.getElementAt(list.getSelectedIndex()).toString();
        packageName.setText("");

        //Create a panel that uses FlowLayout (the default).
        JPanel buttonPane = new JPanel();
        buttonPane.add(packageName);
        buttonPane.add(addButton);
        buttonPane.add(removeButton);
        buttonPane.add(okButton);
        buttonPane.add(cancelButton);

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

        dialog = new JDialog();
        dialog.getContentPane().add(listScrollPane, BorderLayout.CENTER);
        dialog.getContentPane().add(buttonPane, BorderLayout.SOUTH);

        //dialog.setSize(new Dimension(400, 200));
        dialog.setTitle(language.getString("Add_Latex_package"));
        dialog.pack();

        Dimension dialogSize = dialog.getSize();
        dialog.setLocation((d.width / 2) - (dialogSize.width / 2),
            (d.height / 2) - (dialogSize.height / 2));
        dialog.setVisible(true);
    }

    private void removeButtonClicked() {
        //This method can be called only if there's a valid selection.
        int index = list.getSelectedIndex();

        if ((index == 0) || (index == 1) || (index == 2)) {
            Toolkit.getDefaultToolkit().beep();

            return;
        }

        listModel.remove(index);

        int size = listModel.getSize();

        if (size == 3) {
            removeButton.setEnabled(false);
        } else {
            if (index == size) { //removed item in last position
                index--;
            }

            list.setSelectedIndex(index); //otherwise select same index
        }
    }

    /** Executes the corresponding action when the list of Latex packages
     * has been changed.
     * @param e The ListSelectionEvent
     */
    public final void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            int index = list.getSelectedIndex();

            if (index == -1) {
                removeButton.setEnabled(false);
                packageName.setText("");
            } else if ((index == 0) || (index == 1) || (index == 2)) {
                removeButton.setEnabled(false);
                packageName.setText("");
            } else {
                removeButton.setEnabled(true);

                String name = list.getSelectedValue().toString();
                packageName.setText(name);
            }
        }
    }

    /** Returns the Vector holding the list of Latex packages.
     * @return The vector holding the list of Latex packages.
     */
    public final ArrayList getPackageList() {
        return listVector;
    }

    private void okButtonClicked() {
        listVector.clear();

        for (int i = 0; i < listModel.getSize(); i++) {
            listVector.add(listModel.getElementAt(i));
        }

        dialog.dispose();
    }

    private void cancelButtonClicked() {
        dialog.dispose();
    }

    //This listener is shared by the text field and the add button
    class AddPackageListener implements ActionListener {
        public final void actionPerformed(ActionEvent e) {
            if (packageName.getText().equals("")) {
                Toolkit.getDefaultToolkit().beep();

                return;
            }

            //            int index = list.getSelectedIndex();
            int size = listModel.getSize();

            listModel.addElement(packageName.getText());
            list.setSelectedIndex(size);
        }
    }
}

 

Ads help maintain this website.