/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HIDB2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui;
import hidb2.gui.ressources.RscMan;
import java.io.File;
import java.util.logging.Logger;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
/**
* Represents a cell of the icon file browser.
*
*/
public class CanvasCell
{
final static Logger log = Logger.getLogger("hidb2.gui");
/** Indicates the used image comes from the 'standard' images bank and shall not be disposed */
boolean isDefault = false;
Image icon;
/** Absolute X coordinate of the icon (within the cell) */
private int x0;
/** Absolute Y coordinate of the icon (within the cell) */
private int y0;
/** Enclosing Rectangle of the Cell */
private Rectangle rCell;
String fn;
int fnPixLen = 0;
File fic;
FileBrowserParam _fbprm;
boolean isSelected = false;
public CanvasCell(File f, FileBrowserParam fbprm)
{
fic = f;
_fbprm = fbprm;
rCell = new Rectangle(0, 0, fbprm.cellWidth, fbprm.cellHeight);
}
public void computeIcon(Device dev)
{
ImageData id = null;
int nWidth;
int nHeight;
try
{
String afn = fic.getAbsolutePath();
id = new ImageData(afn);
int pos = afn.lastIndexOf(File.separatorChar);
fn = (pos > 0) ? afn.substring(pos + 1) : afn;
}
catch (SWTException sex)
{
log.warning("Can not import file " + fn);
}
if (id == null)
{
icon = RscMan.getImage(RscMan.IN_BOSS);
isDefault = true;
}
else
{
if ((id.width > _fbprm.iconSize) || (id.height > _fbprm.iconSize))
{
// Downscaling is needed
if (id.width > id.height)
{
nWidth = _fbprm.iconSize;
nHeight = (_fbprm.iconSize * id.height) / id.width;
}
else
{
nHeight = _fbprm.iconSize;
nWidth = (_fbprm.iconSize * id.width) / id.height;
}
id = id.scaledTo(nWidth, nHeight);
}
else
{
nWidth = id.width;
nHeight = id.height;
}
icon = new Image(dev, id);
}
}
/**
* Return true if the given set of coordinate is strictly contained by the Cell.
* @param x
* @param y
* @return
*/
public boolean contains(int x, int y)
{
return rCell.contains(x, y); // (x > x0) && (y > y0) && (x < x0 + _fbprm.cellWidth) && (y < y0 + _fbprm.cellWidth);
}
/**
* Return true if the given set of coordinate is strictly contained by the Cell.
* @param x
* @param y
* @return
*/
public boolean intersects(Rectangle r)
{
return rCell.intersects(r);
}
public void setCoord(int xCell, int yCell)
{
Rectangle iconBounds = icon.getBounds();
rCell.x = xCell;
rCell.y = yCell;
x0 = xCell + _fbprm.marginLeft + (_fbprm.iconSize - iconBounds.width) / 2;
y0 = yCell + _fbprm.marginTop + (_fbprm.iconSize - iconBounds.height) / 2;
}
public void drawAt(GC gc, Point origin)
{
gc.drawImage(icon, x0 + origin.x, y0 + origin.y);
gc.drawString(fn, rCell.x + origin.x + 2, rCell.y + origin.y + _fbprm.iconSize + _fbprm.marginTop + 1);
if (isSelected)
{
gc.drawRoundRectangle(rCell.x + origin.x, rCell.y + origin.y, rCell.width, rCell.height, 8, 8);
}
}
}