Java tutorial
/******************************************************************************* * Copyright (c) 2014 AtoS * 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: * Sebastien Lemanceau (AtoS) - initial API and implementation and/or initial documentation *******************************************************************************/ package org.polarsys.reqcycle.repository.connector.local.ui; import java.util.Collections; import javax.inject.Inject; import org.eclipse.core.databinding.DataBindingContext; import org.eclipse.core.databinding.beans.PojoProperties; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IPath; import org.eclipse.emf.common.util.URI; import org.eclipse.jface.databinding.swt.WidgetProperties; import org.eclipse.jface.databinding.viewers.ViewerProperties; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ComboViewer; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.window.Window; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.dialogs.ContainerSelectionDialog; import org.polarsys.reqcycle.repository.data.IDataModelManager; import org.polarsys.reqcycle.repository.data.ScopeConf.Scope; import org.polarsys.reqcycle.repository.data.types.IDataModel; import org.polarsys.reqcycle.utils.inject.ZigguratInject; public class LocalSettingPage extends WizardPage implements Listener { private DataBindingContext m_bindingContext; private ComboViewer cvDataModel; private Combo cDataModel; private ComboViewer cvScope; private Combo cScope; private Label lblCopyFile; private Text txtFile; private Button btnBrowseDestinationFile; public Bean bean; @Inject IDataModelManager dataModelManager; public LocalSettingPage(String pageName) { super(pageName); bean = new Bean(this); ZigguratInject.inject(this); } @Override public void createControl(Composite parent) { Composite compositeMain = new Composite(parent, SWT.NONE); compositeMain.setLayout(new GridLayout(3, false)); compositeMain.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); setControl(compositeMain); Label lblDataModel = new Label(compositeMain, SWT.NONE); lblDataModel.setText("Data Model :"); cvDataModel = new ComboViewer(compositeMain); cDataModel = cvDataModel.getCombo(); cDataModel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); cvDataModel.setContentProvider(ArrayContentProvider.getInstance()); cvDataModel.setLabelProvider(new LabelProvider() { @Override public String getText(Object element) { if (element instanceof IDataModel) { return ((IDataModel) element).getName(); } return super.getText(element); } }); cvDataModel.setInput(dataModelManager.getCurrentDataModels()); Label lblScope = new Label(compositeMain, SWT.NONE); lblScope.setText("Scope :"); cvScope = new ComboViewer(compositeMain); cScope = cvScope.getCombo(); cScope.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); cScope.setEnabled(false); cvScope.setContentProvider(ArrayContentProvider.getInstance()); cvScope.setLabelProvider(new LabelProvider() { @Override public String getText(Object element) { if (element instanceof Scope) { return ((Scope) element).getName(); } return super.getText(element); } }); lblCopyFile = new Label(compositeMain, SWT.NONE); lblCopyFile.setText("Repository Folder :"); txtFile = new Text(compositeMain, SWT.BORDER); txtFile.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); txtFile.setEnabled(false); btnBrowseDestinationFile = new Button(compositeMain, SWT.NONE); btnBrowseDestinationFile.setText("Browse"); hookListeners(); m_bindingContext = initDataBindings(); } protected void hookListeners() { cvDataModel.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { cvScope.setInput(Collections.emptyList()); cScope.setEnabled(false); ISelection selection = event.getSelection(); if (selection instanceof IStructuredSelection) { Object obj = ((IStructuredSelection) selection).getFirstElement(); if (obj instanceof IDataModel) { cScope.setEnabled(true); cvScope.setInput(dataModelManager.getScopes((IDataModel) obj)); } } cvScope.refresh(); } }); btnBrowseDestinationFile.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), true, "select location for save"); if (Window.OK == dialog.open()) { Object[] result = dialog.getResult(); if (result != null && result.length > 0) { if (result[0] instanceof IPath) { IPath path = (IPath) result[0]; txtFile.setText(URI.createPlatformResourceURI( path.append(bean.getFileName()).addFileExtension("reqcycle").toString(), true) .toString()); } } } } }); } public Bean getBean() { return this.bean; } public static class Bean { private IDataModel dataModel; private Scope scope; private String destination; private Listener listener; private String fileName; public Bean(Listener listener) { this.listener = listener; } public IDataModel getDataModel() { return dataModel; } public void setDataModel(IDataModel dataModel) { this.dataModel = dataModel; listener.handleEvent(new Event()); } public Scope getScope() { return scope; } public void setScope(Scope scope) { this.scope = scope; listener.handleEvent(new Event()); } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; listener.handleEvent(new Event()); } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } } protected DataBindingContext initDataBindings() { DataBindingContext bindingContext = new DataBindingContext(); // IObservableValue observeSingleSelectionCvDataModel = ViewerProperties.singleSelection() .observe(cvDataModel); IObservableValue dataModelBeanObserveValue = PojoProperties.value("dataModel").observe(bean); bindingContext.bindValue(observeSingleSelectionCvDataModel, dataModelBeanObserveValue, null, null); // IObservableValue observeSingleSelectionCvScope = ViewerProperties.singleSelection().observe(cvScope); IObservableValue scopeBeanObserveValue = PojoProperties.value("scope").observe(bean); bindingContext.bindValue(observeSingleSelectionCvScope, scopeBeanObserveValue, null, null); // IObservableValue observeTextTxtFileObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtFile); IObservableValue modelPathBeanObserveValue = PojoProperties.value("destination").observe(bean); bindingContext.bindValue(observeTextTxtFileObserveWidget, modelPathBeanObserveValue, null, null); // return bindingContext; } @Override public boolean isPageComplete() { StringBuffer error = new StringBuffer(); boolean result = true; if (bean.getDataModel() == null) { error.append("Choose a Data Model\n"); result = false; } if (bean.getScope() == null) { error.append("Choose a Scope\n"); result = false; } if (bean.getDestination() == null || bean.getDestination().isEmpty()) { error.append("Choose a destination repository"); result = false; } if (!result) { setErrorMessage(error.toString()); } else { setErrorMessage(null); } return result; } @Override public void handleEvent(Event event) { getContainer().updateButtons(); } public void setFileName(String fileName) { bean.setFileName(fileName); } }