001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gis.display;
031
032import java.awt.BorderLayout;
033import java.awt.Graphics;
034import java.awt.GridBagConstraints;
035import java.awt.GridBagLayout;
036import java.awt.Insets;
037import java.awt.event.ActionEvent;
038import java.awt.event.ActionListener;
039import java.util.HashMap;
040
041import javax.swing.BorderFactory;
042import javax.swing.JButton;
043import javax.swing.JLabel;
044import javax.swing.JPanel;
045import javax.swing.JSplitPane;
046
047import com.vividsolutions.jump.workbench.WorkbenchContext;
048import com.vividsolutions.jump.workbench.model.LayerManager;
049import com.vividsolutions.jump.workbench.model.LayerTreeModel;
050import com.vividsolutions.jump.workbench.ui.ErrorHandler;
051import com.vividsolutions.jump.workbench.ui.LayerNamePanel;
052import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
053import com.vividsolutions.jump.workbench.ui.LayerViewPanelContext;
054import com.vividsolutions.jump.workbench.ui.LayerViewPanelListener;
055import com.vividsolutions.jump.workbench.ui.LayerViewPanelProxy;
056import com.vividsolutions.jump.workbench.ui.TreeLayerNamePanel;
057import com.vividsolutions.jump.workbench.ui.Viewport;
058import com.vividsolutions.jump.workbench.ui.WorkbenchToolBar;
059import com.vividsolutions.jump.workbench.ui.zoom.PanTool;
060import com.vividsolutions.jump.workbench.ui.zoom.ZoomTool;
061
062/**
063 * Name: JumpMapTab.java Purpose:A customized GUI simailar to JUMP workbench for
064 * displaying vector GIS data. It was painful to find that we can only use the
065 * built in class of ZoomTool and PanTool becasue they do not refer to
066 * JUMPWokrbench class directly. All other frequently used tools refer to
067 * JUMPWokrbench class while this is not. Thus we have to add our own tool icons
068 * and redict the three map operation requests (zoom to previous extent, zoom to
069 * next extent and zoom to full extent) to Viewport through WorkbenchContext.
070 * Known problem(s): We could have used images instead of texts for the three
071 * map operation buttons (zoom to previous extent, zoom to next extent and zoom
072 * to full extent). But I couldn't find a way to refer to the images in JUMP
073 * package. Knwon problem: c.f. ENMPCPFrame.java Author: Jianting Zhang Date:
074 * August, 2005
075 */
076public class JumpMapTab extends JPanel implements LayerViewPanelContext {
077
078        private JLabel statusLabel = new JLabel();
079        private ErrorHandler errorHandler;
080        private JumpToolBar usrToolBar = new JumpToolBar();
081        private WorkbenchToolBar sysToolBar = new WorkbenchToolBar(
082                        new LayerViewPanelProxy() {
083                                public LayerViewPanel getLayerViewPanel() {
084                                        return layerViewPanel;
085                                }
086                        });
087        private LayerManager layerManager = new LayerManager();
088        private TreeLayerNamePanel layerNamePanel;
089        private LayerViewPanel layerViewPanel;
090        private JPanel toolbarPanel = new JPanel();
091
092        private WorkbenchContext context = new WorkbenchContext() {
093                public ErrorHandler getErrorHandler() {
094                        return JumpMapTab.this;
095                }
096
097                public LayerNamePanel getLayerNamePanel() {
098                        return layerNamePanel;
099                }
100
101                public LayerViewPanel getLayerViewPanel() {
102                        return layerViewPanel;
103                }
104
105                public LayerManager getLayerManager() {
106                        return layerManager;
107                }
108
109        };
110
111        /**
112         * Gets the workbenchContext attribute of the JumpMapTab object
113         * 
114         *@return The workbenchContext value
115         */
116        public WorkbenchContext getWorkbenchContext() {
117                return context;
118        }
119
120        /**
121         * Constructor for the JumpMapTab object
122         * 
123         *@param errorHandler
124         *            Description of the Parameter
125         */
126        public JumpMapTab(ErrorHandler errorHandler) {
127                this.errorHandler = errorHandler;
128                layerViewPanel = new LayerViewPanel(layerManager, this);
129                layerNamePanel = new TreeLayerNamePanel(layerViewPanel,
130                                new LayerTreeModel(layerViewPanel), layerViewPanel
131                                                .getRenderingManager(), new HashMap());
132
133                try {
134                        jbInit();
135                } catch (Exception ex) {
136                        ex.printStackTrace();
137                }
138
139                layerViewPanel.addListener(new LayerViewPanelListener() {
140                        public void painted(Graphics graphics) {
141                        }
142
143                        public void selectionChanged() {
144                        }
145
146                        public void cursorPositionChanged(String x, String y) {
147                                setStatusMessage("(" + x + ", " + y + ")");
148                        }
149                });
150        }
151
152        /**
153         * Description of the Method
154         * 
155         *@param t
156         *            Description of the Parameter
157         */
158        public void handleThrowable(final Throwable t) {
159                errorHandler.handleThrowable(t);
160        }
161
162        /**
163         * Description of the Method
164         * 
165         *@param warning
166         *            Description of the Parameter
167         */
168        public void warnUser(String warning) {
169                setStatusMessage(warning);
170        }
171
172        /**
173         * Sets the statusMessage attribute of the JumpMapTab object
174         * 
175         *@param message
176         *            The new statusMessage value
177         */
178        public void setStatusMessage(String message) {
179                // Make message at least a space so that status bar won't collapse [Jon
180                // Aquino]
181                statusLabel
182                                .setText(((message == null) || (message.length() == 0)) ? " "
183                                                : message);
184        }
185
186        /**
187         * Description of the Method
188         * 
189         *@exception Exception
190         *                Description of the Exception
191         */
192        void jbInit() throws Exception {
193
194                this.setLayout(new BorderLayout());
195
196                toolbarPanel.setLayout(new GridBagLayout());
197                toolbarPanel.setBorder(BorderFactory.createLoweredBevelBorder());
198                GridBagConstraints ct = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
199                                GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
200                                                0, 0, 0, 0), 0, 0);
201                toolbarPanel.add(sysToolBar, ct);
202                toolbarPanel.add(usrToolBar);
203                add(toolbarPanel, BorderLayout.NORTH);
204
205                layerViewPanel.setBorder(BorderFactory.createLoweredBevelBorder());
206                layerNamePanel.setBorder(BorderFactory.createLoweredBevelBorder());
207
208                JSplitPane _splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
209                                true);
210                _splitPane.setLeftComponent(layerNamePanel);
211                _splitPane.setRightComponent(layerViewPanel);
212                _splitPane.setOneTouchExpandable(true);
213                add(_splitPane, BorderLayout.CENTER);
214
215                statusLabel.setBorder(BorderFactory.createLoweredBevelBorder());
216                statusLabel.setText(" ");
217                add(statusLabel, BorderLayout.SOUTH);
218        }
219
220        /**
221         * Description of the Method
222         * 
223         *@exception Exception
224         *                Description of the Exception
225         */
226        public void initialize() throws Exception {
227                sysToolBar.addCursorTool("Zoom In/Out", new ZoomTool());
228                sysToolBar.addCursorTool("Pan", new PanTool());
229        }
230
231        /**
232         * Description of the Class
233         * 
234         *@author berkley
235         *@created June 9, 2005
236         */
237        private class JumpToolBar extends JPanel implements ActionListener {
238                JButton zoom, pan, zoomprev, zoomnext, zoomextent;
239
240                /** Constructor for the JumpToolBar object */
241                JumpToolBar() {
242
243                        // zoomprev =new JButton(new ImageIcon("Prev.gif"));
244                        zoomprev = new JButton("Prev");
245                        zoomprev.addActionListener(JumpToolBar.this);
246                        // zoomnext =new JButton(new ImageIcon("Right.gif"));
247                        zoomnext = new JButton("Next");
248                        zoomnext.addActionListener(JumpToolBar.this);
249                        // zoomextent=new JButton(new ImageIcon("World.gif"));
250                        zoomextent = new JButton("Full");
251                        zoomextent.addActionListener(JumpToolBar.this);
252                        add(zoomprev);
253                        add(zoomnext);
254                        add(zoomextent);
255                }
256
257                /**
258                 * Description of the Method
259                 * 
260                 *@param e
261                 *            Description of the Parameter
262                 */
263                public void actionPerformed(ActionEvent e) {
264                        try {
265                                if (e.getSource() == zoomnext) {
266                                        Viewport viewport = context.getLayerViewPanel()
267                                                        .getViewport();
268                                        if (viewport.getZoomHistory().hasNext()) {
269                                                viewport.getZoomHistory().setAdding(false);
270                                                viewport.zoom(viewport.getZoomHistory().next());
271                                                viewport.getZoomHistory().setAdding(true);
272                                        }
273                                } else if (e.getSource() == zoomprev) {
274                                        Viewport viewport = context.getLayerViewPanel()
275                                                        .getViewport();
276                                        if (viewport.getZoomHistory().hasPrev()) {
277                                                viewport.getZoomHistory().setAdding(false);
278                                                viewport.zoom(viewport.getZoomHistory().prev());
279                                                viewport.getZoomHistory().setAdding(true);
280                                        }
281                                } else if (e.getSource() == zoomextent) {
282                                        context.getLayerViewPanel().getViewport()
283                                                        .zoomToFullExtent();
284                                }
285                        } catch (Exception ex) {
286                                ex.printStackTrace();
287                        }
288                }
289        }
290}