svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / jdbc / JDBCConnectionPickerControllerImpl.java @ 47426
History | View | Annotate | Download (6.79 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.jdbc; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCServerExplorerParametersController; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ItemEvent; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
import javax.swing.ComboBoxModel; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JComboBox; |
11 |
import javax.swing.SwingUtilities; |
12 |
import org.apache.commons.lang3.StringUtils; |
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.store.jdbc.JDBCServerExplorerParameters; |
19 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
20 |
import static org.gvsig.fmap.dal.swing.impl.DefaultDALSwingLibrary.LIBRARY_NAME; |
21 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog; |
22 |
import org.gvsig.tools.swing.api.ListElement; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
24 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
25 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
26 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author jjdelcerro
|
31 |
*/
|
32 |
public class JDBCConnectionPickerControllerImpl |
33 |
extends AbstractPickerController<JDBCServerExplorerParameters>
|
34 |
implements JDBCServerExplorerParametersController
|
35 |
{ |
36 |
|
37 |
private final JComboBox cboConnection; |
38 |
private final JButton btnConnection; |
39 |
|
40 |
public JDBCConnectionPickerControllerImpl(JComboBox cboConnection, JButton btnConnection) { |
41 |
this.btnConnection = btnConnection;
|
42 |
this.cboConnection = cboConnection;
|
43 |
|
44 |
this.fillConnections(this.cboConnection); |
45 |
this.btnConnection.addActionListener((ActionEvent e) -> { |
46 |
doAddConnection(); |
47 |
}); |
48 |
if( "...".equals(this.btnConnection.getText()) ) { |
49 |
this.btnConnection.setText(""); |
50 |
} |
51 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
52 |
this.btnConnection.setIcon(theme.get("picker-dbconnection")); |
53 |
|
54 |
this.cboConnection.addItemListener((ItemEvent e) -> { |
55 |
if(!this.isEnabled()){ |
56 |
return;
|
57 |
} |
58 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
59 |
SwingUtilities.invokeLater(() -> { fireChangeEvent(); });
|
60 |
} |
61 |
}); |
62 |
} |
63 |
|
64 |
private void fillConnections(JComboBox combo) { |
65 |
DataManager dataManager = DALLocator.getDataManager(); |
66 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
67 |
|
68 |
DataServerExplorerParameters params; |
69 |
combo.removeAllItems(); |
70 |
|
71 |
List<DataServerExplorerPoolEntry>entries = new ArrayList<>(); |
72 |
pool.iterator().forEachRemaining((DataServerExplorerPoolEntry e) -> {entries.add(e); }); |
73 |
entries.sort((DataServerExplorerPoolEntry o1, DataServerExplorerPoolEntry o2) -> { |
74 |
return StringUtils.compare(o1.getName(), o2.getName());
|
75 |
}); |
76 |
|
77 |
for (DataServerExplorerPoolEntry entry : entries) {
|
78 |
if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) { |
79 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
80 |
combo.addItem( |
81 |
new ListElement<>(entry.getName(), dbParams)
|
82 |
); |
83 |
} |
84 |
} |
85 |
combo.setSelectedIndex(-1);
|
86 |
} |
87 |
|
88 |
private void doAddConnection() { |
89 |
JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog(); |
90 |
dialog.showDialog(); |
91 |
if (dialog.isCanceled()) {
|
92 |
return;
|
93 |
} |
94 |
// DataManager dataManager = DALLocator.getDataManager();
|
95 |
// DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
|
96 |
|
97 |
JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters(); |
98 |
// pool.add(dialog.getConnectionName(), connectionParameters);
|
99 |
this.fillConnections(this.cboConnection); |
100 |
ListElement.setSelected(cboConnection, connectionParameters); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public JDBCServerExplorerParameters get() {
|
105 |
ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
|
106 |
if (item == null) { |
107 |
return null; |
108 |
} |
109 |
JDBCServerExplorerParameters conn = item.getValue(); |
110 |
return conn;
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public String getName() { |
115 |
ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
|
116 |
if (item == null) { |
117 |
return null; |
118 |
} |
119 |
String name = item.getLabel();
|
120 |
return name;
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void set(JDBCServerExplorerParameters value) { |
125 |
if(value == null){ |
126 |
this.cboConnection.setSelectedIndex(-1); |
127 |
this.cboConnection.setSelectedItem(null); |
128 |
return;
|
129 |
} |
130 |
ComboBoxModel<ListElement<JDBCServerExplorerParameters>> model = this.cboConnection.getModel(); |
131 |
for (int i = 0; i < model.getSize(); i++) { |
132 |
JDBCServerExplorerParameters params = model.getElementAt(i).getValue(); |
133 |
if( params == value ) {
|
134 |
this.cboConnection.setSelectedIndex(i);
|
135 |
return;
|
136 |
} |
137 |
} |
138 |
for (int i = 0; i < model.getSize(); i++) { |
139 |
JDBCServerExplorerParameters params = model.getElementAt(i).getValue(); |
140 |
if( StringUtils.equals(params.getUrl(), value.getUrl())){
|
141 |
this.cboConnection.setSelectedIndex(i);
|
142 |
return;
|
143 |
} |
144 |
} |
145 |
//Si llega aqu? a?adimos un nuevo item
|
146 |
cboConnection.addItem( |
147 |
new ListElement<>(value.getHost()+":"+value.getDBName(), value) |
148 |
); |
149 |
this.cboConnection.setSelectedIndex(this.cboConnection.getModel().getSize()-1); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void coerceAndSet(Object value) { |
154 |
JDBCServerExplorerParameters params; |
155 |
try {
|
156 |
params = (JDBCServerExplorerParameters) value; |
157 |
} catch(Exception ex) { |
158 |
throw new IllegalArgumentException("Can't coerce value to JDBCServerExplorerParameters", ex); |
159 |
} |
160 |
this.set(params);
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void setEnabled(boolean enabled) { |
165 |
this.cboConnection.setEnabled(enabled);
|
166 |
this.btnConnection.setEnabled(enabled);
|
167 |
} |
168 |
|
169 |
@Override
|
170 |
public boolean isEnabled() { |
171 |
return this.cboConnection.isEnabled(); |
172 |
} |
173 |
|
174 |
public static void selfRegister() { |
175 |
boolean n = ToolsSwingUtils.registerIcons(JDBCConnectionPickerControllerImpl.class,
|
176 |
null,
|
177 |
LIBRARY_NAME, |
178 |
new String[]{ "picker", "picker-dbconnection", "picker-dbconnection"} |
179 |
); |
180 |
} |
181 |
|
182 |
|
183 |
} |