OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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