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

History | View | Annotate | Download (11 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

    
31
import javax.swing.JPanel;
32
import javax.swing.JSplitPane;
33

    
34
import org.cresques.cts.IProjection;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.andami.ui.mdiManager.IWindowTransform;
38
import org.gvsig.andami.ui.mdiManager.WindowInfo;
39
import org.gvsig.app.extension.ViewPropertiesExtension;
40
import org.gvsig.app.project.ProjectManager;
41
import org.gvsig.app.project.documents.gui.WindowLayout;
42
import org.gvsig.app.project.documents.view.MapOverview;
43
import org.gvsig.app.project.documents.view.ViewDocument;
44
import org.gvsig.app.project.documents.view.toc.gui.TOC;
45
import org.gvsig.fmap.mapcontrol.MapControl;
46
import org.gvsig.utils.exceptionHandling.ExceptionListener;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50
/**
51
 * 
52
 * @author 2005- Vicente Caballero
53
 * @author 2009- Joaquin del Cerro
54
 * 
55
 */
56

    
57
public abstract class AbstractViewPanel extends JPanel implements IView,
58
    IWindowTransform, PropertyChangeListener {
59

    
60
        private static final Logger logger = LoggerFactory.getLogger(AbstractViewPanel.class);
61
        
62
    private static final long serialVersionUID = -259620280790490262L;
63

    
64
    private static final int DEFAULT_HEIGHT = 450;
65

    
66
    private static final int DEFAULT_WIDTH = 700;
67

    
68
    protected MapControl m_MapControl;
69
    protected MapOverview m_MapLoc;
70

    
71
    // store the properties of the window
72
    protected WindowInfo m_viewInfo = null;
73
    protected WindowLayout windowLayout = null;
74

    
75
    protected TOC m_TOC;
76
    protected ViewDocument modelo;
77
    protected ViewExceptionListener mapControlExceptionListener =
78
        new ViewExceptionListener();
79

    
80
    protected boolean isPalette = false;
81
    protected MapOverViewPalette movp;
82
    protected ViewSplitPane tempMainSplit = null;
83
    protected JSplitPane tempSplitToc = null;
84

    
85
    
86
    // This class is to temporarily solve a problem with jcrs 
87
    private class ViewProjectionContainer implements org.gvsig.andami.ProjectionContainerManager.ProjectionContainer {
88
                public Object getCurrentProjection() {
89
                        return getProjection();
90
                }
91
                public void set() {
92
                    org.gvsig.andami.ProjectionContainerManager.set(this);
93
                }
94
                public void unset() {
95
                        org.gvsig.andami.ProjectionContainerManager.unset(this);
96
                }
97
    }
98
    private ViewProjectionContainer viewProjectionContainer = new ViewProjectionContainer();
99
    
100
        public void windowActivated() {
101
        // This code is to temporarily solve a problem with jcrs 
102
            viewProjectionContainer.set();
103
    }
104

    
105
    public void toPalette() {
106
        // By default do nothing
107
    }
108

    
109
    public void restore() {
110
        // By default do nothing
111
    }
112

    
113
    protected class ViewSplitPane extends JSplitPane {
114

    
115
        private static final long serialVersionUID = -7506953938664812652L;
116
        private int lastDivider = 0;
117

    
118
        public ViewSplitPane(int horizontal_split) {
119
            super(horizontal_split);
120
        }
121

    
122
        protected void paintChildren(Graphics g) {
123
            if (lastDivider != lastDividerLocation) {
124
                    logger.debug("paintChildren = "+ this.lastDividerLocation);
125
                lastDivider = lastDividerLocation;
126
            }
127
            super.paintChildren(g);
128
        }
129

    
130
    }
131

    
132
    /**
133
     * Creates a new View object. Before using it, it must be initialized
134
     * using the <code>initialize()</code> method.
135
     * 
136
     * @see initialize()
137
     */
138
    public AbstractViewPanel() {
139
            ProjectManager.getInstance().getCurrentProject().addPropertyChangeListener(this);
140
    }
141

    
142
    /**
143
     * Create the internal componentes and populate the window with them.
144
     * If the layout properties were set using the
145
     * <code>setWindowData(WindowData)</code> method, the window will be
146
     * populated according to this
147
     * properties.
148
     */
149
    protected void initialize() {
150
        // Do nothing
151
    }
152

    
153
    /**
154
     * This method is used to get <strong>an initial</strong> ViewInfo object
155
     * for this View. It is not intended to retrieve the ViewInfo object in a
156
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
157
     * to retrieve the ViewInfo object at any time after the creation of the
158
     * object.
159
     * 
160
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
161
     */
162
    public WindowInfo getWindowInfo() {
163
        if (m_viewInfo == null) {
164
            m_viewInfo =
165
                new WindowInfo(WindowInfo.ICONIFIABLE | WindowInfo.RESIZABLE
166
                    | WindowInfo.MAXIMIZABLE);
167

    
168
            m_viewInfo.setWidth(DEFAULT_WIDTH);
169
            m_viewInfo.setHeight(DEFAULT_HEIGHT);
170
            m_viewInfo.setTitle(PluginServices.getText(this, "Vista") + ": "
171
                + modelo.getName());
172
        }
173
        return m_viewInfo;
174
    }
175

    
176
    /**
177
     * @see org.gvsig.andami.ui.mdiManager.IWindowListener#windowClosed()
178
     */
179
    public void windowClosed() {
180
            viewProjectionContainer.unset();
181
        if (movp != null) {
182
            PluginServices.getMDIManager().closeWindow(movp);
183
        }
184
        if (modelo != null) {
185
            modelo.setWindowLayout(this.getWindowLayout());
186
        }
187
    }
188

    
189
    /**
190
     * @deprecated See {@link #getViewDocument()}
191
     */
192
    public ViewDocument getModel() {
193
        return modelo;
194
    }
195

    
196
    public ViewDocument getViewDocument() {
197
        return modelo;
198
    }
199

    
200
    public MapOverview getMapOverview() {
201
        return m_MapLoc;
202
    }
203

    
204
    public MapControl getMapControl() {
205
        return m_MapControl;
206
    }
207

    
208
    public TOC getTOC() {
209
        return m_TOC;
210
    }
211

    
212
    /*
213
     * (non-Javadoc)
214
     * 
215
     * @see com.iver.mdiApp.ui.MDIManager.SingletonView#getModel()
216
     */
217

    
218
    public Object getWindowModel() {
219
        return modelo;
220
    }
221

    
222
    /**
223
     * This method is used to get <strong>an initial</strong> ViewInfo object
224
     * for this View. It is not intended to retrieve the ViewInfo object in a
225
     * later time. <strong>Use PluginServices.getMDIManager().getViewInfo(view)
226
     * to retrieve the ViewInfo object at any time after the creation of the
227
     * object.
228
     * 
229
     * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
230
     */
231

    
232
    public boolean isPalette() {
233
        return isPalette;
234
    }
235

    
236
    public void repaintMap() {
237
        m_MapControl.drawMap(false);
238
    }
239

    
240
    public class ViewExceptionListener implements ExceptionListener {
241

    
242
        /**
243
         * @see com.iver.cit.gvsig.fmap.ExceptionListener#exceptionThrown(java.lang.Throwable)
244
         */
245
        public void exceptionThrown(Throwable t) {
246
            NotificationManager.addError(t.getMessage(), t);
247
        }
248

    
249
    }
250

    
251
    /**
252
     * @return
253
     */
254
    public BufferedImage getImage() {
255
        return m_MapControl.getImage();
256
    }
257

    
258
    public void setProjection(IProjection proj) {
259
        getMapControl().setProjection(proj);
260
    }
261

    
262
    public IProjection getProjection() {
263
        return getMapControl().getProjection();
264
    }
265

    
266
    public WindowLayout getWindowLayout() {
267
        if (windowLayout == null) {
268
            windowLayout = new WindowLayout();
269
        }
270
        windowLayout.set("MainWindow.X", Integer.toString(this.getX()));
271
        windowLayout.set("MainWindow.Y", Integer.toString(this.getY()));
272
        windowLayout.set("MainWindow.Width", Integer.toString(this.getWidth()));
273
        windowLayout.set("MainWindow.Height",
274
            Integer.toString(this.getHeight()));
275

    
276
        windowLayout.set("MainDivider.Location",
277
            Integer.toString(tempMainSplit.getDividerLocation()));
278
        windowLayout.set("MainDivider.X",
279
            Integer.toString(tempMainSplit.getX()));
280
        windowLayout.set("MainDivider.Y",
281
            Integer.toString(tempMainSplit.getY()));
282
        windowLayout.set("MainDivider.Width",
283
            Integer.toString(tempMainSplit.getWidth()));
284
        windowLayout.set("MainDivider.Height",
285
            Integer.toString(tempMainSplit.getHeight()));
286

    
287
        if (isPalette()) {
288
            windowLayout.set("GraphicLocator.isPalette", "true");
289
        } else {
290
            windowLayout.set("GraphicLocator.isPalette", "false");
291
            if (tempSplitToc != null) {
292
                windowLayout.set("TOCDivider.Location",
293
                    Integer.toString(tempSplitToc.getDividerLocation()));
294
                windowLayout.set("TOCDivider.X",
295
                    Integer.toString(tempSplitToc.getX()));
296
                windowLayout.set("TOCDivider.Y",
297
                    Integer.toString(tempSplitToc.getY()));
298
                windowLayout.set("TOCDivider.Width",
299
                    Integer.toString(tempSplitToc.getWidth()));
300
                windowLayout.set("TOCDivider.Height",
301
                    Integer.toString(tempSplitToc.getHeight()));
302
            }
303
        }
304
        if (m_TOC != null) {
305
            windowLayout.set("TOC.Width", Integer.toString(m_TOC.getWidth()));
306
            windowLayout.set("TOC.Height", Integer.toString(m_TOC.getHeight()));
307
        }
308
        if (m_MapControl != null) {
309
            windowLayout.set("MapControl.Width",
310
                Integer.toString(m_MapControl.getWidth()));
311
            windowLayout.set("MapControl.Height",
312
                Integer.toString(m_MapControl.getHeight()));
313
        }
314
        if (m_MapLoc != null) {
315
            windowLayout.set("GraphicLocator.Width",
316
                Integer.toString(m_MapLoc.getWidth()));
317
            windowLayout.set("GraphicLocator.Height",
318
                Integer.toString(m_MapLoc.getHeight()));
319
        }
320

    
321
        return windowLayout;
322
    }
323

    
324
    public void setWindowLayout(WindowLayout data) {
325
        windowLayout = data;
326
    }
327
    
328
        public void propertyChange(PropertyChangeEvent evt) {
329
                // Detect when the related document has been removed
330
                // and close or dispose whatever is needed
331
                if ("delDocument".equals(evt.getPropertyName())) {
332
                        if (evt.getOldValue() != null
333
                                        && evt.getOldValue() instanceof ViewDocument) {
334
                                ViewDocument viewDocument = (ViewDocument) evt.getOldValue();
335
                                ViewDocument myViewDocument = getViewDocument();
336
                                if (myViewDocument != null
337
                                                && myViewDocument.equals(viewDocument)) {
338
                                        m_MapControl.dispose();
339
                                        m_MapLoc.dispose();
340
                                }
341
                        }
342
                }
343
        }
344
}