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.TreeSelectionEvent;
027import javax.swing.event.TreeSelectionListener;
028import javax.swing.tree.DefaultMutableTreeNode;
029import java.awt.*;
030import java.awt.event.ActionEvent;
031import java.awt.event.ActionListener;
032import java.util.HashMap;
033import java.util.Iterator;
034import java.util.Map;
035import java.util.StringTokenizer;
036
037/**
038 * @author Alejandro Revilla
039 *
040 * Creates a JTree
041 * i.e:
042 * <pre>
043 *  &lt;tree&gt;
044 *   &lt;node&gt;
045 *    ...
046 *   &lt;/node&gt;
047 *   ...
048 *   ...
049 *   &lt;node&gt;
050 *    ...
051 *   &lt;/node&gt;
052 *  &lt;/tree&gt;
053 * </pre>
054 * @see org.jpos.ui.UIFactory
055 */
056@SuppressWarnings("unchecked")
057public class JTreeFactory implements UIFactory {
058    public JComponent create (UI ui, Element e) {
059        final UI parentUI = ui;
060        final Map map = new HashMap ();
061        final JTree tree = new JTree (getNode (e, map));
062        String font = e.getAttributeValue ("font");
063        if (font != null) 
064            tree.setFont (Font.decode (font));
065        tree.setRootVisible (e.getTextTrim().length() > 0);
066
067        tree.addTreeSelectionListener (
068            new TreeSelectionListener() {
069                public void valueChanged(TreeSelectionEvent evt) {
070                    DefaultMutableTreeNode node = 
071                        (DefaultMutableTreeNode) 
072                            tree.getLastSelectedPathComponent();
073                    if (node != null) {
074                        String s = (String) map.get (node);
075                        if (s != null) {
076                            StringTokenizer st = new StringTokenizer (s);
077                            String action  = st.nextToken ();
078                            String command = null;
079                            if (st.hasMoreTokens ())
080                                command = st.nextToken ();
081
082                            ActionListener al = 
083                                (ActionListener) parentUI.get(action);
084                            if (al != null) {
085                                al.actionPerformed (
086                                    new ActionEvent (node, 0, command)
087                                );
088                            }
089                            // System.out.println ("ACTION: " + action);
090                            // System.out.println ("COMMAND: " + command);
091                        }
092                    }
093                }
094            }
095        );
096        return tree;
097    }
098
099    private DefaultMutableTreeNode getNode (Element e, Map map) { 
100        DefaultMutableTreeNode node = new DefaultMutableTreeNode (
101                e.getTextTrim()
102        );
103        String action = e.getAttributeValue ("action");
104        if (action != null) {
105            String command = e.getAttributeValue ("command");
106            if (command != null)
107                action = action + " " + command;
108
109            map.put (node, action);
110        }
111        Iterator iter = e.getChildren().iterator();
112        while (iter.hasNext()) {
113            node.add (getNode ((Element) iter.next (), map));
114        }
115        return node;
116    }
117}
118