OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.table;
2
 
3
import java.awt.Color;
4
import java.awt.Dimension;
5
import java.awt.FlowLayout;
6
import java.awt.GridBagConstraints;
7
import java.awt.GridBagLayout;
8
import java.awt.Rectangle;
9
import java.awt.event.ActionEvent;
10
import java.awt.event.ActionListener;
11
import java.util.ArrayList;
12
import java.util.Collections;
13
import java.util.Comparator;
14
import java.util.List;
15
import java.util.Set;
16
 
17
import javax.swing.ImageIcon;
18
import javax.swing.JButton;
19
import javax.swing.JLabel;
20
import javax.swing.JPanel;
21
import javax.swing.JViewport;
22
import javax.swing.Scrollable;
23
import javax.swing.SwingConstants;
24
 
25
import org.openconcerto.erp.config.ComptaPropsConfiguration;
26
import org.openconcerto.modules.extensionbuilder.ClickableLabel;
86 ilm 27
import org.openconcerto.modules.extensionbuilder.Extension;
74 ilm 28
import org.openconcerto.sql.element.SQLElement;
29
import org.openconcerto.sql.model.SQLField;
30
import org.openconcerto.sql.model.SQLTable;
31
import org.openconcerto.ui.DefaultGridBagConstraints;
32
import org.openconcerto.ui.DefaultListModel;
33
import org.openconcerto.ui.JLabelBold;
34
import org.openconcerto.utils.Tuple2;
35
 
36
public class TableModifyInfoPanel extends JPanel implements Scrollable {
86 ilm 37
    private Extension extension;
38
 
74 ilm 39
    // TODO: tooltip sur un champs pour indiquer quelles extensions l'utilisent
40
 
86 ilm 41
    public TableModifyInfoPanel(Extension extension, SQLTable t, final TableDescritor desc, final TableModifyLeftPanel leftPanel) {
42
        this.extension = extension;
74 ilm 43
        this.setLayout(new GridBagLayout());
44
        this.setBackground(Color.WHITE);
45
        GridBagConstraints c = new DefaultGridBagConstraints();
46
        c.weightx = 1;
47
 
48
        Set<SQLField> fields = t.getFields();
49
        List<SQLField> allFields = new ArrayList<SQLField>();
50
        for (SQLField sqlField : fields) {
51
            final String fName = sqlField.getName();
52
            if (!fName.equalsIgnoreCase("ARCHIVE") && !fName.equalsIgnoreCase("ORDRE")) {
53
                allFields.add(sqlField);
54
            }
55
        }
56
        Collections.sort(allFields, new Comparator<SQLField>() {
57
 
58
            @Override
59
            public int compare(SQLField o1, SQLField o2) {
60
 
61
                return o1.getName().compareTo(o2.getName());
62
            }
63
        });
64
 
65
        List<SQLTable> lt = new ArrayList<SQLTable>(t.getDBSystemRoot().getGraph().getReferentTables(t));
66
        Collections.sort(lt, new Comparator<SQLTable>() {
67
 
68
            @Override
69
            public int compare(SQLTable o1, SQLTable o2) {
70
 
71
                return o1.getName().compareTo(o2.getName());
72
            }
73
        });
74
        if (lt.size() > 0) {
75
            this.add(new JLabel(lt.size() + " champs des tables suivantes référencent la table " + t.getName() + " :"), c);
76
            c.gridy++;
77
            c.weightx = 0;
78
            for (final SQLTable sqlTable : lt) {
79
                this.add(new ClickableLabel(sqlTable.getName(), new Runnable() {
80
 
81
                    @Override
82
                    public void run() {
83
                        leftPanel.selectTable(sqlTable.getName());
84
 
85
                    }
86
                }), c);
87
                c.gridy++;
88
            }
89
            c.gridy++;
90
            c.weightx = 1;
91
            this.add(new JLabel(" "), c);
92
            c.gridy++;
93
        }
94
        this.add(new JLabel("Cette table contient " + allFields.size() + " champs :"), c);
95
 
96
        c.gridy++;
97
        for (SQLField sqlField : allFields) {
98
            String str = "";
99
 
100
            JPanel line = new JPanel();
101
            line.add(new JLabelBold(sqlField.getName()));
102
            line.setOpaque(false);
103
            line.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
104
            try {
105
                String label = ComptaPropsConfiguration.getTranslator(t).getLabelFor(sqlField);
106
                if (label != null) {
107
                    str += " (" + label + ")";
108
                }
109
            } catch (Exception e) {
110
                // No label...
111
            }
112
            String type = sqlField.getTypeDecl().toLowerCase();
113
            if (type.contains("double") || type.contains("real") || type.contains("float")) {
114
                type = "nombre décimal";
115
            } else if (type.contains("int")) {
116
                type = "nombre entier";
117
            } else if (type.contains("text") || type.contains("varchar")) {
118
                type = "texte";
119
            } else if (type.contains("boolean")) {
120
                type = "booléen";
121
            } else if (type.contains("timestamp")) {
122
                type = "date et heure";
123
            }
124
            if (sqlField.isKey() && sqlField.getForeignTable() != null) {
125
                final SQLTable foreignTable = sqlField.getForeignTable();
126
                str += " référence vers la table ";
127
                line.add(new JLabel(str));
128
                if (foreignTable.getDBRoot().equals(t.getDBRoot())) {
129
                    line.add(new ClickableLabel(foreignTable.getName(), new Runnable() {
130
                        @Override
131
                        public void run() {
132
                            leftPanel.selectTable(foreignTable.getName());
133
                        }
134
                    }));
135
                } else {
136
                    line.add(new JLabel(foreignTable.getName()));
137
                }
138
                SQLElement e = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(foreignTable);
139
                if (e != null) {
140
                    line.add(new JLabel(" (cad " + e.getSingularName() + ")"));
141
                }
142
 
143
            } else if (sqlField.isPrimaryKey()) {
144
                str += " clef primaire";
145
                line.add(new JLabel(str));
146
            } else {
147
                str += " de type " + type;
148
                line.add(new JLabel(str));
149
            }
150
 
151
            this.add(line, c);
152
            c.gridy++;
153
        }
154
 
155
        final JPanel spacer = new JPanel();
156
        spacer.setOpaque(false);
157
        c.weighty = 1;
158
        c.gridy++;
159
        this.add(spacer, c);
160
 
161
    }
162
 
163
    Tuple2<JPanel, GridBagConstraints> createEditorList(final TableDescritor desc) {
164
        JPanel p = new JPanel();
165
        p.setLayout(new GridBagLayout());
166
        GridBagConstraints c = new DefaultGridBagConstraints();
167
        List<FieldDescriptor> fields = desc.getFields();
168
        for (final FieldDescriptor field : fields) {
169
            addField(desc, p, c, field);
170
        }
171
        Tuple2<JPanel, GridBagConstraints> result = Tuple2.create(p, c);
172
        return result;
173
 
174
    }
175
 
176
    private void addField(final TableDescritor desc, final JPanel p, GridBagConstraints c, final FieldDescriptor field) {
177
        c.weightx = 1;
178
        c.gridx = 0;
181 ilm 179
        final FieldDescriptorEditor editor = new FieldDescriptorEditor(this.extension, field);
74 ilm 180
        p.add(editor, c);
181
 
182
        c.gridx++;
183
        c.weightx = 0;
184
        final JButton close = new JButton(new ImageIcon(DefaultListModel.class.getResource("close_popup.png")));
185
 
186
        p.add(close, c);
187
        close.addActionListener(new ActionListener() {
188
 
189
            @Override
190
            public void actionPerformed(ActionEvent e) {
191
                desc.remove(field);
192
                p.remove(editor);
193
                p.remove(close);
194
                p.revalidate();
195
            }
196
        });
197
        c.gridy++;
198
    }
199
 
200
    public Dimension getPreferredScrollableViewportSize() {
201
        return getPreferredSize();
202
    }
203
 
204
    public int getScrollableUnitIncrement(final Rectangle visibleRect, final int orientation, final int direction) {
205
        switch (orientation) {
206
        case SwingConstants.VERTICAL:
207
            return visibleRect.height / 10;
208
        case SwingConstants.HORIZONTAL:
209
            return visibleRect.width / 10;
210
        default:
211
            throw new IllegalArgumentException("Invalid orientation: " + orientation);
212
        }
213
    }
214
 
215
    public int getScrollableBlockIncrement(final Rectangle visibleRect, final int orientation, final int direction) {
216
        switch (orientation) {
217
        case SwingConstants.VERTICAL:
218
            return visibleRect.height;
219
        case SwingConstants.HORIZONTAL:
220
            return visibleRect.width;
221
        default:
222
            throw new IllegalArgumentException("Invalid orientation: " + orientation);
223
        }
224
    }
225
 
226
    public boolean getScrollableTracksViewportWidth() {
227
        if (getParent() instanceof JViewport) {
228
            return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
229
        }
230
        return false;
231
    }
232
 
233
    public boolean getScrollableTracksViewportHeight() {
234
        if (getParent() instanceof JViewport) {
235
            return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
236
        }
237
        return false;
238
    }
239
 
240
}