Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / LinkListener.java @ 20100

History | View | Annotate | Download (7.67 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project.documents.view.toolListeners;
42

    
43
import java.awt.Component;
44
import java.awt.Cursor;
45
import java.awt.Image;
46
import java.awt.Point;
47
import java.awt.Toolkit;
48
import java.awt.geom.Point2D;
49
import java.util.BitSet;
50

    
51
import javax.swing.ImageIcon;
52
import javax.swing.JOptionPane;
53

    
54
import org.apache.log4j.Logger;
55

    
56
import com.hardcode.gdbms.engine.data.DataSource;
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.messages.NotificationManager;
59
import com.iver.cit.gvsig.fmap.DriverException;
60
import com.iver.cit.gvsig.fmap.MapControl;
61
import com.iver.cit.gvsig.fmap.layers.FBitSet;
62
import com.iver.cit.gvsig.fmap.layers.FLayer;
63
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
65
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
66
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
67
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
68
import com.iver.cit.gvsig.gui.panels.LinkPanel;
69
import com.iver.cit.gvsig.project.documents.view.IProjectView;
70
import com.iver.cit.gvsig.project.documents.view.gui.View;
71

    
72

    
73
/**
74
 * <p>Listener that gets, if exists, the associated link at the feature that's at the position selection of an active, selected
75
 *  and vector layer of the associated <code>MapControl</code>; and displays
76
 *  that linked data (image, text, ...) on a dialog.</p>
77
 *
78
 * <p>Listens a single click of any mouse's button.</p>
79
 *
80
 * @author Vicente Caballero Navarro
81
 */
82
public class LinkListener implements PointListener {
83
        /**
84
         * Object used to log messages for this listener.
85
         */
86
        private static Logger logger = Logger.getLogger(InfoListener.class.getName());
87

    
88
        /**
89
         * The image to display when the cursor is active.
90
         */
91
        private final Image img = new ImageIcon(MapControl.class.getResource(
92
                                "images/LinkCursor.gif")).getImage();
93

    
94
        /**
95
         * The cursor used to work with this tool listener.
96
         * 
97
         * @see #getCursor()
98
         */
99
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(img,
100
                        new Point(16, 16), "");
101

    
102
        /**
103
         * Reference to the <code>MapControl</code> object that uses.
104
         */
105
        private MapControl mapCtrl;
106

    
107
        /**
108
         * Auxiliary parameter used to retrieve links associated to the features of the vector, active
109
         *  and selected layers.
110
         */
111
        private BitSet[] bitset;
112

    
113
        /**
114
         * Active and selected layers in the associated <code>MapControl</code>.
115
         */
116
        private FLayer[] layers;
117

    
118
        /**
119
         * A selected field in the model of the <code>MapControl</code>.
120
         */
121
        private String field;
122

    
123
        /**
124
         * Type of extension of the link.
125
         */
126
        private String ext="";
127

    
128
        /**
129
         * Type of link.
130
         */
131
        private int type;
132

    
133
        /**
134
         * <p>Creates a new <code>LinkListener</code> object.</p>
135
         * 
136
         * @param mc the <code>MapControl</code> where will be applied the changes
137
         */
138
        public LinkListener(MapControl mc) {
139
                this.mapCtrl = mc;
140
        }
141

    
142
        /*
143
         * (non-Javadoc)
144
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
145
         */
146
        public void point(PointEvent event) throws BehaviorException {
147
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
148
                View view=(View)PluginServices.getMDIManager().getActiveWindow();
149
                FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
150

    
151
                bitset = new BitSet[sel.length];
152
                layers = new FLayer[sel.length];
153

    
154
                for (int i = 0; i < sel.length; i++) {
155
                        FLayer laCapa = (FLayer) sel[i];
156
            if (laCapa instanceof FLyrVect)
157
            {
158
                    // Tolerancia de 3 pixels
159
                FLyrVect lyrVect = (FLyrVect) laCapa;
160
                double tol = mapCtrl.getViewPort().toMapDistance(3);
161

    
162
                FBitSet newBitSet;
163
                try {
164
                    newBitSet = lyrVect.queryByPoint(pReal, tol);
165
                    bitset[i] = newBitSet;
166
                    layers[i] = laCapa;
167
                } catch (DriverException e) {
168
                    // TODO Auto-generated catch block
169
                    e.printStackTrace();
170
                    throw new BehaviorException("Fallo al consultar " + lyrVect.getName());
171
                }
172
            }
173
                        /* if ((laCapa instanceof VectorialData) &&
174
                                        (laCapa instanceof AlphanumericData)) {
175
                                QueryByPointVisitor visitor = new QueryByPointVisitor();
176
                                visitor.setLayer(laCapa);
177
                                visitor.setQueriedPoint(pReal);
178
                                visitor.setTolerance(mapCtrl.getMapContext().getViewPort()
179
                                                                                        .toMapDistance(3));
180
                                try {
181
                                        ((VectorialData) laCapa).process(visitor);
182
                                } catch (DriverException e) {
183
                                        throw new BehaviorException("No se pudo procesar la capa",e);
184
                                } catch (VisitException e) {
185
                                        throw new RuntimeException(e);
186
                                }
187
                                bitset[i] = visitor.getBitSet();
188
                                layers[i] = laCapa;
189
                        } */
190
                }
191

    
192
                if (bitset != null) {
193
                        for (int i = 0; i < bitset.length; i++) {
194
                                try {
195
                                        IProjectView model=view.getModel();
196
                                        type=model.getTypeLink();
197
                                        field=model.getSelectedField();
198
                                        //field = "NOM_PROVIN";
199
                                        //ext=".png";
200
                                        ext=model.getExtLink();
201
                                        if (layers[i] instanceof AlphanumericData) {
202

    
203
                                                DataSource ds = ((AlphanumericData) layers[i]).getRecordset();
204

    
205
                                                ds.start();
206

    
207
                                                //boolean exist=false;
208
                                                int idField;
209
                                                idField = ds.getFieldIndexByName(field);
210
                                                if (idField != -1)
211
                                                {
212
                                                        for (int j = bitset[i].nextSetBit(0); j >= 0;
213
                                                                        j = bitset[i].nextSetBit(j + 1))
214
                                                        {
215
                                                                LinkPanel linkpanel = new LinkPanel(field,
216
                                                                                ds.getFieldValue(j, idField).toString(),ext,type);
217
                                                                PluginServices.getMDIManager().addWindow(linkpanel);
218

    
219
                                                        }
220
                                                }
221

    
222
                                                logger.debug("ds.stop()");
223
                                                ds.stop();
224
                                        }else {
225
                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"la_capa_no_tiene_hiperenlace");
226
                                        }
227

    
228
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
229
                                        NotificationManager.addError("Error formateando los resultados", e);
230
                                } catch (DriverException e) {
231
                                        NotificationManager.addError("Error formateando los resultados", e);
232
                                }
233
                        }
234

    
235
                }
236
        }
237

    
238
        /*
239
         * (non-Javadoc)
240
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
241
         */
242
        public Cursor getCursor() {
243
                return cur;
244
        }
245

    
246
        /*
247
         * (non-Javadoc)
248
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
249
         */
250
        public boolean cancelDrawing() {
251
                return false;
252
        }
253

    
254
        /*
255
         * (non-Javadoc)
256
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#pointDoubleClick(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
257
         */
258
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
259
                // TODO Auto-generated method stub
260

    
261
        }
262
}