Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Focus question
Focus question [message #431601] Fri, 12 March 2004 06:15
Robert Bacs is currently offline Robert Bacs
Messages: 165
Registered: July 2009
Senior Member
Hi,

I created a custom decoration and the problem was to prevent to gain focus
on mouse click. The only workaround that I found is to create a Label
control on that custom decoration. Is there another solution ?

Here is a snippet:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

class CustomDecoration extends Canvas {

private Listener listener;

/**
* @param parent
* @param style
*/
public CustomDecoration(Composite parent, int style) {
super(parent, style);
addListener(SWT.Paint, getListener());
}

/**
* @return
*/
private Listener getListener() {
if (listener == null){
listener = new Listener(){
public void handleEvent(Event event) {
switch(event.type){
case SWT.Paint:
onPaint(event);
break;
}
}
};
}
return listener;
}

/**
* @param event
*/
private void onPaint(Event event) {
Rectangle clientArea = getClientArea();
drawHighlightShadow(event.gc, clientArea);
drawLowlightShadows(event.gc, clientArea);
}

/**
* Draw the highlight shadow for the given column.
*
* @param gc GC to draw on
* @param colIndex specifies the column to draw
*/
private void drawHighlightShadow(GC gc, Rectangle bounds) {
Color oldForeground = getForeground();


gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGE T_HIGHLIGHT_SHAD
OW));
// draw top horizontal line
gc.drawLine(bounds.x, bounds.y, bounds.x + bounds.width - 1, bounds.y);
// draw left vertical line
gc.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height - 1);
gc.setForeground(oldForeground);
}

/**
* Draw the dark shadow on the lower and right side of a header column.
*
* @param gc GC to draw on
* @param colIndex specifies the column to draw
*/
private void drawLowlightShadows(GC gc, Rectangle bounds) {

Point bottomShadowStart = new Point(bounds.x + 1, bounds.height - 2);
Point bottomShadowStop = new Point(bottomShadowStart.x + bounds.width - 2,
bottomShadowStart.y);
Point rightShadowStart = null;
Point rightShadowStop = null;
Display display = getDisplay();
Color oldForeground = getForeground();

// light inner shadow
gc.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_NOR MAL_SHADOW));
gc.drawLine(
bottomShadowStart.x, bottomShadowStart.y,
bottomShadowStop.x, bottomShadowStop.y);
rightShadowStart = new Point(bounds.x + bounds.width - 2, bounds.y + 1);
rightShadowStop = new Point(rightShadowStart.x, bounds.height - 2);
gc.drawLine(rightShadowStart.x, rightShadowStart.y, rightShadowStop.x,
rightShadowStop.y);
// dark outer shadow
bottomShadowStart.x--;
bottomShadowStart.y++;
bottomShadowStop.y++;
gc.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DAR K_SHADOW));
gc.drawLine(
bottomShadowStart.x, bottomShadowStart.y,
bottomShadowStop.x, bottomShadowStop.y);
rightShadowStart.x++;
rightShadowStart.y--;
rightShadowStop.y++;
rightShadowStop.x++;
gc.drawLine(rightShadowStart.x, rightShadowStart.y, rightShadowStop.x,
rightShadowStop.y);
gc.setForeground(oldForeground);
}
}


/**
* @author Robert Bacs
*/
public class CustomDecorationTest {

public static void main(String []args){
Display display = new Display();
Shell shell = new Shell(display);
// Shell shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN |
SWT.MAX);
GridLayout layout = new GridLayout();
layout.numColumns = 5;
shell.setLayout(layout);

Button button1 = new Button(shell, SWT.RADIO);
button1.setText("First Radio button");

Button button2 = new Button(shell, SWT.RADIO);
button2.setText("Second Radio button");

final CustomDecoration decoration1 = new CustomDecoration(shell,
SWT.NONE);

decoration1.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
GC gc = e.gc;
Rectangle clientArea = decoration1.getClientArea();
String text = "1";
Point p = gc.stringExtent(text);
gc.drawText(text, clientArea.x + (clientArea.width - p.x) / 2,
clientArea.y + (clientArea.height - p.y) / 2);
}
});

final CustomDecoration decoration2 = new CustomDecoration(shell,
SWT.NONE);
Label label = new Label(decoration2, SWT.NONE);

decoration2.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
GC gc = e.gc;
Rectangle clientArea = decoration2.getClientArea();
String text = "2";
Point p = gc.stringExtent(text);
gc.drawText(text, clientArea.x + (clientArea.width - p.x) / 2,
clientArea.y + (clientArea.height - p.y) / 2);
}
});

shell.forceActive();
shell.pack();
shell.open();


while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Previous Topic:Focus question
Next Topic:JavaDoc pages of swt classes
Goto Forum:
  


Current Time: Fri Aug 29 08:18:39 EDT 2014

Powered by FUDForum. Page generated in 0.01563 seconds