All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.jaxx.widgets.status.StatusMessagePanel Maven / Gradle / Ivy

package org.nuiton.jaxx.widgets.status;

/*-
 * #%L
 * JAXX :: Widgets
 * %%
 * Copyright (C) 2008 - 2024 Code Lutin, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.border.BevelBorder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.jaxx.runtime.JAXXBinding;
import org.nuiton.jaxx.runtime.JAXXContext;
import org.nuiton.jaxx.runtime.JAXXObject;
import org.nuiton.jaxx.runtime.JAXXObjectDescriptor;
import org.nuiton.jaxx.runtime.JAXXUtil;
import org.nuiton.jaxx.runtime.binding.SimpleJAXXObjectBinding;
import org.nuiton.jaxx.runtime.context.DefaultJAXXContext;
import org.nuiton.jaxx.runtime.spi.UIHandler;
import org.nuiton.jaxx.runtime.swing.SwingUtil;
import org.nuiton.jaxx.runtime.swing.Table;

public class StatusMessagePanel extends Table implements ActionListener, JAXXObject {

    /*-----------------------------------------------------------------------*/
    /*---------------- Constants for all javaBean properties ----------------*/
    /*-----------------------------------------------------------------------*/

    public static final String PROPERTY_BUSY = "busy";
    public static final String PROPERTY_SHOW_BUSY = "showBusy";
    public static final String PROPERTY_SHOW_CLOCK = "showClock";
    public static final String PROPERTY_SHOW_MEMORY_STATUS = "showMemoryStatus";

    /*-----------------------------------------------------------------------*/
    /*------------------ Constants for all public bindings ------------------*/
    /*-----------------------------------------------------------------------*/

    public static final String BINDING_BUSY_WIDGET_VISIBLE = "busyWidget.visible";

    /*-----------------------------------------------------------------------*/
    /*--------------- Constants for all none public bindings  ---------------*/
    /*-----------------------------------------------------------------------*/

    private static final String BINDING_$CLOCK_WIDGET0_VISIBLE = "$ClockWidget0.visible";
    private static final String BINDING_$MEMORY_STATUS_WIDGET0_VISIBLE = "$MemoryStatusWidget0.visible";

    /*-----------------------------------------------------------------------*/
    /*------------------------- Other static fields -------------------------*/
    /*-----------------------------------------------------------------------*/

    private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAKVUTU8TURS9LbSFVj40fm4MhipGdEY0boT4UQhRUoSAicRufNN5aR9O543vvaHDhoULd/4BF27dGP4DcWXixi3/wYSf4H1vSqdAC4hdTJs755577u295/sfyEgBd7moWX7IFPetdRJFlgh9xRrUWni+trbkrNOqmqOyKliguID4k0pDugIFtx2XCqxKGZnsmMnWTHaLyZ7ljYD71O8gmi5DXqpNj8o6pUrB7Z7JVSnt1TZyOgpC0arVVXW3Wr8+Te2m6NyXNEAUoPwCtj35DwRJ1/1lSDNXwYXyOtkgtkf8GooTzK9hQ0M6NusRKV+RBv0AW5ArQzYgAskU3DvTeAydoYoCBbnia+J49L6CqcPym8ytUSUtqYgKpbVqvhaplKRGl4lPvSAwPFkFo7LOm4u0wcVmDFNwXku3dDtWiXOPEj+BD2r4rMer73VgOHkxoF+UQrl5KN7vHI1liiUeoe4RXSiyZJOZUpF+PdhG5XXmG9OJgqud0IVlwWsC2ykRocGX2jmFuOMycaiHf8yBJBM8CO9z4qJXDla+WOwcSCzhFGM+mqT5rrdph4pmcG2+OyfwdaBbRAJu9dpUvAwruYxkSVMVyIgQwwrGex+lvqsVRMUXNd77onQZA/y4vSW+Nfd2988oi+LGTsrqcAzc6UDwgArFtLaR+IZCxTx7kQTTFdw06qHdGDuZOF75aguJ6lFFvL6ayXpBZB3ZMrndnR+X3/3ug/Q85D1O3Hmi8S9hUNVxj+rcc6Pg6TMj7lxzAJ+jWqaCrMOFixpheMYliow5zHdxmZ5EOKWJ46fUFrW9tPfz6+ebO/uTSqHG4mkyk2ll3kKW+R7zqbGclpt0tZhCIGno8sQquplHSn/ng9ZFTpqn3a3/obj/ZcJ8RbXVzcQyxohCo3NCRXEUOvuhaUz/emTIr52GXBqzbJFr2OOzk+U2mGRohxrw4Kw0Ojx3DMON/2YoIsNfefknNGwHAAA=";
    private static final Logger log = LogManager.getLogger(StatusMessagePanel.class);
    private static final long serialVersionUID = 1L;

    /*-----------------------------------------------------------------------*/
    /*--------------------------- Internal states ---------------------------*/
    /*-----------------------------------------------------------------------*/

    protected List $activeBindings = new ArrayList();
    protected Map $bindingSources = new HashMap();
    protected final Map $bindings = new TreeMap();
    protected Map $objectMap = new HashMap();
    protected Map $previousValues = new HashMap();
    protected final JAXXContext delegateContext = new DefaultJAXXContext();

    /*-----------------------------------------------------------------------*/
    /*------------------------ Protected components  ------------------------*/
    /*-----------------------------------------------------------------------*/

    protected Box box;
    protected Boolean busy;
    protected JProgressBar busyWidget;
    protected StatusMessagePanelHandler handler;
    protected Boolean showBusy;
    protected Boolean showClock;
    protected Boolean showMemoryStatus;
    protected JLabel statusLabel;

    /*-----------------------------------------------------------------------*/
    /*------------------------- Private components  -------------------------*/
    /*-----------------------------------------------------------------------*/

    private Box $Box0;
    private ClockWidget $ClockWidget0;
    private MemoryStatusWidget $MemoryStatusWidget0;
    private StatusMessagePanel $Table0;

    /*-----------------------------------------------------------------------*/
    /*---------------------- Raw body code from script ----------------------*/
    /*-----------------------------------------------------------------------*/

    // To ensure status bar constant height, no matter what font are in use...
    protected final static String EMPTY_STATUS = " ";
    
    
    public void clearStatus() {
        handler.clearStatus();
    }
    
    public void setStatus(String status) {
        handler.setStatus(status);
    }
    
    @Override
    public void actionPerformed(ActionEvent evt) {
        handler.fadeStatus(this);
    }
    
    public  U getWidget(Class clazz) {
        for (Component component : box.getComponents()) {
            if (clazz == component.getClass()) {
                return (U) component;
            }
        }
        return null;
    }
    
    public void addWidget(Component w) {
        box.add(w);
    }
    
    public void addWidget(Component w, int index) {
        box.add(w, index);
    }
    
    public void init() {
        handler.init();
    }

    /*-----------------------------------------------------------------------*/
    /*---------------------------- Constructors  ----------------------------*/
    /*-----------------------------------------------------------------------*/

    public StatusMessagePanel() {
        $initialize();
    }

    public StatusMessagePanel(JAXXContext parentContext) {
        JAXXUtil.initContext(this, parentContext);
        $initialize();
    }

    /*-----------------------------------------------------------------------*/
    /*--------------------------- Statics methods ---------------------------*/
    /*-----------------------------------------------------------------------*/

    public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
        return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
    }

    /*-----------------------------------------------------------------------*/
    /*---------------------- JAXXObject implementation ----------------------*/
    /*-----------------------------------------------------------------------*/

    @Override
    public void applyDataBinding(String $binding) {
        if ($bindings.containsKey($binding)) {
            getDataBinding($binding).applyDataBinding();
        }
        processDataBinding($binding);
    }

    @Override
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        super.firePropertyChange(propertyName, oldValue, newValue);
    }

    @Override
    public Map get$objectMap() {
        return $objectMap;
    }

    @Override
    public JAXXBinding getDataBinding(String bindingId) {
        return $bindings.get(bindingId);
    }

    @Override
    public JAXXBinding[] getDataBindings() {
        return $bindings.values().toArray(new JAXXBinding[$bindings.size()]);
    }

    @Override
    public Object getObjectById(String id) {
        return $objectMap.get(id);
    }

    @Override
    public void processDataBinding(String $binding, boolean $force) {
        if (!$force && $activeBindings.contains($binding)) { 
            return;
        }
        $activeBindings.add($binding);
        try {
            if ($bindings.containsKey($binding)) {
                getDataBinding($binding).processDataBinding();
            }
        } finally {
            $activeBindings.remove($binding);
        }
    }

    @Override
    public void processDataBinding(String $binding) {
        processDataBinding($binding, false);
    }

    @Override
    public void registerDataBinding(JAXXBinding binding) {
        $bindings.put(binding.getId(), binding);
    }

    @Override
    public void removeDataBinding(String $binding) {
        if ($bindings.containsKey($binding)) {
            getDataBinding($binding).removeDataBinding();
        }
    }

    /*-----------------------------------------------------------------------*/
    /*--------------------- JAXXContext implementation  ---------------------*/
    /*-----------------------------------------------------------------------*/

    @Override
    public  T getContextValue(Class clazz) {
        return delegateContext.getContextValue(clazz, null);
    }

    @Override
    public  T getContextValue(Class clazz, String name) {
        return delegateContext.getContextValue(clazz, name);
    }

    @Override
    public JAXXContext getDelegateContext() {
        return delegateContext;
    }

    @Override
    public  O getParentContainer(Class clazz) {
        return SwingUtil.getParentContainer(this, clazz);
    }

    @Override
    public  O getParentContainer(Object source, Class clazz) {
        return SwingUtil.getParentContainer(source, clazz);
    }

    @Override
    public  void removeContextValue(Class clazz) {
        delegateContext.removeContextValue(clazz, null);
    }

    @Override
    public  void removeContextValue(Class clazz, String name) {
        delegateContext.removeContextValue(clazz, name);
    }

    @Override
    public  void setContextValue(T o) {
        delegateContext.setContextValue(o, null);
    }

    @Override
    public  void setContextValue(T o, String name) {
        delegateContext.setContextValue(o, name);
    }

    /*-----------------------------------------------------------------------*/
    /*----------------------- Public accessor methods -----------------------*/
    /*-----------------------------------------------------------------------*/

    public Box getBox() {
        return box;
    }

    public Boolean getBusy() {
        return busy;
    }

    public JProgressBar getBusyWidget() {
        return busyWidget;
    }

    public StatusMessagePanelHandler getHandler() {
        return handler;
    }

    public Boolean getShowBusy() {
        return showBusy;
    }

    public Boolean getShowClock() {
        return showClock;
    }

    public Boolean getShowMemoryStatus() {
        return showMemoryStatus;
    }

    public JLabel getStatusLabel() {
        return statusLabel;
    }

    public Boolean isBusy() {
        return busy !=null && busy;
    }

    public Boolean isShowBusy() {
        return showBusy !=null && showBusy;
    }

    public Boolean isShowClock() {
        return showClock !=null && showClock;
    }

    public Boolean isShowMemoryStatus() {
        return showMemoryStatus !=null && showMemoryStatus;
    }

    /*-----------------------------------------------------------------------*/
    /*----------------------- Public mutator methods  -----------------------*/
    /*-----------------------------------------------------------------------*/

    public void setBusy(Boolean busy) {
        Boolean oldValue = this.busy;
        this.busy = busy;
        firePropertyChange(PROPERTY_BUSY, oldValue, busy);
    }

    public void setShowBusy(Boolean showBusy) {
        Boolean oldValue = this.showBusy;
        this.showBusy = showBusy;
        firePropertyChange(PROPERTY_SHOW_BUSY, oldValue, showBusy);
    }

    public void setShowClock(Boolean showClock) {
        Boolean oldValue = this.showClock;
        this.showClock = showClock;
        firePropertyChange(PROPERTY_SHOW_CLOCK, oldValue, showClock);
    }

    public void setShowMemoryStatus(Boolean showMemoryStatus) {
        Boolean oldValue = this.showMemoryStatus;
        this.showMemoryStatus = showMemoryStatus;
        firePropertyChange(PROPERTY_SHOW_MEMORY_STATUS, oldValue, showMemoryStatus);
    }

    /*-----------------------------------------------------------------------*/
    /*--------------------- Protected accessors methods ---------------------*/
    /*-----------------------------------------------------------------------*/

    protected Box get$Box0() {
        return $Box0;
    }

    protected ClockWidget get$ClockWidget0() {
        return $ClockWidget0;
    }

    protected MemoryStatusWidget get$MemoryStatusWidget0() {
        return $MemoryStatusWidget0;
    }

    /*-----------------------------------------------------------------------*/
    /*--------------------- Components creation methods ---------------------*/
    /*-----------------------------------------------------------------------*/

    protected void addChildrenToBox() {
        box.add($MemoryStatusWidget0);
        box.add($ClockWidget0);
    }

    protected void createBox() {
        $objectMap.put("box", box = new Box(0));
        
        box.setName("box");
    }

    protected void createBusy() {
        $objectMap.put("busy", busy = Boolean.FALSE);
    }

    protected void createBusyWidget() {
        $objectMap.put("busyWidget", busyWidget = new JProgressBar());
        
        busyWidget.setName("busyWidget");
        busyWidget.setBorderPainted(true);
        busyWidget.setStringPainted(false);
    }

    protected void createShowBusy() {
        $objectMap.put("showBusy", showBusy = Boolean.FALSE);
    }

    protected void createShowClock() {
        $objectMap.put("showClock", showClock = Boolean.TRUE);
    }

    protected void createShowMemoryStatus() {
        $objectMap.put("showMemoryStatus", showMemoryStatus = Boolean.TRUE);
    }

    protected void createStatusLabel() {
        $objectMap.put("statusLabel", statusLabel = new JLabel());
        
        statusLabel.setName("statusLabel");
    }

    /*-----------------------------------------------------------------------*/
    /*------------------------ Internal jaxx methods ------------------------*/
    /*-----------------------------------------------------------------------*/

    protected void $initialize() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        $Table0 = this;
        JAXXObject.initialize(
                this,
                this::$initialize_01_createHandler,
                this::$initialize_01_createComponents,
                this::$initialize_02_registerDataBindings,
                this::$initialize_03_finalizeCreateComponents,
                this::$initialize_03_registerActions,
                this::$initialize_04_applyDataBindings,
                this::$initialize_05_setProperties,
                this::$initialize_06_finalizeInitialize);
    }

    protected void $initialize_01_createComponents() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        $objectMap.put("$Table0", $Table0);
        createShowMemoryStatus();
        createShowClock();
        createShowBusy();
        createBusy();
        // inline creation of $Box0
        $objectMap.put("$Box0", $Box0 = new Box(0));
        
        $Box0.setName("$Box0");
        createBusyWidget();
        createStatusLabel();
        createBox();
        // inline creation of $MemoryStatusWidget0
        $objectMap.put("$MemoryStatusWidget0", $MemoryStatusWidget0 = new MemoryStatusWidget());
        
        $MemoryStatusWidget0.setName("$MemoryStatusWidget0");
        // inline creation of $ClockWidget0
        $objectMap.put("$ClockWidget0", $ClockWidget0 = new ClockWidget());
        
        $ClockWidget0.setName("$ClockWidget0");
        // inline creation of $Table0
        setName("$Table0");
    }

    protected UIHandler $initialize_01_createHandler() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        return handler = new StatusMessagePanelHandler();
    }

    protected void $initialize_02_registerDataBindings() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // register 3 data bindings
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_BUSY_WIDGET_VISIBLE, true ,"showBusy") {
        
            @Override
            public void processDataBinding() {
                busyWidget.setVisible(isShowBusy());
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_$MEMORY_STATUS_WIDGET0_VISIBLE, true ,"showMemoryStatus") {
        
            @Override
            public void processDataBinding() {
                $MemoryStatusWidget0.setVisible( isShowMemoryStatus() );
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_$CLOCK_WIDGET0_VISIBLE, true ,"showClock") {
        
            @Override
            public void processDataBinding() {
                $ClockWidget0.setVisible( isShowClock() );
            }
        });
    }

    protected void $initialize_03_finalizeCreateComponents() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // inline complete setup of $Table0
        add($Box0, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, 17, 1, new Insets(0, 0, 0, 0), 0, 0));
        add(box, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 13, 0, new Insets(0, 0, 0, 0), 0, 0));
        // inline complete setup of $Box0
        $Box0.add(busyWidget);
        $Box0.add(statusLabel);
        addChildrenToBox();
    }

    protected void $initialize_03_registerActions() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
    }

    protected void $initialize_04_applyDataBindings() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        
        // apply 3 data bindings
        JAXXUtil.applyDataBinding(this, $bindings.keySet());
    }

    protected void $initialize_05_setProperties() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        
        // apply 1 property setters
        setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    }

    protected void $initialize_06_finalizeInitialize() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
    }

}