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