svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / graphic / listview / painters / SmallIcon.java @ 12592
History | View | Annotate | Download (3.94 KB)
1 |
/* 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.graphic.listview.painters; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.awt.Dimension; |
23 |
import java.awt.Graphics2D; |
24 |
import java.awt.Rectangle; |
25 |
import java.awt.Shape; |
26 |
import java.util.ArrayList; |
27 |
|
28 |
import org.gvsig.gui.beans.graphic.listview.IListViewPainter; |
29 |
import org.gvsig.gui.beans.graphic.listview.ListViewComponent; |
30 |
import org.gvsig.gui.beans.graphic.listview.ListViewItem; |
31 |
/**
|
32 |
* Iconos de 82x28
|
33 |
*
|
34 |
* @version 28/06/2007
|
35 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
36 |
*/
|
37 |
public class SmallIcon implements IListViewPainter { |
38 |
|
39 |
ListViewComponent parent; |
40 |
ArrayList items = null; |
41 |
int iconsWidth = 40; |
42 |
int minIconsWidth = 40; |
43 |
int iconsHeight = 28; |
44 |
Dimension lastDimension = new Dimension(0, 0); |
45 |
int cols = 0; |
46 |
|
47 |
public SmallIcon(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 "SmallIcon"; |
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 |
int aux = (int) Math.floor(visibleRect.getWidth() / (minIconsWidth + 2)); |
73 |
if (aux > items.size())
|
74 |
aux = items.size(); |
75 |
iconsWidth = (int) (Math.floor(visibleRect.getWidth() / aux) - 2); |
76 |
|
77 |
int height2 = 0; |
78 |
|
79 |
int posX = 0; |
80 |
int posY = 0; |
81 |
cols = 0;
|
82 |
for (int i = 0; i < items.size(); i++) { |
83 |
// Evito que se pueda editar el nombre
|
84 |
((ListViewItem) items.get(i)).setNameRectangle(null);
|
85 |
|
86 |
((ListViewItem) items.get(i)).setShowTooltip(true);
|
87 |
if (posX != 0) { |
88 |
if (((posX + 1) * (iconsWidth + 2)) > visibleRect.getWidth()) { |
89 |
posX = 0;
|
90 |
posY++; |
91 |
} |
92 |
} |
93 |
|
94 |
if (((ListViewItem) items.get(i)).isSelected()) {
|
95 |
g.setColor(new Color(49, 106, 197)); |
96 |
g.fillRect(posX * (iconsWidth + 2), posY * (iconsHeight + 2), iconsWidth + 2, iconsHeight + 2); |
97 |
} |
98 |
|
99 |
Shape clip = g.getClip();
|
100 |
g.translate(posX * (iconsWidth + 2) + 1, (posY * (iconsHeight + 2)) + 1); |
101 |
g.setClip(0, 0, iconsWidth, iconsHeight); |
102 |
|
103 |
if (((ListViewItem) items.get(i)).getIcon() != null) |
104 |
((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected()); |
105 |
|
106 |
g.setClip(clip); |
107 |
g.translate(- (posX * (iconsWidth + 2) + 1), - ((posY * (iconsHeight + 2)) + 1)); |
108 |
|
109 |
if (height2 < ((posY + 1) * (iconsHeight + 2))) |
110 |
height2 = (posY + 1) * (iconsHeight + 2); |
111 |
|
112 |
if (cols < posX)
|
113 |
cols = posX; |
114 |
|
115 |
posX++; |
116 |
} |
117 |
|
118 |
lastDimension = new Dimension(minIconsWidth + 2, height2); |
119 |
} |
120 |
|
121 |
/*
|
122 |
* (non-Javadoc)
|
123 |
* @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getItem(int, int)
|
124 |
*/
|
125 |
public int getItem(int x, int y) { |
126 |
if (((cols + 1) * (iconsWidth + 2)) < x) |
127 |
return -1; |
128 |
|
129 |
int col = (int) Math.floor(x / (iconsWidth + 2)); |
130 |
int row = (int) Math.floor(y / (iconsHeight + 2)); |
131 |
|
132 |
int pos = ((row * (cols + 1)) + col); |
133 |
if ((pos >= 0) && (pos < items.size())) |
134 |
return pos;
|
135 |
|
136 |
return -1; |
137 |
} |
138 |
} |