001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.ui.factory;
020
021import org.jdom2.Element;
022import org.jpos.ui.UI;
023import org.jpos.ui.UIFactory;
024
025import javax.swing.*;
026import javax.swing.event.ChangeEvent;
027import javax.swing.event.ChangeListener;
028import java.awt.*;
029import java.awt.event.ActionEvent;
030import java.awt.event.ActionListener;
031import java.util.ArrayList;
032import java.util.Iterator;
033import java.util.List;
034
035/**
036 * @author Alejandro Revilla
037 *
038 * creates a tabbed pane
039 * i.e:
040 * <pre>
041 *  &lt;tabbed-pane font="xxx"&gt;
042 *   &lt;pane title="xxx" icon="optional/icon/file.jpg"
043 *            action="yyy" command="zzz"&gt;
044 *   ...
045 *   ...
046 *   &lt;/pane&gt;
047 *  &lt;/tabbed-pane&gt;
048 * </pre>
049 * @see org.jpos.ui.UIFactory
050 */
051@SuppressWarnings("unchecked")
052public class JTabbedPaneFactory implements UIFactory, ChangeListener {
053    UI ui;
054    List actions = new ArrayList();
055    JTabbedPane p;
056
057    public JComponent create (UI ui, Element e) {
058        this.ui    = ui;
059        p = new JTabbedPane ();
060
061        String font = e.getAttributeValue ("font");
062        if (font != null) 
063            p.setFont (Font.decode (font));
064
065        Iterator iter = e.getChildren ("pane").iterator();
066        while (iter.hasNext()) {
067            add (p, (Element) iter.next ());
068        }
069        p.addChangeListener(this);
070        return p;
071    }
072
073    private void add (JTabbedPane p, Element e) {
074        if (e != null) {
075            Icon icon = null;
076            String iconFile = e.getAttributeValue ("icon");
077            if (iconFile != null)
078                icon = new ImageIcon (iconFile);
079            p.addTab (e.getAttributeValue ("title"), icon, ui.create (e));
080
081            String action[] = new String[2];
082            action[0]=e.getAttributeValue ("command");
083            action[1]=e.getAttributeValue ("action");
084            actions.add(action);
085        }
086    }
087
088    public void stateChanged (ChangeEvent e) {
089        try {
090            String action[] = new String[2];
091            action = (String[]) actions.get(p.getSelectedIndex());
092            Object al = ui.get (action[1]);
093            if (al instanceof ActionListener) {
094                ActionEvent ae = new ActionEvent(this,0,action[0]);
095                ((ActionListener) al).actionPerformed(ae);
096            }
097        } catch (Exception f) {
098            f.printStackTrace();
099        }
100    }
101}
102