Line 1... |
Line 1... |
1 |
/*
|
1 |
/*
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3 |
*
|
3 |
*
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
5 |
*
|
5 |
*
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
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
|
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
|
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.
|
9 |
* language governing permissions and limitations under the License.
|
Line 12... |
Line 12... |
12 |
*/
|
12 |
*/
|
13 |
|
13 |
|
14 |
package org.openconcerto.sql.sqlobject;
|
14 |
package org.openconcerto.sql.sqlobject;
|
15 |
|
15 |
|
16 |
import org.openconcerto.sql.model.SQLField;
|
16 |
import org.openconcerto.sql.model.SQLField;
|
17 |
import org.openconcerto.sql.model.SQLRow;
|
- |
|
18 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
17 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
19 |
import org.openconcerto.sql.model.SQLRowValues;
|
18 |
import org.openconcerto.sql.model.SQLRowValues;
|
20 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
19 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
21 |
import org.openconcerto.sql.model.SQLSelect;
|
20 |
import org.openconcerto.sql.model.SQLSelect;
|
22 |
import org.openconcerto.sql.model.SQLTable;
|
21 |
import org.openconcerto.sql.model.SQLTable;
|
Line 42... |
Line 41... |
42 |
import java.awt.event.KeyListener;
|
41 |
import java.awt.event.KeyListener;
|
43 |
import java.beans.PropertyChangeListener;
|
42 |
import java.beans.PropertyChangeListener;
|
44 |
import java.beans.PropertyChangeSupport;
|
43 |
import java.beans.PropertyChangeSupport;
|
45 |
import java.sql.SQLException;
|
44 |
import java.sql.SQLException;
|
46 |
import java.util.ArrayList;
|
45 |
import java.util.ArrayList;
|
- |
|
46 |
import java.util.HashMap;
|
47 |
import java.util.Iterator;
|
47 |
import java.util.Iterator;
|
48 |
import java.util.List;
|
48 |
import java.util.List;
|
- |
|
49 |
import java.util.Map;
|
49 |
import java.util.Stack;
|
50 |
import java.util.Stack;
|
50 |
import java.util.Vector;
|
51 |
import java.util.Vector;
|
51 |
|
52 |
|
52 |
import javax.swing.JComponent;
|
53 |
import javax.swing.JComponent;
|
53 |
import javax.swing.JPanel;
|
54 |
import javax.swing.JPanel;
|
Line 95... |
Line 96... |
95 |
private int autoCheckDelay = 1000;
|
96 |
private int autoCheckDelay = 1000;
|
96 |
private boolean disposed = false;
|
97 |
private boolean disposed = false;
|
97 |
private Stack<String> searchStack = new Stack<String>();
|
98 |
private Stack<String> searchStack = new Stack<String>();
|
98 |
private boolean autoselectIfMatch;
|
99 |
private boolean autoselectIfMatch;
|
99 |
private static final int PAUSE_MS = 150;
|
100 |
private static final int PAUSE_MS = 150;
|
- |
|
101 |
private final boolean hasDeclinaison;
|
100 |
|
102 |
|
101 |
public ITextArticleWithCompletion(SQLTable tableArticle, SQLTable tableARticleFournisseur) {
|
103 |
public ITextArticleWithCompletion(SQLTable tableArticle, SQLTable tableARticleFournisseur, boolean withDeclinaison) {
|
102 |
this.tableArticle = tableArticle;
|
104 |
this.tableArticle = tableArticle;
|
103 |
this.tableArticleFournisseur = tableARticleFournisseur;
|
105 |
this.tableArticleFournisseur = tableARticleFournisseur;
|
- |
|
106 |
this.hasDeclinaison = withDeclinaison;
|
104 |
this.supp = new PropertyChangeSupport(this);
|
107 |
this.supp = new PropertyChangeSupport(this);
|
105 |
this.popup = new ITextWithCompletionPopUp(this.model, this);
|
108 |
this.popup = new ITextWithCompletionPopUp(this.model, this);
|
- |
|
109 |
this.popup.setMinWith(450);
|
106 |
this.text = new JTextField();
|
110 |
this.text = new JTextField();
|
107 |
this.setLayout(new GridLayout(1, 1));
|
111 |
this.setLayout(new GridLayout(1, 1));
|
108 |
this.add(this.text);
|
112 |
this.add(this.text);
|
109 |
setTextEditor(this.text);
|
113 |
setTextEditor(this.text);
|
110 |
setPopupInvoker(this);
|
114 |
setPopupInvoker(this);
|
Line 283... |
Line 287... |
283 |
aText = aText.trim();
|
287 |
aText = aText.trim();
|
284 |
|
288 |
|
285 |
if (aText.length() > 0) {
|
289 |
if (aText.length() > 0) {
|
286 |
|
290 |
|
287 |
List<SQLSelect> listSel = new ArrayList<SQLSelect>();
|
291 |
List<SQLSelect> listSel = new ArrayList<SQLSelect>();
|
- |
|
292 |
|
288 |
// CODE ARTICLE = aText
|
293 |
// CODE ARTICLE = aText
|
289 |
SQLSelect selMatchingCode = new SQLSelect();
|
294 |
SQLSelect selMatchingCode = new SQLSelect();
|
290 |
// selMatchingCode.addSelectStar(this.tableArticle);
|
- |
|
- |
|
295 |
|
291 |
selMatchingCode.addSelect(this.tableArticle.getKey());
|
296 |
selMatchingCode.addSelect(this.tableArticle.getKey());
|
292 |
selMatchingCode.addSelect(this.tableArticle.getField("CODE"));
|
297 |
selMatchingCode.addSelect(this.tableArticle.getField("CODE"));
|
293 |
selMatchingCode.addSelect(this.tableArticle.getField("NOM"));
|
298 |
selMatchingCode.addSelect(this.tableArticle.getField("NOM"));
|
294 |
selMatchingCode.addSelect(this.tableArticle.getField("CODE_BARRE"));
|
299 |
selMatchingCode.addSelect(this.tableArticle.getField("CODE_BARRE"));
|
- |
|
300 |
if (this.hasDeclinaison) {
|
- |
|
301 |
for (String fieldName : this.tableArticle.getFieldsName()) {
|
- |
|
302 |
if (fieldName.startsWith("ID_ARTICLE_DECLINAISON_")) {
|
- |
|
303 |
selMatchingCode.addSelect(this.tableArticle.getField(fieldName));
|
- |
|
304 |
|
- |
|
305 |
SQLSelect selDecl = new SQLSelect();
|
- |
|
306 |
SQLTable tableDecl = this.tableArticle.getForeignTable(fieldName);
|
- |
|
307 |
selDecl.addSelect(tableDecl.getKey());
|
- |
|
308 |
selDecl.addSelect(tableDecl.getField("NOM"));
|
- |
|
309 |
listSel.add(selDecl);
|
- |
|
310 |
}
|
- |
|
311 |
}
|
- |
|
312 |
}
|
295 |
Where wMatchingCode = new Where(this.tableArticle.getField("CODE"), "=", aText);
|
313 |
Where wMatchingCode = new Where(this.tableArticle.getField("CODE"), "=", aText);
|
296 |
wMatchingCode = wMatchingCode.or(new Where(this.tableArticle.getField("NOM"), "=", aText));
|
314 |
wMatchingCode = wMatchingCode.or(new Where(this.tableArticle.getField("NOM"), "=", aText));
|
297 |
wMatchingCode = wMatchingCode.or(new Where(this.tableArticle.getField("CODE_BARRE"), "=", aText));
|
315 |
wMatchingCode = wMatchingCode.or(new Where(this.tableArticle.getField("CODE_BARRE"), "=", aText));
|
- |
|
316 |
wMatchingCode = wMatchingCode.and(new Where(this.tableArticle.getField("VIRTUEL"), "=", Boolean.FALSE));
|
298 |
if (this.whereAdditionnal != null) {
|
317 |
if (this.whereAdditionnal != null) {
|
299 |
wMatchingCode = wMatchingCode.and(this.whereAdditionnal);
|
318 |
wMatchingCode = wMatchingCode.and(this.whereAdditionnal);
|
300 |
}
|
319 |
}
|
301 |
selMatchingCode.setWhere(wMatchingCode);
|
320 |
selMatchingCode.setWhere(wMatchingCode);
|
302 |
if (this.selTransformer != null) {
|
321 |
if (this.selTransformer != null) {
|
Line 309... |
Line 328... |
309 |
// selContains.addSelectStar(this.tableArticle);
|
328 |
// selContains.addSelectStar(this.tableArticle);
|
310 |
selContains.addSelect(this.tableArticle.getKey());
|
329 |
selContains.addSelect(this.tableArticle.getKey());
|
311 |
selContains.addSelect(this.tableArticle.getField("CODE"));
|
330 |
selContains.addSelect(this.tableArticle.getField("CODE"));
|
312 |
selContains.addSelect(this.tableArticle.getField("NOM"));
|
331 |
selContains.addSelect(this.tableArticle.getField("NOM"));
|
313 |
selContains.addSelect(this.tableArticle.getField("CODE_BARRE"));
|
332 |
selContains.addSelect(this.tableArticle.getField("CODE_BARRE"));
|
- |
|
333 |
if (this.hasDeclinaison) {
|
- |
|
334 |
for (String fieldName : this.tableArticle.getFieldsName()) {
|
- |
|
335 |
if (fieldName.startsWith("ID_ARTICLE_DECLINAISON_")) {
|
- |
|
336 |
selContains.addSelect(this.tableArticle.getField(fieldName));
|
- |
|
337 |
}
|
- |
|
338 |
}
|
- |
|
339 |
}
|
314 |
Where wContains = new Where(this.tableArticle.getField("CODE"), "LIKE", "%" + aText + "%");
|
340 |
Where wContains = new Where(this.tableArticle.getField("CODE"), "LIKE", "%" + aText + "%");
|
315 |
wContains = wContains.or(new Where(this.tableArticle.getField("NOM"), "LIKE", "%" + aText + "%"));
|
341 |
wContains = wContains.or(new Where(this.tableArticle.getField("NOM"), "LIKE", "%" + aText + "%"));
|
316 |
wContains = wContains.or(new Where(this.tableArticle.getField("CODE_BARRE"), "LIKE", "%" + aText + "%"));
|
342 |
wContains = wContains.or(new Where(this.tableArticle.getField("CODE_BARRE"), "LIKE", "%" + aText + "%"));
|
- |
|
343 |
wContains = wContains.and(new Where(this.tableArticle.getField("VIRTUEL"), "=", Boolean.FALSE));
|
317 |
if (this.whereAdditionnal != null) {
|
344 |
if (this.whereAdditionnal != null) {
|
318 |
wContains = wContains.and(this.whereAdditionnal);
|
345 |
wContains = wContains.and(this.whereAdditionnal);
|
319 |
}
|
346 |
}
|
320 |
|
347 |
|
321 |
selContains.setWhere(wContains.and(wMatchingCode.not()));
|
348 |
selContains.setWhere(wContains.and(wMatchingCode.not()));
|
Line 364... |
Line 391... |
364 |
|
391 |
|
365 |
// Recherche dans les codes fournisseurs
|
392 |
// Recherche dans les codes fournisseurs
|
366 |
SQLTable tableCodeArt = this.tableArticle.getDBRoot().getTable("CODE_FOURNISSEUR");
|
393 |
SQLTable tableCodeArt = this.tableArticle.getDBRoot().getTable("CODE_FOURNISSEUR");
|
367 |
SQLRowValues rowValsCodeF = new SQLRowValues(tableCodeArt);
|
394 |
SQLRowValues rowValsCodeF = new SQLRowValues(tableCodeArt);
|
368 |
rowValsCodeF.putNulls("CODE");
|
395 |
rowValsCodeF.putNulls("CODE");
|
- |
|
396 |
final SQLRowValues putRowValuesArt = rowValsCodeF.putRowValues("ID_ARTICLE");
|
369 |
rowValsCodeF.putRowValues("ID_ARTICLE").putNulls(this.tableArticle.getFieldsName());
|
397 |
putRowValuesArt.putNulls(this.tableArticle.getFieldsName());
|
- |
|
398 |
if (this.hasDeclinaison) {
|
- |
|
399 |
for (String fieldName : this.tableArticle.getFieldsName()) {
|
- |
|
400 |
if (fieldName.startsWith("ID_ARTICLE_DECLINAISON_")) {
|
- |
|
401 |
putRowValuesArt.putRowValues(fieldName).putNulls("NOM");
|
- |
|
402 |
}
|
- |
|
403 |
}
|
370 |
|
404 |
}
|
371 |
final String codeText = aText;
|
405 |
final String codeText = aText;
|
372 |
SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsCodeF);
|
406 |
SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsCodeF);
|
373 |
fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
407 |
fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
374 |
@Override
|
408 |
@Override
|
375 |
public SQLSelect transformChecked(SQLSelect input) {
|
409 |
public SQLSelect transformChecked(SQLSelect input) {
|
Line 381... |
Line 415... |
381 |
}
|
415 |
}
|
382 |
});
|
416 |
});
|
383 |
List<SQLRowValues> resultCodeF = fetcher.fetch();
|
417 |
List<SQLRowValues> resultCodeF = fetcher.fetch();
|
384 |
resultList.add(2, resultCodeF);
|
418 |
resultList.add(2, resultCodeF);
|
385 |
|
419 |
|
- |
|
420 |
Map<String, Map<Number, SQLRowAccessor>> mapDecl = new HashMap<>();
|
386 |
for (List<? extends SQLRowAccessor> list : resultList) {
|
421 |
for (List<? extends SQLRowAccessor> list : resultList) {
|
387 |
|
422 |
|
- |
|
423 |
if (!list.isEmpty()) {
|
- |
|
424 |
if (list.get(0).getTable().getName().startsWith("ARTICLE_DECLINAISON")) {
|
- |
|
425 |
Map<Number, SQLRowAccessor> map = new HashMap<>();
|
- |
|
426 |
for (SQLRowAccessor sqlRow : list) {
|
- |
|
427 |
map.put(sqlRow.getIDNumber(), sqlRow);
|
- |
|
428 |
}
|
- |
|
429 |
mapDecl.put("ID_" + list.get(0).getTable().getName(), map);
|
- |
|
430 |
} else {
|
388 |
for (SQLRowAccessor sqlRow : list) {
|
431 |
for (SQLRowAccessor sqlRow : list) {
|
389 |
|
432 |
|
390 |
StringBuffer buf = new StringBuffer();
|
433 |
StringBuffer buf = new StringBuffer();
|
391 |
if (sqlRow.getTable().getName().equals("CODE_FOURNISSEUR")) {
|
434 |
if (sqlRow.getTable().getName().equals("CODE_FOURNISSEUR")) {
|
392 |
SQLRowAccessor rArt = sqlRow.getForeign("ID_ARTICLE");
|
435 |
SQLRowAccessor rArt = sqlRow.getForeign("ID_ARTICLE");
|
393 |
buf.append(sqlRow.getString("CODE") + " -- ");
|
436 |
buf.append(sqlRow.getString("CODE") + " -- ");
|
394 |
buf.append(rArt.getString("CODE") + " -- ");
|
437 |
buf.append(rArt.getString("CODE") + " -- ");
|
395 |
buf.append(rArt.getString("NOM"));
|
438 |
buf.append(rArt.getString("NOM"));
|
- |
|
439 |
if (this.hasDeclinaison) {
|
- |
|
440 |
for (String fieldName : rArt.getFields()) {
|
- |
|
441 |
if (fieldName.startsWith("ID_ARTICLE_DECLINAISON")) {
|
- |
|
442 |
Number rowDecl = rArt.getObject(fieldName) == null ? null : rArt.getNonEmptyForeignIDNumber(fieldName);
|
- |
|
443 |
if (rowDecl != null) {
|
- |
|
444 |
final Map<Number, SQLRowAccessor> mapIdDecl = mapDecl.get(fieldName);
|
- |
|
445 |
if (mapIdDecl != null) {
|
- |
|
446 |
final SQLRowAccessor sqlRowAccessor = mapIdDecl.get(rowDecl);
|
- |
|
447 |
if (sqlRowAccessor != null) {
|
- |
|
448 |
buf.append(" -- " + sqlRowAccessor.getString("NOM"));
|
- |
|
449 |
}
|
- |
|
450 |
}
|
- |
|
451 |
}
|
- |
|
452 |
}
|
- |
|
453 |
}
|
- |
|
454 |
}
|
- |
|
455 |
|
396 |
result.add(new IComboSelectionItem(rArt, buf.toString()));
|
456 |
result.add(new IComboSelectionItem(rArt, buf.toString()));
|
397 |
} else {
|
457 |
} else {
|
398 |
if (sqlRow.getString("CODE_BARRE") != null && sqlRow.getString("CODE_BARRE").trim().length() > 0) {
|
458 |
if (sqlRow.getString("CODE_BARRE") != null && sqlRow.getString("CODE_BARRE").trim().length() > 0) {
|
399 |
buf.append(sqlRow.getString("CODE_BARRE") + " -- ");
|
459 |
buf.append(sqlRow.getString("CODE_BARRE") + " -- ");
|
400 |
}
|
460 |
}
|
401 |
buf.append(sqlRow.getString("CODE") + " -- ");
|
461 |
buf.append(sqlRow.getString("CODE") + " -- ");
|
402 |
buf.append(sqlRow.getString("NOM"));
|
462 |
buf.append(sqlRow.getString("NOM"));
|
- |
|
463 |
if (this.hasDeclinaison) {
|
- |
|
464 |
for (String fieldName : sqlRow.getFields()) {
|
- |
|
465 |
if (fieldName.startsWith("ID_ARTICLE_DECLINAISON")) {
|
- |
|
466 |
Number rowDecl = sqlRow.getObject(fieldName) == null ? null : sqlRow.getNonEmptyForeignIDNumber(fieldName);
|
- |
|
467 |
if (rowDecl != null) {
|
- |
|
468 |
final Map<Number, SQLRowAccessor> mapIdDecl = mapDecl.get(fieldName);
|
- |
|
469 |
if (mapIdDecl != null) {
|
- |
|
470 |
final SQLRowAccessor sqlRowAccessor = mapIdDecl.get(rowDecl);
|
- |
|
471 |
if (sqlRowAccessor != null) {
|
- |
|
472 |
buf.append(" -- " + sqlRowAccessor.getString("NOM"));
|
- |
|
473 |
}
|
- |
|
474 |
}
|
- |
|
475 |
}
|
- |
|
476 |
}
|
- |
|
477 |
}
|
- |
|
478 |
}
|
403 |
result.add(new IComboSelectionItem(sqlRow, buf.toString()));
|
479 |
result.add(new IComboSelectionItem(sqlRow, buf.toString()));
|
404 |
}
|
480 |
}
|
405 |
}
|
481 |
}
|
406 |
}
|
482 |
}
|
- |
|
483 |
}
|
- |
|
484 |
}
|
407 |
|
485 |
|
408 |
}
|
486 |
}
|
409 |
|
487 |
|
410 |
return result;
|
488 |
return result;
|
411 |
}
|
489 |
}
|
Line 500... |
Line 578... |
500 |
this.popup.setVisible(false);
|
578 |
this.popup.setVisible(false);
|
501 |
}
|
579 |
}
|
502 |
|
580 |
|
503 |
private synchronized void showPopup() {
|
581 |
private synchronized void showPopup() {
|
504 |
if (this.model.getSize() > 0) {
|
582 |
if (this.model.getSize() > 0) {
|
505 |
if (this.popupInvoker.isShowing())
|
583 |
if (this.popupInvoker.isShowing()) {
|
- |
|
584 |
|
- |
|
585 |
String max = "";
|
- |
|
586 |
for (IComboSelectionItem item : this.model.getList()) {
|
- |
|
587 |
if (max.length() < item.getLabel().length()) {
|
- |
|
588 |
max = item.getLabel();
|
- |
|
589 |
}
|
- |
|
590 |
}
|
- |
|
591 |
final int stringWidth = this.text.getGraphics().getFontMetrics().stringWidth(max);
|
- |
|
592 |
this.popup.setMinWith(Math.max(450, stringWidth + 20));
|
- |
|
593 |
|
506 |
this.popup.show(this.popupInvoker, 0, this.text.getBounds().height);
|
594 |
this.popup.show(this.popupInvoker, 0, this.text.getBounds().height);
|
507 |
}
|
595 |
}
|
508 |
}
|
596 |
}
|
- |
|
597 |
}
|
509 |
|
598 |
|
510 |
public void changedUpdate(DocumentEvent e) {
|
599 |
public void changedUpdate(DocumentEvent e) {
|
511 |
updateAutoCompletion(false);
|
600 |
updateAutoCompletion(false);
|
512 |
this.supp.firePropertyChange("value", null, this.getText());
|
601 |
this.supp.firePropertyChange("value", null, this.getText());
|
513 |
}
|
602 |
}
|