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 @ 44821
History | View | Annotate | Download (4.48 KB)
1 | 44304 | jjdelcerro | package org.gvsig.geodb.databaseworkspace; |
---|---|---|---|
2 | |||
3 | import javax.swing.JComponent; |
||
4 | 44308 | jjdelcerro | import javax.swing.event.ChangeEvent; |
5 | import javax.swing.event.ChangeListener; |
||
6 | 44305 | jjdelcerro | import org.apache.commons.lang.StringUtils; |
7 | 44304 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
8 | import org.gvsig.fmap.dal.DataManager; |
||
9 | import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
||
10 | 44305 | jjdelcerro | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYID; |
11 | import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYLABEL; |
||
12 | 44304 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
13 | 44308 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
14 | 44304 | jjdelcerro | import org.gvsig.tools.swing.api.Component; |
15 | 44308 | jjdelcerro | import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
16 | 44304 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
17 | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
||
18 | |||
19 | /**
|
||
20 | *
|
||
21 | * @author jjdelcerro
|
||
22 | */
|
||
23 | public class ConnectToDatabaseWorkspacePanel |
||
24 | extends ConnectToDatabaseWorkspacePanelView
|
||
25 | implements Component |
||
26 | { |
||
27 | |||
28 | private Dialog dialog; |
||
29 | 44308 | jjdelcerro | private PickerController<JDBCServerExplorerParameters> connectionPicker;
|
30 | 44304 | jjdelcerro | |
31 | public ConnectToDatabaseWorkspacePanel() {
|
||
32 | this.initComponents();
|
||
33 | } |
||
34 | |||
35 | public boolean connect() { |
||
36 | JDBCServerExplorerParameters conn = getConnection(); |
||
37 | if( conn == null ) { |
||
38 | return false; |
||
39 | } |
||
40 | DataManager dataManager = DALLocator.getDataManager(); |
||
41 | DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(conn); |
||
42 | 44728 | jjdelcerro | workspace.connect(); |
43 | 44304 | jjdelcerro | return true; |
44 | } |
||
45 | |||
46 | 44821 | jjdelcerro | public void disconnect() { |
47 | String id = this.getID(); |
||
48 | DataManager dataManager = DALLocator.getDataManager(); |
||
49 | DatabaseWorkspaceManager workspace = dataManager.getDatabaseWorkspace(id); |
||
50 | if( workspace!=null ) { |
||
51 | workspace.disconnect(); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | 44304 | jjdelcerro | private void initComponents() { |
56 | 44308 | jjdelcerro | this.connectionPicker = DALSwingLocator.getSwingManager().createJDBCConnectionPickerController(
|
57 | this.cboConnection,
|
||
58 | this.btnConnection
|
||
59 | ); |
||
60 | this.connectionPicker.addChangeListener(new ChangeListener() { |
||
61 | 44304 | jjdelcerro | @Override
|
62 | 44308 | jjdelcerro | public void stateChanged(ChangeEvent e) { |
63 | doUpdateStoresRepositoryInfo(); |
||
64 | 44304 | jjdelcerro | } |
65 | }); |
||
66 | 44305 | jjdelcerro | doUpdateStoresRepositoryInfo(); |
67 | 44304 | jjdelcerro | } |
68 | |||
69 | @Override
|
||
70 | public JComponent asJComponent() { |
||
71 | return this; |
||
72 | } |
||
73 | |||
74 | public JDBCServerExplorerParameters getConnection() {
|
||
75 | 44308 | jjdelcerro | JDBCServerExplorerParameters conn = this.connectionPicker.get();
|
76 | if (conn == null) { |
||
77 | 44304 | jjdelcerro | return null; |
78 | } |
||
79 | DataManager dataManager = DALLocator.getDataManager(); |
||
80 | DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(conn); |
||
81 | if( !workspace.isValidStoresRepository() ) {
|
||
82 | return null; |
||
83 | } |
||
84 | return conn;
|
||
85 | } |
||
86 | |||
87 | private void doUpdateEnableComponents() { |
||
88 | 44821 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
89 | |||
90 | 44304 | jjdelcerro | boolean theEnabled = this.getConnection() != null; |
91 | 44305 | jjdelcerro | if( dialog!=null ) { |
92 | 44821 | jjdelcerro | this.dialog.setButtonEnabled(
|
93 | WindowManager_v2.BUTTON_OK, |
||
94 | theEnabled |
||
95 | ); |
||
96 | String id = this.getID(); |
||
97 | this.dialog.setButtonEnabled(
|
||
98 | WindowManager_v2.BUTTON_APPLY, |
||
99 | dataManager.getDatabaseWorkspace(id)!=null
|
||
100 | ); |
||
101 | 44305 | jjdelcerro | } |
102 | 44304 | jjdelcerro | } |
103 | 44821 | jjdelcerro | |
104 | 44305 | jjdelcerro | private void doUpdateStoresRepositoryInfo() { |
105 | 44821 | jjdelcerro | if( this.getConnection()!=null ) { |
106 | DataManager dataManager = DALLocator.getDataManager(); |
||
107 | JDBCServerExplorerParameters conn = getConnection(); |
||
108 | DatabaseWorkspaceManager workspace = dataManager.createDatabaseWorkspaceManager(conn); |
||
109 | String id = workspace.get(CONFIG_NAME_STORESREPOSITORYID);
|
||
110 | String label = workspace.get(CONFIG_NAME_STORESREPOSITORYLABEL);
|
||
111 | this.txtID.setText(StringUtils.defaultIfBlank(id, "")); |
||
112 | this.txtLabel.setText(StringUtils.defaultIfBlank(label, "")); |
||
113 | this.connectionPicker.set(conn);
|
||
114 | } else {
|
||
115 | this.txtID.setText(""); |
||
116 | this.txtLabel.setText(""); |
||
117 | this.connectionPicker.set(null); |
||
118 | 44305 | jjdelcerro | } |
119 | this.doUpdateEnableComponents();
|
||
120 | } |
||
121 | 44304 | jjdelcerro | |
122 | 44821 | jjdelcerro | public String getID() { |
123 | return this.txtID.getText(); |
||
124 | } |
||
125 | 44305 | jjdelcerro | |
126 | 44821 | jjdelcerro | public String getLabel() { |
127 | return this.txtLabel.getText(); |
||
128 | } |
||
129 | |||
130 | 44304 | jjdelcerro | void setDialog(Dialog dialog) { |
131 | this.dialog = dialog;
|
||
132 | 44305 | jjdelcerro | this.doUpdateEnableComponents();
|
133 | 44304 | jjdelcerro | } |
134 | |||
135 | } |