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 / project / documents / view / gui / AbstractViewPanel.java @ 43599

History | View | Annotate | Download (13.2 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.project.documents.view.gui;
25

    
26
import java.awt.Graphics;
27
import java.awt.image.BufferedImage;
28
import java.beans.PropertyChangeEvent;
29
import java.beans.PropertyChangeListener;
30
import java.util.Iterator;
31
import java.util.List;
32
import javax.swing.JComponent;
33

    
34
import javax.swing.JPanel;
35
import javax.swing.JSplitPane;
36
import org.apache.commons.lang3.StringUtils;
37

    
38
import org.cresques.cts.IProjection;
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.messages.NotificationManager;
41
import org.gvsig.andami.ui.mdiManager.IWindowTransform;
42
import org.gvsig.andami.ui.mdiManager.WindowInfo;
43
import org.gvsig.app.project.ProjectManager;
44
import org.gvsig.app.project.documents.gui.WindowLayout;
45
import org.gvsig.app.project.documents.view.MapOverview;
46
import org.gvsig.app.project.documents.view.ViewDocument;
47
import org.gvsig.app.project.documents.view.toc.actions.ShowLayerErrorsTocMenuEntry;
48
import org.gvsig.app.project.documents.view.toc.gui.TOC;
49
import org.gvsig.fmap.mapcontrol.MapControl;
50
import org.gvsig.tools.swing.api.Component;
51
import org.gvsig.utils.exceptionHandling.ExceptionListener;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
/**
56
 * 
57
 * @author 2005- Vicente Caballero
58
 * @author 2009- Joaquin del Cerro
59
 * 
60
 */
61

    
62
public abstract class AbstractViewPanel extends JPanel implements IView,
63
    IWindowTransform, PropertyChangeListener, Component {
64

    
65
        protected static final Logger logger = LoggerFactory.getLogger(AbstractViewPanel.class);
66
        
67
    private static final long serialVersionUID = -259620280790490262L;
68

    
69
    private static final int DEFAULT_HEIGHT = 450;
70

    
71
    private static final int DEFAULT_WIDTH = 700;
72

    
73
    protected MapControl m_MapControl;
74
    protected MapOverview m_MapLoc;
75

    
76
    // store the properties of the window
77
    protected WindowInfo m_viewInfo = null;
78
    protected WindowLayout windowLayout = null;
79

    
80
    protected TOC m_TOC;
81
    protected ViewDocument modelo;
82
    protected ViewExceptionListener mapControlExceptionListener =
83
        new ViewExceptionListener();
84

    
85
    protected boolean isPalette = false;
86
    protected MapOverViewPalette movp;
87
    protected ViewSplitPane tempMainSplit = null;
88
    protected JSplitPane tempSplitToc = null;
89

    
90
    protected ViewInformationArea viewInformationArea;
91
    
92
    // This class is to temporarily solve a problem with jcrs 
93
    private class ViewProjectionContainer implements org.gvsig.andami.ProjectionContainerManager.ProjectionContainer {
94
                public Object getCurrentProjection() {
95
                        return getProjection();
96
                }
97
                public void set() {
98
                    org.gvsig.andami.ProjectionContainerManager.set(this);
99
                }
100
                public void unset() {
101
                        org.gvsig.andami.ProjectionContainerManager.unset(this);
102
                }
103
    }
104
    private ViewProjectionContainer viewProjectionContainer = new ViewProjectionContainer();
105
    
106
        public void windowActivated() {
107
        // This code is to temporarily solve a problem with jcrs 
108
            viewProjectionContainer.set();
109
    }
110

    
111
    public JComponent asJComponent() {
112
        return this;
113
    }
114

    
115
    public ViewInformationArea getViewInformationArea() {
116
        return this.viewInformationArea;
117
    }
118
    
119
    public void toPalette() {
120
        // By default do nothing
121
    }
122

    
123
    public void restore() {
124
        // By default do nothing
125
    }
126

    
127
    protected class ViewSplitPane extends JSplitPane {
128

    
129
        private static final long serialVersionUID = -7506953938664812652L;
130
        private int lastDivider = 0;
131

    
132
        public ViewSplitPane(int horizontal_split) {
133
            super(horizontal_split);
134
        }
135

    
136
        protected void paintChildren(Graphics g) {
137
            if (lastDivider != lastDividerLocation) {
138
                    logger.debug("paintChildren = "+ this.lastDividerLocation);
139
                lastDivider = lastDividerLocation;
140
            }
141
            super.paintChildren(g);
142
        }
143

    
144
    }
145

    
146
    /**
147
     * Creates a new View object. Before using it, it must be initialized
148
     * using the <code>initialize()</code> method.
149
     * 
150
     * @see initialize()
151
     */
152
    public AbstractViewPanel() {
153
            ProjectManager.getInstance().getCurrentProject().addPropertyChangeListener(this);
154
    }
155

    
156
    /**
157
     * Create the internal componentes and populate the window with them.
158
     * If the layout properties were set using the
159
     * <code>setWindowData(WindowData)</code> method, the window will be
160
     * populated according to this
161
     * properties.
162
     */
163
    protected void initialize() {
164
        // Do nothing
165
    }
166

    
167
    /**
168
     * This method is used to get <strong>an initial</strong> ViewInfo object
169
     * for this View. It is not intended to retrieve the ViewInfo object in a
170
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
171
     * to retrieve the ViewInfo object at any time after the creation of the
172
     * object.
173
     * 
174
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
175
     */
176
    public WindowInfo getWindowInfo() {
177
        if (m_viewInfo == null) {
178
            m_viewInfo =
179
                new WindowInfo(WindowInfo.ICONIFIABLE | WindowInfo.RESIZABLE
180
                    | WindowInfo.MAXIMIZABLE);
181

    
182
            m_viewInfo.setWidth(DEFAULT_WIDTH);
183
            m_viewInfo.setHeight(DEFAULT_HEIGHT);
184
            m_viewInfo.setTitle(PluginServices.getText(this, "Vista") + ": "
185
                + modelo.getName());
186
        }
187
        return m_viewInfo;
188
    }
189

    
190
    /**
191
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
192
     */
193
    public void windowClosed() {
194
            viewProjectionContainer.unset();
195
        if (movp != null) {
196
            PluginServices.getMDIManager().closeWindow(movp);
197
        }
198
        if (modelo != null) {
199
            modelo.setWindowLayout(this.getWindowLayout());
200
        }
201
    }
202

    
203
    /**
204
     * @deprecated See {@link #getViewDocument()}
205
     */
206
    public ViewDocument getModel() {
207
        return modelo;
208
    }
209

    
210
    public ViewDocument getViewDocument() {
211
        return modelo;
212
    }
213

    
214
    public MapOverview getMapOverview() {
215
        return m_MapLoc;
216
    }
217

    
218
    public MapControl getMapControl() {
219
        return m_MapControl;
220
    }
221

    
222
    public TOC getTOC() {
223
        return m_TOC;
224
    }
225

    
226
    /*
227
     * (non-Javadoc)
228
     * 
229
     * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getModel()
230
     */
231

    
232
    public Object getWindowModel() {
233
        return modelo;
234
    }
235

    
236
    /**
237
     * This method is used to get <strong>an initial</strong> ViewInfo object
238
     * for this View. It is not intended to retrieve the ViewInfo object in a
239
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
240
     * to retrieve the ViewInfo object at any time after the creation of the
241
     * object.
242
     * 
243
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
244
     */
245

    
246
    public boolean isPalette() {
247
        return isPalette;
248
    }
249

    
250
    public void repaintMap() {
251
        m_MapControl.drawMap(false);
252
    }
253

    
254
    public class ViewExceptionListener implements ExceptionListener {
255

    
256
        /**
257
         * @see com.iver.cit.gvsig.fmap.ExceptionListener#exceptionThrown(java.lang.Throwable)
258
         */
259
        @Override
260
        public void exceptionThrown(Throwable t) {
261
            
262
            NotificationManager.addError(getMessages(t), t);
263
        }
264
        
265
        private class ExceptionIterator implements Iterator {
266

    
267
            Throwable exception;
268

    
269
            ExceptionIterator(Throwable exception) {
270
                this.exception = exception;
271
            }
272

    
273
            @Override
274
            public boolean hasNext() {
275
                return this.exception != null;
276
            }
277

    
278
            @Override
279
            public Object next() {
280
                Throwable exception = this.exception;
281
                this.exception = exception.getCause();
282
                return exception;
283
            }
284

    
285
            @Override
286
            public void remove() {
287
                throw new UnsupportedOperationException();
288
            }
289
        }
290
        
291
        private String getMessages(Throwable ex) {
292
            StringBuilder msg = new StringBuilder();
293
            boolean firstline = true;
294
            Iterator exceptions = new ExceptionIterator(ex);
295
            String lastmsg = "";
296

    
297
            while (exceptions.hasNext()) {
298
                Throwable ex1 = ((Throwable) exceptions.next());
299
                String message;
300
                message = ex1.getMessage();
301
                if ( !StringUtils.isEmpty(message) ) {
302
                    if( firstline ) {
303
                        msg.append(message);
304
                        msg.append("\n\n");
305
                        firstline = false;
306
                    } else {
307
                        if (!message.equalsIgnoreCase(lastmsg)) {
308
                            msg.append("- ");
309
                            msg.append(message.replace("\n","\n  "));
310
                            msg.append("\n");
311
                        }
312
                    }
313
                    lastmsg = message;
314
                }
315
            }
316
            return msg.toString();
317
        }
318

    
319
    }
320

    
321
    /**
322
     * @return
323
     */
324
    public BufferedImage getImage() {
325
        return m_MapControl.getImage();
326
    }
327

    
328
    public void setProjection(IProjection proj) {
329
        getMapControl().setProjection(proj);
330
    }
331

    
332
    public IProjection getProjection() {
333
        return getMapControl().getProjection();
334
    }
335

    
336
    public WindowLayout getWindowLayout() {
337
        if (windowLayout == null) {
338
            windowLayout = new WindowLayout();
339
        }
340
        windowLayout.set("MainWindow.X", Integer.toString(this.getX()));
341
        windowLayout.set("MainWindow.Y", Integer.toString(this.getY()));
342
        windowLayout.set("MainWindow.Width", Integer.toString(this.getWidth()));
343
        windowLayout.set("MainWindow.Height",
344
            Integer.toString(this.getHeight()));
345

    
346
        windowLayout.set("MainDivider.Location",
347
            Integer.toString(tempMainSplit.getDividerLocation()));
348
        windowLayout.set("MainDivider.X",
349
            Integer.toString(tempMainSplit.getX()));
350
        windowLayout.set("MainDivider.Y",
351
            Integer.toString(tempMainSplit.getY()));
352
        windowLayout.set("MainDivider.Width",
353
            Integer.toString(tempMainSplit.getWidth()));
354
        windowLayout.set("MainDivider.Height",
355
            Integer.toString(tempMainSplit.getHeight()));
356

    
357
        if (isPalette()) {
358
            windowLayout.set("GraphicLocator.isPalette", "true");
359
        } else {
360
            windowLayout.set("GraphicLocator.isPalette", "false");
361
            if (tempSplitToc != null) {
362
                windowLayout.set("TOCDivider.Location",
363
                    Integer.toString(tempSplitToc.getDividerLocation()));
364
                windowLayout.set("TOCDivider.X",
365
                    Integer.toString(tempSplitToc.getX()));
366
                windowLayout.set("TOCDivider.Y",
367
                    Integer.toString(tempSplitToc.getY()));
368
                windowLayout.set("TOCDivider.Width",
369
                    Integer.toString(tempSplitToc.getWidth()));
370
                windowLayout.set("TOCDivider.Height",
371
                    Integer.toString(tempSplitToc.getHeight()));
372
            }
373
        }
374
        if (m_TOC != null) {
375
            windowLayout.set("TOC.Width", Integer.toString(m_TOC.getWidth()));
376
            windowLayout.set("TOC.Height", Integer.toString(m_TOC.getHeight()));
377
        }
378
        if (m_MapControl != null) {
379
            windowLayout.set("MapControl.Width",
380
                Integer.toString(m_MapControl.getWidth()));
381
            windowLayout.set("MapControl.Height",
382
                Integer.toString(m_MapControl.getHeight()));
383
        }
384
        if (m_MapLoc != null) {
385
            windowLayout.set("GraphicLocator.Width",
386
                Integer.toString(m_MapLoc.getWidth()));
387
            windowLayout.set("GraphicLocator.Height",
388
                Integer.toString(m_MapLoc.getHeight()));
389
        }
390

    
391
        return windowLayout;
392
    }
393

    
394
    public void setWindowLayout(WindowLayout data) {
395
        windowLayout = data;
396
    }
397
    
398
        public void propertyChange(PropertyChangeEvent evt) {
399
                // Detect when the related document has been removed
400
                // and close or dispose whatever is needed
401
                if ("delDocument".equals(evt.getPropertyName())) {
402
                        if (evt.getOldValue() != null
403
                                        && evt.getOldValue() instanceof ViewDocument) {
404
                                ViewDocument viewDocument = (ViewDocument) evt.getOldValue();
405
                                ViewDocument myViewDocument = getViewDocument();
406
                                if (myViewDocument != null
407
                                                && myViewDocument.equals(viewDocument)) {
408
                                        m_MapControl.dispose();
409
                                        m_MapLoc.dispose();
410
                                }
411
                        }
412
                }
413
        }
414
}