Statistics
| Revision:

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 @ 44259

History | View | Annotate | Download (8.24 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40558 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40558 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40558 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40558 jjdelcerro
 *
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 40435 jjdelcerro
 */
24
package org.gvsig.app.extension;
25
26 44130 jjdelcerro
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28 40435 jjdelcerro
import org.gvsig.andami.IconThemeHelper;
29
import org.gvsig.andami.plugins.Extension;
30 44130 jjdelcerro
import org.gvsig.app.ApplicationLocator;
31
import org.gvsig.app.ApplicationManager;
32 40435 jjdelcerro
import org.gvsig.app.project.documents.table.TableDocument;
33 44130 jjdelcerro
import org.gvsig.app.project.documents.table.TableManager;
34 44252 jjdelcerro
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
35 44130 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
36
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
37
import org.gvsig.fmap.dal.feature.EditableFeatureType;
38 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
39 44130 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
40
import org.gvsig.fmap.dal.swing.DALSwingLocator;
41
import org.gvsig.fmap.dal.swing.DataSwingManager;
42
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypeAttributePanel;
43
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.i18n.I18nManager;
46
import org.gvsig.tools.swing.api.ToolsSwingLocator;
47
import org.gvsig.tools.swing.api.windowmanager.Dialog;
48
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
49
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
50 40435 jjdelcerro
51
/**
52
 * Extensi?n que abre la ventana para cambiar la configuraci?n de la estructura
53
 * de la tabla.
54
 *
55
 */
56 44130 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
57 40435 jjdelcerro
public class TableEditAttributes extends Extension {
58
59 44130 jjdelcerro
    public void initialize() {
60
        IconThemeHelper.registerIcon("action", "table-column-manager", this);
61
        IconThemeHelper.registerIcon("action", "table-add-column", this);
62
    }
63 40435 jjdelcerro
64 44130 jjdelcerro
    @Override
65
    public boolean canQueryByAction() {
66
        return true;
67
    }
68
69 40435 jjdelcerro
    public void execute(String s) {
70 44130 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
71
        WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
72
        DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager();
73
        ApplicationManager application = ApplicationLocator.getManager();
74
75
        TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME);
76
        if( tabledoc == null ) {
77
            return;
78
        }
79 44252 jjdelcerro
//        final FeatureStore featureStore = tabledoc.getStore();
80
        final FeatureTableDocumentPanel tablePanel = (FeatureTableDocumentPanel) tabledoc.getMainComponent();
81
        final FeatureStore featureStore = tablePanel.getFeatureStore();
82 44130 jjdelcerro
83 40435 jjdelcerro
            if( "table-column-manager".equalsIgnoreCase(s)) {
84 44130 jjdelcerro
            try {
85
                final FeatureType featureType = featureStore.getDefaultFeatureType();
86
                final EditableFeatureType editableFeatureType = featureType.getEditable();
87
                final FeatureTypePanel panel = dataSwingManager.createFeatureTypePanel();
88
                panel.put(editableFeatureType);
89
                panel.setEditable(featureStore.isEditing());
90
                final Dialog dialog = winManager.createDialog(
91
                        panel.asJComponent(),
92 44202 jjdelcerro
                        i18n.getTranslation("_Column_manager") + " - " + tabledoc.getName(),
93 44130 jjdelcerro
                        null,
94
                        featureStore.isEditing()? WindowManager_v2.BUTTONS_OK_CANCEL:WindowManager_v2.BUTTONS_OK
95
                );
96
                dialog.addActionListener(new ActionListener() {
97
                    @Override
98
                    public void actionPerformed(ActionEvent e) {
99
                        if( dialog.getAction()!=WindowManager_v2.BUTTONS_OK ) {
100
                            return;
101
                        }
102
                        if( !featureStore.isEditing() ) {
103
                            return;
104
                        }
105
                        try {
106
                            panel.fetch(editableFeatureType);
107
                            featureStore.update(editableFeatureType);
108
                        } catch (Exception ex) {
109
                            logger.warn("Problems updating featuretype.", ex);
110
                        }
111
                    }
112
                });
113
                dialog.show(WindowManager.MODE.WINDOW);
114
            } catch (Exception ex) {
115
                logger.warn("Problems show FeatureTypePanel.", ex);
116
            }
117 40730 jldominguez
118 44130 jjdelcerro
        } else if ("table-add-column".equals(s)) {
119
            try {
120
                final FeatureType featureType = featureStore.getDefaultFeatureType();
121
                final FeatureTypeAttributePanel panel = dataSwingManager.createFeatureTypeAttributePanel();
122
                panel.clean();
123
                panel.setNameAsNew(featureType);
124
                final Dialog dialog = winManager.createDialog(
125
                        panel.asJComponent(),
126
                        i18n.getTranslation("_New_field") + " - " + tabledoc.getName(),
127
                        null,
128
                        WindowManager_v2.BUTTONS_OK_CANCEL
129
                );
130
                dialog.addActionListener(new ActionListener() {
131
                    @Override
132
                    public void actionPerformed(ActionEvent e) {
133
                        if( dialog.getAction()!=WindowManager_v2.BUTTONS_OK ) {
134
                            return;
135
                        }
136
                        if( !featureStore.isEditing() ) {
137
                            return;
138
                        }
139
                        try {
140
                            EditableFeatureType editableFeatureType = featureType.getEditable();
141
                            EditableFeatureAttributeDescriptor descriptor = editableFeatureType.add(
142
                                    panel.getName(),
143
                                    DataTypes.STRING
144
                            );
145
                            panel.fetch(descriptor);
146
                            featureStore.update(editableFeatureType);
147
                        } catch (Exception ex) {
148
                            logger.warn("Problems updating featuretype.", ex);
149
                        }
150
                    }
151
                });
152
                dialog.show(WindowManager.MODE.WINDOW);
153
            } catch (Exception ex) {
154
                logger.warn("Problems show FeatureTypeAttributePanel.", ex);
155
            }
156 40730 jldominguez
157 40435 jjdelcerro
            }
158
    }
159
160 44130 jjdelcerro
    public boolean isEnabled() {
161
        ApplicationManager application = ApplicationLocator.getManager();
162
163
        TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME);
164
        if( tabledoc == null ) {
165
            return false;
166
        }
167
        return tabledoc.getStore().isEditing();
168 40435 jjdelcerro
    }
169
170 44130 jjdelcerro
    public boolean isVisible() {
171
        ApplicationManager application = ApplicationLocator.getManager();
172
173
        TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME);
174
        return tabledoc!=null;
175 40435 jjdelcerro
    }
176
177 44130 jjdelcerro
    @Override
178
    public boolean isVisible(String action) {
179
        return this.isVisible();
180
    }
181
182
    @Override
183
    public boolean isEnabled(String action) {
184
        ApplicationManager application = ApplicationLocator.getManager();
185
186
        TableDocument tabledoc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME);
187
        if( tabledoc == null ) {
188
            return false;
189
        }
190
            if( "table-column-manager".equalsIgnoreCase(action)) {
191 40435 jjdelcerro
            return true;
192 44130 jjdelcerro
193
        } else if ("table-add-column".equals(action)) {
194
            return tabledoc.getStore().isEditing();
195
196 40435 jjdelcerro
        }
197
        return false;
198
    }
199 44130 jjdelcerro
200 40435 jjdelcerro
}