svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial.app / org.gvsig.h2spatial.app.mainplugin / src / main / java / org / gvsig / h2spatial / ImportTable.java @ 43402
History | View | Annotate | Download (6.47 KB)
1 |
|
---|---|
2 |
package org.gvsig.h2spatial; |
3 |
|
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.io.File; |
7 |
import javax.swing.JComboBox; |
8 |
import javax.swing.JFileChooser; |
9 |
import javax.swing.filechooser.FileFilter; |
10 |
import org.apache.commons.io.FilenameUtils; |
11 |
import org.apache.commons.lang3.ArrayUtils; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.fmap.dal.DALLocator; |
14 |
import org.gvsig.fmap.dal.DataManager; |
15 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
16 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
17 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
18 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
19 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
20 |
import org.gvsig.tools.ToolsLocator; |
21 |
import org.gvsig.tools.folders.FoldersManager; |
22 |
import org.gvsig.tools.i18n.I18nManager; |
23 |
import org.gvsig.tools.swing.api.ListElement; |
24 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
25 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
26 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
27 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
28 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
29 |
|
30 |
|
31 |
public class ImportTable extends ImportTableView { |
32 |
|
33 |
private static final long serialVersionUID = 3084071737749916812L; |
34 |
|
35 |
private File file = null; |
36 |
|
37 |
public ImportTable() {
|
38 |
this.initComponents();
|
39 |
} |
40 |
|
41 |
private void initComponents() { |
42 |
loadConnectionCombo(this.cboConnection, null); |
43 |
this.btnConnection.addActionListener(new ActionListener() { |
44 |
@Override
|
45 |
public void actionPerformed(ActionEvent e) { |
46 |
doConnectionExplore(); |
47 |
} |
48 |
}); |
49 |
this.cboFileType.removeAllItems();
|
50 |
this.cboFileType.addItem("GPX"); |
51 |
this.cboFileType.addItem("GeoJSon"); |
52 |
this.cboFileType.addItem("OSM"); |
53 |
this.btnFile.addActionListener(new ActionListener() { |
54 |
@Override
|
55 |
public void actionPerformed(ActionEvent e) { |
56 |
doFileExplore(); |
57 |
} |
58 |
}); |
59 |
|
60 |
} |
61 |
|
62 |
public JDBCServerExplorerParameters getExplorerParameters() {
|
63 |
ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
|
64 |
if( item == null ) { |
65 |
return null; |
66 |
} |
67 |
return item.getValue();
|
68 |
} |
69 |
|
70 |
public String getFileType() { |
71 |
return (String) this.cboFileType.getSelectedItem(); |
72 |
} |
73 |
|
74 |
public File getFile() { |
75 |
return this.file; |
76 |
} |
77 |
|
78 |
public String getTableName() { |
79 |
String s = this.txtTableName.getText(); |
80 |
if( StringUtils.isEmpty(s) ) {
|
81 |
if( this.file == null ) { |
82 |
return null; |
83 |
} |
84 |
return FilenameUtils.getBaseName(this.file.getName()); |
85 |
} |
86 |
return s.trim();
|
87 |
} |
88 |
|
89 |
public boolean getDeleteTables() { |
90 |
return this.chkDeleteTables.isSelected(); |
91 |
} |
92 |
|
93 |
private void loadConnectionCombo(JComboBox combo, String selected) { |
94 |
combo.removeAllItems(); |
95 |
combo.addItem("");
|
96 |
DataManager dataManager = DALLocator.getDataManager(); |
97 |
DataServerExplorerPool explorersPool = dataManager.getDataServerExplorerPool(); |
98 |
for( DataServerExplorerPoolEntry entry : explorersPool) {
|
99 |
if( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
100 |
ListElement<JDBCServerExplorerParameters> item = new ListElement(
|
101 |
entry.getName(), |
102 |
entry.getExplorerParameters() |
103 |
); |
104 |
combo.addItem(item); |
105 |
if( StringUtils.equalsIgnoreCase(selected, item.toString()) ) {
|
106 |
combo.setSelectedItem(item); |
107 |
} |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
private void doConnectionExplore() { |
113 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
114 |
WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
115 |
final JDBCConnectionPanel jdbcServerExplorer = DALSwingLocator.getSwingManager().createJDBCConnectionPanel();
|
116 |
jdbcServerExplorer.setConnectorFilter(new JDBCConnectionPanel.ConnectorFilter() {
|
117 |
@Override
|
118 |
public boolean accept(JDBCServerExplorerParameters parameters) { |
119 |
return StringUtils.equalsIgnoreCase(parameters.getExplorerName(), H2SpatialExtension.DAL_EXPLORER_NAME);
|
120 |
} |
121 |
}); |
122 |
final Dialog dlg = winmanager.createDialog( |
123 |
jdbcServerExplorer.asJComponent(), |
124 |
i18n.getTranslation("_Import_file_in_H2_database"),
|
125 |
i18n.getTranslation("_Import_a_Gpx_Geojson_or_OSM_as_tables_in_a_H2_database"),
|
126 |
WindowManager_v2.BUTTONS_OK_CANCEL |
127 |
); |
128 |
dlg.addActionListener(new ActionListener() { |
129 |
@Override
|
130 |
public void actionPerformed(ActionEvent e) { |
131 |
if( dlg.getAction()==WindowManager_v2.BUTTONS_OK ) {
|
132 |
loadConnectionCombo(cboConnection, jdbcServerExplorer.getConnectionName()); |
133 |
} |
134 |
} |
135 |
}); |
136 |
dlg.show(WindowManager.MODE.WINDOW); |
137 |
} |
138 |
|
139 |
private void doFileExplore() { |
140 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
141 |
FoldersManager foldersManager = ToolsLocator.getFoldersManager(); |
142 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
143 |
File[] files = dialogs.showChooserDialog( |
144 |
i18n.getTranslation("_Select_the_file_to_import"),
|
145 |
JFileChooser.OPEN_DIALOG,
|
146 |
JFileChooser.FILES_ONLY,
|
147 |
false,
|
148 |
foldersManager.get("DataFolder"),
|
149 |
new FileFilter() { |
150 |
@Override
|
151 |
public boolean accept(File f) { |
152 |
if( f.isDirectory() ) {
|
153 |
return true; |
154 |
} |
155 |
return FilenameUtils.isExtension(f.getName().toLowerCase(), new String[] {"gpx", "geojson", "osm"}); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public String getDescription() { |
160 |
return "GPX, GeoJSon and OSM files"; |
161 |
} |
162 |
}, |
163 |
false
|
164 |
); |
165 |
if( !ArrayUtils.isEmpty(files) ) {
|
166 |
this.file = files[0]; |
167 |
this.txtFile.setText(this.file.getAbsolutePath()); |
168 |
if( StringUtils.isEmpty(this.txtTableName.getText()) ) { |
169 |
this.txtTableName.setText(FilenameUtils.getBaseName(this.file.getName())); |
170 |
} |
171 |
} |
172 |
} |
173 |
} |