Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / develtools / EditingListenerPanel.java @ 41335

History | View | Annotate | Download (7.93 KB)

1 41323 jjdelcerro
/**
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.develtools;
25
26
import java.awt.Dimension;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.awt.event.ComponentEvent;
30
import java.awt.event.ComponentListener;
31
32
import javax.swing.JOptionPane;
33
import org.gvsig.app.ApplicationLocator;
34
import org.gvsig.app.ApplicationManager;
35
import org.gvsig.editing.EditingNotificationManager;
36
import org.gvsig.editing.EditingNotification;
37
import org.gvsig.fmap.mapcontrol.MapControlLocator;
38
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
39 41332 jjdelcerro
import org.gvsig.tools.dynobject.DynObject;
40 41323 jjdelcerro
import org.gvsig.tools.observer.Observable;
41
import org.gvsig.tools.observer.Observer;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44
45
46
public class EditingListenerPanel extends EditingListenerPanelLayout implements Observer {
47
    private static final long serialVersionUID = -4106763914259136033L;
48
    private static final Logger logger = LoggerFactory.getLogger(EditingListenerPanel.class);
49
    private boolean askToAceptNotification;
50
    private boolean closed = true;
51
    private StringBuilder buffer = new StringBuilder();
52
53
    public EditingListenerPanel() {
54
        initComponents();
55
        startObservation();
56
    }
57
58
    private void initComponents() {
59
        this.btnClear.addActionListener(new ActionListener() {
60
            public void actionPerformed(ActionEvent ae) {
61
                onClear();
62
            }
63
        });
64
        this.btnClose.addActionListener(new ActionListener() {
65
            public void actionPerformed(ActionEvent ae) {
66
                setVisible(false);
67
            }
68
        });
69
70
        this.chkAskToAceptNotification.addActionListener(new ActionListener() {
71
            public void actionPerformed(ActionEvent ae) {
72
                onAskToAceptNotification();
73
            }
74
        });
75 41335 jjdelcerro
        this.chkSkipFeatureValidation.setSelected(false);
76
77 41323 jjdelcerro
        this.chkAskToAceptNotification.setSelected(false);
78
        this.addComponentListener(new ComponentListener() {
79
            public void componentResized(ComponentEvent ce) {
80
            }
81
            public void componentMoved(ComponentEvent ce) {
82
            }
83
            public void componentShown(ComponentEvent ce) {
84
                closed = false;
85
            }
86
            public void componentHidden(ComponentEvent ce) {
87
                onClose();
88
            }
89
        });
90
        this.setPreferredSize(new Dimension(300, 400));
91
    }
92
93
    public boolean isClosed() {
94
        return closed;
95
    }
96
97
    private void onClose() {
98
        this.stopObservation();
99
        this.closed = true;
100
    }
101
102
    private void onClear() {
103
        this.txtNotifications.setText("");
104
        this.buffer = new StringBuilder();
105
    }
106
107
    private void onAskToAceptNotification() {
108
        this.askToAceptNotification = this.chkAskToAceptNotification.isSelected();
109
    }
110
111
    private void addText(String text) {
112
        this.buffer.append(text);
113
        if( this.buffer.length()> 30000) {
114
            this.buffer.delete(0, 20000);
115
        }
116
        String s = "<html>\n"+buffer.toString()+"</html>\n";
117
        this.txtNotifications.setText(s);
118
        this.txtNotifications.setSelectionStart(s.length());
119
    }
120
121
    public void update(Observable observable, Object notification) {
122
        EditingNotification n = (EditingNotification) notification;
123
        StringBuilder b = new StringBuilder();
124
        b.append("<b>Notification type</b>: ");
125
        b.append(n.getType());
126
        b.append("<br>\n");
127
        if( n.getSource() == null ) {
128
            b.append("<b>Source</b>: (unknow)<br>\n");
129
        } else {
130
            b.append("<b>Source</b>: <i>");
131
            b.append(n.getSource().getClass().getName());
132
            b.append("</i> (");
133
            b.append(n.getSource().hashCode());
134
            b.append(") <br>\n");
135
        }
136
        if( n.getDocument() == null ) {
137
            b.append("<b>Document</b>: (unknow)<br>\n");
138
        } else {
139
            b.append("<b>Document</b>: <i>");
140
            b.append(n.getDocument().toString());
141
            b.append("</i> (");
142
            b.append(n.getDocument().hashCode());
143
            b.append(", ");
144
            b.append(n.getDocument().getClass().getName());
145
            b.append(") <br>\n");
146
        }
147
        if( n.getLayer() == null ) {
148
            b.append("<b>Layer</b>: (unknow)<br>\n");
149
        } else {
150
            b.append("<b>Layer</b>: <i>");
151
            b.append(n.getLayer().getName());
152
            b.append("</i><br>\n");
153
        }
154
        if( n.getStore()== null ) {
155
            b.append("<b>Store</b>: (unknow)<br>\n");
156
        } else {
157
            b.append("<b>Store</b>: <i>");
158
            b.append(n.getStore().getName());
159
            b.append("</i> (");
160
            b.append(n.getStore().hashCode());
161
            b.append(", ");
162
            b.append(n.getStore().getClass().getName());
163
            b.append(") <br>\n");
164
        }
165
        if( n.getFeature()== null ) {
166
            b.append("<b>Feature</b>: (unknow)<br>\n");
167
        } else {
168
            b.append("<b>Feature</b>: ");
169
            b.append(n.getFeature().toString());
170
            b.append("<br>\n");
171
        }
172
173
        b.append("<hr><br>\n");
174
        this.addText(b.toString());
175 41335 jjdelcerro
176
        n.setSkipFeatureValidation(this.chkSkipFeatureValidation.isSelected());
177 41323 jjdelcerro
178
        if( n.isCancelable() ) {
179
            if( this.chkShowFeatureInForm.isSelected() && n.getFeature()!=null ) {
180
                try {
181 41332 jjdelcerro
                    DynObject data = n.getFeature().getAsDynObject();
182
                    DynObjectEditor editor = new DynObjectEditor(data);
183 41335 jjdelcerro
                    editor.setTitle("Editing monitor - "+n.getFeature().getType().getName());
184 41323 jjdelcerro
                    editor.editObject(true);
185 41332 jjdelcerro
                    if( editor.isCanceled() ) {
186
                        if( this.askToAceptNotification ) {
187
                                n.cancel();
188 41323 jjdelcerro
                        }
189 41332 jjdelcerro
                    } else {
190
                         editor.getData(data);
191 41323 jjdelcerro
                    }
192
                } catch (Exception ex) {
193
                    logger.warn("Problems showing the feature in a form.",ex);
194
                }
195
            } else if( this.askToAceptNotification ) {
196
                ApplicationManager application = ApplicationLocator.getManager();
197
                int r = application.confirmDialog(
198
                        "Do you want to cancel the process that generated this notification?",
199
                        "Cancel process",
200
                        JOptionPane.YES_NO_OPTION,
201
                        JOptionPane.QUESTION_MESSAGE);
202
                if( r == JOptionPane.YES_OPTION ) {
203
                    n.cancel();
204
                }
205
            }
206
        }
207
    }
208
209
    private void startObservation() {
210
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
211
        editingNotificationManager.addObserver(this);
212
    }
213
214
    private void stopObservation() {
215
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
216
        editingNotificationManager.deleteObserver(this);
217
    }
218
}