Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / LinkPanel.java @ 24986

History | View | Annotate | Download (15.7 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.gui.panels;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Dimension;
45
import java.awt.Graphics2D;
46
import java.awt.GridBagConstraints;
47
import java.awt.GridBagLayout;
48
import java.awt.GridLayout;
49
import java.awt.Image;
50
import java.awt.RenderingHints;
51
import java.awt.event.ComponentEvent;
52
import java.awt.event.ComponentListener;
53
import java.awt.geom.AffineTransform;
54
import java.awt.geom.Rectangle2D;
55
import java.awt.image.BufferedImage;
56
import java.io.File;
57
import java.io.IOException;
58
import java.net.MalformedURLException;
59
import java.net.URI;
60
import java.net.URL;
61

    
62
import javax.swing.ImageIcon;
63
import javax.swing.JButton;
64
import javax.swing.JDialog;
65
import javax.swing.JLabel;
66
import javax.swing.JPanel;
67
import javax.swing.JScrollPane;
68
import javax.swing.JTextPane;
69
import javax.swing.event.HyperlinkEvent;
70

    
71
import org.apache.batik.bridge.BridgeContext;
72
import org.apache.batik.bridge.DocumentLoader;
73
import org.apache.batik.bridge.GVTBuilder;
74
import org.apache.batik.bridge.UserAgentAdapter;
75
import org.apache.batik.bridge.ViewBox;
76
import org.apache.batik.gvt.GraphicsNode;
77
import org.apache.batik.gvt.renderer.StaticRenderer;
78
import org.apache.log4j.Logger;
79
import org.w3c.dom.Document;
80
import org.w3c.dom.Element;
81
import org.w3c.dom.svg.SVGDocument;
82

    
83
import com.iver.andami.PluginServices;
84
import com.iver.andami.messages.NotificationManager;
85
import com.iver.andami.ui.mdiManager.IWindow;
86
import com.iver.andami.ui.mdiManager.WindowInfo;
87
import com.iver.cit.gvsig.project.documents.gui.PDFViewerWindow;
88
import com.iver.cit.gvsig.project.documents.view.toolListeners.LinkListener;
89
import com.iver.utiles.BrowserControl;
90
import com.sun.jimi.core.Jimi;
91

    
92

    
93
/**
94
 * This class extends JPanel. This class implements a Panel to show the content of the URI
95
 * that the constructor of the class receives. The URI is interpreted according to the type
96
 * of the HyperLink. The allowed types are Text, WWW, image (jpg,gif,tiff,bmp,ico), PDF
97
 * documents and SVG images. Implements listener to resize the component that contains the
98
 * text when resize the window.
99
 *
100
 * @author Vicente Caballero Navarro
101
 *
102
 */
103
public class LinkPanel extends JPanel implements IWindow, ComponentListener {
104
        private static Logger logger = Logger.getLogger(LinkPanel.class.getName());
105
        private JPanel jPane = null;
106
        private JScrollPane jScrollPane = null;
107
//        private JEditorPane jEditorPane1 = null;
108
        private JPanel jPanel = null; //  @jve:decl-index=0:visual-constraint="220,10"
109
        private URI uri=null;
110
        private int type=LinkListener.TYPELINKIMAGE;
111
        private JPanel jPanel1Izq = null;
112
        private JPanel jPanel1Der = null;
113
        private JLabel jLabelUri = null;
114
        private JButton jButtonCerrar = null;
115
        private JScrollPane scroll=null;
116
        private int alto;
117
        private int ancho;
118
        private WindowInfo m_ViewInfo;
119
        private JTextPane textPane;
120
        private Element elt;
121
    private GVTBuilder gvtBuilder = new GVTBuilder();
122
    private GraphicsNode gvtRoot = null;
123
    private BridgeContext ctx = null;
124
    private StaticRenderer renderer = new StaticRenderer();
125
    protected static RenderingHints defaultRenderingHints;
126
    static {
127
        defaultRenderingHints = new RenderingHints(null);
128
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
129
            RenderingHints.VALUE_ANTIALIAS_ON);
130

    
131
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
132
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
133
    }
134
        /**
135
         * This is the default constructor. Needs the URI with the information of the HyperLink
136
         * and the type of the HyperLink
137
         */
138
        public LinkPanel(URI Uri, int t){
139
                super();
140
                type=t;
141
                uri=Uri;
142
                addComponentListener(this);
143
                initialize();
144
        }
145

    
146
        /**
147
         * This method initializes this
148
         */
149
        private void initialize() {
150
                this.setLayout(new BorderLayout());
151
                this.setSize(600, 450);
152
                alto=this.getHeight()-100;
153
                ancho=this.getWidth()-100;
154
                this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
155
                this.add(getJPanel(), java.awt.BorderLayout.SOUTH);
156
        }
157

    
158

    
159
        /**
160
         * This method initializes jScrollPane
161
         *
162
         * @return javax.swing.JScrollPane
163
         */
164
        private JScrollPane getJScrollPane() {
165
                if (jScrollPane == null) {
166
                        jScrollPane = new JScrollPane();
167
                        jScrollPane.setPreferredSize(new java.awt.Dimension(300, 400));
168
                        jScrollPane.setViewportView(getJPane());
169
                }
170
                return jScrollPane;
171
        }
172

    
173
        /**
174
         * This method initializes jEditorPane1. Check the type of the HyperLink and analize the
175
         * URI. According to the type, adds to the panel the necessary components to show the
176
         * content of the URI. If the type is "0" (text/WWW) adds a ScrollPane with the text.
177
         * If the type "1" (smages) adds a ImageIcon with a label. If the type is "2" uses the
178
         * library "JPedal" to open PDF documents and if the type is "3" implements some methods
179
         * to open SVG images
180
         *
181
         * @return javax.swing.JEditorPane
182
         */
183
        private JPanel getJPane() {
184

    
185
                if (jPane == null) {
186
                        jPane=new JPanel();
187
                        System.out.println(type);
188

    
189
                        //Enlazar a documento de texto o WWW
190
                        if (type==1){
191
                                textPane = new JTextPane();
192

    
193
                                scroll = new JScrollPane(textPane);
194
                                scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
195
                        scroll.setPreferredSize(new Dimension(ancho-100,alto-100));
196
                        scroll.setMinimumSize(new Dimension(ancho-100,alto-100));
197
                        textPane.setEditable(false);
198

    
199
                                if(uri==null)System.out.println("NULO");
200
                                if (uri != null) {
201
                                        URL url=null;
202
                                        try {
203
                                                url=uri.normalize().toURL();
204
                                        } catch (MalformedURLException e1) {
205
                                                NotificationManager.addError(e1);
206
                                        }
207
                                        try {
208

    
209
                                                textPane.setPage(url);
210
                                                textPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
211
                                                        public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent e) {
212
                                                                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
213
                                                                        System.out.println("hyperlinkUpdate()");
214
                                                                        BrowserControl.displayURL(e.getURL().toString());
215

    
216
                                                                        }
217
                                                                }
218
                                                        });
219
                                        } catch (IOException e) {
220
                                                System.err.println("Attempted to read a bad URL: " +
221
                                                uri);
222
                                        }
223
                                        jPane.add(scroll);
224
                                } else {
225
                                        System.err.println("Couldn't find file.");
226
                                }
227
                        }
228
                        //Enlazar a archivo de Im?gen
229
                        else if (type==0){
230

    
231
                                ImageIcon image = null;
232
                                String iString=uri.toString();
233
                                iString=iString.toLowerCase();
234
                                if (uri == null){
235
                                        System.out.println("Uri creada incorrectamente");
236
                                        return null;}
237

    
238
                                if (iString.endsWith("jpg") || iString.endsWith("jpeg") ||
239
                                iString.endsWith("gif")) {
240
                            try {
241
                                                image = new ImageIcon(Jimi.getImage(uri.toURL()));
242
                                        } catch (MalformedURLException e) {
243
                                                e.printStackTrace();
244
                                        }
245
                        } else
246
                                if (iString.endsWith("png") || iString.endsWith("tiff") ||
247
                                iString.endsWith("ico") || iString.endsWith("xpm") ||
248
                                iString.endsWith("bmp")) {
249
                            try {
250
                                                image = new ImageIcon(Jimi.getImage(uri.toURL()));
251
                                        } catch (MalformedURLException e) {
252
                                                e.printStackTrace();
253
                                        }
254

    
255
                        }else {
256
                                try {
257
                                                image=new ImageIcon(uri.toURL());
258
                                        } catch (MalformedURLException e) {
259
                                                e.printStackTrace();
260
                                        }
261
                        }
262
                                if (image==null)return null;
263

    
264
                                JLabel label = new JLabel(image);
265
                                jPane.add(label);
266

    
267
                                jPane.setVisible(true);
268
                        }
269

    
270
                        //Tipo 2: Enlazar a documentos PDF
271
                        else if (type==2){
272

    
273
                                String aux=null;
274
                                try {
275
                                        aux = uri.normalize().toURL().toString();
276
                                } catch (MalformedURLException e) {
277
                                        e.printStackTrace();
278
                                }
279
                                aux=aux.replaceFirst("file:/","");
280
                                System.out.println(aux);
281
                                PDFViewerWindow pdf =new PDFViewerWindow(aux);
282
                                jPane.add(pdf);
283
                                jPane.setVisible(true);
284
                        }
285

    
286
                        //Tipo 3: Enlazar a im?genes SDV
287
                        else if (type==3){
288
                                String iString=uri.toString();
289
                                iString=iString.toLowerCase();
290
                                if (uri == null){
291
                                        System.out.println("Uri creada incorrectamente");
292
                                        return null;}
293
                                File file=new File(uri);
294
                                iString=uri.toString().replaceFirst("file:/","");
295
                                if (iString.endsWith("svg")){
296
                                        paintSVGToPanel(file);
297
                                }
298
                        }
299
                }
300
                return jPane;
301
         }
302

    
303
        /**
304
         * Allows paint SVG images in the panel.
305
         *
306
         * @param file, this file has been extracted from the URI
307
         */
308
        private void paintSVGToPanel(File file) {
309
                BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(),
310
                                BufferedImage.TYPE_INT_ARGB);
311
            Graphics2D g = image.createGraphics();
312
                Rectangle2D rect = new Rectangle2D.Double();
313
                rect.setFrame(0,0,this.getWidth(),this.getHeight());
314
                obtainStaticRenderer(file);
315
                drawSVG(g,rect, null);
316
                jPane.setVisible(true);
317
                ImageIcon icon=new ImageIcon((Image)image);
318
                JLabel label = new JLabel(icon);
319
                jPane.add(label);
320
        }
321

    
322
        /**
323
         * Render the image to add to the panel.
324
         * @param file, this file has been extracted from the URI
325
         */
326
        private void obtainStaticRenderer(File file) {
327
        try {
328
            UserAgentAdapter userAgent = new UserAgentAdapter();
329
            DocumentLoader loader = new DocumentLoader(userAgent);
330
            ctx = new BridgeContext(userAgent, loader);
331
            Document svgDoc = loader.loadDocument(file.toURI().toString());
332
            gvtRoot = gvtBuilder.build(ctx, svgDoc);
333
            renderer.setTree(gvtRoot);
334
            elt = ((SVGDocument) svgDoc).getRootElement();
335
        } catch (Exception ex) {
336
            ex.printStackTrace();
337
        }
338
    }
339

    
340
         /**
341
         * Draw SVG in the Graphics that receives like parameter.
342
     *
343
     * @param g Graphics
344
     * @param rect Rectangle that fills the Graphic.
345
     * @param rv Rectangle. This forms the visible part in the Layout
346
     */
347
    private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv) {
348
        if ((rv == null) || rv.contains(rect)) {
349
            AffineTransform ataux = new AffineTransform();
350

    
351
            ataux.translate(rect.getX(), rect.getY());
352
            try {
353
                ataux.concatenate(ViewBox.getViewTransform(null, elt,
354
                        (float) rect.getWidth(), (float) rect.getHeight(), ctx));
355
                gvtRoot.setTransform(ataux);
356

    
357
            } catch (Exception e) {
358
                // TODO: handle exception
359
            }
360
        } else {
361
            AffineTransform ataux = new AffineTransform();
362

    
363
            ataux.translate(rect.getX(), rect.getY());
364
            ataux.concatenate(ViewBox.getViewTransform(null, elt,
365
                    (float) rect.getWidth(), (float) rect.getHeight(), ctx));
366

    
367
            gvtRoot.setTransform(ataux);
368
        }
369

    
370
        RenderingHints renderingHints = defaultRenderingHints;
371
        g.setRenderingHints(renderingHints);
372

    
373
        if (gvtRoot != null) {
374
            gvtRoot.paint(g);
375
        }
376
    }
377

    
378
        /**
379
         * This method initializes jPanel
380
         *
381
         * @return javax.swing.JPanel
382
         */
383
        private JPanel getJPanel() {
384
                if (jPanel == null) {
385
                        //JLabel name = new JLabel();
386
                        GridLayout gridLayout = new GridLayout();
387
                        gridLayout.setRows(1);
388
                        jPanel = new JPanel();
389
                        jPanel.setLayout(gridLayout);
390
                        jPanel.setPreferredSize(new java.awt.Dimension(10, 50));
391
                        jPanel.setSize(411, 50);
392
                        jPanel.add(getJPanel1Izq(), null);
393
                        jPanel.add(getJPanel1Der(), null);
394
                        String auxext="";
395
                }
396

    
397
                return jPanel;
398
        }
399

    
400
        /* (non-Javadoc)
401
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
402
         */
403
        public WindowInfo getWindowInfo() {
404
                m_ViewInfo = new WindowInfo(WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE | WindowInfo.ICONIFIABLE);
405
                m_ViewInfo.setTitle(PluginServices.getText(this,"Hiperenlace"));
406

    
407
                return m_ViewInfo;
408
        }
409

    
410
        /* (non-Javadoc)
411
         * @see com.iver.mdiApp.ui.MDIManager.View#viewActivated()
412
         */
413
        public void viewActivated() {
414
                // TODO Auto-generated method stub
415
        }
416

    
417
        /**
418
         * This method initializes jPanel1Izq
419
         *
420
         * @return javax.swing.JPanel
421
         */
422
        private JPanel getJPanel1Izq() {
423
                if (jPanel1Izq == null) {
424
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
425
                        gridBagConstraints.gridx = 0;
426
                        gridBagConstraints.gridy = 0;
427
                        jLabelUri = new JLabel();
428
                        jLabelUri.setText(uri.toString());
429
                        jLabelUri.setPreferredSize(new java.awt.Dimension(172,17));
430
                        jPanel1Izq = new JPanel();
431
                        jPanel1Izq.setLayout(new GridBagLayout());
432
                        jPanel1Izq.setPreferredSize(new java.awt.Dimension(10,10));
433
                        jPanel1Izq.setAlignmentX(0.5F);
434
                        jPanel1Izq.add(jLabelUri, gridBagConstraints);
435
                }
436
                return jPanel1Izq;
437
        }
438

    
439
        /**
440
         * This method initializes jPanel1Der
441
         *
442
         * @return javax.swing.JPanel
443
         */
444
        private JPanel getJPanel1Der() {
445
                if (jPanel1Der == null) {
446
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
447
                        gridBagConstraints1.gridx = 0;
448
                        gridBagConstraints1.gridy = 0;
449
                        jPanel1Der = new JPanel();
450
                        jPanel1Der.setLayout(new GridBagLayout());
451
                        jPanel1Der.add(getJButtonCerrar(), gridBagConstraints1);
452
                }
453
                return jPanel1Der;
454
        }
455

    
456
        /**
457
         * Initializes jButtonCerrar, and implements the Listener to close the window
458
         *
459
         * @return javax.swing.JButton
460
         */
461
        private JButton getJButtonCerrar() {
462
                if (jButtonCerrar == null) {
463
                        jButtonCerrar = new JButton();
464
                        jButtonCerrar.setPreferredSize(new java.awt.Dimension(94,25));
465
                        jButtonCerrar.setText(PluginServices.getText(this,"Cerrar"));
466
                        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
467
                                public void actionPerformed(java.awt.event.ActionEvent e) {
468
                                        System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
469

    
470
                                        if (PluginServices.getMainFrame() != null) {
471
                                                PluginServices.getMDIManager().closeWindow(LinkPanel.this);
472
                                        } else {
473
                                                ((JDialog) (getParent().getParent().getParent()
474
                                                                                .getParent())).dispose();
475
                                        }
476
                                }
477
                        });
478

    
479
                }
480
                return jButtonCerrar;
481
        }
482
        /**
483
         * Resizes the scroll that contains the text when the window changes its own size
484
         */
485
        private void redimensionarScroll(){
486
                Dimension currentSize = getSize();
487
                scroll.setPreferredSize(new Dimension(currentSize.width-100,currentSize.height-100));
488
        scroll.setMinimumSize(new Dimension(currentSize.width-100,currentSize.height-100));
489
                scroll.updateUI();
490
                System.out.println(scroll.getSize().toString());
491
        }
492

    
493
        /**
494
         * Invoked when the frame?s size changes
495
         */
496
        public void componentResized(ComponentEvent e) {
497
                if(type==1)redimensionarScroll();
498
        }
499

    
500
        /*
501
         *  (non-Javadoc)
502
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
503
         */
504
        public void componentMoved(ComponentEvent e) {
505
                // TODO Auto-generated method stub
506

    
507
        }
508

    
509
        /*
510
         *  (non-Javadoc)
511
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
512
         */
513
        public void componentShown(ComponentEvent e) {
514
                // TODO Auto-generated method stub
515

    
516
        }
517

    
518
        /*
519
         *  (non-Javadoc)
520
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
521
         */
522
        public void componentHidden(ComponentEvent e) {
523
                // TODO Auto-generated method stub
524

    
525
        }
526

    
527
        public Object getWindowProfile() {
528
                return WindowInfo.TOOL_PROFILE;
529
        }
530

    
531
}