gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.swing / org.gvsig.online.swing.impl / src / main / java / org / gvsig / online / swing / impl / OnlineWorkingcopyPickerControllerImpl.java @ 9681
History | View | Annotate | Download (8.7 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.online.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.online.lib.api.OnlineLocator; |
44 |
import org.gvsig.online.lib.api.OnlineManager; |
45 |
import org.gvsig.online.swing.api.OnlineWorkspacePickerController; |
46 |
import org.gvsig.tools.dispose.Disposable; |
47 |
import org.gvsig.tools.swing.api.ListElement; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
49 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
50 |
import org.gvsig.tools.util.CompareUtils.NullSafeComparator; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy; |
54 |
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopyDescriptor; |
55 |
|
56 |
/**
|
57 |
*
|
58 |
* @author gvSIG Team
|
59 |
*/
|
60 |
public class OnlineWorkingcopyPickerControllerImpl extends AbstractPickerController<OnlineWorkingcopy> implements OnlineWorkspacePickerController, Disposable { |
61 |
|
62 |
private static final Logger LOGGER = LoggerFactory.getLogger(OnlineWorkingcopyPickerControllerImpl.class); |
63 |
|
64 |
private final JComboBox<OnlineWorkingcopyDescriptor> cboWorkspace; |
65 |
private final JButton btnWorkspace; |
66 |
|
67 |
|
68 |
public static final void selfRegister() { |
69 |
ToolsSwingUtils.registerIcons(OnlineWorkingcopyPickerControllerImpl.class, |
70 |
"/org/gvsig/online/swing/impl/images",
|
71 |
"Online",
|
72 |
new String[] { "picker", "picker-online-workingcopy", "picker-online" } |
73 |
); |
74 |
} |
75 |
|
76 |
public OnlineWorkingcopyPickerControllerImpl(JComboBox cboWorkspaces, JButton btnInitWorkspace) { |
77 |
this.cboWorkspace = cboWorkspaces;
|
78 |
if( btnInitWorkspace==null ) { |
79 |
btnInitWorkspace = new JButton(); |
80 |
} |
81 |
this.btnWorkspace = btnInitWorkspace;
|
82 |
ToolsSwingUtils.configurePickersButton( |
83 |
btnWorkspace, |
84 |
"_Select_a_working_copy",
|
85 |
"picker-online-workingcopy",
|
86 |
(ActionEvent e) -> { doAddConnection(); },
|
87 |
null
|
88 |
); |
89 |
this.fillWorkspaces(this.cboWorkspace); |
90 |
|
91 |
// this.cboWorkspace.setRenderer(new DefaultListCellRenderer() {
|
92 |
// @Override
|
93 |
// public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
94 |
// JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
95 |
// label.setIcon(null);
|
96 |
// try {
|
97 |
// VCSGisWorkspace workspace = (VCSGisWorkspace) value;
|
98 |
// label.setText(workspace.getLabel());
|
99 |
// } catch (Exception ex) {
|
100 |
// // Do nothing, ignore
|
101 |
// }
|
102 |
// return label;
|
103 |
// }
|
104 |
// });
|
105 |
|
106 |
this.cboWorkspace.addItemListener(new ItemListener() { |
107 |
@Override
|
108 |
public void itemStateChanged(ItemEvent e) { |
109 |
if(e.getStateChange()==ItemEvent.SELECTED){ |
110 |
OnlineWorkingcopyDescriptor descriptor = (OnlineWorkingcopyDescriptor) cboWorkspace.getSelectedItem(); |
111 |
LOGGER.info("WorkspacePiccker fireChangeEvent"+Objects.toString(descriptor));
|
112 |
String url = ""; |
113 |
if( descriptor.getExplorerParameters()!=null ) { |
114 |
url = descriptor.getExplorerParameters().getUrl(); |
115 |
} |
116 |
cboWorkspace.setToolTipText(url); |
117 |
SwingUtilities.invokeLater(() -> { fireChangeEvent(new ChangeEvent(e)); }); |
118 |
} |
119 |
} |
120 |
}); |
121 |
} |
122 |
|
123 |
private void doAddConnection() { |
124 |
JDBCConnectionDialog dialog = DALSwingLocator.getSwingManager().createJDBCConectionDialog(); |
125 |
dialog.showDialog(); |
126 |
if (dialog.isCanceled()) {
|
127 |
return;
|
128 |
} |
129 |
DataManager dataManager = DALLocator.getDataManager(); |
130 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
131 |
|
132 |
JDBCServerExplorerParameters connectionParameters = dialog.getServerExplorerParameters(); |
133 |
pool.add(dialog.getConnectionName(), connectionParameters); |
134 |
this.fillWorkspaces(this.cboWorkspace); |
135 |
ListElement.setSelected(cboWorkspace, connectionParameters); |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public OnlineWorkingcopy get() {
|
140 |
OnlineWorkingcopyDescriptor descriptor = (OnlineWorkingcopyDescriptor) this.cboWorkspace.getSelectedItem();
|
141 |
if( descriptor==null ) { |
142 |
return null; |
143 |
} |
144 |
return descriptor.getWorkspace();
|
145 |
} |
146 |
|
147 |
public void setSelectedIndex(int index) { |
148 |
try {
|
149 |
this.cboWorkspace.setSelectedIndex(index);
|
150 |
} catch(Exception ex) { |
151 |
// Do nothing
|
152 |
} |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void set(OnlineWorkingcopy value) { |
157 |
if( value == null ) { |
158 |
this.cboWorkspace.setSelectedIndex(-1); |
159 |
return;
|
160 |
} |
161 |
ComboBoxModel<OnlineWorkingcopyDescriptor> model = this.cboWorkspace.getModel(); |
162 |
for (int i = 0; i < model.getSize(); i++) { |
163 |
OnlineWorkingcopyDescriptor descriptor = model.getElementAt(i); |
164 |
if( descriptor.getCode().equalsIgnoreCase(value.getCode()) ) {
|
165 |
this.cboWorkspace.setSelectedIndex(i);
|
166 |
String url = ""; |
167 |
if( descriptor.getExplorerParameters()!=null ) { |
168 |
url = descriptor.getExplorerParameters().getUrl(); |
169 |
} |
170 |
this.cboWorkspace.setToolTipText(url);
|
171 |
return;
|
172 |
} |
173 |
} |
174 |
this.cboWorkspace.setToolTipText(""); |
175 |
this.cboWorkspace.setSelectedIndex(-1); |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public void coerceAndSet(Object value) { |
180 |
OnlineWorkingcopy workspace; |
181 |
try {
|
182 |
workspace = (OnlineWorkingcopy) value; |
183 |
} catch(Exception ex) { |
184 |
throw new IllegalArgumentException("Can't coerce value to vcsgisWorkspace", ex); |
185 |
} |
186 |
this.set(workspace);
|
187 |
} |
188 |
|
189 |
@Override
|
190 |
public void setEnabled(boolean enabled) { |
191 |
this.cboWorkspace.setEnabled(enabled);
|
192 |
this.btnWorkspace.setEnabled(enabled);
|
193 |
} |
194 |
|
195 |
@Override
|
196 |
public boolean isEnabled() { |
197 |
return this.cboWorkspace.isEnabled(); |
198 |
} |
199 |
|
200 |
private void fillWorkspaces(JComboBox cboWorkspaces) { |
201 |
OnlineManager manager = OnlineLocator.getOnlineManager(); |
202 |
Map<String, OnlineWorkingcopyDescriptor> workspaceDescriptors = manager.getWorkingcopy(); |
203 |
|
204 |
cboWorkspaces.removeAllItems(); |
205 |
List<OnlineWorkingcopyDescriptor> descriptors = new ArrayList<>(workspaceDescriptors.values()); |
206 |
descriptors.sort(new NullSafeComparator<OnlineWorkingcopyDescriptor>() {
|
207 |
@Override
|
208 |
public int safeCompare(OnlineWorkingcopyDescriptor o1, OnlineWorkingcopyDescriptor o2) { |
209 |
return StringUtils.compare(o1.getLabel(),o2.getLabel());
|
210 |
} |
211 |
}); |
212 |
for (OnlineWorkingcopyDescriptor workspaceDescriptor : descriptors) {
|
213 |
cboWorkspaces.addItem(workspaceDescriptor); |
214 |
} |
215 |
cboWorkspaces.setSelectedIndex(-1);
|
216 |
|
217 |
} |
218 |
|
219 |
@Override
|
220 |
public void dispose() { |
221 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
222 |
} |
223 |
|
224 |
} |