Skip to content

Preferences and Properties

ljelinkova edited this page Aug 7, 2015 · 3 revisions

Preferences and properties looks in Eclipse UI very similar, the main difference is that preferences are stored on workbench level and properties are tight to some object - e.g. project or file. In fact, in Red Deer as well as in Eclipse, the PropertyDialog and PropertyPage are inherited from the PreferenceDialog and PreferencePage classes.

To start working with preferences (properties) in RD, you need to create an instance of a dialog. RD provides an implementation of PreferenceDialog called WorkbenchPreferenceDialog that uses Eclipse menu Window > Preferences to open it. Similarly, there is ExplorerItemPropertyDialog that opens PropertyDialog for given explorer item. These dialogs allows you to select preference page, to save its values or to cancel the dialog.

PreferenceDialog dialog = new WorkbenchPreferenceDialog();
dialog.open();

Once you have opened the dialog you need to instantiate the PreferencePage / PropertyPage and let the dialog to select it. Now you are ready to work with the concrete page that will allow you to apply / revert the values.

dialog.select(new RuntimePreferencePage());

Here is a small example of how to work with preferences.

package org.jboss.reddeer.snippet.preference;

import static org.junit.Assert.assertTrue;

import org.jboss.reddeer.eclipse.m2e.core.ui.preferences.MavenPreferencePage;
import org.jboss.reddeer.jface.preference.PreferenceDialog;
import org.jboss.reddeer.junit.runner.RedDeerSuite;
import org.jboss.reddeer.workbench.ui.dialogs.WorkbenchPreferenceDialog;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RedDeerSuite.class)
public class MavenPreferencePageTest {

	@Test
	public void test() {
		// create instances of dialog and page
		PreferenceDialog dialog = new WorkbenchPreferenceDialog();
		MavenPreferencePage page = new MavenPreferencePage();
		
		// open the dialog using Eclipse top menu
		dialog.open();
		// select page in the left menu
		dialog.select(page);
		
		// work with the page
		page.setDebugOutput(true);
		assertTrue(page.isDebugOutputChecked());
		
		// press OK button to store the values and close the dialog
		dialog.ok();
	}
}

source code

And how to work with properties.

package org.jboss.reddeer.snippet.preference;

import static org.junit.Assert.assertTrue;

import org.jboss.reddeer.eclipse.core.resources.Project;
import org.jboss.reddeer.eclipse.jdt.ui.packageexplorer.PackageExplorer;
import org.jboss.reddeer.eclipse.ui.dialogs.ExplorerItemPropertyDialog;
import org.jboss.reddeer.eclipse.ui.dialogs.PropertyDialog;
import org.jboss.reddeer.eclipse.wst.common.project.facet.ui.RuntimesPropertyPage;
import org.junit.Test;

public class RuntimesPropertyPageTest {

	private static final String PROJECT = "test-project";
	
	private static final String SERVER = "test-server";

	@Test
	public void selectRuntime() {
		// Create instances of dialog and page
		PropertyDialog dialog = new ExplorerItemPropertyDialog(getProject());
		RuntimesPropertyPage propertyPage = new RuntimesPropertyPage();

		// open the dialog using context menu of the project
		dialog.open();
		// select page in the left menu
		dialog.select(propertyPage);
		
		// work with the page
		propertyPage.selectRuntime(SERVER);
		assertTrue(propertyPage.getSelectedRuntimes().contains(SERVER));
		
		// press OK button to store the values and close the dialog
		dialog.ok();
	}

	private Project getProject() {
		PackageExplorer packageExplorer = new PackageExplorer();
		packageExplorer.open();
		return packageExplorer.getProject(PROJECT);
	}
}

source code

Clone this wiki locally