Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / panels / LinkPanel.java @ 34002

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 org.gvsig.app.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.RenderingHints;
50
import java.awt.event.ComponentEvent;
51
import java.awt.event.ComponentListener;
52
import java.awt.geom.AffineTransform;
53
import java.awt.geom.Rectangle2D;
54
import java.awt.image.BufferedImage;
55
import java.io.File;
56
import java.io.IOException;
57
import java.net.MalformedURLException;
58
import java.net.URI;
59
import java.net.URL;
60

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

    
70
import org.apache.batik.bridge.BridgeContext;
71
import org.apache.batik.bridge.DocumentLoader;
72
import org.apache.batik.bridge.GVTBuilder;
73
import org.apache.batik.bridge.UserAgentAdapter;
74
import org.apache.batik.bridge.ViewBox;
75
import org.apache.batik.gvt.GraphicsNode;
76
import org.apache.batik.gvt.renderer.StaticRenderer;
77
import org.gvsig.andami.PluginServices;
78
import org.gvsig.andami.messages.NotificationManager;
79
import org.gvsig.andami.ui.mdiManager.IWindow;
80
import org.gvsig.andami.ui.mdiManager.WindowInfo;
81
import org.gvsig.app.project.documents.gui.PDFViewerWindow;
82
import org.gvsig.app.project.documents.view.toolListeners.LinkListener;
83
import org.gvsig.utils.BrowserControl;
84

    
85
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
87

    
88
import org.w3c.dom.Document;
89
import org.w3c.dom.Element;
90
import org.w3c.dom.svg.SVGDocument;
91

    
92
import com.sun.jimi.core.Jimi;
93

    
94

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

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

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

    
162

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

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

    
189
                if (jPane == null) {
190
                        jPane=new JPanel();
191
                        System.out.println(type);
192

    
193
                        //Enlazar a documento de texto o WWW
194
                        if (type==1){
195
                                textPane = new JTextPane();
196

    
197
                                scroll = new JScrollPane(textPane);
198
                                scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
199
                        scroll.setPreferredSize(new Dimension(ancho-100,alto-100));
200
                        scroll.setMinimumSize(new Dimension(ancho-100,alto-100));
201
                        textPane.setEditable(false);
202

    
203
                                if(uri==null) {
204
                                        System.out.println("NULO");
205
                                }
206
                                if (uri != null) {
207
                                        URL url=null;
208
                                        try {
209
                                                url=uri.normalize().toURL();
210
                                        } catch (MalformedURLException e1) {
211
                                                NotificationManager.addError(e1);
212
                                        }
213
                                        try {
214

    
215
                                                textPane.setPage(url);
216
                                                textPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
217
                                                        public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent e) {
218
                                                                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
219
                                                                        System.out.println("hyperlinkUpdate()");
220
                                                                        BrowserControl.displayURL(e.getURL().toString());
221

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

    
237
                                ImageIcon image = null;
238
                                String iString=uri.toString();
239
                                iString=iString.toLowerCase();
240
                                if (uri == null){
241
                                        System.out.println("Uri creada incorrectamente");
242
                                        return null;}
243

    
244
                                if (iString.endsWith("jpg") || iString.endsWith("jpeg") ||
245
                                iString.endsWith("gif")) {
246
                            try {
247
                                                image = new ImageIcon(Jimi.getImage(uri.toURL()));
248
                                        } catch (MalformedURLException e) {
249
                                                e.printStackTrace();
250
                                        }
251
                        } else
252
                                if (iString.endsWith("png") || iString.endsWith("tiff") ||
253
                                iString.endsWith("ico") || iString.endsWith("xpm")) {
254
                            try {
255
                                                image = new ImageIcon(Jimi.getImage(uri.toURL()));
256
                                        } catch (MalformedURLException e) {
257
                                                e.printStackTrace();
258
                                        }
259

    
260
                        }else {
261
                                try {
262
                                                image=new ImageIcon(uri.toURL());
263
                                        } catch (MalformedURLException e) {
264
                                                e.printStackTrace();
265
                                        }
266
                        }
267
                                if (image==null) {
268
                                        return null;
269
                                }
270

    
271
                                JLabel label = new JLabel(image);
272
                                jPane.add(label);
273

    
274
                                jPane.setVisible(true);
275
                        }
276

    
277
                        //Tipo 2: Enlazar a documentos PDF
278
                        else if (type==2){
279

    
280
                                String aux=null;
281
                                try {
282
                                        aux = uri.normalize().toURL().toString();
283
                                } catch (MalformedURLException e) {
284
                                        e.printStackTrace();
285
                                }
286
                                aux=aux.replaceFirst("file:/","");
287
                                System.out.println(aux);
288
                                PDFViewerWindow pdf =new PDFViewerWindow(aux);
289
                                jPane.add(pdf);
290
                                jPane.setVisible(true);
291
                        }
292

    
293
                        //Tipo 3: Enlazar a im?genes SDV
294
                        else if (type==3){
295
                                String iString=uri.toString();
296
                                iString=iString.toLowerCase();
297
                                if (uri == null){
298
                                        System.out.println("Uri creada incorrectamente");
299
                                        return null;}
300
                                File file=new File(uri);
301
                                iString=uri.toString().replaceFirst("file:/","");
302
                                if (iString.endsWith("svg")){
303
                                        paintSVGToPanel(file);
304
                                }
305
                        }
306
                }
307
                return jPane;
308
         }
309

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

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

    
347
         /**
348
         * Draw SVG in the Graphics that receives like parameter.
349
     *
350
     * @param g Graphics
351
     * @param rect Rectangle that fills the Graphic.
352
     * @param rv Rectangle. This forms the visible part in the Layout
353
     */
354
    private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv) {
355
        if ((rv == null) || rv.contains(rect)) {
356
            AffineTransform ataux = new AffineTransform();
357

    
358
            ataux.translate(rect.getX(), rect.getY());
359
            try {
360
                ataux.concatenate(ViewBox.getViewTransform(null, elt,
361
                        (float) rect.getWidth(), (float) rect.getHeight(), ctx));
362
                gvtRoot.setTransform(ataux);
363

    
364
            } catch (Exception e) {
365
                // TODO: handle exception
366
            }
367
        } else {
368
            AffineTransform ataux = new AffineTransform();
369

    
370
            ataux.translate(rect.getX(), rect.getY());
371
            ataux.concatenate(ViewBox.getViewTransform(null, elt,
372
                    (float) rect.getWidth(), (float) rect.getHeight(), ctx));
373

    
374
            gvtRoot.setTransform(ataux);
375
        }
376

    
377
        RenderingHints renderingHints = defaultRenderingHints;
378
        g.setRenderingHints(renderingHints);
379

    
380
        if (gvtRoot != null) {
381
            gvtRoot.paint(g);
382
        }
383
    }
384

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

    
404
                return jPanel;
405
        }
406

    
407
        /* (non-Javadoc)
408
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
409
         */
410
        public WindowInfo getWindowInfo() {
411
                m_ViewInfo = new WindowInfo(WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE | WindowInfo.ICONIFIABLE);
412
                m_ViewInfo.setTitle(PluginServices.getText(this,"Hiperenlace"));
413

    
414
                return m_ViewInfo;
415
        }
416

    
417
        /* (non-Javadoc)
418
         * @see com.iver.mdiApp.ui.MDIManager.View#viewActivated()
419
         */
420
        public void viewActivated() {
421
                // TODO Auto-generated method stub
422
        }
423

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

    
446
        /**
447
         * This method initializes jPanel1Der
448
         *
449
         * @return javax.swing.JPanel
450
         */
451
        private JPanel getJPanel1Der() {
452
                if (jPanel1Der == null) {
453
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
454
                        gridBagConstraints1.gridx = 0;
455
                        gridBagConstraints1.gridy = 0;
456
                        jPanel1Der = new JPanel();
457
                        jPanel1Der.setLayout(new GridBagLayout());
458
                        jPanel1Der.add(getJButtonCerrar(), gridBagConstraints1);
459
                }
460
                return jPanel1Der;
461
        }
462

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

    
477
                                        if (PluginServices.getMainFrame() != null) {
478
                                                PluginServices.getMDIManager().closeWindow(LinkPanel.this);
479
                                        } else {
480
                                                ((JDialog) (getParent().getParent().getParent()
481
                                                                                .getParent())).dispose();
482
                                        }
483
                                }
484
                        });
485

    
486
                }
487
                return jButtonCerrar;
488
        }
489
        /**
490
         * Resizes the scroll that contains the text when the window changes its own size
491
         */
492
        private void redimensionarScroll(){
493
                Dimension currentSize = getSize();
494
                scroll.setPreferredSize(new Dimension(currentSize.width-100,currentSize.height-100));
495
        scroll.setMinimumSize(new Dimension(currentSize.width-100,currentSize.height-100));
496
                scroll.updateUI();
497
                System.out.println(scroll.getSize().toString());
498
        }
499

    
500
        /**
501
         * Invoked when the frame?s size changes
502
         */
503
        public void componentResized(ComponentEvent e) {
504
                if(type==1) {
505
                        redimensionarScroll();
506
                }
507
        }
508

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

    
516
        }
517

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

    
525
        }
526

    
527
        /*
528
         *  (non-Javadoc)
529
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
530
         */
531
        public void componentHidden(ComponentEvent e) {
532
                // TODO Auto-generated method stub
533

    
534
        }
535

    
536
        public Object getWindowProfile() {
537
                return WindowInfo.TOOL_PROFILE;
538
        }
539
}