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 / registrationOfEditingStores / VCSGisJRegistrationOfEditingStoresImpl.java @ 8713
History | View | Annotate | Download (9.84 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.vcsgis.swing.impl.registrationOfEditingStores; |
7 |
|
8 |
import java.awt.Cursor; |
9 |
import java.awt.event.ActionEvent; |
10 |
import java.lang.ref.WeakReference; |
11 |
import java.util.List; |
12 |
import javax.swing.DefaultListModel; |
13 |
import javax.swing.ImageIcon; |
14 |
import javax.swing.JComponent; |
15 |
import javax.swing.ListSelectionModel; |
16 |
import javax.swing.event.ListSelectionEvent; |
17 |
import org.apache.commons.lang3.tuple.Pair; |
18 |
import org.gvsig.fmap.dal.exception.DataException; |
19 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
20 |
import org.gvsig.tools.swing.api.Component; |
21 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
22 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
24 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
25 |
import org.gvsig.tools.util.LabeledValue; |
26 |
import org.gvsig.tools.util.LabeledValueImpl; |
27 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
28 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
29 |
import org.gvsig.vcsgis.swing.VCSGisPanel; |
30 |
import org.gvsig.vcsgis.swing.VCSGisSwingLocator; |
31 |
import org.gvsig.vcsgis.swing.VCSGisSwingServices; |
32 |
import org.gvsig.vcsgis.swing.impl.VCSGisSwingImplLibrary; |
33 |
import org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl; |
34 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_GROUP_NAME; |
35 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_PROVIDER_NAME; |
36 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.registerIcons; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
/**
|
40 |
*
|
41 |
* @author gvSIG Team
|
42 |
*/
|
43 |
@SuppressWarnings("UseSpecificCatch") |
44 |
public class VCSGisJRegistrationOfEditingStoresImpl extends VCSGisJRegistrationOfEditingStoresView implements Component, VCSGisPanel { |
45 |
|
46 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJRegistrationOfEditingStoresImpl.class); |
47 |
|
48 |
private Dialog dialog; |
49 |
|
50 |
public VCSGisJRegistrationOfEditingStoresImpl() {
|
51 |
translate(); |
52 |
initComponents(); |
53 |
} |
54 |
|
55 |
private void initComponents() { |
56 |
this.btnCancelEditing.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
57 |
this.btnFinishEditing.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
58 |
this.btnRelease.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
59 |
this.btnRefresh.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
60 |
|
61 |
this.lstStores.addListSelectionListener((ListSelectionEvent e) -> { |
62 |
doUpdateEnableComponents(); |
63 |
}); |
64 |
|
65 |
this.btnCancelEditing.addActionListener((ActionEvent e) -> { |
66 |
doCancelEditing(); |
67 |
fillEditingStores(); |
68 |
doUpdateEnableComponents(); |
69 |
}); |
70 |
this.btnFinishEditing.addActionListener((ActionEvent e) -> { |
71 |
doFinishEditing(); |
72 |
fillEditingStores(); |
73 |
doUpdateEnableComponents(); |
74 |
}); |
75 |
this.btnRelease.addActionListener((ActionEvent e) -> { |
76 |
doRelease(); |
77 |
fillEditingStores(); |
78 |
doUpdateEnableComponents(); |
79 |
}); |
80 |
this.btnRefresh.addActionListener((ActionEvent e) -> { |
81 |
fillEditingStores(); |
82 |
this.lstStores.getSelectionModel().clearSelection();
|
83 |
doUpdateEnableComponents(); |
84 |
}); |
85 |
|
86 |
fillEditingStores(); |
87 |
doUpdateEnableComponents(); |
88 |
|
89 |
ToolsSwingUtils.ensureRowsCols(this, 11, 60); |
90 |
|
91 |
} |
92 |
|
93 |
private void doRelease() { |
94 |
LabeledValue<FeatureStore> labeledStore = (LabeledValue<FeatureStore>) lstStores.getSelectedValue(); |
95 |
if( labeledStore==null ) { |
96 |
return;
|
97 |
} |
98 |
FeatureStore store = labeledStore.getValue(); |
99 |
if( store == null ) { |
100 |
return;
|
101 |
} |
102 |
if( store.getMode()!=FeatureStore.MODE_QUERY ) {
|
103 |
return;
|
104 |
} |
105 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
106 |
manager.forceRemoveFromEditingStores(store); |
107 |
try {
|
108 |
Thread.sleep(2000); |
109 |
} catch (InterruptedException ex) { |
110 |
|
111 |
} |
112 |
fillEditingStores(); |
113 |
} |
114 |
|
115 |
private void doFinishEditing() { |
116 |
LabeledValue<FeatureStore> labeledStore = (LabeledValue<FeatureStore>) lstStores.getSelectedValue(); |
117 |
if( labeledStore==null ) { |
118 |
return;
|
119 |
} |
120 |
FeatureStore store = labeledStore.getValue(); |
121 |
if( store == null ) { |
122 |
return;
|
123 |
} |
124 |
if( store.getMode()==FeatureStore.MODE_QUERY ) {
|
125 |
return;
|
126 |
} |
127 |
try {
|
128 |
store.finishEditing(); |
129 |
} catch (DataException ex) {
|
130 |
LOGGER.warn("Can't force finish editing of '"+store.getName()+"'.",ex); |
131 |
} |
132 |
try {
|
133 |
Thread.sleep(2000); |
134 |
} catch (InterruptedException ex) { |
135 |
|
136 |
} |
137 |
fillEditingStores(); |
138 |
} |
139 |
|
140 |
private void doCancelEditing() { |
141 |
LabeledValue<FeatureStore> labeledStore = (LabeledValue<FeatureStore>) lstStores.getSelectedValue(); |
142 |
if( labeledStore==null ) { |
143 |
return;
|
144 |
} |
145 |
FeatureStore store = labeledStore.getValue(); |
146 |
if( store == null ) { |
147 |
return;
|
148 |
} |
149 |
if( store.getMode()==FeatureStore.MODE_QUERY ) {
|
150 |
return;
|
151 |
} |
152 |
try {
|
153 |
store.cancelEditing(); |
154 |
} catch (DataException ex) {
|
155 |
LOGGER.warn("Can't force cancel editing of '"+store.getName()+"'.",ex); |
156 |
} |
157 |
try {
|
158 |
Thread.sleep(2000); |
159 |
} catch (InterruptedException ex) { |
160 |
|
161 |
} |
162 |
fillEditingStores(); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public void setDialog(Dialog dialog) { |
167 |
this.dialog = dialog;
|
168 |
this.doUpdateEnableComponents();
|
169 |
} |
170 |
|
171 |
private void doUpdateEnableComponents() { |
172 |
LabeledValue<FeatureStore> labeledStore = (LabeledValue<FeatureStore>) lstStores.getSelectedValue(); |
173 |
FeatureStore store = null;
|
174 |
int mode = FeatureStore.MODE_QUERY;
|
175 |
if( labeledStore!=null ) { |
176 |
store = labeledStore.getValue(); |
177 |
mode = store.getMode(); |
178 |
} |
179 |
this.btnCancelEditing.setEnabled(store!=null && mode!=FeatureStore.MODE_QUERY ); |
180 |
this.btnFinishEditing.setEnabled(store!=null && mode!=FeatureStore.MODE_QUERY ); |
181 |
this.btnRelease.setEnabled(store!=null && mode==FeatureStore.MODE_QUERY ); |
182 |
this.btnRefresh.setEnabled(true); |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public JComponent asJComponent() { |
187 |
return this; |
188 |
} |
189 |
|
190 |
private void translate() { |
191 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
192 |
swingManager.translate(this.btnCancelEditing);
|
193 |
swingManager.translate(this.btnFinishEditing);
|
194 |
swingManager.translate(this.btnRelease);
|
195 |
swingManager.translate(this.btnRefresh);
|
196 |
swingManager.translate(this.lstStores);
|
197 |
} |
198 |
|
199 |
private void fillEditingStores() { |
200 |
|
201 |
int n = this.lstStores.getSelectedIndex(); |
202 |
lstStores.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
203 |
|
204 |
lstStores.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
|
205 |
doUpdateEnableComponents(); |
206 |
}); |
207 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
208 |
VCSGisSwingServices services = VCSGisSwingLocator.getVCSGisSwingManager().getDefaultServices(); |
209 |
List<Pair<WeakReference<FeatureStore>, Integer>> stores_info = manager.getEditingStores(); |
210 |
DefaultListModel<LabeledValue<FeatureStore>> model = new DefaultListModel<>(); |
211 |
for (Pair<WeakReference<FeatureStore>, Integer> store_info : stores_info) { |
212 |
if( store_info == null ) { |
213 |
continue;
|
214 |
} |
215 |
FeatureStore store = store_info.getLeft().get(); |
216 |
int mode = store_info.getRight();
|
217 |
String label;
|
218 |
if( store == null ) { |
219 |
label = "unknown (" + getEditingModeLabel(mode) + ")"; |
220 |
} else {
|
221 |
if( mode == store.getMode() ) {
|
222 |
label = store.getName() + "(" + getEditingModeLabel(mode) + ")"; |
223 |
} else {
|
224 |
label = store.getName() + "(" + getEditingModeLabel(mode) + "/" + getEditingModeLabel(store.getMode()) + ") [*1]"; |
225 |
} |
226 |
if( !services.isInSomeDocument(store) ) {
|
227 |
label = label + "[*2]";
|
228 |
} |
229 |
} |
230 |
model.addElement(new LabeledValueImpl<>(label,store));
|
231 |
} |
232 |
lstStores.setModel(model); |
233 |
try {
|
234 |
if( n<0 ) { |
235 |
n = 0;
|
236 |
} |
237 |
if( n>=this.lstStores.getModel().getSize() ) { |
238 |
n = this.lstStores.getModel().getSize()-1; |
239 |
} |
240 |
this.lstStores.setSelectedIndex(n);
|
241 |
} catch(Exception ex) { |
242 |
LOGGER.debug("",ex);
|
243 |
} |
244 |
} |
245 |
|
246 |
private String getEditingModeLabel(int mode) { |
247 |
switch(mode) {
|
248 |
case FeatureStore.MODE_QUERY:
|
249 |
return "query"; |
250 |
case FeatureStore.MODE_FULLEDIT:
|
251 |
return "full-edit"; |
252 |
case FeatureStore.MODE_PASS_THROUGH:
|
253 |
return "pass-through"; |
254 |
case FeatureStore.MODE_APPEND:
|
255 |
return "append"; |
256 |
default:
|
257 |
return "unknown"; |
258 |
} |
259 |
} |
260 |
|
261 |
@Override
|
262 |
public ImageIcon loadImage(String imageName) { |
263 |
return VCSGisSwingManagerImpl.loadImage(imageName);
|
264 |
} |
265 |
|
266 |
public static void selfRegister() { |
267 |
// ToolsSwingUtils.registerIcons(
|
268 |
// VCSGisSwingImplLibrary.class,
|
269 |
// "/org/gvsig/vcsgis/swing/impl/images",
|
270 |
// ICON_PROVIDER_NAME,
|
271 |
// new String[]{ "preferences", "vcsgis-workspace-add", ICON_GROUP_NAME},
|
272 |
// new String[]{ "preferences", "vcsgis-workspace-remove", ICON_GROUP_NAME}
|
273 |
// );
|
274 |
} |
275 |
|
276 |
@Override
|
277 |
public boolean isProcessing() { |
278 |
return false; |
279 |
} |
280 |
|
281 |
} |