Statistics
| Revision:

gvsig-catalog / org.gvsig.catalog / branches / org.gvsig.catalog-CSW2.0.2 / org.gvsig.catalog / org.gvsig.catalog.lib / src / main / java / org / gvsig / catalog / ui / showresults / ShowResultsPanel.java @ 55

History | View | Annotate | Download (15.1 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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 2
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42
package org.gvsig.catalog.ui.showresults;
43
import java.awt.AlphaComposite;
44
import java.awt.BorderLayout;
45
import java.awt.Color;
46
import java.awt.Dimension;
47
import java.awt.FlowLayout;
48
import java.awt.Font;
49
import java.awt.GradientPaint;
50
import java.awt.Graphics2D;
51
import java.awt.RenderingHints;
52
import java.awt.Shape;
53
import java.awt.font.TextLayout;
54
import java.awt.geom.AffineTransform;
55
import java.awt.geom.Rectangle2D;
56
import java.awt.image.BufferedImage;
57
import java.net.URL;
58

    
59
import javax.swing.BorderFactory;
60
import javax.swing.BoxLayout;
61
import javax.swing.ImageIcon;
62
import javax.swing.JButton;
63
import javax.swing.JEditorPane;
64
import javax.swing.JLabel;
65
import javax.swing.JPanel;
66
import javax.swing.JScrollPane;
67
import javax.swing.border.BevelBorder;
68

    
69
import org.gvsig.catalog.CatalogClient;
70
import org.gvsig.catalog.metadataxml.XMLNode;
71
import org.gvsig.catalog.schemas.Record;
72
import org.gvsig.catalog.utils.ImageRetriever;
73
import org.gvsig.i18n.Messages;
74

    
75
/**
76
 * 
77
 * 
78
 * 
79
 * @author Jorge Piera Llodra (piera_jor@gva.es)
80
 */
81
public class ShowResultsPanel extends JPanel {
82
        public static boolean mustShowThumbnails = true;
83
        private Record record = null;
84
        private CatalogClient client = null;
85
        private URL imageURL = null;
86
        private JPanel descriptionPanel = null;
87
        private JEditorPane descriptionArea = null;
88
        private JPanel linksPanel = null;
89
        private JPanel jPanel = null;
90
        private JButton descriptionButton = null;
91
        private JLabel jLabel = null;
92
        private JButton mapButton = null;
93
        private JScrollPane descriptionScroll = null;
94
        private JPanel imagePanel = null;
95
        private JLabel jLabel1 = null;
96
        private JLabel imageLabel = null;
97
        private JPanel jPanel1 = null;
98
        private JPanel nextLastPanel = null;
99
        private JButton lastButton = null;
100
        private JLabel textLabel = null;
101
        private JButton nextButton = null;
102
        private int numRecords; 
103
        private JButton closeButton = null;
104

    
105
        /**
106
         * Constaructor
107
         * 
108
         * @param server Server URL
109
         * 
110
         * @param node Answer Node
111
         * @param protocol Search protocol
112
         * @param numRecords Number of records returned by the query
113
         * @param serverURL 
114
         * @param translator 
115
         */
116
        public  ShowResultsPanel(CatalogClient client, int numRecords) {        
117
                super();
118
                this.client = client;                
119
                this.numRecords = numRecords;        
120
                initialize();                
121
        } 
122

    
123
        /**
124
         * This method initializes this
125
         * 
126
         */
127
        private void initialize() {        
128
                this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
129
                this.setSize(703, 364);
130
                this.add(getNextLastPanel(), null);
131
                this.add(getJPanel1(), null);
132
                this.add(getDescriptionPanel(), null);
133
                this.add(getJPanel(), null);
134
                this.add(getLinksPanel(), null);
135
                actualizaLabel(1);
136
        } 
137

    
138
        /**
139
         * Actualiza el valor de la cadena de taexto que muestra los resultados
140
         * 
141
         * 
142
         * @param number Registro actual
143
         */
144
        public void actualizaLabel(int number) {        
145
                textLabel.setText(Messages.getText("results") + ": " + String.valueOf(number) + " " + Messages.getText("of") + " " +
146
                                String.valueOf(this.numRecords));
147
        } 
148

    
149
        /**
150
         * @param descripcionBoton The descripcionBoton to set.
151
         */
152
        public void setDescripcionBoton(JButton descripcionBoton) {        
153
                this.descriptionButton = descripcionBoton;
154
        } 
155

    
156
        /**
157
         * @param mapButton The mapButton to set.
158
         */
159
        public void setMapButton(JButton mapButton) {        
160
                this.mapButton = mapButton;
161
        } 
162

    
163
        /**
164
         * This method initializes jPanel
165
         * @return javax.swing.JPanel
166
         */
167
        private JPanel getDescriptionPanel() {        
168
                if (descriptionPanel == null) {
169
                        jLabel1 = new JLabel();
170
                        descriptionPanel = new JPanel();
171
                        descriptionPanel.setLayout(new BoxLayout(descriptionPanel,
172
                                        BoxLayout.X_AXIS));
173
                        descriptionPanel.setPreferredSize(new java.awt.Dimension(600, 300));
174
                        jLabel1.setText("");
175
                        jLabel1.setPreferredSize(new java.awt.Dimension(40, 0));
176
                        descriptionPanel.add(getDescriptionScroll(), null);
177
                        if (mustShowThumbnails) {
178
                                descriptionPanel.add(jLabel1, null);
179
                                descriptionPanel.add(getImagePanel(), null);
180
                        } else {
181
                                descriptionPanel.setPreferredSize(new java.awt.Dimension(400,
182
                                                300));
183
                        }
184
                }
185
                return descriptionPanel;
186
        } 
187

    
188
        /**
189
         * This method initializes jTextArea
190
         * @return javax.swing.JTextArea
191
         */
192
        public JEditorPane getDescriptionArea() {        
193
                if (descriptionArea == null) {
194
                        descriptionArea = new JEditorPane();
195
                        descriptionArea.setPreferredSize(new java.awt.Dimension(400, 300));
196
                        descriptionArea.setContentType("text/html");                        
197
                }
198
                return descriptionArea;
199
        } 
200

    
201
        /**
202
         * This method initializes jPanel
203
         * @return javax.swing.JPanel
204
         */
205
        private JPanel getLinksPanel() {        
206
                if (linksPanel == null) {
207
                        FlowLayout flowLayout = new FlowLayout();
208
                        flowLayout.setAlignment(flowLayout.RIGHT);
209
                        linksPanel = new JPanel(flowLayout);
210
                        linksPanel.add(getDescriptionButton(), null);
211
                        linksPanel.add(getMapButton(), null);
212
                        linksPanel.add(getCloseButton(), null);
213
                }
214
                return linksPanel;
215
        } 
216

    
217
        /**
218
         * This method initializes jPanel
219
         * @return javax.swing.JPanel
220
         */
221
        private JPanel getJPanel() {        
222
                if (jPanel == null) {
223
                        jPanel = new JPanel();
224
                }
225
                return jPanel;
226
        } 
227

    
228
        /**
229
         * This method initializes jButton
230
         * @return javax.swing.JButton
231
         */
232
        public JButton getDescriptionButton() {        
233
                if (descriptionButton == null) {
234
                        descriptionButton = new JButton();
235
                        descriptionButton.setText(Messages.getText("description"));
236
                        descriptionButton.setActionCommand("description");
237
                        descriptionButton.setPreferredSize(new Dimension(95, 23));
238
                }
239
                return descriptionButton;
240
        } 
241

    
242
        /**
243
         * This method initializes jLabel
244
         * @return javax.swing.JLabel
245
         */
246
        private JLabel getJLabel() {        
247
                if (jLabel == null) {
248
                        jLabel = new JLabel();
249
                }
250
                return jLabel;
251
        } 
252

    
253
        /**
254
         * This method initializes mapButton
255
         * @return javax.swing.JButton
256
         */
257
        public JButton getMapButton() {        
258
                if (mapButton == null) {
259
                        mapButton = new JButton();
260
                        mapButton.setText(Messages.getText("addLayer"));
261
                        mapButton.setActionCommand("layer");
262
                        mapButton.setPreferredSize(new Dimension(95, 23));
263
                }
264
                return mapButton;
265
        } 
266

    
267
        /**
268
         * This method initializes descriptionScroll
269
         * @return javax.swing.JScrollPane
270
         */
271
        public JScrollPane getDescriptionScroll() {        
272
                if (descriptionScroll == null) {
273
                        descriptionScroll = new JScrollPane();
274
                        descriptionScroll.setViewportView(getDescriptionArea());
275
                        descriptionScroll.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
276
                        descriptionScroll.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
277
                        descriptionScroll.setPreferredSize(new java.awt.Dimension(400, 250));
278
                }
279
                return descriptionScroll;
280
        } 
281

    
282
        /**
283
         * @return Returns the record.
284
         */
285
        public Record getRecord() {        
286
                return record;
287
        } 
288

    
289
        /**
290
         * @param record The Record to set.
291
         */
292
        public void setRecord(Record record) {        
293
                this.record = record;
294
        } 
295

    
296
        /**
297
         * @param node 
298
         * @param protocol 
299
         */
300
        public void loadTextNewRecord(Record record) {        
301
                setRecord(record);
302
                descriptionArea.setText(this.getHtml());
303
                descriptionArea.setCaretPosition(0);
304
                if (getRecord() != null){
305
                        if (getRecord().getImage() == null){
306
                                imageLabel.setIcon(getImgIcon(null));        
307
                                //DOWNLOAD the image
308
                                new ImageThread();                        
309
                        }else{
310
                                imageLabel.setIcon(getImgIcon(getRecord().getImage()));
311
                        }
312
                }
313
                this.repaint();
314
        } 
315

    
316
        /**
317
         * @return the HTML code to show
318
         */
319
        private String getHtml() {        
320
                StringBuffer html = new StringBuffer();
321
                html.append("<html><body>");
322
                if(getRecord() == null){
323
                        html.append("Error");
324
                        html.append("</body></html>");
325
                        return html.toString();
326
                }
327
                if ((getRecord().getNode() != null) && (getRecord().getNode().getName() != null)){
328
                        if (getRecord().getNode().getName().equals(XMLNode.ISNOTXML)){
329
                                html.append(getRecord().getNode().getText());
330
                                html.append("</body></html>");
331
                                return html.toString();
332
                        }
333
                }
334
                html.append("<font COLOR=\"#0000FF\">");
335
                if (this.getRecord().getTitle() != null) {
336
                        html.append(this.getRecord().getTitle());
337
                }
338
                html.append("<font COLOR=\"#000000\">");
339
                if ((this.getRecord().getAbstract_() != null) &&
340
                                (!(this.getRecord().getAbstract_().equals("")))) {
341
                        html.append("<br>");
342
                        html.append("<br>");
343
                        html.append("<b>" + Messages.getText("abstract") + ": </b>");
344
                        html.append(this.getRecord().getAbstract_());
345
                }
346
                if ((this.getRecord().getPurpose() != null) &&
347
                                (!(this.getRecord().getPurpose().equals("")))) {
348
                        html.append("<br>");
349
                        html.append("<br>");
350
                        html.append("<b>" + Messages.getText("purpose") +": </b>");
351
                        html.append(this.getRecord().getPurpose());
352
                }
353
                if (this.getRecord().getKeyWords() != null) {
354
                        if (this.getRecord().getKeyWords()[0] != null) {
355
                                html.append("<br>");
356
                                html.append("<br>");
357
                                html.append("<b>" + Messages.getText("keyWords") + ":</b>");
358
                                html.append(this.getRecord().getKeyWords()[0]);
359
                        }
360
                }
361
                if (this.getRecord().getKeyWords() != null) {
362
                        for (int i = 1; i < this.getRecord().getKeyWords().length; i++)
363
                                if (this.getRecord().getKeyWords()[i] != null) {
364
                                        html.append(", " + this.getRecord().getKeyWords()[i]);
365
                                }
366
                }
367
                html.append("</body></html>");
368
                return html.toString();
369
        } 
370

    
371
        /**
372
         * This method initializes imagePanel
373
         * @return javax.swing.JPanel
374
         */
375
        private JPanel getImagePanel() {        
376
                if (imagePanel == null) {
377
                        imagePanel = new JPanel();
378
                        imagePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
379
                        imagePanel.setMaximumSize(new java.awt.Dimension(207, 210));
380
                        JPanel borderPanel = new JPanel();
381
                        borderPanel.setLayout(new BorderLayout());
382
                        borderPanel.setBorder(BorderFactory.createCompoundBorder(
383
                                        BorderFactory.createBevelBorder(BevelBorder.LOWERED),
384
                                        BorderFactory.createEmptyBorder(5, 5, 5, 5)));
385
                        imagePanel.add(borderPanel, BorderLayout.CENTER);
386
                        imageLabel = new JLabel(getImgIcon(null));
387
                        borderPanel.add(imageLabel, BorderLayout.CENTER);
388
                }
389
                return imagePanel;
390
        } 
391

    
392
        /**
393
         * @return 
394
         * @param pic 
395
         */
396
        public ImageIcon getImgIcon(BufferedImage pic) {        
397
                BufferedImage img = new BufferedImage(180, 180,
398
                                BufferedImage.TYPE_INT_ARGB);
399
                Graphics2D g = img.createGraphics();
400
                if (pic != null) {
401
                        double fw =         1.0;
402
                        double fh =         1.0;
403
                        if (pic.getWidth() > pic.getHeight()){
404
                                fw = 180D / pic.getWidth();
405
                                fh = fw; 
406
                        }else{
407
                                fh = 180D / pic.getHeight();
408
                                fw = fh;
409
                        }
410

    
411
                        AffineTransform mat = AffineTransform.getScaleInstance(fw, fh);
412
                        g.drawImage(pic, mat, null);
413
                } else {
414

    
415
                        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
416
                                        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
417
                        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
418
                                        RenderingHints.VALUE_FRACTIONALMETRICS_ON);
419
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
420
                                        RenderingHints.VALUE_ANTIALIAS_ON);
421
                        g.setFont(new Font("Lucida Bright", Font.ITALIC, 30));
422
                        g.rotate(-Math.PI / 4, 100D, 100D);
423
                        String text = "NO PICTURE"; //DateFormat.getDateInstance(DateFormat.SHORT).format(new Date());
424
                        TextLayout tl = new TextLayout(text, g.getFont(),
425
                                        g.getFontRenderContext());
426
                        Rectangle2D bounds = tl.getBounds();
427
                        double x = (( 180D - bounds.getWidth()) / 2) - bounds.getX();
428
                        double y = (( 180D - bounds.getHeight()) / 2) - bounds.getY();
429
                        Shape outline = tl.getOutline(AffineTransform.getTranslateInstance(x +
430
                                        2, y + 1));
431
                        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
432
                        g.setPaint(Color.WHITE);
433
                        g.draw(outline);
434
                        g.setPaint(new GradientPaint(0, 0, new Color(192, 192, 255), 30,
435
                                        20, new Color(255, 255, 64), true));
436
                        tl.draw(g, (float) x, (float) y);
437
                }
438
                g.dispose();
439
                return new ImageIcon(img);
440
        } 
441

    
442
        /**
443
         * @return Returns the imageURL.
444
         */
445
        public URL getImageURL() {        
446
                return imageURL;
447
        } 
448

    
449
        /**
450
         * @param imageURL The imageURL to set.
451
         */
452
        public void setImageURL(URL imageURL) {        
453
                this.imageURL = imageURL;
454
        } 
455

    
456
        /**
457
         * This method initializes jPanel1
458
         * @return javax.swing.JPanel
459
         */
460
        private JPanel getJPanel1() {        
461
                if (jPanel1 == null) {
462
                        jPanel1 = new JPanel();
463
                }
464
                return jPanel1;
465
        } 
466

    
467
        /**
468
         * This method initializes jPanel2
469
         * @return javax.swing.JPanel
470
         */
471
        private JPanel getNextLastPanel() {        
472
                if (nextLastPanel == null) {
473
                        textLabel = new JLabel();
474
                        nextLastPanel = new JPanel();
475
                        textLabel.setText("JLabel");
476
                        nextLastPanel.add(getLastButton(), null);
477
                        nextLastPanel.add(textLabel, null);
478
                        nextLastPanel.add(getNextButton(), null);
479
                }
480
                return nextLastPanel;
481
        } 
482

    
483
        /**
484
         * This method initializes jButton
485
         * 
486
         * 
487
         * @return javax.swing.JButton
488
         */
489
        public JButton getLastButton() {        
490
                if (lastButton == null) {
491
                        lastButton = new JButton();
492
                        lastButton.setText(Messages.getText("last"));
493
                        lastButton.setActionCommand("last");
494
                        lastButton.setEnabled(false);
495
                        lastButton.setPreferredSize(new Dimension(95, 23));
496
                }
497
                return lastButton;
498
        } 
499

    
500
        /**
501
         * This method initializes jButton
502
         * @return javax.swing.JButton
503
         */
504
        public JButton getNextButton() {        
505
                if (nextButton == null) {
506
                        nextButton = new JButton();
507
                        nextButton.setText(Messages.getText("next"));
508
                        nextButton.setActionCommand("next");
509
                        nextButton.setPreferredSize(new Dimension(95, 23));
510
                        if (this.numRecords < 2) {
511
                                nextButton.setEnabled(false);
512
                        }
513
                }
514
                return nextButton;
515
        } 
516

    
517
        /**
518
         * This method initializes jButton
519
         * @return javax.swing.JButton
520
         */
521
        public JButton getCloseButton() {        
522
                if (closeButton == null) {
523
                        closeButton = new JButton();
524
                        closeButton.setText(Messages.getText("close"));
525
                        closeButton.setActionCommand("close");
526
                        closeButton.setPreferredSize(new Dimension(80, 23));
527
                }
528
                return closeButton;
529
        } 
530

    
531
        /**
532
         * This thread is used to download the image
533
         * @author jorpiell
534
         *
535
         */
536
        private class ImageThread implements Runnable {
537
                volatile Thread myThread = null;
538

    
539
                public  ImageThread() {        
540
                        myThread = new Thread(this);
541
                        myThread.start();
542
                } 
543

    
544
                public void stop() {        
545
                        myThread.stop();
546
                } 
547

    
548
                /*
549
                 * (non-Javadoc)
550
                 * @see java.lang.Runnable#run()
551
                 */
552
                public void run() {        
553
                        BufferedImage img = ImageRetriever.getImageUrl(getRecord());
554
                        imageLabel.setIcon(getImgIcon(img));
555
                } 
556
        }
557

    
558
}
559
//@jve:decl-index=0:visual-constraint="107,10"