Skip to main content
summaryrefslogtreecommitdiffstats
blob: e60a940de8b488ab2fca0ebde14e193ca6ad4b9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2011 Laurent CARON
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API
 *******************************************************************************/
package org.mihalis.opal.imageSelector;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.FilenameUtils;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.mihalis.opal.OpalItem;
import org.mihalis.opal.utils.SWTGraphicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Instances of this class represents items manipulated by the ImageSelector
 * widget.
 */
public class ISItem extends OpalItem implements Comparable<ISItem> {

	/** The z position. */
	private double zPosition;
	
	/** The upper left corner. */
	private Point upperLeftCorner;
	
	/** The lower right corner. */
	private Point lowerRightCorner;

	private Image swtImage;

	private URL url;

	/** The Constant LOGGER. */
	private static final Logger LOGGER = LoggerFactory.getLogger( ISItem.class );

	/**
	 * Constructor.
	 *
	 * @param fileName file name of the image that will be displayed
	 */
	public ISItem(final String fileName) {
		setImage(SWTGraphicUtil.createImageFromFile(fileName));
	}

	/**
	 * Constructor.
	 *
	 * @param title the title of the image
	 * @param fileName file name of the image that will be displayed
	 */
	public ISItem(final String title, final String fileName) {
		setImage(SWTGraphicUtil.createImageFromFile(fileName));
		setText(title);
	}

	/**
	 * Constructor.
	 *
	 * @param title the title of the image
	 * @param img image that will be displayed
	 */
	public ISItem(final String title, final Image img) {
		setImage(img);
		setText(title);
	}

	/**
	 * Constructor.
	 *
	 * @param imgFile
	 *            the img file
	 */
	public ISItem(final File imgFile) {
		try (InputStream is = imgFile.toURI().toURL().openStream()) {
			swtImage = new Image( Display.getCurrent(), is );
			setImage( swtImage );
			setText(FilenameUtils.removeExtension(imgFile.getName()));
			setUrl(imgFile.toURI().toURL());
		} catch (IOException e) {
			LOGGER.error(e.getLocalizedMessage());
		}

	}
	
	/**
	 * Dispose SWT image.
	 */
	public void dispose () {
		swtImage.dispose();
	}

	/**
	 * Gets the url.
	 *
	 * @return the url
	 */
	public URL getUrl() {
		return url;
	}

	/**
	 * Sets the url.
	 *
	 * @param url
	 *            the new url
	 */
	public void setUrl(URL url) {
		this.url = url;
	}

	/**
	 * Gets the z position.
	 *
	 * @return the zPosition
	 */
	double getzPosition() {
		return zPosition;
	}

	/**
	 * Setz position.
	 *
	 * @param zPosition the zPosition to set
	 * @return the checks if is item
	 */
	ISItem setzPosition(final double zPosition) {
		this.zPosition = zPosition;
		return this;
	}

	/**
	 * Gets the upper left corner.
	 *
	 * @return the upperLeftCorner
	 */
	Point getUpperLeftCorner() {
		return upperLeftCorner;
	}

	/**
	 * Sets the upper left corner.
	 *
	 * @param x the upperLeftCorner.x to set
	 * @param y the upperLeftCorner.y to set
	 */
	void setUpperLeftCorner(final int x, final int y) {
		upperLeftCorner = new Point(x, y);
	}

	/**
	 * Gets the lower right corner.
	 *
	 * @return the lowerRightCorner
	 */
	Point getLowerRightCorner() {
		return lowerRightCorner;
	}

	/**
	 * Sets the lower right corner.
	 *
	 * @param x the lowerRightCorner.x to set
	 * @param y the lowerRightCorner.y to set
	 */
	void setLowerRightCorner(final int x, final int y) {
		lowerRightCorner = new Point(x, y);
	}

	/**
	 * Reset corner to null.
	 */
	void resetCornerToNull() {
		upperLeftCorner = null;
		lowerRightCorner = null;

	}

	/**
	 * To string.
	 *
	 * @return the string
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "ISItem [getText()=" + getText() + "]";
	}

	/**
	 * Compare to.
	 *
	 * @param o the o
	 * @return the int
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(final ISItem o) {
//		return new Double(Math.abs(zPosition)).compareTo(Math.abs(o.getzPosition())) * -1;
		return this.getText().compareTo(o.getText());
	}

	/**
	 * Indicates whether some other {@code ISItem} is "equal" to this one. Two
	 * objects of type {@code ISItem} are considered "equal" if their attributes
	 * {@literal zPosition} are equal.
	 * 
	 * If the passed object is null or <b>not</b> of type {@code ISItem} the
	 * method returns {@code false}. Returns {@code true} if the result of
	 * method {@code compareTo()} is 0} .
	 * 
	 * This method is overridden so as to maintain the general contract for
	 * classes that implement the {@code Comparable} interface.
	 *
	 * @author gu
	 * @param o the reference object with which to compare (should be of type
	 *            ISItem).
	 * @return {@code true} if this {@code ISItem} is the same as the passed
	 *         object of type {@code ISItem}; {@code false} otherwise.
	 * @see java.lang.Object#equals(java.lang.Object)
	 * @see compareTo
	 */
	@Override
	public boolean equals ( Object o ) {
		if	( o == null )
			return	false;

		if	( o.getClass() != this.getClass() )
			return	false;

		return	compareTo( (ISItem) o ) == 0;
	}

	/**
	 * Returns a hash code value for the object, which is the result of passing
	 * attribute {@literal zPosition} to method {@code Math.ceil()}.
	 * 
	 * This method is overridden so as to maintain the general contract for
	 * classes that implement the {@code Comparable} interface.
	 *
	 * @author gu
	 * @return a hash code value for this object.
	 * @see java.lang.Math.ceil(double)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode () {
		return	(int) Math.ceil( zPosition );
	}

}

Back to the top