package com.minres.scviewer.database.ui.swt.internal.slider; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTException; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; public class ImageButton extends Composite { private Image hoverImage; private Image normalImage; private Image pressedImage; private int width; private int height; private boolean hover; private boolean pressed; private boolean autoFire; private ActionTimer actionTimer; private ActionTimer.TimerAction timerAction; public ImageButton(Composite parent, int style) { super(parent, style); timerAction = new ActionTimer.TimerAction() { @Override public void run() { notifyListeners(); } @Override public boolean isEnabled() { return pressed; } }; actionTimer = new ActionTimer(timerAction, this.getDisplay() ); addListener(SWT.Dispose, event -> { if (hoverImage != null) hoverImage.dispose(); if (normalImage != null) normalImage.dispose(); if (pressedImage != null) pressedImage.dispose(); }); addListener(SWT.Paint, event -> { paintControl(event); }); addListener(SWT.MouseDown, event -> { pressed=true; notifyListeners(); if(autoFire) actionTimer.activate(); redraw(); }); addListener(SWT.MouseUp, event -> { pressed=false; redraw(); }); addListener(SWT.MouseMove, event -> { Point sz = ((ImageButton)event.widget).getSize(); final boolean within_x = event.x>0 && event.x0 && event.y1? new Image(d, imgs[1], SWT.IMAGE_COPY): new Image(d,imgs[0],SWT.IMAGE_GRAY); pressedImage = imgs.length>2? new Image(d, imgs[2], SWT.IMAGE_COPY): new Image(d,imgs[0],SWT.IMAGE_DISABLE); width = imgs[0].getBounds().width; height = imgs[0].getBounds().height; redraw(); } @Override public Point computeSize(int wHint, int hHint, boolean changed) { int overallWidth = width; int overallHeight = height; /* Consider hints */ if (wHint != SWT.DEFAULT && wHint < overallWidth) overallWidth = wHint; if (hHint != SWT.DEFAULT && hHint < overallHeight) overallHeight = hHint; /* Return computed dimensions plus border */ return new Point(overallWidth + 2, overallHeight + 2); } /** * Adds the listener to the collection of listeners who will be notified when * the user changes the receiver's value, by sending it one of the messages * defined in the SelectionListener interface. *

* widgetSelected is called when the user changes the receiver's * value. widgetDefaultSelected is not called. *

* * @param listener the listener which should be notified * * @exception IllegalArgumentException * * @exception SWTException * * * @see SelectionListener * @see #removeSelectionListener */ public void addSelectionListener(final SelectionListener listener) { checkWidget(); SelectionListenerUtil.addSelectionListener(this, listener); } /** * Removes the listener from the collection of listeners who will be notified * when the user changes the receiver's value. * * @param listener the listener which should no longer be notified * * @exception IllegalArgumentException * * @exception SWTException * * * @see SelectionListener * @see #addSelectionListener */ public void removeSelectionListener(final SelectionListener listener) { checkWidget(); SelectionListenerUtil.removeSelectionListener(this, listener); } private void notifyListeners() { Event e = new Event(); e.widget=this; e.type=SWT.Selection; SelectionListenerUtil.fireSelectionListeners(this,e); } public boolean isAutoFire() { return autoFire; } public void setAutoFire(boolean autoFire) { this.autoFire = autoFire; } }