gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.swing / org.gvsig.vcsgis.swing.impl / src / main / java / org / gvsig / vcsgis / swing / impl / initworkspace / VCSGisJInitWorkspaceImpl.java @ 2728
History | View | Annotate | Download (11 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (c) 2007-2020 gvSIG Association
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.vcsgis.swing.impl.initworkspace; |
25 |
|
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.FocusEvent; |
29 |
import java.awt.event.FocusListener; |
30 |
import java.io.File; |
31 |
import java.net.URL; |
32 |
import javax.swing.JComponent; |
33 |
import javax.swing.event.ChangeEvent; |
34 |
import javax.swing.event.ChangeListener; |
35 |
import org.apache.commons.io.FilenameUtils; |
36 |
import org.apache.commons.lang3.StringUtils; |
37 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
38 |
import org.gvsig.vcsgis.lib.vcsgis; |
39 |
import org.gvsig.vcsgis.swing.VCSGisJInitWorkspace; |
40 |
import org.gvsig.fmap.dal.DALLocator; |
41 |
import org.gvsig.fmap.dal.DataManager; |
42 |
import org.gvsig.fmap.dal.exception.InitializeException; |
43 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
44 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
45 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
46 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
47 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.dataTypes.CoercionException; |
50 |
import org.gvsig.tools.dataTypes.DataTypes; |
51 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
52 |
import org.gvsig.tools.swing.api.Component; |
53 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
54 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
55 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
56 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
57 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
58 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
59 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
60 |
import org.gvsig.vcsgis.lib.vcsgisadmin; |
61 |
import org.slf4j.LoggerFactory; |
62 |
|
63 |
/**
|
64 |
*
|
65 |
* @author gvSIG Team
|
66 |
*/
|
67 |
public class VCSGisJInitWorkspaceImpl extends VCSGisJInitWorkspaceView |
68 |
implements Component, VCSGisJInitWorkspace { |
69 |
|
70 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJInitWorkspaceImpl.class); |
71 |
|
72 |
private Dialog dialog; |
73 |
|
74 |
private PickerController<JDBCServerExplorerParameters> repositoryServerPicker;
|
75 |
private JDBCServerExplorer repositoryServer;
|
76 |
private VCSGisRepository repository;
|
77 |
|
78 |
private FilePickerController workspaceFilePicker;
|
79 |
private PickerController<JDBCServerExplorerParameters> workspaceServerPicker;
|
80 |
|
81 |
|
82 |
public VCSGisJInitWorkspaceImpl() {
|
83 |
translate(); |
84 |
|
85 |
initializeComponents(); |
86 |
|
87 |
} |
88 |
|
89 |
private void initializeComponents() { |
90 |
this.workspaceServerPicker = DALSwingLocator.getSwingManager().createJDBCConnectionPickerController(
|
91 |
this.cboWorkspaceConnection,
|
92 |
this.btnWorkspaceConnection
|
93 |
); |
94 |
this.repositoryServerPicker = DALSwingLocator.getSwingManager().createJDBCConnectionPickerController(
|
95 |
this.cboRepositoryConnection,
|
96 |
this.btnRepositoryConnection
|
97 |
); |
98 |
this.repositoryServerPicker.addChangeListener((ChangeEvent e) -> { |
99 |
repositoryServer = null;
|
100 |
repository = null;
|
101 |
doUpdateEnableComponents(); |
102 |
}); |
103 |
this.workspaceFilePicker = ToolsSwingLocator.getToolsSwingManager().createFilePickerController(
|
104 |
txtWorkspaceFile, |
105 |
btnWorkspaceFile |
106 |
); |
107 |
this.workspaceFilePicker.addChangeListener((ChangeEvent e) -> { |
108 |
if (StringUtils.isBlank(this.txtName.getText())) { |
109 |
File file = this.workspaceFilePicker.get(); |
110 |
if(file != null){ |
111 |
this.txtName.setText(FilenameUtils.getBaseName(file.getName()));
|
112 |
} |
113 |
} |
114 |
doUpdateEnableComponents(); |
115 |
}); |
116 |
|
117 |
this.txtWorkspaceFile.addFocusListener(new FocusListener() { |
118 |
@Override
|
119 |
public void focusGained(FocusEvent e) { |
120 |
//DO NOTHING
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void focusLost(FocusEvent e) { |
125 |
doUpdateEnableComponents(); |
126 |
} |
127 |
}); |
128 |
|
129 |
this.rdbRepositoryDatabaseServer.addActionListener((ActionEvent e) -> { |
130 |
if(rdbRepositoryDatabaseServer.isSelected()){
|
131 |
cboRepositoryConnection.setEnabled(true);
|
132 |
btnRepositoryConnection.setEnabled(true);
|
133 |
txtRepositoryServiceURL.setEnabled(false);
|
134 |
doUpdateEnableComponents(); |
135 |
} |
136 |
}); |
137 |
|
138 |
this.rdbRepositoryService.addActionListener((ActionEvent e) -> { |
139 |
if(rdbRepositoryService.isSelected()){
|
140 |
cboRepositoryConnection.setEnabled(false);
|
141 |
btnRepositoryConnection.setEnabled(false);
|
142 |
//FIXME: Set to true when implemented
|
143 |
txtRepositoryServiceURL.setEnabled(false);
|
144 |
doUpdateEnableComponents(); |
145 |
} |
146 |
}); |
147 |
|
148 |
this.txtName.addActionListener((ActionEvent e) -> { |
149 |
doUpdateEnableComponents(); |
150 |
}); |
151 |
|
152 |
this.txtName.addFocusListener(new FocusListener() { |
153 |
@Override
|
154 |
public void focusGained(FocusEvent e) { |
155 |
//DO NOTHING
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void focusLost(FocusEvent e) { |
160 |
doUpdateEnableComponents(); |
161 |
} |
162 |
}); |
163 |
|
164 |
this.setPreferredSize(
|
165 |
new Dimension(520, 320) |
166 |
); |
167 |
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public void setDialog(Dialog dialog) { |
172 |
this.dialog = dialog;
|
173 |
this.doUpdateEnableComponents();
|
174 |
} |
175 |
|
176 |
private void doUpdateEnableComponents() { |
177 |
|
178 |
boolean enableInit = (
|
179 |
((rdbRepositoryDatabaseServer.isSelected() && getRepository() != null) ||
|
180 |
(rdbRepositoryService.isSelected() && this.getRepositoryURL() != null)) |
181 |
&& this.getWorkspaceFile() != null |
182 |
&& StringUtils.isNotBlank(this.txtName.getText()));
|
183 |
|
184 |
if (dialog != null) { |
185 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,
|
186 |
enableInit |
187 |
); |
188 |
} |
189 |
} |
190 |
|
191 |
|
192 |
@Override
|
193 |
public JComponent asJComponent() { |
194 |
return this; |
195 |
} |
196 |
|
197 |
private void translate() { |
198 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
199 |
swingManager.translate(this.chkCreateConnectionToDatabase);
|
200 |
swingManager.translate(this.lblName);
|
201 |
swingManager.translate(this.lblRepository);
|
202 |
swingManager.translate(this.lblRepositoryConnection);
|
203 |
swingManager.translate(this.lblRepositoryConnection);
|
204 |
swingManager.translate(this.lblWorkspaceConnection);
|
205 |
swingManager.translate(this.lblWorkspaceFile);
|
206 |
swingManager.translate(this.lblWorkspaceVCS);
|
207 |
swingManager.translate(this.rdbRepositoryDatabaseServer);
|
208 |
swingManager.translate(this.rdbRepositoryService);
|
209 |
swingManager.translate(this.rdbWorkspacePersonalDatabase);
|
210 |
swingManager.translate(this.rdbWorkspaceServerDatabase);
|
211 |
} |
212 |
|
213 |
@Override
|
214 |
public JDBCServerExplorerParameters getRepositoryConnection() {
|
215 |
if(rdbRepositoryDatabaseServer.isEnabled() && repositoryServerPicker.isEnabled()){
|
216 |
return this.repositoryServerPicker.get(); |
217 |
} |
218 |
return null; |
219 |
} |
220 |
|
221 |
@Override
|
222 |
public URL getRepositoryURL() { |
223 |
if(rdbRepositoryService.isEnabled() && txtRepositoryServiceURL.isEnabled()){
|
224 |
String value = this.txtRepositoryServiceURL.getText(); |
225 |
|
226 |
URL url;
|
227 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
228 |
try {
|
229 |
url = (URL) manager.coerce(DataTypes.URL, value);
|
230 |
} catch (CoercionException ex) {
|
231 |
String s = (value==null)? "null":StringUtils.abbreviate(value, 100); |
232 |
LOGGER.warn("Can't coerce value ("+s+") to "+DataTypes.URL_NAME+".", ex); |
233 |
return null; |
234 |
} |
235 |
|
236 |
return url;
|
237 |
} |
238 |
return null; |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public File getWorkspaceFile() { |
243 |
File file = this.workspaceFilePicker.get(); |
244 |
return file;
|
245 |
} |
246 |
|
247 |
@Override
|
248 |
public int initializeWorkspace() { |
249 |
File file = this.getWorkspaceFile(); |
250 |
if (file != null) { |
251 |
if(rdbRepositoryDatabaseServer.isSelected()){
|
252 |
VCSGisRepository theRepository = getRepository(); |
253 |
if (theRepository != null) { |
254 |
return vcsgis.init_workspace(file, theRepository, getName());
|
255 |
} |
256 |
if( this.chkCreateConnectionToDatabase.isSelected() ) { |
257 |
VCSGisWorkspace ws = vcsgis.open_workspace(file); |
258 |
JDBCServerExplorerParameters params = ws.getExplorerParameters(); |
259 |
DataManager dataManager = DALLocator.getDataManager(); |
260 |
dataManager.getDataServerExplorerPool().add(this.getName(), params);
|
261 |
} |
262 |
} else if (rdbRepositoryService.isSelected()) { |
263 |
URL url = this.getRepositoryURL(); |
264 |
//TODO:
|
265 |
return 0; |
266 |
} |
267 |
} |
268 |
return -1; |
269 |
} |
270 |
|
271 |
@Override
|
272 |
public VCSGisRepository getRepository() {
|
273 |
JDBCServerExplorer server = this.getRepositoryServer();
|
274 |
if (repository == null && server != null){ |
275 |
repository = vcsgisadmin.open_repository(server); |
276 |
} |
277 |
return repository;
|
278 |
} |
279 |
|
280 |
@Override
|
281 |
public JDBCServerExplorer getRepositoryServer() {
|
282 |
JDBCServerExplorerParameters conn = getRepositoryConnection(); |
283 |
if(this.repositoryServer == null && conn != null){ |
284 |
try {
|
285 |
DataManager dataManager = DALLocator.getDataManager(); |
286 |
this.repositoryServer = (JDBCServerExplorer) dataManager.openServerExplorer(
|
287 |
conn.getExplorerName(), conn |
288 |
); |
289 |
} catch (InitializeException | ProviderNotRegisteredException | ValidateDataParametersException ex) {
|
290 |
LOGGER.warn("Can't open server "+conn.getExplorerName());
|
291 |
this.repositoryServer = null; |
292 |
} |
293 |
} |
294 |
return this.repositoryServer; |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public String getWorkspaceName() { |
299 |
return txtName.getText();
|
300 |
} |
301 |
|
302 |
} |