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

History | View | Annotate | Download (8.04 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.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.fmap.dal.EditingNotification;
36
import org.gvsig.fmap.dal.EditingNotificationManager;
37
import org.gvsig.fmap.dal.swing.DALSwingLocator;
38
import org.gvsig.fmap.mapcontext.layers.FLayer;
39
import org.gvsig.fmap.mapcontrol.MapControlLocator;
40
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
41
import org.gvsig.tools.dynobject.DynObject;
42
import org.gvsig.tools.observer.Observable;
43
import org.gvsig.tools.observer.Observer;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47

    
48
public class EditingListenerPanel extends EditingListenerPanelLayout implements Observer {
49
    private static final long serialVersionUID = -4106763914259136033L;
50
    private static final Logger logger = LoggerFactory.getLogger(EditingListenerPanel.class);
51
    private boolean askToAceptNotification;
52
    private boolean closed = true;
53
    private StringBuilder buffer = new StringBuilder();
54
    
55
    public EditingListenerPanel() {
56
        initComponents();
57
        startObservation();
58
    }
59
    
60
    private void initComponents() {
61
        this.btnClear.addActionListener(new ActionListener() {
62
            public void actionPerformed(ActionEvent ae) {
63
                onClear();
64
            }
65
        });
66
        this.btnClose.addActionListener(new ActionListener() {
67
            public void actionPerformed(ActionEvent ae) {
68
                setVisible(false);
69
            }
70
        });
71

    
72
        this.chkAskToAceptNotification.addActionListener(new ActionListener() {
73
            public void actionPerformed(ActionEvent ae) {
74
                onAskToAceptNotification();
75
            }
76
        });
77
        this.chkSkipFeatureValidation.setSelected(false);
78

    
79
        this.chkAskToAceptNotification.setSelected(false);
80
        this.addComponentListener(new ComponentListener() {
81
            public void componentResized(ComponentEvent ce) {
82
            }
83
            public void componentMoved(ComponentEvent ce) {
84
            }
85
            public void componentShown(ComponentEvent ce) {
86
                closed = false;
87
            }
88
            public void componentHidden(ComponentEvent ce) {
89
                onClose();
90
            }
91
        });
92
        this.setPreferredSize(new Dimension(300, 400));
93
    }
94
    
95
    public boolean isClosed() {
96
        return closed;
97
    }
98

    
99
    private void onClose() {
100
        this.stopObservation();
101
        this.closed = true;
102
    }
103
    
104
    private void onClear() {
105
        this.txtNotifications.setText("");
106
        this.buffer = new StringBuilder();
107
    }
108
    
109
    private void onAskToAceptNotification() {
110
        this.askToAceptNotification = this.chkAskToAceptNotification.isSelected();
111
    }
112

    
113
    private void addText(String text) {
114
        this.buffer.append(text);
115
        if( this.buffer.length()> 30000) {
116
            this.buffer.delete(0, 20000);
117
        }
118
        String s = "<html>\n"+buffer.toString()+"</html>\n";
119
        this.txtNotifications.setText(s);
120
        this.txtNotifications.setSelectionStart(s.length());
121
    }
122
    
123
    public void update(Observable observable, Object notification) {
124
        EditingNotification n = (EditingNotification) notification;
125
        StringBuilder b = new StringBuilder();
126
        b.append("<b>Notification type</b>: ");
127
        b.append(n.getType());
128
        b.append("<br>\n");
129
        if( n.getSource() == null ) {
130
            b.append("<b>Source</b>: (unknow)<br>\n");
131
        } else {
132
            b.append("<b>Source</b>: <i>");
133
            b.append(n.getSource().getClass().getName());
134
            b.append("</i> (");
135
            b.append(n.getSource().hashCode());
136
            b.append(") <br>\n");
137
        }
138
        if( n.getDocument() == null ) {
139
            b.append("<b>Document</b>: (unknow)<br>\n");
140
        } else {
141
            b.append("<b>Document</b>: <i>");
142
            b.append(n.getDocument().toString());
143
            b.append("</i> (");
144
            b.append(n.getDocument().hashCode());
145
            b.append(", ");
146
            b.append(n.getDocument().getClass().getName());
147
            b.append(") <br>\n");
148
        }
149
        if( n.getAuxData()== null ) {
150
            b.append("<b>Layer</b>: (unknow)<br>\n");
151
        } else {
152
            b.append("<b>Layer</b>: <i>");
153
            b.append(((FLayer)(n.getAuxData())).getName());
154
            b.append("</i><br>\n");
155
        }
156
        if( n.getStore()== null ) {
157
            b.append("<b>Store</b>: (unknow)<br>\n");
158
        } else {
159
            b.append("<b>Store</b>: <i>");
160
            b.append(n.getStore().getName());
161
            b.append("</i> (");
162
            b.append(n.getStore().hashCode());
163
            b.append(", ");
164
            b.append(n.getStore().getClass().getName());
165
            b.append(") <br>\n");
166
        }
167
        if( n.getFeature()== null ) {
168
            b.append("<b>Feature</b>: (unknow)<br>\n");
169
        } else {
170
            b.append("<b>Feature</b>: ");
171
            b.append(n.getFeature().toString());
172
            b.append("<br>\n");
173
        }
174

    
175
        b.append("<hr><br>\n");
176
        this.addText(b.toString());
177

    
178
        n.setSkipFeatureValidation(this.chkSkipFeatureValidation.isSelected());
179
        
180
        if( n.isCancelable() ) {
181
            if( this.chkShowFeatureInForm.isSelected() && n.getFeature()!=null ) {
182
                try {
183
                    DynObject data = n.getFeature().getAsDynObject();
184
                    DynObjectEditor editor = new DynObjectEditor(data);
185
                    editor.setTitle("Editing monitor - "+n.getFeature().getType().getName());
186
                    editor.editObject(true);
187
                    if( editor.isCanceled() ) {
188
                        if( this.askToAceptNotification ) {
189
                                n.cancel();
190
                        }
191
                    } else {
192
                         editor.getData(data);
193
                    }
194
                } catch (Exception ex) {
195
                    logger.warn("Problems showing the feature in a form.",ex);
196
                }
197
            } else if( this.askToAceptNotification ) {
198
                ApplicationManager application = ApplicationLocator.getManager();
199
                int r = application.confirmDialog(
200
                        "Do you want to cancel the process that generated this notification?", 
201
                        "Cancel process", 
202
                        JOptionPane.YES_NO_OPTION,
203
                        JOptionPane.QUESTION_MESSAGE);
204
                if( r == JOptionPane.YES_OPTION ) {
205
                    n.cancel();
206
                }
207
            }
208
        }
209
    }
210

    
211
    private void startObservation() {
212
        EditingNotificationManager editingNotificationManager = DALSwingLocator.getEditingNotificationManager();
213
        editingNotificationManager.addObserver(this);
214
    }
215

    
216
    private void stopObservation() {
217
        EditingNotificationManager editingNotificationManager = DALSwingLocator.getEditingNotificationManager();
218
        editingNotificationManager.deleteObserver(this);
219
    }
220
}