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