| /******************************************************************************* |
| * Copyright (c) 2004-2008 Istvan Rath and Daniel Varro |
| * 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: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.frameworkgui.wizards.ui;
|
|
|
| import java.util.ArrayList; |
| |
| import org.eclipse.jface.viewers.ColumnWeightData; |
| import org.eclipse.jface.viewers.ICellModifier; |
| import org.eclipse.jface.viewers.ILabelProviderListener; |
| import org.eclipse.jface.viewers.ISelectionChangedListener; |
| import org.eclipse.jface.viewers.IStructuredContentProvider; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.ITableLabelProvider; |
| import org.eclipse.jface.viewers.LabelProvider; |
| import org.eclipse.jface.viewers.ListViewer; |
| import org.eclipse.jface.viewers.SelectionChangedEvent; |
| import org.eclipse.jface.viewers.TableLayout; |
| import org.eclipse.jface.viewers.TableViewer; |
| import org.eclipse.jface.viewers.TextCellEditor; |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.jface.wizard.WizardPage; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.SashForm; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.TableColumn; |
| import org.eclipse.swt.widgets.TableItem; |
| import org.eclipse.viatra2.frameworkgui.FrameworkGUIPlugin; |
| import org.eclipse.viatra2.frameworkgui.wizards.model.FileTemplateExtension; |
| import org.eclipse.viatra2.frameworkgui.wizards.model.FileTemplateExtension.TemplateParameter; |
|
|
| public class TemplateSelectionPage extends WizardPage {
|
|
|
|
|
| String extension;
|
|
|
| public TemplateSelectionPage(String aKind)
|
| {
|
| super("Select a "+aKind.toUpperCase()+" template");
|
| extension = aKind;
|
| }
|
|
|
| protected ListViewer templateList;
|
|
|
| protected TableViewer parameterTable;
|
|
|
| protected FileTemplateExtension template;
|
|
|
|
|
| public void createControl(Composite parent) {
|
| SashForm sash = new SashForm(parent, SWT.BORDER|SWT.VERTICAL);
|
|
|
| templateList = new ListViewer(sash, SWT.SINGLE);
|
| templateList.setLabelProvider(new LabelProvider());
|
| templateList.setContentProvider(new IStructuredContentProvider(){
|
|
|
| public void dispose() {}
|
|
|
| public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
|
|
|
| public Object[] getElements(Object inputElement)
|
| {
|
| ArrayList<FileTemplateExtension> r = new ArrayList<FileTemplateExtension>();
|
| for (FileTemplateExtension ex : FrameworkGUIPlugin.getDefault().getFileTemplates())
|
| {
|
| if (ex.extension.toLowerCase().equals(TemplateSelectionPage.this.extension.toLowerCase()))
|
| {
|
| r.add(ex);
|
| }
|
| }
|
| return r.toArray();
|
| }
|
| });
|
| templateList.setInput(FrameworkGUIPlugin.getDefault());
|
| templateList.addSelectionChangedListener(new ISelectionChangedListener(){
|
|
|
| public void selectionChanged(SelectionChangedEvent event)
|
| {
|
| if (event.getSelection() instanceof IStructuredSelection)
|
| {
|
| Object o = ((IStructuredSelection)event.getSelection()).getFirstElement();
|
| if (o instanceof FileTemplateExtension)
|
| {
|
| template = (FileTemplateExtension)o;
|
| parameterTable.setInput(template);
|
| }
|
| }
|
| }
|
| });
|
|
|
| parameterTable = new TableViewer(sash, SWT.DEFAULT);
|
| TableLayout tlay = new TableLayout();
|
| tlay.addColumnData(new ColumnWeightData(30, true));
|
| tlay.addColumnData(new ColumnWeightData(30, true));
|
| tlay.addColumnData(new ColumnWeightData(40, true));
|
| parameterTable.getTable().setLayout(tlay);
|
| TableColumn keyC =new TableColumn(parameterTable.getTable(),SWT.LEFT);
|
| keyC.setText("Parameter name");
|
| TableColumn defvalC =new TableColumn(parameterTable.getTable(),SWT.LEFT);
|
| defvalC.setText("Default Value");
|
| TableColumn valC =new TableColumn(parameterTable.getTable(),SWT.LEFT);
|
| valC.setText("Actual Value");
|
|
|
| parameterTable.getTable().setHeaderVisible(true);
|
| parameterTable.getTable().setLinesVisible(true);
|
| parameterTable.setLabelProvider(new ITableLabelProvider(){
|
|
|
| public Image getColumnImage(Object element, int columnIndex)
|
| {
|
| return null;
|
| }
|
|
|
|
|
| public String getColumnText(Object element, int columnIndex) {
|
| switch (columnIndex)
|
| {
|
| case 0: // display parameter name
|
| return ((FileTemplateExtension.TemplateParameter)element).name;
|
| case 1: // default value
|
| return ((FileTemplateExtension.TemplateParameter)element).defaultValue;
|
| default:
|
| case 2:
|
| return ((FileTemplateExtension.TemplateParameter)element).value;
|
| }
|
| }
|
|
|
|
|
| public void addListener(ILabelProviderListener listener) {}
|
|
|
|
|
| public void dispose() {}
|
|
|
|
|
| public boolean isLabelProperty(Object element, String property) {return false;}
|
|
|
|
|
| public void removeListener(ILabelProviderListener listener) {}
|
|
|
| });
|
| parameterTable.setContentProvider(new IStructuredContentProvider(){
|
|
|
|
|
| public Object[] getElements(Object element) {
|
| Object t = element;
|
| if (element instanceof IStructuredSelection)
|
| {
|
| t = ((IStructuredSelection)element).getFirstElement();
|
| }
|
|
|
| if (t instanceof FileTemplateExtension)
|
| {
|
| return ((FileTemplateExtension)t).params.toArray();
|
| }
|
|
|
| return null;
|
| }
|
|
|
|
|
| public void dispose() {}
|
|
|
|
|
| public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
|
|
|
| });
|
| // TODO add cell editing support
|
| parameterTable.setCellEditors(new TextCellEditor[]
|
| {
|
| new TextCellEditor(parameterTable.getTable()),
|
| new TextCellEditor(parameterTable.getTable()),
|
| new TextCellEditor(parameterTable.getTable())
|
| }
|
| );
|
| parameterTable.setColumnProperties(new String[]{"name","def","value"});
|
| parameterTable.setCellModifier(new ICellModifier(){
|
|
|
|
|
| public boolean canModify(Object element, String property)
|
| {
|
| return "value".equals(property);
|
| }
|
|
|
|
|
| public Object getValue(Object element, String property)
|
| {
|
| if ("value".equals(property))
|
| return ((FileTemplateExtension.TemplateParameter)element).value;
|
| return null;
|
| }
|
|
|
|
|
| public void modify(Object element, String property, Object value)
|
| {
|
| if ("value".equals(property))
|
| {
|
| FileTemplateExtension.TemplateParameter p = null;
|
| if (element instanceof TableItem)
|
| {
|
| p = (TemplateParameter) ((TableItem)element).getData();
|
| }
|
| else
|
| p = (TemplateParameter) element;
|
|
|
| template.params.remove(p);
|
| TemplateParameter _p = template.new TemplateParameter();
|
| _p.name = p.name;
|
| _p.value = value.toString();
|
| _p.defaultValue = p.defaultValue;
|
| _p.description = p.description;
|
| template.params.add(_p);
|
|
|
| parameterTable.refresh();
|
| }
|
| }
|
|
|
| });
|
|
|
| setDescription("Select the template to use for the new file");
|
| sash.setWeights(new int[]{2,1});
|
| setControl(sash);
|
| }
|
|
|
|
|
| public boolean isPageComplete()
|
| {
|
| return template!=null;
|
| }
|
|
|
| public FileTemplateExtension getSelectedTemplate()
|
| {
|
| return template;
|
| }
|
|
|
|
|
| }
|