Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ShalomExtension.java @ 28960

History | View | Annotate | Download (5.49 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2008 Software Colaborativo S.L.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 * www.scolab.es
22
 */
23
package com.iver.cit.gvsig;
24

    
25
import java.awt.Component;
26
import java.io.File;
27
import java.io.IOException;
28
import java.io.RandomAccessFile;
29

    
30
import javax.swing.JFileChooser;
31
import javax.swing.JOptionPane;
32
import javax.swing.filechooser.FileFilter;
33

    
34
import com.iver.andami.PluginServices;
35
import com.iver.andami.plugins.Extension;
36
import com.iver.andami.preferences.IPreference;
37
import com.iver.andami.preferences.IPreferenceExtension;
38
import com.iver.cit.gvsig.About;
39
import com.iver.cit.gvsig.fmap.drivers.dbf.DbfEncodings;
40
import com.iver.cit.gvsig.gui.preferencespage.DbfDefaultEncodingPage;
41
import com.iver.cit.gvsig.gui.preferencespage.ViewPage;
42
import com.iver.utiles.extensionPoints.ExtensionPoints;
43
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
44

    
45

    
46

    
47
/**
48
 * @author Francisco Jos? Pe?arrubia
49
 *
50
 *  Set encoding to dbf files.
51
 *  To install it, you need to copy (overwrite) dbf.jar, fmap.jar, iver-utiles.jar and of course,
52
 *  install this plugin to gvSIG/extensiones.
53
 *
54
 */
55
public class ShalomExtension extends Extension implements IPreferenceExtension {
56
        static File lastDir = null;
57
        private static  DbfDefaultEncodingPage dbfDefaultEncodingPage = new DbfDefaultEncodingPage();
58
        private DbfEncodings dbfEncodings = DbfEncodings.getInstance();
59

    
60
        private class MyOption extends Object {
61
                public int dbfLanguageId;
62
                public String charSetName;
63

    
64
                public MyOption(int aux) {
65
                        dbfLanguageId = aux;
66
                        charSetName = dbfEncodings.getCharsetForDbfId(aux);
67
                }
68

    
69
                @Override
70
                public String toString() {
71
                        return charSetName;
72
                }
73

    
74
        }
75

    
76

    
77

    
78
        /**
79
         * @see com.iver.andami.plugins.Extension#inicializar()
80
         */
81
        public void initialize() {
82
        About classAbout = (About) PluginServices.getExtension(com.iver.cit.gvsig.About.class);
83
        java.net.URL aboutURL2 = ShalomExtension.class.getResource(
84
        "/about.htm");
85

    
86
        classAbout.getAboutPanel().addAboutUrl("Shalom", aboutURL2);
87

    
88

    
89
        // TODO: To use in future releases.
90
//        ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
91
//
92
//        extensionPoints.add("AplicationPreferences","EncodingsPage", new DbfDefaultEncodingPage());
93

    
94

    
95
        }
96

    
97
        /**
98
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
99
         */
100
        public void execute(String actionCommand) {
101
                JFileChooser fc = new JFileChooser();
102
                fc.setFileFilter(new FileFilter() {
103

    
104
                        @Override
105
                        public boolean accept(File f) {
106
                                if (f.isDirectory())
107
                                        return true;
108
                                if (f.canWrite())
109
                                        if (f.getName().toLowerCase().endsWith(".dbf"))
110
                                                return true;
111
                                return false;
112
                        }
113

    
114
                        @Override
115
                        public String getDescription() {
116
                                return "Dbf files (*.dbf)";
117
                        }
118

    
119
                });
120
                if (lastDir != null)
121
                        fc.setCurrentDirectory(lastDir);
122
                fc.setMultiSelectionEnabled(true);
123

    
124
                int res = fc.showOpenDialog((Component)PluginServices.getMainFrame());
125
                if (res == JFileChooser.APPROVE_OPTION)
126
                {
127
                        lastDir = fc.getCurrentDirectory();
128
                        int dbfEncoding = getSelectedDbfEncoding();
129
                        if (dbfEncoding == -1)
130
                                return;
131
                        File[] files = fc.getSelectedFiles();
132
                        for (int i=0; i < files.length; i++) {
133
                                try {
134
                                        setSelectedDbfEncoding(files[i], dbfEncoding);
135
                                } catch (IOException e) {
136
                                        // TODO Auto-generated catch block
137
                                        e.printStackTrace();
138
                                }
139
                        }
140
                }
141
        }
142

    
143
        private int getSelectedDbfEncoding() {
144

    
145
                MyOption[] myOptions = new MyOption[dbfEncodings.getSupportedDbfLanguageIDs().length];
146
                for (int i=0; i < dbfEncodings.getSupportedDbfLanguageIDs().length; i++)
147
                {
148
                        int aux = dbfEncodings.getSupportedDbfLanguageIDs()[i];
149
                        myOptions[i] = new MyOption(aux);
150
                }
151
                MyOption selectedOption = (MyOption) JOptionPane.showInputDialog((Component) PluginServices.getMainFrame(),
152
                                PluginServices.getText(this, "select_encoding"),
153
                                PluginServices.getText(this, "select_encoding"),
154
                                JOptionPane.OK_CANCEL_OPTION,
155
                                null,
156
                                myOptions, 2);
157
                if (selectedOption == null)
158
                        return -1;
159

    
160
                return selectedOption.dbfLanguageId;
161
        }
162

    
163
        private void setSelectedDbfEncoding(File file, int dbfEncoding) throws IOException {
164
                RandomAccessFile fo = new RandomAccessFile(file, "rw");
165
                fo.seek(29);
166
                fo.writeByte(dbfEncoding);
167
                fo.close();
168

    
169
        }
170

    
171

    
172
        /**
173
         * @see com.iver.andami.plugins.Extension#isEnabled()
174
         */
175
        public boolean isEnabled() {
176
                return true;
177
        }
178

    
179
        /**
180
         * @see com.iver.andami.plugins.Extension#isVisible()
181
         */
182
        public boolean isVisible() {
183
                return true;
184
        }
185

    
186
        public IPreference[] getPreferencesPages() {
187
                IPreference[] preferences=new IPreference[1];
188
                preferences[0] = dbfDefaultEncodingPage;
189
                return preferences;
190
        }
191
}