Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / listview / painters / PaintList.java @ 40561

History | View | Annotate | Download (4.47 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.listview.painters;
25

    
26
import java.awt.Color;
27
import java.awt.Dimension;
28
import java.awt.FontMetrics;
29
import java.awt.GradientPaint;
30
import java.awt.Graphics2D;
31
import java.awt.Rectangle;
32
import java.awt.Shape;
33
import java.util.ArrayList;
34

    
35
import org.gvsig.gui.beans.listview.IListViewPainter;
36
import org.gvsig.gui.beans.listview.ListViewItem;
37
/**
38
 * @version 28/06/2007
39
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
40
 */
41
public class PaintList implements IListViewPainter {
42
        ArrayList items = null;
43
        int iconsWidth = 35;
44
        int minIconsWidth = 35;
45
        Dimension lastDimension = new Dimension(0, 0);
46

    
47
        public PaintList(ArrayList items) {
48
                this.items = items;
49
        }
50

    
51
        /*
52
         * (non-Javadoc)
53
         * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getName()
54
         */
55
        public String getName() {
56
                return "List";
57
        }
58

    
59
        /*
60
         * (non-Javadoc)
61
         * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getPreferredSize()
62
         */
63
        public Dimension getPreferredSize() {
64
                return lastDimension;
65
        }
66

    
67
        /*
68
         * (non-Javadoc)
69
         * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#paint(java.awt.Graphics2D, int, int)
70
         */
71
        public void paint(Graphics2D g, Rectangle visibleRect) {
72
                FontMetrics fm = g.getFontMetrics();
73

    
74
                int minAux = 0;
75
                for (int i = 0; i < items.size(); i++) {
76
                        int auxWidth = g.getFontMetrics().stringWidth(((ListViewItem) items.get(i)).getName()) + 8;
77
                        if (minAux < auxWidth)
78
                                minAux = auxWidth;
79
                }
80
                minAux = visibleRect.width - minAux;
81
                if (minAux < minIconsWidth)
82
                        minAux = minIconsWidth;
83
                iconsWidth = minAux;
84

    
85
                int height2 = 0;
86
                int width2 = 0;
87
                for (int i = 0; i < items.size(); i++) {
88
                        ((ListViewItem) items.get(i)).setNameRectangle(null);
89

    
90
                        int auxWidth = g.getFontMetrics().stringWidth(((ListViewItem) items.get(i)).getName());
91
                        if ((minIconsWidth + 3 + auxWidth - visibleRect.x) > visibleRect.width) {
92
                                ((ListViewItem) items.get(i)).setShowTooltip(true);
93
                        } else {
94
                                ((ListViewItem) items.get(i)).setShowTooltip(false);
95
                        }
96
                        if (width2 < auxWidth)
97
                                width2 = auxWidth;
98

    
99
                        ((ListViewItem) items.get(i)).getItemRectangle().setBounds(visibleRect.x, i * 17, visibleRect.width, 17);
100

    
101
                        if (!((ListViewItem) items.get(i)).getItemRectangle().intersects(visibleRect))
102
                                continue;
103

    
104
                        int upper = fm.getLeading() + fm.getAscent() + ((17 - fm.getHeight()) / 2);
105

    
106
                        if (((ListViewItem) items.get(i)).isSelected()) {
107
                                Color color1 = new Color(89, 153, 229);
108
                                Color color2 = new Color(31, 92, 207);
109
                                g.setPaint(new GradientPaint(0, i * 17 + 1, color1, 0, i * 17 + 16, color2, false));
110
                                g.fillRect(visibleRect.x, i * 17 + 1, visibleRect.width, 16);
111
                                g.setColor(new Color(61, 123, 218));
112
                                g.drawLine(visibleRect.x, i * 17, visibleRect.x + visibleRect.width, i * 17);
113
                                g.setColor(Color.white);
114
                        } else {
115
                                g.setColor(Color.black);
116
                        }
117
                        g.drawString(((ListViewItem) items.get(i)).getName(), iconsWidth + 3, (i * 17) + upper);
118
                        // Guardar el estado de donde se visualiza el nombre y cuanto ocupa
119
                        ((ListViewItem) items.get(i)).setNameRectangle(new Rectangle(iconsWidth + 2, i * 17 - 1, visibleRect.width - (iconsWidth + 2), 20));
120

    
121
                        Shape clip = g.getClip();
122
                        g.translate(1, i * 17 + 1);
123
                        g.setClip(0, 0, iconsWidth, 15);
124

    
125
                        if (((ListViewItem) items.get(i)).getIcon() != null)
126
                                ((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected());
127

    
128
                        g.setClip(clip);
129
                        g.translate(-1, -(i * 17 + 1));
130
                }
131
                height2 = items.size() * 17;
132

    
133
                lastDimension = new Dimension(minIconsWidth + 3 + width2, height2);
134
        //lastDimension = new Dimension(0, height2);
135
        }
136
}