Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.app.jcrs / org.gvsig.projection.app.jcrs.common / src / main / java / org / gvsig / crs / preferences / JCRSPreferencesPage.java @ 1938

History | View | Annotate | Download (8.38 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.crs.preferences;
7

    
8
import java.awt.BorderLayout;
9
import java.io.File;
10
import java.util.Collection;
11
import java.util.Iterator;
12
import javax.swing.ComboBoxModel;
13
import javax.swing.DefaultComboBoxModel;
14
import javax.swing.DefaultListModel;
15
import javax.swing.ImageIcon;
16
import javax.swing.JPanel;
17
import org.apache.commons.io.FileUtils;
18
import org.apache.commons.io.FilenameUtils;
19
import org.apache.commons.lang3.BooleanUtils;
20
import org.apache.commons.lang3.StringUtils;
21
import org.gvsig.andami.IconThemeHelper;
22
import org.gvsig.andami.PluginServices;
23
import org.gvsig.andami.PluginsLocator;
24
import org.gvsig.andami.PluginsManager;
25
import org.gvsig.andami.preferences.AbstractPreferencePage;
26
import org.gvsig.andami.preferences.StoreException;
27
import org.gvsig.crs.CrsFactory;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dynobject.DynObject;
30
import org.gvsig.tools.exception.BaseException;
31
import org.gvsig.tools.i18n.I18nManager;
32
import org.gvsig.tools.packageutils.PackageInfo;
33
import org.gvsig.tools.packageutils.PackageManager;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
/**
38
 *
39
 * @author usuario
40
 */
41
public class JCRSPreferencesPage extends AbstractPreferencePage {
42

    
43
    private static final Logger logger = LoggerFactory.getLogger(JCRSPreferencesPage.class);
44
    private static final long serialVersionUID = -7838901334080793221L;
45
            
46
    private JCRSPreferencesPageView preferences;
47
    private DynObject pluginProperties;
48
    private PluginServices plugin;
49

    
50
    private class ListItem {
51

    
52
        private PackageInfo pkg;
53
        private File epsgFile;
54

    
55
        ListItem(PackageInfo pkg, File epsgFile) {
56
            this.pkg = pkg;
57
            this.epsgFile = epsgFile;
58
        }
59

    
60
        public File getEPSGFile() {
61
            return this.epsgFile;
62
        }
63
        
64
        public PackageInfo getPackageInfo() {
65
            return this.pkg;
66
        }
67

    
68
        public String toString() {
69
            return this.pkg.getName();
70
        }
71

    
72
    }
73

    
74
    public JCRSPreferencesPage() {
75
        initComponents();
76
    }
77

    
78
    private void initComponents() {
79
        I18nManager i18n = ToolsLocator.getI18nManager();
80
        PluginsManager pluginManager = PluginsLocator.getManager();
81
        this.plugin = pluginManager.getPlugin(this);
82
        this.pluginProperties = this.plugin.getPluginProperties();
83
        
84
        this.preferences = new JCRSPreferencesPageView();
85
        this.preferences.lblHeader.setText( 
86
                i18n.getTranslation("_Select_the_EPSG_data_base_to_use")
87
        );
88
        this.preferences.chkUseMomoryChacheForCRSs.setText(
89
                       i18n.getTranslation("_Use_cache_for_CRSs")
90
        );        
91
        this.setLayout(new BorderLayout());
92
        this.add(this.preferences, BorderLayout.NORTH);
93
        
94
        this.preferences.lblDatabaseInitializationMode.setText( 
95
                i18n.getTranslation("_When_initializing_the_EPSG_database")
96
        );
97
        DefaultComboBoxModel modelCombo = new DefaultComboBoxModel();
98
        modelCombo.addElement(i18n.getTranslation("_On_start_in_background"));
99
        modelCombo.addElement(i18n.getTranslation("_On_start_in_foreground"));
100
        modelCombo.addElement(i18n.getTranslation("_On_first_use"));
101
        this.preferences.cboDatabaseInitializationMode.setModel(modelCombo);
102

    
103
        initializeValues();
104
    }
105

    
106
    public void storeValues() throws StoreException {
107
        ListItem item = (ListItem) this.preferences.lstBBDD.getSelectedValue();
108
        if( item == null ) {
109
            return;
110
        }
111
        this.pluginProperties.setDynValue("epsgDatabase", item.getEPSGFile());
112
        this.pluginProperties.setDynValue("useMemoryCacheForCRSs", 
113
                this.preferences.chkUseMomoryChacheForCRSs.isSelected()
114
        );
115
        this.pluginProperties.setDynValue(
116
            "databaseInitializationMode", 
117
            this.preferences.cboDatabaseInitializationMode.getSelectedIndex()
118
        );
119
        this.plugin.savePluginProperties();
120
    }
121

    
122
    public void setChangesApplied() {
123
        // ????
124
    }
125

    
126
    public String getID() {
127
        return getClass().getName();
128
    }
129

    
130
    public String getTitle() {
131
        I18nManager i18nManager = ToolsLocator.getI18nManager();
132
        return i18nManager.getTranslation("jCRS_preferences");
133

    
134
    }
135

    
136
    public JPanel getPanel() {
137
        return this;
138
    }
139

    
140
    @Override
141
    public void initializeValues() {
142
        PackageManager pkgManager = ToolsLocator.getPackageManager();
143
        
144
        File dbfolder = CrsFactory.getDataBaseFolder();
145
        File epsgFolders = new File(dbfolder,"EPSG");
146
        Collection<File> files = FileUtils.listFiles(epsgFolders, new String[]{"info"}, true);
147
        DefaultListModel model = new DefaultListModel();
148
        
149
        File currentEPSG = getCurrentEPSG();
150

    
151
        int selectedOption = 0;
152
        int n = 0;
153
        Iterator<File> it = files.iterator();
154
        while( it.hasNext() ) {
155
            File f = it.next();
156
            if( f.getName().equals("package.info") ) {
157
                try {
158
                    PackageInfo pkginfo = pkgManager.createPackageInfo(f);
159
                    File epsgdb = new File(f.getParentFile(),"EPSG.sql");
160
                    epsgdb = makeRelative(dbfolder, epsgdb);
161
                    model.addElement(new ListItem(pkginfo, epsgdb));
162
                    if( epsgdb.equals(currentEPSG) ) {
163
                        selectedOption = n;
164
                    }
165
                    n++;
166
                } catch (BaseException ex) {
167
                    logger.warn("Can't load package information from '"+f.getAbsolutePath()+"'.",ex);
168
                }
169
            }
170
        }
171
        this.preferences.lstBBDD.setModel(model);
172
        this.preferences.lstBBDD.setSelectedIndex(selectedOption);
173
        
174
        boolean currentUseChacheForCRSs = BooleanUtils.isTrue(
175
                (Boolean) this.pluginProperties.getDynValue("useMemoryCacheForCRSs")
176
        );
177
        this.preferences.chkUseMomoryChacheForCRSs.setSelected(currentUseChacheForCRSs);
178
        
179
        this.preferences.cboDatabaseInitializationMode.setSelectedIndex( 
180
            (Integer)this.pluginProperties.getDynValue("databaseInitializationMode")
181
        );
182
    }
183
    
184
    private File makeRelative(File parent, File file) {
185
        String parent_path = parent.getAbsolutePath();
186
        if( !parent_path.endsWith(File.separator) ) {
187
            parent_path += File.separator;
188
        }
189
        if( file.getAbsolutePath().startsWith(parent_path) ) {
190
            file = new File(file.getAbsolutePath().substring(parent_path.length()));
191
        }
192
        return file;
193
    }
194

    
195
    private File getCurrentEPSG() {
196
        File dbfolder = CrsFactory.getDataBaseFolder();
197
        File currentEPSG = null;
198
        String s = StringUtils.defaultIfBlank((String) this.pluginProperties.getDynValue("epsgDatabase"),null);
199
        if( s!=null ) {
200
            currentEPSG = new File(s);    
201
            if( !currentEPSG.isAbsolute() ) {
202
                currentEPSG = new File(dbfolder,s);    
203
            }
204
            currentEPSG = makeRelative(dbfolder,currentEPSG);
205
        }
206
        return currentEPSG;
207
    }
208
    
209
    public void initializeDefaults() {
210

    
211
    }
212

    
213
    public ImageIcon getIcon() {
214
        return IconThemeHelper.getImageIcon("jcrs-preferences");
215
    }
216

    
217
    public boolean isValueChanged() {
218
        File currentEPSG = this.getCurrentEPSG();
219
        ListItem item = (ListItem) this.preferences.lstBBDD.getSelectedValue();
220
        if( item == null && currentEPSG == null) {
221
            return false;
222
        }
223
        if( item == null || currentEPSG == null) {
224
            return true;
225
        }
226
        boolean changed = ! currentEPSG.equals(item.getEPSGFile());
227
        if( !changed ) {
228
            boolean currentUseChacheForCRSs = BooleanUtils.isTrue(
229
                (Boolean) this.pluginProperties.getDynValue("useMemoryCacheForCRSs")
230
            );
231
            if( this.preferences.chkUseMomoryChacheForCRSs.isSelected()!=currentUseChacheForCRSs ) {
232
                changed = true;
233
            }
234
        }
235
        if( !changed ) {
236
            int n = this.preferences.cboDatabaseInitializationMode.getSelectedIndex();
237
            changed = n!=(Integer)this.pluginProperties.getDynValue("databaseInitializationMode");
238
        }
239
        return changed;
240
    }
241

    
242
    public boolean isResizeable() {
243
        return true;
244
    }
245

    
246
    
247
}