Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.coreplugin.app / org.gvsig.coreplugin.app.mainplugin / src / main / java / org / gvsig / coreplugin / preferences / general / appearance / AppearancePage.java @ 43913

History | View | Annotate | Download (11 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.coreplugin.preferences.general.appearance;
25

    
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.Comparator;
29
import java.util.List;
30
import javax.swing.DefaultComboBoxModel;
31
import javax.swing.ImageIcon;
32
import javax.swing.JPanel;
33
import javax.swing.LookAndFeel;
34
import javax.swing.UIManager;
35
import javax.swing.UIManager.LookAndFeelInfo;
36
import org.apache.commons.lang3.BooleanUtils;
37
import org.apache.commons.lang3.StringUtils;
38
import org.gvsig.andami.IconThemeHelper;
39
import org.gvsig.andami.Launcher;
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.andami.PluginsLocator;
42
import org.gvsig.andami.PluginsManager;
43
import org.gvsig.andami.preferences.IPreference;
44
import org.gvsig.andami.preferences.StoreException;
45
import org.gvsig.coreplugin.PreferencesExtension;
46
import org.gvsig.coreplugin.preferences.general.GeneralPage;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.dynobject.DynObject;
49
import org.gvsig.tools.i18n.I18nManager;
50
import org.gvsig.tools.swing.api.ListElement;
51
import org.gvsig.tools.util.ToolsUtilLocator;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
/**
56
 * Appearance page. Where the user can choose Look&Feels and maybe some more
57
 * stuff.
58
 */
59
public class AppearancePage extends AppearancePageView implements IPreference {
60
    //
61
    // The class AppearancePageView can extend from AbstractPreferencePage, not JPanel,
62
    // if rereate it from the GUI builder change te class declaration to:
63
    //    public abstract class AppearancePageView extends AbstractPreferencePage
64

    
65
    private static final long serialVersionUID = 9004756473020260443L;
66

    
67
    private static final Logger logger = LoggerFactory.getLogger(AppearancePage.class);
68

    
69
    public static String id = AppearancePage.class.getName();
70
    private ImageIcon icon;
71
    private boolean enableLookAndFeel = true;
72
    private final String defaultLookAndFeel;
73

    
74
    public AppearancePage() {
75
        setParentID(GeneralPage.id);
76
        installDefaultLookAndFeel();       
77
        this.defaultLookAndFeel = Launcher.getDefaultLookAndFeel();
78
        String x = PluginServices.getArgumentByName("enablelaf");
79
        if( x == null ) {            
80
            this.enableLookAndFeel = true;
81
        } else {
82
            this.enableLookAndFeel = BooleanUtils.toBoolean(x);
83
        }
84
        this.initComponents();
85
    }
86

    
87
    private void installDefaultLookAndFeel() {
88
        String[][] lafs = {
89
            { "Plastic XP", "com.jgoodies.looks.plastic.PlasticXPLookAndFeel" },
90
            { "Quaqua", "ch.randelshofer.quaqua.QuaquaLookAndFeel" },
91
            { "Web", "com.alee.laf.WebLookAndFeel" },
92
            { "Sea Glass", "com.seaglasslookandfeel.SeaGlassLookAndFeel" },
93
//            Substance da errores en la carga de gvSIG y el L&F no funciona 
94
//            { "Substance", "org.pushingpixels.substance.api.SubstanceLookAndFeel" },
95
//            { "SubstanceAutumn", "org.pushingpixels.substance.api.skin.SubstanceAutumnLookAndFeel" },
96
//            { "SubstanceOfficeBlue2007", "org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel" },
97
//            { "SubstanceBusiness", "org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel" },
98
//            { "SubstanceMistAqua", "org.pushingpixels.substance.api.skin.SubstanceMistAquaLookAndFeel" },
99
            { "Acryl", "com.jtattoo.plaf.acryl.AcrylLookAndFeel" },
100
            { "Fast", "com.jtattoo.plaf.fast.FastLookAndFeel" },
101
            { "Bernstein", "com.jtattoo.plaf.bernstein.BernsteinLookAndFeel" },
102
            { "Aluminium", "com.jtattoo.plaf.aluminium.AluminiumLookAndFeel" },
103
            { "Aero", "com.jtattoo.plaf.aero.AeroLookAndFeel"  },
104
            { "HiFi", "com.jtattoo.plaf.hifi.HiFiLookAndFeel" },
105
            { "McWin", "com.jtattoo.plaf.mcwin.McWinLookAndFeel" },
106
            { "Mint", "com.jtattoo.plaf.mint.MintLookAndFeel" },
107
            { "Noire", "com.jtattoo.plaf.noire.NoireLookAndFeel" },
108
            { "Smart", "com.jtattoo.plaf.smart.SmartLookAndFeel" },
109
            { "Luna", "com.jtattoo.plaf.luna.LunaLookAndFeel" },
110
            { "Texture", "com.jtattoo.plaf.texture.TextureLookAndFeel" }
111
        };            
112

    
113
        for( String[] laf : lafs ) {
114
            try {
115
                UIManager.installLookAndFeel(laf[0], laf[1]);
116
            } catch (Throwable th) {
117
                logger.info("Problems installing look and feel '"+laf[1]+"'", th);
118
            }
119
        }
120
    }
121

    
122
    private LookAndFeelInfo[] getSupportedsLookAndFeels() {
123
        if( !this.enableLookAndFeel ) {
124
            return null;
125
        }
126
        List<LookAndFeelInfo> supporteds = new ArrayList();
127
        try {
128
            LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();
129
            for( LookAndFeelInfo lookAndfeelInfo : installedLookAndFeels ) {
130
                try {
131
                    Class<?> lookAndFeelClass = Class.forName(lookAndfeelInfo.getClassName());
132
                    LookAndFeel lookAndFeel = (LookAndFeel) lookAndFeelClass.newInstance();
133
                    if( lookAndFeel.isSupportedLookAndFeel() ) {
134
                        supporteds.add(lookAndfeelInfo);
135
                    }
136
                } catch (Throwable th) {
137

    
138
                }
139
            }
140
        } catch (Throwable th) {
141
            return null;
142
        }
143
        if( supporteds.isEmpty() ) {
144
            return null;
145
        }
146
        Collections.sort(supporteds, new Comparator<LookAndFeelInfo>() {
147
            @Override
148
            public int compare(LookAndFeelInfo o1, LookAndFeelInfo o2) {
149
                return StringUtils.compare(o1.getName(),o2.getName());
150
            }
151
        });
152
        return supporteds.toArray(new LookAndFeelInfo[supporteds.size()]);
153
    }
154

    
155
    private void initComponents() {
156
        icon = IconThemeHelper.getImageIcon("edit-setup-appearance");
157
        LookAndFeelInfo[] lafs = this.getSupportedsLookAndFeels();
158
        if( lafs == null ) {
159
            this.cboLookAndFeel.setEnabled(false);
160
        } else {
161
            DefaultComboBoxModel model = new DefaultComboBoxModel();
162
            for( LookAndFeelInfo laf : lafs ) {
163
                model.addElement(new ListElement<>(laf.getName(), laf.getClassName()));
164
            }
165
            this.cboLookAndFeel.setModel(model);
166
            this.cboLookAndFeel.setEnabled(true);
167
            ListElement.setSelected(cboLookAndFeel, Launcher.getAndamiConfig().getLookAndFeel());
168

    
169
        }
170
        this.translateAll();
171
    }
172

    
173
    private void translateAll() {
174
        I18nManager i18n = ToolsLocator.getI18nManager();
175
        this.lblNeedRestart.setText("("+i18n.getTranslation("Los_cambios_efectuados_sobre_estos_valores_se_aplicaran_al_reiniciar_la_aplicacion")+")");
176
        this.lbllookAndFeel.setText(i18n.getTranslation("options.general.select_theme"));
177
        this.chkUseNativeFileChooser.setText(i18n.getTranslation("_use_native_file_dialog_chooser"));
178
    }
179

    
180
    @Override
181
    public boolean isResizeable() {
182
        return true;
183
    }
184

    
185
    @Override
186
    public void initializeValues() {
187
        this.initializeDefaults();
188
        this.chkUseNativeFileChooser.setSelected(ToolsUtilLocator.getFileDialogChooserManager().useNativeFileChooser());
189
    }
190

    
191
    @Override
192
    public void initializeDefaults() {
193
        PluginsManager pluginsManager = PluginsLocator.getManager();
194
        PluginServices plugin = pluginsManager.getPlugin(PreferencesExtension.class);
195

    
196
        DynObject pluginProperties = plugin.getPluginProperties();
197

    
198
        this.chkUseNativeFileChooser.setSelected((Boolean) pluginProperties.getDynValue("useNativeFileChooser"));
199

    
200
        if( this.cboLookAndFeel.getModel().getSize()>0 ) {
201
            this.cboLookAndFeel.setSelectedIndex(0);
202
            String currentLaF = getCurrentLookAndFeel();
203
            if( !StringUtils.isEmpty(currentLaF) ) {
204
                ListElement.setSelected(cboLookAndFeel, currentLaF);
205
            } else {
206
                this.cboLookAndFeel.setSelectedIndex(-1);
207
            }
208
            this.cboLookAndFeel.setEnabled(true);
209
        } else {
210
            this.cboLookAndFeel.setEnabled(false);
211
        }
212
    }
213
    
214
    private String getCurrentLookAndFeel() {
215
        String s = Launcher.getAndamiConfig().getLookAndFeel();
216
        if( StringUtils.isBlank(s) ) {
217
            s = defaultLookAndFeel;
218
        }
219
        return s;
220
    }
221

    
222
    @Override
223
    public String getID() {
224
        return id;
225
    }
226

    
227
    @Override
228
    public String getTitle() {
229
        I18nManager i18nManager = ToolsLocator.getI18nManager();
230
        return i18nManager.getTranslation("pref.appearance");
231
    }
232

    
233
    @Override
234
    public JPanel getPanel() {
235
        return this;
236
    }
237

    
238
    @Override
239
    public void storeValues() throws StoreException {
240
        PluginsManager pluginsManager = PluginsLocator.getManager();
241
        PluginServices plugin = pluginsManager.getPlugin(PreferencesExtension.class);
242

    
243
        DynObject pluginProperties = plugin.getPluginProperties();
244

    
245
        pluginProperties.setDynValue("useNativeFileChooser", this.chkUseNativeFileChooser.isSelected());
246
        ToolsUtilLocator.getFileDialogChooserManager().setUseNativeFileChooser(this.chkUseNativeFileChooser.isSelected());
247
        
248
        if( this.cboLookAndFeel.isEnabled() ) {
249
           String lookAndFeel = (String) ListElement.getSelected(cboLookAndFeel);
250
           if( lookAndFeel!=null ) {
251
               Launcher.getAndamiConfig().setLookAndFeel(lookAndFeel);
252
           }
253
        }
254
    }
255

    
256
    @Override
257
    public ImageIcon getIcon() {
258
        return icon;
259
    }
260

    
261
    @Override
262
    public boolean isValueChanged() {
263
        boolean changed = false;
264
        
265
        if( !changed ) {
266
            changed = this.chkUseNativeFileChooser.isSelected() != ToolsUtilLocator.getFileDialogChooserManager().useNativeFileChooser();
267
        }
268
        if( !changed ) {
269
            if( this.cboLookAndFeel.isEnabled() ) {
270
                String lookAndFeel = (String) ListElement.getSelected(cboLookAndFeel);
271
                if( lookAndFeel!=null ) {
272
                    changed = !lookAndFeel.equalsIgnoreCase(getCurrentLookAndFeel());
273
                }
274
            }
275
        }
276
        return changed;
277
    }
278

    
279
    @Override
280
    public void setChangesApplied() {
281
        // Do nothing
282
    }
283

    
284
}