blob: 9b0a40d0ea52d1aeff18ea23632b8da4960ce7d9 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2011, 2018 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.internal.r.ui.wizards;
import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.statet.ecommons.databinding.core.util.DirtyTracker;
import org.eclipse.statet.ecommons.ui.util.LayoutUtil;
import org.eclipse.statet.r.core.rlang.RPkgNameValidator;
public class RPkgProjectWizardPage extends WizardPage {
private final NewRProjectWizardPage projectPage;
private Text pkgNameControl;
private DataBindingContext dbc;
private IObservableValue<String> pkgNameValue;
private boolean wasVisible;
private DirtyTracker pkgUserChanged;
public RPkgProjectWizardPage(final NewRProjectWizardPage projectPage) {
super("RPkgWizardPage"); //$NON-NLS-1$
setTitle(Messages.RPkgWizardPage_title);
setDescription(Messages.RPkgWizardPage_description);
this.projectPage= projectPage;
}
@Override
public void createControl(final Composite parent) {
initializeDialogUnits(parent);
final Composite composite= new Composite(parent, SWT.NONE);
composite.setLayout(LayoutUtil.createContentGrid(1));
{ final Composite group= createRPkgGroup(composite);
group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
}
Dialog.applyDialogFont(composite);
setControl(composite);
final Realm realm= Realm.getDefault();
this.dbc= new DataBindingContext(realm);
addBindings(this.dbc, realm);
WizardPageSupport.create(this, this.dbc);
}
protected Composite createRPkgGroup(final Composite parent) {
final Group composite= new Group(parent, SWT.NONE);
composite.setLayout(LayoutUtil.createGroupGrid(2));
composite.setText("R Package");
final Label label= new Label(composite, SWT.NONE);
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
label.setText("Package Name: ");
this.pkgNameControl= new Text(composite, SWT.LEFT | SWT.BORDER);
this.pkgNameControl.setFont(JFaceResources.getTextFont());
this.pkgNameControl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
return composite;
}
protected void addBindings(final DataBindingContext dbc, final Realm realm) {
this.pkgNameValue= new WritableValue<>(realm, "", String.class); //$NON-NLS-1$
this.pkgUserChanged= new DirtyTracker();
final Binding binding= dbc.bindValue(
WidgetProperties.text(SWT.Modify).observe(this.pkgNameControl),
this.pkgNameValue,
new UpdateValueStrategy().setAfterGetValidator(new RPkgNameValidator()),
null );
this.pkgUserChanged.add(binding);
}
@Override
public void setVisible(final boolean visible) {
super.setVisible(visible);
if (visible) {
final boolean firstTime= !this.wasVisible;
this.wasVisible= true;
if (!this.pkgUserChanged.isDirty() && this.projectPage != null) {
this.pkgNameControl.setText(this.projectPage.getProjectName());
this.pkgUserChanged.resetDirty();
}
if (firstTime) {
setMessage(null);
setErrorMessage(null);
}
}
}
public String getPkgName() {
return this.pkgNameValue.getValue();
}
}