gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.swing / org.gvsig.vcsgis.swing.impl / src / main / java / org / gvsig / vcsgis / swing / impl / VCSWorkspacePickerControllerImpl.java @ 8713
History | View | Annotate | Download (8.44 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
package org.gvsig.vcsgis.swing.impl; |
23 |
|
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ItemEvent; |
26 |
import java.awt.event.ItemListener; |
27 |
import java.util.ArrayList; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.Objects; |
31 |
import javax.swing.ComboBoxModel; |
32 |
import javax.swing.JButton; |
33 |
import javax.swing.JComboBox; |
34 |
import javax.swing.SwingUtilities; |
35 |
import javax.swing.event.ChangeEvent; |
36 |
import org.apache.commons.lang3.StringUtils; |
37 |
import org.gvsig.fmap.dal.DALLocator; |
38 |
import org.gvsig.fmap.dal.DataManager; |
39 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
40 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
41 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
42 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionDialog; |
43 |
import org.gvsig.tools.dispose.Disposable; |
44 |
import org.gvsig.tools.swing.api.ListElement; |
45 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
46 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
47 |
import org.gvsig.tools.util.CompareUtils.NullSafeComparator; |
48 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
49 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
50 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
51 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceDescriptor; |
52 |
import org.gvsig.vcsgis.swing.VCSWorkspacePickerController; |
53 |
import org.slf4j.Logger; |
54 |
import org.slf4j.LoggerFactory; |
55 |
|
56 |
/**
|
57 |
*
|
58 |
* @author gvSIG Team
|
59 |
*/
|
60 |
public class VCSWorkspacePickerControllerImpl extends AbstractPickerController<VCSGisWorkspace> implements VCSWorkspacePickerController, Disposable { |
61 |
|
62 |
private static final Logger LOGGER = LoggerFactory.getLogger(VCSWorkspacePickerControllerImpl.class); |
63 |
|
64 |
private final JComboBox<VCSGisWorkspaceDescriptor> cboWorkspace; |
65 |
private final JButton btnWorkspace; |
66 |
|
67 |
|
68 |
public static final void selfRegister() { |
69 |
ToolsSwingUtils.registerIcons( |
70 |
VCSWorkspacePickerControllerImpl.class, |
71 |
"/org/gvsig/vcsgis/swing/impl/images",
|
72 |
"VCSGis",
|
73 |
new String[] { "picker", "picker-vcsgis-workingcopy", "picker-vcsgis" } |
74 |
); |
75 |
} |
76 |
|
77 |
public VCSWorkspacePickerControllerImpl(JComboBox cboWorkspaces, JButton btnInitWorkspace) { |
78 |
this.cboWorkspace = cboWorkspaces;
|
79 |
if( btnInitWorkspace==null ) { |
80 |
btnInitWorkspace = new JButton(); |
81 |
} |
82 |
this.btnWorkspace = btnInitWorkspace;
|
83 |
ToolsSwingUtils.configurePickersButton( |
84 |
btnWorkspace, |
85 |
"_Select_a_working_copy",
|
86 |
"picker-vcsgis-workingcopy",
|
87 |
(ActionEvent e) -> { doAddConnection(); },
|
88 |
null
|
89 |
); |
90 |
this.fillWorkspaces(this.cboWorkspace); |
91 |
|
92 |
// this.cboWorkspace.setRenderer(new DefaultListCellRenderer() {
|
93 |
// @Override
|
94 |
// public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
95 |
// JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
96 |
// label.setIcon(null);
|
97 |
// try {
|
98 |
// VCSGisWorkspace workspace = (VCSGisWorkspace) value;
|
99 |
// label.setText(workspace.getLabel());
|
100 |
// } catch (Exception ex) {
|
101 |
// // Do nothing, ignore
|
102 |
// }
|
103 |
// return label;
|
104 |
// }
|
105 |
// });
|
106 |
|
107 |
this.cboWorkspace.addItemListener(new ItemListener() { |
108 |
@Override
|
109 |
public void itemStateChanged(ItemEvent e) { |
110 |
if(e.getStateChange()==ItemEvent.SELECTED){ |
111 |
VCSGisWorkspaceDescriptor descriptor = (VCSGisWorkspaceDescriptor) cboWorkspace.getSelectedItem(); |
112 |
LOGGER.info("WorkspacePiccker fireChangeEvent"+Objects.toString(descriptor));
|
113 |
String url = ""; |
114 |
if( descriptor.getExplorerParameters()!=null ) { |
115 |
url = descriptor.getExplorerParameters().getUrl(); |
116 |
} |
117 |
cboWorkspace.setToolTipText(url); |
118 |
SwingUtilities.invokeLater(() -> { fireChangeEvent(new ChangeEvent(e)); }); |
119 |
} |
120 |
} |
121 |
}); |
122 |
} |
123 |
|
124 |
private void doAddConnection() { |
125 |
JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog(); |
126 |
dialog.showDialog(); |
127 |
if (dialog.isCanceled()) {
|
128 |
return;
|
129 |
} |
130 |
DataManager dataManager = DALLocator.getDataManager(); |
131 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
132 |
|
133 |
JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters(); |
134 |
pool.add(dialog.getConnectionName(), connectionParameters); |
135 |
this.fillWorkspaces(this.cboWorkspace); |
136 |
ListElement.setSelected(cboWorkspace, connectionParameters); |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public VCSGisWorkspace get() {
|
141 |
VCSGisWorkspaceDescriptor descriptor = (VCSGisWorkspaceDescriptor) this.cboWorkspace.getSelectedItem();
|
142 |
if( descriptor==null ) { |
143 |
return null; |
144 |
} |
145 |
return descriptor.getWorkspace();
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public void set(VCSGisWorkspace value) { |
150 |
if( value == null ) { |
151 |
this.cboWorkspace.setSelectedIndex(-1); |
152 |
return;
|
153 |
} |
154 |
ComboBoxModel<VCSGisWorkspaceDescriptor> model = this.cboWorkspace.getModel(); |
155 |
for (int i = 0; i < model.getSize(); i++) { |
156 |
VCSGisWorkspaceDescriptor descriptor = model.getElementAt(i); |
157 |
if( descriptor.getCode().equalsIgnoreCase(value.getCode()) ) {
|
158 |
this.cboWorkspace.setSelectedIndex(i);
|
159 |
String url = ""; |
160 |
if( descriptor.getExplorerParameters()!=null ) { |
161 |
url = descriptor.getExplorerParameters().getUrl(); |
162 |
} |
163 |
this.cboWorkspace.setToolTipText(url);
|
164 |
return;
|
165 |
} |
166 |
} |
167 |
this.cboWorkspace.setToolTipText(""); |
168 |
this.cboWorkspace.setSelectedIndex(-1); |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public void coerceAndSet(Object value) { |
173 |
VCSGisWorkspace workspace; |
174 |
try {
|
175 |
workspace = (VCSGisWorkspace) value; |
176 |
} catch(Exception ex) { |
177 |
throw new IllegalArgumentException("Can't coerce value to vcsgisWorkspace", ex); |
178 |
} |
179 |
this.set(workspace);
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public void setEnabled(boolean enabled) { |
184 |
this.cboWorkspace.setEnabled(enabled);
|
185 |
this.btnWorkspace.setEnabled(enabled);
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public boolean isEnabled() { |
190 |
return this.cboWorkspace.isEnabled(); |
191 |
} |
192 |
|
193 |
private void fillWorkspaces(JComboBox cboWorkspaces) { |
194 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
195 |
Map<String, VCSGisWorkspaceDescriptor> workspaceDescriptors = manager.getWorkspaces(); |
196 |
|
197 |
cboWorkspaces.removeAllItems(); |
198 |
List<VCSGisWorkspaceDescriptor> descriptors = new ArrayList<>(workspaceDescriptors.values()); |
199 |
descriptors.sort(new NullSafeComparator<VCSGisWorkspaceDescriptor>() {
|
200 |
@Override
|
201 |
public int safeCompare(VCSGisWorkspaceDescriptor o1, VCSGisWorkspaceDescriptor o2) { |
202 |
return StringUtils.compare(o1.getLabel(),o2.getLabel());
|
203 |
} |
204 |
}); |
205 |
for (VCSGisWorkspaceDescriptor workspaceDescriptor : descriptors) {
|
206 |
cboWorkspaces.addItem(workspaceDescriptor); |
207 |
} |
208 |
cboWorkspaces.setSelectedIndex(-1);
|
209 |
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public void dispose() { |
214 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
215 |
} |
216 |
|
217 |
} |