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

History | View | Annotate | Download (13 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
    
91
    // This class is to temporarily solve a problem with jcrs 
92
    private class ViewProjectionContainer implements org.gvsig.andami.ProjectionContainerManager.ProjectionContainer {
93
                public Object getCurrentProjection() {
94
                        return getProjection();
95
                }
96
                public void set() {
97
                    org.gvsig.andami.ProjectionContainerManager.set(this);
98
                }
99
                public void unset() {
100
                        org.gvsig.andami.ProjectionContainerManager.unset(this);
101
                }
102
    }
103
    private ViewProjectionContainer viewProjectionContainer = new ViewProjectionContainer();
104
    
105
        public void windowActivated() {
106
        // This code is to temporarily solve a problem with jcrs 
107
            viewProjectionContainer.set();
108
    }
109

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

    
114
    public void toPalette() {
115
        // By default do nothing
116
    }
117

    
118
    public void restore() {
119
        // By default do nothing
120
    }
121

    
122
    protected class ViewSplitPane extends JSplitPane {
123

    
124
        private static final long serialVersionUID = -7506953938664812652L;
125
        private int lastDivider = 0;
126

    
127
        public ViewSplitPane(int horizontal_split) {
128
            super(horizontal_split);
129
        }
130

    
131
        protected void paintChildren(Graphics g) {
132
            if (lastDivider != lastDividerLocation) {
133
                    logger.debug("paintChildren = "+ this.lastDividerLocation);
134
                lastDivider = lastDividerLocation;
135
            }
136
            super.paintChildren(g);
137
        }
138

    
139
    }
140

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

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

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

    
177
            m_viewInfo.setWidth(DEFAULT_WIDTH);
178
            m_viewInfo.setHeight(DEFAULT_HEIGHT);
179
            m_viewInfo.setTitle(PluginServices.getText(this, "Vista") + ": "
180
                + modelo.getName());
181
        }
182
        return m_viewInfo;
183
    }
184

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

    
198
    /**
199
     * @deprecated See {@link #getViewDocument()}
200
     */
201
    public ViewDocument getModel() {
202
        return modelo;
203
    }
204

    
205
    public ViewDocument getViewDocument() {
206
        return modelo;
207
    }
208

    
209
    public MapOverview getMapOverview() {
210
        return m_MapLoc;
211
    }
212

    
213
    public MapControl getMapControl() {
214
        return m_MapControl;
215
    }
216

    
217
    public TOC getTOC() {
218
        return m_TOC;
219
    }
220

    
221
    /*
222
     * (non-Javadoc)
223
     * 
224
     * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getModel()
225
     */
226

    
227
    public Object getWindowModel() {
228
        return modelo;
229
    }
230

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

    
241
    public boolean isPalette() {
242
        return isPalette;
243
    }
244

    
245
    public void repaintMap() {
246
        m_MapControl.drawMap(false);
247
    }
248

    
249
    public class ViewExceptionListener implements ExceptionListener {
250

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

    
262
            Throwable exception;
263

    
264
            ExceptionIterator(Throwable exception) {
265
                this.exception = exception;
266
            }
267

    
268
            @Override
269
            public boolean hasNext() {
270
                return this.exception != null;
271
            }
272

    
273
            @Override
274
            public Object next() {
275
                Throwable exception = this.exception;
276
                this.exception = exception.getCause();
277
                return exception;
278
            }
279

    
280
            @Override
281
            public void remove() {
282
                throw new UnsupportedOperationException();
283
            }
284
        }
285
        
286
        private String getMessages(Throwable ex) {
287
            StringBuilder msg = new StringBuilder();
288
            boolean firstline = true;
289
            Iterator exceptions = new ExceptionIterator(ex);
290
            String lastmsg = "";
291

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

    
314
    }
315

    
316
    /**
317
     * @return
318
     */
319
    public BufferedImage getImage() {
320
        return m_MapControl.getImage();
321
    }
322

    
323
    public void setProjection(IProjection proj) {
324
        getMapControl().setProjection(proj);
325
    }
326

    
327
    public IProjection getProjection() {
328
        return getMapControl().getProjection();
329
    }
330

    
331
    public WindowLayout getWindowLayout() {
332
        if (windowLayout == null) {
333
            windowLayout = new WindowLayout();
334
        }
335
        windowLayout.set("MainWindow.X", Integer.toString(this.getX()));
336
        windowLayout.set("MainWindow.Y", Integer.toString(this.getY()));
337
        windowLayout.set("MainWindow.Width", Integer.toString(this.getWidth()));
338
        windowLayout.set("MainWindow.Height",
339
            Integer.toString(this.getHeight()));
340

    
341
        windowLayout.set("MainDivider.Location",
342
            Integer.toString(tempMainSplit.getDividerLocation()));
343
        windowLayout.set("MainDivider.X",
344
            Integer.toString(tempMainSplit.getX()));
345
        windowLayout.set("MainDivider.Y",
346
            Integer.toString(tempMainSplit.getY()));
347
        windowLayout.set("MainDivider.Width",
348
            Integer.toString(tempMainSplit.getWidth()));
349
        windowLayout.set("MainDivider.Height",
350
            Integer.toString(tempMainSplit.getHeight()));
351

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

    
386
        return windowLayout;
387
    }
388

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