OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 93 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 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
 /*
15
 * Créé le 2 mai 2005
16
 */
17
package org.openconcerto.sql.element;
18
 
73 ilm 19
import static org.openconcerto.sql.TM.getTM;
17 ilm 20
import org.openconcerto.sql.model.SQLRow;
73 ilm 21
import org.openconcerto.sql.view.EditFrame;
17 ilm 22
import org.openconcerto.utils.checks.ValidListener;
23
import org.openconcerto.utils.checks.ValidObject;
21 ilm 24
import org.openconcerto.utils.checks.ValidState;
17 ilm 25
 
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.awt.GridLayout;
29
import java.awt.Insets;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
 
80 ilm 33
import javax.swing.BorderFactory;
17 ilm 34
import javax.swing.BoxLayout;
35
import javax.swing.ImageIcon;
36
import javax.swing.JButton;
37
import javax.swing.JOptionPane;
38
import javax.swing.JPanel;
39
import javax.swing.JSeparator;
40
import javax.swing.SwingConstants;
25 ilm 41
import javax.swing.UIManager;
17 ilm 42
 
43
/**
44
 * A default ElementSQLObject that displays a button to create, and when created a button to delete
45
 * and a button to hide/expand.
46
 *
47
 * @author Sylvain CUAZ
48
 */
49
public class DefaultElementSQLObject extends ElementSQLObject {
50
 
51
    // possible si currentID != 1 && isValidated
52
    private JButton expandBtn;
53
    private boolean expanded;
54
 
55
    // valent null si requis
56
    private JButton createBtn;
57
    private JButton supprBtn;
58
    private JSeparator separator;
59
    private boolean isSeparatorVisible = true;
60
    private JPanel editP;
61
    private JPanel createP;
62
 
63
    public DefaultElementSQLObject(SQLComponent parent, SQLComponent comp) {
64
        super(parent, comp);
65
 
66
        this.addValidListener(new ValidListener() {
25 ilm 67
            @Override
21 ilm 68
            public void validChange(ValidObject src, ValidState newValue) {
17 ilm 69
                compChanged();
70
            }
71
        });
72
    }
73
 
74
    public void showSeparator(boolean visible) {
75
        this.isSeparatorVisible = visible;
25 ilm 76
        if (this.separator != null)
17 ilm 77
            this.separator.setVisible(visible);
78
    }
79
 
80
    public void setDecorated(boolean decorated) {
25 ilm 81
        if (this.expandBtn != null)
17 ilm 82
            this.expandBtn.setVisible(decorated);
83
    }
84
 
25 ilm 85
    @Override
83 ilm 86
    protected void uiChanged() {
87
        super.uiChanged();
88
        updateBtns();
89
    }
90
 
91
    protected void updateBtns() {
92
        this.createBtn.setVisible(this.isCreatedUIVisible());
93
        this.supprBtn.setVisible(this.isCreatedUIVisible() && this.deleteAllowed() && !Boolean.FALSE.equals(this.isExpanded()));
94
    }
95
 
96
    @Override
17 ilm 97
    protected void uiInit() {
25 ilm 98
        final boolean isPlastic = UIManager.getLookAndFeel().getClass().getName().startsWith("com.jgoodies.plaf.plastic");
99
 
17 ilm 100
        this.expandBtn = new JButton("+/-");
101
        this.expandBtn.setEnabled(false);
25 ilm 102
        this.expandBtn.setOpaque(false);
17 ilm 103
        this.expandBtn.addActionListener(new ActionListener() {
25 ilm 104
            @Override
17 ilm 105
            public void actionPerformed(ActionEvent e) {
106
                toggleExpand();
107
            }
108
        });
109
 
80 ilm 110
        this.supprBtn = new JButton(new ImageIcon(DefaultElementSQLObject.class.getResource("delete.png")));
73 ilm 111
        this.supprBtn.setToolTipText(getTM().translate("remove"));
17 ilm 112
        this.supprBtn.setOpaque(false);
25 ilm 113
        if (isPlastic)
80 ilm 114
            this.supprBtn.setBorder(BorderFactory.createEmptyBorder());
17 ilm 115
        this.supprBtn.addActionListener(new ActionListener() {
25 ilm 116
            @Override
17 ilm 117
            public void actionPerformed(ActionEvent e) {
118
                if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0 || this.confirm())
119
                    setCreated(false);
120
            }
121
 
122
            private boolean confirm() {
73 ilm 123
                return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(DefaultElementSQLObject.this, getTM().trM("elementSQLObject.delete", "element", getSQLChild().getElement().getName()),
124
                        getTM().trA("sqlElement.confirmDelete"), JOptionPane.YES_NO_OPTION);
17 ilm 125
            }
126
        });
73 ilm 127
        this.createBtn = new JButton(EditFrame.getCreateMessage(this.getSQLChild().getElement()));
25 ilm 128
        // false leaves only a line for the button under Plastic3DLookAndFeel
129
        this.createBtn.setOpaque(isPlastic);
17 ilm 130
        this.createBtn.addActionListener(new ActionListener() {
25 ilm 131
            @Override
17 ilm 132
            public void actionPerformed(ActionEvent e) {
133
                setCreated(true);
134
            }
135
        });
136
 
137
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
138
    }
139
 
25 ilm 140
    @Override
17 ilm 141
    protected final void setCreatePanel() {
142
        if (this.editP != null)
143
            this.editP.setVisible(false);
144
        this.getCreatePanel().setVisible(true);
83 ilm 145
        this.createBtn.requestFocusInWindow();
17 ilm 146
        // don't call setCurrentID() otherwise, when updating we won't know
147
        // what observation to archive
148
        this.revalidate();
149
        this.repaint();
150
    }
151
 
152
    private final JPanel getCreatePanel() {
153
        if (this.createP == null) {
154
            this.createP = new JPanel();
155
            this.createP.setOpaque(false);
156
            this.createP.setLayout(new GridLayout());
157
            this.createP.add(this.createBtn);
158
            this.add(this.createP);
159
        }
160
        return this.createP;
161
    }
162
 
25 ilm 163
    @Override
17 ilm 164
    protected final void setEditPanel() {
165
        if (this.createP != null)
166
            this.createP.setVisible(false);
167
        this.getEditPanel().setVisible(true);
83 ilm 168
        this.getEditPanel().requestFocusInWindow();
17 ilm 169
        // toujours afficher un composant vierge à la création
170
        // do it after add() as it calls SQLRowView.activate() which refresh comp from the db
171
        // possibly resurecting a deleted row
172
        this.getSQLChild().resetValue();
173
 
174
        this.revalidate();
175
        // Swing ne le fait pas de lui même
176
        // => on sélectionne une longue observation, puis une courte
177
        // la fin de la longue reste affichée à l'écran, pareil pour les DS
178
        this.repaint();
179
        this.expand(true);
180
    }
181
 
182
    private final JPanel getEditPanel() {
183
        if (this.editP == null) {
184
            this.editP = new JPanel();
185
            this.editP.setLayout(new GridBagLayout());
186
            this.editP.setOpaque(false);
187
            GridBagConstraints c = new GridBagConstraints();
188
            c.gridx = 0;
189
            c.gridy = 0;
190
 
191
            c.weightx = 0.001;
192
            c.weighty = 0;
193
            // en haut a gauche pour que quand on cache, le bouton reste à la même place
194
            c.anchor = GridBagConstraints.FIRST_LINE_START;
195
            this.editP.add(this.expandBtn, c);
196
            c.gridy++;
197
            this.editP.add(this.supprBtn, c);
198
 
199
            c.gridx = 2;
200
            c.gridy = 0;
201
            c.weightx = 1;
202
            c.weighty = 1;
203
            c.gridheight = GridBagConstraints.REMAINDER;
204
            c.fill = GridBagConstraints.BOTH;
205
            this.getSQLChild().uiInit();
206
            this.editP.add(this.getSQLChild(), c);
207
 
208
            if (this.isSeparatorVisible) {
209
                c.gridx = 1;
210
                c.gridy = 0;
211
                c.weightx = 0;
212
                c.insets = new Insets(2, 2, 2, 2);
213
                this.separator = new JSeparator(SwingConstants.VERTICAL);
214
                this.editP.add(this.separator, c);
215
            }
216
            this.add(this.editP);
217
        }
218
        return this.editP;
219
    }
220
 
25 ilm 221
    @Override
17 ilm 222
    protected void compChanged() {
21 ilm 223
        this.expandBtn.setEnabled(this.getCurrentID() != SQLRow.NONEXISTANT_ID && this.getValidState().isValid());
17 ilm 224
    }
225
 
83 ilm 226
    /**
227
     * Whether the edit panel is expanded.
228
     *
229
     * @return <code>true</code> if the edit panel is expanded, <code>false</code> if not and
230
     *         <code>null</code> if not {@link #isCreated()}.
231
     */
232
    private final Boolean isExpanded() {
233
        return this.isCreated() ? this.expanded : null;
17 ilm 234
    }
235
 
236
    private void expand(boolean b) {
237
        if (!this.isCreated()) {
238
            throw new IllegalStateException("cannot expand if not created");
239
        }
240
 
83 ilm 241
        this.expanded = b;
242
        this.updateBtns();
17 ilm 243
        this.getSQLChild().setVisible(b);
244
        this.validate();
245
    }
246
 
247
    private void toggleExpand() {
248
        this.expand(!this.isExpanded());
249
    }
250
 
25 ilm 251
    @Override
17 ilm 252
    public void setEditable(boolean enabled) {
253
        super.setEditable(enabled);
254
        this.createBtn.setEnabled(enabled);
255
        this.supprBtn.setEnabled(enabled);
256
    }
257
 
258
}