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