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

History | View | Annotate | Download (7.15 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.DefaultListModel;
13
import javax.swing.ImageIcon;
14
import javax.swing.JPanel;
15
import org.apache.commons.io.FileUtils;
16
import org.apache.commons.io.FilenameUtils;
17
import org.apache.commons.lang3.BooleanUtils;
18
import org.apache.commons.lang3.StringUtils;
19
import org.gvsig.andami.IconThemeHelper;
20
import org.gvsig.andami.PluginServices;
21
import org.gvsig.andami.PluginsLocator;
22
import org.gvsig.andami.PluginsManager;
23
import org.gvsig.andami.preferences.AbstractPreferencePage;
24
import org.gvsig.andami.preferences.StoreException;
25
import org.gvsig.crs.CrsFactory;
26
import org.gvsig.tools.ToolsLocator;
27
import org.gvsig.tools.dynobject.DynObject;
28
import org.gvsig.tools.exception.BaseException;
29
import org.gvsig.tools.i18n.I18nManager;
30
import org.gvsig.tools.packageutils.PackageInfo;
31
import org.gvsig.tools.packageutils.PackageManager;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
/**
36
 *
37
 * @author usuario
38
 */
39
public class JCRSPreferencesPage extends AbstractPreferencePage {
40

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

    
48
    private class ListItem {
49

    
50
        private PackageInfo pkg;
51
        private File epsgFile;
52

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

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

    
66
        public String toString() {
67
            return this.pkg.getName();
68
        }
69

    
70
    }
71

    
72
    public JCRSPreferencesPage() {
73
        initComponents();
74
    }
75

    
76
    private void initComponents() {
77
        I18nManager i18nManager = ToolsLocator.getI18nManager();
78
        PluginsManager pluginManager = PluginsLocator.getManager();
79
        this.plugin = pluginManager.getPlugin(this);
80
        this.pluginProperties = this.plugin.getPluginProperties();
81
        
82
        this.preferences = new JCRSPreferencesPanel();
83
        this.preferences.lblHeader.setText( 
84
                i18nManager.getTranslation("_Select_the_EPSG_data_base_to_use")
85
        );
86
        this.preferences.chkUseMomoryChacheForCRSs.setText(
87
                       i18nManager.getTranslation("_Use_cache_for_CRSs")
88
        );        
89
        this.setLayout(new BorderLayout());
90
        this.add(this.preferences, BorderLayout.NORTH);
91
        initializeValues();
92
    }
93

    
94
    public void storeValues() throws StoreException {
95
        ListItem item = (ListItem) this.preferences.lstBBDD.getSelectedValue();
96
        if( item == null ) {
97
            return;
98
        }
99
        this.pluginProperties.setDynValue("epsgDatabase", item.getEPSGFile());
100
        this.pluginProperties.setDynValue("useMemoryCacheForCRSs", 
101
                new Boolean(this.preferences.chkUseMomoryChacheForCRSs.isSelected())
102
        );
103
        this.plugin.savePluginProperties();
104
    }
105

    
106
    public void setChangesApplied() {
107
        // ????
108
    }
109

    
110
    public String getID() {
111
        return getClass().getName();
112
    }
113

    
114
    public String getTitle() {
115
        I18nManager i18nManager = ToolsLocator.getI18nManager();
116
        return i18nManager.getTranslation("jCRS_preferences");
117

    
118
    }
119

    
120
    public JPanel getPanel() {
121
        return this;
122
    }
123

    
124
    public void initializeValues() {
125
        PackageManager pkgManager = ToolsLocator.getPackageManager();
126
        
127
        File dbfolder = CrsFactory.getDataBaseFolder();
128
        File epsgFolders = new File(dbfolder,"EPSG");
129
        Collection<File> files = FileUtils.listFiles(epsgFolders, new String[]{"info"}, true);
130
        DefaultListModel<ListItem> model = new DefaultListModel<ListItem>();
131
        
132
        File currentEPSG = getCurrentEPSG();
133

    
134
        int selectedOption = 0;
135
        int n = 0;
136
        Iterator<File> it = files.iterator();
137
        while( it.hasNext() ) {
138
            File f = it.next();
139
            if( f.getName().equals("package.info") ) {
140
                try {
141
                    PackageInfo pkginfo = pkgManager.createPackageInfo(f);
142
                    File epsgdb = new File(f.getParentFile(),"EPSG.sql");
143
                    epsgdb = makeRelative(dbfolder, epsgdb);
144
                    model.addElement(new ListItem(pkginfo, epsgdb));
145
                    if( epsgdb.equals(currentEPSG) ) {
146
                        selectedOption = n;
147
                    }
148
                    n++;
149
                } catch (BaseException ex) {
150
                    logger.warn("Can't load package information from '"+f.getAbsolutePath()+"'.",ex);
151
                }
152
            }
153
        }
154
        this.preferences.lstBBDD.setModel(model);
155
        this.preferences.lstBBDD.setSelectedIndex(selectedOption);
156
        
157
        boolean currentUseChacheForCRSs = BooleanUtils.isTrue(
158
                (Boolean) this.pluginProperties.getDynValue("useMemoryCacheForCRSs")
159
        );
160
        this.preferences.chkUseMomoryChacheForCRSs.setSelected(currentUseChacheForCRSs);
161
    }
162
    
163
    private File makeRelative(File parent, File file) {
164
        String parent_path = parent.getAbsolutePath();
165
        if( !parent_path.endsWith(File.separator) ) {
166
            parent_path += File.separator;
167
        }
168
        if( file.getAbsolutePath().startsWith(parent_path) ) {
169
            file = new File(file.getAbsolutePath().substring(parent_path.length()));
170
        }
171
        return file;
172
    }
173

    
174
    private File getCurrentEPSG() {
175
        File dbfolder = CrsFactory.getDataBaseFolder();
176
        File currentEPSG = null;
177
        String s = StringUtils.defaultIfBlank((String) this.pluginProperties.getDynValue("epsgDatabase"),null);
178
        if( s!=null ) {
179
            currentEPSG = new File(s);    
180
            if( !currentEPSG.isAbsolute() ) {
181
                currentEPSG = new File(dbfolder,s);    
182
            }
183
            currentEPSG = makeRelative(dbfolder,currentEPSG);
184
        }
185
        return currentEPSG;
186
    }
187
    
188
    public void initializeDefaults() {
189

    
190
    }
191

    
192
    public ImageIcon getIcon() {
193
        return IconThemeHelper.getImageIcon("jcrs-preferences");
194
    }
195

    
196
    public boolean isValueChanged() {
197
        File currentEPSG = this.getCurrentEPSG();
198
        ListItem item = (ListItem) this.preferences.lstBBDD.getSelectedValue();
199
        if( item == null ) {
200
            return false;
201
        }
202
        boolean changed = ! currentEPSG.equals(item.getEPSGFile());
203
        if( !changed ) {
204
            boolean currentUseChacheForCRSs = BooleanUtils.isTrue(
205
                (Boolean) this.pluginProperties.getDynValue("useMemoryCacheForCRSs")
206
            );
207
            if( this.preferences.chkUseMomoryChacheForCRSs.isSelected()!=currentUseChacheForCRSs ) {
208
                changed = true;
209
            }
210
        }
211
        return changed;
212
    }
213

    
214
    public boolean isResizeable() {
215
        return true;
216
    }
217

    
218
    
219
}