OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 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.erp.core.common.ui;
15
 
73 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
156 ilm 17
import org.openconcerto.erp.utils.TM;
61 ilm 18
import org.openconcerto.sql.Configuration;
18 ilm 19
import org.openconcerto.sql.model.SQLField;
73 ilm 20
import org.openconcerto.sql.model.SQLRow;
18 ilm 21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.view.list.IListe;
24
import org.openconcerto.ui.DefaultGridBagConstraints;
25
import org.openconcerto.ui.JDate;
26
import org.openconcerto.utils.Tuple2;
27
import org.openconcerto.utils.cc.ITransformer;
28
 
29
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.beans.PropertyChangeEvent;
34
import java.beans.PropertyChangeListener;
73 ilm 35
import java.io.File;
18 ilm 36
import java.util.Calendar;
37
import java.util.Date;
41 ilm 38
import java.util.HashMap;
18 ilm 39
import java.util.LinkedHashMap;
40
import java.util.Map;
41
 
42
import javax.swing.BorderFactory;
43
import javax.swing.DefaultComboBoxModel;
44
import javax.swing.JComboBox;
45
import javax.swing.JLabel;
46
import javax.swing.JPanel;
73 ilm 47
import javax.swing.event.EventListenerList;
18 ilm 48
 
49
public class IListFilterDatePanel extends JPanel {
50
 
41 ilm 51
    private JDate dateDu, dateAu;
156 ilm 52
    private static final String CUSTOM_COMBO_ITEM = TM.tr("dateFilter.custom");
41 ilm 53
    private Map<IListe, SQLField> mapList;
57 ilm 54
    // Cache des transformers initiaux
55
    private Map<IListe, ITransformer<SQLSelect, SQLSelect>> mapListTransformer;
41 ilm 56
    // Liste des filtres
57
    private Map<String, Tuple2<Date, Date>> map;
61 ilm 58
    private static LinkedHashMap<String, Tuple2<Date, Date>> mapDefault;
93 ilm 59
    public static boolean LOAD_STATE = true;
18 ilm 60
 
61 ilm 61
    private JComboBox combo;
62
 
73 ilm 63
    private EventListenerList listeners = new EventListenerList();
64
 
80 ilm 65
    private final PropertyChangeListener listener = new PropertyChangeListener() {
73 ilm 66
 
18 ilm 67
        @Override
80 ilm 68
        public void propertyChange(PropertyChangeEvent evt) {
73 ilm 69
            combo.setSelectedItem(CUSTOM_COMBO_ITEM);
18 ilm 70
            fireDateChanged();
71
        }
72
    };
73
 
74
    // FIXME Cacher pour ne pas recharger la liste si il n'y a aucune modif
75
    public IListFilterDatePanel(IListe l, SQLField fieldDate) {
76
        this(l, fieldDate, null);
61 ilm 77
        if (l.getRequest() == Configuration.getInstance().getDirectory().getElement(l.getSource().getPrimaryTable()).getListRequest()) {
93 ilm 78
            System.err.println("Attention il ne faut pas utiliser la ListRequest par défaut sinon les filtres restent !!!");
61 ilm 79
            Thread.dumpStack();
80
        }
18 ilm 81
    }
82
 
83
    public static Map<String, Tuple2<Date, Date>> getDefaultMap() {
61 ilm 84
        if (mapDefault == null) {
85
            initDefaultMap();
86
        }
18 ilm 87
        Map<String, Tuple2<Date, Date>> m = new LinkedHashMap<String, Tuple2<Date, Date>>();
61 ilm 88
        m.putAll(mapDefault);
89
        return m;
90
    }
18 ilm 91
 
61 ilm 92
    private static void initDefaultMap() {
93
        mapDefault = new LinkedHashMap<String, Tuple2<Date, Date>>();
73 ilm 94
 
95
        // ALL
96
        Date emptyDate = null;
156 ilm 97
        mapDefault.put(TM.tr("dateFilter.none"), Tuple2.create(emptyDate, emptyDate));
73 ilm 98
 
61 ilm 99
        Calendar c = Calendar.getInstance();
18 ilm 100
        // Année courante
73 ilm 101
        clearTimeSchedule(c);
18 ilm 102
        c.set(Calendar.DATE, 1);
103
        c.set(Calendar.MONTH, 0);
104
        Date d1 = c.getTime();
73 ilm 105
        setEndTimeSchedule(c);
18 ilm 106
        c.set(Calendar.DATE, 31);
107
        c.set(Calendar.MONTH, 11);
108
        Date d2 = c.getTime();
156 ilm 109
        mapDefault.put(TM.tr("dateFilter.currentYear"), Tuple2.create(d1, d2));
18 ilm 110
 
111
        // Année précedente
73 ilm 112
        clearTimeSchedule(c);
18 ilm 113
        c.set(Calendar.DATE, 1);
114
        c.set(Calendar.MONTH, 0);
115
        c.add(Calendar.YEAR, -1);
116
        Date d3 = c.getTime();
73 ilm 117
 
118
        setEndTimeSchedule(c);
18 ilm 119
        c.set(Calendar.DATE, 31);
120
        c.set(Calendar.MONTH, 11);
121
        Date d4 = c.getTime();
156 ilm 122
        mapDefault.put(TM.tr("dateFilter.previousYear"), Tuple2.create(d3, d4));
18 ilm 123
 
124
        // Mois courant
125
        c = Calendar.getInstance();
73 ilm 126
        clearTimeSchedule(c);
18 ilm 127
        c.set(Calendar.DATE, 1);
128
        Date d5 = c.getTime();
129
        c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
73 ilm 130
        setEndTimeSchedule(c);
18 ilm 131
        Date d6 = c.getTime();
156 ilm 132
        mapDefault.put(TM.tr("dateFilter.currentMonth"), Tuple2.create(d5, d6));
18 ilm 133
 
134
        // Mois précédent
135
        c = Calendar.getInstance();
73 ilm 136
        clearTimeSchedule(c);
18 ilm 137
        c.set(Calendar.DATE, 1);
138
        c.add(Calendar.MONTH, -1);
139
        Date d7 = c.getTime();
140
        c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
73 ilm 141
        setEndTimeSchedule(c);
18 ilm 142
        Date d8 = c.getTime();
156 ilm 143
        mapDefault.put(TM.tr("dateFilter.previousMonth"), Tuple2.create(d7, d8));
18 ilm 144
 
145
        // semaine courante
146
        c = Calendar.getInstance();
73 ilm 147
        clearTimeSchedule(c);
18 ilm 148
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
149
        Date d9 = c.getTime();
150
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
73 ilm 151
        setEndTimeSchedule(c);
18 ilm 152
        Date d10 = c.getTime();
156 ilm 153
        mapDefault.put(TM.tr("dateFilter.currentWeek"), Tuple2.create(d9, d10));
18 ilm 154
 
155
        // semaine précédente
156
        c = Calendar.getInstance();
73 ilm 157
        clearTimeSchedule(c);
18 ilm 158
        c.add(Calendar.DATE, -7);
159
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
160
        Date d11 = c.getTime();
161
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
73 ilm 162
        setEndTimeSchedule(c);
18 ilm 163
        Date d12 = c.getTime();
156 ilm 164
        mapDefault.put(TM.tr("dateFilter.previousWeek"), Tuple2.create(d11, d12));
73 ilm 165
 
166
        // Exercice courant
167
        SQLRow rowEx = ComptaPropsConfiguration.getInstanceCompta().getRowSociete().getForeignRow("ID_EXERCICE_COMMON");
168
        Calendar c1 = rowEx.getDate("DATE_DEB");
169
        clearTimeSchedule(c1);
170
        Calendar c2 = rowEx.getDate("DATE_FIN");
171
        setEndTimeSchedule(c2);
172
 
173
        final Tuple2<Date, Date> exerciceTuple = Tuple2.create(c1.getTime(), c2.getTime());
174
        if (findItem(exerciceTuple, mapDefault).equals(CUSTOM_COMBO_ITEM)) {
156 ilm 175
            mapDefault.put(TM.tr("dateFilter.currentFiscalYear"), exerciceTuple);
73 ilm 176
        }
177
 
178
        // Custom
179
        mapDefault.put(CUSTOM_COMBO_ITEM, null);
61 ilm 180
    }
18 ilm 181
 
61 ilm 182
    public static void addDefaultValue(String label, Tuple2<Date, Date> period) {
183
        if (mapDefault == null)
184
            initDefaultMap();
185
        mapDefault.put(label, period);
18 ilm 186
    }
187
 
188
    public IListFilterDatePanel(IListe l, SQLField fieldDate, Map<String, Tuple2<Date, Date>> m) {
189
        super(new GridBagLayout());
61 ilm 190
        setOpaque(false);
41 ilm 191
        Map<IListe, SQLField> map = new HashMap<IListe, SQLField>();
192
        map.put(l, fieldDate);
193
 
194
        init(map, m);
195
 
196
    }
197
 
198
    public IListFilterDatePanel(Map<IListe, SQLField> l, Map<String, Tuple2<Date, Date>> m) {
199
        super(new GridBagLayout());
200
        init(l, m);
201
 
202
    }
203
 
204
    public void init(Map<IListe, SQLField> mapList, Map<String, Tuple2<Date, Date>> m) {
205
 
156 ilm 206
        this.setBorder(BorderFactory.createTitledBorder(TM.tr("dateFilter.range")));
41 ilm 207
        this.mapList = mapList;
57 ilm 208
 
209
        this.mapListTransformer = new HashMap<IListe, ITransformer<SQLSelect, SQLSelect>>();
210
        for (IListe l : mapList.keySet()) {
73 ilm 211
 
57 ilm 212
            this.mapListTransformer.put(l, l.getRequest().getSelectTransf());
213
        }
214
 
18 ilm 215
        this.dateDu = new JDate();
216
        this.dateAu = new JDate();
217
        this.map = m;
218
 
219
        GridBagConstraints c = new DefaultGridBagConstraints();
220
        c.gridx = GridBagConstraints.RELATIVE;
221
        c.weightx = 0;
90 ilm 222
        c.fill = GridBagConstraints.NONE;
18 ilm 223
 
224
        // Période pédéfini
225
        if (map != null && map.keySet().size() > 0) {
226
            DefaultComboBoxModel model = new DefaultComboBoxModel();
73 ilm 227
 
18 ilm 228
            for (String s : this.map.keySet()) {
229
                model.addElement(s);
230
            }
231
 
61 ilm 232
            this.combo = new JComboBox(model);
233
 
18 ilm 234
            c.weightx = 0;
235
            this.add(combo, c);
236
 
237
            combo.addActionListener(new ActionListener() {
238
                @Override
239
                public void actionPerformed(ActionEvent e) {
240
                    String s = (String) combo.getSelectedItem();
241
                    setPeriode(map.get(s));
242
                }
243
            });
244
        }
245
        // Filtre
156 ilm 246
        this.add(new JLabel(TM.tr("dateFilter.from")), c);
18 ilm 247
        this.add(this.dateDu, c);
156 ilm 248
        this.add(new JLabel(TM.tr("dateFilter.to")), c);
18 ilm 249
        this.add(this.dateAu, c);
80 ilm 250
        this.dateAu.addValueListener(this.listener);
251
        this.dateDu.addValueListener(this.listener);
73 ilm 252
 
253
        IListFilterDateStateManager stateManager = new IListFilterDateStateManager(this, getConfigFile(mapList), true);
93 ilm 254
        if (LOAD_STATE) {
255
            stateManager.loadState();
256
        }
18 ilm 257
    }
258
 
259
    public void setDateDu(Date d) {
260
        this.dateDu.setValue(d);
261
    }
262
 
263
    public void setDateAu(Date d) {
264
        this.dateAu.setValue(d);
265
    }
266
 
73 ilm 267
    public Date getFromValue() {
268
        return this.dateDu.getValue();
269
    }
270
 
271
    public Date getToValue() {
272
        return this.dateAu.getValue();
273
    }
274
 
61 ilm 275
    private static Tuple2<String, Tuple2<Date, Date>> DEFAULT_FILTER = null;
276
 
277
    public static void setDefaultFilter(Tuple2<String, Tuple2<Date, Date>> t) {
278
        DEFAULT_FILTER = t;
41 ilm 279
    }
280
 
61 ilm 281
    public void setFilterOnDefault() {
282
 
283
        if (DEFAULT_FILTER != null) {
284
 
285
            if (this.combo != null) {
286
                this.combo.setSelectedItem(DEFAULT_FILTER.get0());
287
            } else {
288
                setPeriode(DEFAULT_FILTER.get1().get0(), DEFAULT_FILTER.get1().get1());
289
            }
290
        }
291
    }
292
 
73 ilm 293
    /**
294
     *
295
     * @param t do nothing if t is null
296
     */
18 ilm 297
    public void setPeriode(Tuple2<Date, Date> t) {
73 ilm 298
        if (t != null) {
18 ilm 299
            setPeriode(t.get0(), t.get1());
300
        }
301
    }
302
 
303
    public void setPeriode(Date du, Date au) {
304
        setDateAu(au);
305
        setDateDu(du);
306
 
307
        fireDateChanged();
308
    }
309
 
310
    public void fireDateChanged() {
311
        if (this.dateAu.getValue() == null && this.dateDu.getValue() == null) {
57 ilm 312
            for (IListe list : this.mapList.keySet()) {
313
                list.getRequest().setSelectTransf(this.mapListTransformer.get(list));
18 ilm 314
            }
73 ilm 315
        } else if (this.dateAu.getValue() == null) {
18 ilm 316
            final Calendar c = Calendar.getInstance();
317
            c.setTime(this.dateDu.getValue());
73 ilm 318
            clearTimeSchedule(c);
18 ilm 319
 
57 ilm 320
            for (final IListe list : this.mapList.keySet()) {
41 ilm 321
                final SQLField filterField = this.mapList.get(list);
322
                list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
323
                    @Override
324
                    public SQLSelect transformChecked(SQLSelect input) {
73 ilm 325
                        Where w = new Where(input.getAlias(filterField), ">=", c.getTime());
326
                        return setWhere(input, w, mapListTransformer.get(list));
18 ilm 327
                    }
41 ilm 328
                });
329
            }
18 ilm 330
 
73 ilm 331
        } else if (this.dateDu.getValue() == null) {
18 ilm 332
            final Calendar c = Calendar.getInstance();
333
            c.setTime(this.dateAu.getValue());
73 ilm 334
            setEndTimeSchedule(c);
57 ilm 335
            for (final IListe list : this.mapList.keySet()) {
41 ilm 336
                final SQLField filterField = this.mapList.get(list);
18 ilm 337
 
41 ilm 338
                list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
339
                    @Override
340
                    public SQLSelect transformChecked(SQLSelect input) {
73 ilm 341
                        Where w = new Where(input.getAlias(filterField), "<=", c.getTime());
342
                        return setWhere(input, w, mapListTransformer.get(list));
18 ilm 343
                    }
41 ilm 344
                });
345
            }
73 ilm 346
 
347
        } else {
348
            final Calendar c = Calendar.getInstance();
349
            c.setTime(this.dateAu.getValue());
350
            setEndTimeSchedule(c);
351
 
352
            final Calendar c2 = Calendar.getInstance();
353
            c2.setTime(this.dateDu.getValue());
354
            clearTimeSchedule(c2);
355
 
356
            for (final IListe list : this.mapList.keySet()) {
357
                final SQLField filterField = this.mapList.get(list);
358
 
359
                list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
360
                    @Override
361
                    public SQLSelect transformChecked(SQLSelect input) {
362
                        final Where w = new Where(input.getAlias(filterField), c2.getTime(), c.getTime());
363
                        return setWhere(input, w, mapListTransformer.get(list));
364
                    }
365
                });
366
            }
18 ilm 367
        }
368
 
73 ilm 369
        final Tuple2<Date, Date> selectedTuple = Tuple2.create(this.dateDu.getValue(), this.dateAu.getValue());
370
        this.combo.setSelectedItem(findItem(selectedTuple, this.map));
371
        for (PropertyChangeListener l : this.listeners.getListeners(PropertyChangeListener.class)) {
372
            l.propertyChange(new PropertyChangeEvent(this, "valueChanged", null, selectedTuple));
373
        }
374
    }
375
 
376
    public void addValueListener(PropertyChangeListener l) {
377
        this.listeners.add(PropertyChangeListener.class, l);
378
    }
379
 
380
    public void rmValueListener(PropertyChangeListener l) {
381
        this.listeners.remove(PropertyChangeListener.class, l);
382
    }
383
 
384
    private SQLSelect setWhere(SQLSelect input, Where w, ITransformer<SQLSelect, SQLSelect> t) {
385
        if (t != null) {
386
            input = t.transformChecked(input);
387
        }
388
        input.andWhere(w);
389
        return input;
390
    }
391
 
392
    public static String findItem(Tuple2<Date, Date> t, Map<String, Tuple2<Date, Date>> mapItem) {
393
 
394
        Date d1 = t.get0();
395
        Date d2 = t.get1();
396
        Calendar c1 = getCalendarFromDate(d1);
397
        Calendar c2 = getCalendarFromDate(d2);
398
 
399
        for (String label : mapItem.keySet()) {
400
            Tuple2<Date, Date> t2 = mapItem.get(label);
401
            if (t2 != null) {
402
                final Date get0 = t2.get0();
403
                final Date get1 = t2.get1();
404
                Calendar cGet0 = getCalendarFromDate(get0);
405
                Calendar cGet1 = getCalendarFromDate(get1);
406
 
407
                if (isDateEquals(c1, cGet0) && isDateEquals(c2, cGet1)) {
408
                    return label;
409
                }
410
            }
411
        }
412
        return CUSTOM_COMBO_ITEM;
413
    }
414
 
415
    private static Calendar getCalendarFromDate(final Date d) {
416
        Calendar cal = null;
417
        if (d != null) {
418
            cal = Calendar.getInstance();
419
            cal.setTime(d);
420
        }
421
        return cal;
422
    }
423
 
424
    public static boolean isDateEquals(Calendar d1, Calendar d2) {
425
        boolean b = false;
426
        if (d1 == null && d2 == null) {
427
            b = true;
428
        } else if (d1 != null && d2 != null) {
429
            b = d1.get(Calendar.DAY_OF_MONTH) == d2.get(Calendar.DAY_OF_MONTH) && d2.get(Calendar.YEAR) == d1.get(Calendar.YEAR) && d1.get(Calendar.MONTH) == d2.get(Calendar.MONTH);
430
        } else {
431
            b = false;
432
        }
433
        return b;
434
    }
435
 
436
    private static void clearTimeSchedule(final Calendar c) {
437
        c.set(Calendar.HOUR_OF_DAY, 0);
438
        c.set(Calendar.MINUTE, 0);
439
        c.set(Calendar.SECOND, 0);
440
        c.set(Calendar.MILLISECOND, 0);
441
    }
442
 
443
    private static void setEndTimeSchedule(final Calendar c) {
18 ilm 444
        c.set(Calendar.HOUR_OF_DAY, 23);
445
        c.set(Calendar.MINUTE, 59);
73 ilm 446
        c.set(Calendar.SECOND, 59);
18 ilm 447
        c.set(Calendar.MILLISECOND, 59);
73 ilm 448
    }
18 ilm 449
 
73 ilm 450
    static private final File getConfigFile(Map<IListe, SQLField> mapList) {
451
        final Configuration conf = Configuration.getInstance();
452
        if (conf == null)
453
            return null;
41 ilm 454
 
73 ilm 455
        String name = null;
456
        for (IListe l : mapList.keySet()) {
90 ilm 457
            final File configFile = l.getConfigFile();
458
            if (configFile != null) {
459
                String confName = configFile.getName();
460
                if (name == null) {
461
                    name = confName;
462
                } else if (name.compareTo(confName) > 0) {
463
                    name = confName;
464
                }
465
            } else {
466
                name = l.getSource().getPrimaryTable().getName();
73 ilm 467
            }
41 ilm 468
        }
73 ilm 469
        return new File(conf.getConfDir(), "DateRanges" + File.separator + name);
18 ilm 470
    }
471
}