svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / databaseworkspace / WorkspaceConfigurationPanel.java @ 44304
History | View | Annotate | Download (12.8 KB)
1 |
package org.gvsig.geodb.databaseworkspace; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.awt.event.ItemEvent; |
7 |
import java.awt.event.ItemListener; |
8 |
import javax.swing.JButton; |
9 |
import javax.swing.JComboBox; |
10 |
import javax.swing.JComponent; |
11 |
import org.apache.commons.lang.StringUtils; |
12 |
import org.gvsig.featureform.swing.JFeaturesForm; |
13 |
import org.gvsig.fmap.dal.DALLocator; |
14 |
import org.gvsig.fmap.dal.DataManager; |
15 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
16 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
17 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
18 |
import org.gvsig.fmap.dal.DataStoreParameters; |
19 |
import org.gvsig.fmap.dal.StoresRepository; |
20 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
21 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
22 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
23 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
24 |
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
25 |
import org.gvsig.geodb.vectorialdb.wizard.VectorialDBConnectionParamsDialog; |
26 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYID; |
27 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYLABEL; |
28 |
import org.gvsig.tools.swing.api.Component; |
29 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
30 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
31 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
32 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
33 |
import org.gvsig.tools.util.LabeledValueImpl; |
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
@SuppressWarnings("UseSpecificCatch") |
42 |
public class WorkspaceConfigurationPanel |
43 |
extends WorkspaceConfigurationPanelView
|
44 |
implements Component |
45 |
{ |
46 |
|
47 |
private static final Logger LOGGER = LoggerFactory.getLogger(WorkspaceConfigurationPanel.class); |
48 |
|
49 |
private class TableController { |
50 |
|
51 |
private final JButton btnCreate; |
52 |
private final JButton btnDrop; |
53 |
private final JButton btnSearch; |
54 |
private final JButton btnShowForm; |
55 |
private final int tableid; |
56 |
|
57 |
public TableController(
|
58 |
int tableid,
|
59 |
JButton btnCreate,
|
60 |
JButton btnDrop,
|
61 |
JButton btnSearch,
|
62 |
JButton btnShowForm
|
63 |
) { |
64 |
this.tableid = tableid;
|
65 |
this.btnCreate = btnCreate;
|
66 |
this.btnDrop = btnDrop;
|
67 |
this.btnSearch = btnSearch;
|
68 |
this.btnShowForm = btnShowForm;
|
69 |
this.btnCreate.addActionListener(new ActionListener() { |
70 |
@Override
|
71 |
public void actionPerformed(ActionEvent e) { |
72 |
doCreate(); |
73 |
} |
74 |
}); |
75 |
this.btnDrop.addActionListener(new ActionListener() { |
76 |
@Override
|
77 |
public void actionPerformed(ActionEvent e) { |
78 |
doDrop(); |
79 |
} |
80 |
}); |
81 |
this.btnShowForm.addActionListener(new ActionListener() { |
82 |
@Override
|
83 |
public void actionPerformed(ActionEvent e) { |
84 |
doShowForm(); |
85 |
} |
86 |
}); |
87 |
this.btnSearch.addActionListener(new ActionListener() { |
88 |
@Override
|
89 |
public void actionPerformed(ActionEvent e) { |
90 |
doSearch(); |
91 |
} |
92 |
}); |
93 |
} |
94 |
|
95 |
private void doCreate() { |
96 |
DataManager dataManager = DALLocator.getDataManager(); |
97 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(getConnection()); |
98 |
if (workspace.existsTable(this.tableid)) { |
99 |
return;
|
100 |
} |
101 |
workspace.createTable(this.tableid);
|
102 |
} |
103 |
|
104 |
private void doShowForm() { |
105 |
try {
|
106 |
DataManager dataManager = DALLocator.getDataManager(); |
107 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(getConnection()); |
108 |
if (!workspace.existsTable(this.tableid)) { |
109 |
return;
|
110 |
} |
111 |
FeatureStore store = workspace.getTable(this.tableid);
|
112 |
DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager(); |
113 |
JFeaturesForm form = dalSwingManager.createJFeaturesForm(store); |
114 |
WindowManager winManager = ToolsSwingLocator.getWindowManager(); |
115 |
winManager.showWindow( |
116 |
form.asJComponent(), |
117 |
"Resources",
|
118 |
WindowManager.MODE.WINDOW |
119 |
); |
120 |
} catch (Exception ex) { |
121 |
LOGGER.warn("Can't show form of resources table.", ex);
|
122 |
} |
123 |
} |
124 |
|
125 |
private void doDrop() { |
126 |
DataManager dataManager = DALLocator.getDataManager(); |
127 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(getConnection()); |
128 |
if (!workspace.existsTable(this.tableid)) { |
129 |
return;
|
130 |
} |
131 |
workspace.dropTable(this.tableid);
|
132 |
} |
133 |
|
134 |
private void doSearch() { |
135 |
try {
|
136 |
DataManager dataManager = DALLocator.getDataManager(); |
137 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(getConnection()); |
138 |
if (!workspace.existsTable(this.tableid)) { |
139 |
return;
|
140 |
} |
141 |
FeatureStore store = workspace.getTable(this.tableid);
|
142 |
DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager(); |
143 |
FeatureStoreSearchPanel search = dalSwingManager.createFeatureStoreSearchPanel(store); |
144 |
WindowManager winManager = ToolsSwingLocator.getWindowManager(); |
145 |
winManager.showWindow( |
146 |
search.asJComponent(), |
147 |
"Resources",
|
148 |
WindowManager.MODE.WINDOW |
149 |
); |
150 |
} catch (Exception ex) { |
151 |
LOGGER.warn("Can't show search panel for resources table.", ex);
|
152 |
} |
153 |
} |
154 |
|
155 |
public void setEnabled(boolean enabled) { |
156 |
this.btnCreate.setEnabled(enabled);
|
157 |
this.btnDrop.setEnabled(enabled);
|
158 |
this.btnSearch.setEnabled(enabled);
|
159 |
this.btnShowForm.setEnabled(enabled);
|
160 |
} |
161 |
} |
162 |
|
163 |
private TableController resourcesTableController;
|
164 |
private TableController repositoryTableController;
|
165 |
private TableController configurationTableController;
|
166 |
|
167 |
public WorkspaceConfigurationPanel() {
|
168 |
this.initComponents();
|
169 |
} |
170 |
|
171 |
private void initComponents() { |
172 |
this.fillConnections(this.cboConnections); |
173 |
this.cboConnections.addItemListener(new ItemListener() { |
174 |
@Override
|
175 |
public void itemStateChanged(ItemEvent e) { |
176 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
177 |
doUpdateEnableComponents(); |
178 |
doUpdateStoresRepositoryInfo(); |
179 |
} |
180 |
} |
181 |
}); |
182 |
this.btnAddConnection.addActionListener(new ActionListener() { |
183 |
@Override
|
184 |
public void actionPerformed(ActionEvent e) { |
185 |
doAddConnection(); |
186 |
} |
187 |
}); |
188 |
this.resourcesTableController = new TableController( |
189 |
DatabaseWorkspaceManager.TABLE_RESOURCES, |
190 |
btnCreateResourceTable, |
191 |
btnDropResourceTable, |
192 |
btnSearchResourceTable, |
193 |
btnShowResourceTable |
194 |
); |
195 |
this.repositoryTableController = new TableController( |
196 |
DatabaseWorkspaceManager.TABLE_REPOSITORY, |
197 |
btnCreateRepositoryTable, |
198 |
btnDropRepositoryTable, |
199 |
btnSearchRepositoryTable, |
200 |
btnShowRepositoryTable |
201 |
); |
202 |
this.configurationTableController = new TableController( |
203 |
DatabaseWorkspaceManager.TABLE_CONFIGURATION, |
204 |
btnCreateConfigurationTable, |
205 |
btnDropConfigurationTable, |
206 |
btnSearchConfigurationTable, |
207 |
btnShowConfigurationTable |
208 |
); |
209 |
|
210 |
this.btnRepositoryAddTable.addActionListener(new ActionListener() { |
211 |
@Override
|
212 |
public void actionPerformed(ActionEvent e) { |
213 |
doAddTableToRepository(); |
214 |
} |
215 |
}); |
216 |
|
217 |
this.btnConnectToRepository.addActionListener(new ActionListener() { |
218 |
@Override
|
219 |
public void actionPerformed(ActionEvent e) { |
220 |
doConnectToRepository(); |
221 |
} |
222 |
}); |
223 |
|
224 |
doUpdateEnableComponents(); |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public JComponent asJComponent() { |
229 |
return this; |
230 |
} |
231 |
|
232 |
public JDBCServerExplorerParameters getConnection() {
|
233 |
LabeledValueImpl<JDBCServerExplorerParameters> item = (LabeledValueImpl<JDBCServerExplorerParameters>) this.cboConnections.getSelectedItem();
|
234 |
if (item == null) { |
235 |
return null; |
236 |
} |
237 |
return item.getValue();
|
238 |
} |
239 |
|
240 |
private void fillConnections(JComboBox combo) { |
241 |
DataManager dataManager = DALLocator.getDataManager(); |
242 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
243 |
|
244 |
DataServerExplorerParameters params; |
245 |
combo.removeAllItems(); |
246 |
for (DataServerExplorerPoolEntry entry : pool) {
|
247 |
if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) { |
248 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
249 |
combo.addItem( |
250 |
new LabeledValueImpl<>(entry.getName(), dbParams)
|
251 |
); |
252 |
} |
253 |
} |
254 |
combo.setSelectedIndex(-1);
|
255 |
} |
256 |
|
257 |
private void doAddConnection() { |
258 |
VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
|
259 |
newco.showDialog(); |
260 |
if (newco.isCanceled()) {
|
261 |
return;
|
262 |
} |
263 |
DataManager dataManager = DALLocator.getDataManager(); |
264 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
265 |
|
266 |
pool.add(newco.getConnectionName(), newco.getServerExplorerParameters()); |
267 |
this.fillConnections(this.cboConnections); |
268 |
} |
269 |
|
270 |
private void doUpdateEnableComponents() { |
271 |
boolean theEnabled = this.getConnection() != null; |
272 |
this.repositoryTableController.setEnabled(theEnabled);
|
273 |
this.resourcesTableController.setEnabled(theEnabled);
|
274 |
this.configurationTableController.setEnabled(theEnabled);
|
275 |
this.btnRepositoryAddTable.setEnabled(theEnabled);
|
276 |
this.btnConnectToRepository.setEnabled(theEnabled);
|
277 |
} |
278 |
|
279 |
private void doAddTableToRepository() { |
280 |
final RepositoryAddTablePanel panel = new RepositoryAddTablePanel(); |
281 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
282 |
final Dialog dialog = winManager.createDialog( |
283 |
panel, |
284 |
"Add table to repository",
|
285 |
null,
|
286 |
WindowManager_v2.BUTTONS_OK_CANCEL |
287 |
); |
288 |
dialog.addActionListener(new ActionListener() { |
289 |
@Override
|
290 |
public void actionPerformed(ActionEvent e) { |
291 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
292 |
DataManager dataManager = DALLocator.getDataManager(); |
293 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(getConnection()); |
294 |
String tableName = panel.getTableName();
|
295 |
DataStoreParameters parameters = panel.getDataStoreParameters(); |
296 |
if( !workspace.storesRepositoryWriteEntry(tableName, parameters) ) {
|
297 |
// TODO: mensaje al usuario,no se ha podido crear
|
298 |
} |
299 |
} |
300 |
} |
301 |
}); |
302 |
dialog.show(WindowManager.MODE.WINDOW); |
303 |
} |
304 |
|
305 |
private void doUpdateStoresRepositoryInfo() { |
306 |
DataManager dataManager = DALLocator.getDataManager(); |
307 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(getConnection()); |
308 |
String id = workspace.get(CONFIG_NAME_STORESREPOSITORYID);
|
309 |
String label = workspace.get(CONFIG_NAME_STORESREPOSITORYLABEL);
|
310 |
this.txtStoresRepositoryId.setText(StringUtils.defaultIfBlank(id, "")); |
311 |
this.txtStoresRepositoryLabel.setText(StringUtils.defaultIfBlank(label, "")); |
312 |
|
313 |
this.btnConnectToRepository.setEnabled(!StringUtils.isBlank(id));
|
314 |
} |
315 |
|
316 |
private void doConnectToRepository() { |
317 |
DataManager dataManager = DALLocator.getDataManager(); |
318 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(getConnection()); |
319 |
StoresRepository repo = workspace.getStoresRepository(); |
320 |
dataManager.getStoresRepository().addRepository(repo); |
321 |
} |
322 |
} |