OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
140 ilm 1
package org.openconcerto.modules.label;
2
 
3
import java.awt.BasicStroke;
4
import java.awt.Color;
5
import java.awt.Dimension;
6
import java.awt.Graphics;
7
import java.awt.Graphics2D;
8
import java.awt.RenderingHints;
9
import java.awt.event.MouseAdapter;
10
import java.awt.event.MouseEvent;
11
import java.awt.print.PageFormat;
12
import java.awt.print.Printable;
13
import java.awt.print.PrinterException;
14
import java.util.List;
15
 
16
import javax.swing.JPanel;
17
 
18
public class LabelPanel extends JPanel implements Printable {
19
 
181 ilm 20
    private List<? extends Label> list;
140 ilm 21
    private int lines;
22
    private int columns;
23
    private Boolean[][] mask;
24
    private final Color VERY_LIGHT_GRAY = new Color(230, 230, 230);
25
    private LabelRenderer renderer;
26
    private boolean ignoreMargins = false;
27
    private int topMargin;
28
    private int leftMargin;
29
 
181 ilm 30
    public LabelPanel(List<? extends Label> list, int l, int c, LabelRenderer labelRenderer) {
140 ilm 31
        this.list = list;
32
        this.renderer = labelRenderer;
33
        setSizeLayoutSize(l, c);
34
        this.addMouseListener(new MouseAdapter() {
35
            @Override
36
            public void mousePressed(MouseEvent e) {
37
                int w = getWidth() / columns;
38
                int h = getHeight() / lines;
39
                int j = e.getX() / w;
40
                int i = e.getY() / h;
41
                if (i >= 0 && i < lines && j >= 0 && j < columns) {
42
                    mask[i][j] = !mask[i][j];
43
                }
44
                repaint();
45
            }
46
        });
47
    }
48
 
181 ilm 49
    public void setList(List<? extends Label> list) {
50
        this.list = list;
51
        repaint();
52
    }
53
 
140 ilm 54
    private void setSizeLayoutSize(int l, int c) {
55
        this.lines = l;
56
        this.columns = c;
57
        this.mask = new Boolean[l][c];
58
        for (int i = 0; i < l; i++) {
59
            for (int j = 0; j < c; j++) {
60
                this.mask[i][j] = Boolean.FALSE;
61
            }
62
        }
63
    }
64
 
65
    public void setIgnoreMargins(boolean ignoreMargins) {
66
        this.ignoreMargins = ignoreMargins;
67
    }
68
 
69
    @Override
70
    public void paint(Graphics g) {
71
        g.setColor(Color.WHITE);
72
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
73
 
74
        int w = (this.getWidth() - leftMargin * 2) / this.columns;
75
        int h = (this.getHeight() - topMargin * 2) / this.lines;
76
 
77
        Graphics2D g2 = (Graphics2D) g;
78
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
79
        float dash1[] = { 10.0f };
80
        BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
81
        BasicStroke n = new BasicStroke(1.0f);
82
        int rowIndex = 0;
83
 
84
        for (int i = 0; i < this.lines; i++) {
85
            for (int j = 0; j < this.columns; j++) {
86
                boolean mask = this.mask[i][j];
87
                final int x = j * w + leftMargin;
88
                final int y = i * h + topMargin;
89
                if (mask) {
90
                    // Disabled
91
                    g.setColor(VERY_LIGHT_GRAY);
92
                    g.fillRect(x, y, w, h);
93
                    // Dashed borders
94
                    g.setColor(Color.GRAY);
95
                    g2.setStroke(dashed);
96
                    g.drawRect(x, y, w, h);
97
 
98
                } else {
99
                    // Dashed borders
100
                    g.setColor(Color.WHITE);
101
                    g2.setStroke(n);
102
                    g.drawRect(x, y, w, h);
103
                    g.setColor(Color.GRAY);
104
                    g2.setStroke(dashed);
105
                    g.drawRect(x, y, w, h);
106
                    // Label
107
                    if (rowIndex < list.size()) {
181 ilm 108
                        Label row = list.get(rowIndex);
140 ilm 109
                        renderer.paintLabel(g, row, x, y, w, h, 10f);
110
                    }
111
                    rowIndex++;
112
                }
113
 
114
            }
115
        }
116
 
117
    }
118
 
119
    @Override
120
    public Dimension getPreferredSize() {
121
        return new Dimension(210 * 3, 297 * 3);
122
    }
123
 
124
    public void setLineCount(int l) {
125
        setSizeLayoutSize(l, this.columns);
126
        repaint();
127
    }
128
 
129
    public void setColumnCount(int c) {
130
        setSizeLayoutSize(this.lines, c);
131
        repaint();
132
    }
133
 
134
    @Override
135
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
136
        int nbLabelPerPage = 0;
137
        for (int i = 0; i < this.lines; i++) {
138
            for (int j = 0; j < this.columns; j++) {
139
                boolean mask = this.mask[i][j];
140
                if (!mask) {
141
                    nbLabelPerPage++;
142
                }
143
            }
144
        }
145
        if (nbLabelPerPage < 1) {
146
            return NO_SUCH_PAGE;
147
        }
148
        int labelCount = this.list.size();
149
        if (nbLabelPerPage * pageIndex > labelCount) {
150
            return NO_SUCH_PAGE;
151
        }
152
 
153
        int imageableWidth = (int) pageFormat.getImageableWidth();
154
        int imageableHeight = (int) pageFormat.getImageableHeight();
155
        int imageableX = (int) pageFormat.getImageableX();
156
        int imageableY = (int) pageFormat.getImageableY();
157
        if (ignoreMargins) {
158
            imageableWidth = (int) pageFormat.getWidth();
159
            imageableHeight = (int) pageFormat.getHeight();
160
            imageableX = 0;
161
            imageableY = 0;
162
        }
163
 
164
        int w = (imageableWidth - 2 * leftMargin) / this.columns;
165
        int h = (imageableHeight - 2 * topMargin) / this.lines;
166
        // Margins
167
        imageableX += leftMargin;
168
        imageableY += topMargin;
169
        // print labels
170
        int rowIndex = pageIndex * nbLabelPerPage;
171
        for (int i = 0; i < this.lines; i++) {
172
            for (int j = 0; j < this.columns; j++) {
173
                boolean mask = this.mask[i][j];
174
                if (!mask && rowIndex < list.size()) {
181 ilm 175
                    Label row = list.get(rowIndex);
140 ilm 176
                    renderer.paintLabel(g, row, j * w + imageableX, i * h + imageableY, w, h, 10f);
177
                }
178
                rowIndex++;
179
            }
180
 
181
        }
182
        return PAGE_EXISTS;
183
    }
184
 
185
    public void setTopMargin(int margin) {
186
        this.topMargin = margin;
187
        repaint();
188
    }
189
 
190
    public void setLeftMargin(int margin) {
191
        this.leftMargin = margin;
192
        repaint();
193
    }
194
}