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