blob: 809b6848f9974cef47510b852f04d28e7ffde9dd [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* 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
*
* Contributors:
* Pierre Allard,
* Regent L'Archeveque,
<<<<<<< HEAD
* Olivier L. Larouche
*
=======
* Olivier L. Larouche - initial API and implementation
>>>>>>> refs/heads/eclipse_pa
* SPDX-License-Identifier: EPL-1.0
*
*******************************************************************************/
package org.eclipse.apogy.common.emf.ui.preferences;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.apogy.common.emf.ui.Activator;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateListStrategy;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
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.Color;
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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.plugin.AbstractUIPlugin;
public class NativeDecimalFormatComposite extends Composite {
private DataBindingContext bindingContext;
private final Text doubleText;
private final Text floatText;
private final Text byteText;
private final Text shortText;
private final Text intText;
private final Text longText;
private final List<Text> textList;
public NativeDecimalFormatComposite(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(2, false));
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (NativeDecimalFormatComposite.this.bindingContext != null) {
NativeDecimalFormatComposite.this.bindingContext.dispose();
}
}
});
this.textList = new ArrayList<Text>();
/** Synthax information button */
Button infoButton = new Button(this, SWT.None);
infoButton.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true, 2, 1));
ImageDescriptor image = AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipse.jface",
"/icons/full/message_info.png");
infoButton.setImage(image.createImage());
infoButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String message = "0\trepresents a digit\n" + "#\trepresents a digit, zero shows as absent\n"
+ ".\trepresents a placeholder for decimal separator\n"
+ ",\trepresents a placeholder for grouping separator\n\n" + "Examples with input 123123.123:\n"
+ "\t- 000000000.000000 : " + new DecimalFormat("000000000.000000").format(123123.123)
+ "\n\t- #########.###### : " + new DecimalFormat("#########.######").format(123123.123)
+ "\n\t- ###,###.### : " + new DecimalFormat("###,###.###").format(123123.123);
MessageDialog.openInformation(getShell(), "Synthax", message);
}
});
/** Double */
Label doubleLabel = new Label(this, SWT.None);
doubleLabel.setText("Double float :");
doubleLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.doubleText = new Text(this, SWT.BORDER);
this.doubleText.setText(" ");
this.doubleText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.textList.add(this.doubleText);
/** Float */
Label floatLabel = new Label(this, SWT.None);
floatLabel.setText("Floating point :");
floatLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.floatText = new Text(this, SWT.BORDER);
this.floatText.setText(" ");
this.floatText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.textList.add(this.floatText);
/** Byte */
Label byteLabel = new Label(this, SWT.None);
byteLabel.setText("Byte :");
byteLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.byteText = new Text(this, SWT.BORDER);
this.byteText.setText(" ");
this.byteText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.textList.add(this.byteText);
/** Short */
Label shortLabel = new Label(this, SWT.None);
shortLabel.setText("Short integer :");
shortLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.shortText = new Text(this, SWT.BORDER);
this.shortText.setText(" ");
this.shortText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.textList.add(this.shortText);
/** Int */
Label intLabel = new Label(this, SWT.None);
intLabel.setText("Integer :");
intLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.intText = new Text(this, SWT.BORDER);
this.intText.setText(" ");
this.intText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.textList.add(this.intText);
/** Long */
Label longLabel = new Label(this, SWT.None);
longLabel.setText("Long integer :");
longLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
this.longText = new Text(this, SWT.BORDER);
this.longText.setText(" ");
this.longText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
this.textList.add(this.longText);
updateTexts();
initDataBindings();
}
/**
* Updates all the texts.
*/
public void updateTexts() {
this.doubleText.setText(
Activator.getDefault().getPreferenceStore().getString(PreferencesConstants.NATIVE_FORMAT_DOUBLE_ID));
this.floatText.setText(
Activator.getDefault().getPreferenceStore().getString(PreferencesConstants.NATIVE_FORMAT_FLOAT_ID));
this.byteText.setText(
Activator.getDefault().getPreferenceStore().getString(PreferencesConstants.NATIVE_FORMAT_BYTE_ID));
this.shortText.setText(
Activator.getDefault().getPreferenceStore().getString(PreferencesConstants.NATIVE_FORMAT_SHORT_ID));
this.intText.setText(
Activator.getDefault().getPreferenceStore().getString(PreferencesConstants.NATIVE_FORMAT_INT_ID));
this.longText.setText(
Activator.getDefault().getPreferenceStore().getString(PreferencesConstants.NATIVE_FORMAT_LONG_ID));
}
private void initDataBindings() {
if (this.bindingContext != null) {
this.bindingContext.dispose();
}
this.bindingContext = new DataBindingContext();
/** Update value strategies */
UpdateValueStrategy policyNever = new UpdateValueStrategy(UpdateListStrategy.POLICY_NEVER);
UpdateValueStrategy validFormatUpdateStrategy = new UpdateValueStrategy(UpdateListStrategy.POLICY_UPDATE)
.setConverter(new Converter(String.class, Color.class) {
@Override
public Object convert(Object fromObject) {
if (fromObject != null && isValid((String) fromObject)) {
return getDisplay().getSystemColor(SWT.COLOR_TRANSPARENT);
}
return getDisplay().getSystemColor(SWT.COLOR_RED);
}
});
/** Bind the text with the background for each text */
for (Text text : this.textList) {
IObservableValue<?> textText = WidgetProperties.text(SWT.Modify).observe(text);
IObservableValue<?> backgroundText = WidgetProperties.background().observe(text);
this.bindingContext.bindValue(backgroundText, textText, policyNever, validFormatUpdateStrategy);
}
}
/**
* Used to save the content of the texts boxes in the preferences if their input
* is valid.
*/
public void savePreferences() {
if (isValid(this.doubleText.getText())) {
Activator.getDefault().getPreferenceStore().setValue(PreferencesConstants.NATIVE_FORMAT_DOUBLE_ID,
this.doubleText.getText());
}
if (isValid(this.floatText.getText())) {
Activator.getDefault().getPreferenceStore().setValue(PreferencesConstants.NATIVE_FORMAT_FLOAT_ID,
this.floatText.getText());
}
if (isValid(this.byteText.getText())) {
Activator.getDefault().getPreferenceStore().setValue(PreferencesConstants.NATIVE_FORMAT_BYTE_ID,
this.byteText.getText());
}
if (isValid(this.shortText.getText())) {
Activator.getDefault().getPreferenceStore().setValue(PreferencesConstants.NATIVE_FORMAT_SHORT_ID,
this.shortText.getText());
}
if (isValid(this.intText.getText())) {
Activator.getDefault().getPreferenceStore().setValue(PreferencesConstants.NATIVE_FORMAT_INT_ID,
this.intText.getText());
}
if (isValid(this.longText.getText())) {
Activator.getDefault().getPreferenceStore().setValue(PreferencesConstants.NATIVE_FORMAT_LONG_ID,
this.longText.getText());
}
}
/**
* Tries to create a {@link DecimalFormat} with the specified {@link String}.
*
* @param str {@link String} to use as pattern for the created
* {@link DecimalFormat}.
* @return true if a {@link DecimalFormat} can be created, false otherwise.
*/
private boolean isValid(String str) {
if ("".equals(str)) {
return false;
}
for (int i = 0; i < str.length(); i++) {
char character = str.charAt(i);
if (character != '0' && character != '.' && character != '#' && character != ',') {
return false;
}
}
try {
new DecimalFormat(str);
return true;
} catch (Exception e) {
return false;
}
}
}