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