OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.ui.grid;

import org.openconcerto.ui.VFlowLayout;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DecoratedGridPanel extends JPanel {

    private GridPanel grid;

    public DecoratedGridPanel(JComponent header, int columnCount, int rowHeight, List<JComponent> rowHeader) {
        this.setBackground(Color.WHITE);
        this.setLayout(new GridBagLayout());
        JPanel topLeft = new JPanel();
        this.grid = new GridPanel(columnCount, rowHeader.size(), rowHeight);
        header.setPreferredSize(new Dimension(this.grid.getPreferredSize().width, header.getMinimumSize().height));
        header.setMaximumSize(new Dimension(this.grid.getPreferredSize().width, header.getMinimumSize().height));
        header.setMinimumSize(new Dimension(this.grid.getPreferredSize().width, header.getMinimumSize().height));
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.BOTH;
        this.add(topLeft, c);
        c.gridx++;
        c.weightx = 0;
        this.add(header, c);
        //
        c.gridwidth = 2;
        c.weightx = 1;
        c.weighty = 1;
        c.gridx = 0;
        c.gridy++;
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c2 = new GridBagConstraints();
        c2.gridx = 0;
        c2.gridy = 0;
        c2.weightx = 1;
        c2.anchor = GridBagConstraints.NORTH;
        c2.fill = GridBagConstraints.BOTH;
        panel.add(createLeft(rowHeader, this.grid.getCellHeight()), c2);
        c2.gridx++;
        c2.weightx = 0;
        c2.weighty = 1;
        panel.add(this.grid, c2);

        final JScrollPane scroll = new JScrollPane(panel);
        scroll.setBorder(null);
        this.add(scroll, c);

    }

    private Component createLeft(List<JComponent> rowHeader, int h) {
        JPanel p = new JPanel();
        p.setLayout(new VFlowLayout(VFlowLayout.TOP, 1, 1, true));

        for (JComponent c : rowHeader) {
            c.setMinimumSize(new Dimension(c.getMinimumSize().width, h));
            c.setPreferredSize(new Dimension(c.getPreferredSize().width, h));
            p.add(c);
        }

        return p;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
                    //
                }
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                TwoYearsHeaderPanel b = new TwoYearsHeaderPanel(2018);
                List<JComponent> panels = new ArrayList<>();
                for (int i = 0; i < 5; i++) {
                    panels.add(new JButton("line " + i));
                }

                final DecoratedGridPanel contentPane = new DecoratedGridPanel(b, 24, 40, panels);
                contentPane.add(new GridItem(0, 0, 1, 1));
                contentPane.add(new GridItem(4, 0, 3, 1));
                contentPane.add(new GridItem(1, 1, 2, 1));
                f.setContentPane(contentPane);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }

    public void add(GridItem gridItem) {
        this.grid.add(gridItem);

    }

    public void setEnableEditMode(boolean enable) {
        this.grid.setEnableEditMode(enable);
    }

    public void setGridListener(GridListener gridListener) {
        this.grid.setGridListener(gridListener);
    }

}