Statistics
| Revision:

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 @ 44308

History | View | Annotate | Download (4.56 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 javax.swing.ComboBoxModel;
8
import javax.swing.JButton;
9
import javax.swing.JComboBox;
10
import org.gvsig.fmap.dal.DALLocator;
11
import org.gvsig.fmap.dal.DataManager;
12
import org.gvsig.fmap.dal.DataServerExplorerParameters;
13
import org.gvsig.fmap.dal.DataServerExplorerPool;
14
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry;
15
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
16
import org.gvsig.fmap.dal.swing.DALSwingLocator;
17
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog;
18
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController;
19
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
20
import org.gvsig.tools.util.LabeledValue;
21
import org.gvsig.tools.util.LabeledValueImpl;
22

    
23
/**
24
 *
25
 * @author jjdelcerro
26
 */
27
public class JDBCConnectionPickerController 
28
        extends AbstractPickerController<JDBCServerExplorerParameters>
29
        implements PickerController<JDBCServerExplorerParameters>
30
    {
31

    
32
    private final JComboBox cboConnection;
33
    private final JButton btnConnection;
34

    
35
    public JDBCConnectionPickerController(JComboBox cboConnection, JButton btnConnection) {
36
        this.btnConnection = btnConnection;
37
        this.cboConnection = cboConnection;
38
    
39
        this.fillConnections(this.cboConnection);
40
        this.btnConnection.addActionListener(new ActionListener() {
41
            @Override
42
            public void actionPerformed(ActionEvent e) {
43
                doAddConnection();
44
            }
45
        });
46
        this.cboConnection.addItemListener(new ItemListener() {
47
            @Override
48
            public void itemStateChanged(ItemEvent e) {
49
                if (e.getStateChange() == ItemEvent.SELECTED) {
50
                    fireChangeEvent();
51
                }
52
            }
53
        });
54
    }
55
    
56
    private void fillConnections(JComboBox combo) {
57
        DataManager dataManager = DALLocator.getDataManager();
58
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
59

    
60
        DataServerExplorerParameters params;
61
        combo.removeAllItems();
62
        for (DataServerExplorerPoolEntry entry : pool) {
63
            if (entry.getExplorerParameters() instanceof JDBCServerExplorerParameters) {
64
                JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters();
65
                combo.addItem(
66
                        new LabeledValueImpl<>(entry.getName(), dbParams)
67
                );
68
            }
69
        }
70
        combo.setSelectedIndex(-1);
71
    }
72

    
73
    private void doAddConnection() {
74
        JDBCConnectionDialog newco = DALSwingLocator.getSwingManager().createJDBCConectionDialog();
75
        newco.showDialog();
76
        if (newco.isCanceled()) {
77
            return;
78
        }
79
        DataManager dataManager = DALLocator.getDataManager();
80
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
81

    
82
        pool.add(newco.getConnectionName(), newco.getServerExplorerParameters());
83
        this.fillConnections(this.cboConnection);
84
    }
85
    
86
    @Override
87
    public JDBCServerExplorerParameters get() {
88
        LabeledValueImpl<JDBCServerExplorerParameters> item = (LabeledValueImpl<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
89
        if (item == null) {
90
            return null;
91
        }
92
        JDBCServerExplorerParameters conn = item.getValue();
93
        return conn;
94
    }
95

    
96
    @Override
97
    public void set(JDBCServerExplorerParameters value) {
98
        ComboBoxModel<LabeledValue<JDBCServerExplorerParameters>> model = this.cboConnection.getModel();
99
        for (int i = 0; i < model.getSize(); i++) {
100
            JDBCServerExplorerParameters params = model.getElementAt(i).getValue();
101
            if( params == value ) {
102
                this.cboConnection.setSelectedIndex(i);
103
                return;
104
            }
105
        }
106
    }
107

    
108
    @Override
109
    public void coerceAndSet(Object value) {
110
        JDBCServerExplorerParameters params;
111
        try {
112
            params = (JDBCServerExplorerParameters) value;
113
        } catch(Exception ex) {
114
            throw new IllegalArgumentException("Can't coerce value to JDBCServerExplorerParameters", ex);
115
        }
116
        this.set(params);
117
    }
118

    
119
    @Override
120
    public void setEnabled(boolean enabled) {
121
        this.cboConnection.setEnabled(enabled);
122
        this.btnConnection.setEnabled(enabled);
123
    }
124

    
125
    @Override
126
    public boolean isEnabled() {
127
        return this.cboConnection.isEnabled();
128
    }
129
    
130
}