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 @ 43322

History | View | Annotate | Download (9.98 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.List;
28
import javax.swing.DefaultComboBoxModel;
29
import javax.swing.ImageIcon;
30
import javax.swing.JPanel;
31
import javax.swing.LookAndFeel;
32
import javax.swing.UIManager;
33
import javax.swing.UIManager.LookAndFeelInfo;
34
import org.apache.commons.lang3.BooleanUtils;
35
import org.apache.commons.lang3.StringUtils;
36
import org.gvsig.andami.IconThemeHelper;
37
import org.gvsig.andami.Launcher;
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.PluginsLocator;
40
import org.gvsig.andami.PluginsManager;
41
import org.gvsig.andami.preferences.IPreference;
42
import org.gvsig.andami.preferences.StoreException;
43
import org.gvsig.coreplugin.PreferencesExtension;
44
import org.gvsig.coreplugin.preferences.general.GeneralPage;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynObject;
47
import org.gvsig.tools.i18n.I18nManager;
48
import org.gvsig.tools.util.ToolsUtilLocator;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

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

    
62
    private static final long serialVersionUID = 9004756473020260443L;
63

    
64
    private static final Logger logger = LoggerFactory.getLogger(AppearancePage.class);
65

    
66
    public static String id = AppearancePage.class.getName();
67
    private ImageIcon icon;
68
    private boolean enableLookAndFeel = true;
69
    private final String defaultLookAndFeel;
70

    
71
    public static class ListElement<T> {
72

    
73
        private final String label;
74
        private final T value;
75

    
76
        public ListElement(String label, T value) {
77
            this.label = label;
78
            this.value = value;
79
        }
80

    
81
        @Override
82
        public String toString() {
83
            return this.label;
84
        }
85

    
86
        public T getValue() {
87
            return this.value;
88
        }
89
    }
90

    
91
    public AppearancePage() {
92
        setParentID(GeneralPage.id);
93
        installDefaultLookAndFeel();       
94
        this.defaultLookAndFeel = Launcher.getDefaultLookAndFeel();
95
        String x = PluginServices.getArgumentByName("enablelaf");
96
        if( x == null ) {            
97
            this.enableLookAndFeel = true;
98
        } else {
99
            this.enableLookAndFeel = BooleanUtils.toBoolean(x);
100
        }
101
        this.initComponents();
102
    }
103

    
104
    private void installDefaultLookAndFeel() {
105
        try {
106
            // install the plastic look and feel before getting the laf combobox
107
            UIManager.installLookAndFeel("Plastic XP", "com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
108
            // install the extra LAF's before getting the LAF combobox
109
            String osName = System.getProperty("os.name");
110
            if( osName.toLowerCase().startsWith("mac os x") ) {
111
                UIManager.installLookAndFeel("Quaqua", "ch.randelshofer.quaqua.QuaquaLookAndFeel");
112
            }
113
        } catch (Throwable th) {
114
            logger.warn("Problems installing default look and feel", th);
115
        }
116
    }
117

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

    
134
                }
135
            }
136
        } catch (Throwable th) {
137
            return null;
138
        }
139
        if( supporteds.isEmpty() ) {
140
            return null;
141
        }
142
        return supporteds.toArray(new LookAndFeelInfo[supporteds.size()]);
143
    }
144

    
145
    private void initComponents() {
146
        icon = IconThemeHelper.getImageIcon("edit-setup-appearance");
147
        LookAndFeelInfo[] lafs = this.getSupportedsLookAndFeels();
148
        if( lafs == null ) {
149
            this.cboLookAndFeel.setEnabled(false);
150
        } else {
151
            DefaultComboBoxModel model = new DefaultComboBoxModel();
152
            for( LookAndFeelInfo laf : lafs ) {
153
                model.addElement(new ListElement<>(laf.getName(), laf));
154
            }
155
            this.cboLookAndFeel.setModel(model);
156
            this.cboLookAndFeel.setEnabled(true);
157
        }
158
        this.translateAll();
159
    }
160

    
161
    private void translateAll() {
162
        I18nManager i18n = ToolsLocator.getI18nManager();
163
        this.lblNeedRestart.setText("("+i18n.getTranslation("Los_cambios_efectuados_sobre_estos_valores_se_aplicaran_al_reiniciar_la_aplicacion")+")");
164
        this.lbllookAndFeel.setText(i18n.getTranslation("options.general.select_theme"));
165
        this.chkUseNativeFileChooser.setText(i18n.getTranslation("_use_native_file_dialog_chooser"));
166
    }
167

    
168
    @Override
169
    public boolean isResizeable() {
170
        return true;
171
    }
172

    
173
    @Override
174
    public void initializeValues() {
175
        this.initializeDefaults();
176
        this.chkUseNativeFileChooser.setSelected(ToolsUtilLocator.getFileDialogChooserManager().useNativeFileChooser());
177
    }
178

    
179
    @Override
180
    public void initializeDefaults() {
181
        PluginsManager pluginsManager = PluginsLocator.getManager();
182
        PluginServices plugin = pluginsManager.getPlugin(PreferencesExtension.class);
183

    
184
        DynObject pluginProperties = plugin.getPluginProperties();
185

    
186
        this.chkUseNativeFileChooser.setSelected((Boolean) pluginProperties.getDynValue("useNativeFileChooser"));
187

    
188
        if( this.cboLookAndFeel.getModel().getSize()>0 ) {
189
            this.cboLookAndFeel.setSelectedIndex(0);
190
            if( !StringUtils.isEmpty(defaultLookAndFeel) ) {
191
                DefaultComboBoxModel<ListElement<LookAndFeelInfo>> model = (DefaultComboBoxModel<ListElement<LookAndFeelInfo>>) this.cboLookAndFeel.getModel();
192
                for( int i=0; i<model.getSize() ; i++ ) {
193
                    ListElement<LookAndFeelInfo> element = model.getElementAt(i);
194
                    if( defaultLookAndFeel.equals(element.getValue().getClassName()) ) {
195
                        this.cboLookAndFeel.setSelectedIndex(i);
196
                        break;
197
                    }
198
                }
199
            }
200
            this.cboLookAndFeel.setEnabled(true);
201
        } else {
202
            this.cboLookAndFeel.setEnabled(false);
203
        }
204
    }
205

    
206
    @Override
207
    public String getID() {
208
        return id;
209
    }
210

    
211
    @Override
212
    public String getTitle() {
213
        I18nManager i18nManager = ToolsLocator.getI18nManager();
214
        return i18nManager.getTranslation("pref.appearance");
215
    }
216

    
217
    @Override
218
    public JPanel getPanel() {
219
        return this;
220
    }
221

    
222
    @Override
223
    public void storeValues() throws StoreException {
224
        PluginsManager pluginsManager = PluginsLocator.getManager();
225
        PluginServices plugin = pluginsManager.getPlugin(PreferencesExtension.class);
226

    
227
        DynObject pluginProperties = plugin.getPluginProperties();
228

    
229
        pluginProperties.setDynValue("useNativeFileChooser", this.chkUseNativeFileChooser.isSelected());
230
        ToolsUtilLocator.getFileDialogChooserManager().setUseNativeFileChooser(this.chkUseNativeFileChooser.isSelected());
231
        
232
        if( this.cboLookAndFeel.isEnabled() ) {
233
           ListElement<LookAndFeelInfo> element = (ListElement<LookAndFeelInfo>) this.cboLookAndFeel.getSelectedItem();
234
           if( element!=null ) {
235
               String lookAndFeel = element.getValue().getClassName();
236
                Launcher.getAndamiConfig().setLookAndFeel(lookAndFeel);
237
           }
238
        }
239
    }
240

    
241
    @Override
242
    public ImageIcon getIcon() {
243
        return icon;
244
    }
245

    
246
    @Override
247
    public boolean isValueChanged() {
248
        PluginsManager pluginsManager = PluginsLocator.getManager();
249
        PluginServices plugin = pluginsManager.getPlugin(PreferencesExtension.class);
250

    
251
        DynObject pluginProperties = plugin.getPluginProperties();
252

    
253
        boolean changed = false;
254
        
255
        if( !changed ) {
256
            changed = this.chkUseNativeFileChooser.isSelected() != ToolsUtilLocator.getFileDialogChooserManager().useNativeFileChooser();
257
        }
258
        if( changed ) {
259
            if( this.cboLookAndFeel.isEnabled() ) {
260
               ListElement<LookAndFeelInfo> element = (ListElement<LookAndFeelInfo>) this.cboLookAndFeel.getSelectedItem();
261
               if( element!=null ) {
262
                   changed = element.getValue().getClassName().equalsIgnoreCase(defaultLookAndFeel);
263
               }
264
            }
265
        }
266
        return changed;
267
    }
268

    
269
    @Override
270
    public void setChangesApplied() {
271
        // Do nothing
272
    }
273

    
274
}