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

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

    
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
        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
        this.cboConnection.addItemListener(new ItemListener() {
56
            @Override
57
            public void itemStateChanged(ItemEvent e) {
58
                if (e.getStateChange() == ItemEvent.SELECTED) {
59
                    fireChangeEvent();
60
                }
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
                        new LabeledValueImpl<>(entry.getName(), dbParams)
76
                );
77
            }
78
        }
79
        combo.setSelectedIndex(-1);
80
    }
81

    
82
    private void doAddConnection() {
83
        JDBCConnectionDialog newco = DALSwingLocator.getSwingManager().createJDBCConectionDialog();
84
        newco.showDialog();
85
        if (newco.isCanceled()) {
86
            return;
87
        }
88
        DataManager dataManager = DALLocator.getDataManager();
89
        DataServerExplorerPool pool = dataManager.getDataServerExplorerPool();
90

    
91
        pool.add(newco.getConnectionName(), newco.getServerExplorerParameters());
92
        this.fillConnections(this.cboConnection);
93
    }
94
    
95
    @Override
96
    public JDBCServerExplorerParameters get() {
97
        LabeledValueImpl<JDBCServerExplorerParameters> item = (LabeledValueImpl<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
98
        if (item == null) {
99
            return null;
100
        }
101
        JDBCServerExplorerParameters conn = item.getValue();
102
        return conn;
103
    }
104

    
105
    @Override
106
    public void set(JDBCServerExplorerParameters value) {
107
        ComboBoxModel<LabeledValue<JDBCServerExplorerParameters>> model = this.cboConnection.getModel();
108
        for (int i = 0; i < model.getSize(); i++) {
109
            JDBCServerExplorerParameters params = model.getElementAt(i).getValue();
110
            if( params == value ) {
111
                this.cboConnection.setSelectedIndex(i);
112
                return;
113
            }
114
        }
115
    }
116

    
117
    @Override
118
    public void coerceAndSet(Object value) {
119
        JDBCServerExplorerParameters params;
120
        try {
121
            params = (JDBCServerExplorerParameters) value;
122
        } catch(Exception ex) {
123
            throw new IllegalArgumentException("Can't coerce value to JDBCServerExplorerParameters", ex);
124
        }
125
        this.set(params);
126
    }
127

    
128
    @Override
129
    public void setEnabled(boolean enabled) {
130
        this.cboConnection.setEnabled(enabled);
131
        this.btnConnection.setEnabled(enabled);
132
    }
133

    
134
    @Override
135
    public boolean isEnabled() {
136
        return this.cboConnection.isEnabled();
137
    }
138
    
139
    public static void selfRegister() {
140
        String[][] iconNames = new String[][]{
141
            new String[]{"dalswing", "database-connection-add"}
142
        };
143
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
144
        for (String[] icon : iconNames) {
145
            URL url = JDBCConnectionPickerController.class.getResource(icon[1] + ".png");
146
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
147
        }
148
        
149
    }
150
    
151
    
152
}