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

History | View | Annotate | Download (7.54 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.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
import org.gvsig.tools.observer.Observable;
40
import org.gvsig.tools.observer.Observer;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44

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

    
69
        this.chkAskToAceptNotification.addActionListener(new ActionListener() {
70
            public void actionPerformed(ActionEvent ae) {
71
                onAskToAceptNotification();
72
            }
73
        });
74
        this.chkAskToAceptNotification.setSelected(false);
75
        this.addComponentListener(new ComponentListener() {
76
            public void componentResized(ComponentEvent ce) {
77
            }
78
            public void componentMoved(ComponentEvent ce) {
79
            }
80
            public void componentShown(ComponentEvent ce) {
81
                closed = false;
82
            }
83
            public void componentHidden(ComponentEvent ce) {
84
                onClose();
85
            }
86
        });
87
        this.setPreferredSize(new Dimension(300, 400));
88
    }
89
    
90
    public boolean isClosed() {
91
        return closed;
92
    }
93

    
94
    private void onClose() {
95
        this.stopObservation();
96
        this.closed = true;
97
    }
98
    
99
    private void onClear() {
100
        this.txtNotifications.setText("");
101
        this.buffer = new StringBuilder();
102
    }
103
    
104
    private void onAskToAceptNotification() {
105
        this.askToAceptNotification = this.chkAskToAceptNotification.isSelected();
106
    }
107

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

    
170
        b.append("<hr><br>\n");
171
        this.addText(b.toString());
172
        
173
        if( n.isCancelable() ) {
174
            if( this.chkShowFeatureInForm.isSelected() && n.getFeature()!=null ) {
175
                try {
176
                    DynObjectEditor editor = new DynObjectEditor(n.getFeature().getAsDynObject());
177
                    editor.editObject(true);
178
                    if( this.askToAceptNotification ) {
179
                        if( editor.isCanceled() ) {
180
                            n.cancel();
181
                        }
182
                    }
183
                } catch (Exception ex) {
184
                    logger.warn("Problems showing the feature in a form.",ex);
185
                }
186
            } else if( this.askToAceptNotification ) {
187
                ApplicationManager application = ApplicationLocator.getManager();
188
                int r = application.confirmDialog(
189
                        "Do you want to cancel the process that generated this notification?", 
190
                        "Cancel process", 
191
                        JOptionPane.YES_NO_OPTION,
192
                        JOptionPane.QUESTION_MESSAGE);
193
                if( r == JOptionPane.YES_OPTION ) {
194
                    n.cancel();
195
                }
196
            }
197
        }
198
    }
199

    
200
    private void startObservation() {
201
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
202
        editingNotificationManager.addObserver(this);
203
    }
204

    
205
    private void stopObservation() {
206
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
207
        editingNotificationManager.deleteObserver(this);
208
    }
209
}