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