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

History | View | Annotate | Download (5.59 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 javax.swing.SwingUtilities;
12
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
import org.gvsig.tools.swing.api.ListElement;
21
import org.gvsig.tools.swing.api.ToolsSwingLocator;
22
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController;
23
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
24
import org.gvsig.tools.swing.icontheme.IconTheme;
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
                    SwingUtilities.invokeLater(() -> { 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 ListElement<>(entry.getName(), dbParams)
76
                );
77
            }
78
        }
79
        combo.setSelectedIndex(-1);
80
    }
81

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

    
91
        JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters();
92
        pool.add(dialog.getConnectionName(), connectionParameters);
93
        this.fillConnections(this.cboConnection);
94
        ListElement.setSelected(cboConnection, connectionParameters);
95
    }
96
    
97
    @Override
98
    public JDBCServerExplorerParameters get() {
99
        ListElement<JDBCServerExplorerParameters> item = (ListElement<JDBCServerExplorerParameters>) this.cboConnection.getSelectedItem();
100
        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
        ComboBoxModel<ListElement<JDBCServerExplorerParameters>> model = this.cboConnection.getModel();
110
        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
    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
}