OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 package org.openconcerto.sql.view.list;
15
 
16
import org.openconcerto.sql.element.SQLElement;
73 ilm 17
import org.openconcerto.sql.model.SQLField;
18
import org.openconcerto.sql.model.SQLRowValues;
132 ilm 19
import org.openconcerto.sql.model.SQLSelect;
17 ilm 20
import org.openconcerto.sql.model.Where;
21
import org.openconcerto.sql.request.ComboSQLRequest;
22
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
61 ilm 23
import org.openconcerto.sql.view.IListFrame;
24
import org.openconcerto.sql.view.ListeAddPanel;
25
import org.openconcerto.ui.FrameUtil;
26
import org.openconcerto.ui.list.selection.BaseListStateModel;
132 ilm 27
import org.openconcerto.utils.cc.ITransformer;
156 ilm 28
import org.openconcerto.utils.convertor.NumberConvertor;
17 ilm 29
 
19 ilm 30
import java.awt.Color;
17 ilm 31
import java.awt.Component;
61 ilm 32
import java.awt.event.ActionEvent;
17 ilm 33
import java.awt.event.MouseEvent;
34
import java.beans.PropertyChangeEvent;
35
import java.beans.PropertyChangeListener;
36
import java.util.EventObject;
174 ilm 37
import java.util.List;
17 ilm 38
 
61 ilm 39
import javax.swing.AbstractAction;
17 ilm 40
import javax.swing.AbstractCellEditor;
41
import javax.swing.Action;
42
import javax.swing.JTable;
19 ilm 43
import javax.swing.border.EmptyBorder;
44
import javax.swing.border.LineBorder;
17 ilm 45
import javax.swing.table.TableCellEditor;
46
 
47
public class SQLTextComboTableCellEditor extends AbstractCellEditor implements TableCellEditor {
48
 
49
    private SQLRequestComboBox comboBox;
73 ilm 50
 
51
    private Where w;
17 ilm 52
    // Stock Value of Combo to fix problem with undefined
156 ilm 53
    Integer val = 1;
17 ilm 54
 
55
    boolean addUndefined;
56
 
61 ilm 57
    private IListFrame listFrame = null;
58
 
17 ilm 59
    public SQLTextComboTableCellEditor(final SQLElement elt, final boolean addUndefined) {
174 ilm 60
        this(elt, addUndefined, false,null);
61 ilm 61
    }
62
 
63
    /**
64
     *
65
     * @param elt Element à afficher dans la combo
66
     * @param addUndefined ajout de l'indéfini
67
     * @param chooseInListe possibilité de choisir via une IListe
68
     */
69
    public SQLTextComboTableCellEditor(final SQLElement elt, final boolean addUndefined, boolean chooseInListe) {
174 ilm 70
        this(elt, addUndefined, chooseInListe, null);
71
    }
17 ilm 72
 
174 ilm 73
    /**
74
     *
75
     * @param elt Element à afficher dans la combo
76
     * @param addUndefined ajout de l'indéfini
77
     * @param chooseInListe possibilité de choisir via une IListe
78
     */
79
    public SQLTextComboTableCellEditor(final SQLElement elt, final boolean addUndefined, boolean chooseInListe, List<String> fieldsInCombo) {
80
         super();
81
 
17 ilm 82
        this.addUndefined = addUndefined;
83
 
84
        this.comboBox = new SQLRequestComboBox(addUndefined);
61 ilm 85
 
19 ilm 86
        // Mimic JTable.GenericEditor behavior
87
        this.comboBox.getTextComp().setBorder(new EmptyBorder(0, 0, 0, 18));
88
        this.comboBox.setBorder(new LineBorder(Color.black));
73 ilm 89
        this.comboBox.getPulseComponents().iterator().next().setBorder(null);
19 ilm 90
 
174 ilm 91
        ComboSQLRequest c = fieldsInCombo == null || fieldsInCombo.isEmpty() ? elt.getComboRequest(true) : elt.createComboRequest(fieldsInCombo, null);
17 ilm 92
        this.comboBox.uiInit(c);
61 ilm 93
 
94
        if (chooseInListe) {
95
            this.comboBox.getActions().add(0, new AbstractAction("Tout afficher") {
96
                @Override
97
                public void actionPerformed(ActionEvent e) {
98
 
99
                    if (SQLTextComboTableCellEditor.this.listFrame == null) {
100
                        SQLTextComboTableCellEditor.this.listFrame = new IListFrame(new ListeAddPanel(elt));
101
 
102
                        SQLTextComboTableCellEditor.this.listFrame.getPanel().getListe().getSelection().addPropertyChangeListener("userSelectedID", new PropertyChangeListener() {
103
                            @Override
104
                            public void propertyChange(PropertyChangeEvent evt) {
105
                                final int newID = ((Number) evt.getNewValue()).intValue();
106
                                SQLTextComboTableCellEditor.this.comboBox.setValue(newID == BaseListStateModel.INVALID_ID ? null : newID);
107
                            }
108
                        });
109
                        SQLTextComboTableCellEditor.this.listFrame.getPanel().getListe().selectID(SQLTextComboTableCellEditor.this.comboBox.getSelectedId());
110
                    }
111
                    FrameUtil.show(SQLTextComboTableCellEditor.this.listFrame);
112
                }
113
            });
114
        }
17 ilm 115
    }
116
 
117
    public SQLRequestComboBox getCombo() {
118
        return this.comboBox;
119
    }
120
 
121
    public void addAction(Action a) {
122
        this.comboBox.getActions().add(a);
123
    }
124
 
125
    public boolean isCellEditable(EventObject e) {
126
 
127
        if (e instanceof MouseEvent) {
128
            return ((MouseEvent) e).getClickCount() >= 2;
129
        }
130
        return super.isCellEditable(e);
131
    }
132
 
133
    public Object getCellEditorValue() {
134
        return this.val;
135
    }
136
 
137
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
156 ilm 138
 
139
        this.val = (Integer) value;
140
        this.comboBox.setValue(this.val);
141
 
17 ilm 142
        this.comboBox.grabFocus();
143
 
142 ilm 144
        this.comboBox.addModelListener("wantedID", new PropertyChangeListener() {
17 ilm 145
            @Override
142 ilm 146
            public void propertyChange(final PropertyChangeEvent evt) {
156 ilm 147
                SQLTextComboTableCellEditor.this.val = NumberConvertor.convertExact((Number) evt.getNewValue(), Integer.class);
17 ilm 148
            }
149
        });
156 ilm 150
 
73 ilm 151
        // Filtre sur une valeur specifique
152
        if (this.fieldWhere != null && table instanceof RowValuesTable) {
153
            RowValuesTable rowVals = (RowValuesTable) table;
154
            SQLRowValues rowValues = rowVals.getRowValuesTableModel().getRowValuesAt(row);
155
            if (rowValues.isForeignEmpty(this.fieldWhere.getName())) {
156
 
157
                if (this.w != null) {
158
                    this.comboBox.getRequest().setWhere(this.w);
159
                } else {
160
                    this.comboBox.getRequest().setWhere(null);
161
                }
162
            } else {
163
                final Where w2 = new Where(this.fieldWhere, "=", rowValues.getForeign(this.fieldWhere.getName()).getID());
164
                if (this.w != null) {
165
                    this.comboBox.getRequest().setWhere(this.w.and(w2));
166
                } else {
167
                    this.comboBox.getRequest().setWhere(w2);
168
                }
169
            }
170
        }
17 ilm 171
        return this.comboBox;
73 ilm 172
 
17 ilm 173
    }
174
 
175
    public int getComboSelectedId() {
156 ilm 176
        return SQLTextComboTableCellEditor.this.comboBox.getWantedID();
17 ilm 177
    }
178
 
73 ilm 179
    private SQLField fieldWhere;
180
 
181
    public void setDynamicWhere(SQLField field) {
182
        this.fieldWhere = field;
183
    }
184
 
132 ilm 185
    public void setSelectTransformer(ITransformer<SQLSelect, SQLSelect> selTrans) {
186
        this.comboBox.getRequest().setSelectTransf(selTrans);
187
    }
188
 
17 ilm 189
    public void setWhere(Where w) {
73 ilm 190
        this.w = w;
17 ilm 191
        this.comboBox.getRequest().setWhere(w);
192
    }
193
 
194
    public void addSelectionListener(PropertyChangeListener l) {
195
        this.comboBox.addValueListener(l);
196
    }
197
}