gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.app / org.gvsig.vectorediting.app.mainplugin / src / main / java / org / gvsig / vectorediting / app / mainplugin / MultilayerFinishEditingPanel.java @ 3047
History | View | Annotate | Download (11.5 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.vectorediting.app.mainplugin; |
7 |
|
8 |
import java.awt.Dimension; |
9 |
import java.awt.GridBagConstraints; |
10 |
import java.awt.GridBagLayout; |
11 |
import java.awt.Insets; |
12 |
import java.awt.event.ActionEvent; |
13 |
import java.awt.event.ItemEvent; |
14 |
import java.util.ArrayList; |
15 |
import java.util.Iterator; |
16 |
import java.util.List; |
17 |
import javax.swing.DefaultComboBoxModel; |
18 |
import javax.swing.JButton; |
19 |
import javax.swing.JComponent; |
20 |
import javax.swing.JLabel; |
21 |
import javax.swing.JPanel; |
22 |
import javax.swing.JScrollPane; |
23 |
import org.gvsig.andami.PluginServices; |
24 |
import org.gvsig.export.ExportLocator; |
25 |
import org.gvsig.export.ExportManager; |
26 |
import org.gvsig.export.ExportProcess; |
27 |
import org.gvsig.export.swing.ExportSwingLocator; |
28 |
import org.gvsig.export.swing.ExportSwingManager; |
29 |
import org.gvsig.export.swing.JExportProcessPanel; |
30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.fmap.mapcontext.MapContext; |
32 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
33 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.i18n.I18nManager; |
36 |
import org.gvsig.tools.swing.api.Component; |
37 |
import org.gvsig.tools.swing.api.DropDown; |
38 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
39 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
40 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
41 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
42 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
43 |
import org.gvsig.tools.util.LabeledValue; |
44 |
import org.gvsig.tools.util.LabeledValueImpl; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
/**
|
49 |
*
|
50 |
* @author jovivas
|
51 |
*/
|
52 |
public class MultilayerFinishEditingPanel extends JPanel implements Component { |
53 |
|
54 |
private static final int OPTION_SAVE = 0; |
55 |
private static final int OPTION_DISMISS = 1; |
56 |
private static final int OPTION_CONTINUE = 2; |
57 |
private static final int OPTION_EXPORT = 3; |
58 |
|
59 |
private static final Logger LOGGER = LoggerFactory.getLogger(EditingExtension.class); |
60 |
private Dialog dialog; |
61 |
|
62 |
private static class Row { |
63 |
|
64 |
private final FLyrVect layer; |
65 |
private final DropDown dropDownOptions; |
66 |
private final JButton jButtonExport; |
67 |
private final LabeledValue[] options; |
68 |
|
69 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
70 |
private final JLabel dropDownComponent; |
71 |
private final MultilayerFinishEditingPanel panel; |
72 |
// private final JComboBox comboBoxComponent;
|
73 |
|
74 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
75 |
public Row(MultilayerFinishEditingPanel panel, FLyrVect layer) {
|
76 |
this.layer = layer;
|
77 |
this.panel = panel;
|
78 |
|
79 |
options = new LabeledValue[]{ |
80 |
new LabeledValueImpl(i18n.getTranslation("_save"),OPTION_SAVE), |
81 |
new LabeledValueImpl(i18n.getTranslation("_dismiss"), OPTION_DISMISS), |
82 |
new LabeledValueImpl(i18n.getTranslation("_continue"), OPTION_CONTINUE), |
83 |
new LabeledValueImpl(i18n.getTranslation("_export"), OPTION_EXPORT)}; |
84 |
|
85 |
DefaultComboBoxModel modelJComboBoxOptions = new DefaultComboBoxModel(); |
86 |
for (LabeledValue option : options) {
|
87 |
modelJComboBoxOptions.addElement(option); |
88 |
} |
89 |
|
90 |
ToolsSwingManager toolSwingmanager = ToolsSwingLocator.getToolsSwingManager(); |
91 |
this.dropDownComponent = new JLabel(); |
92 |
// this.comboBoxComponent = new JComboBox();
|
93 |
this.dropDownOptions = toolSwingmanager.createDropDown(dropDownComponent);
|
94 |
this.dropDownOptions.setModel(modelJComboBoxOptions);
|
95 |
// this.comboBoxComponent.setModel(modelJComboBoxOptions);
|
96 |
if (this.allowSave()) { |
97 |
this.dropDownOptions.setSelectedIndex(OPTION_SAVE);
|
98 |
} else {
|
99 |
this.dropDownOptions.setSelectedIndex(OPTION_EXPORT);
|
100 |
} |
101 |
// if (this.layer.getFeatureStore().allowWrite()) {
|
102 |
// this.comboBoxComponent.setSelectedIndex(OPTION_SAVE);
|
103 |
// } else {
|
104 |
// this.comboBoxComponent.setSelectedIndex(OPTION_EXPORT);
|
105 |
// }
|
106 |
this.dropDownOptions.setVisibleDropdownArrow(true); |
107 |
|
108 |
this.jButtonExport = new JButton(i18n.getTranslation("_export")); |
109 |
|
110 |
this.jButtonExport.addActionListener((ActionEvent e) -> { |
111 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
112 |
ExportManager exportManager = ExportLocator.getManager(); |
113 |
ExportSwingManager exportSwingManager = ExportSwingLocator.getSwingManager(); |
114 |
ExportProcess process = exportManager.createProcess(layer.getFeatureStore()); |
115 |
JExportProcessPanel panel1 = exportSwingManager.createJExportProcessPanel(process); |
116 |
windowManager.showWindow(panel1.asJComponent(), "_export", WindowManager.MODE.WINDOW);
|
117 |
MultilayerFinishEditingPanel.Row.this.dropDownOptions.setSelectedIndex(OPTION_DISMISS); |
118 |
MultilayerFinishEditingPanel.Row.this.panel.updateEnableOkButton(); |
119 |
}); |
120 |
|
121 |
this.dropDownOptions.addItemListener((ItemEvent e) -> { |
122 |
panel.updateEnableOkButton(); |
123 |
}); |
124 |
|
125 |
// this.comboBoxComponent.addActionListener(new ActionListener() {
|
126 |
// @Override
|
127 |
// public void actionPerformed(ActionEvent e) {
|
128 |
// panel.updateEnableOkButton();
|
129 |
// }
|
130 |
// });
|
131 |
} |
132 |
|
133 |
public boolean allowSave() { |
134 |
return this.layer.getFeatureStore().allowWrite(); |
135 |
} |
136 |
|
137 |
public FLyrVect getLayer() {
|
138 |
return this.layer; |
139 |
} |
140 |
|
141 |
public JLabel getJLabel() { |
142 |
String layerName = this.layer.getName(); |
143 |
JLabel layerNameJLabel = new JLabel(layerName); |
144 |
return layerNameJLabel;
|
145 |
} |
146 |
|
147 |
public JComponent getJDropDown() { |
148 |
return this.dropDownOptions.asJComponent(); |
149 |
} |
150 |
|
151 |
public JButton getJButton() { |
152 |
return this.jButtonExport; |
153 |
} |
154 |
|
155 |
public int getJComboBoxOption() { |
156 |
LabeledValue<Integer> optionSelected = (LabeledValue) this.dropDownOptions.getSelectedItem(); |
157 |
return optionSelected.getValue();
|
158 |
} |
159 |
} |
160 |
|
161 |
private final List<Row> rows; |
162 |
private final MapContext mapContext; |
163 |
|
164 |
public MultilayerFinishEditingPanel(MapContext mapContext) {
|
165 |
this.rows = new ArrayList<>(); |
166 |
this.mapContext = mapContext;
|
167 |
this.initComponents();
|
168 |
} |
169 |
|
170 |
private Row getRow(int rowIndex) { |
171 |
Row row = this.rows.get(rowIndex);
|
172 |
return row;
|
173 |
} |
174 |
|
175 |
public void addLayer(FLayer layer) { |
176 |
if (layer instanceof FLyrVect) { |
177 |
FLyrVect layerVect = (FLyrVect) layer; |
178 |
this.rows.add(new Row(this, layerVect)); |
179 |
} |
180 |
} |
181 |
|
182 |
public void build() { |
183 |
this.removeAll();
|
184 |
this.setLayout(new GridBagLayout()); |
185 |
int rownum = 0; |
186 |
for (Row row : rows) {
|
187 |
addComponent(rownum, 0, row.getJLabel());
|
188 |
addComponent(rownum, 1, row.getJDropDown());
|
189 |
addComponent(rownum, 2, row.getJButton());
|
190 |
rownum++; |
191 |
} |
192 |
GridBagConstraints c = new GridBagConstraints(); |
193 |
c.fill = GridBagConstraints.BOTH;
|
194 |
c.weighty = 0.9;
|
195 |
c.weightx = 0.9;
|
196 |
c.gridx = 1;
|
197 |
c.gridy = rownum; |
198 |
this.add(new JLabel(""), c); |
199 |
} |
200 |
|
201 |
private void addComponent(int gridY, int gridX, JComponent component) { |
202 |
GridBagConstraints c = new GridBagConstraints(); |
203 |
c.insets = new Insets(5, 5, 5, 5); |
204 |
c.fill = GridBagConstraints.HORIZONTAL;
|
205 |
c.ipadx = 2;
|
206 |
c.ipady = 2;
|
207 |
c.gridx = gridX; |
208 |
c.gridy = gridY; |
209 |
this.add(component, c);
|
210 |
} |
211 |
|
212 |
private void initComponents() { |
213 |
Iterator<FLayer> layers = mapContext.deepiterator();
|
214 |
while (layers.hasNext()) {
|
215 |
FLayer layer = layers.next(); |
216 |
if (layer.isEditing()) {
|
217 |
this.addLayer(layer);
|
218 |
} |
219 |
} |
220 |
this.build();
|
221 |
} |
222 |
|
223 |
private boolean checkExportLayers() { |
224 |
boolean exportLayer = false; |
225 |
for (int i = 0; i < this.rows.size(); i++) { |
226 |
Row row = this.getRow(i);
|
227 |
if (row.getJComboBoxOption() == OPTION_EXPORT) {
|
228 |
exportLayer = true;
|
229 |
} |
230 |
} |
231 |
return exportLayer;
|
232 |
} |
233 |
|
234 |
private void updateEnableOkButton(){ |
235 |
if (this.dialog == null){ |
236 |
return;
|
237 |
} |
238 |
if (checkExportLayers()){
|
239 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false);
|
240 |
}else {
|
241 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, true);
|
242 |
} |
243 |
} |
244 |
|
245 |
public void setDialog (Dialog dialog ){ |
246 |
this.dialog=dialog;
|
247 |
updateEnableOkButton(); |
248 |
} |
249 |
|
250 |
// private boolean checkDismissLayers() {
|
251 |
// boolean dismissLayer = false;
|
252 |
//// List dismissLayers = new ArrayList();
|
253 |
// for (int i = 0; i < this.rows.size(); i++) {
|
254 |
// Row row = this.getRow(i);
|
255 |
// if (row.getJComboBoxOption() == OPTION_DISMISS) {
|
256 |
// dismissLayer = true;
|
257 |
//// dismissLayers.add(row.getLayer().getName());
|
258 |
// }
|
259 |
// }
|
260 |
//// if (dismissLayer) {
|
261 |
//// I18nManager i18n = ToolsLocator.getI18nManager();
|
262 |
//// StringBuilder message = new StringBuilder();
|
263 |
//// message.append(i18n.getTranslation("_The_following_layers_dismiss_the_changes"));
|
264 |
//// for (int i = 0; i < dismissLayers.size(); i++) {
|
265 |
//// message.append(dismissLayers.get(i));
|
266 |
//// message.append('\n');
|
267 |
//// }
|
268 |
//// String title = i18n.getTranslation("_Dismiss_information");
|
269 |
//// ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(message.toString(),
|
270 |
//// title,
|
271 |
//// JOptionPane.OK_CANCEL_OPTION);
|
272 |
//// }
|
273 |
// return dismissLayer;
|
274 |
// }
|
275 |
|
276 |
public void apply() { |
277 |
for (int i = 0; i < this.rows.size(); i++) { |
278 |
Row row = this.getRow(i);
|
279 |
if (row.getJComboBoxOption() == OPTION_SAVE) {
|
280 |
try {
|
281 |
row.getLayer().getFeatureStore().finishEditing(); |
282 |
} catch (DataException ex) {
|
283 |
LOGGER.error(PluginServices.getText(this, "Finish editing process has problems, save option"), ex); |
284 |
} |
285 |
} else if (row.getJComboBoxOption() == OPTION_DISMISS) { |
286 |
try {
|
287 |
row.getLayer().getFeatureStore().cancelEditing(); |
288 |
} catch (DataException ex) {
|
289 |
LOGGER.error(PluginServices.getText(this, "Finish editing process has problems, dismiss option"), ex); |
290 |
} |
291 |
} |
292 |
} |
293 |
} |
294 |
|
295 |
@Override
|
296 |
public JComponent asJComponent() { |
297 |
ToolsSwingManager toolSwingmanager = ToolsSwingLocator.getToolsSwingManager(); |
298 |
|
299 |
this.build();
|
300 |
this.setPreferredSize(new Dimension(400, 200)); |
301 |
|
302 |
JScrollPane scrollPanel = new JScrollPane(); |
303 |
scrollPanel.setPreferredSize(new Dimension(420, 220)); |
304 |
scrollPanel.setViewportView(this);
|
305 |
|
306 |
toolSwingmanager.removeBorder(this);
|
307 |
scrollPanel.setBorder(null);
|
308 |
return scrollPanel;
|
309 |
} |
310 |
} |