Revision 12623
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/listview/IListViewPainter.java | ||
---|---|---|
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.listview; |
|
20 |
|
|
21 |
import java.awt.Dimension; |
|
22 |
import java.awt.Graphics2D; |
|
23 |
import java.awt.Rectangle; |
|
24 |
/** |
|
25 |
* @version 28/06/2007 |
|
26 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es) |
|
27 |
*/ |
|
28 |
public interface IListViewPainter { |
|
29 |
/** |
|
30 |
* Devuelve el nombre de la vista |
|
31 |
* @return |
|
32 |
*/ |
|
33 |
public String getName(); |
|
34 |
|
|
35 |
/** |
|
36 |
* Devuelve la dimension minima del componente |
|
37 |
* @return |
|
38 |
*/ |
|
39 |
public Dimension getPreferredSize(); |
|
40 |
|
|
41 |
/** |
|
42 |
* Pintar el componente |
|
43 |
* @param g |
|
44 |
*/ |
|
45 |
public void paint(Graphics2D g, Rectangle visibleRect); |
|
46 |
} |
|
0 | 47 |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/listview/ListViewItem.java | ||
---|---|---|
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.listview; |
|
20 |
|
|
21 |
import java.awt.Rectangle; |
|
22 |
|
|
23 |
/** |
|
24 |
* <code>ListViewItem</code> representa un item para ser usado desde |
|
25 |
* ListViewComponent |
|
26 |
* |
|
27 |
* @version 28/06/2007 |
|
28 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es) |
|
29 |
*/ |
|
30 |
public class ListViewItem { |
|
31 |
IIconPaint icon = null; |
|
32 |
String name = null; |
|
33 |
Rectangle nameRectangle = null; |
|
34 |
Rectangle itemRectangle = null; |
|
35 |
boolean selected = false; |
|
36 |
boolean showTooltip = true; |
|
37 |
Object tag = null; |
|
38 |
|
|
39 |
/** |
|
40 |
* Construye un ListViewItem con un icono y su nombre. |
|
41 |
* @param icon |
|
42 |
* @param name |
|
43 |
*/ |
|
44 |
public ListViewItem(IIconPaint icon, String name) { |
|
45 |
this.icon = icon; |
|
46 |
this.name = name; |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* Obtener el nombre del item |
|
51 |
* @return |
|
52 |
*/ |
|
53 |
public String getName() { |
|
54 |
return name; |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Definir el nombre del item |
|
59 |
* @param name |
|
60 |
*/ |
|
61 |
public void setName(String name) { |
|
62 |
this.name = name; |
|
63 |
} |
|
64 |
|
|
65 |
/** |
|
66 |
* Obtener el icono del item |
|
67 |
* @return |
|
68 |
*/ |
|
69 |
public IIconPaint getIcon() { |
|
70 |
return icon; |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* Especificar el icono del item |
|
75 |
* @param icon |
|
76 |
*/ |
|
77 |
public void setIcon(IIconPaint icon) { |
|
78 |
this.icon = icon; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Comprobar si el item esta seleccionado |
|
83 |
* @return |
|
84 |
*/ |
|
85 |
public boolean isSelected() { |
|
86 |
return selected; |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* Definir si el item esta seleccionado |
|
91 |
* @param selected |
|
92 |
*/ |
|
93 |
public void setSelected(boolean selected) { |
|
94 |
this.selected = selected; |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* Definir algun campo extra necesario por el programador |
|
99 |
* @return |
|
100 |
*/ |
|
101 |
public Object getTag() { |
|
102 |
return tag; |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Obtiene el campo extra |
|
107 |
* @return |
|
108 |
*/ |
|
109 |
public void setTag(Object tag) { |
|
110 |
this.tag = tag; |
|
111 |
} |
|
112 |
|
|
113 |
public boolean isShowTooltip() { |
|
114 |
return showTooltip; |
|
115 |
} |
|
116 |
|
|
117 |
public void setShowTooltip(boolean showTooltip) { |
|
118 |
this.showTooltip = showTooltip; |
|
119 |
} |
|
120 |
|
|
121 |
public Rectangle getNameRectangle() { |
|
122 |
return nameRectangle; |
|
123 |
} |
|
124 |
|
|
125 |
public void setNameRectangle(Rectangle nameRectangle) { |
|
126 |
this.nameRectangle = nameRectangle; |
|
127 |
} |
|
128 |
|
|
129 |
public Rectangle getItemRectangle() { |
|
130 |
return itemRectangle; |
|
131 |
} |
|
132 |
|
|
133 |
public void setItemRectangle(Rectangle itemRectangle) { |
|
134 |
this.itemRectangle = itemRectangle; |
|
135 |
} |
|
136 |
} |
|
0 | 137 |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/listview/painters/SmallIcon.java | ||
---|---|---|
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.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.listview.IListViewPainter; |
|
29 |
import org.gvsig.gui.beans.listview.ListViewComponent; |
|
30 |
import org.gvsig.gui.beans.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 |
((ListViewItem) items.get(i)).setItemRectangle(null); |
|
86 |
|
|
87 |
((ListViewItem) items.get(i)).setShowTooltip(true); |
|
88 |
if (posX != 0) { |
|
89 |
if (((posX + 1) * (iconsWidth + 2)) > visibleRect.getWidth()) { |
|
90 |
posX = 0; |
|
91 |
posY++; |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
Rectangle rectangleItem = new Rectangle(posX * (iconsWidth + 2) + 1, (posY * (iconsHeight + 2)) + 1, iconsWidth - 1, iconsHeight - 1); |
|
96 |
if (rectangleItem.intersects(visibleRect)) { |
|
97 |
if (((ListViewItem) items.get(i)).isSelected()) { |
|
98 |
g.setColor(new Color(49, 106, 197)); |
|
99 |
g.fillRect(posX * (iconsWidth + 2), posY * (iconsHeight + 2), iconsWidth + 2, iconsHeight + 2); |
|
100 |
} |
|
101 |
|
|
102 |
Shape clip = g.getClip(); |
|
103 |
g.translate(posX * (iconsWidth + 2) + 1, (posY * (iconsHeight + 2)) + 1); |
|
104 |
g.setClip(0, 0, iconsWidth, iconsHeight); |
|
105 |
|
|
106 |
if (((ListViewItem) items.get(i)).getIcon() != null) |
|
107 |
((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected()); |
|
108 |
|
|
109 |
g.setClip(clip); |
|
110 |
g.translate(- (posX * (iconsWidth + 2) + 1), - ((posY * (iconsHeight + 2)) + 1)); |
|
111 |
|
|
112 |
((ListViewItem) items.get(i)).setItemRectangle(rectangleItem); |
|
113 |
} |
|
114 |
|
|
115 |
if (height2 < ((posY + 1) * (iconsHeight + 2))) |
|
116 |
height2 = (posY + 1) * (iconsHeight + 2); |
|
117 |
|
|
118 |
if (cols < posX) |
|
119 |
cols = posX; |
|
120 |
|
|
121 |
posX++; |
|
122 |
} |
|
123 |
|
|
124 |
lastDimension = new Dimension(minIconsWidth + 2, height2); |
|
125 |
} |
|
126 |
} |
|
0 | 127 |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/listview/painters/LargeIcon.java | ||
---|---|---|
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.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.listview.IListViewPainter; |
|
29 |
import org.gvsig.gui.beans.listview.ListViewComponent; |
|
30 |
import org.gvsig.gui.beans.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 LargeIcon implements IListViewPainter { |
|
38 |
ListViewComponent parent; |
|
39 |
ArrayList items = null; |
|
40 |
int iconsWidth = 82; |
|
41 |
int minIconsWidth = 82; |
|
42 |
int iconsHeight = 28; |
|
43 |
Dimension lastDimension = new Dimension(0, 0); |
|
44 |
int cols = 0; |
|
45 |
|
|
46 |
public LargeIcon(ArrayList items) { |
|
47 |
this.items = items; |
|
48 |
} |
|
49 |
|
|
50 |
/* |
|
51 |
* (non-Javadoc) |
|
52 |
* @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getName() |
|
53 |
*/ |
|
54 |
public String getName() { |
|
55 |
return "LargeIcon"; |
|
56 |
} |
|
57 |
|
|
58 |
/* |
|
59 |
* (non-Javadoc) |
|
60 |
* @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getPreferredSize() |
|
61 |
*/ |
|
62 |
public Dimension getPreferredSize() { |
|
63 |
return lastDimension; |
|
64 |
} |
|
65 |
|
|
66 |
/* |
|
67 |
* (non-Javadoc) |
|
68 |
* @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#paint(java.awt.Graphics2D, int, int) |
|
69 |
*/ |
|
70 |
public void paint(Graphics2D g, Rectangle visibleRect) { |
|
71 |
int aux = (int) Math.floor(visibleRect.getWidth() / (minIconsWidth + 2)); |
|
72 |
if (aux > items.size()) |
|
73 |
aux = items.size(); |
|
74 |
iconsWidth = (int) (Math.floor(visibleRect.getWidth() / aux) - 2); |
|
75 |
|
|
76 |
int height2 = 0; |
|
77 |
|
|
78 |
int posX = 0; |
|
79 |
int posY = 0; |
|
80 |
cols = 0; |
|
81 |
for (int i = 0; i < items.size(); i++) { |
|
82 |
// Evito que se pueda editar el nombre |
|
83 |
((ListViewItem) items.get(i)).setNameRectangle(null); |
|
84 |
((ListViewItem) items.get(i)).setItemRectangle(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 |
Rectangle rectangleItem = new Rectangle(posX * (iconsWidth + 2) + 1, (posY * (iconsHeight + 2)) + 1, iconsWidth - 1, iconsHeight - 1); |
|
95 |
if (rectangleItem.intersects(visibleRect)) { |
|
96 |
if (((ListViewItem) items.get(i)).isSelected()) { |
|
97 |
g.setColor(new Color(49, 106, 197)); |
|
98 |
g.fillRect(posX * (iconsWidth + 2), posY * (iconsHeight + 2), iconsWidth + 2, iconsHeight + 2); |
|
99 |
} |
|
100 |
|
|
101 |
Shape clip = g.getClip(); |
|
102 |
g.translate(posX * (iconsWidth + 2) + 1, (posY * (iconsHeight + 2)) + 1); |
|
103 |
g.setClip(0, 0, iconsWidth, iconsHeight); |
|
104 |
|
|
105 |
if (((ListViewItem) items.get(i)).getIcon() != null) |
|
106 |
((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected()); |
|
107 |
|
|
108 |
g.setClip(clip); |
|
109 |
g.translate(-(posX * (iconsWidth + 2) + 1), -((posY * (iconsHeight + 2)) + 1)); |
|
110 |
|
|
111 |
((ListViewItem) items.get(i)).setItemRectangle(rectangleItem); |
|
112 |
} |
|
113 |
|
|
114 |
if (height2 < ((posY + 1) * (iconsHeight + 2))) |
|
115 |
height2 = (posY + 1) * (iconsHeight + 2); |
|
116 |
|
|
117 |
if (cols < posX) |
|
118 |
cols = posX; |
|
119 |
|
|
120 |
posX++; |
|
121 |
} |
|
122 |
|
|
123 |
lastDimension = new Dimension(minIconsWidth + 2, height2); |
|
124 |
} |
|
125 |
} |
|
0 | 126 |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/listview/painters/PaintList.java | ||
---|---|---|
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.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 |
((ListViewItem) items.get(i)).setItemRectangle(null); |
|
87 |
|
|
88 |
int auxWidth = g.getFontMetrics().stringWidth(((ListViewItem) items.get(i)).getName()); |
|
89 |
if ((minIconsWidth + 3 + auxWidth - visibleRect.x) > visibleRect.width) { |
|
90 |
((ListViewItem) items.get(i)).setShowTooltip(true); |
|
91 |
} else { |
|
92 |
((ListViewItem) items.get(i)).setShowTooltip(false); |
|
93 |
} |
|
94 |
if (width2 < auxWidth) |
|
95 |
width2 = auxWidth; |
|
96 |
|
|
97 |
if ((((i + 1) * 17) < visibleRect.y) || ((i * 17) > (visibleRect.y + visibleRect.height))) |
|
98 |
continue; |
|
99 |
|
|
100 |
int upper = fm.getLeading() + fm.getAscent() + ((17 - fm.getHeight()) / 2); |
|
101 |
|
|
102 |
if (((ListViewItem) items.get(i)).isSelected()) { |
|
103 |
Color color1 = new Color(89, 153, 229); |
|
104 |
Color color2 = new Color(31, 92, 207); |
|
105 |
g.setPaint(new GradientPaint(0, i * 17 + 1, color1, 0, i * 17 + 16, color2, false)); |
|
106 |
g.fillRect(visibleRect.x, i * 17 + 1, visibleRect.width, 16); |
|
107 |
g.setColor(new Color(61, 123, 218)); |
|
108 |
g.drawLine(visibleRect.x, i * 17, visibleRect.x + visibleRect.width, i * 17); |
|
109 |
g.setColor(Color.white); |
|
110 |
} else { |
|
111 |
g.setColor(Color.black); |
|
112 |
} |
|
113 |
g.drawString(((ListViewItem) items.get(i)).getName(), iconsWidth + 3, (i * 17) + upper); |
|
114 |
// Guardar el estado de donde se visualiza el nombre y cuanto ocupa |
|
115 |
((ListViewItem) items.get(i)).setNameRectangle(new Rectangle(iconsWidth + 2, i * 17 - 1, visibleRect.width - (iconsWidth + 2), 20)); |
|
116 |
|
|
117 |
Shape clip = g.getClip(); |
|
118 |
g.translate(1, i * 17 + 1); |
|
119 |
g.setClip(0, 0, iconsWidth, 15); |
|
120 |
|
|
121 |
((ListViewItem) items.get(i)).setItemRectangle(new Rectangle(visibleRect.x, i * 17, visibleRect.width, 17)); |
|
122 |
|
|
123 |
if (((ListViewItem) items.get(i)).getIcon() != null) |
|
124 |
((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected()); |
|
125 |
|
|
126 |
g.setClip(clip); |
|
127 |
g.translate(-1, -(i * 17 + 1)); |
|
128 |
} |
|
129 |
height2 = items.size() * 17; |
|
130 |
|
|
131 |
lastDimension = new Dimension(minIconsWidth + 3 + width2, height2); |
|
132 |
//lastDimension = new Dimension(0, height2); |
|
133 |
} |
|
134 |
} |
|
0 | 135 |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/listview/ListViewComponent.java | ||
---|---|---|
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.listview; |
|
20 |
|
|
21 |
import java.awt.Color; |
|
22 |
import java.awt.Dimension; |
|
23 |
import java.awt.Graphics; |
|
24 |
import java.awt.Graphics2D; |
|
25 |
import java.awt.Image; |
|
26 |
import java.awt.Point; |
|
27 |
import java.awt.Rectangle; |
|
28 |
import java.awt.event.ActionEvent; |
|
29 |
import java.awt.event.ActionListener; |
|
30 |
import java.awt.event.FocusEvent; |
|
31 |
import java.awt.event.FocusListener; |
|
32 |
import java.awt.event.InputEvent; |
|
33 |
import java.awt.event.KeyEvent; |
|
34 |
import java.awt.event.KeyListener; |
|
35 |
import java.awt.event.MouseEvent; |
|
36 |
import java.awt.event.MouseListener; |
|
37 |
import java.awt.event.MouseMotionListener; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Iterator; |
|
40 |
|
|
41 |
import javax.swing.ButtonGroup; |
|
42 |
import javax.swing.JComponent; |
|
43 |
import javax.swing.JMenu; |
|
44 |
import javax.swing.JPopupMenu; |
|
45 |
import javax.swing.JRadioButtonMenuItem; |
|
46 |
import javax.swing.JScrollPane; |
|
47 |
import javax.swing.JTextField; |
|
48 |
import javax.swing.JViewport; |
|
49 |
import javax.swing.event.ListSelectionEvent; |
|
50 |
import javax.swing.event.ListSelectionListener; |
|
51 |
|
|
52 |
import org.gvsig.gui.beans.listview.painters.LargeIcon; |
|
53 |
import org.gvsig.gui.beans.listview.painters.PaintList; |
|
54 |
import org.gvsig.gui.beans.listview.painters.SmallIcon; |
|
55 |
/** |
|
56 |
* |
|
57 |
* @version 28/06/2007 |
|
58 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es) |
|
59 |
*/ |
|
60 |
public class ListViewComponent extends JComponent implements MouseListener, MouseMotionListener, ActionListener, KeyListener, FocusListener { |
|
61 |
private static final long serialVersionUID = 6177600314634665863L; |
|
62 |
|
|
63 |
/** |
|
64 |
* Lista de los tipos de vista existentes |
|
65 |
*/ |
|
66 |
private ArrayList painters = new ArrayList(); |
|
67 |
private ArrayList paintersMenu = new ArrayList(); |
|
68 |
|
|
69 |
/** |
|
70 |
* Lista de items |
|
71 |
*/ |
|
72 |
private ArrayList items = new ArrayList(); |
|
73 |
|
|
74 |
/** |
|
75 |
* Selecciona el tipo de vista |
|
76 |
*/ |
|
77 |
private int view = 0; |
|
78 |
|
|
79 |
/** |
|
80 |
* Booleano para saber si se permite la multiselecci?n |
|
81 |
*/ |
|
82 |
private boolean multiSelect = false; |
|
83 |
|
|
84 |
private Image image = null; |
|
85 |
private int width = 0; |
|
86 |
private int height = 0; |
|
87 |
private Graphics2D widgetGraphics = null; |
|
88 |
private JMenu jMenu = null; |
|
89 |
private ButtonGroup buttonGroup = null; |
|
90 |
private JPopupMenu jPopupMenu = null; |
|
91 |
|
|
92 |
private JTextField jRenameEdit = null; |
|
93 |
|
|
94 |
|
|
95 |
private int itemEdited = -1; |
|
96 |
private int lastSelected = -1; |
|
97 |
private int cursorPos = -1; |
|
98 |
|
|
99 |
|
|
100 |
private boolean editable = false; |
|
101 |
|
|
102 |
private ArrayList actionCommandListeners = new ArrayList(); |
|
103 |
|
|
104 |
|
|
105 |
/** |
|
106 |
* Construye un <code>ListViewComponent</code> |
|
107 |
* |
|
108 |
*/ |
|
109 |
public ListViewComponent() { |
|
110 |
setFocusable(true); |
|
111 |
|
|
112 |
initialize(); |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Inicializa el <code>ListViewComponent</code> |
|
117 |
*/ |
|
118 |
private void initialize() { |
|
119 |
addListViewPainter(new PaintList(items)); |
|
120 |
addListViewPainter(new SmallIcon(items)); |
|
121 |
addListViewPainter(new LargeIcon(items)); |
|
122 |
|
|
123 |
addKeyListener(this); |
|
124 |
addMouseListener(this); |
|
125 |
addMouseMotionListener(this); |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Obtiene que vista se esta usando en el componente |
|
130 |
* @return |
|
131 |
*/ |
|
132 |
public int getView() { |
|
133 |
return view; |
|
134 |
} |
|
135 |
|
|
136 |
/** |
|
137 |
* Define que vista es la que se va a usar |
|
138 |
* @param view |
|
139 |
*/ |
|
140 |
public void setView(int view) { |
|
141 |
this.view = view; |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* Agrega una vista al componente |
|
146 |
* @param item |
|
147 |
*/ |
|
148 |
public void addListViewPainter(IListViewPainter item) { |
|
149 |
painters.add(item); |
|
150 |
|
|
151 |
JRadioButtonMenuItem jRadioButtonMenuItem = new JRadioButtonMenuItem(); |
|
152 |
getButtonGroup().add(jRadioButtonMenuItem); |
|
153 |
|
|
154 |
jRadioButtonMenuItem.setText(item.getName()); |
|
155 |
if (paintersMenu.size() == 0) |
|
156 |
jRadioButtonMenuItem.setSelected(true); |
|
157 |
getJMenu().add(jRadioButtonMenuItem); |
|
158 |
|
|
159 |
jRadioButtonMenuItem.addActionListener(this); |
|
160 |
|
|
161 |
paintersMenu.add(jRadioButtonMenuItem); |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
165 |
* Agrega un item al componente |
|
166 |
* @param item |
|
167 |
*/ |
|
168 |
public void addItem(ListViewItem item) { |
|
169 |
addItem(item, false); |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* Agrega un item al componente, si acceptRepeatNames es false no se aceptaran |
|
174 |
* nombres repetidos |
|
175 |
* @param item |
|
176 |
* @param acceptRepeatNames |
|
177 |
*/ |
|
178 |
public void addItem(ListViewItem item, boolean acceptRepeatNames) { |
|
179 |
items.add(item); |
|
180 |
if (!acceptRepeatNames) |
|
181 |
changeName(item.getName(), items.size() - 1); |
|
182 |
repaint(); |
|
183 |
viewItem(items.size() - 1); |
|
184 |
} |
|
185 |
|
|
186 |
/** |
|
187 |
* Agrega el item en la posicion especificada de la lista. |
|
188 |
* @param pos |
|
189 |
* @param item |
|
190 |
*/ |
|
191 |
public void addItem(int pos, ListViewItem item) { |
|
192 |
items.add(pos, item); |
|
193 |
changeName(item.getName(), pos); |
|
194 |
repaint(); |
|
195 |
viewItem(pos); |
|
196 |
} |
|
197 |
|
|
198 |
/** |
|
199 |
* Agrega un item al componente |
|
200 |
* @param item |
|
201 |
*/ |
|
202 |
public void removeItem(int index) { |
|
203 |
items.remove(index); |
|
204 |
repaint(); |
|
205 |
} |
|
206 |
|
|
207 |
/** |
|
208 |
* Borra todos los items seleccionados |
|
209 |
*/ |
|
210 |
public void removeSelecteds() { |
|
211 |
for (int i = (items.size()-1); i>=0; i--) |
|
212 |
if (((ListViewItem) items.get(i)).isSelected()) |
|
213 |
items.remove(i); |
|
214 |
|
|
215 |
repaint(); |
|
216 |
} |
|
217 |
|
|
218 |
/** |
|
219 |
* Devuelve un ArrayList con todos los items |
|
220 |
* @return |
|
221 |
*/ |
|
222 |
public ArrayList getItems() { |
|
223 |
return items; |
|
224 |
} |
|
225 |
|
|
226 |
private Graphics2D getWidgetGraphics() { |
|
227 |
getWidgetImage(); |
|
228 |
return widgetGraphics; |
|
229 |
} |
|
230 |
|
|
231 |
private Image getWidgetImage() { |
|
232 |
int width2 = getVisibleRect().width; |
|
233 |
int height2 = getVisibleRect().height; |
|
234 |
if (width2 <= 0) |
|
235 |
width2 = 1; |
|
236 |
if (height2 <= 0) |
|
237 |
height2=1; |
|
238 |
|
|
239 |
if ((width != width2) || (height != height2)) { |
|
240 |
image = createImage(width2, height2); |
|
241 |
if (image == null) |
|
242 |
return null; |
|
243 |
widgetGraphics = (Graphics2D) image.getGraphics(); |
|
244 |
} |
|
245 |
|
|
246 |
width = width2; |
|
247 |
height = height2; |
|
248 |
return image; |
|
249 |
} |
|
250 |
|
|
251 |
/** |
|
252 |
* Redibujar el componente en el graphics temporal |
|
253 |
*/ |
|
254 |
private void redrawBuffer() { |
|
255 |
getWidgetGraphics().translate(-getVisibleRect().x, -getVisibleRect().y); |
|
256 |
getWidgetGraphics().setColor(Color.white); |
|
257 |
getWidgetGraphics().fillRect(getVisibleRect().x, getVisibleRect().y, getVisibleRect().width, getVisibleRect().height); |
|
258 |
|
|
259 |
((IListViewPainter) painters.get(view)).paint((Graphics2D) getWidgetGraphics(), getVisibleRect()); |
|
260 |
getWidgetGraphics().translate(getVisibleRect().x, getVisibleRect().y); |
|
261 |
} |
|
262 |
|
|
263 |
public void paint(Graphics g) { |
|
264 |
|
|
265 |
redrawBuffer(); |
|
266 |
|
|
267 |
if (image != null) |
|
268 |
g.drawImage(image, getVisibleRect().x, getVisibleRect().y, this); |
|
269 |
|
|
270 |
Dimension size = getPreferredSize(); |
|
271 |
Dimension aux = ((IListViewPainter) painters.get(view)).getPreferredSize(); |
|
272 |
if (!size.equals(aux)) { |
|
273 |
setPreferredSize(aux); |
|
274 |
setSize(aux); |
|
275 |
} |
|
276 |
} |
|
277 |
|
|
278 |
public boolean isMultiSelect() { |
|
279 |
return multiSelect; |
|
280 |
} |
|
281 |
|
|
282 |
public void setMultiSelect(boolean multiSelect) { |
|
283 |
if (multiSelect == false) { |
|
284 |
for (int i = 0; i<items.size(); i++) |
|
285 |
((ListViewItem) items.get(i)).setSelected(false); |
|
286 |
if ((lastSelected != -1) && (lastSelected < items.size())) |
|
287 |
((ListViewItem) items.get(lastSelected)).setSelected(true); |
|
288 |
} |
|
289 |
|
|
290 |
this.multiSelect = multiSelect; |
|
291 |
repaint(); |
|
292 |
} |
|
293 |
|
|
294 |
private int getItem(int x, int y) { |
|
295 |
Point point = new Point(x, y); |
|
296 |
Rectangle rectangle = null; |
|
297 |
for (int i = 0; i < items.size(); i++) { |
|
298 |
rectangle = ((ListViewItem) items.get(i)).getItemRectangle(); |
|
299 |
if ((rectangle != null) && (rectangle.getBounds().contains(point))) |
|
300 |
return i; |
|
301 |
} |
|
302 |
|
|
303 |
return -1; |
|
304 |
} |
|
305 |
|
|
306 |
/* |
|
307 |
* (non-Javadoc) |
|
308 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) |
|
309 |
*/ |
|
310 |
public void mousePressed(MouseEvent e) { |
|
311 |
requestFocus(); |
|
312 |
|
|
313 |
try { |
|
314 |
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK) |
|
315 |
return; |
|
316 |
|
|
317 |
cursorPos = getItem(e.getX(), e.getY()); |
|
318 |
if (cursorPos == -1) |
|
319 |
return; |
|
320 |
|
|
321 |
if (isMultiSelect()) { |
|
322 |
if ((e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) { |
|
323 |
int pos1 = cursorPos; |
|
324 |
int pos2 = lastSelected; |
|
325 |
if (pos2 < pos1) { |
|
326 |
pos1 = lastSelected; |
|
327 |
pos2 = cursorPos; |
|
328 |
} |
|
329 |
|
|
330 |
if ((e.getModifiers() & InputEvent.CTRL_MASK) != InputEvent.CTRL_MASK) |
|
331 |
for (int i = 0; i < items.size(); i++) |
|
332 |
((ListViewItem) items.get(i)).setSelected(false); |
|
333 |
|
|
334 |
for (int i = pos1; i <= pos2; i++) |
|
335 |
((ListViewItem) items.get(i)).setSelected(true); |
|
336 |
return; |
|
337 |
} |
|
338 |
|
|
339 |
lastSelected = cursorPos; |
|
340 |
|
|
341 |
if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) { |
|
342 |
((ListViewItem) items.get(cursorPos)).setSelected(!((ListViewItem) items.get(cursorPos)).isSelected()); |
|
343 |
return; |
|
344 |
} |
|
345 |
|
|
346 |
for (int i = 0; i < items.size(); i++) |
|
347 |
((ListViewItem) items.get(i)).setSelected(false); |
|
348 |
|
|
349 |
((ListViewItem) items.get(cursorPos)).setSelected(true); |
|
350 |
} else { |
|
351 |
boolean selected = true; |
|
352 |
|
|
353 |
lastSelected = cursorPos; |
|
354 |
|
|
355 |
if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) |
|
356 |
selected = !((ListViewItem) items.get(cursorPos)).isSelected(); |
|
357 |
|
|
358 |
for (int i = 0; i < items.size(); i++) |
|
359 |
((ListViewItem) items.get(i)).setSelected(false); |
|
360 |
|
|
361 |
((ListViewItem) items.get(cursorPos)).setSelected(selected); |
|
362 |
} |
|
363 |
} finally { |
|
364 |
repaint(); |
|
365 |
} |
|
366 |
} |
|
367 |
|
|
368 |
/** |
|
369 |
* Establece que un item debe estar visible en la vista |
|
370 |
* @param pos |
|
371 |
*/ |
|
372 |
private void viewItem(int pos) { |
|
373 |
if (getParent() instanceof JViewport) { |
|
374 |
JViewport jViewport = (JViewport) getParent(); |
|
375 |
|
|
376 |
if (jViewport.getParent() instanceof JScrollPane) { |
|
377 |
ListViewItem lvi = ((ListViewItem) items.get(pos)); |
|
378 |
Rectangle rectangle = lvi.getItemRectangle(); |
|
379 |
if (rectangle == null) |
|
380 |
return; |
|
381 |
rectangle.setLocation((int) rectangle.getX() - getVisibleRect().x, (int) rectangle.getY() - getVisibleRect().y); |
|
382 |
jViewport.scrollRectToVisible(rectangle); |
|
383 |
} |
|
384 |
} |
|
385 |
} |
|
386 |
|
|
387 |
public void mouseDragged(MouseEvent e) { |
|
388 |
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK) |
|
389 |
return; |
|
390 |
|
|
391 |
if (isMultiSelect()) { |
|
392 |
if ((e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) |
|
393 |
return; |
|
394 |
if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) |
|
395 |
return; |
|
396 |
} |
|
397 |
|
|
398 |
int itemSelected = getItem(e.getX(), e.getY()); |
|
399 |
|
|
400 |
if (itemSelected == -1) |
|
401 |
return; |
|
402 |
|
|
403 |
lastSelected = itemSelected; |
|
404 |
cursorPos = itemSelected; |
|
405 |
|
|
406 |
for (int i = 0; i<items.size(); i++) |
|
407 |
((ListViewItem) items.get(i)).setSelected(false); |
|
408 |
|
|
409 |
((ListViewItem) items.get(itemSelected)).setSelected(true); |
|
410 |
|
|
411 |
repaint(); |
|
412 |
|
|
413 |
viewItem(itemSelected); |
|
414 |
} |
|
415 |
|
|
416 |
private JPopupMenu getPopupMenu() { |
|
417 |
if (jPopupMenu == null) { |
|
418 |
jPopupMenu = new JPopupMenu(); |
|
419 |
getJMenu().setText("View"); |
|
420 |
|
|
421 |
jPopupMenu.add(getJMenu()); |
|
422 |
} |
|
423 |
return jPopupMenu; |
|
424 |
} |
|
425 |
|
|
426 |
public void mouseReleased(MouseEvent e) { |
|
427 |
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) |
|
428 |
getPopupMenu().show (this, e.getX (), e.getY () ); |
|
429 |
|
|
430 |
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { |
|
431 |
fireSelectionValueChanged(1, 1, false); |
|
432 |
} |
|
433 |
} |
|
434 |
|
|
435 |
public void mouseMoved(MouseEvent e) { |
|
436 |
int itemSelected = getItem(e.getX(), e.getY()); |
|
437 |
|
|
438 |
if (itemSelected == -1) { |
|
439 |
setToolTipText(null); |
|
440 |
return; |
|
441 |
} |
|
442 |
if (((ListViewItem) items.get(itemSelected)).isShowTooltip()) |
|
443 |
setToolTipText(((ListViewItem) items.get(itemSelected)).getName()); |
|
444 |
else |
|
445 |
setToolTipText(null); |
|
446 |
} |
|
447 |
|
|
448 |
private ButtonGroup getButtonGroup() { |
|
449 |
if (buttonGroup == null) |
|
450 |
buttonGroup = new ButtonGroup(); |
|
451 |
return buttonGroup; |
|
452 |
} |
|
453 |
|
|
454 |
private JMenu getJMenu() { |
|
455 |
if (jMenu == null) |
|
456 |
jMenu = new JMenu(); |
|
457 |
return jMenu; |
|
458 |
} |
|
459 |
|
|
460 |
/* |
|
461 |
* (non-Javadoc) |
|
462 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) |
|
463 |
*/ |
|
464 |
public void actionPerformed(ActionEvent e) { |
|
465 |
int pos = paintersMenu.indexOf(e.getSource()); |
|
466 |
view = pos; |
|
467 |
repaint(); |
|
468 |
} |
|
469 |
|
|
470 |
/** |
|
471 |
* Returns an array of the values for the selected cells. The returned values |
|
472 |
* are sorted in increasing index order. |
|
473 |
* |
|
474 |
* @return the selected values or an empty list if nothing is selected |
|
475 |
*/ |
|
476 |
public ListViewItem[] getSelectedValues() { |
|
477 |
int cont = 0; |
|
478 |
for (int i = 0; i < items.size(); i++) { |
|
479 |
if (((ListViewItem) items.get(i)).isSelected()) |
|
480 |
cont++; |
|
481 |
} |
|
482 |
ListViewItem[] values = new ListViewItem[cont]; |
|
483 |
cont = 0; |
|
484 |
for (int i = 0; i < items.size(); i++) { |
|
485 |
if (((ListViewItem) items.get(i)).isSelected()) { |
|
486 |
values[cont] = (ListViewItem) items.get(i); |
|
487 |
cont++; |
|
488 |
} |
|
489 |
} |
|
490 |
|
|
491 |
return values; |
|
492 |
} |
|
493 |
|
|
494 |
/** |
|
495 |
* Returns the first selected index; returns -1 if there is no selected item. |
|
496 |
* |
|
497 |
* @return the value of <code>getMinSelectionIndex</code> |
|
498 |
*/ |
|
499 |
public int getSelectedIndex() { |
|
500 |
for (int i = 0; i < items.size(); i++) { |
|
501 |
if (((ListViewItem) items.get(i)).isSelected()) |
|
502 |
return i; |
|
503 |
} |
|
504 |
return -1; |
|
505 |
} |
|
506 |
|
|
507 |
|
|
508 |
/** |
|
509 |
* Select the index value |
|
510 |
* |
|
511 |
* @return the value of <code>getMinSelectionIndex</code> |
|
512 |
*/ |
|
513 |
public void setSelectedIndex(int value) { |
|
514 |
for (int i = 0; i < items.size(); i++) { |
|
515 |
((ListViewItem) items.get(i)).setSelected(i == value); |
|
516 |
} |
|
517 |
} |
|
518 |
|
|
519 |
/** |
|
520 |
* Returns the first selected value, or <code>null</code> if the selection |
|
521 |
* is empty. |
|
522 |
* |
|
523 |
* @return the first selected value |
|
524 |
*/ |
|
525 |
public ListViewItem getSelectedValue() { |
|
526 |
for (int i = 0; i < items.size(); i++) { |
|
527 |
if (((ListViewItem) items.get(i)).isSelected()) |
|
528 |
return (ListViewItem) items.get(i); |
|
529 |
} |
|
530 |
return null; |
|
531 |
} |
|
532 |
|
|
533 |
/** |
|
534 |
* Returns an array of all of the selected indices in increasing order. |
|
535 |
* |
|
536 |
* @return all of the selected indices, in increasing order |
|
537 |
*/ |
|
538 |
public int[] getSelectedIndices() { |
|
539 |
int cont = 0; |
|
540 |
for (int i = 0; i < items.size(); i++) { |
|
541 |
if (((ListViewItem) items.get(i)).isSelected()) |
|
542 |
cont++; |
|
543 |
} |
|
544 |
int[] values = new int[cont]; |
|
545 |
cont = 0; |
|
546 |
for (int i = 0; i < items.size(); i++) { |
|
547 |
if (((ListViewItem) items.get(i)).isSelected()) { |
|
548 |
values[cont] = i; |
|
549 |
cont++; |
|
550 |
} |
|
551 |
} |
|
552 |
|
|
553 |
return values; |
|
554 |
} |
|
555 |
|
|
556 |
/** |
|
557 |
* A?adir un listener a la lista de eventos |
|
558 |
* @param listener |
|
559 |
*/ |
|
560 |
public void addListSelectionListener(ListSelectionListener listener) { |
|
561 |
if (!actionCommandListeners.contains(listener)) |
|
562 |
actionCommandListeners.add(listener); |
|
563 |
} |
|
564 |
|
|
565 |
/** |
|
566 |
* Borrar un listener de la lista de eventos |
|
567 |
* @param listener |
|
568 |
*/ |
|
569 |
public void removeListSelectionListener(ListSelectionListener listener) { |
|
570 |
actionCommandListeners.remove(listener); |
|
571 |
} |
|
572 |
|
|
573 |
/** |
|
574 |
* Invocar a los eventos asociados al componente |
|
575 |
*/ |
|
576 |
private void fireSelectionValueChanged(int firstIndex, int lastIndex, boolean isAdjusting) { |
|
577 |
Iterator acIterator = actionCommandListeners.iterator(); |
|
578 |
ListSelectionEvent e = null; |
|
579 |
while (acIterator.hasNext()) { |
|
580 |
ListSelectionListener listener = (ListSelectionListener) acIterator.next(); |
|
581 |
if (e == null) |
|
582 |
e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting); |
|
583 |
listener.valueChanged(e); |
|
584 |
} |
|
585 |
} |
|
586 |
|
|
587 |
public void renameItem(int item) { |
|
588 |
if (!isEditable()) |
|
589 |
return; |
|
590 |
|
|
591 |
if ((item >= 0) && (item < items.size())) { |
|
592 |
if (((ListViewItem) items.get(item)).isSelected()) { |
|
593 |
Rectangle rectangle = ((ListViewItem) items.get(item)).getNameRectangle(); |
|
594 |
|
|
595 |
if (rectangle != null) { |
|
596 |
itemEdited = item; |
|
597 |
((ListViewItem) items.get(itemEdited)).setSelected(false); |
|
598 |
repaint(); |
|
599 |
this.setLayout(null); |
|
600 |
getJRenameEdit().setText(((ListViewItem) items.get(item)).getName()); |
|
601 |
this.add(getJRenameEdit()); |
|
602 |
getJRenameEdit().setBounds(rectangle); |
|
603 |
getJRenameEdit().addFocusListener(this); |
|
604 |
getJRenameEdit().addKeyListener(this); |
|
605 |
getJRenameEdit().requestFocus(); |
|
606 |
getJRenameEdit().setSelectionStart(0); |
|
607 |
getJRenameEdit().setSelectionEnd(getJRenameEdit().getText().length()); |
|
608 |
} |
|
609 |
} |
|
610 |
} |
|
611 |
} |
|
612 |
|
|
613 |
public JTextField getJRenameEdit() { |
|
614 |
if (jRenameEdit == null) { |
|
615 |
jRenameEdit = new JTextField(); |
|
616 |
} |
|
617 |
return jRenameEdit; |
|
618 |
} |
|
619 |
|
|
620 |
public void changeName(String newName, int pos) { |
|
621 |
if (newName.length() == 0) |
|
622 |
return; |
|
623 |
String newNameAux = newName; |
|
624 |
boolean isItem; |
|
625 |
int newNumber = 0; |
|
626 |
do { |
|
627 |
isItem = false; |
|
628 |
for (int i = 0; i < items.size(); i++) { |
|
629 |
if ((i != pos) && (((ListViewItem) items.get(i)).getName().equals(newNameAux))) { |
|
630 |
isItem = true; |
|
631 |
newNumber++; |
|
632 |
newNameAux = newName + "_" + newNumber; |
|
633 |
break; |
|
634 |
} |
|
635 |
} |
|
636 |
} while (isItem); |
|
637 |
((ListViewItem) items.get(pos)).setName(newNameAux); |
|
638 |
} |
|
639 |
|
|
640 |
public void closeRenameEdit() { |
|
641 |
if (jRenameEdit == null) |
|
642 |
return; |
|
643 |
|
|
644 |
if (itemEdited != -1) { |
|
645 |
changeName(getJRenameEdit().getText(), itemEdited); |
|
646 |
|
|
647 |
((ListViewItem) items.get(cursorPos)).setSelected(true); |
|
648 |
itemEdited = -1; |
|
649 |
repaint(); |
|
650 |
} |
|
651 |
this.remove(getJRenameEdit()); |
|
652 |
jRenameEdit = null; |
|
653 |
this.requestFocus(); |
|
654 |
} |
|
655 |
|
|
656 |
public void keyPressed(KeyEvent e) { |
|
657 |
if (e.getSource() == getJRenameEdit()) { |
|
658 |
switch (e.getKeyCode()) { |
|
659 |
case KeyEvent.VK_ESCAPE: |
|
660 |
getJRenameEdit().setText(((ListViewItem) items.get(itemEdited)).getName()); |
|
661 |
closeRenameEdit(); |
|
662 |
break; |
|
663 |
case KeyEvent.VK_ENTER: |
|
664 |
closeRenameEdit(); |
|
665 |
break; |
|
666 |
} |
|
667 |
|
|
668 |
return; |
|
669 |
} |
|
670 |
if (e.getSource() == this) { |
|
671 |
if (e.getKeyCode() == KeyEvent.VK_F2) { |
|
672 |
renameItem(cursorPos); |
|
673 |
} |
|
674 |
} |
|
675 |
} |
|
676 |
|
|
677 |
public void focusLost(FocusEvent e) { |
|
678 |
closeRenameEdit(); |
|
679 |
} |
|
680 |
|
|
681 |
public void mouseClicked(MouseEvent e) { |
|
682 |
if (e.getSource() == this) |
|
683 |
// Si es doble click y hay alg?n elemento seleccionado en la lista lo eliminamos |
|
684 |
if (e.getClickCount() == 2) { |
|
685 |
renameItem(cursorPos); |
|
686 |
} |
|
687 |
} |
|
688 |
|
|
689 |
/** |
|
690 |
* Devuelve si se puede cambiar el nombre de los items |
|
691 |
* @return |
|
692 |
*/ |
|
693 |
public boolean isEditable() { |
|
694 |
return editable; |
|
695 |
} |
|
696 |
|
|
697 |
/** |
|
698 |
* Define si se puede cambiar el nombre de los items |
|
699 |
* @param editable |
|
700 |
*/ |
|
701 |
public void setEditable(boolean editable) { |
|
702 |
this.editable = editable; |
|
703 |
} |
|
704 |
|
|
705 |
public void mouseEntered(MouseEvent e) {} |
|
706 |
public void mouseExited(MouseEvent e) {} |
|
707 |
public void keyReleased(KeyEvent e) {} |
|
708 |
public void keyTyped(KeyEvent e) {} |
|
709 |
public void focusGained(FocusEvent e) {} |
|
710 |
} |
|
0 | 711 |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/listview/IIconPaint.java | ||
---|---|---|
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.listview; |
|
20 |
|
|
21 |
import java.awt.Graphics2D; |
|
22 |
/** |
|
23 |
* |
|
24 |
* @version 29/06/2007 |
|
25 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es) |
|
26 |
*/ |
|
27 |
public interface IIconPaint { |
|
28 |
public void paint(Graphics2D g, boolean isSelected); |
|
29 |
} |
|
0 | 30 |
trunk/libraries/libUIComponent/src-test-ui/org/gvsig/gui/beans/listview/RampPainter.java | ||
---|---|---|
24 | 24 |
import java.awt.Rectangle; |
25 | 25 |
import java.awt.Shape; |
26 | 26 |
|
27 |
import org.gvsig.gui.beans.graphic.listview.IIconPaint;
|
|
27 |
import org.gvsig.gui.beans.listview.IIconPaint; |
|
28 | 28 |
/** |
29 | 29 |
* |
30 | 30 |
* @version 29/06/2007 |
trunk/libraries/libUIComponent/src-test-ui/org/gvsig/gui/beans/listview/TestListView.java | ||
---|---|---|
30 | 30 |
import javax.swing.JPanel; |
31 | 31 |
import javax.swing.JScrollPane; |
32 | 32 |
|
33 |
import org.gvsig.gui.beans.graphic.listview.ListViewComponent;
|
|
34 |
import org.gvsig.gui.beans.graphic.listview.ListViewItem;
|
|
33 |
import org.gvsig.gui.beans.listview.ListViewComponent; |
|
34 |
import org.gvsig.gui.beans.listview.ListViewItem; |
|
35 | 35 |
/** |
36 |
* |
|
37 | 36 |
* @version 28/06/2007 |
38 | 37 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es) |
39 | 38 |
*/ |
... | ... | |
41 | 40 |
private JFrame jFrame = null; |
42 | 41 |
private JScrollPane jScrollPane = null; |
43 | 42 |
private ListViewComponent listViewComponent = null; |
44 |
int items = 0; |
|
45 | 43 |
JButton botonAdd = null; |
46 | 44 |
JButton botonDel = null; |
47 | 45 |
JCheckBox multiselect = null; |
... | ... | |
58 | 56 |
listViewComponent.setEditable(true); |
59 | 57 |
jScrollPane.setViewportView(listViewComponent); |
60 | 58 |
|
61 |
for (int i=0; i<100; i++) { |
|
62 |
items++; |
|
63 |
ListViewItem item = new ListViewItem(new RampPainter(), "Prueba " + items); |
|
64 |
listViewComponent.addItem(item); |
|
59 |
for (int i=0; i<20000; i++) { |
|
60 |
ListViewItem item = new ListViewItem(new RampPainter(), "Prueba-" + i); |
|
61 |
listViewComponent.addItem(item, true); |
|
65 | 62 |
} |
66 |
ListViewItem item = new ListViewItem(new RampPainter(), "En un lugar de la mancha de cuyo nombre no quiero acordarme..."); |
|
67 |
listViewComponent.addItem(item); |
|
68 | 63 |
|
69 | 64 |
JPanel jpane = new JPanel(); |
70 | 65 |
jpane.setLayout(new BorderLayout()); |
... | ... | |
120 | 115 |
*/ |
121 | 116 |
public void actionPerformed(ActionEvent e) { |
122 | 117 |
if (e.getSource() == botonAdd) { |
123 |
items++; |
|
124 |
ListViewItem item = new ListViewItem(new RampPainter(), "Prueba asfg asfgasfg asfg sdfgsdfg " + items); |
|
118 |
ListViewItem item = new ListViewItem(new RampPainter(), "Prueba"); |
|
125 | 119 |
listViewComponent.addItem(item); |
126 | 120 |
return; |
127 | 121 |
} |
Also available in: Unified diff