Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / listview / painters / PaintList.java @ 12632

History | View | Annotate | Download (4.45 KB)

1 12623 bsanchez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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
package org.gvsig.gui.beans.listview.painters;
20
21
import java.awt.Color;
22
import java.awt.Dimension;
23
import java.awt.FontMetrics;
24
import java.awt.GradientPaint;
25
import java.awt.Graphics2D;
26
import java.awt.Rectangle;
27
import java.awt.Shape;
28
import java.util.ArrayList;
29
30
import org.gvsig.gui.beans.listview.IListViewPainter;
31
import org.gvsig.gui.beans.listview.ListViewComponent;
32
import org.gvsig.gui.beans.listview.ListViewItem;
33
/**
34
 * @version 28/06/2007
35
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
36
 */
37
public class PaintList implements IListViewPainter {
38
        ListViewComponent parent;
39
        ArrayList items = null;
40
        int iconsWidth = 35;
41
        int minIconsWidth = 35;
42
        Dimension lastDimension = new Dimension(0, 0);
43
44
        public PaintList(ArrayList items) {
45
                this.items = items;
46
        }
47
48
        /*
49
         * (non-Javadoc)
50
         * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getName()
51
         */
52
        public String getName() {
53
                return "List";
54
        }
55
56
        /*
57
         * (non-Javadoc)
58
         * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getPreferredSize()
59
         */
60
        public Dimension getPreferredSize() {
61
                return lastDimension;
62
        }
63
64
        /*
65
         * (non-Javadoc)
66
         * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#paint(java.awt.Graphics2D, int, int)
67
         */
68
        public void paint(Graphics2D g, Rectangle visibleRect) {
69
                FontMetrics fm = g.getFontMetrics();
70
71
                int minAux = 0;
72
                for (int i = 0; i < items.size(); i++) {
73
                        int auxWidth = g.getFontMetrics().stringWidth(((ListViewItem) items.get(i)).getName()) + 8;
74
                        if (minAux < auxWidth)
75
                                minAux = auxWidth;
76
                }
77
                minAux = visibleRect.width - minAux;
78
                if (minAux < minIconsWidth)
79
                        minAux = minIconsWidth;
80
                iconsWidth = minAux;
81
82
                int height2 = 0;
83
                int width2 = 0;
84
                for (int i = 0; i < items.size(); i++) {
85
                        ((ListViewItem) items.get(i)).setNameRectangle(null);
86
87
                        int auxWidth = g.getFontMetrics().stringWidth(((ListViewItem) items.get(i)).getName());
88
                        if ((minIconsWidth + 3 + auxWidth - visibleRect.x) > visibleRect.width) {
89
                                ((ListViewItem) items.get(i)).setShowTooltip(true);
90
                        } else {
91
                                ((ListViewItem) items.get(i)).setShowTooltip(false);
92
                        }
93
                        if (width2 < auxWidth)
94
                                width2 = auxWidth;
95
96 12632 bsanchez
                        ((ListViewItem) items.get(i)).getItemRectangle().setBounds(visibleRect.x, i * 17, visibleRect.width, 17);
97
98
                        if (!((ListViewItem) items.get(i)).getItemRectangle().intersects(visibleRect))
99 12623 bsanchez
                                continue;
100
101
                        int upper = fm.getLeading() + fm.getAscent() + ((17 - fm.getHeight()) / 2);
102
103
                        if (((ListViewItem) items.get(i)).isSelected()) {
104
                                Color color1 = new Color(89, 153, 229);
105
                                Color color2 = new Color(31, 92, 207);
106
                                g.setPaint(new GradientPaint(0, i * 17 + 1, color1, 0, i * 17 + 16, color2, false));
107
                                g.fillRect(visibleRect.x, i * 17 + 1, visibleRect.width, 16);
108
                                g.setColor(new Color(61, 123, 218));
109
                                g.drawLine(visibleRect.x, i * 17, visibleRect.x + visibleRect.width, i * 17);
110
                                g.setColor(Color.white);
111
                        } else {
112
                                g.setColor(Color.black);
113
                        }
114
                        g.drawString(((ListViewItem) items.get(i)).getName(), iconsWidth + 3, (i * 17) + upper);
115
                        // Guardar el estado de donde se visualiza el nombre y cuanto ocupa
116
                        ((ListViewItem) items.get(i)).setNameRectangle(new Rectangle(iconsWidth + 2, i * 17 - 1, visibleRect.width - (iconsWidth + 2), 20));
117
118
                        Shape clip = g.getClip();
119
                        g.translate(1, i * 17 + 1);
120
                        g.setClip(0, 0, iconsWidth, 15);
121
122
                        if (((ListViewItem) items.get(i)).getIcon() != null)
123
                                ((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected());
124
125
                        g.setClip(clip);
126
                        g.translate(-1, -(i * 17 + 1));
127
                }
128
                height2 = items.size() * 17;
129
130
                lastDimension = new Dimension(minIconsWidth + 3 + width2, height2);
131
        //lastDimension = new Dimension(0, height2);
132
        }
133
}