svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / TableEditStopExtension.java @ 40558
History | View | Annotate | Download (12.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 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, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.extension; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Component; |
28 |
import java.awt.Font; |
29 |
import java.awt.GridBagConstraints; |
30 |
import java.awt.GridBagLayout; |
31 |
import java.awt.Insets; |
32 |
import java.util.ArrayList; |
33 |
import java.util.List; |
34 |
|
35 |
import javax.swing.JLabel; |
36 |
import javax.swing.JOptionPane; |
37 |
import javax.swing.JPanel; |
38 |
|
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
import org.gvsig.andami.IconThemeHelper; |
43 |
import org.gvsig.andami.PluginServices; |
44 |
import org.gvsig.andami.messages.NotificationManager; |
45 |
import org.gvsig.andami.plugins.IExtension; |
46 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
47 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
48 |
import org.gvsig.andami.plugins.status.UnsavedData; |
49 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
50 |
import org.gvsig.app.ApplicationLocator; |
51 |
import org.gvsig.app.project.Project; |
52 |
import org.gvsig.app.project.ProjectManager; |
53 |
import org.gvsig.app.project.documents.Document; |
54 |
import org.gvsig.app.project.documents.table.TableDocument; |
55 |
import org.gvsig.app.project.documents.table.TableManager; |
56 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
57 |
import org.gvsig.fmap.dal.exception.DataException; |
58 |
import org.gvsig.fmap.dal.exception.ReadException; |
59 |
import org.gvsig.fmap.dal.exception.WriteException; |
60 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
61 |
import org.gvsig.gui.beans.Messages; |
62 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
63 |
|
64 |
public class TableEditStopExtension extends AbstractTableEditExtension { |
65 |
|
66 |
private static Logger logger = |
67 |
LoggerFactory.getLogger(TableEditStopExtension.class); |
68 |
|
69 |
public void initialize() { |
70 |
super.initialize();
|
71 |
IconThemeHelper.registerIcon("action", "table-stop-editing", this); |
72 |
} |
73 |
|
74 |
public void execute(String actionCommand) { |
75 |
if ("table-stop-editing".equals(actionCommand)) { |
76 |
stopEditing(table); |
77 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
78 |
} |
79 |
} |
80 |
|
81 |
private void stopEditing(FeatureTableDocumentPanel table) { |
82 |
|
83 |
Object[] options = { |
84 |
PluginServices.getText(this, "_Guardar"), |
85 |
" " + PluginServices.getText(this, "_Descartar") + " ", |
86 |
PluginServices.getText(this, "_Continuar") }; |
87 |
|
88 |
JPanel explanation_panel = getExplanationPanel(table.getModel().getName());
|
89 |
|
90 |
int resp = JOptionPane |
91 |
.showOptionDialog( |
92 |
(Component) PluginServices.getMainFrame(),
|
93 |
explanation_panel, |
94 |
PluginServices.getText(this, "stop_edition"), |
95 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
96 |
JOptionPane.QUESTION_MESSAGE, null, options, |
97 |
options[2]);
|
98 |
|
99 |
try {
|
100 |
if (resp == JOptionPane.NO_OPTION) { |
101 |
// CANCEL EDITING
|
102 |
table.getModel().getStore().cancelEditing(); |
103 |
} else {
|
104 |
|
105 |
if (resp == JOptionPane.YES_OPTION) { |
106 |
// Save table
|
107 |
table.getModel().getStore().finishEditing(); |
108 |
} else {
|
109 |
// This happens when user clicks on [x]
|
110 |
// to abruptly close previous JOptionPane dialog
|
111 |
// We do nothing (equivalent to 'Continue editing')
|
112 |
} |
113 |
} |
114 |
} catch (DataException e) {
|
115 |
logger.error("While finishing or canceling table editing: "
|
116 |
+ e.getMessage(), e); |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
122 |
*/
|
123 |
public boolean isEnabled() { |
124 |
return true; |
125 |
} |
126 |
|
127 |
/**
|
128 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
129 |
*/
|
130 |
public boolean isVisible() { |
131 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
132 |
|
133 |
if (v == null) { |
134 |
return false; |
135 |
} else
|
136 |
if (v instanceof FeatureTableDocumentPanel |
137 |
&& ((FeatureTableDocumentPanel) v).getModel().getStore() |
138 |
.isEditing() |
139 |
&& ((FeatureTableDocumentPanel) v).getModel() |
140 |
.getAssociatedLayer() == null) {
|
141 |
table = (FeatureTableDocumentPanel) v; |
142 |
return true; |
143 |
} else {
|
144 |
return false; |
145 |
} |
146 |
} |
147 |
|
148 |
/**
|
149 |
* <p>
|
150 |
* This class provides the status of extensions. If this extension has some
|
151 |
* unsaved editing table (and save them), and methods to check if the
|
152 |
* extension has some associated background tasks.
|
153 |
*
|
154 |
* @author Vicente Caballero Navarro
|
155 |
*
|
156 |
*/
|
157 |
private class StopEditingStatus implements IExtensionStatus { |
158 |
|
159 |
/**
|
160 |
* This method is used to check if this extension has some unsaved
|
161 |
* editing tables.
|
162 |
*
|
163 |
* @return true if the extension has some unsaved editing tables, false
|
164 |
* otherwise.
|
165 |
*/
|
166 |
public boolean hasUnsavedData() { |
167 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
168 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
169 |
for (int i = 0; i < tables.size(); i++) { |
170 |
FeatureStore store = ((TableDocument) tables.get(i)).getStore(); |
171 |
if (store == null) { |
172 |
continue;
|
173 |
} |
174 |
if (store.isEditing()) {
|
175 |
return true; |
176 |
} |
177 |
} |
178 |
return false; |
179 |
} |
180 |
|
181 |
/**
|
182 |
* This method is used to check if the extension has some associated
|
183 |
* background process which is currently running.
|
184 |
*
|
185 |
* @return true if the extension has some associated background process,
|
186 |
* false otherwise.
|
187 |
*/
|
188 |
public boolean hasRunningProcesses() { |
189 |
return false; |
190 |
} |
191 |
|
192 |
/**
|
193 |
* <p>
|
194 |
* Gets an array of the traceable background tasks associated with this
|
195 |
* extension. These tasks may be tracked, canceled, etc.
|
196 |
* </p>
|
197 |
*
|
198 |
* @return An array of the associated background tasks, or null in case
|
199 |
* there is
|
200 |
* no associated background tasks.
|
201 |
*/
|
202 |
public IMonitorableTask[] getRunningProcesses() { |
203 |
return null; |
204 |
} |
205 |
|
206 |
/**
|
207 |
* <p>
|
208 |
* Gets an array of the UnsavedData objects, which contain information
|
209 |
* about the unsaved editing tables and allows to save it.
|
210 |
* </p>
|
211 |
*
|
212 |
* @return An array of the associated unsaved editing layers, or null in
|
213 |
* case the extension
|
214 |
* has not unsaved editing tables.
|
215 |
*/
|
216 |
public IUnsavedData[] getUnsavedData() { |
217 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
218 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
219 |
List<UnsavedTable> unsavedTables = new ArrayList<UnsavedTable>(); |
220 |
for (int i = 0; i < tables.size(); i++) { |
221 |
TableDocument table = (TableDocument) tables.get(i); |
222 |
FeatureStore store = table.getStore(); |
223 |
if (store == null) { |
224 |
continue;
|
225 |
} |
226 |
if (store.isEditing()) {
|
227 |
UnsavedTable ul = |
228 |
new UnsavedTable(TableEditStopExtension.this);
|
229 |
ul.setTable(table); |
230 |
unsavedTables.add(ul); |
231 |
} |
232 |
} |
233 |
return unsavedTables
|
234 |
.toArray(new IUnsavedData[unsavedTables.size()]);
|
235 |
} |
236 |
} |
237 |
|
238 |
private class UnsavedTable extends UnsavedData { |
239 |
|
240 |
private TableDocument table;
|
241 |
|
242 |
public UnsavedTable(IExtension extension) {
|
243 |
super(extension);
|
244 |
} |
245 |
|
246 |
public String getDescription() { |
247 |
return PluginServices.getText(this, "editing_table_unsaved"); |
248 |
} |
249 |
|
250 |
public String getResourceName() { |
251 |
return table.getName();
|
252 |
} |
253 |
|
254 |
public boolean saveData() { |
255 |
return executeSaveTable(table);
|
256 |
} |
257 |
|
258 |
public void setTable(TableDocument table) { |
259 |
this.table = table;
|
260 |
} |
261 |
|
262 |
public String getIcon() { |
263 |
return "document-table-icon-small"; |
264 |
} |
265 |
} |
266 |
|
267 |
// TODO Este c?digo est? duplicado, tambi?n est? en la clase Table en el
|
268 |
// m?todo "public void stopEditing()"
|
269 |
private boolean executeSaveTable(TableDocument table2) { |
270 |
FeatureStore fs = table2.getStore(); |
271 |
try {
|
272 |
fs.finishEditing(); |
273 |
} catch (WriteException e) {
|
274 |
NotificationManager.addError( |
275 |
PluginServices.getText(this, "error_saving_table"), e); |
276 |
return false; |
277 |
} catch (ReadException e) {
|
278 |
NotificationManager.addError( |
279 |
PluginServices.getText(this, "error_saving_table"), e); |
280 |
return false; |
281 |
} catch (DataException e) {
|
282 |
NotificationManager.addError( |
283 |
PluginServices.getText(this, "error_saving_table"), e); |
284 |
return false; |
285 |
} |
286 |
// if (ies instanceof IWriteable) {
|
287 |
// IWriteable w = (IWriteable) ies;
|
288 |
// IWriter writer = w.getWriter();
|
289 |
// if (writer == null) {
|
290 |
// return false;
|
291 |
// }
|
292 |
// try {
|
293 |
// ITableDefinition tableDef = ies.getTableDefinition();
|
294 |
// writer.initialize(tableDef);
|
295 |
// ies.stopEdition(writer, EditionEvent.ALPHANUMERIC);
|
296 |
// ies.getSelection().clear();
|
297 |
// } catch (InitializeWriterException e) {
|
298 |
// NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
|
299 |
// return false;
|
300 |
// } catch (StopWriterVisitorException e) {
|
301 |
// NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
|
302 |
// return false;
|
303 |
// } catch (ReadDriverException e) {
|
304 |
// NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
|
305 |
// return false;
|
306 |
// }
|
307 |
// }
|
308 |
return true; |
309 |
} |
310 |
|
311 |
public IExtensionStatus getStatus() {
|
312 |
return new StopEditingStatus(); |
313 |
} |
314 |
|
315 |
|
316 |
private JPanel getExplanationPanel(String name) { |
317 |
|
318 |
BorderLayout bl = new BorderLayout(10, 10); |
319 |
JPanel resp = new JPanel(bl); |
320 |
|
321 |
String msg = Messages.getText("realmente_desea_guardar"); |
322 |
JLabel topLabel = new JLabel(msg); |
323 |
|
324 |
JPanel mainPanel = new JPanel(new GridBagLayout()); |
325 |
GridBagConstraints cc = new GridBagConstraints(); |
326 |
|
327 |
cc.gridx = 0;
|
328 |
cc.gridy = 0;
|
329 |
cc.anchor = GridBagConstraints.WEST;
|
330 |
|
331 |
cc.insets = new Insets(3, 6, 3, 6); |
332 |
|
333 |
Font boldf = mainPanel.getFont().deriveFont(Font.BOLD); |
334 |
|
335 |
JLabel lbl = new JLabel(Messages.getText("_Guardar")); |
336 |
lbl.setFont(boldf); |
337 |
mainPanel.add(lbl, cc); |
338 |
cc.gridx = 1;
|
339 |
mainPanel.add(new JLabel(Messages.getText("_Save_changes_performed")), cc); |
340 |
|
341 |
cc.gridx = 0;
|
342 |
cc.gridy = 1;
|
343 |
lbl = new JLabel(Messages.getText("_Descartar")); |
344 |
lbl.setFont(boldf); |
345 |
mainPanel.add(lbl, cc); |
346 |
cc.gridx = 1;
|
347 |
mainPanel.add(new JLabel(Messages.getText("_Discard_and_lose_changes")), cc); |
348 |
|
349 |
cc.gridx = 0;
|
350 |
cc.gridy = 2;
|
351 |
lbl = new JLabel(Messages.getText("_Continuar")); |
352 |
lbl.setFont(boldf); |
353 |
mainPanel.add(lbl, cc); |
354 |
cc.gridx = 1;
|
355 |
mainPanel.add(new JLabel(Messages.getText("_Do_not_save_yet_Stay_in_editing_mode")), cc); |
356 |
|
357 |
resp.add(mainPanel, BorderLayout.CENTER);
|
358 |
resp.add(topLabel, BorderLayout.NORTH);
|
359 |
return resp;
|
360 |
} |
361 |
} |