Revision 34761

View differences:

tags/org.gvsig.ui/libUIComponent-2.0.0/src/org/gvsig/gui/IColorTables.java
1
package org.gvsig.gui;
2

  
3
import java.awt.Color;
4
import java.awt.Graphics2D;
5
import java.awt.Rectangle;
6
import java.util.Iterator;
7

  
8

  
9
public interface IColorTables {
10

  
11
	public abstract void load(String palettesPath, boolean interpolateValues);
12

  
13
	public abstract IColorTablePaint get(int i);
14

  
15
	public abstract int size();
16

  
17
	public abstract Iterator iterator();
18

  
19
	public interface IColorTablePaint {
20

  
21
		/**
22
		 * Defines if the values are interpolated between them or not.
23
		 *
24
		 * @param value
25
		 */
26
		public abstract void setInterpolated(boolean value);
27

  
28
		public abstract Color[] getColors();
29

  
30
		/**
31
		 * Method to paint the color table
32
		 *
33
		 * @param g
34
		 * @param isSelected
35
		 */
36
		public abstract void paint(Graphics2D g, boolean isSelected,
37
				Rectangle bounds);
38

  
39
		public abstract String getTableName();
40

  
41
	}
42
}
43

  
0 44

  
tags/org.gvsig.ui/libUIComponent-2.0.0/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

  
tags/org.gvsig.ui/libUIComponent-2.0.0/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 implements Comparable<ListViewItem> {
31
	IIconPaint icon          = null;
32
	String     name          = null;
33
	Rectangle  nameRectangle = null;
34
	Rectangle  itemRectangle = new Rectangle();
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

  
137
	public int compareTo(ListViewItem listViewItem) {
138
		return getName().compareTo(listViewItem.getName());
139
	}
140
}
0 141

  
tags/org.gvsig.ui/libUIComponent-2.0.0/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.ListViewItem;
30
/**
31
 * Iconos de 82x28
32
 *
33
 * @version 28/06/2007
34
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
35
 */
36
public class SmallIcon implements IListViewPainter {
37
	ArrayList items = null;
38
	int iconsWidth = 40;
39
	int minIconsWidth = 40;
40
	int iconsHeight = 28;
41
	Dimension lastDimension = new Dimension(0, 0);
42
	int cols = 0;
43

  
44
	public SmallIcon(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 "SmallIcon";
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
		int aux = (int) Math.floor(visibleRect.getWidth() / (minIconsWidth + 2));
70
		if (aux > items.size())
71
			aux = items.size();
72
		iconsWidth = (int) (Math.floor(visibleRect.getWidth() / aux) - 2);
73

  
74
		int height2 = 0;
75

  
76
		int posX = 0;
77
		int posY = 0;
78
		cols = 0;
79
		for (int i = 0; i < items.size(); i++) {
80
			// Evito que se pueda editar el nombre
81
			((ListViewItem) items.get(i)).setNameRectangle(null);
82

  
83
			((ListViewItem) items.get(i)).setShowTooltip(true);
84
			if (posX != 0) {
85
				if (((posX + 1) * (iconsWidth + 2)) > visibleRect.getWidth()) {
86
					posX = 0;
87
					posY++;
88
				}
89
			}
90

  
91
			((ListViewItem) items.get(i)).getItemRectangle().setBounds(posX * (iconsWidth + 2), (posY * (iconsHeight + 2)), iconsWidth + 2, iconsHeight + 2);
92
			if (((ListViewItem) items.get(i)).getItemRectangle().intersects(visibleRect)) {
93
				if (((ListViewItem) items.get(i)).isSelected()) {
94
					g.setColor(new Color(49, 106, 197));
95
					g.fillRect(posX * (iconsWidth + 2), posY * (iconsHeight + 2), iconsWidth + 2, iconsHeight + 2);
96
				}
97

  
98
				Shape clip = g.getClip();
99
				g.translate(posX * (iconsWidth + 2) + 1, (posY * (iconsHeight + 2)) + 1);
100
				g.setClip(0, 0, iconsWidth, iconsHeight);
101

  
102
				if (((ListViewItem) items.get(i)).getIcon() != null)
103
					((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected());
104

  
105
				g.setClip(clip);
106
				g.translate(- (posX * (iconsWidth + 2) + 1), - ((posY * (iconsHeight + 2)) + 1));
107
			}
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
}
0 121

  
tags/org.gvsig.ui/libUIComponent-2.0.0/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.ListViewItem;
30
/**
31
 * Iconos de 82x28
32
 *
33
 * @version 28/06/2007
34
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
35
 */
36
public class LargeIcon implements IListViewPainter {
37
	ArrayList items = null;
38
	int iconsWidth = 82;
39
	int minIconsWidth = 82;
40
	int iconsHeight = 28;
41
	Dimension lastDimension = new Dimension(0, 0);
42
	int cols = 0;
43

  
44
	public LargeIcon(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 "LargeIcon";
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
		int aux = (int) Math.floor(visibleRect.getWidth() / (minIconsWidth + 2));
70
		if (aux > items.size())
71
			aux = items.size();
72
		iconsWidth = (int) (Math.floor(visibleRect.getWidth() / aux) - 2);
73

  
74
		int height2 = 0;
75

  
76
		int posX = 0;
77
		int posY = 0;
78
		cols = 0;
79
		for (int i = 0; i < items.size(); i++) {
80
			// Evito que se pueda editar el nombre
81
			((ListViewItem) items.get(i)).setNameRectangle(null);
82

  
83
			((ListViewItem) items.get(i)).setShowTooltip(true);
84
			if (posX != 0) {
85
				if (((posX + 1) * (iconsWidth + 2)) > visibleRect.getWidth()) {
86
					posX = 0;
87
					posY++;
88
				}
89
			}
90

  
91
			((ListViewItem) items.get(i)).getItemRectangle().setBounds(posX * (iconsWidth + 2), (posY * (iconsHeight + 2)), iconsWidth + 2, iconsHeight + 2);
92
			if (((ListViewItem) items.get(i)).getItemRectangle().intersects(visibleRect)) {
93
				if (((ListViewItem) items.get(i)).isSelected()) {
94
					g.setColor(new Color(49, 106, 197));
95
					g.fillRect(posX * (iconsWidth + 2), posY * (iconsHeight + 2), iconsWidth + 2, iconsHeight + 2);
96
				}
97

  
98
				Shape clip = g.getClip();
99
				g.translate(posX * (iconsWidth + 2) + 1, (posY * (iconsHeight + 2)) + 1);
100
				g.setClip(0, 0, iconsWidth, iconsHeight);
101

  
102
				if (((ListViewItem) items.get(i)).getIcon() != null)
103
					((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected());
104

  
105
				g.setClip(clip);
106
				g.translate(-(posX * (iconsWidth + 2) + 1), -((posY * (iconsHeight + 2)) + 1));
107
			}
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
}
0 121

  
tags/org.gvsig.ui/libUIComponent-2.0.0/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.ListViewItem;
32
/**
33
 * @version 28/06/2007
34
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
35
 */
36
public class PaintList implements IListViewPainter {
37
	ArrayList items = null;
38
	int iconsWidth = 35;
39
	int minIconsWidth = 35;
40
	Dimension lastDimension = new Dimension(0, 0);
41

  
42
	public PaintList(ArrayList items) {
43
		this.items = items;
44
	}
45

  
46
	/*
47
	 * (non-Javadoc)
48
	 * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getName()
49
	 */
50
	public String getName() {
51
		return "List";
52
	}
53

  
54
	/*
55
	 * (non-Javadoc)
56
	 * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#getPreferredSize()
57
	 */
58
	public Dimension getPreferredSize() {
59
		return lastDimension;
60
	}
61

  
62
	/*
63
	 * (non-Javadoc)
64
	 * @see org.gvsig.gui.beans.graphic.listview.IListViewPainter#paint(java.awt.Graphics2D, int, int)
65
	 */
66
	public void paint(Graphics2D g, Rectangle visibleRect) {
67
		FontMetrics fm = g.getFontMetrics();
68

  
69
		int minAux = 0;
70
		for (int i = 0; i < items.size(); i++) {
71
			int auxWidth = g.getFontMetrics().stringWidth(((ListViewItem) items.get(i)).getName()) + 8;
72
			if (minAux < auxWidth)
73
				minAux = auxWidth;
74
		}
75
		minAux = visibleRect.width - minAux;
76
		if (minAux < minIconsWidth)
77
			minAux = minIconsWidth;
78
		iconsWidth = minAux;
79

  
80
		int height2 = 0;
81
		int width2 = 0;
82
		for (int i = 0; i < items.size(); i++) {
83
			((ListViewItem) items.get(i)).setNameRectangle(null);
84

  
85
			int auxWidth = g.getFontMetrics().stringWidth(((ListViewItem) items.get(i)).getName());
86
			if ((minIconsWidth + 3 + auxWidth - visibleRect.x) > visibleRect.width) {
87
				((ListViewItem) items.get(i)).setShowTooltip(true);
88
			} else {
89
				((ListViewItem) items.get(i)).setShowTooltip(false);
90
			}
91
			if (width2 < auxWidth)
92
				width2 = auxWidth;
93

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

  
96
			if (!((ListViewItem) items.get(i)).getItemRectangle().intersects(visibleRect))
97
				continue;
98

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

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

  
116
			Shape clip = g.getClip();
117
			g.translate(1, i * 17 + 1);
118
			g.setClip(0, 0, iconsWidth, 15);
119

  
120
			if (((ListViewItem) items.get(i)).getIcon() != null)
121
				((ListViewItem) items.get(i)).getIcon().paint(g, ((ListViewItem) items.get(i)).isSelected());
122

  
123
			g.setClip(clip);
124
			g.translate(-1, -(i * 17 + 1));
125
		}
126
		height2 = items.size() * 17;
127

  
128
		lastDimension = new Dimension(minIconsWidth + 3 + width2, height2);
129
	//lastDimension = new Dimension(0, height2);
130
	}
131
}
0 132

  
tags/org.gvsig.ui/libUIComponent-2.0.0/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.RenderingHints;
29
import java.awt.color.ColorSpace;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.awt.event.FocusEvent;
33
import java.awt.event.FocusListener;
34
import java.awt.event.InputEvent;
35
import java.awt.event.KeyEvent;
36
import java.awt.event.KeyListener;
37
import java.awt.event.MouseEvent;
38
import java.awt.event.MouseListener;
39
import java.awt.event.MouseMotionListener;
40
import java.awt.image.BufferedImage;
41
import java.awt.image.ColorConvertOp;
42
import java.util.ArrayList;
43
import java.util.Arrays;
44
import java.util.EventObject;
45
import java.util.Iterator;
46

  
47
import javax.swing.ButtonGroup;
48
import javax.swing.JComponent;
49
import javax.swing.JMenu;
50
import javax.swing.JPopupMenu;
51
import javax.swing.JRadioButtonMenuItem;
52
import javax.swing.JScrollPane;
53
import javax.swing.JTextField;
54
import javax.swing.JViewport;
55
import javax.swing.event.AncestorEvent;
56
import javax.swing.event.AncestorListener;
57

  
58
import org.gvsig.gui.beans.listview.painters.LargeIcon;
59
import org.gvsig.gui.beans.listview.painters.PaintList;
60
import org.gvsig.gui.beans.listview.painters.SmallIcon;
61
/**
62
 * Componente grafico para representar una lista de valores
63
 *
64
 * @version 28/06/2007
65
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
66
 */
67
public class ListViewComponent extends JComponent implements MouseListener, MouseMotionListener, ActionListener, KeyListener, FocusListener, AncestorListener {
68
	private static final long serialVersionUID = 6177600314634665863L;
69

  
70
	/**
71
	 * Lista de los tipos de vista existentes
72
	 */
73
	private ArrayList<IListViewPainter> painters = new ArrayList<IListViewPainter>();
74
	private ArrayList<JRadioButtonMenuItem> paintersMenu = new ArrayList<JRadioButtonMenuItem>();
75

  
76
	/**
77
	 * Lista de items
78
	 */
79
	private ArrayList<ListViewItem> items = new ArrayList<ListViewItem>();
80

  
81
	/**
82
	 * Selecciona el tipo de vista
83
	 */
84
	private int         view           = 0;
85

  
86
	/**
87
	 * Booleano para saber si se permite la multiselecci?n
88
	 */
89
	private boolean     multiSelect    = false;
90

  
91
	private Image       image          = null;
92
	private int         width          = 0;
93
	private int         height         = 0;
94
	private Graphics2D  widgetGraphics = null;
95
	private JMenu       jMenu          = null;
96
	private ButtonGroup buttonGroup    = null;
97
	private JPopupMenu  jPopupMenu     = null;
98

  
99
	private JTextField  jRenameEdit    = null;
100

  
101

  
102
	private int         itemEdited     = -1;
103
	private int         lastSelected   = -1;
104
	private int         cursorPos      = -1;
105

  
106

  
107
	private boolean     editable       = false;
108

  
109
	private ArrayList<ListViewListener> actionCommandListeners = new ArrayList<ListViewListener>();
110

  
111
	private ListViewItem  lastSelectedItem = null;
112

  
113
	/**
114
	 * Construye un <code>ListViewComponent</code>
115
	 *
116
	 */
117
	public ListViewComponent() {
118
		setFocusable(true);
119

  
120
		initialize();
121
	}
122

  
123
	/**
124
	 * Inicializa el <code>ListViewComponent</code>
125
	 */
126
	private void initialize() {
127
		addListViewPainter(new PaintList(items));
128
		addListViewPainter(new SmallIcon(items));
129
		addListViewPainter(new LargeIcon(items));
130

  
131
		addAncestorListener(this);
132

  
133
		addKeyListener(this);
134
		addMouseListener(this);
135
		addMouseMotionListener(this);
136
	}
137

  
138
	/**
139
	 * Obtiene que vista se esta usando en el componente
140
	 * @return
141
	 */
142
	public int getView() {
143
		return view;
144
	}
145

  
146
	/**
147
	 * Define que vista es la que se va a usar
148
	 * @param view
149
	 */
150
	public void setView(int view) {
151
		this.view = view;
152
	}
153

  
154
	/**
155
	 * Agrega una vista al componente
156
	 * @param item
157
	 */
158
	public void addListViewPainter(IListViewPainter item) {
159
		painters.add(item);
160

  
161
		JRadioButtonMenuItem jRadioButtonMenuItem = new JRadioButtonMenuItem();
162
		getButtonGroup().add(jRadioButtonMenuItem);
163

  
164
		jRadioButtonMenuItem.setText(item.getName());
165
		if (paintersMenu.size() == 0)
166
			jRadioButtonMenuItem.setSelected(true);
167
		getJMenu().add(jRadioButtonMenuItem);
168

  
169
		jRadioButtonMenuItem.addActionListener(this);
170

  
171
		paintersMenu.add(jRadioButtonMenuItem);
172
	}
173
	
174
	/**
175
	 * Sorts the specified array of objects into ascending order
176
	 */
177
	public void sort() {
178
		Object[] list = items.toArray();
179
		Arrays.sort(list);
180
		items.clear();
181
		for (int i = 0; i < list.length; i++) 
182
			items.add((ListViewItem)list[i]);
183
	}
184

  
185
	/**
186
	 * Agrega un item al componente
187
	 * @param item
188
	 */
189
	public void addItem(ListViewItem item) {
190
		addItem(item, false);
191
	}
192

  
193
	/**
194
	 * Agrega un item al componente, si acceptRepeatNames es false no se aceptaran
195
	 * nombres repetidos
196
	 * @param item
197
	 * @param acceptRepeatNames
198
	 */
199
	public void addItem(ListViewItem item, boolean acceptRepeatNames) {
200
		items.add(item);
201
		if (!acceptRepeatNames)
202
			changeName(item.getName(), items.size() - 1);
203

  
204
		viewItem(items.size() - 1);
205
	}
206

  
207
	/**
208
	 * Agrega el item en la posicion especificada de la lista.
209
	 * @param pos
210
	 * @param item
211
	 */
212
	public void addItem(int pos, ListViewItem item) {
213
		items.add(pos, item);
214
		changeName(item.getName(), pos);
215

  
216
		viewItem(pos);
217
	}
218

  
219
	/**
220
	 * Agrega un item al componente
221
	 * @param item
222
	 */
223
	public void removeItem(int index) {
224
		items.remove(index);
225
		repaint();
226
	}
227

  
228
	/**
229
	 * Borra todos los items seleccionados
230
	 */
231
	public void removeSelecteds() {
232
		for (int i = (items.size()-1); i>=0; i--)
233
			if (((ListViewItem) items.get(i)).isSelected())
234
				items.remove(i);
235

  
236
		repaint();
237
	}
238

  
239
	/**
240
	 * Devuelve un ArrayList con todos los items
241
	 * @return
242
	 */
243
	@SuppressWarnings("unchecked")
244
	public ArrayList getItems() {
245
		return items;
246
	}
247

  
248
	private Graphics2D getWidgetGraphics() {
249
		getWidgetImage();
250
		return widgetGraphics;
251
	}
252

  
253
	private Image getWidgetImage() {
254
		int width2 = getVisibleRect().width;
255
		int height2 = getVisibleRect().height;
256
		if (width2 <= 0)
257
			width2 = 1;
258
		if (height2 <= 0)
259
			height2=1;
260

  
261
		if ((width != width2) || (height != height2)) {
262
			image = createImage(width2, height2);
263
			if (image == null)
264
				return null;
265
			widgetGraphics = (Graphics2D) image.getGraphics();
266
		}
267

  
268
		width = width2;
269
		height = height2;
270
		return image;
271
	}
272

  
273
	/**
274
	 * Redibujar el componente en el graphics temporal
275
	 */
276
	private void redrawBuffer() {
277
		if (getWidgetGraphics() == null)
278
			return;
279

  
280
		/** desactivaci?n del anti-aliasing */
281
		getWidgetGraphics().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
282
		getWidgetGraphics().setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
283

  
284
		/** demanda de rendimiento r?pido */
285
		getWidgetGraphics().setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
286
		getWidgetGraphics().setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
287
		getWidgetGraphics().setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
288
		getWidgetGraphics().setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
289

  
290
		getWidgetGraphics().translate(-getVisibleRect().x, -getVisibleRect().y);
291
		getWidgetGraphics().setColor(Color.white);
292
		getWidgetGraphics().fillRect(getVisibleRect().x, getVisibleRect().y, getVisibleRect().width, getVisibleRect().height);
293

  
294
		((IListViewPainter) painters.get(view)).paint((Graphics2D) getWidgetGraphics(), getVisibleRect());
295
		getWidgetGraphics().translate(getVisibleRect().x, getVisibleRect().y);
296
	}
297

  
298
	public void paint(Graphics g) {
299
		redrawBuffer();
300

  
301
		if (image != null) {
302
			if (isEnabled()) {
303
				g.drawImage(image, getVisibleRect().x, getVisibleRect().y, this);
304
			} else {
305
				// Dibujar en escala de grises y aclarado para cuando esta inactivo
306
				BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
307

  
308
				Graphics big = bi.createGraphics();
309
				big.drawImage(image, 0, 0, this);
310

  
311
				ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
312
				colorConvert.filter(bi, bi);
313

  
314
				big.setColor(new Color(255, 255, 255, 164));
315
				big.fillRect(0, 0, width, height);
316

  
317
				g.drawImage(bi, getVisibleRect().x, getVisibleRect().y, this);
318
			}
319
		}
320

  
321

  
322
		Dimension size = getPreferredSize();
323
		Dimension aux = ((IListViewPainter) painters.get(view)).getPreferredSize();
324
		if (!size.equals(aux)) {
325
			setPreferredSize(aux);
326
			setSize(aux);
327
		}
328

  
329
		if (getParent() instanceof JViewport) {
330
			JViewport jViewport = (JViewport) getParent();
331
			if (jViewport.getParent() instanceof JScrollPane) {
332
				if (items.size() > 0)
333
					((JScrollPane) jViewport.getParent()).getVerticalScrollBar().setUnitIncrement(((ListViewItem) items.get(0)).getItemRectangle().height);
334
			}
335
		}
336
	}
337

  
338
	public boolean isMultiSelect() {
339
		return multiSelect;
340
	}
341

  
342
	public void setMultiSelect(boolean multiSelect) {
343
		if (multiSelect == false) {
344
			for (int i = 0; i<items.size(); i++)
345
				((ListViewItem) items.get(i)).setSelected(false);
346
			if ((lastSelected != -1) && (lastSelected < items.size()))
347
				((ListViewItem) items.get(lastSelected)).setSelected(true);
348
		}
349

  
350
		this.multiSelect = multiSelect;
351
		repaint();
352
	}
353

  
354
	private int getItem(int x, int y) {
355
		Point point = new Point(x, y);
356
		Rectangle rectangle = null;
357
		for (int i = 0; i < items.size(); i++) {
358
			rectangle = ((ListViewItem) items.get(i)).getItemRectangle();
359
			if ((rectangle != null) && (rectangle.getBounds().contains(point)))
360
				return i;
361
		}
362

  
363
		return -1;
364
	}
365

  
366
	/*
367
	 * (non-Javadoc)
368
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
369
	 */
370
	public void mousePressed(MouseEvent e) {
371
		if (!isEnabled())
372
			return;
373
		requestFocus();
374

  
375
		try {
376
			if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK)
377
				return;
378

  
379
			cursorPos = getItem(e.getX(), e.getY());
380
			viewItem(cursorPos);
381
			if (cursorPos == -1)
382
				return;
383

  
384
			if (isMultiSelect()) {
385
				if ((e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) {
386
					int pos1 = cursorPos;
387
					int pos2 = lastSelected;
388
					if (pos2 < pos1) {
389
						pos1 = lastSelected;
390
						pos2 = cursorPos;
391
					}
392

  
393
					if ((e.getModifiers() & InputEvent.CTRL_MASK) != InputEvent.CTRL_MASK)
394
						for (int i = 0; i < items.size(); i++)
395
							((ListViewItem) items.get(i)).setSelected(false);
396

  
397
					for (int i = pos1; i <= pos2; i++)
398
						((ListViewItem) items.get(i)).setSelected(true);
399
					return;
400
				}
401

  
402
				lastSelected = cursorPos;
403

  
404
				if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
405
					((ListViewItem) items.get(cursorPos)).setSelected(!((ListViewItem) items.get(cursorPos)).isSelected());
406
					return;
407
				}
408

  
409
				for (int i = 0; i < items.size(); i++)
410
					((ListViewItem) items.get(i)).setSelected(false);
411

  
412
				((ListViewItem) items.get(cursorPos)).setSelected(true);
413
			} else {
414
				boolean selected = true;
415

  
416
				lastSelected = cursorPos;
417

  
418
				if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK)
419
					selected = !((ListViewItem) items.get(cursorPos)).isSelected();
420

  
421
				for (int i = 0; i < items.size(); i++)
422
					((ListViewItem) items.get(i)).setSelected(false);
423

  
424
				((ListViewItem) items.get(cursorPos)).setSelected(selected);
425
			}
426
		} finally {
427
			repaint();
428
		}
429
	}
430

  
431
	/**
432
	 * Establece que un item debe estar visible en la vista
433
	 * @param pos
434
	 */
435
	private void viewItem(int pos) {
436
		if ((pos == -1) || (items.size()==0))
437
			return;
438
		redrawBuffer();
439
		Dimension aux = ((IListViewPainter) painters.get(view)).getPreferredSize();
440
		setPreferredSize(aux);
441
		setSize(aux);
442

  
443
		if (pos < 0)
444
			pos = 0;
445

  
446
		if (pos >= items.size())
447
			pos = items.size() - 1;
448

  
449
		if (getParent() instanceof JViewport) {
450
			JViewport jViewport = (JViewport) getParent();
451

  
452
			if (jViewport.getParent() instanceof JScrollPane) {
453
				ListViewItem lvi = ((ListViewItem) items.get(pos));
454
				Rectangle rectangle = (Rectangle) lvi.getItemRectangle().clone();
455
				if (rectangle == null)
456
					return;
457
				rectangle.setLocation((int) rectangle.getX() - getVisibleRect().x, (int) rectangle.getY() - getVisibleRect().y);
458
				jViewport.scrollRectToVisible(rectangle);
459
				((JScrollPane)jViewport.getParent()).getVerticalScrollBar().paintImmediately(((JScrollPane)jViewport.getParent()).getVerticalScrollBar().getVisibleRect());
460
			}
461
		}
462
	}
463

  
464
	public void mouseDragged(MouseEvent e) {
465
		if (!isEnabled())
466
			return;
467
		if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK)
468
			return;
469

  
470
		if (isMultiSelect()) {
471
			if ((e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK)
472
				return;
473
			if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK)
474
				return;
475
		}
476

  
477
		int itemSelected = getItem(e.getX(), e.getY());
478

  
479
		if (itemSelected == -1)
480
			return;
481

  
482
		lastSelected = itemSelected;
483
		cursorPos = itemSelected;
484

  
485
		for (int i = 0; i<items.size(); i++)
486
			((ListViewItem) items.get(i)).setSelected(false);
487

  
488
		((ListViewItem) items.get(itemSelected)).setSelected(true);
489

  
490
		repaint();
491

  
492
		viewItem(itemSelected);
493
	}
494

  
495
	private JPopupMenu getPopupMenu() {
496
		if (jPopupMenu == null) {
497
			jPopupMenu = new JPopupMenu();
498
			getJMenu().setText("View");
499

  
500
			jPopupMenu.add(getJMenu());
501
		}
502
		return jPopupMenu;
503
	}
504

  
505
	public void mouseReleased(MouseEvent e) {
506
		if (!isEnabled())
507
			return;
508
		if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
509
			getPopupMenu().show(this, e.getX (), e.getY () );
510

  
511
		if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
512
			fireSelectionValueChanged();
513
		}
514
	}
515

  
516
	public void mouseMoved(MouseEvent e) {
517
		if (!isEnabled())
518
			return;
519
		int itemSelected = getItem(e.getX(), e.getY());
520

  
521
		if (itemSelected == -1) {
522
			setToolTipText(null);
523
			return;
524
		}
525
		if (((ListViewItem) items.get(itemSelected)).isShowTooltip())
526
			setToolTipText(((ListViewItem) items.get(itemSelected)).getName());
527
		else
528
			setToolTipText(null);
529
	}
530

  
531
	private ButtonGroup getButtonGroup() {
532
		if (buttonGroup == null)
533
			buttonGroup = new ButtonGroup();
534
		return buttonGroup;
535
	}
536

  
537
	private JMenu getJMenu() {
538
		if (jMenu == null)
539
			jMenu = new JMenu();
540
		return jMenu;
541
	}
542

  
543
	/*
544
	 * (non-Javadoc)
545
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
546
	 */
547
	public void actionPerformed(ActionEvent e) {
548
		int pos = paintersMenu.indexOf(e.getSource());
549
		view = pos;
550
		viewItem(cursorPos);
551
	}
552

  
553
	/**
554
	 * Returns an array of the values for the selected cells. The returned values
555
	 * are sorted in increasing index order.
556
	 *
557
	 * @return the selected values or an empty list if nothing is selected
558
	 */
559
	public ListViewItem[] getSelectedValues() {
560
		int cont = 0;
561
		for (int i = 0; i < items.size(); i++) {
562
			if (((ListViewItem) items.get(i)).isSelected())
563
				cont++;
564
		}
565
		ListViewItem[] values = new ListViewItem[cont];
566
		cont = 0;
567
		for (int i = 0; i < items.size(); i++) {
568
			if (((ListViewItem) items.get(i)).isSelected()) {
569
				values[cont] = (ListViewItem) items.get(i);
570
				cont++;
571
			}
572
		}
573

  
574
		return values;
575
	}
576

  
577
	/**
578
	 * Returns the first selected index; returns -1 if there is no selected item.
579
	 *
580
	 * @return the value of <code>getMinSelectionIndex</code>
581
	 */
582
	public int getSelectedIndex() {
583
		for (int i = 0; i < items.size(); i++) {
584
			if (((ListViewItem) items.get(i)).isSelected())
585
				return i;
586
		}
587
		return -1;
588
	}
589

  
590

  
591
	/**
592
	 * Select the index value
593
	 *
594
	 * @return the value of <code>getMinSelectionIndex</code>
595
	 */
596
	public void setSelectedIndex(int value) {
597
		if (value < 0)
598
			value = 0;
599
		if (value >= items.size())
600
			value = items.size() - 1;
601
		for (int i = 0; i < items.size(); i++) {
602
			((ListViewItem) items.get(i)).setSelected(i == value);
603
		}
604
		lastSelectedItem = getSelectedValue();
605
		viewItem(value);
606
		update(getGraphics());
607
	}
608

  
609
	/**
610
	 * Returns the first selected value, or <code>null</code> if the selection
611
	 * is empty.
612
	 *
613
	 * @return the first selected value
614
	 */
615
	public ListViewItem getSelectedValue() {
616
		for (int i = 0; i < items.size(); i++) {
617
			if (((ListViewItem) items.get(i)).isSelected())
618
				return (ListViewItem) items.get(i);
619
		}
620
		return null;
621
	}
622

  
623
	/**
624
	 * Returns an array of all of the selected indices in increasing order.
625
	 *
626
	 * @return all of the selected indices, in increasing order
627
	 */
628
	public int[] getSelectedIndices() {
629
		int cont = 0;
630
		for (int i = 0; i < items.size(); i++) {
631
			if (((ListViewItem) items.get(i)).isSelected())
632
				cont++;
633
		}
634
		int[] values = new int[cont];
635
		cont = 0;
636
		for (int i = 0; i < items.size(); i++) {
637
			if (((ListViewItem) items.get(i)).isSelected()) {
638
				values[cont] = i;
639
				cont++;
640
			}
641
		}
642

  
643
		return values;
644
	}
645

  
646
	/**
647
	 * A?adir un listener a la lista de eventos
648
	 * @param listener
649
	 */
650
	public void addListSelectionListener(ListViewListener listener) {
651
		if (!actionCommandListeners.contains(listener))
652
			actionCommandListeners.add(listener);
653
	}
654

  
655
	/**
656
	 * Borrar un listener de la lista de eventos
657
	 * @param listener
658
	 */
659
	public void removeListSelectionListener(ListViewListener listener) {
660
		actionCommandListeners.remove(listener);
661
	}
662

  
663
	/**
664
	 * Invocar a los eventos asociados al componente
665
	 */
666
	private void fireSelectionValueChanged() {
667
		lastSelectedItem = getSelectedValue();
668
		Iterator<ListViewListener> acIterator = actionCommandListeners.iterator();
669
		EventObject e = null;
670
		while (acIterator.hasNext()) {
671
			ListViewListener listener = acIterator.next();
672
			if (e == null)
673
				e = new EventObject(this);
674
			listener.actionValueChanged(e);
675
		}
676
	}
677

  
678
	/**
679
	 * Invocar a los eventos asociados al componente
680
	 */
681
	private void fireItemNameChanged(String oldName, ListViewItem item) {
682
		Iterator<ListViewListener> acIterator = actionCommandListeners.iterator();
683
		EventObject e = null;
684
		while (acIterator.hasNext()) {
685
			ListViewListener listener = acIterator.next();
686
			if (e == null)
687
				e = new EventObject(this);
688
			listener.actionItemNameChanged(e, oldName, item);
689
		}
690
	}
691
	public void renameItem(int item) {
692
		if (!isEditable())
693
			return;
694

  
695
		if ((item >= 0) && (item < items.size())) {
696
			if (((ListViewItem) items.get(item)).isSelected()) {
697
				Rectangle rectangle = ((ListViewItem) items.get(item)).getNameRectangle();
698

  
699
				if (rectangle != null) {
700
					itemEdited = item;
701
					((ListViewItem) items.get(itemEdited)).setSelected(false);
702
					repaint();
703
					this.setLayout(null);
704
					getJRenameEdit().setText(((ListViewItem) items.get(item)).getName());
705
					this.add(getJRenameEdit());
706
					getJRenameEdit().setBounds(rectangle);
707
					getJRenameEdit().addFocusListener(this);
708
					getJRenameEdit().addKeyListener(this);
709
					getJRenameEdit().requestFocus();
710
					getJRenameEdit().setSelectionStart(0);
711
					getJRenameEdit().setSelectionEnd(getJRenameEdit().getText().length());
712
				}
713
			}
714
		}
715
	}
716

  
717
	public JTextField getJRenameEdit() {
718
		if (jRenameEdit == null) {
719
			jRenameEdit = new JTextField();
720
		}
721
		return jRenameEdit;
722
 	}
723

  
724
	public void changeName(String newName, int pos) {
725
		newName = newName.trim();
726
		if (newName.length() == 0)
727
			return;
728
		String newNameAux = newName;
729
		boolean isItem;
730
		int newNumber = 0;
731
		do {
732
			isItem = false;
733
			for (int i = 0; i < items.size(); i++) {
734
				if ((i != pos) && (((ListViewItem) items.get(i)).getName().equals(newNameAux))) {
735
					isItem = true;
736
					newNumber++;
737
					newNameAux = newName + "_" + newNumber;
738
					break;
739
				}
740
			}
741
		} while (isItem);
742
		((ListViewItem) items.get(pos)).setName(newNameAux);
743
	}
744

  
745
	public void closeRenameEdit() {
746
		if (jRenameEdit == null)
747
			return;
748

  
749
		if (itemEdited != -1) {
750
			String oldName = ((ListViewItem) items.get(itemEdited)).getName();
751

  
752
			changeName(getJRenameEdit().getText(), itemEdited);
753

  
754
			fireItemNameChanged(oldName, (ListViewItem) items.get(itemEdited));
755

  
756
			((ListViewItem) items.get(cursorPos)).setSelected(true);
757
			itemEdited = -1;
758
			repaint();
759
		}
760
		this.remove(getJRenameEdit());
761
		jRenameEdit = null;
762
		this.requestFocus();
763
	}
764

  
765
	/**
766
	 * Mueve el cursor hacia abajo
767
	 */
768
	private void moveDown() {
769
		int selItem = -1;
770

  
771
		for (int i = 0; i < items.size(); i++) {
772
			if (cursorPos == i)
773
				continue;
774
			if (((ListViewItem) items.get(i)).getItemRectangle().y >= (((ListViewItem) items.get(cursorPos)).getItemRectangle().y + ((ListViewItem) items.get(cursorPos)).getItemRectangle().height)) {
775
				if (((ListViewItem) items.get(i)).getItemRectangle().x == ((ListViewItem) items.get(cursorPos)).getItemRectangle().x) {
776
					selItem = i;
777
					break;
778
				}
779
			}
780
		}
781

  
782
		if (selItem != -1) {
783
			cursorPos = selItem;
784
			setSelectedIndex(selItem);
785
		}
786
	}
787

  
788
	/**
789
	 * Mueve el cursor hacia la izquierda
790
	 */
791
	private void moveLeft() {
792
		int selItem = -1;
793
		for (int i = items.size() - 1; i >= 0; i--) {
794
			if (cursorPos == i)
795
				continue;
796
			if ((((ListViewItem) items.get(i)).getItemRectangle().x + ((ListViewItem) items.get(i)).getItemRectangle().width) <= ((ListViewItem) items.get(cursorPos)).getItemRectangle().x) {
797
				if (((ListViewItem) items.get(i)).getItemRectangle().y == ((ListViewItem) items.get(cursorPos)).getItemRectangle().y) {
798
					selItem = i;
799
					break;
800
				}
801
			}
802
		}
803

  
804
		if (selItem != -1) {
805
			cursorPos = selItem;
806
			setSelectedIndex(selItem);
807
		}
808
	}
809

  
810
	/**
811
	 * Mueve el cursor hacia la derecha
812
	 */
813
	private void moveRight() {
814
		int selItem = -1;
815
		for (int i = 0; i < items.size(); i++) {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff