/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* 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:
* IBM Corporation - initial API and implementation
* Exadel, Inc.
* Red Hat, Inc.
*******************************************************************************/
package org.jboss.tools.common.model.ui.widgets.xpl;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TypedListener;
public abstract class SelectableControl extends Canvas {
private boolean hasFocus;
public SelectableControl(Composite parent, int style) {
super(parent, style);
addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
paint(e);
}
});
addMouseListener(new MouseAdapter () {
public void mouseUp(MouseEvent e) {
notifyListeners(SWT.Selection);
}
});
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.character == '\r') {
// Activation
notifyListeners(SWT.Selection);
}
}
});
addListener(SWT.Traverse, new Listener () {
public void handleEvent(Event e) {
if (e.detail != SWT.TRAVERSE_RETURN)
e.doit = true;
}
});
addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
if (!hasFocus) {
hasFocus=true;
redraw();
}
}
public void focusLost(FocusEvent e) {
if (hasFocus) {
hasFocus=false;
redraw();
}
}
});
}
protected void paint(PaintEvent e) {
GC gc = e.gc;
Point size = getSize();
gc.setFont(getFont());
paint(gc);
if (hasFocus) {
gc.setForeground(getForeground());
gc.drawFocus(0, 0, size.x, size.y);
}
}
protected abstract void paint(GC gc);
private void notifyListeners(int eventType) {
Event event = new Event();
event.type = eventType;
event.widget = this;
notifyListeners(eventType, event);
}
public void addSelectionListener(SelectionListener listener) {
checkWidget ();
if (listener == null) return;
TypedListener typedListener = new TypedListener (listener);
addListener (SWT.Selection,typedListener);
}
public void removeSelectionListener(SelectionListener listener) {
checkWidget ();
if (listener == null) return;
removeListener (SWT.Selection, listener);
}
}