View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui;
21  
22  import java.io.File;
23  
24  import javax.swing.Icon;
25  
26  import com.cosylab.gui.components.util.IconHelper;
27  
28  /**
29   * <code>ValueIconPair</code> combines a long value with a specifi icon.
30   * 
31   * @author Jaka Bobnar, Cosylab
32   *
33   */
34  public class ValueIconPair implements Comparable {
35  	
36  	private Icon icon;
37  	private Long value;
38  	private String iconName;
39  	
40  	/**
41  	 * Constructs a new ValueIconPair with the given value and icon.
42  	 * 
43  	 * @param value
44  	 * @param icon
45  	 */
46  	public ValueIconPair(long value, Icon icon) {
47  		this(new Long(value),icon);
48  	}
49  	
50  	/**
51  	 * Constructs a new ValueIconPair with the given value and icon.
52  	 * The icon name is in this case the icon.toString().
53  	 * 
54  	 * @param value
55  	 * @param icon
56  	 */
57  	public ValueIconPair(Long value, Icon icon) {
58  		this.icon = icon;
59  		this.value = value;
60  		this.iconName = icon.toString();
61  	}
62  	
63  	/**
64  	 * Constructs a new ValueIconPair with the given value and path to the icon in 
65  	 * the classpath. 
66  	 * 
67  	 * @param value
68  	 * @param iconName
69  	 */
70  	public ValueIconPair(Long value, String iconName) {
71  		this.value = value;
72  		this.icon = IconHelper.createIcon(iconName);
73  		this.iconName = iconName;
74  	}
75  	
76  	/**
77  	 * Constructs a new ValueIconPair with the given value and the File describing
78  	 * the image icon. The file can point to any location on the disk as long as it
79  	 * is accessible. The file does not need to be in the classpath. The name of the
80  	 * file is the absolute path of the icon file.
81  	 * 
82  	 * @param value
83  	 * @param iconFile
84  	 */
85  	public ValueIconPair(Long value, File iconFile) {
86  		this.value = value;
87  		this.icon = IconHelper.createIcon(iconFile);
88  		this.iconName = iconFile.getAbsolutePath();
89  	}
90  
91  	/**
92  	 * Returns the icon.
93  	 * 
94  	 * @return
95  	 */
96  	public Icon getIcon() {
97  		return icon;
98  	}
99  
100 	/**
101 	 * Returns the value.
102 	 * 
103 	 * @return
104 	 */
105 	public Long getValue() {
106 		return value;
107 	}
108 	
109 	/**
110 	 * Returns the icon name.
111 	 * 
112 	 * @return
113 	 */
114 	public String getIconName() {
115 		return iconName;
116 	}
117 	
118 	/*
119 	 * (non-Javadoc)
120 	 * @see java.lang.Object#equals(java.lang.Object)
121 	 */
122 	public boolean equals(Object o) {
123 		if (!(o instanceof ValueIconPair)) return false;
124 		ValueIconPair d = (ValueIconPair)o;
125 		if (value != null)
126 			return (value.equals(d.value) && d.icon.equals(icon));
127 		else 
128 			return (d.value == null && d.icon.equals(icon));
129 		
130 	}
131 	
132 	/*
133 	 * (non-Javadoc)
134 	 * @see java.lang.Object#toString()
135 	 */
136 	public String toString() {
137 		return value + "= " + iconName ;
138 	}
139 	
140 	/* (non-Javadoc)
141 	 * @see java.lang.Comparable#compareTo(T)
142 	 */
143 	public int compareTo(Object o) {
144 		if (o instanceof ValueIconPair) {
145 			ValueIconPair v= (ValueIconPair)o;
146 			return (int)(value-v.value);
147 		}
148 		return 0;
149 	}
150 }