Package fr.soleil.gui.flyscan.gui.custom.tabbedpane

Source Code of fr.soleil.gui.flyscan.gui.custom.tabbedpane.CloseSaveButtonTabbedPane$CloseSaveButtonTab

package fr.soleil.gui.flyscan.gui.custom.tabbedpane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.plaf.metal.MetalIconFactory;

import fr.soleil.comete.swing.StringButton;
import fr.soleil.gui.flyscan.ConfigurationGUI;
import fr.soleil.gui.flyscan.gui.custom.Utilities;
import fr.soleil.lib.flyscan.model.parsing.configuration.Configuration;

/**
* Cf http://halilkarakose.blogspot.fr/2009/01/easiest-tab-implementation-with-close.html
* User: Halil KARAKOSE
*/
public class CloseSaveButtonTabbedPane extends JTabbedPane {

    private static final long serialVersionUID = 6962156616195622387L;
    private final boolean isExpert;

    public CloseSaveButtonTabbedPane(boolean isExpert) {
        super();
        this.isExpert = isExpert;

    }

    @Override
    public void addTab(String title, Icon icon, Component component, String tip) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(component, BorderLayout.CENTER);
        super.addTab(title, icon, panel, tip);
        int count = this.getTabCount() - 1;
        if (component instanceof ConfigurationGUI) {
            ConfigurationGUI configGui = ((ConfigurationGUI) component);
            boolean isEnabled = configGui.isEnabled();
            setTabComponentAt(count, new CloseSaveButtonTab(panel, title, icon, (ConfigurationGUI) component, isExpert,
                    isEnabled));
        }
    }

    @Override
    public void addTab(String title, Icon icon, Component component) {
        addTab(title, icon, component, null);
    }

    @Override
    public void addTab(String title, Component component) {
        addTab(title, null, component);
    }

    /**
     * Add tab with header
     *
     * @param title
     * @param component
     * @param errorComponent
     * @param tabIndex (default value -1)
     */
    public void addTabWithHeader(String title, Component component, JComponent errorComponent, int tabIndex) {

        final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, errorComponent, component);

        if (errorComponent.isVisible()) {
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerSize(8);
        } else {
            splitPane.setDividerLocation(0);
            splitPane.setDividerSize(0);
            splitPane.setOneTouchExpandable(false);
        }

        splitPane.setResizeWeight(0.1);

        int count = -1;
        if (tabIndex == -1) {
            super.addTab(title, null, splitPane, null);
            count = this.getTabCount() - 1;
        } else {
            count = tabIndex;
            setComponentAt(count, component);
        }

        if (component instanceof ConfigurationGUI) {
            ConfigurationGUI configGui = ((ConfigurationGUI) component);
            boolean isEnabled = configGui.isEnabled();
            setTabComponentAt(count, new CloseSaveButtonTab(splitPane, title, null, (ConfigurationGUI) component,
                    isExpert, isEnabled));
        }
    }

    @Override
    public void setComponentAt(int index, Component component) {
        Component comp = getComponentAt(index);
        if (comp instanceof JSplitPane) {
            JSplitPane splitPane = (JSplitPane) comp;
            if (splitPane != null) {
                splitPane.setLeftComponent(component);
            }
        } else {
            super.setComponentAt(index, component);
        }

    }

    @Override
    public void setTitleAt(int index, String title) {
        Component component = getTabComponentAt(index);
        CloseSaveButtonTab closeSaveButtonTab = (CloseSaveButtonTab) component;
        closeSaveButtonTab.setTitle(title);
    }

    @Override
    public String getTitleAt(int index) {
        String title = null;
        Component component = getTabComponentAt(index);
        CloseSaveButtonTab closeSaveButtonTab = (CloseSaveButtonTab) component;
        if (closeSaveButtonTab != null) {
            title = closeSaveButtonTab.getTitle();
        }
        return title;
    }

    @Override
    public int indexOfComponent(Component component) {
        int index = -1;
        for (Component comp : getComponents()) {
            if (comp instanceof JSplitPane/* && ((JPanel) comp).getLayout() instanceof BorderLayout*/) {
                index++;
                for (Component c : ((JSplitPane) comp).getComponents()) {
                    if (component.equals(c)) {
                        return index;
                    }
                }
            }
        }
        return super.indexOfComponent(component);
    }

    public void enableSaveButton(int index, boolean enable) {
        Component component = getTabComponentAt(index);
        CloseSaveButtonTab closeSaveButtonTab = (CloseSaveButtonTab) component;
        closeSaveButtonTab.getSaveButton().setEnabled(enable);

    }

//    public void enableCheckButton(int index, boolean enable) {
//        Component component = getTabComponentAt(index);
//        CloseSaveButtonTab closeSaveButtonTab = (CloseSaveButtonTab) component;
//        closeSaveButtonTab.getCheckButton().setEnabled(enable);
//    }

    /**
     * Return configuration
     *
     * @param index int
     * @return Configuration
     */
    public Configuration getConfiguration(int index) {
        Configuration config = null;
        Component component = getTabComponentAt(index);
        CloseSaveButtonTab closeSaveButtonTab = (CloseSaveButtonTab) component;
        if (closeSaveButtonTab != null) {
            config = closeSaveButtonTab.getConfigurationGUI().getConfiguration();
        }
        return config;

    }

    public class CloseSaveButtonTab extends JPanel {

        private static final long serialVersionUID = 2980721798267279743L;

        private final Component tab;

        private String title;

        private final JLabel label;

        private final JButton saveButton;

        private final StringButton checkButton;

        private final ConfigurationGUI configurationGUI;

        private final boolean isExpert;

        private final boolean isEnabled;

        public CloseSaveButtonTab(final Component tab, final String title, Icon icon,
                ConfigurationGUI configurationGUI, boolean isExpert, boolean isEnabled) {
            this.tab = tab;
            this.title = title;
            this.configurationGUI = configurationGUI;
            this.isExpert = isExpert;
            this.isEnabled = isEnabled;

            setOpaque(false);
            FlowLayout flowLayout = new FlowLayout(FlowLayout.LEADING, 5, 5);
            setLayout(flowLayout);
            setVisible(true);

            label = new JLabel(this.title);
            label.setIcon(icon);
            add(label);

            saveButton = new JButton(Utilities.ICONS.getIcon("flyscan.save"));

            saveButton.setEnabled(false);
            saveButton.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
            if (this.isEnabled && this.isExpert) {
                add(saveButton);
            }

            checkButton = new StringButton();
            checkButton.setIcon(Utilities.ICONS.getIcon("flyscan.check"));
            checkButton.setToolTipText("Ask flyscan device to check configuration");
            checkButton.setParameter(this.title);

            checkButton.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));

            checkButton.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    JButton button = (JButton) e.getSource();
                    button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    JButton button = (JButton) e.getSource();
                    button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
                }
            });

            if (this.isEnabled) {
                add(checkButton);
            }

            JButton closeButton = new JButton(MetalIconFactory.getInternalFrameCloseIcon(16));
            closeButton.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
            closeButton.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (getTitle().trim().endsWith(Utilities.ASTERISK)) {
                        Object[] options = { "Abort", "Close the tab" };
                        int n = JOptionPane.showOptionDialog(tab,
                                "Some changes have not been saved. Do you still want to close the tab?",
                                "Changes not saved", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                                null, options, options[0]);
                        if (n == 1) {
                            removeTab(tab);
                        }
                    } else {
                        removeTab(tab);
                    }

                }

                private void removeTab(final Component tab) {
                    JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent();
                    tabbedPane.remove(tab);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    JButton button = (JButton) e.getSource();
                    button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    JButton button = (JButton) e.getSource();
                    button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
                }
            });
            add(closeButton);

        }

        public Component getTab() {
            return tab;
        }

        public void setTitle(String title) {
            this.title = title;
            label.setText(title);
        }

        public String getTitle() {
            if (title == null) {
                title = label.getText();
            }
            return title;
        }

        public JButton getSaveButton() {
            return saveButton;
        }

        public StringButton getCheckButton() {
            return checkButton;
        }

        public ConfigurationGUI getConfigurationGUI() {
            return configurationGUI;
        }
    }
}
TOP

Related Classes of fr.soleil.gui.flyscan.gui.custom.tabbedpane.CloseSaveButtonTabbedPane$CloseSaveButtonTab

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.