OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | 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.finance.accounting.ui;
15
 
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.State;
174 ilm 18
import org.openconcerto.sql.model.SQLBackgroundTableCache;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
24
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
18 ilm 25
import org.openconcerto.ui.state.WindowStateManager;
26
 
27
import java.awt.DisplayMode;
28
import java.awt.GraphicsEnvironment;
29
import java.awt.GridLayout;
174 ilm 30
import java.awt.event.ActionEvent;
18 ilm 31
import java.awt.event.ComponentAdapter;
32
import java.awt.event.ComponentEvent;
33
import java.io.File;
174 ilm 34
import java.util.ArrayList;
35
import java.util.Collections;
36
import java.util.Comparator;
37
import java.util.List;
18 ilm 38
 
174 ilm 39
import javax.swing.AbstractAction;
18 ilm 40
import javax.swing.JFrame;
41
import javax.swing.event.TableModelEvent;
42
import javax.swing.event.TableModelListener;
43
 
44
public class ConsultationCompteFrame extends JFrame {
45
    // panel contenant la liste des ecritures
46
    private final ListPanelEcritures panelEcritures;
47
    // panel complet liste des ecritures + totaux
48
    private final ListeDesEcrituresPanel panel;
49
 
50
    private String titre;
51
 
174 ilm 52
    private int indexID;
53
 
54
    public ConsultationCompteFrame(ListeDesEcrituresPanel panel, SQLRowAccessor rowCpt) {
18 ilm 55
        super();
56
        this.panel = panel;
57
        this.panelEcritures = panel.getListPanelEcritures();
174 ilm 58
        this.titre = "Consultation compte n°" + rowCpt.getString("NUMERO") + " " + rowCpt.getString("NOM");
18 ilm 59
        // rafraichir le titre à chaque changement de la liste
60
        this.panelEcritures.getListe().addListener(new TableModelListener() {
61
            public void tableChanged(TableModelEvent e) {
62
                setTitle();
63
            }
64
        });
65
 
66
        this.uiInit();
67
        if (State.DEBUG) {
68
            State.INSTANCE.frameCreated();
69
            this.addComponentListener(new ComponentAdapter() {
70
                public void componentHidden(ComponentEvent e) {
71
                    State.INSTANCE.frameHidden();
72
                }
73
 
74
                public void componentShown(ComponentEvent e) {
75
                    State.INSTANCE.frameShown();
76
                }
77
            });
78
        }
174 ilm 79
 
80
        SQLTable ecrTable = rowCpt.getTable().getTable("ECRITURE");
81
 
82
        final int id = rowCpt.getID();
83
        Where w = new Where(ecrTable.getField("ID_COMPTE_PCE"), "=", id);
84
        getPanel().getListe().getRequest().setWhere(w);
85
 
86
        final SQLTable cptTable = ecrTable.getForeignTable("ID_COMPTE_PCE");
87
        List<SQLRow> rowsCpt = new ArrayList<SQLRow>(SQLBackgroundTableCache.getInstance().getCacheForTable(cptTable).getRows());
88
        Collections.sort(rowsCpt, new Comparator<SQLRow>() {
89
            @Override
90
            public int compare(SQLRow o1, SQLRow o2) {
91
 
92
                return o1.getString("NUMERO").compareTo(o2.getString("NUMERO"));
93
            }
94
        });
95
        final List<Integer> idsCpt = new ArrayList<>();
96
        for (SQLRow sqlRow : rowsCpt) {
97
            idsCpt.add(sqlRow.getID());
98
        }
99
        this.indexID = idsCpt.indexOf(rowCpt.getID());
100
 
101
        final PredicateRowAction prec = new PredicateRowAction(new AbstractAction("Précédent") {
102
 
103
            @Override
104
            public void actionPerformed(ActionEvent e) {
105
 
106
                if (indexID > 0) {
107
                    int newCptId = idsCpt.get(indexID - 1);
108
                    Where w = new Where(ecrTable.getField("ID_COMPTE_PCE"), "=", newCptId);
109
                    getPanel().getListe().getRequest().setWhere(w);
110
                    SQLRow rowCptNew = SQLBackgroundTableCache.getInstance().getCacheForTable(cptTable).getRowFromId(newCptId);
111
                    setTitle("Consultation compte n°" + rowCptNew.getString("NUMERO") + " " + rowCptNew.getString("NOM"));
112
                    indexID--;
113
                }
114
 
115
            }
116
        }, true, false);
117
        prec.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
118
        panel.getListPanelEcritures().getListe().addIListeAction(prec);
119
 
120
        final PredicateRowAction suivant = new PredicateRowAction(new AbstractAction("Suivant") {
121
 
122
            @Override
123
            public void actionPerformed(ActionEvent e) {
124
 
125
                if (indexID < idsCpt.size() - 1) {
126
                    int newCptId = idsCpt.get(indexID + 1);
127
                    Where w = new Where(ecrTable.getField("ID_COMPTE_PCE"), "=", newCptId);
128
                    getPanel().getListe().getRequest().setWhere(w);
129
                    SQLRow rowCptNew = SQLBackgroundTableCache.getInstance().getCacheForTable(cptTable).getRowFromId(newCptId);
130
                    setTitle("Consultation compte n°" + rowCptNew.getString("NUMERO") + " " + rowCptNew.getString("NOM"));
131
                    indexID++;
132
                }
133
 
134
            }
135
        }, true, false);
136
        suivant.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
137
        panel.getListPanelEcritures().getListe().addIListeAction(suivant);
138
 
18 ilm 139
    }
140
 
174 ilm 141
    @Override
142
    public void setTitle(String title) {
143
        this.titre = title;
144
        super.setTitle(title);
145
    }
146
 
18 ilm 147
    private String getPlural(String s, int nb) {
148
        return nb + " " + s + (nb > 1 ? "s" : "");
149
    }
150
 
151
    private void setTitle(boolean displayRowCount, boolean displayItemCount) {
152
 
153
        String title = this.titre;
154
 
155
        if (displayRowCount) {
156
            final int rowCount = this.panelEcritures.getListe().getRowCount();
157
            title += ", " + getPlural("ligne", rowCount);
158
            final int total = this.panelEcritures.getListe().getTotalRowCount();
159
            if (total != rowCount)
160
                title += " / " + total;
161
        }
162
        if (displayItemCount) {
163
            int count = this.panelEcritures.getListe().getItemCount();
164
            if (count >= 0)
165
                title += ", " + this.getPlural("élément", count);
166
        }
167
        this.setTitle(title);
168
    }
169
 
170
    public void setTitle() {
171
        this.setTitle(true, true);
172
    }
173
 
174
    final private void uiInit() {
175
        this.setTitle();
176
        this.getContentPane().setLayout(new GridLayout());
177
        this.getContentPane().add(this.panel);
178
        this.setBounds();
179
        final File configFile = new File(Configuration.getInstance().getConfDir(), this.panelEcritures.getElement().getPluralName() + "-window.xml");
180
        new WindowStateManager(this, configFile).loadState();
181
    }
182
 
183
    final protected void setBounds() {
184
        // TODO use getMaximiumWindowBounds
185
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
186
        DisplayMode dm = ge.getDefaultScreenDevice().getDisplayMode();
187
 
188
        final int topOffset = 50;
189
        if (dm.getWidth() <= 800 || dm.getHeight() <= 600) {
190
            this.setLocation(0, topOffset);
191
            this.setSize(dm.getWidth(), dm.getHeight() - topOffset);
192
        } else {
193
            this.setLocation(10, topOffset);
194
            this.setSize(dm.getWidth() - 50, dm.getHeight() - 20 - topOffset);
195
        }
196
    }
197
 
198
    public final ListPanelEcritures getPanel() {
199
        return this.panelEcritures;
200
    }
201
 
202
}