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 * UIFactory that builds a Swing {@link JTabbedPane} from {@code <pane>} children. 037 * 038 * @author Alejandro Revilla 039 * 040 * creates a tabbed pane 041 * i.e: 042 * <pre> 043 * <tabbed-pane font="xxx"> 044 * <pane title="xxx" icon="optional/icon/file.jpg" 045 * action="yyy" command="zzz"> 046 * ... 047 * ... 048 * </pane> 049 * </tabbed-pane> 050 * </pre> 051 * @see org.jpos.ui.UIFactory 052 */ 053@SuppressWarnings("unchecked") 054public class JTabbedPaneFactory implements UIFactory, ChangeListener { 055 /** Default constructor for {@link UIFactory} discovery. */ 056 public JTabbedPaneFactory() {} 057 UI ui; 058 List actions = new ArrayList(); 059 JTabbedPane p; 060 061 public JComponent create (UI ui, Element e) { 062 this.ui = ui; 063 p = new JTabbedPane (); 064 065 String font = e.getAttributeValue ("font"); 066 if (font != null) 067 p.setFont (Font.decode (font)); 068 069 Iterator iter = e.getChildren ("pane").iterator(); 070 while (iter.hasNext()) { 071 add (p, (Element) iter.next ()); 072 } 073 p.addChangeListener(this); 074 return p; 075 } 076 077 private void add (JTabbedPane p, Element e) { 078 if (e != null) { 079 Icon icon = null; 080 String iconFile = e.getAttributeValue ("icon"); 081 if (iconFile != null) 082 icon = new ImageIcon (iconFile); 083 p.addTab (e.getAttributeValue ("title"), icon, ui.create (e)); 084 085 String action[] = new String[2]; 086 action[0]=e.getAttributeValue ("command"); 087 action[1]=e.getAttributeValue ("action"); 088 actions.add(action); 089 } 090 } 091 092 public void stateChanged (ChangeEvent e) { 093 try { 094 String action[] = new String[2]; 095 action = (String[]) actions.get(p.getSelectedIndex()); 096 Object al = ui.get (action[1]); 097 if (al instanceof ActionListener) { 098 ActionEvent ae = new ActionEvent(this,0,action[0]); 099 ((ActionListener) al).actionPerformed(ae); 100 } 101 } catch (Exception f) { 102 f.printStackTrace(); 103 } 104 } 105} 106