OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
41 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.
41 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.erp.preferences;
15
 
144 ilm 16
import org.openconcerto.laf.LAFUtils;
41 ilm 17
import org.openconcerto.ui.DefaultGridBagConstraints;
18
import org.openconcerto.ui.table.AlternateTableCellRenderer;
19
import org.openconcerto.ui.warning.JLabelWarning;
20
import org.openconcerto.utils.ExceptionHandler;
21
 
22
import java.awt.Color;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.GridLayout;
26
import java.awt.event.MouseAdapter;
27
import java.awt.event.MouseEvent;
28
import java.util.ArrayList;
29
import java.util.Collections;
151 ilm 30
import java.util.Enumeration;
41 ilm 31
import java.util.List;
32
import java.util.Properties;
33
 
34
import javax.swing.BorderFactory;
35
import javax.swing.JComboBox;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38
import javax.swing.UIManager;
39
import javax.swing.UIManager.LookAndFeelInfo;
40
import javax.swing.UnsupportedLookAndFeelException;
41
import javax.swing.border.LineBorder;
42
 
180 ilm 43
import com.formdev.flatlaf.FlatLightLaf;
44
 
41 ilm 45
public class UIPreferencePanel extends DefaultLocalPreferencePanel {
46
 
47
    private static final String UI_PROPERTIES = "ui.properties";
48
    private static final String ALTERNATE_COLOR_BLUE = "ui.list.alternate.color.blue";
49
    private static final String ALTERNATE_COLOR_GREEN = "ui.list.alternate.color.green";
50
    private static final String ALTERNATE_COLOR_RED = "ui.list.alternate.color.red";
151 ilm 51
    private static final String UI_DPI = "ui.default.dpi";
41 ilm 52
    private static final String UI_LOOK = "ui.look";
53
    private JLabel selectedButton;
151 ilm 54
    private JComboBox comboLook, comboDPI;
41 ilm 55
 
56
    public UIPreferencePanel() {
57
        super("Interface graphique", UI_PROPERTIES);
58
    }
59
 
60
    @Override
61
    public void uiInit() {
62
        this.setLayout(new GridBagLayout());
63
        final GridBagConstraints c = new DefaultGridBagConstraints();
64
        c.fill = GridBagConstraints.NONE;
65
        this.add(new JLabel("Look"), c);
180 ilm 66
        comboLook = new JComboBox(new String[] { "natif du système", "Nimbus", "Flat" });
41 ilm 67
        String look = this.properties.getProperty(UI_LOOK);
180 ilm 68
        if (look != null && look.equals("nimbus")) {
41 ilm 69
            comboLook.setSelectedIndex(1);
180 ilm 70
        } else if (look != null && look.equals("flat")) {
71
            comboLook.setSelectedIndex(2);
41 ilm 72
        } else {
73
            comboLook.setSelectedIndex(0);
74
        }
75
 
76
        c.gridx++;
77
        this.add(comboLook, c);
151 ilm 78
 
79
        c.gridy++;
80
        c.gridx = 0;
81
        this.add(new JLabel("High DPI"), c);
82
        this.comboDPI = new JComboBox(new String[] { "Désactivé", "x1.3", "x2", "x3", "x4" });
83
        String dpi = this.properties.getProperty(UI_DPI);
84
        if (dpi != null && !dpi.equals("1")) {
85
            comboDPI.setSelectedIndex(Float.valueOf(dpi).intValue());
86
        }
87
 
88
        c.gridx++;
89
        this.add(comboDPI, c);
90
 
41 ilm 91
        final JLabel labelAlternate = new JLabel("Couleur de fond dans les liste pour l'alternance");
92
        c.gridx = 0;
93
        c.gridwidth = 2;
94
        c.gridy++;
95
        this.add(labelAlternate, c);
96
        List<Color> colors = new ArrayList<Color>();
97
        // Gris
98
        int col = 245;
99
        colors.add(new Color(col, col, col));
100
        col -= 10;
101
        colors.add(new Color(col, col, col));
102
        col -= 10;
103
        colors.add(new Color(col, col, col));
104
        col -= 10;
105
        colors.add(new Color(col, col, col));
106
        // Bleu petrole
107
        colors.add(new Color(232, 232, 240));
108
        colors.add(new Color(206, 206, 222));
109
        colors.add(new Color(180, 180, 205));
110
        colors.add(new Color(154, 154, 188));
111
 
112
        // Bleus
113
        colors.add(new Color(224, 240, 247));
114
        colors.add(new Color(190, 222, 239));
115
        colors.add(new Color(155, 205, 230));
116
        colors.add(new Color(120, 188, 221));
117
 
118
        // Bleus vert
119
        colors.add(new Color(217, 240, 247));
120
        colors.add(new Color(173, 222, 239));
121
        colors.add(new Color(130, 205, 230));
122
        colors.add(new Color(87, 188, 221));
123
        // Bleus Ciel
124
        colors.add(new Color(224, 247, 240));
125
        colors.add(new Color(190, 239, 222));
126
        colors.add(new Color(155, 230, 205));
127
        colors.add(new Color(120, 221, 188));
128
        // Violet
129
        colors.add(new Color(240, 217, 232));
130
        colors.add(new Color(222, 173, 206));
131
        colors.add(new Color(205, 130, 180));
132
        colors.add(new Color(188, 87, 154));
133
 
134
        // Marrons
135
        colors.add(new Color(240, 224, 217));
136
        colors.add(new Color(222, 190, 173));
137
        colors.add(new Color(205, 155, 130));
138
        colors.add(new Color(188, 120, 87));
139
 
140
        final JPanel colorPanel = new JPanel();
141
        colorPanel.setBorder(new LineBorder(Color.white, 2));
142
        colorPanel.setOpaque(true);
143
        colorPanel.setBackground(Color.WHITE);
144
        colorPanel.setLayout(new GridLayout(colors.size() / 4, 4, 2, 2));
145
        final JLabel[] buttonsAlternate = new JLabel[colors.size()];
146
 
147
        for (int i = 0; i < colors.size(); i++) {
148
            final Color color = colors.get(i);
149
            final JLabel button = new JLabel("    label test   ");
150
            button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
151
            buttonsAlternate[i] = button;
152
            button.addMouseListener(new MouseAdapter() {
153
                @Override
154
                public void mouseClicked(MouseEvent e) {
155
                    selectButton(button);
156
                }
157
            });
158
            button.setOpaque(true);
159
            button.setBackground(color);
160
            colorPanel.add(button);
161
        }
162
        c.gridy++;
163
 
164
        final String cRed = properties.getProperty(ALTERNATE_COLOR_RED);
165
        final String cGreen = properties.getProperty(ALTERNATE_COLOR_GREEN);
166
        final String cBlue = properties.getProperty(ALTERNATE_COLOR_BLUE);
167
        int r = -1, g = -1, b = -1;
168
        if (cRed != null) {
169
            r = Integer.parseInt(cRed);
170
        }
171
        if (cGreen != null) {
172
            g = Integer.parseInt(cGreen);
173
        }
174
        if (cBlue != null) {
175
            b = Integer.parseInt(cBlue);
176
 
177
        }
178
        selectButton(buttonsAlternate[1]);
179
        // Select the saved color setting
180
        if (r >= 0 && g >= 0 && b >= 0) {
181
            final Color expectedColor = new Color(r, g, b);
182
            for (int i = 0; i < buttonsAlternate.length; i++) {
183
                final JLabel jLabel = buttonsAlternate[i];
184
                if (jLabel.getBackground().equals(expectedColor)) {
185
                    selectButton(jLabel);
186
                    break;
187
                }
188
            }
189
        }
190
 
191
        this.add(colorPanel, c);
192
        c.gridy++;
193
        c.weightx = 1;
194
        c.weighty = 1;
195
        c.anchor = GridBagConstraints.NORTHWEST;
196
        this.add(new JLabelWarning("Les modifications d'interface nécessitent un redémarrage du logiciel."), c);
197
    }
198
 
199
    private void selectButton(JLabel button) {
200
        if (button != this.selectedButton) {
201
            if (selectedButton != null) {
202
                this.selectedButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
203
            }
204
            button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
205
            this.selectedButton = button;
206
        }
207
    }
208
 
209
    @Override
210
    public void storeValues() {
211
        final Color background = this.selectedButton.getBackground();
212
        properties.setProperty(ALTERNATE_COLOR_RED, String.valueOf(background.getRed()));
213
        properties.setProperty(ALTERNATE_COLOR_GREEN, String.valueOf(background.getGreen()));
214
        properties.setProperty(ALTERNATE_COLOR_BLUE, String.valueOf(background.getBlue()));
215
        AlternateTableCellRenderer.setDefaultMap(Collections.singletonMap(Color.WHITE, background));
144 ilm 216
        if (this.comboLook.getSelectedIndex() == 0) {
217
            properties.setProperty(UI_LOOK, "system");
180 ilm 218
        } else if (this.comboLook.getSelectedIndex() == 1) {
219
            properties.setProperty(UI_LOOK, "nimbus");
144 ilm 220
        } else {
180 ilm 221
            properties.setProperty(UI_LOOK, "flat");
41 ilm 222
        }
151 ilm 223
 
224
        if (this.comboDPI.getSelectedIndex() == 1) {
225
            properties.setProperty(UI_DPI, "1.3");
226
        } else if (this.comboDPI.getSelectedIndex() == 2) {
227
            properties.setProperty(UI_DPI, "2");
228
        } else if (this.comboDPI.getSelectedIndex() == 3) {
229
            properties.setProperty(UI_DPI, "3");
230
        } else if (this.comboDPI.getSelectedIndex() == 4) {
231
            properties.setProperty(UI_DPI, "4");
232
        } else {
233
            properties.setProperty(UI_DPI, "1");
234
        }
235
 
41 ilm 236
        super.storeValues();
237
    }
238
 
239
    private static void useNimbusLF() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
240
        UIManager.setLookAndFeel(getNimbusClassName());
241
        UIManager.put("control", new Color(240, 240, 240));
242
        UIManager.put("Table.showGrid", Boolean.TRUE);
243
        UIManager.put("FormattedTextField.background", new Color(240, 240, 240));
244
        UIManager.put("Table.alternateRowColor", Color.WHITE);
245
    }
246
 
247
    // only available from sun's release 6u10
248
    public static String getNimbusClassName() {
249
        // http://java.sun.com/javase/6/docs/technotes/guides/jweb/otherFeatures/nimbus_laf.html
250
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
251
            if ("Nimbus".equals(info.getName())) {
252
                return info.getClassName();
253
            }
254
        }
255
        return null;
256
    }
257
 
258
    public static void initUIFromPreferences() {
259
        Properties properties;
260
        try {
261
            properties = getPropertiesFromFile(UI_PROPERTIES);
262
            final String cRed = properties.getProperty(ALTERNATE_COLOR_RED);
263
            final String cGreen = properties.getProperty(ALTERNATE_COLOR_GREEN);
264
            final String cBlue = properties.getProperty(ALTERNATE_COLOR_BLUE);
265
            int r = -1, g = -1, b = -1;
266
            if (cRed != null) {
267
                r = Integer.parseInt(cRed);
268
            }
269
            if (cGreen != null) {
270
                g = Integer.parseInt(cGreen);
271
            }
272
            if (cBlue != null) {
273
                b = Integer.parseInt(cBlue);
274
            }
275
            if (r >= 0 && g >= 0 && b >= 0) {
276
                AlternateTableCellRenderer.setDefaultMap(Collections.singletonMap(Color.WHITE, new Color(r, g, b)));
277
            }
278
 
279
            final String look = properties.getProperty(UI_LOOK);
280
            final String nimbusClassName = getNimbusClassName();
180 ilm 281
            if (look != null && look.equals("flat")) {
182 ilm 282
                UIManager.put("Table.showHorizontalLines", true);
283
                UIManager.put("Table.showVerticalLines", true);
284
                UIManager.put("ScrollBar.showButtons", true);
285
                UIManager.put("TabbedPane.selectedBackground", Color.white);
286
                UIManager.put("TabbedPane.showTabSeparators", true);
180 ilm 287
                FlatLightLaf.install();
288
            } else if (look != null && look.equals("system")) {
73 ilm 289
                useSystemLF();
41 ilm 290
            } else if (look != null && look.equals("nimbus")) {
291
                useNimbusLF();
292
            } else if (nimbusClassName == null || !System.getProperty("os.name", "??").toLowerCase().contains("linux")) {
73 ilm 293
                useSystemLF();
41 ilm 294
            } else {
180 ilm 295
                FlatLightLaf.install();
41 ilm 296
            }
151 ilm 297
            final String dpi = properties.getProperty(UI_DPI);
298
            if (dpi != null && dpi.length() > 0 && !dpi.equalsIgnoreCase("1")) {
299
                setUIFont(Float.valueOf(dpi));
300
                UIManager.put("dpi.scale", Float.valueOf(dpi));
301
            } else {
302
                UIManager.put("dpi.scale", Float.valueOf(1));
303
            }
41 ilm 304
        } catch (Exception e) {
305
            ExceptionHandler.handle("Unable to restore UI preferences", e);
306
        }
307
    }
73 ilm 308
 
151 ilm 309
    public static void setUIFont(float factor) {
310
        Enumeration<Object> keys = UIManager.getDefaults().keys();
311
 
312
        while (keys.hasMoreElements()) {
313
            Object key = keys.nextElement();
314
            Object value = UIManager.get(key);
315
            if (value instanceof javax.swing.plaf.FontUIResource) {
316
                javax.swing.plaf.FontUIResource r = (javax.swing.plaf.FontUIResource) value;
317
 
318
                UIManager.put(key, r.deriveFont(r.getSize() * factor));
319
            }
320
        }
321
    }
322
 
73 ilm 323
    private static void useSystemLF() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
144 ilm 324
        LAFUtils.setLookAndFeel();
73 ilm 325
    }
41 ilm 326
}