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 / TableEditAttributes.java @ 45739
History | View | Annotate | Download (14 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.Component; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.io.File; |
30 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.Action; |
32 |
import static javax.swing.Action.ACTION_COMMAND_KEY; |
33 |
import static javax.swing.Action.NAME; |
34 |
import javax.swing.JOptionPane; |
35 |
import javax.swing.event.ChangeEvent; |
36 |
import javax.swing.event.ChangeListener; |
37 |
import org.gvsig.andami.IconThemeHelper; |
38 |
import org.gvsig.andami.plugins.Extension; |
39 |
import org.gvsig.app.ApplicationLocator; |
40 |
import org.gvsig.app.ApplicationManager; |
41 |
import org.gvsig.app.project.Project; |
42 |
import org.gvsig.app.project.ProjectManager; |
43 |
import org.gvsig.app.project.documents.table.TableDocument; |
44 |
import org.gvsig.app.project.documents.table.TableManager; |
45 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
46 |
import org.gvsig.configurableactions.ConfigurableActionsMamager; |
47 |
import org.gvsig.fmap.dal.DALLocator; |
48 |
import org.gvsig.fmap.dal.DataManager; |
49 |
import org.gvsig.fmap.dal.DataStoreProviderFactory; |
50 |
import org.gvsig.fmap.dal.DataTypes; |
51 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
52 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
53 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
54 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
55 |
import org.gvsig.fmap.dal.feature.FeatureType; |
56 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
57 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
58 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypeAttributePanel; |
59 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel; |
60 |
import org.gvsig.tools.ToolsLocator; |
61 |
import org.gvsig.tools.folders.FoldersManager; |
62 |
import org.gvsig.tools.i18n.I18nManager; |
63 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
64 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
65 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
66 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
67 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
68 |
import org.gvsig.tools.util.ToolsUtilLocator; |
69 |
|
70 |
/**
|
71 |
* Extensi?n que abre la ventana para cambiar la configuraci?n de la estructura
|
72 |
* de la tabla.
|
73 |
*
|
74 |
*/
|
75 |
@SuppressWarnings("UseSpecificCatch") |
76 |
public class TableEditAttributes extends Extension { |
77 |
|
78 |
@Override
|
79 |
public void initialize() { |
80 |
IconThemeHelper.registerIcon("action", "table-column-manager", this); |
81 |
IconThemeHelper.registerIcon("action", "table-add-column", this); |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void postInitialize() { |
86 |
ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager(); |
87 |
cfgActionsManager.addConfigurableAction(FeatureTypePanel.CONFIGURABLE_PANEL_ID, new ShowAsTableAction());
|
88 |
cfgActionsManager.addConfigurableAction(FeatureTypePanel.CONFIGURABLE_PANEL_ID, new WriteToDALFileAction());
|
89 |
} |
90 |
|
91 |
@Override
|
92 |
public boolean canQueryByAction() { |
93 |
return true; |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void execute(String s) { |
98 |
final I18nManager i18n = ToolsLocator.getI18nManager();
|
99 |
final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
100 |
final DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
|
101 |
final DataManager dataMamager = DALLocator.getDataManager();
|
102 |
final ApplicationManager application = ApplicationLocator.getManager();
|
103 |
|
104 |
TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME); |
105 |
if (tabledoc == null) { |
106 |
return;
|
107 |
} |
108 |
// final FeatureStore featureStore = tabledoc.getStore();
|
109 |
final FeatureTableDocumentPanel tablePanel = (FeatureTableDocumentPanel) tabledoc.getMainComponent();
|
110 |
final FeatureStore featureStore = tablePanel.getFeatureStore();
|
111 |
|
112 |
if ("table-column-manager".equalsIgnoreCase(s)) { |
113 |
try {
|
114 |
final FeatureType featureType = featureStore.getDefaultFeatureType();
|
115 |
final EditableFeatureType editableFeatureType = featureType.getEditable();
|
116 |
final FeatureTypePanel panel = dataSwingManager.createFeatureTypePanel();
|
117 |
panel.put(editableFeatureType); |
118 |
FeatureStore store = tabledoc.getStore(); |
119 |
|
120 |
panel.setMode( |
121 |
featureStore.isEditing() |
122 |
? FeatureTypePanel.MODE_EDIT_ALL |
123 |
: FeatureTypePanel.MODE_EDIT_ONLY_METADATA |
124 |
); |
125 |
|
126 |
if (!featureStore.isEditing()) {
|
127 |
ApplicationManager aplication = ApplicationLocator.getManager(); |
128 |
aplication.messageDialog( |
129 |
i18n.getTranslation("_The_table_is_not_in_edit_mode._You_can_only_add_virtual_fields_and_modify_some_table_data"),
|
130 |
i18n.getTranslation("_Warning"),
|
131 |
JOptionPane.WARNING_MESSAGE);
|
132 |
} |
133 |
|
134 |
DataStoreProviderFactory factory = store.getProviderFactory(); |
135 |
if (factory instanceof FeatureStoreProviderFactory) { |
136 |
FeatureStoreProviderFactory ffactory = (FeatureStoreProviderFactory) factory; |
137 |
if (ffactory.allowEditableFeatureType() == FeatureStoreProviderFactory.NO) {
|
138 |
panel.setMode(FeatureTypePanel.MODE_SHOW_ONLY); |
139 |
} |
140 |
} |
141 |
|
142 |
final Dialog dialog = winManager.createDialog( |
143 |
panel.asJComponent(), |
144 |
i18n.getTranslation("_Column_manager") + " - " + tabledoc.getName(), |
145 |
null,
|
146 |
WindowManager_v2.BUTTONS_OK_CANCEL |
147 |
); |
148 |
panel.addChangeListener((ChangeEvent e) -> {
|
149 |
if (panel.isEditing()) {
|
150 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false);
|
151 |
} else {
|
152 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, true);
|
153 |
} |
154 |
}); |
155 |
dialog.addActionListener((ActionEvent e) -> {
|
156 |
if (dialog.getAction() != WindowManager_v2.BUTTON_OK) {
|
157 |
return;
|
158 |
} |
159 |
try {
|
160 |
panel.fetch(editableFeatureType); |
161 |
featureStore.update(editableFeatureType); |
162 |
dataMamager.clearAvailableValuesCache(); |
163 |
} catch (Exception ex) { |
164 |
logger.warn("Problems updating featuretype.", ex);
|
165 |
} |
166 |
}); |
167 |
dialog.show(WindowManager.MODE.WINDOW); |
168 |
} catch (Exception ex) { |
169 |
logger.warn("Problems show FeatureTypePanel.", ex);
|
170 |
} |
171 |
|
172 |
} else if ("table-add-column".equals(s)) { |
173 |
try {
|
174 |
final FeatureType featureType = featureStore.getDefaultFeatureType();
|
175 |
final FeatureTypeAttributePanel panel = dataSwingManager.createFeatureTypeAttributePanel();
|
176 |
panel.clean(); |
177 |
panel.setNameAsNew(featureType); |
178 |
panel.setMode(FeatureTypePanel.MODE_EDIT_ALL); |
179 |
final Dialog dialog = winManager.createDialog( |
180 |
panel.asJComponent(), |
181 |
i18n.getTranslation("_New_field") + " - " + tabledoc.getName(), |
182 |
null,
|
183 |
WindowManager_v2.BUTTONS_OK_CANCEL |
184 |
); |
185 |
dialog.addActionListener(new ActionListener() { |
186 |
@Override
|
187 |
public void actionPerformed(ActionEvent e) { |
188 |
if (dialog.getAction() != WindowManager_v2.BUTTONS_OK) {
|
189 |
return;
|
190 |
} |
191 |
if (!featureStore.isEditing()) {
|
192 |
return;
|
193 |
} |
194 |
try {
|
195 |
EditableFeatureType editableFeatureType = featureType.getEditable(); |
196 |
EditableFeatureAttributeDescriptor descriptor = editableFeatureType.add( |
197 |
panel.getName(), |
198 |
DataTypes.STRING |
199 |
); |
200 |
panel.fetch(descriptor); |
201 |
featureStore.update(editableFeatureType); |
202 |
} catch (Exception ex) { |
203 |
logger.warn("Problems updating featuretype.", ex);
|
204 |
} |
205 |
} |
206 |
}); |
207 |
dialog.show(WindowManager.MODE.WINDOW); |
208 |
} catch (Exception ex) { |
209 |
logger.warn("Problems show FeatureTypeAttributePanel.", ex);
|
210 |
} |
211 |
|
212 |
} |
213 |
} |
214 |
public static class WriteToDALFileAction extends AbstractAction { |
215 |
|
216 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
217 |
public WriteToDALFileAction() {
|
218 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
219 |
|
220 |
this.putValue(NAME, i18n.getTranslation("_Save_as_DAL_file")); |
221 |
this.putValue(ACTION_COMMAND_KEY, "WriteToDALFile"); |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public void actionPerformed(ActionEvent ae) { |
226 |
FeatureTypePanel panel = (FeatureTypePanel) ae.getSource(); |
227 |
EditableFeatureType featureType = panel.fetch(null);
|
228 |
|
229 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
230 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
231 |
FoldersManager foldersManager = ToolsLocator.getFoldersManager(); |
232 |
File[] f = application.showSaveFileDialog( |
233 |
i18n.getTranslation("_Save_as_DAL_file"),
|
234 |
foldersManager.getLastPath("SaveAsDALFile", null) |
235 |
); |
236 |
if( f==null || f.length==0 ) { |
237 |
return;
|
238 |
} |
239 |
foldersManager.setLastPath("SaveAsDALFile", f[0].getParentFile()); |
240 |
featureType.writeAsDALFile(f[0]);
|
241 |
} |
242 |
} |
243 |
|
244 |
|
245 |
public static class ShowAsTableAction extends AbstractAction { |
246 |
|
247 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
248 |
public ShowAsTableAction() {
|
249 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
250 |
|
251 |
this.putValue(NAME, i18n.getTranslation("_Show_as_table")); |
252 |
this.putValue(ACTION_COMMAND_KEY, "ShowAsTable"); |
253 |
this.putValue(
|
254 |
Action.SMALL_ICON,
|
255 |
ToolsSwingLocator.getIconThemeManager().getCurrent().get("layer-show-attributes-table")
|
256 |
); |
257 |
} |
258 |
|
259 |
@Override
|
260 |
public void actionPerformed(ActionEvent ae) { |
261 |
FeatureTypePanel panel = (FeatureTypePanel) ae.getSource(); |
262 |
EditableFeatureType featureType = panel.fetch(null);
|
263 |
FeatureStore store = featureType.getAsFeatureStore(); |
264 |
|
265 |
ProjectManager projectManager = ApplicationLocator.getManager().getProjectManager(); |
266 |
Project project = projectManager.getCurrentProject(); |
267 |
TableDocument tableDoc = (TableDocument) project.getDocument(store.getName(), TableManager.TYPENAME); |
268 |
if (tableDoc == null) { |
269 |
tableDoc = (TableDocument) projectManager.createDocument(TableManager.TYPENAME); |
270 |
tableDoc.setStore(store); |
271 |
tableDoc.setName(store.getName()); |
272 |
project.addDocument(tableDoc); |
273 |
} |
274 |
ApplicationLocator.getManager().getUIManager().addWindow(tableDoc.getMainWindow()); |
275 |
} |
276 |
} |
277 |
|
278 |
@Override
|
279 |
public boolean isEnabled() { |
280 |
ApplicationManager application = ApplicationLocator.getManager(); |
281 |
|
282 |
TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME); |
283 |
if (tabledoc == null) { |
284 |
return false; |
285 |
} |
286 |
return tabledoc.getStore().isEditing();
|
287 |
} |
288 |
|
289 |
@Override
|
290 |
public boolean isVisible() { |
291 |
ApplicationManager application = ApplicationLocator.getManager(); |
292 |
|
293 |
TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME); |
294 |
return tabledoc != null; |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public boolean isVisible(String action) { |
299 |
return this.isVisible(); |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public boolean isEnabled(String action) { |
304 |
ApplicationManager application = ApplicationLocator.getManager(); |
305 |
|
306 |
TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME); |
307 |
if (tabledoc == null) { |
308 |
return false; |
309 |
} |
310 |
if ("table-column-manager".equalsIgnoreCase(action)) { |
311 |
return true; |
312 |
|
313 |
} else if ("table-add-column".equals(action)) { |
314 |
FeatureStore store = tabledoc.getStore(); |
315 |
|
316 |
DataStoreProviderFactory factory = store.getProviderFactory(); |
317 |
if (factory instanceof FeatureStoreProviderFactory) { |
318 |
FeatureStoreProviderFactory ffactory = (FeatureStoreProviderFactory) factory; |
319 |
if (ffactory.allowEditableFeatureType() == FeatureStoreProviderFactory.NO) {
|
320 |
return false; |
321 |
} else {
|
322 |
return tabledoc.getStore().isEditing();
|
323 |
} |
324 |
} else {
|
325 |
return tabledoc.getStore().isEditing();
|
326 |
} |
327 |
|
328 |
} |
329 |
return false; |
330 |
} |
331 |
|
332 |
} |