Skip to content

Sample 011

Michael Karneim edited this page May 9, 2018 · 1 revision
package wiki.snippets;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.Border;

import org.beanfabrics.ModelProvider;
import org.beanfabrics.Path;
import org.beanfabrics.View;
import org.beanfabrics.model.AbstractPM;
import org.beanfabrics.model.DatePM;
import org.beanfabrics.model.ListPM;
import org.beanfabrics.model.PMManager;
import org.beanfabrics.model.PresentationModel;
import org.beanfabrics.model.TextPM;
import org.beanfabrics.swing.BnLabel;
import org.beanfabrics.swing.list.BnList;
import org.beanfabrics.swing.list.CellConfig;
import org.beanfabrics.swing.list.cellrenderer.BnListCellRenderer;
import org.beanfabrics.swing.list.cellrenderer.PMListCellRenderer;

/**
 * Custom renderer for BnList
 */
public class Sample011 {
    /**
     * PresentationModel for a chronicle entry
     */
    static class ChronicleEntryPM extends AbstractPM {
        TextPM title = new TextPM();
        TextPM type = new TextPM();
        DatePM installationDate = new DatePM();
        TextPM status = new TextPM();

        public ChronicleEntryPM() {
            PMManager.setup(this);
            DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM);
            format.setLenient(false);
            installationDate.setFormat(format);
        }

        public void setData( String title, String type, Date date, String status) {
            this.title.setText(title);
            this.type.setText(type);
            this.installationDate.setDate(date);
            this.status.setText(status);
        }
    }
    /**
     * PresentationModel for the chronicle with it's entries
     */
    static class ChroniclePM extends AbstractPM {
        ListPM<ChronicleEntryPM> entries = new ListPM<ChronicleEntryPM>();

        public ChroniclePM() {
            PMManager.setup(this);
        }
        public void addEntry( String title, String type, Date date, String status) {
            ChronicleEntryPM entry = new ChronicleEntryPM();
            entry.setData(title, type, date, status);
            entries.add( entry);
        }
    }

    /**
     * View for a single chronicle entry
     */
    @SuppressWarnings("serial")
    static class ChronicleEntryPanel extends JPanel implements View<ChronicleEntryPM> {
        private BnLabel dateValueLabel;
        private BnLabel statusValueLabel;
        private JLabel statusLabel;
        private JLabel installationDateLabel;
        private BnLabel typeValueLabel;
        private BnLabel titleValueLabel;
        private ModelProvider localProvider = new ModelProvider(); // @wb:location=8,429

        public ChronicleEntryPanel() {
            localProvider.setPresentationModelType(ChronicleEntryPM.class);
            final GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.columnWidths = new int[] {0,7};
            gridBagLayout.rowHeights = new int[] {0,7,7,7};
            setLayout( gridBagLayout);
            final GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.insets = new Insets(2, 2, 2, 2);
            gridBagConstraints.weightx = 1;
            gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
            gridBagConstraints.gridwidth = 2;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.gridx = 0;
            add(getTitleValueLabel(), gridBagConstraints);
            final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
            gridBagConstraints_1.insets = new Insets(2, 2, 2, 2);
            gridBagConstraints_1.weightx = 1;
            gridBagConstraints_1.fill = GridBagConstraints.HORIZONTAL;
            gridBagConstraints_1.gridwidth = 2;
            gridBagConstraints_1.gridy = 1;
            gridBagConstraints_1.gridx = 0;
            add(getTypeValueLabel(), gridBagConstraints_1);
            final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
            gridBagConstraints_2.insets = new Insets(2, 2, 2, 2);
            gridBagConstraints_2.fill = GridBagConstraints.HORIZONTAL;
            gridBagConstraints_2.gridy = 2;
            gridBagConstraints_2.gridx = 0;
            add(getInstallationDateLabel(), gridBagConstraints_2);
            final GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
            gridBagConstraints_3.insets = new Insets(2, 2, 2, 2);
            gridBagConstraints_3.fill = GridBagConstraints.HORIZONTAL;
            gridBagConstraints_3.gridy = 3;
            gridBagConstraints_3.gridx = 0;
            final GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();
            gridBagConstraints_5.insets = new Insets(2, 2, 2, 2);
            gridBagConstraints_5.weightx = 1;
            gridBagConstraints_5.fill = GridBagConstraints.HORIZONTAL;
            gridBagConstraints_5.gridy = 2;
            gridBagConstraints_5.gridx = 1;
            add(getDateValueLabel(), gridBagConstraints_5);
            add(getStatusLabel(), gridBagConstraints_3);
            final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
            gridBagConstraints_4.insets = new Insets(2, 2, 2, 2);
            gridBagConstraints_4.weightx = 1;
            gridBagConstraints_4.fill = GridBagConstraints.HORIZONTAL;
            gridBagConstraints_4.gridy = 3;
            gridBagConstraints_4.gridx = 1;
            add(getStatusValueLabel(), gridBagConstraints_4);
            setBackground(Color.WHITE);
            
        }

        public ChronicleEntryPM getPresentationModel() {
            return localProvider.getPresentationModel();
        }

        public void setPresentationModel(ChronicleEntryPM pModel) {
            localProvider.setPresentationModel(pModel);
        }

        protected BnLabel getTitleValueLabel() {
            if (titleValueLabel == null) {
                titleValueLabel = new BnLabel();
                titleValueLabel.setPath(new org.beanfabrics.Path("this.title"));
                titleValueLabel.setModelProvider(localProvider);
                titleValueLabel.setFont( titleValueLabel.getFont().deriveFont(Font.BOLD,12));
            }
            return titleValueLabel;
        }

        protected BnLabel getTypeValueLabel() {
            if (typeValueLabel == null) {
                typeValueLabel = new BnLabel();
                typeValueLabel.setForeground(new Color(178, 34, 34));
                typeValueLabel.setPath(new org.beanfabrics.Path("this.type"));
                typeValueLabel.setModelProvider(localProvider);
                typeValueLabel.setFont( typeValueLabel.getFont().deriveFont(Font.BOLD,12));
            }
            return typeValueLabel;
        }

        protected JLabel getInstallationDateLabel() {
            if (installationDateLabel == null) {
                installationDateLabel = new JLabel();
                installationDateLabel.setText("Installation Date:");
                installationDateLabel.setFont( installationDateLabel.getFont().deriveFont(Font.PLAIN));
            }
            return installationDateLabel;
        }

        protected JLabel getStatusLabel() {
            if (statusLabel == null) {
                statusLabel = new JLabel();
                statusLabel.setText("Status:");
                statusLabel.setFont( statusLabel.getFont().deriveFont(Font.PLAIN));
            }
            return statusLabel;
        }

        protected BnLabel getStatusValueLabel() {
            if (statusValueLabel == null) {
                statusValueLabel = new BnLabel();
                statusValueLabel.setPath(new org.beanfabrics.Path("this.status"));
                statusValueLabel.setModelProvider(localProvider);
                statusValueLabel.setFont( statusValueLabel.getFont().deriveFont(Font.PLAIN));
            }
            return statusValueLabel;
        }

        protected BnLabel getDateValueLabel() {
            if (dateValueLabel == null) {
                dateValueLabel = new BnLabel();
                dateValueLabel.setPath(new org.beanfabrics.Path("this.installationDate"));
                dateValueLabel.setModelProvider(localProvider);
                dateValueLabel.setFont( dateValueLabel.getFont().deriveFont(Font.PLAIN));
            }
            return dateValueLabel;
        }
    }

    /**
     * The custom renderer for a chronicle entry
     */
    static class ChronicleEntryRenderer implements PMListCellRenderer {
        private ChronicleEntryPanel panel = new ChronicleEntryPanel();
        private Border focusBorder = BorderFactory.createLineBorder( UIManager.getColor("Button.focus"));
        private Border emptyBorder = BorderFactory.createCompoundBorder(
                BorderFactory.createMatteBorder(0, 0, 1, 0, Color.lightGray),
                BorderFactory.createEmptyBorder(1, 1, 0, 1)
                );
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            if ( value instanceof ChronicleEntryPM) {
                panel.setPresentationModel( ((ChronicleEntryPM)value));
                if ( isSelected ) {
                    panel.setBackground( list.getSelectionBackground());
                } else {
                    panel.setBackground( list.getBackground());
                }
                if ( cellHasFocus) {
                    panel.setBorder( focusBorder);
                } else {
                    panel.setBorder( emptyBorder);
                }
                return panel;
            } else {
                return null;
            }
        }
        public boolean supportsPresentationModel(PresentationModel model) {
            return model instanceof ChronicleEntryPM;
        }
    }

    /**
     * View for the chronicle
     */
    @SuppressWarnings("serial")
    static class ChroniclePanel extends JPanel implements View<ChroniclePM> {
        private BnList bnList;
        private JScrollPane scrollPane;
        private ModelProvider localProvider = new ModelProvider(); // @wb:location=8,429

        public ChroniclePanel() {
            localProvider.setPresentationModelType(ChroniclePM.class);
            setLayout( new BorderLayout());
            add(getScrollPane(), BorderLayout.CENTER);
            
        }

        public ChroniclePM getPresentationModel() {
            return localProvider.getPresentationModel();
        }

        public void setPresentationModel(ChroniclePM pModel) {
            localProvider.setPresentationModel(pModel);
        }

        protected JScrollPane getScrollPane() {
            if (scrollPane == null) {
                scrollPane = new JScrollPane();
                scrollPane.setViewportView(getBnList());
            }
            return scrollPane;
        }

        protected BnList getBnList() {
            if (bnList == null) {
                bnList = new BnList();
                bnList.setModelProvider(localProvider);
                bnList.setPath( new Path("this.entries"));
                BnListCellRenderer renderer = new BnListCellRenderer();
                // Add the custom renderer here
                renderer.getInstalledRenderers().add( new ChronicleEntryRenderer());
                bnList.setCellRenderer( renderer);
                bnList.setCellConfig( new CellConfig( new Path("this")));
            }
            return bnList;
        }

    }

    /**
     * main program
     */
    public static void main(String[] args) throws Exception {
        Locale.setDefault(Locale.US);
        final ChroniclePM chronicle = new ChroniclePM();
        chronicle.addEntry("Firefox 3.0.4 (2008102920)", "Security Update", getDate(2008, 11, 29, 15, 11), "Update was installed successfully");
        chronicle.addEntry("Firefox 3.0.3 (2008092417)", "Security Update", getDate(2008, 11, 17, 10, 47), "Update was installed successfully");
        chronicle.addEntry("Firefox 3.0.1 (2008070208)", "New Version", getDate(2008, 8, 26, 11, 31), "Update was installed successfully");
        chronicle.addEntry("Firefox 2.0.0.16 (2008070205)", "Security Update", getDate(2008, 7, 16, 9, 33), "Update was installed successfully");

        EventQueue.invokeLater( new Runnable() {
            public void run() {
                ChroniclePanel panel = new ChroniclePanel();
                panel.setPresentationModel( chronicle);

                JFrame frame = new JFrame("Update ChroniclePM");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add( panel, BorderLayout.CENTER);
                frame.setSize(400, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    static Date getDate( int year, int month, int date, int hour, int minute) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(year, month-1, date, hour, minute);
        return cal.getTime();
    }
}
Clone this wiki locally