OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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