Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » How to create UI fields at runtime?
How to create UI fields at runtime? [message #894321] Sun, 08 July 2012 07:03 Go to next message
Dirk Ufermann is currently offline Dirk Ufermann
Messages: 2
Registered: July 2012
Junior Member
I'm looking for help with how to build a dynamic UI with Scout. I understand that Scout provides a model which is used to render a GUI, but I'm struggling in using that model so I can perform typical UI tasks with it. I wrote a few lines of SWT code to show what I'd like to achieve. How do I do the same with Scout? The key points here are:

1.) I'd like to create text fields at runtime
2.) I'd like to do something when any key is pressed (not just when the _value_ of the field changes)

Any help is greatly appreciated!
Dirk

SWT example:
public class DynamicFieldsDemo {

	private Display display;
	
	private Shell shell;

	private List<Text> textFields = new ArrayList<Text>();

	private KeyListener keyListener = new KeyAdapter() {
		@Override public void keyPressed(KeyEvent evt) {
			for (Text field : textFields) {
				boolean isActive = field == evt.getSource();
				int bgColor = isActive ? SWT.COLOR_YELLOW : SWT.COLOR_GRAY;
				field.setBackground(display.getSystemColor(bgColor));
				if (!isActive) field.setText("");
			}
		}
	};
	
	DynamicFieldsDemo() {
		display = new Display ();
		shell   = new Shell(display);
	}
	
	void open(int numberOfFields) {
		int y = 10, x = 10;
		// fields
		for (int i = 0; i < numberOfFields; i++) {
			Text field = new Text(shell, SWT.BORDER);
			field.setLocation(x, y += 30); field.setSize(200, 25);
			field.addKeyListener(keyListener);
			textFields.add(field);
		}
		// open dialog
		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}

	public static void main(String[] args) {
		int numberOfFields = 5;
		if (args.length == 1) {
			numberOfFields = Integer.valueOf(args[0]);
		}
		new DynamicFieldsDemo().open(numberOfFields);
	}
	
}
Re: How to create UI fields at runtime? [message #894343 is a reply to message #894321] Sun, 08 July 2012 13:54 Go to previous messageGo to next message
Claudio Guglielmo is currently offline Claudio Guglielmo
Messages: 127
Registered: March 2010
Senior Member
Hi Dirk

Have a look at AbstractStringField#getConfiguredValidateOnAnyKey() and AbstractCompositeField#injectFieldsInternal(). These two methods should help you to achieve your goal.

Good luck Smile
Claudio

Re: How to create UI fields at runtime? [message #895132 is a reply to message #894343] Wed, 11 July 2012 18:38 Go to previous messageGo to next message
Dirk Ufermann is currently offline Dirk Ufermann
Messages: 2
Registered: July 2012
Junior Member
Hi Claudio

Thanks for your help. Unfortunately the getConfiguredValidateOnAnyKey() method is only defined on the AbstractStringField, but not on other classes. How do I do the same with an Integer- or BigDecimal field?

Couldn't get the injectFieldsInternal() to work either: in my example "skyscraper configurator" I have a form with an IntegerField (NumberOfFloors) and a Button (Create). When I click on the button it should create as many StringFields as the value of NumberOfFloors is. When I type in another value, click the button again, the same should happen with the new value from NumberOfFloors. Of course this means, the size of the groupbox must change at runtime as well.

As you suggested I've overriden the injectFieldsInternal method of the CompositeField. But this seems to be called only when the form is initialized. I hacked around for some time, so initConfig() --> injectFieldsInternal() is called again when the button is clicked, but no luck - the UI didn't change.

It would be very helpful if someone could post some sample code to demonstrate how to use the injectFieldsInternal() method correctly.

Here's what I've done so far...

@Override
protected void injectFieldsInternal(List<IFormField> fieldList) {
  if (numberOfFloors != null) { // member variable is set when execClickAction() is executed
    for (int i = 1; i <= numberOfFloors; i++) {
      final int floorNo = i;
      fieldList.add(new AbstractStringField() {
        @Override
        protected String getConfiguredLabel() {
          return "Floor no. " + floorNo;
        }
      });
    }
  }
}


Dirk
Re: How to create UI fields at runtime? [message #895181 is a reply to message #895132] Wed, 11 July 2012 21:57 Go to previous messageGo to next message
Claudio Guglielmo is currently offline Claudio Guglielmo
Messages: 127
Registered: March 2010
Senior Member
Hi Dirk

As you already noticed injectFieldsInternal is called when a form gets constructed. At this time you can add or remove fields. Creating fields while a form is open is not supported so far.

What you can do is to initially create a lot of invisible fields and set the fields visible if you click on the button. Another approach would be to use a table with editable columns. Just set getConfiguredEditable to true on the column and you're done.

You're right, validateOnAnyKey only exists on string fields. If you need a key listener on an Integer field you could create a custom gui component. Just extend the existing SwtScoutIntegerField and add a key listener in the initializeSwt() method. Now you only have to register this component with the extension point org.eclipse.scout.rt.ui.swt.formfields and map it to IIntegerField.

Hope it helps.
Claudio
Re: How to create UI fields at runtime? [message #902441 is a reply to message #895181] Fri, 17 August 2012 15:11 Go to previous messageGo to next message
Pietro Bonanno is currently offline Pietro Bonanno
Messages: 13
Registered: July 2009
Junior Member
I'm actually migrating a small app, I followed the Wiki's Notes but it keep to crash with a NPE at start.
Comparing my project with a test one created directly into 3.8, I found this difference in product's config.ini:
3.7:
osgi.bundles=org.eclipse.equinox.common@2:start,org.eclipse.update.configurator@3:start,org.eclipse.core.runtime@start

3.8:
osgi.bundles=org.eclipse.equinox.common@2\:start,org.eclipse.update.configurator@3\:start,org.eclipse.equinox.ds@3\:start,org.eclipse.core.runtime@start


When I updated it, my app started without errors! Smile
Now I have to solve only the lack of labels...

HTH

[Updated on: Fri, 17 August 2012 15:13]

Report message to a moderator

Re: How to create UI fields at runtime? [message #902753 is a reply to message #902441] Mon, 20 August 2012 09:26 Go to previous message
Claudio Guglielmo is currently offline Claudio Guglielmo
Messages: 127
Registered: March 2010
Senior Member
Hi Pietro

Have you seen this site http://wiki.eclipse.org/Scout/Migration/3.8? There are introductions to migrate your app. Have a look at the ticket concerning the textprovider services, maybe this helps you to fix your label problem.

Regards
Claudio
Previous Topic:Security in Juno
Next Topic:Rayo look and feel problem
Goto Forum:
  


Current Time: Fri Oct 24 08:31:35 GMT 2014

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

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