Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 itemis AG (http://www.itemis.eu) 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 /
  
  package org.eclipse.xtext.ui.editor.preferences.fields;
 
 import  org.eclipse.jface.preference.FieldEditor;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.events.DisposeEvent;
 import  org.eclipse.swt.events.DisposeListener;
 import  org.eclipse.swt.events.SelectionAdapter;
 import  org.eclipse.swt.events.SelectionEvent;
 import  org.eclipse.swt.graphics.Font;
 import  org.eclipse.swt.layout.GridData;
 import  org.eclipse.swt.layout.GridLayout;
 import  org.eclipse.swt.widgets.Button;
 import  org.eclipse.swt.widgets.Composite;
 import  org.eclipse.swt.widgets.Control;
 import  org.eclipse.swt.widgets.Group;

Author(s):
Dennis H´┐Żbner - Initial contribution and API
 
 public abstract class CheckBoxGroupFieldEditor extends FieldEditor {
 
 	private Composite checkBoxBox;
 	private final boolean useGroup;
 	private final String[][] labelsAndValues;
 	private final int numColumns;
 	private Button[] checkBoxButtons;
 	protected String result;
 
 	public CheckBoxGroupFieldEditor(String nameString labeltextint numColumnsString[][] labelsAndValues,
 			Composite fieldEditorParentboolean useGroup) {
 		init(namelabeltext);
 		this. = numColumns;
 		this. = labelsAndValues.clone();
 		this. = useGroup;
 		createControl(fieldEditorParent);
 	}

Parameters:
settings String[][fieldName,fieldValue,isChecked]
Returns:
value as String
 
 	protected abstract String calculateResult(String[][] settings);
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
 	 */
 	protected void adjustForNumColumns(int numColumns) {
 		Control control = getLabelControl();
 		if (control != null) {
 			((GridData) control.getLayoutData()).horizontalSpan = numColumns;
 		}
 		((GridData) .getLayoutData()).horizontalSpan = numColumns;
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see
 	 * org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt
 	 * .widgets.Composite, int)
 	 */
 	protected void doFillIntoGrid(Composite parentint numColumns) {
 		if () {
 			Control control = getCheckBoxControl(parent);
 			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
 			control.setLayoutData(gd);
 		}
 		else {
 			Control control = getLabelControl(parent);
 			GridData gd = new GridData();
 			gd.horizontalSpan = numColumns;
 			control.setLayoutData(gd);
 			control = getCheckBoxControl(parent);
 			gd = new GridData();
 			gd.horizontalSpan = numColumns;
 			control.setLayoutData(gd);
 		}
 	}
 
 	private Control getCheckBoxControl(Composite parent) {
 		if ( == null) {
 			Font font = parent.getFont();
 			if () {
 				Group group = new Group(parent, SWT.NONE);
 				group.setFont(font);
				String text = getLabelText();
				if (text != null) {
					group.setText(text);
				}
				 = group;
				GridLayout layout = new GridLayout();
				layout.horizontalSpacing = HORIZONTAL_GAP;
				layout.numColumns = ;
				.setLayout(layout);
			}
			else {
				 = new Composite(parent, SWT.NONE);
				GridLayout layout = new GridLayout();
				layout.marginWidth = 0;
				layout.marginHeight = 0;
				layout.horizontalSpacing = HORIZONTAL_GAP;
				layout.numColumns = ;
				.setLayout(layout);
				.setFont(font);
			}
			for (int i = 0; i < .i++) {
				Button checkBox = new Button(, SWT.CHECK | SWT.LEFT);
				[i] = checkBox;
				String[] labelAndValue = [i];
				checkBox.setText(labelAndValue[0]);
				checkBox.setData(labelAndValue[1]);
				checkBox.setFont(font);
				checkBox.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {
						String oldResult = ;
						fireValueChanged(VALUE, oldResult);
					}
				});
			}
			.addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent event) {
					 = null;
				}
			});
		}
		else {
			checkParent(parent);
		}
		return ;
	}
	private String gatherSettings() {
		String[][] settings = new String[.][3];
		for (int i = 0; i < .i++) {
			Button currentCheckBox = [i];
			String name = currentCheckBox.getText();
			String value = (StringcurrentCheckBox.getData();
			String checked = String.valueOf(currentCheckBox.getSelection());
			settings[i] = new String[] { namevaluechecked };
		}
		return calculateResult(settings);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.FieldEditor#doLoad()
	 */
	protected void doLoad() {
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
	 */
	protected void doLoadDefault() {
	}
	private void setupControls(String valueToSet) {
		for (int i = 0; i < .i++) {
			Button currentCheckBox = [i];
			currentCheckBox.setSelection(isSelected(currentCheckBox.getText(), valueToSet));
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.FieldEditor#doStore()
	 */
	protected void doStore() {
		if ( == null) {
			return;
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
	 */
	public int getNumberOfControls() {
		return 1;
	}
	public void setEnabled(boolean enabled, Composite parent) {
		if (!) {
			super.setEnabled(enabledparent);
		}
		for (Button button : ) {
			button.setEnabled(enabled);
		}
	}
	abstract protected boolean isSelected(String fieldNameString valueToSet);
New to GrepCode? Check out our FAQ X