Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Custom Control Traversal
Custom Control Traversal [message #450513] Mon, 14 February 2005 19:30 Go to next message
Scott Pelton is currently offline Scott PeltonFriend
Messages: 81
Registered: July 2009
Member
I am having trouble understanding focus traversal in a custom control. I
have a custom control that extends composite. My custom control contains
three other custom controls. I want to traverse these custom controls in
order as a tab group.

I have played around with the few things I have found in this forum
(archive) to help me get a grasp on understanding traversal.... however I
am coming up more confused than when I began.

Do I need to implement traversal on my own (simply setting the tab list
does not do the trick)? If so, do I do this in a traverse listener for
each control in my composite or in a traverse listener for for my custom
control (composite)? Do I do this using the traverse() method or is the
setFocus() method prefered?
Re: Custom Control Traversal [message #450554 is a reply to message #450513] Tue, 15 February 2005 15:04 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Is focus ever supposed to go to the parent of the three other custom
controls?
Have you seen Snippet21? It might be exactly what you are looking for to
use with the three controls.

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet21.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup

If you could post a small stand alone snippet, we could probably help you
more.

"Scott Pelton" <scottp@worldpac.com> wrote in message
news:cuqu80$am1$1@www.eclipse.org...
> I am having trouble understanding focus traversal in a custom control. I
> have a custom control that extends composite. My custom control contains
> three other custom controls. I want to traverse these custom controls in
> order as a tab group.
>
> I have played around with the few things I have found in this forum
> (archive) to help me get a grasp on understanding traversal.... however I
> am coming up more confused than when I began.
>
> Do I need to implement traversal on my own (simply setting the tab list
> does not do the trick)? If so, do I do this in a traverse listener for
> each control in my composite or in a traverse listener for for my custom
> control (composite)? Do I do this using the traverse() method or is the
> setFocus() method prefered?
>
Re: Custom Control Traversal [message #450588 is a reply to message #450554] Tue, 15 February 2005 18:42 Go to previous messageGo to next message
Scott Pelton is currently offline Scott PeltonFriend
Messages: 81
Registered: July 2009
Member
> Is focus ever supposed to go to the parent of the three other custom
> controls?
No, just the custom child controls.
Here is the design of my custom control:
Composite comp0 - is the parent.
Composite comp1 - is a child of comp0.
Composite comp2 - is a child of comp0.
Composite comp3 - is a child of comp0.
comp1 contains 2 Labels
comp2 contains n Canvas (where n >= 1; generally 1 to 3)
comp3 contains 1 Button

I want focus order to be:
comp1, comp2(canvas[0]), comp2(canvas[1]), ..., comp2(canvas[n]),
comp2(Button);


> Have you seen Snippet21? It might be exactly what you are looking for to
> use with the three controls.

Yes, this is where I started.

>
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet21.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup

> If you could post a small stand alone snippet, we could probably help you
> more.

I will work on getting one posted. BTW, I am developing for PPC.


> "Scott Pelton" <scottp@worldpac.com> wrote in message
> news:cuqu80$am1$1@www.eclipse.org...
>> I am having trouble understanding focus traversal in a custom control. I
>> have a custom control that extends composite. My custom control contains
>> three other custom controls. I want to traverse these custom controls in
>> order as a tab group.
>>
>> I have played around with the few things I have found in this forum
>> (archive) to help me get a grasp on understanding traversal.... however I
>> am coming up more confused than when I began.
>>
>> Do I need to implement traversal on my own (simply setting the tab list
>> does not do the trick)? If so, do I do this in a traverse listener for
>> each control in my composite or in a traverse listener for for my custom
>> control (composite)? Do I do this using the traverse() method or is the
>> setFocus() method prefered?
>>
Re: Custom Control Traversal [message #450642 is a reply to message #450588] Wed, 16 February 2005 13:58 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Ok, I'll wait for the snippet.

"Scott Pelton" <scottp@worldpac.com> wrote in message
news:cutfrd$bmn$1@www.eclipse.org...
> > Is focus ever supposed to go to the parent of the three other custom
> > controls?
> No, just the custom child controls.
> Here is the design of my custom control:
> Composite comp0 - is the parent.
> Composite comp1 - is a child of comp0.
> Composite comp2 - is a child of comp0.
> Composite comp3 - is a child of comp0.
> comp1 contains 2 Labels
> comp2 contains n Canvas (where n >= 1; generally 1 to 3)
> comp3 contains 1 Button
>
> I want focus order to be:
> comp1, comp2(canvas[0]), comp2(canvas[1]), ..., comp2(canvas[n]),
> comp2(Button);
>
>
> > Have you seen Snippet21? It might be exactly what you are looking for
to
> > use with the three controls.
>
> Yes, this is where I started.
>
> >
>
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet21.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
>
> > If you could post a small stand alone snippet, we could probably help
you
> > more.
>
> I will work on getting one posted. BTW, I am developing for PPC.
>
>
> > "Scott Pelton" <scottp@worldpac.com> wrote in message
> > news:cuqu80$am1$1@www.eclipse.org...
> >> I am having trouble understanding focus traversal in a custom control.
I
> >> have a custom control that extends composite. My custom control
contains
> >> three other custom controls. I want to traverse these custom controls
in
> >> order as a tab group.
> >>
> >> I have played around with the few things I have found in this forum
> >> (archive) to help me get a grasp on understanding traversal.... however
I
> >> am coming up more confused than when I began.
> >>
> >> Do I need to implement traversal on my own (simply setting the tab list
> >> does not do the trick)? If so, do I do this in a traverse listener for
> >> each control in my composite or in a traverse listener for for my
custom
> >> control (composite)? Do I do this using the traverse() method or is the
> >> setFocus() method prefered?
> >>
>
>
Re: Custom Control Traversal [message #450722 is a reply to message #450642] Thu, 17 February 2005 00:34 Go to previous messageGo to next message
Scott Pelton is currently offline Scott PeltonFriend
Messages: 81
Registered: July 2009
Member
Here is the snippet.....it is quite long...
the main class "snippet_DPPanel" plus five classes (1 inner and 1 inner
inner)

import java.util.*;

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.*;

/**
* @author spelton Feb 16, 2005
* Snippet to test custom control (DPPanel)
* I am having trouble getting tabbing to work.
*
* NOTE1: to be run on pocket pc
*
* NOTE2: if you pass in an integer n on the command line then n controls
will be created
* (recommend n=5) otherwise two controls are created.
*
* NOTE3: there is no functionality in this snippet
* (except for a popup menu appearing upon clicking down arrow button)
*
*/
public class snippet_DPPanel
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display, SWT.NONE);
shell.setLayout(new GridLayout());
//
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);

int cnt = 2;
try
{
cnt = Integer.parseInt(args[0]);
}
catch(Exception e)
{

}

//just test data
DPPanel[] dpp = new DPPanel[cnt];
for(int i = 0; i < cnt; i++)
{
dpp[i] = new DPPanel(shell, "test"+i, i+1);
dpp[i].setLayoutData(new GridData(GridData.FILL_BOTH));
}
dpp[0].setFocus();

shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch ()) display.sleep();
}
display.dispose();
}
}

class DPPanel extends Composite
{
ImageLabel imglblIdentifier;
Label lblStatus;
BoxesPanel boxesPanel;
FormData fd1;

ArrowButtonPanel abPanel;
Menu popupMenu;
MenuItem itemPartial, itemLater, itemCancel, itemReset;

int iBoxes;

/**
* Constructor for DPPanel.
* @param arg0 - Composite to which this DPPanel is a child of.
* @param sID - identifies this delivery/pickup (i.e. Pick Ticket #)
* @param iBoxes - number of packages
* Generic constructor
*/
public DPPanel(Composite arg0, String sID, int iBoxes)
{
super(arg0, SWT.NONE);

this.iBoxes = iBoxes;

//creat layout stuff
FormLayout fl = new FormLayout();
fl.marginWidth = 2;
fl.marginHeight = 2;
this.setLayout(fl);

FormData fd0 = new FormData();
fd0.left = new FormAttachment(0 ,2);
fd0.top = new FormAttachment(0 ,2);

//add identifier label
imglblIdentifier = new ImageLabel(this, SWT.LEFT);
imglblIdentifier.setImage(null);
imglblIdentifier.setText(sID);
imglblIdentifier.setLayoutData(fd0);

FormData fd2 = new FormData();
fd2.top = new FormAttachment(0,-4);
fd2.right = new FormAttachment(100, 0);

//add arrow button
abPanel = new ArrowButtonPanel(this);
abPanel.setLayoutData(fd2);

//do popup menu stuff
popupMenu = new Menu(this);

itemPartial = new MenuItem(popupMenu, SWT.PUSH);
itemPartial.setText("Partial");

itemLater = new MenuItem(popupMenu, SWT.PUSH);
itemLater.setText("Later");

itemCancel = new MenuItem(popupMenu, SWT.PUSH);
itemCancel.setText("Cancel");

itemReset = new MenuItem(popupMenu, SWT.PUSH);
itemReset.setText("Reset");

fd1 = new FormData();
fd1.top = new FormAttachment(0,-4);
fd1.left = new FormAttachment(30, 2);
fd1.right = new FormAttachment(90, 0);

boxesPanel = new BoxesPanel(this, iBoxes, 0);
boxesPanel.setLayoutData(fd1);

//add listeners
addListeners();
}

/**
*
*
*
*/
public void setBackground(Color clr)
{
super.setBackground(clr);
imglblIdentifier.setBackground(clr);
abPanel.setBackground(clr);
boxesPanel.setBackground(clr);
}


/**
*
*
*
*/
void addListeners()
{
this.addDisposeListener(new DisposeListener()
{
public void widgetDisposed(DisposeEvent e)
{

}
});
this.addListener(SWT.Traverse, new Listener()
{
public void handleEvent(Event e)
{
switch(e.detail)
{
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
e.doit = true;
break;
}
}
});
this.addListener(SWT.FocusIn, new Listener()
{
public void handleEvent(Event e)
{
DPPanel.this.redraw();
}
});
this.addListener(SWT.FocusOut, new Listener()
{
public void handleEvent(Event e)
{
DPPanel.this.redraw();
}
});
abPanel.ab.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
showPopupMenu();
}
});
abPanel.ab.addKeyListener(new KeyAdapter()
{
public void keyDown(KeyEvent e)
{
if(e.character == SWT.CR) showPopupMenu();
}
});
}




/**
*
*
*
*/
void showPopupMenu()
{
if(popupMenu.isVisible()) return;
Rectangle rect = abPanel.ab.getBounds();
rect = Display.getCurrent().map(abPanel.ab.getParent(), null, rect);
int x = rect.x;
int y = rect.y + rect.height;

//note: menu.getSize() will be available in SWT3.0 M8
//which may not help here if it only returns size of a visible menu
//see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=48341

//show popup menu
int xAlign =54;
popupMenu.setLocation(x-xAlign, y-1);
popupMenu.setVisible(true);
}


/**
*
* INNER class
* control that represents the number of pkgs
*/
class BoxesPanel extends Composite
{
BoxButton[] bbArray;
int iBoxes;

/**
* @author spelton Nov 24, 2003
* CONSTR
*
*/
public BoxesPanel(Composite arg0, int iB, int iL)
{
super(arg0, SWT.NONE);
RowLayout rl = new RowLayout(SWT.HORIZONTAL);
rl.marginWidth = 2;
rl.marginHeight = 2;
this.setLayout(rl);


this.iBoxes = iB;
int n = 0;
if(iL > iB) n = iL - iB;
bbArray = new BoxButton[iBoxes+n];
//do boxes up to pkg count
int i = 0;
for(; i < iBoxes; i++)
{
bbArray[i] = new BoxButton(this, i);
bbArray[i].setLayoutData(new RowData(17,17));
}

this.addListener(SWT.Traverse, new Listener()
{
public void handleEvent(Event e)
{
switch(e.detail)
{
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
e.doit = true;
break;
}
}
});
this.addListener(SWT.FocusIn, new Listener()
{
public void handleEvent(Event e)
{
BoxesPanel.this.redraw();
}
});
this.addListener(SWT.FocusOut, new Listener()
{
public void handleEvent(Event e)
{
BoxesPanel.this.redraw();
}
});
}

/**
*
*
*
*/
public void setEnabled(boolean b)
{
super.setEnabled(b);
for (int i = 0; i < bbArray.length; i++)
{
bbArray[i].setEnabled(b);
}
}


/**
* INNER INNER class
*
*
*/
class BoxButton extends Canvas
{
final static int PKG_NOT_LOADED = 20;
int iState = PKG_NOT_LOADED;
String sID;

private boolean bEnabled = true;
Color clrTopTri;//previous bckgrnd color

/**
* @author spelton Nov 26, 2003
* CONSTR
*
*/
public BoxButton(Composite parent, int idx)
{
super(parent, SWT.NO_BACKGROUND);
this.sID = Integer.toString(idx+1);
clrTopTri = getBackground();
addListeners();
}

/**
* @author spelton Feb 9, 2005
*
*
*/
void addListeners()
{
this.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent e)
{
paint();
}
});
//
this.addListener(SWT.Traverse, new Listener()
{
public void handleEvent(Event e)
{
switch(e.detail)
{
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
e.doit = true;
break;
}
}
});
this.addListener(SWT.FocusIn, new Listener()
{
public void handleEvent(Event e)
{
BoxButton.this.redraw();
}
});
this.addListener(SWT.FocusOut, new Listener()
{
public void handleEvent(Event e)
{
BoxButton.this.redraw();
}
});
}

/**
*
* override setEnabled
*
*/
public void setEnabled(boolean b)
{
bEnabled = b;
paint();
}
public boolean isEnabled(){ return bEnabled; }



/**
*
* draw this box button
*
*/
void paint()
{
GC gc = new GC(this);
gc.setBackground(getBackground());
gc.setForeground(getForeground());

//
gc.setLineWidth(this.isFocusControl() ? 3 : 1);
//draw box border
gc.drawRectangle(0, 0, 16, 16);

//draw box id - package number
String fnt_name = gc.getFont().getFontData()[0].getName();
Font f = new Font(Display.getCurrent(), fnt_name, 7, SWT.NORMAL);
gc.setFont(f);
Point pt = gc.stringExtent(sID);
gc.drawString(sID, 8-pt.x/2, 8-pt.y/2, true);

//
f.dispose();
gc.dispose();
}
}
}
}


class ArrowButtonPanel extends Composite
{
Button ab;

/**
* @author spelton Nov 24, 2003
* CONSTR
*
*/
public ArrowButtonPanel(Composite arg0)
{
super(arg0, SWT.NONE);
RowLayout rl = new RowLayout(SWT.HORIZONTAL);
rl.marginWidth = 0;
rl.marginHeight = 2;
this.setLayout(rl);

ab = new Button(this, SWT.ARROW | SWT.DOWN);
ab.setLayoutData(new RowData(17,17));
addListeners();
this.setTabList(new Control[] {ab});
}

/**
* @author spelton Feb 9, 2005
*
*
*/
void addListeners()
{
this.addListener(SWT.Traverse, new Listener()
{
public void handleEvent(Event e)
{
switch(e.detail)
{
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
e.doit = true;
break;
}
}
});
this.addListener(SWT.FocusIn, new Listener()
{
public void handleEvent(Event e)
{
ArrowButtonPanel.this.redraw();
}
});
this.addListener(SWT.FocusOut, new Listener()
{
public void handleEvent(Event e)
{
ArrowButtonPanel.this.redraw();
}
});
}
}


class ImageLabel extends Composite
{
Label lblImage, lblText;

/**
* Constructor for ImageLabel.
* @param arg0
* @param arg1
*/
public ImageLabel(Composite arg0, int arg1)
{
super(arg0, arg1);
//creat layout stuff
FormLayout fl = new FormLayout();
fl.marginWidth = 1;
fl.marginHeight = 1;
this.setLayout(fl);

FormData fd00 = new FormData();
fd00.left = new FormAttachment(0,0);
fd00.top = new FormAttachment(0,0);

//add image label
lblImage = new Label(this, SWT.CENTER | SWT.NO_FOCUS);
lblImage.setLayoutData(fd00);

FormData fd0 = new FormData();
fd0.left = new FormAttachment(lblImage, 2);
fd0.top = new FormAttachment(0,0);

//add text label
lblText = new Label(this, SWT.LEFT | SWT.NO_FOCUS);
lblText.setLayoutData(fd0);

addListeners();
}

/**
* @author spelton Dec 17, 2004
*
*
*/
void addListeners()
{
//underline text
lblText.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent e)
{
Point p = e.gc.textExtent(lblText.getText());
e.gc.drawLine(0, p.y-1, p.x, p.y-1);
}
});
//
this.addListener(SWT.Traverse, new Listener()
{
public void handleEvent(Event e)
{
switch(e.detail)
{
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
e.doit = true;
break;
}
}
});
this.addListener(SWT.FocusIn, new Listener()
{
public void handleEvent(Event e)
{
ImageLabel.this.redraw();
}
});
this.addListener(SWT.FocusOut, new Listener()
{
public void handleEvent(Event e)
{
ImageLabel.this.redraw();
}
});
//draw focus border
this.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent e)
{
paint();
}
});
}


/**
* @author spelton Feb 8, 2005
*
*
*/
void paint()
{
GC gc = new GC(this);
if(isFocusControl())
{
Rectangle r = getBounds();
gc.drawFocus(0,0,r.width,r.height);
}
else
{
gc.setForeground(getBackground());
Rectangle r = getBounds();
gc.drawRectangle(0,0,r.width,r.height);
}
gc.dispose();
}

/**
* @author spelton May 27, 2004
*
*
*/
public void setImage(Image img)
{
lblImage.setImage(img);
}

/**
* @author spelton May 27, 2004
*
*
*/
public void setText(String s)
{
lblText.setText(s);
}

/**
* @author spelton May 27, 2004
*
*
*/
public void setBackground(Color clr)
{
super.setBackground(clr);
lblImage.setBackground(clr);
lblText.setBackground(clr);
}
}

Steve Northover wrote:

> Ok, I'll wait for the snippet.

> "Scott Pelton" <scottp@worldpac.com> wrote in message
> news:cutfrd$bmn$1@www.eclipse.org...
>> > Is focus ever supposed to go to the parent of the three other custom
>> > controls?
>> No, just the custom child controls.
>> Here is the design of my custom control:
>> Composite comp0 - is the parent.
>> Composite comp1 - is a child of comp0.
>> Composite comp2 - is a child of comp0.
>> Composite comp3 - is a child of comp0.
>> comp1 contains 2 Labels
>> comp2 contains n Canvas (where n >= 1; generally 1 to 3)
>> comp3 contains 1 Button
>>
>> I want focus order to be:
>> comp1, comp2(canvas[0]), comp2(canvas[1]), ..., comp2(canvas[n]),
>> comp2(Button);
>>
>>
>> > Have you seen Snippet21? It might be exactly what you are looking for
> to
>> > use with the three controls.
>>
>> Yes, this is where I started.
>>
>> >
>>
>
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet21.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
>>
>> > If you could post a small stand alone snippet, we could probably help
> you
>> > more.
>>
>> I will work on getting one posted. BTW, I am developing for PPC.
>>
>>
>> > "Scott Pelton" <scottp@worldpac.com> wrote in message
>> > news:cuqu80$am1$1@www.eclipse.org...
>> >> I am having trouble understanding focus traversal in a custom control.
> I
>> >> have a custom control that extends composite. My custom control
> contains
>> >> three other custom controls. I want to traverse these custom controls
> in
>> >> order as a tab group.
>> >>
>> >> I have played around with the few things I have found in this forum
>> >> (archive) to help me get a grasp on understanding traversal.... however
> I
>> >> am coming up more confused than when I began.
>> >>
>> >> Do I need to implement traversal on my own (simply setting the tab list
>> >> does not do the trick)? If so, do I do this in a traverse listener for
>> >> each control in my composite or in a traverse listener for for my
> custom
>> >> control (composite)? Do I do this using the traverse() method or is the
>> >> setFocus() method prefered?
>> >>
>>
>>
Re: Custom Control Traversal [message #450756 is a reply to message #450722] Thu, 17 February 2005 18:25 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Yikes ... that's a lot of code. Ok, it was impossible for me to know if it
was working but when I added key listeners to the various components, I was
able to tab around. You need to add a key listener to a control or it won't
get focus (because there is nobody processing keys).

"Scott Pelton" <scottp@worldpac.com> wrote in message
news:cv0org$n87$1@www.eclipse.org...
> Here is the snippet.....it is quite long...
> the main class "snippet_DPPanel" plus five classes (1 inner and 1 inner
> inner)
>
> import java.util.*;
>
> 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.*;
>
> /**
> * @author spelton Feb 16, 2005
> * Snippet to test custom control (DPPanel)
> * I am having trouble getting tabbing to work.
> *
> * NOTE1: to be run on pocket pc
> *
> * NOTE2: if you pass in an integer n on the command line then n controls
> will be created
> * (recommend n=5) otherwise two controls are created.
> *
> * NOTE3: there is no functionality in this snippet
> * (except for a popup menu appearing upon clicking down arrow button)
> *
> */
> public class snippet_DPPanel
> {
> public static void main(String[] args)
> {
> Display display = new Display();
> Shell shell = new Shell(display, SWT.NONE);
> shell.setLayout(new GridLayout());
> //
> Menu bar = new Menu(shell, SWT.BAR);
> shell.setMenuBar(bar);
>
> int cnt = 2;
> try
> {
> cnt = Integer.parseInt(args[0]);
> }
> catch(Exception e)
> {
>
> }
>
> //just test data
> DPPanel[] dpp = new DPPanel[cnt];
> for(int i = 0; i < cnt; i++)
> {
> dpp[i] = new DPPanel(shell, "test"+i, i+1);
> dpp[i].setLayoutData(new GridData(GridData.FILL_BOTH));
> }
> dpp[0].setFocus();
>
> shell.open();
> while(!shell.isDisposed())
> {
> if(!display.readAndDispatch ()) display.sleep();
> }
> display.dispose();
> }
> }
>
> class DPPanel extends Composite
> {
> ImageLabel imglblIdentifier;
> Label lblStatus;
> BoxesPanel boxesPanel;
> FormData fd1;
>
> ArrowButtonPanel abPanel;
> Menu popupMenu;
> MenuItem itemPartial, itemLater, itemCancel, itemReset;
>
> int iBoxes;
>
> /**
> * Constructor for DPPanel.
> * @param arg0 - Composite to which this DPPanel is a child of.
> * @param sID - identifies this delivery/pickup (i.e. Pick Ticket #)
> * @param iBoxes - number of packages
> * Generic constructor
> */
> public DPPanel(Composite arg0, String sID, int iBoxes)
> {
> super(arg0, SWT.NONE);
>
> this.iBoxes = iBoxes;
>
> //creat layout stuff
> FormLayout fl = new FormLayout();
> fl.marginWidth = 2;
> fl.marginHeight = 2;
> this.setLayout(fl);
>
> FormData fd0 = new FormData();
> fd0.left = new FormAttachment(0 ,2);
> fd0.top = new FormAttachment(0 ,2);
>
> //add identifier label
> imglblIdentifier = new ImageLabel(this, SWT.LEFT);
> imglblIdentifier.setImage(null);
> imglblIdentifier.setText(sID);
> imglblIdentifier.setLayoutData(fd0);
>
> FormData fd2 = new FormData();
> fd2.top = new FormAttachment(0,-4);
> fd2.right = new FormAttachment(100, 0);
>
> //add arrow button
> abPanel = new ArrowButtonPanel(this);
> abPanel.setLayoutData(fd2);
>
> //do popup menu stuff
> popupMenu = new Menu(this);
>
> itemPartial = new MenuItem(popupMenu, SWT.PUSH);
> itemPartial.setText("Partial");
>
> itemLater = new MenuItem(popupMenu, SWT.PUSH);
> itemLater.setText("Later");
>
> itemCancel = new MenuItem(popupMenu, SWT.PUSH);
> itemCancel.setText("Cancel");
>
> itemReset = new MenuItem(popupMenu, SWT.PUSH);
> itemReset.setText("Reset");
>
> fd1 = new FormData();
> fd1.top = new FormAttachment(0,-4);
> fd1.left = new FormAttachment(30, 2);
> fd1.right = new FormAttachment(90, 0);
>
> boxesPanel = new BoxesPanel(this, iBoxes, 0);
> boxesPanel.setLayoutData(fd1);
>
> //add listeners
> addListeners();
> }
>
> /**
> *
> *
> *
> */
> public void setBackground(Color clr)
> {
> super.setBackground(clr);
> imglblIdentifier.setBackground(clr);
> abPanel.setBackground(clr);
> boxesPanel.setBackground(clr);
> }
>
>
> /**
> *
> *
> *
> */
> void addListeners()
> {
> this.addDisposeListener(new DisposeListener()
> {
> public void widgetDisposed(DisposeEvent e)
> {
>
> }
> });
> this.addListener(SWT.Traverse, new Listener()
> {
> public void handleEvent(Event e)
> {
> switch(e.detail)
> {
> /* Do tab group traversal */
> case SWT.TRAVERSE_ESCAPE:
> case SWT.TRAVERSE_RETURN:
> case SWT.TRAVERSE_TAB_NEXT:
> case SWT.TRAVERSE_TAB_PREVIOUS:
> case SWT.TRAVERSE_PAGE_NEXT:
> case SWT.TRAVERSE_PAGE_PREVIOUS:
> e.doit = true;
> break;
> }
> }
> });
> this.addListener(SWT.FocusIn, new Listener()
> {
> public void handleEvent(Event e)
> {
> DPPanel.this.redraw();
> }
> });
> this.addListener(SWT.FocusOut, new Listener()
> {
> public void handleEvent(Event e)
> {
> DPPanel.this.redraw();
> }
> });
> abPanel.ab.addSelectionListener(new SelectionAdapter()
> {
> public void widgetSelected(SelectionEvent e)
> {
> showPopupMenu();
> }
> });
> abPanel.ab.addKeyListener(new KeyAdapter()
> {
> public void keyDown(KeyEvent e)
> {
> if(e.character == SWT.CR) showPopupMenu();
> }
> });
> }
>
>
>
>
> /**
> *
> *
> *
> */
> void showPopupMenu()
> {
> if(popupMenu.isVisible()) return;
> Rectangle rect = abPanel.ab.getBounds();
> rect = Display.getCurrent().map(abPanel.ab.getParent(), null, rect);
> int x = rect.x;
> int y = rect.y + rect.height;
>
> //note: menu.getSize() will be available in SWT3.0 M8
> //which may not help here if it only returns size of a visible menu
> //see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=48341
>
> //show popup menu
> int xAlign =54;
> popupMenu.setLocation(x-xAlign, y-1);
> popupMenu.setVisible(true);
> }
>
>
> /**
> *
> * INNER class
> * control that represents the number of pkgs
> */
> class BoxesPanel extends Composite
> {
> BoxButton[] bbArray;
> int iBoxes;
>
> /**
> * @author spelton Nov 24, 2003
> * CONSTR
> *
> */
> public BoxesPanel(Composite arg0, int iB, int iL)
> {
> super(arg0, SWT.NONE);
> RowLayout rl = new RowLayout(SWT.HORIZONTAL);
> rl.marginWidth = 2;
> rl.marginHeight = 2;
> this.setLayout(rl);
>
>
> this.iBoxes = iB;
> int n = 0;
> if(iL > iB) n = iL - iB;
> bbArray = new BoxButton[iBoxes+n];
> //do boxes up to pkg count
> int i = 0;
> for(; i < iBoxes; i++)
> {
> bbArray[i] = new BoxButton(this, i);
> bbArray[i].setLayoutData(new RowData(17,17));
> }
>
> this.addListener(SWT.Traverse, new Listener()
> {
> public void handleEvent(Event e)
> {
> switch(e.detail)
> {
> /* Do tab group traversal */
> case SWT.TRAVERSE_ESCAPE:
> case SWT.TRAVERSE_RETURN:
> case SWT.TRAVERSE_TAB_NEXT:
> case SWT.TRAVERSE_TAB_PREVIOUS:
> case SWT.TRAVERSE_PAGE_NEXT:
> case SWT.TRAVERSE_PAGE_PREVIOUS:
> e.doit = true;
> break;
> }
> }
> });
> this.addListener(SWT.FocusIn, new Listener()
> {
> public void handleEvent(Event e)
> {
> BoxesPanel.this.redraw();
> }
> });
> this.addListener(SWT.FocusOut, new Listener()
> {
> public void handleEvent(Event e)
> {
> BoxesPanel.this.redraw();
> }
> });
> }
>
> /**
> *
> *
> *
> */
> public void setEnabled(boolean b)
> {
> super.setEnabled(b);
> for (int i = 0; i < bbArray.length; i++)
> {
> bbArray[i].setEnabled(b);
> }
> }
>
>
> /**
> * INNER INNER class
> *
> *
> */
> class BoxButton extends Canvas
> {
> final static int PKG_NOT_LOADED = 20;
> int iState = PKG_NOT_LOADED;
> String sID;
>
> private boolean bEnabled = true;
> Color clrTopTri;//previous bckgrnd color
>
> /**
> * @author spelton Nov 26, 2003
> * CONSTR
> *
> */
> public BoxButton(Composite parent, int idx)
> {
> super(parent, SWT.NO_BACKGROUND);
> this.sID = Integer.toString(idx+1);
> clrTopTri = getBackground();
> addListeners();
> }
>
> /**
> * @author spelton Feb 9, 2005
> *
> *
> */
> void addListeners()
> {
> this.addPaintListener(new PaintListener()
> {
> public void paintControl(PaintEvent e)
> {
> paint();
> }
> });
> //
> this.addListener(SWT.Traverse, new Listener()
> {
> public void handleEvent(Event e)
> {
> switch(e.detail)
> {
> /* Do tab group traversal */
> case SWT.TRAVERSE_ESCAPE:
> case SWT.TRAVERSE_RETURN:
> case SWT.TRAVERSE_TAB_NEXT:
> case SWT.TRAVERSE_TAB_PREVIOUS:
> case SWT.TRAVERSE_PAGE_NEXT:
> case SWT.TRAVERSE_PAGE_PREVIOUS:
> e.doit = true;
> break;
> }
> }
> });
> this.addListener(SWT.FocusIn, new Listener()
> {
> public void handleEvent(Event e)
> {
> BoxButton.this.redraw();
> }
> });
> this.addListener(SWT.FocusOut, new Listener()
> {
> public void handleEvent(Event e)
> {
> BoxButton.this.redraw();
> }
> });
> }
>
> /**
> *
> * override setEnabled
> *
> */
> public void setEnabled(boolean b)
> {
> bEnabled = b;
> paint();
> }
> public boolean isEnabled(){ return bEnabled; }
>
>
>
> /**
> *
> * draw this box button
> *
> */
> void paint()
> {
> GC gc = new GC(this);
> gc.setBackground(getBackground());
> gc.setForeground(getForeground());
>
> //
> gc.setLineWidth(this.isFocusControl() ? 3 : 1);
> //draw box border
> gc.drawRectangle(0, 0, 16, 16);
>
> //draw box id - package number
> String fnt_name = gc.getFont().getFontData()[0].getName();
> Font f = new Font(Display.getCurrent(), fnt_name, 7, SWT.NORMAL);
> gc.setFont(f);
> Point pt = gc.stringExtent(sID);
> gc.drawString(sID, 8-pt.x/2, 8-pt.y/2, true);
>
> //
> f.dispose();
> gc.dispose();
> }
> }
> }
> }
>
>
> class ArrowButtonPanel extends Composite
> {
> Button ab;
>
> /**
> * @author spelton Nov 24, 2003
> * CONSTR
> *
> */
> public ArrowButtonPanel(Composite arg0)
> {
> super(arg0, SWT.NONE);
> RowLayout rl = new RowLayout(SWT.HORIZONTAL);
> rl.marginWidth = 0;
> rl.marginHeight = 2;
> this.setLayout(rl);
>
> ab = new Button(this, SWT.ARROW | SWT.DOWN);
> ab.setLayoutData(new RowData(17,17));
> addListeners();
> this.setTabList(new Control[] {ab});
> }
>
> /**
> * @author spelton Feb 9, 2005
> *
> *
> */
> void addListeners()
> {
> this.addListener(SWT.Traverse, new Listener()
> {
> public void handleEvent(Event e)
> {
> switch(e.detail)
> {
> /* Do tab group traversal */
> case SWT.TRAVERSE_ESCAPE:
> case SWT.TRAVERSE_RETURN:
> case SWT.TRAVERSE_TAB_NEXT:
> case SWT.TRAVERSE_TAB_PREVIOUS:
> case SWT.TRAVERSE_PAGE_NEXT:
> case SWT.TRAVERSE_PAGE_PREVIOUS:
> e.doit = true;
> break;
> }
> }
> });
> this.addListener(SWT.FocusIn, new Listener()
> {
> public void handleEvent(Event e)
> {
> ArrowButtonPanel.this.redraw();
> }
> });
> this.addListener(SWT.FocusOut, new Listener()
> {
> public void handleEvent(Event e)
> {
> ArrowButtonPanel.this.redraw();
> }
> });
> }
> }
>
>
> class ImageLabel extends Composite
> {
> Label lblImage, lblText;
>
> /**
> * Constructor for ImageLabel.
> * @param arg0
> * @param arg1
> */
> public ImageLabel(Composite arg0, int arg1)
> {
> super(arg0, arg1);
> //creat layout stuff
> FormLayout fl = new FormLayout();
> fl.marginWidth = 1;
> fl.marginHeight = 1;
> this.setLayout(fl);
>
> FormData fd00 = new FormData();
> fd00.left = new FormAttachment(0,0);
> fd00.top = new FormAttachment(0,0);
>
> //add image label
> lblImage = new Label(this, SWT.CENTER | SWT.NO_FOCUS);
> lblImage.setLayoutData(fd00);
>
> FormData fd0 = new FormData();
> fd0.left = new FormAttachment(lblImage, 2);
> fd0.top = new FormAttachment(0,0);
>
> //add text label
> lblText = new Label(this, SWT.LEFT | SWT.NO_FOCUS);
> lblText.setLayoutData(fd0);
>
> addListeners();
> }
>
> /**
> * @author spelton Dec 17, 2004
> *
> *
> */
> void addListeners()
> {
> //underline text
> lblText.addPaintListener(new PaintListener()
> {
> public void paintControl(PaintEvent e)
> {
> Point p = e.gc.textExtent(lblText.getText());
> e.gc.drawLine(0, p.y-1, p.x, p.y-1);
> }
> });
> //
> this.addListener(SWT.Traverse, new Listener()
> {
> public void handleEvent(Event e)
> {
> switch(e.detail)
> {
> /* Do tab group traversal */
> case SWT.TRAVERSE_ESCAPE:
> case SWT.TRAVERSE_RETURN:
> case SWT.TRAVERSE_TAB_NEXT:
> case SWT.TRAVERSE_TAB_PREVIOUS:
> case SWT.TRAVERSE_PAGE_NEXT:
> case SWT.TRAVERSE_PAGE_PREVIOUS:
> e.doit = true;
> break;
> }
> }
> });
> this.addListener(SWT.FocusIn, new Listener()
> {
> public void handleEvent(Event e)
> {
> ImageLabel.this.redraw();
> }
> });
> this.addListener(SWT.FocusOut, new Listener()
> {
> public void handleEvent(Event e)
> {
> ImageLabel.this.redraw();
> }
> });
> //draw focus border
> this.addPaintListener(new PaintListener()
> {
> public void paintControl(PaintEvent e)
> {
> paint();
> }
> });
> }
>
>
> /**
> * @author spelton Feb 8, 2005
> *
> *
> */
> void paint()
> {
> GC gc = new GC(this);
> if(isFocusControl())
> {
> Rectangle r = getBounds();
> gc.drawFocus(0,0,r.width,r.height);
> }
> else
> {
> gc.setForeground(getBackground());
> Rectangle r = getBounds();
> gc.drawRectangle(0,0,r.width,r.height);
> }
> gc.dispose();
> }
>
> /**
> * @author spelton May 27, 2004
> *
> *
> */
> public void setImage(Image img)
> {
> lblImage.setImage(img);
> }
>
> /**
> * @author spelton May 27, 2004
> *
> *
> */
> public void setText(String s)
> {
> lblText.setText(s);
> }
>
> /**
> * @author spelton May 27, 2004
> *
> *
> */
> public void setBackground(Color clr)
> {
> super.setBackground(clr);
> lblImage.setBackground(clr);
> lblText.setBackground(clr);
> }
> }
>
> Steve Northover wrote:
>
> > Ok, I'll wait for the snippet.
>
> > "Scott Pelton" <scottp@worldpac.com> wrote in message
> > news:cutfrd$bmn$1@www.eclipse.org...
> >> > Is focus ever supposed to go to the parent of the three other custom
> >> > controls?
> >> No, just the custom child controls.
> >> Here is the design of my custom control:
> >> Composite comp0 - is the parent.
> >> Composite comp1 - is a child of comp0.
> >> Composite comp2 - is a child of comp0.
> >> Composite comp3 - is a child of comp0.
> >> comp1 contains 2 Labels
> >> comp2 contains n Canvas (where n >= 1; generally 1 to 3)
> >> comp3 contains 1 Button
> >>
> >> I want focus order to be:
> >> comp1, comp2(canvas[0]), comp2(canvas[1]), ..., comp2(canvas[n]),
> >> comp2(Button);
> >>
> >>
> >> > Have you seen Snippet21? It might be exactly what you are looking
for
> > to
> >> > use with the three controls.
> >>
> >> Yes, this is where I started.
> >>
> >> >
> >>
> >
>
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet21.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
> >>
> >> > If you could post a small stand alone snippet, we could probably help
> > you
> >> > more.
> >>
> >> I will work on getting one posted. BTW, I am developing for PPC.
> >>
> >>
> >> > "Scott Pelton" <scottp@worldpac.com> wrote in message
> >> > news:cuqu80$am1$1@www.eclipse.org...
> >> >> I am having trouble understanding focus traversal in a custom
control.
> > I
> >> >> have a custom control that extends composite. My custom control
> > contains
> >> >> three other custom controls. I want to traverse these custom
controls
> > in
> >> >> order as a tab group.
> >> >>
> >> >> I have played around with the few things I have found in this forum
> >> >> (archive) to help me get a grasp on understanding traversal....
however
> > I
> >> >> am coming up more confused than when I began.
> >> >>
> >> >> Do I need to implement traversal on my own (simply setting the tab
list
> >> >> does not do the trick)? If so, do I do this in a traverse listener
for
> >> >> each control in my composite or in a traverse listener for for my
> > custom
> >> >> control (composite)? Do I do this using the traverse() method or is
the
> >> >> setFocus() method prefered?
> >> >>
> >>
> >>
>
>
Previous Topic:swt-gdip-win32.dll in latest SWT ?
Next Topic:Eclipse Windows Properties
Goto Forum:
  


Current Time: Fri Apr 19 19:33:20 GMT 2024

Powered by FUDForum. Page generated in 0.03275 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top