OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
174 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.grid;
15
 
16
import org.openconcerto.ui.VFlowLayout;
17
 
18
import java.awt.Color;
19
import java.awt.Component;
20
import java.awt.Dimension;
21
import java.awt.GridBagConstraints;
22
import java.awt.GridBagLayout;
23
import java.util.ArrayList;
24
import java.util.List;
25
 
26
import javax.swing.JButton;
27
import javax.swing.JComponent;
28
import javax.swing.JFrame;
29
import javax.swing.JPanel;
30
import javax.swing.JScrollPane;
31
import javax.swing.SwingUtilities;
32
import javax.swing.UIManager;
33
import javax.swing.UnsupportedLookAndFeelException;
34
 
35
public class DecoratedGridPanel extends JPanel {
36
 
37
    private GridPanel grid;
38
 
39
    public DecoratedGridPanel(JComponent header, int columnCount, int rowHeight, List<JComponent> rowHeader) {
40
        this.setBackground(Color.WHITE);
41
        this.setLayout(new GridBagLayout());
42
        JPanel topLeft = new JPanel();
177 ilm 43
        topLeft.setMinimumSize(new Dimension(0, 0));
44
 
174 ilm 45
        this.grid = new GridPanel(columnCount, rowHeader.size(), rowHeight);
46
        header.setPreferredSize(new Dimension(this.grid.getPreferredSize().width, header.getMinimumSize().height));
47
        header.setMaximumSize(new Dimension(this.grid.getPreferredSize().width, header.getMinimumSize().height));
48
        header.setMinimumSize(new Dimension(this.grid.getPreferredSize().width, header.getMinimumSize().height));
49
        GridBagConstraints c = new GridBagConstraints();
50
        c.gridx = 0;
51
        c.gridy = 0;
52
        c.weightx = 1;
53
        c.gridwidth = 1;
54
        c.fill = GridBagConstraints.BOTH;
55
        this.add(topLeft, c);
56
        c.gridx++;
57
        c.weightx = 0;
58
        this.add(header, c);
177 ilm 59
        c.gridx++;
60
        c.weightx = 0;
61
        JPanel spacerScroll = new JPanel();
62
        int wScroll = ((Integer) UIManager.get("ScrollBar.width")).intValue();
63
        spacerScroll.setMinimumSize(new Dimension(wScroll, header.getHeight()));
64
        spacerScroll.setPreferredSize(new Dimension(wScroll, header.getHeight()));
65
        spacerScroll.setMaximumSize(new Dimension(wScroll, header.getHeight()));
66
        spacerScroll.setSize(new Dimension(wScroll, header.getHeight()));
67
        this.add(spacerScroll, c);
68
 
174 ilm 69
        //
177 ilm 70
        c.gridwidth = 3;
174 ilm 71
        c.weightx = 1;
72
        c.weighty = 1;
73
        c.gridx = 0;
74
        c.gridy++;
177 ilm 75
        JPanel panel = new ScrollablePanel() {
76
            @Override
77
            public boolean getScrollableTracksViewportWidth() {
78
                return true;
79
            }
80
 
81
            @Override
82
            public boolean getScrollableTracksViewportHeight() {
83
                return false;
84
            }
85
        };
174 ilm 86
        panel.setLayout(new GridBagLayout());
87
        GridBagConstraints c2 = new GridBagConstraints();
88
        c2.gridx = 0;
89
        c2.gridy = 0;
90
        c2.weightx = 1;
91
        c2.anchor = GridBagConstraints.NORTH;
92
        c2.fill = GridBagConstraints.BOTH;
93
        panel.add(createLeft(rowHeader, this.grid.getCellHeight()), c2);
94
        c2.gridx++;
95
        c2.weightx = 0;
96
        c2.weighty = 1;
97
        panel.add(this.grid, c2);
98
 
99
        final JScrollPane scroll = new JScrollPane(panel);
177 ilm 100
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
174 ilm 101
        scroll.setBorder(null);
102
        this.add(scroll, c);
103
 
104
    }
105
 
106
    private Component createLeft(List<JComponent> rowHeader, int h) {
107
        JPanel p = new JPanel();
108
        p.setLayout(new VFlowLayout(VFlowLayout.TOP, 1, 1, true));
109
 
110
        for (JComponent c : rowHeader) {
111
            c.setMinimumSize(new Dimension(c.getMinimumSize().width, h));
112
            c.setPreferredSize(new Dimension(c.getPreferredSize().width, h));
113
            p.add(c);
114
        }
115
 
116
        return p;
117
    }
118
 
119
    public static void main(String[] args) {
120
        SwingUtilities.invokeLater(new Runnable() {
121
 
122
            @Override
123
            public void run() {
124
                try {
125
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
126
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
127
                    //
128
                }
129
                JFrame f = new JFrame();
130
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
131
 
132
                TwoYearsHeaderPanel b = new TwoYearsHeaderPanel(2018);
133
                List<JComponent> panels = new ArrayList<>();
134
                for (int i = 0; i < 5; i++) {
135
                    panels.add(new JButton("line " + i));
136
                }
137
 
138
                final DecoratedGridPanel contentPane = new DecoratedGridPanel(b, 24, 40, panels);
139
                contentPane.add(new GridItem(0, 0, 1, 1));
140
                contentPane.add(new GridItem(4, 0, 3, 1));
141
                contentPane.add(new GridItem(1, 1, 2, 1));
142
                f.setContentPane(contentPane);
143
                f.pack();
144
                f.setLocationRelativeTo(null);
145
                f.setVisible(true);
146
            }
147
        });
148
    }
149
 
150
    public void add(GridItem gridItem) {
151
        this.grid.add(gridItem);
152
 
153
    }
154
 
155
    public void setEnableEditMode(boolean enable) {
156
        this.grid.setEnableEditMode(enable);
157
    }
158
 
159
    public void setGridListener(GridListener gridListener) {
160
        this.grid.setGridListener(gridListener);
161
    }
162
 
163
}