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