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 / JDBCConnectionPickerController.java @ 45499
History | View | Annotate | Download (6.17 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.jdbc; |
---|---|
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 java.net.URL; |
8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
10 |
import java.util.function.Consumer; |
11 |
import javax.swing.ComboBoxModel; |
12 |
import javax.swing.JButton; |
13 |
import javax.swing.JComboBox; |
14 |
import javax.swing.SwingUtilities; |
15 |
import org.apache.commons.lang3.StringUtils; |
16 |
import org.gvsig.fmap.dal.DALLocator; |
17 |
import org.gvsig.fmap.dal.DataManager; |
18 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
19 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
20 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
21 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
22 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
23 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog; |
24 |
import org.gvsig.tools.swing.api.ListElement; |
25 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
26 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
27 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
28 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
29 |
import org.gvsig.tools.util.CompareUtils; |
30 |
import org.gvsig.tools.util.CompareUtils.NullSafeComparator; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author jjdelcerro
|
35 |
*/
|
36 |
public class JDBCConnectionPickerController |
37 |
extends AbstractPickerController<JDBCServerExplorerParameters>
|
38 |
implements PickerController<JDBCServerExplorerParameters>
|
39 |
{ |
40 |
|
41 |
private final JComboBox cboConnection; |
42 |
private final JButton btnConnection; |
43 |
|
44 |
public JDBCConnectionPickerController(JComboBox cboConnection, JButton btnConnection) { |
45 |
this.btnConnection = btnConnection;
|
46 |
this.cboConnection = cboConnection;
|
47 |
|
48 |
this.fillConnections(this.cboConnection); |
49 |
this.btnConnection.addActionListener(new ActionListener() { |
50 |
@Override
|
51 |
public void actionPerformed(ActionEvent e) { |
52 |
doAddConnection(); |
53 |
} |
54 |
}); |
55 |
if( "...".equals(this.btnConnection.getText()) ) { |
56 |
this.btnConnection.setText(""); |
57 |
} |
58 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
59 |
this.btnConnection.setIcon(theme.get("database-connection-add")); |
60 |
|
61 |
this.cboConnection.addItemListener(new ItemListener() { |
62 |
@Override
|
63 |
public void itemStateChanged(ItemEvent e) { |
64 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
65 |
SwingUtilities.invokeLater(() -> { fireChangeEvent(); });
|
66 |
} |
67 |
} |
68 |
}); |
69 |
} |
70 |
|
71 |
private void fillConnections(JComboBox combo) { |
72 |
DataManager dataManager = DALLocator.getDataManager(); |
73 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
74 |
|
75 |
DataServerExplorerParameters params; |
76 |
combo.removeAllItems(); |
77 |
|
78 |
List<DataServerExplorerPoolEntry>entries = new ArrayList<>(); |
79 |
pool.iterator().forEachRemaining((DataServerExplorerPoolEntry e) -> {entries.add(e); }); |
80 |
entries.sort((DataServerExplorerPoolEntry o1, DataServerExplorerPoolEntry o2) -> { |
81 |
return StringUtils.compare(o1.getName(), o2.getName());
|
82 |
}); |
83 |
|
84 |
for (DataServerExplorerPoolEntry entry : entries) {
|
85 |
if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) { |
86 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
87 |
combo.addItem( |
88 |
new ListElement<>(entry.getName(), dbParams)
|
89 |
); |
90 |
} |
91 |
} |
92 |
combo.setSelectedIndex(-1);
|
93 |
} |
94 |
|
95 |
private void doAddConnection() { |
96 |
JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog(); |
97 |
dialog.showDialog(); |
98 |
if (dialog.isCanceled()) {
|
99 |
return;
|
100 |
} |
101 |
DataManager dataManager = DALLocator.getDataManager(); |
102 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
103 |
|
104 |
JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters(); |
105 |
pool.add(dialog.getConnectionName(), connectionParameters); |
106 |
this.fillConnections(this.cboConnection); |
107 |
ListElement.setSelected(cboConnection, connectionParameters); |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public JDBCServerExplorerParameters get() {
|
112 |
ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
|
113 |
if (item == null) { |
114 |
return null; |
115 |
} |
116 |
JDBCServerExplorerParameters conn = item.getValue(); |
117 |
return conn;
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void set(JDBCServerExplorerParameters value) { |
122 |
ComboBoxModel<ListElement<JDBCServerExplorerParameters>> model = this.cboConnection.getModel(); |
123 |
for (int i = 0; i < model.getSize(); i++) { |
124 |
JDBCServerExplorerParameters params = model.getElementAt(i).getValue(); |
125 |
if( params == value ) {
|
126 |
this.cboConnection.setSelectedIndex(i);
|
127 |
return;
|
128 |
} |
129 |
} |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public void coerceAndSet(Object value) { |
134 |
JDBCServerExplorerParameters params; |
135 |
try {
|
136 |
params = (JDBCServerExplorerParameters) value; |
137 |
} catch(Exception ex) { |
138 |
throw new IllegalArgumentException("Can't coerce value to JDBCServerExplorerParameters", ex); |
139 |
} |
140 |
this.set(params);
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void setEnabled(boolean enabled) { |
145 |
this.cboConnection.setEnabled(enabled);
|
146 |
this.btnConnection.setEnabled(enabled);
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
public boolean isEnabled() { |
151 |
return this.cboConnection.isEnabled(); |
152 |
} |
153 |
|
154 |
public static void selfRegister() { |
155 |
String[][] iconNames = new String[][]{ |
156 |
new String[]{"dalswing", "database-connection-add"} |
157 |
}; |
158 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
159 |
for (String[] icon : iconNames) { |
160 |
URL url = JDBCConnectionPickerController.class.getResource(icon[1] + ".png"); |
161 |
theme.registerDefault("DALSwing", icon[0], icon[1], null, url); |
162 |
} |
163 |
|
164 |
} |
165 |
|
166 |
|
167 |
} |