/**
* 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.util;
import hidb2.kern.Attribut;
import hidb2.kern.HIDBConst;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Shell;
/**
* Image viewer dialog
*
*/
public class DlgImageViewer extends Dialog implements HIDBConst
{
private Shell _dlg;
private ImageCanvas _imgArea;
private List<File> _lstFic = null;
private int _curIdx = -1;
/**
* Ugly preference setter to have an roughtly independant object.
* @param minZoom
* @param maxZoom
* @param DefaultWidth
* @param DefaultHeight
*/
public void setPref(int minZoom, int maxZoom, int DefaultWidth, int DefaultHeight)
{
_imgArea.setPref(minZoom, maxZoom, DefaultWidth, DefaultHeight);
}
/**
* Create a new dialog for image browsing.
*
* @param parent
*/
public DlgImageViewer(Shell parent)
{
super(parent, SWT.DIALOG_TRIM | SWT.RESIZE);
_dlg = new Shell(parent.getShell(), SWT.DIALOG_TRIM | SWT.RESIZE);
_dlg.setLayout(new GridLayout(1, false));
_imgArea = new ImageCanvas(_dlg, SWT.NONE);
GridData gd = new GridData();
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
gd.horizontalAlignment = SWT.FILL;
gd.verticalAlignment = SWT.FILL;
_imgArea.setLayoutData(gd);
_imgArea.addKeyListener(new KeyListener()
{
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
// System.out.println("KeyCode:" + e.keyCode);
if ((e.keyCode == SWT.ARROW_LEFT) || (e.keyCode == SWT.ARROW_RIGHT))
{
switch (e.keyCode)
{
case SWT.ARROW_LEFT:
prevImageInDir();
_imgArea.redraw();
break;
case SWT.ARROW_RIGHT:
nextImageInDir();
_imgArea.redraw();
break;
default:
break;
}
}
else
{
switch (e.character)
{
case SWT.ESC:
_dlg.close();
break;
case ' ':
nextImageInDir();
_imgArea.redraw();
break;
case '0':
_imgArea.setZoom(ImageCanvas.OPTIMAL_ZOOM);
break;
case '1':
_imgArea.setZoom(100);
break;
case '2':
_imgArea.setZoom(200);
break;
case '3':
_imgArea.setZoom(300);
break;
case '4':
_imgArea.setZoom(400);
break;
case '-':
_imgArea.setZoom(_imgArea.getZoom() - 10);
break;
case '+':
_imgArea.setZoom(_imgArea.getZoom() + 10);
break;
case 'f': // Full Screen switch
_dlg.setFullScreen(!_dlg.getFullScreen());
break;
default:
break;
}
}
}
});
_imgArea.addMenuItem("Next", new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent e)
{
nextImageInDir();
_imgArea.redraw();
}
});
_imgArea.addMenuItem("Prev", new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent e)
{
prevImageInDir();
_imgArea.redraw();
}
});
_imgArea.initZoomMenu();
_imgArea.addMenuItem("Close", new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent e)
{
_dlg.close();
}
});
}
private void initDir()
{
if (_lstFic == null)
{
File curFic = new File(_imgArea.getImagePath());
File curDir = curFic.getParentFile();
File[] tabFic = curDir.listFiles();
_lstFic = new ArrayList<File>(tabFic.length);
for (int i = 0; i < tabFic.length; i++)
{
if (Attribut.isImageExtension(tabFic[i]))
{
_lstFic.add(tabFic[i]);
if (curFic.equals(tabFic[i]))
{
_curIdx = i;
}
}
}
}
}
public void prevImageInDir()
{
initDir();
if (_curIdx >= 0)
{
_curIdx--;
if (_curIdx < 0)
{
_curIdx = _lstFic.size() - 1;
}
_imgArea.setImagePath(_lstFic.get(_curIdx).getAbsolutePath());
}
}
public void nextImageInDir()
{
initDir();
if (_curIdx >= 0)
{
_curIdx = (_curIdx + 1) % _lstFic.size();
_imgArea.setImagePath(_lstFic.get(_curIdx).getAbsolutePath());
}
}
/**
* Open an image browser dialog with a given file to display.
* @param path First file to display.
*/
public void open(String path)
{
_imgArea.setImagePath(path);
_imgArea.computeDialogSize(_dlg);
_dlg.open();
while (!_dlg.isDisposed())
{
if (!_dlg.getDisplay().readAndDispatch())
_dlg.getDisplay().sleep();
}
_dlg.dispose();
_imgArea.dispose();
}
/**
* Open an image browser dialog with a given file list.
*/
public void open(List<File> lstFic)
{
if ((lstFic != null) && (!lstFic.isEmpty()))
{
_lstFic = lstFic;
_curIdx = 0;
open(lstFic.get(_curIdx).getAbsolutePath());
}
}
}