OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
83 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.sqlobject;
15
 
16
import org.openconcerto.sql.model.SQLField;
17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRowAccessor;
180 ilm 19
import org.openconcerto.sql.model.SQLRowValues;
20
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
83 ilm 21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.Where;
24
import org.openconcerto.sql.request.MultipleSQLSelectExecutor;
25
import org.openconcerto.ui.component.text.TextComponent;
26
import org.openconcerto.utils.CompareUtils;
27
import org.openconcerto.utils.OrderedSet;
132 ilm 28
import org.openconcerto.utils.cc.ITransformer;
83 ilm 29
import org.openconcerto.utils.checks.MutableValueObject;
30
import org.openconcerto.utils.model.DefaultIMutableListModel;
31
import org.openconcerto.utils.text.DocumentFilterList;
32
import org.openconcerto.utils.text.DocumentFilterList.FilterType;
33
import org.openconcerto.utils.text.LimitedSizeDocumentFilter;
34
 
35
import java.awt.Component;
36
import java.awt.GridLayout;
37
import java.awt.event.ComponentEvent;
38
import java.awt.event.ComponentListener;
39
import java.awt.event.FocusEvent;
40
import java.awt.event.FocusListener;
41
import java.awt.event.KeyEvent;
42
import java.awt.event.KeyListener;
43
import java.beans.PropertyChangeListener;
44
import java.beans.PropertyChangeSupport;
45
import java.sql.SQLException;
46
import java.util.ArrayList;
47
import java.util.Iterator;
48
import java.util.List;
49
import java.util.Stack;
50
import java.util.Vector;
51
 
52
import javax.swing.JComponent;
53
import javax.swing.JPanel;
54
import javax.swing.JTextField;
55
import javax.swing.SwingUtilities;
144 ilm 56
import javax.swing.SwingWorker;
83 ilm 57
import javax.swing.event.DocumentEvent;
58
import javax.swing.event.DocumentListener;
59
import javax.swing.text.AbstractDocument;
60
import javax.swing.text.DocumentFilter;
61
import javax.swing.text.JTextComponent;
62
 
63
public class ITextArticleWithCompletion extends JPanel implements DocumentListener, TextComponent, MutableValueObject<String>, IComboSelectionItemListener {
64
 
65
    public static int SQL_RESULT_LIMIT = 50;
66
 
67
    public static final int MODE_STARTWITH = 1;
68
    public static final int MODE_CONTAINS = 2;
69
 
70
    private JTextComponent text;
71
 
72
    private DefaultIMutableListModel<IComboSelectionItem> model = new DefaultIMutableListModel<IComboSelectionItem>();
73
 
74
    private boolean completionEnabled = true;
75
 
76
    private SQLRowAccessor selectedRow = null;
77
 
78
    private boolean selectAuto = true;
79
 
80
    protected ITextWithCompletionPopUp popup;
81
 
82
    OrderedSet<SelectionRowListener> listeners = new OrderedSet<SelectionRowListener>();
83
    Component popupInvoker;
84
 
85
    private boolean isLoading = false;
86
    private SQLRowAccessor rowToSelect = null;
87
 
88
    private String fillWith = "CODE";
89
    private final PropertyChangeSupport supp;
90
 
91
    private final SQLTable tableArticle, tableArticleFournisseur;
92
 
93
    // Asynchronous filling
94
    private Thread searchThread;
95
    private int autoCheckDelay = 1000;
96
    private boolean disposed = false;
97
    private Stack<String> searchStack = new Stack<String>();
98
    private boolean autoselectIfMatch;
99
    private static final int PAUSE_MS = 150;
100
 
101
    public ITextArticleWithCompletion(SQLTable tableArticle, SQLTable tableARticleFournisseur) {
102
        this.tableArticle = tableArticle;
103
        this.tableArticleFournisseur = tableARticleFournisseur;
104
        this.supp = new PropertyChangeSupport(this);
105
        this.popup = new ITextWithCompletionPopUp(this.model, this);
106
        this.text = new JTextField();
107
        this.setLayout(new GridLayout(1, 1));
108
        this.add(this.text);
109
        setTextEditor(this.text);
110
        setPopupInvoker(this);
111
 
112
        //
113
        disposed = false;
114
        searchThread = new Thread() {
115
            public void run() {
116
                while (!disposed) {
117
                    if (autoCheckDelay == 0) {
118
                        autoCheckDelay = -1;
119
                        SwingUtilities.invokeLater(new Runnable() {
120
                            @Override
121
                            public void run() {
122
                                loadAutoCompletion();
123
                            }
124
                        });
125
 
126
                    } else if (autoCheckDelay > 0) {
127
                        autoCheckDelay -= PAUSE_MS;
128
                    }
129
                    try {
130
                        Thread.sleep(PAUSE_MS);
131
                    } catch (InterruptedException e) {
132
                        e.printStackTrace();
133
                    }
134
                }
135
 
136
            };
137
        };
138
        searchThread.setName("ITextArticleWithCompletion thread");
139
        searchThread.setPriority(Thread.MIN_PRIORITY);
140
        searchThread.setDaemon(true);
141
        searchThread.start();
142
 
143
    }
144
 
145
    public void setPopupListEnabled(boolean b) {
146
        this.popup.setListEnabled(b);
147
    }
148
 
94 ilm 149
    private Where whereAdditionnal;
132 ilm 150
    private ITransformer<SQLSelect, SQLSelect> selTransformer;
94 ilm 151
 
152
    public void setWhere(Where w) {
153
        this.whereAdditionnal = w;
154
    }
155
 
132 ilm 156
    public void setSelectTransformer(ITransformer<SQLSelect, SQLSelect> selTransformer) {
157
        this.selTransformer = selTransformer;
158
    }
159
 
83 ilm 160
    public void setTextEditor(final JTextComponent atext) {
161
        if (atext == null) {
162
            throw new IllegalArgumentException("null textEditor");
163
        }
164
        this.text = atext;
165
        atext.getDocument().addDocumentListener(this);
166
        atext.addKeyListener(new KeyListener() {
167
 
168
            private boolean consume;
169
 
170
            public void keyPressed(KeyEvent e) {
171
                if (e.getKeyCode() == KeyEvent.VK_TAB) {
172
                    // Complete si exactement la valeur souhaitée
173
                    updateAutoCompletion(true);
174
                    e.consume();
94 ilm 175
 
83 ilm 176
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
177
                    if (ITextArticleWithCompletion.this.popup.isShowing()) {
178
                        ITextArticleWithCompletion.this.popup.selectNext();
179
                        e.consume();
180
                    } else {
181
                        if (getSelectedRow() == null) {
182
                            // updateAutoCompletion();
183
                            showPopup();
184
                        }
185
                    }
186
 
187
                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
188
                    if (ITextArticleWithCompletion.this.popup.isShowing()) {
189
                        ITextArticleWithCompletion.this.popup.selectPrevious();
190
                        e.consume();
191
                    }
192
 
193
                } else if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_TAB) {
194
                    if (ITextArticleWithCompletion.this.popup.isShowing()) {
195
                        ITextArticleWithCompletion.this.popup.validateSelection();
196
                        e.consume();
197
                    } else {
198
                        autoselectIfMatch = true;
199
                        e.consume();
200
                    }
201
                } else if (e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
202
                    if (ITextArticleWithCompletion.this.popup.isShowing()) {
203
                        ITextArticleWithCompletion.this.popup.selectNextPage();
204
                        e.consume();
205
                    }
206
                } else if (e.getKeyCode() == KeyEvent.VK_PAGE_UP) {
207
                    if (ITextArticleWithCompletion.this.popup.isShowing()) {
208
                        ITextArticleWithCompletion.this.popup.selectPreviousPage();
209
                        e.consume();
210
                    }
211
                } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
212
                    if (ITextArticleWithCompletion.this.popup.isShowing()) {
213
                        hidePopup();
214
                    }
215
 
216
                }
217
 
218
                // else {
219
                // if (e.getKeyCode() != KeyEvent.VK_RIGHT && e.getKeyCode() !=
220
                // KeyEvent.VK_LEFT) {
221
                // fireSelectionId(-1);
222
                // }
223
                // }
224
                // Evite les bips
225
                if (ITextArticleWithCompletion.this.text.getDocument().getLength() == 0 && (e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_BACK_SPACE)) {
226
                    System.err.println("consume");
227
                    this.consume = true;
228
                    e.consume();
229
                }
230
 
231
            }
232
 
233
            public void keyReleased(KeyEvent e) {
234
            }
235
 
236
            public void keyTyped(KeyEvent e) {
237
                // Evite les bips
238
                if (this.consume) {
239
                    e.consume();
240
                    this.consume = false;
241
                }
242
            }
243
        });
244
        this.addComponentListener(new ComponentListener() {
245
            public void componentHidden(ComponentEvent e) {
246
            }
247
 
248
            public void componentMoved(ComponentEvent e) {
249
            }
250
 
251
            public void componentResized(ComponentEvent e) {
252
                // ajuste la taille min de la popup
253
                ITextArticleWithCompletion.this.popup.setMinWith(atext.getBounds().width);
254
            }
255
 
256
            public void componentShown(ComponentEvent e) {
257
            }
258
        });
259
        atext.addFocusListener(new FocusListener() {
260
            @Override
261
            public void focusGained(FocusEvent e) {
262
 
263
            }
264
 
265
            @Override
266
            public void focusLost(FocusEvent e) {
267
                hidePopup();
268
            }
269
        });
270
    }
271
 
272
    /**
273
     * Retourne une liste de IComboSelectionItem, qui sont les selections possibles pour le text
274
     * passé
275
     *
276
     * @throws SQLException
277
     */
278
    List<IComboSelectionItem> getPossibleValues(String aText) throws SQLException {
279
        List<IComboSelectionItem> result = new Vector<IComboSelectionItem>();
280
        if (aText.isEmpty()) {
281
            return result;
282
        }
283
        aText = aText.trim();
284
 
285
        if (aText.length() > 0) {
286
 
287
            List<SQLSelect> listSel = new ArrayList<SQLSelect>();
288
            // CODE ARTICLE = aText
289
            SQLSelect selMatchingCode = new SQLSelect();
290
            // selMatchingCode.addSelectStar(this.tableArticle);
291
            selMatchingCode.addSelect(this.tableArticle.getKey());
292
            selMatchingCode.addSelect(this.tableArticle.getField("CODE"));
293
            selMatchingCode.addSelect(this.tableArticle.getField("NOM"));
294
            selMatchingCode.addSelect(this.tableArticle.getField("CODE_BARRE"));
295
            Where wMatchingCode = new Where(this.tableArticle.getField("CODE"), "=", aText);
296
            wMatchingCode = wMatchingCode.or(new Where(this.tableArticle.getField("NOM"), "=", aText));
297
            wMatchingCode = wMatchingCode.or(new Where(this.tableArticle.getField("CODE_BARRE"), "=", aText));
94 ilm 298
            if (this.whereAdditionnal != null) {
299
                wMatchingCode = wMatchingCode.and(this.whereAdditionnal);
300
            }
83 ilm 301
            selMatchingCode.setWhere(wMatchingCode);
132 ilm 302
            if (this.selTransformer != null) {
303
                selMatchingCode = this.selTransformer.transformChecked(selMatchingCode);
304
            }
83 ilm 305
            listSel.add(selMatchingCode);
306
 
307
            // CODE ARTICLE LIKE %aText% with limit
308
            SQLSelect selContains = new SQLSelect();
309
            // selContains.addSelectStar(this.tableArticle);
310
            selContains.addSelect(this.tableArticle.getKey());
311
            selContains.addSelect(this.tableArticle.getField("CODE"));
312
            selContains.addSelect(this.tableArticle.getField("NOM"));
313
            selContains.addSelect(this.tableArticle.getField("CODE_BARRE"));
314
            Where wContains = new Where(this.tableArticle.getField("CODE"), "LIKE", "%" + aText + "%");
315
            wContains = wContains.or(new Where(this.tableArticle.getField("NOM"), "LIKE", "%" + aText + "%"));
316
            wContains = wContains.or(new Where(this.tableArticle.getField("CODE_BARRE"), "LIKE", "%" + aText + "%"));
94 ilm 317
            if (this.whereAdditionnal != null) {
318
                wContains = wContains.and(this.whereAdditionnal);
319
            }
132 ilm 320
 
83 ilm 321
            selContains.setWhere(wContains.and(wMatchingCode.not()));
132 ilm 322
            selContains.setExcludeUndefined(false, this.tableArticle.getForeignTable("ID_STOCK"));
83 ilm 323
            selContains.setLimit(SQL_RESULT_LIMIT);
132 ilm 324
            if (this.selTransformer != null) {
325
                selContains = this.selTransformer.transformChecked(selContains);
326
            }
83 ilm 327
            listSel.add(selContains);
328
 
329
            // CODE ARTICLE = aText
94 ilm 330
            final Where wNotSync = new Where(this.tableArticleFournisseur.getField("ID_ARTICLE"), "IS", (Object) null)
331
                    .or(new Where(this.tableArticleFournisseur.getField("ID_ARTICLE"), "=", this.tableArticleFournisseur.getUndefinedID()));
83 ilm 332
 
333
            SQLSelect selMatchingCodeF = new SQLSelect();
334
            // selMatchingCodeF.addSelectStar(this.tableArticleFournisseur);
335
            selMatchingCodeF.addSelect(this.tableArticleFournisseur.getKey());
336
            selMatchingCodeF.addSelect(this.tableArticleFournisseur.getField("CODE"));
337
            selMatchingCodeF.addSelect(this.tableArticleFournisseur.getField("NOM"));
338
            selMatchingCodeF.addSelect(this.tableArticleFournisseur.getField("CODE_BARRE"));
339
            Where wMatchingCodeF = new Where(this.tableArticleFournisseur.getField("CODE"), "=", aText);
340
            wMatchingCodeF = wMatchingCodeF.or(new Where(this.tableArticleFournisseur.getField("CODE_BARRE"), "=", aText));
341
            wMatchingCodeF = wMatchingCodeF.or(new Where(this.tableArticleFournisseur.getField("NOM"), "=", aText));
342
            selMatchingCodeF.setWhere(wMatchingCodeF.and(wNotSync));
343
            listSel.add(selMatchingCodeF);
344
 
345
            // CODE ARTICLE_FOURNISSEUR LIKE %aText% with limit
346
            SQLSelect selContainsCodeF = new SQLSelect();
347
            // selContainsCodeF.addSelectStar(this.tableArticleFournisseur);
348
            selContainsCodeF.addSelect(this.tableArticleFournisseur.getKey());
349
            selContainsCodeF.addSelect(this.tableArticleFournisseur.getField("CODE"));
350
            selContainsCodeF.addSelect(this.tableArticleFournisseur.getField("NOM"));
351
            selContainsCodeF.addSelect(this.tableArticleFournisseur.getField("CODE_BARRE"));
352
            Where wContainsCodeF = new Where(this.tableArticleFournisseur.getField("CODE"), "LIKE", "%" + aText + "%");
353
            wContainsCodeF = wContainsCodeF.or(new Where(this.tableArticleFournisseur.getField("CODE_BARRE"), "LIKE", "%" + aText + "%"));
354
            wContainsCodeF = wContainsCodeF.or(new Where(this.tableArticleFournisseur.getField("NOM"), "LIKE", "%" + aText + "%"));
355
            selContainsCodeF.setWhere(wContainsCodeF.and(wMatchingCodeF.not()).and(wNotSync));
356
            selContainsCodeF.setLimit(SQL_RESULT_LIMIT);
357
 
358
            listSel.add(selContainsCodeF);
359
 
360
            MultipleSQLSelectExecutor mult = new MultipleSQLSelectExecutor(this.tableArticle.getDBSystemRoot(), listSel);
361
 
180 ilm 362
            List<List<? extends SQLRowAccessor>> resultList = new ArrayList<>();
363
            resultList.addAll(mult.execute());
83 ilm 364
 
180 ilm 365
            // Recherche dans les codes fournisseurs
366
            SQLTable tableCodeArt = this.tableArticle.getDBRoot().getTable("CODE_FOURNISSEUR");
367
            SQLRowValues rowValsCodeF = new SQLRowValues(tableCodeArt);
368
            rowValsCodeF.putNulls("CODE");
369
            rowValsCodeF.putRowValues("ID_ARTICLE").putNulls(this.tableArticle.getFieldsName());
83 ilm 370
 
180 ilm 371
            final String codeText = aText;
372
            SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsCodeF);
373
            fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
374
                @Override
375
                public SQLSelect transformChecked(SQLSelect input) {
83 ilm 376
 
180 ilm 377
                    Where wCodeFContains = new Where(tableCodeArt.getField("CODE"), "LIKE", "%" + codeText + "%");
378
                    input.setWhere(wCodeFContains);
379
                    input.setLimit(SQL_RESULT_LIMIT);
380
                    return input;
381
                }
382
            });
383
            List<SQLRowValues> resultCodeF = fetcher.fetch();
384
            resultList.add(2, resultCodeF);
385
 
386
            for (List<? extends SQLRowAccessor> list : resultList) {
387
 
388
                for (SQLRowAccessor sqlRow : list) {
389
 
83 ilm 390
                    StringBuffer buf = new StringBuffer();
180 ilm 391
                    if (sqlRow.getTable().getName().equals("CODE_FOURNISSEUR")) {
392
                        SQLRowAccessor rArt = sqlRow.getForeign("ID_ARTICLE");
393
                        buf.append(sqlRow.getString("CODE") + " -- ");
394
                        buf.append(rArt.getString("CODE") + " -- ");
395
                        buf.append(rArt.getString("NOM"));
396
                        result.add(new IComboSelectionItem(rArt, buf.toString()));
397
                    } else {
398
                        if (sqlRow.getString("CODE_BARRE") != null && sqlRow.getString("CODE_BARRE").trim().length() > 0) {
399
                            buf.append(sqlRow.getString("CODE_BARRE") + " -- ");
400
                        }
401
                        buf.append(sqlRow.getString("CODE") + " -- ");
402
                        buf.append(sqlRow.getString("NOM"));
403
                        result.add(new IComboSelectionItem(sqlRow, buf.toString()));
83 ilm 404
                    }
405
                }
406
            }
407
 
408
        }
409
 
410
        return result;
411
    }
412
 
413
    private void updateAutoCompletion(boolean autoselectIfMatch) {
414
        this.autoselectIfMatch = autoselectIfMatch;
415
        this.autoCheckDelay = PAUSE_MS * 2;
416
        synchronized (searchStack) {
417
            this.searchStack.push(this.text.getText().trim());
418
        }
419
    }
420
 
421
    private void loadAutoCompletion() {
422
        if (!this.isCompletionEnabled() || this.isLoading) {
423
            return;
424
        }
425
        final String t;
426
        synchronized (searchStack) {
427
            if (this.searchStack.isEmpty()) {
428
                return;
429
            }
430
            t = this.searchStack.pop();
431
            this.searchStack.clear();
432
        }
433
 
144 ilm 434
        final SwingWorker<List<IComboSelectionItem>, Object> worker = new SwingWorker<List<IComboSelectionItem>, Object>() {
83 ilm 435
 
436
            @Override
437
            protected List<IComboSelectionItem> doInBackground() throws Exception {
438
                List<IComboSelectionItem> l = getPossibleValues(t); // Liste de IComboSelection
439
                return l;
440
            }
441
 
442
            @Override
443
            protected void done() {
444
                List<IComboSelectionItem> l;
445
                try {
446
                    l = get();
447
                } catch (Exception e) {
448
                    l = new ArrayList<IComboSelectionItem>(0);
449
                    e.printStackTrace();
450
                }
94 ilm 451
 
83 ilm 452
                // On cache la popup si le nombre de ligne change afin que sa taille soit correcte
453
                if (l.size() != model.getSize() && l.size() <= ITextWithCompletionPopUp.MAXROW) {
454
                    hidePopup();
455
                }
456
                // on vide le model
457
                model.removeAllElements();
458
                model.addAll(l);
459
 
460
                if (l.size() > 0) {
461
                    showPopup();
462
                } else {
463
                    hidePopup();
464
                }
465
                SQLRowAccessor newRow = selectedRow;
94 ilm 466
                IComboSelectionItem newSelectedItem = null;
83 ilm 467
                boolean found = false;
468
                for (Iterator<IComboSelectionItem> iter = l.iterator(); iter.hasNext();) {
469
                    IComboSelectionItem element = iter.next();
94 ilm 470
 
471
                    if ((element.getRow().getString("CODE_BARRE").toLowerCase().equals(t.toLowerCase()) || element.getRow().getString("CODE").toLowerCase().equals(t.toLowerCase()))
472
                            && autoselectIfMatch) {
83 ilm 473
                        newRow = element.getRow();
94 ilm 474
                        newSelectedItem = element;
83 ilm 475
                        hidePopup();
476
                        found = true;
477
                        break;
478
                    }
479
                }
480
                if (selectAuto && found && !CompareUtils.equals(newRow, selectedRow)) {
94 ilm 481
                    final IComboSelectionItem selectedItem = newSelectedItem;
83 ilm 482
                    SwingUtilities.invokeLater(new Runnable() {
483
                        public void run() {
94 ilm 484
                            itemSelected(selectedItem);
83 ilm 485
                        }
486
                    });
487
                }
488
                if (!found) {
489
                    selectedRow = null;
94 ilm 490
                    itemSelected(null);
83 ilm 491
                }
94 ilm 492
 
83 ilm 493
            }
494
        };
495
        worker.execute();
496
 
497
    }
498
 
499
    public synchronized void hidePopup() {
500
        this.popup.setVisible(false);
501
    }
502
 
503
    private synchronized void showPopup() {
504
        if (this.model.getSize() > 0) {
505
            if (this.popupInvoker.isShowing())
506
                this.popup.show(this.popupInvoker, 0, this.text.getBounds().height);
507
        }
508
    }
509
 
510
    public void changedUpdate(DocumentEvent e) {
511
        updateAutoCompletion(false);
512
        this.supp.firePropertyChange("value", null, this.getText());
513
    }
514
 
515
    public void insertUpdate(DocumentEvent e) {
516
        updateAutoCompletion(false);
517
        this.supp.firePropertyChange("value", null, this.getText());
518
    }
519
 
520
    public void removeUpdate(DocumentEvent e) {
521
        updateAutoCompletion(false);
522
        this.supp.firePropertyChange("value", null, this.getText());
523
    }
524
 
525
    public SQLRowAccessor getSelectedRow() {
526
        return this.selectedRow;
527
    }
528
 
529
    public void setSelectedRow(SQLRowAccessor row) {
530
        this.selectedRow = row;
531
    }
532
 
533
    private void clearText() {
534
        setText("");
535
    }
536
 
537
    public void setEditable(boolean b) {
538
        this.text.setEditable(b);
539
    }
540
 
541
    public void setFillWithField(String s) {
542
        this.fillWith = s;
543
    }
544
 
545
    public SQLField getFillWithField() {
546
        return this.tableArticle.getField(fillWith);
547
    }
548
 
549
    public void selectItem(IComboSelectionItem item) {
550
        if (!SwingUtilities.isEventDispatchThread()) {
551
            throw new IllegalStateException("Not in Swing!");
552
        }
553
        if (item != null) {
554
            if (this.fillWith != null) {
555
                // FIXME SQL request in Swing
556
                SQLRowAccessor row = item.getRow();
557
                this.setText(row.getObject(this.fillWith).toString());
558
            } else {
559
                this.setText(item.getLabel());
560
            }
561
        } else {
562
            this.clearText();
563
        }
564
        hidePopup();
565
    }
566
 
567
    public void setText(final String label) {
568
        if (!SwingUtilities.isEventDispatchThread()) {
569
            throw new IllegalStateException("Not in Swing!");
570
        }
571
        setCompletionEnabled(false);
572
        this.text.setText(label);
573
        if (label != null) {
574
            this.text.setCaretPosition(label.length());
575
        }
576
        this.text.repaint();
577
        setCompletionEnabled(true);
578
    }
579
 
580
    // Gestion des listeners de selection d'id
581
    public void addSelectionListener(SelectionRowListener l) {
582
        this.listeners.add(l);
583
    }
584
 
585
    public void removeSelectionListener(SelectionRowListener l) {
586
        this.listeners.remove(l);
587
    }
588
 
589
    private boolean isDispatching = false;
590
 
591
    private void fireSelectionRow(SQLRowAccessor row) {
592
        if (!this.isDispatching) {
94 ilm 593
 
83 ilm 594
            this.isDispatching = true;
595
            for (Iterator<SelectionRowListener> iter = this.listeners.iterator(); iter.hasNext();) {
596
                SelectionRowListener element = iter.next();
597
                element.rowSelected(row, this);
598
            }
599
            this.isDispatching = false;
600
        }
601
    }
602
 
603
    /**
604
     * @return Returns the completionEnabled.
605
     */
606
    boolean isCompletionEnabled() {
607
        return this.completionEnabled;
608
    }
609
 
610
    /**
611
     * @param completionEnabled The completionEnabled to set.
612
     */
613
    void setCompletionEnabled(boolean completionEnabled) {
614
        this.completionEnabled = completionEnabled;
615
    }
616
 
617
    public Object getText() {
618
        return this.text.getText();
619
    }
620
 
621
    /**
622
     * @param popupInvoker The popupInvoker to set.
623
     */
624
    public void setPopupInvoker(Component popupInvoker) {
625
        this.popupInvoker = popupInvoker;
626
    }
627
 
628
    public JTextComponent getTextComp() {
629
        return this.text;
630
    }
631
 
632
    public JComponent getComp() {
633
        return this;
634
    }
635
 
636
    public void setSelectionAutoEnabled(boolean b) {
637
        this.selectAuto = b;
638
    }
639
 
640
    public void setLimitedSize(int nbChar) {
641
        // rm previous ones
642
        final DocumentFilterList dfl = DocumentFilterList.get((AbstractDocument) this.text.getDocument());
643
        final Iterator<DocumentFilter> iter = dfl.getFilters().iterator();
644
        while (iter.hasNext()) {
645
            final DocumentFilter df = iter.next();
646
            if (df instanceof LimitedSizeDocumentFilter)
647
                iter.remove();
648
        }
649
        // add the new one
650
        DocumentFilterList.add((AbstractDocument) this.text.getDocument(), new LimitedSizeDocumentFilter(nbChar), FilterType.SIMPLE_FILTER);
651
    }
652
 
653
    @Override
654
    public void resetValue() {
655
        this.setText("");
656
    }
657
 
658
    @Override
659
    public void setValue(String val) {
660
        this.setText(val);
661
    }
662
 
663
    @Override
664
    public void addValueListener(PropertyChangeListener l) {
665
        this.supp.addPropertyChangeListener(l);
666
    }
667
 
668
    @Override
669
    public String getValue() {
670
        return (String) this.getText();
671
    }
672
 
673
    @Override
674
    public void rmValueListener(PropertyChangeListener l) {
675
        this.supp.removePropertyChangeListener(l);
676
    }
677
 
678
    @Override
679
    public void itemSelected(IComboSelectionItem item) {
680
        if (item == null) {
681
            fireSelectionRow(null);
682
        } else {
683
            final SQLRowAccessor row = item.getRow();
684
            if (this.isLoading) {
685
                this.rowToSelect = row;
686
 
687
            } else {
688
                if (!CompareUtils.equals(this.selectedRow, row)) {
689
                    this.setSelectedRow(row);
690
                    this.selectItem(item);
691
                    this.fireSelectionRow(row);
692
                }
693
            }
694
        }
695
    }
696
 
697
}