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.iso.ISOChannel;
023import org.jpos.iso.ISOUtil;
024import org.jpos.iso.gui.ISOChannelPanel;
025import org.jpos.iso.gui.ISOMeter;
026import org.jpos.ui.UI;
027import org.jpos.ui.UIFactory;
028import org.jpos.util.NameRegistrar;
029
030import javax.swing.*;
031import java.util.Observable;
032
033/**
034 * UIFactory that builds an {@link ISOMeter} hooked to the channel resolved
035 * from the configuration's {@code idref} attribute.
036 *
037 * @author Alejandro Revilla
038 *
039 * Creates an ISOMeter component
040 * i.e:
041 * <pre>
042 *  &lt;iso-meter idref="id" scroll="true|false" refresh="nnn"/&gt;
043 * </pre>
044 * @see org.jpos.ui.UIFactory
045 */
046
047public class ISOMeterFactory implements UIFactory {
048    /** Default constructor for {@link UIFactory} discovery. */
049    public ISOMeterFactory() {}
050    public JComponent create (UI ui, Element e) {
051        ISOChannelPanel icp = null;
052        try {
053            Object obj = NameRegistrar.get (e.getAttributeValue ("idref"));
054
055            if (obj instanceof ISOChannel) {
056                icp = new ISOChannelPanel ((ISOChannel) obj, e.getText ());
057            } else if (obj instanceof Observable) {
058                icp = new ISOChannelPanel (e.getText());
059                ((Observable)obj).addObserver (icp);
060            }
061            ISOMeter meter = icp.getISOMeter ();
062            if ("false".equals (e.getAttributeValue ("scroll")))
063                meter.setScroll (false);
064
065            String protect = e.getAttributeValue ("protect");
066            if (protect != null)
067                icp.setProtectFields (ISOUtil.toIntArray (protect));
068            String wipe = e.getAttributeValue ("wipe");
069            if (wipe != null)
070                icp.setWipeFields (ISOUtil.toIntArray (wipe));
071
072            String refresh = e.getAttributeValue ("refresh");
073            if (refresh != null)
074                meter.setRefresh (Integer.parseInt (refresh));
075        } catch (Exception ex) {
076            ex.printStackTrace ();
077            return new JLabel (ex.getMessage());
078        }
079        return icp;
080    }
081}
082