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 / ConnectToDatabaseWorkspacePanel.java @ 44304
History | View | Annotate | Download (4.41 KB)
1 |
package org.gvsig.geodb.databaseworkspace; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.awt.event.ActionListener; |
5 |
import java.awt.event.ItemEvent; |
6 |
import java.awt.event.ItemListener; |
7 |
import javax.swing.JComboBox; |
8 |
import javax.swing.JComponent; |
9 |
import org.gvsig.fmap.dal.DALLocator; |
10 |
import org.gvsig.fmap.dal.DataManager; |
11 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
12 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
13 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
14 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
15 |
import org.gvsig.fmap.dal.StoresRepository; |
16 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
17 |
import org.gvsig.geodb.vectorialdb.wizard.VectorialDBConnectionParamsDialog; |
18 |
import org.gvsig.tools.swing.api.Component; |
19 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
20 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
21 |
import org.gvsig.tools.util.LabeledValueImpl; |
22 |
|
23 |
/**
|
24 |
*
|
25 |
* @author jjdelcerro
|
26 |
*/
|
27 |
public class ConnectToDatabaseWorkspacePanel |
28 |
extends ConnectToDatabaseWorkspacePanelView
|
29 |
implements Component |
30 |
{ |
31 |
|
32 |
private Dialog dialog; |
33 |
|
34 |
public ConnectToDatabaseWorkspacePanel() {
|
35 |
this.initComponents();
|
36 |
} |
37 |
|
38 |
public boolean connect() { |
39 |
JDBCServerExplorerParameters conn = getConnection(); |
40 |
if( conn == null ) { |
41 |
return false; |
42 |
} |
43 |
DataManager dataManager = DALLocator.getDataManager(); |
44 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(conn); |
45 |
StoresRepository repo = workspace.getStoresRepository(); |
46 |
dataManager.getStoresRepository().addRepository(repo); |
47 |
return true; |
48 |
} |
49 |
|
50 |
private void initComponents() { |
51 |
this.fillConnections(this.cboConnection); |
52 |
this.cboConnection.addItemListener(new ItemListener() { |
53 |
@Override
|
54 |
public void itemStateChanged(ItemEvent e) { |
55 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
56 |
doUpdateEnableComponents(); |
57 |
} |
58 |
} |
59 |
}); |
60 |
this.btnConnection.addActionListener(new ActionListener() { |
61 |
@Override
|
62 |
public void actionPerformed(ActionEvent e) { |
63 |
doAddConnection(); |
64 |
} |
65 |
}); |
66 |
doUpdateEnableComponents(); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public JComponent asJComponent() { |
71 |
return this; |
72 |
} |
73 |
|
74 |
public JDBCServerExplorerParameters getConnection() {
|
75 |
LabeledValueImpl<JDBCServerExplorerParameters> item = (LabeledValueImpl<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
|
76 |
if (item == null) { |
77 |
return null; |
78 |
} |
79 |
JDBCServerExplorerParameters conn = item.getValue(); |
80 |
DataManager dataManager = DALLocator.getDataManager(); |
81 |
DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(conn); |
82 |
if( !workspace.isValidStoresRepository() ) {
|
83 |
return null; |
84 |
} |
85 |
return conn;
|
86 |
} |
87 |
|
88 |
private void fillConnections(JComboBox combo) { |
89 |
DataManager dataManager = DALLocator.getDataManager(); |
90 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
91 |
|
92 |
DataServerExplorerParameters params; |
93 |
combo.removeAllItems(); |
94 |
for (DataServerExplorerPoolEntry entry : pool) {
|
95 |
if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) { |
96 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
97 |
combo.addItem( |
98 |
new LabeledValueImpl<>(entry.getName(), dbParams)
|
99 |
); |
100 |
} |
101 |
} |
102 |
combo.setSelectedIndex(-1);
|
103 |
} |
104 |
|
105 |
private void doAddConnection() { |
106 |
VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
|
107 |
newco.showDialog(); |
108 |
if (newco.isCanceled()) {
|
109 |
return;
|
110 |
} |
111 |
DataManager dataManager = DALLocator.getDataManager(); |
112 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
113 |
|
114 |
pool.add(newco.getConnectionName(), newco.getServerExplorerParameters()); |
115 |
this.fillConnections(this.cboConnection); |
116 |
} |
117 |
|
118 |
private void doUpdateEnableComponents() { |
119 |
boolean theEnabled = this.getConnection() != null; |
120 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, theEnabled);
|
121 |
} |
122 |
|
123 |
void setDialog(Dialog dialog) { |
124 |
this.dialog = dialog;
|
125 |
} |
126 |
|
127 |
} |