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 | 44308 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.jdbc; |
---|---|---|---|
2 | |||
3 | 47284 | jjdelcerro | import org.gvsig.fmap.dal.swing.jdbc.JDBCServerExplorerParametersController; |
4 | 44308 | jjdelcerro | import java.awt.event.ActionEvent; |
5 | import java.awt.event.ItemEvent; |
||
6 | 45499 | jjdelcerro | import java.util.ArrayList; |
7 | import java.util.List; |
||
8 | 44308 | jjdelcerro | import javax.swing.ComboBoxModel; |
9 | import javax.swing.JButton; |
||
10 | import javax.swing.JComboBox; |
||
11 | 45060 | jjdelcerro | import javax.swing.SwingUtilities; |
12 | 45499 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
13 | 44308 | jjdelcerro | 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 | 46863 | jjdelcerro | import static org.gvsig.fmap.dal.swing.impl.DefaultDALSwingLibrary.LIBRARY_NAME; |
21 | 44308 | jjdelcerro | import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog; |
22 | 44591 | jjdelcerro | import org.gvsig.tools.swing.api.ListElement; |
23 | 44533 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
24 | 46863 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingUtils; |
25 | 44308 | jjdelcerro | import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
26 | 44533 | jjdelcerro | import org.gvsig.tools.swing.icontheme.IconTheme; |
27 | 44308 | jjdelcerro | |
28 | /**
|
||
29 | *
|
||
30 | * @author jjdelcerro
|
||
31 | */
|
||
32 | 47284 | jjdelcerro | public class JDBCConnectionPickerControllerImpl |
33 | 44308 | jjdelcerro | extends AbstractPickerController<JDBCServerExplorerParameters>
|
34 | 47284 | jjdelcerro | implements JDBCServerExplorerParametersController
|
35 | 44308 | jjdelcerro | { |
36 | |||
37 | private final JComboBox cboConnection; |
||
38 | private final JButton btnConnection; |
||
39 | |||
40 | 47284 | jjdelcerro | public JDBCConnectionPickerControllerImpl(JComboBox cboConnection, JButton btnConnection) { |
41 | 44308 | jjdelcerro | this.btnConnection = btnConnection;
|
42 | this.cboConnection = cboConnection;
|
||
43 | |||
44 | this.fillConnections(this.cboConnection); |
||
45 | 46601 | fdiaz | this.btnConnection.addActionListener((ActionEvent e) -> { |
46 | doAddConnection(); |
||
47 | 44308 | jjdelcerro | }); |
48 | 44533 | jjdelcerro | if( "...".equals(this.btnConnection.getText()) ) { |
49 | this.btnConnection.setText(""); |
||
50 | } |
||
51 | IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
||
52 | 46863 | jjdelcerro | this.btnConnection.setIcon(theme.get("picker-dbconnection")); |
53 | 44533 | jjdelcerro | |
54 | 46601 | fdiaz | this.cboConnection.addItemListener((ItemEvent e) -> { |
55 | if(!this.isEnabled()){ |
||
56 | return;
|
||
57 | 44308 | jjdelcerro | } |
58 | 46601 | fdiaz | if (e.getStateChange() == ItemEvent.SELECTED) { |
59 | SwingUtilities.invokeLater(() -> { fireChangeEvent(); });
|
||
60 | } |
||
61 | 44308 | jjdelcerro | }); |
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 | 45499 | jjdelcerro | |
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 | 44308 | jjdelcerro | if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) { |
79 | JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
||
80 | combo.addItem( |
||
81 | 44591 | jjdelcerro | new ListElement<>(entry.getName(), dbParams)
|
82 | 44308 | jjdelcerro | ); |
83 | } |
||
84 | } |
||
85 | combo.setSelectedIndex(-1);
|
||
86 | } |
||
87 | |||
88 | private void doAddConnection() { |
||
89 | 44591 | jjdelcerro | JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog(); |
90 | dialog.showDialog(); |
||
91 | if (dialog.isCanceled()) {
|
||
92 | 44308 | jjdelcerro | return;
|
93 | } |
||
94 | 46323 | jjdelcerro | // DataManager dataManager = DALLocator.getDataManager();
|
95 | // DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
|
||
96 | 44308 | jjdelcerro | |
97 | 45060 | jjdelcerro | JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters(); |
98 | 46323 | jjdelcerro | // pool.add(dialog.getConnectionName(), connectionParameters);
|
99 | 44308 | jjdelcerro | this.fillConnections(this.cboConnection); |
100 | 45060 | jjdelcerro | ListElement.setSelected(cboConnection, connectionParameters); |
101 | 44308 | jjdelcerro | } |
102 | |||
103 | @Override
|
||
104 | public JDBCServerExplorerParameters get() {
|
||
105 | 44591 | jjdelcerro | ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
|
106 | 44308 | jjdelcerro | if (item == null) { |
107 | return null; |
||
108 | } |
||
109 | JDBCServerExplorerParameters conn = item.getValue(); |
||
110 | return conn;
|
||
111 | } |
||
112 | |||
113 | @Override
|
||
114 | 47284 | jjdelcerro | 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 | 44308 | jjdelcerro | public void set(JDBCServerExplorerParameters value) { |
125 | 46452 | fdiaz | if(value == null){ |
126 | this.cboConnection.setSelectedIndex(-1); |
||
127 | this.cboConnection.setSelectedItem(null); |
||
128 | return;
|
||
129 | } |
||
130 | 44591 | jjdelcerro | ComboBoxModel<ListElement<JDBCServerExplorerParameters>> model = this.cboConnection.getModel(); |
131 | 44308 | jjdelcerro | 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 | 46601 | fdiaz | 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 | 44308 | jjdelcerro | } |
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 | 44533 | jjdelcerro | public static void selfRegister() { |
175 | 47284 | jjdelcerro | boolean n = ToolsSwingUtils.registerIcons(JDBCConnectionPickerControllerImpl.class,
|
176 | 46863 | jjdelcerro | null,
|
177 | LIBRARY_NAME, |
||
178 | 47426 | jjdelcerro | new String[]{ "picker", "picker-dbconnection", "picker-dbconnection"} |
179 | 46863 | jjdelcerro | ); |
180 | 44533 | jjdelcerro | } |
181 | |||
182 | |||
183 | 44308 | jjdelcerro | } |