OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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