OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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;
15
 
16
import static org.openconcerto.utils.FileUtils.FILENAME_ESCAPER;
142 ilm 17
 
17 ilm 18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.State;
20
import org.openconcerto.sql.element.SQLComponent;
21
import org.openconcerto.sql.element.SQLElement;
22
import org.openconcerto.sql.view.list.ITableModel;
73 ilm 23
import org.openconcerto.ui.FrameUtil;
17 ilm 24
import org.openconcerto.ui.state.WindowStateManager;
142 ilm 25
import org.openconcerto.utils.StringUtils;
17 ilm 26
 
27
import java.awt.GridLayout;
142 ilm 28
import java.awt.Window;
17 ilm 29
import java.awt.event.ComponentAdapter;
30
import java.awt.event.ComponentEvent;
31
import java.beans.PropertyChangeEvent;
32
import java.beans.PropertyChangeListener;
33
import java.io.File;
182 ilm 34
import java.util.function.Function;
17 ilm 35
 
36
import javax.swing.JFrame;
37
import javax.swing.event.TableModelEvent;
38
import javax.swing.event.TableModelListener;
39
 
40
/**
41
 * Une frame affichant une liste et des boutons pour la manipuler, ainsi que des statistiques dans
42
 * la barre de titre.
43
 *
44
 * @author ILM Informatique 11 juin 2004
45
 */
46
public class IListFrame extends JFrame {
47
 
73 ilm 48
    public static final String SHORT_TITLE = "org.openconcerto.listframe.shortTitle";
142 ilm 49
    private static final String FILE_STRUCT_VERSION = "20160923";
73 ilm 50
 
182 ilm 51
    private static Function<? super SQLElement, File> CONF_DIR_FUNCTION = (elem) -> {
52
        final Configuration conf = Configuration.getInstance();
53
        return conf == null ? null : conf.getConfDir();
54
    };
55
 
56
    static synchronized public final void setConfDirFunction(final Function<? super SQLElement, File> f) {
57
        CONF_DIR_FUNCTION = f;
58
    }
59
 
60
    static synchronized public final File getConfDir(final SQLElement elem) {
61
        if (CONF_DIR_FUNCTION == null)
62
            return null;
63
        return CONF_DIR_FUNCTION.apply(elem);
64
    }
65
 
142 ilm 66
    // windowState-20160923/IListFrame/ARTICLE.xml
17 ilm 67
    static public final File getConfigFile(final SQLElement elem, final Class<? extends JFrame> c) {
68
        return getConfigFile(elem, null, c);
69
    }
70
 
142 ilm 71
    // windowState-20160923/IListFrame/ARTICLE-componentCode.xml
17 ilm 72
    static public final File getConfigFile(final SQLComponent comp, final Class<? extends JFrame> c) {
73
        return getConfigFile(comp.getElement(), comp, c);
74
    }
75
 
142 ilm 76
    static private final File getConfigFile(final SQLElement elem, final SQLComponent comp, final Class<? extends Window> c) {
77
        final String compName = comp == null ? "" : "-" + comp.getCode();
182 ilm 78
        return getConfigFile(getConfDir(elem), c, elem.getCode() + compName);
142 ilm 79
    }
80
 
81
    // windowState-20160923/WindowClass/code.xml
82
    static public final File getConfigFile(final Class<? extends Window> c, final String code) {
182 ilm 83
        return getConfigFile(getConfDir(null), c, code);
84
    }
85
 
86
    static public final File getConfigFile(final File rootDir, final Class<? extends Window> c, final String code) {
87
        if (rootDir == null)
73 ilm 88
            return null;
182 ilm 89
        final File structFile = new File(rootDir, "windowState-" + FILE_STRUCT_VERSION);
142 ilm 90
        return new File(structFile, c.getSimpleName() + File.separator + getConfigFileName(code));
17 ilm 91
    }
92
 
142 ilm 93
    static final String getConfigFileName(String code) {
94
        if (StringUtils.isEmpty(code, true))
95
            code = "default";
96
        return StringUtils.Shortener.MD5.getBoundedLengthString(FILENAME_ESCAPER.escape(code), 70) + ".xml";
97
    }
98
 
17 ilm 99
    private final IListPanel panel;
100
    private String title;
101
 
102
    public IListFrame(final IListPanel panel) {
103
        this.panel = panel;
104
        this.title = null;
105
 
106
        // rafraichir le titre à chaque changement de la liste
107
        this.panel.getListe().addListener(new TableModelListener() {
108
            public void tableChanged(TableModelEvent e) {
109
                setTitle();
110
            }
111
        });
112
        this.getPanel().getListe().addListenerOnModel(new PropertyChangeListener() {
113
            @Override
114
            public void propertyChange(PropertyChangeEvent evt) {
115
                if (evt.getPropertyName() == null || evt.getPropertyName().equals("loading") || evt.getPropertyName().equals("searching"))
116
                    setTitle();
117
            }
118
        });
119
 
120
        this.uiInit();
121
        if (State.DEBUG) {
122
            State.INSTANCE.frameCreated();
123
            this.addComponentListener(new ComponentAdapter() {
124
                public void componentHidden(ComponentEvent e) {
125
                    State.INSTANCE.frameHidden();
126
                }
127
 
128
                public void componentShown(ComponentEvent e) {
129
                    State.INSTANCE.frameShown();
130
                }
131
            });
132
        }
133
    }
134
 
135
    protected String getPlural(String s, int nb) {
136
        return nb + " " + s + (nb > 1 ? "s" : "");
137
    }
138
 
139
    protected void setTitle(boolean displayRowCount, boolean displayItemCount) {
140
        String title;
141
        if (this.title == null) {
73 ilm 142
            final String prefix = Boolean.getBoolean(SHORT_TITLE) ? "" : "Liste des ";
17 ilm 143
            title = prefix + this.panel.getElement().getPluralName();
144
        } else
145
            title = this.title;
146
        if (this.panel.getListe().isDead())
147
            title += ", détruite";
148
        else {
149
            if (displayRowCount) {
150
                final int rowCount = this.panel.getListe().getRowCount();
151
                title += ", " + getPlural("ligne", rowCount);
152
                final int total = this.panel.getListe().getTotalRowCount();
153
                if (total != rowCount)
154
                    title += " / " + total;
155
            }
156
            if (displayItemCount) {
157
                int count = this.panel.getListe().getItemCount();
158
                if (count >= 0)
159
                    title += ", " + this.getPlural("élément", count);
160
            }
161
            final ITableModel model = this.getPanel().getListe().getModel();
162
            if (model.isLoading())
163
                title += ", chargement en cours";
164
            if (model.isSearching())
165
                title += ", recherche en cours";
166
        }
167
        this.setTitle(title);
168
    }
169
 
170
    /**
171
     * Change the title of this frame from the default "Liste des " + getElement().getPluralName()
172
     * to <code>s</code>.
173
     *
174
     * @param s the title of this frame, can be <code>null</code> to restore the default behaviour.
175
     */
176
    public void setTextTitle(String s) {
177
        this.title = s;
178
    }
179
 
180
    public void setTitle() {
181
        this.setTitle(true, true);
182
    }
183
 
184
    final private void uiInit() {
185
        this.setTitle();
186
        this.getContentPane().setLayout(new GridLayout());
187
        this.getContentPane().add(this.panel);
73 ilm 188
        FrameUtil.setBounds(this);
17 ilm 189
        final File file;
190
        if (this.getPanel().getSQLComponent() != null)
191
            file = getConfigFile(this.getPanel().getSQLComponent(), this.getClass());
192
        else
193
            file = getConfigFile(this.getPanel().getElement(), this.getClass());
73 ilm 194
        if (file != null)
195
            new WindowStateManager(this, file).loadState();
17 ilm 196
    }
197
 
198
    public final IListPanel getPanel() {
199
        return this.panel;
200
    }
201
 
202
}