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

History | View | Annotate | Download (7.02 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.setLayout(new BorderLayout());
87
        this.add(this.preferences, BorderLayout.NORTH);
88
        initializeValues();
89
    }
90

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

    
103
    public void setChangesApplied() {
104
        // ????
105
    }
106

    
107
    public String getID() {
108
        return getClass().getName();
109
    }
110

    
111
    public String getTitle() {
112
        I18nManager i18nManager = ToolsLocator.getI18nManager();
113
        return i18nManager.getTranslation("jCRS_preferences");
114

    
115
    }
116

    
117
    public JPanel getPanel() {
118
        return this;
119
    }
120

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

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

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

    
187
    }
188

    
189
    public ImageIcon getIcon() {
190
        return IconThemeHelper.getImageIcon("jcrs-preferences");
191
    }
192

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

    
211
    public boolean isResizeable() {
212
        return true;
213
    }
214

    
215
    
216
}