/** | |
* <copyright> | |
* | |
* Copyright (c) 2012 itemis 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 | |
* | |
* Contributors: | |
* itemis - Initial API and implementation | |
* | |
* </copyright> | |
*/ | |
package org.eclipse.sphinx.emf.ui.wizards.pages; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.MissingResourceException; | |
import java.util.StringTokenizer; | |
import org.eclipse.emf.common.EMFPlugin; | |
import org.eclipse.jface.wizard.WizardPage; | |
import org.eclipse.sphinx.emf.ui.internal.Activator; | |
import org.eclipse.sphinx.emf.ui.internal.messages.Messages; | |
import org.eclipse.sphinx.emf.ui.wizards.AbstractModelNewWizard; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.events.ModifyEvent; | |
import org.eclipse.swt.events.ModifyListener; | |
import org.eclipse.swt.layout.GridData; | |
import org.eclipse.swt.layout.GridLayout; | |
import org.eclipse.swt.widgets.Combo; | |
import org.eclipse.swt.widgets.Composite; | |
import org.eclipse.swt.widgets.Label; | |
/** | |
* This is the page where the type of object to create is selected. | |
*/ | |
public class AbstractModelInitialObjectCreationPage extends WizardPage { | |
/** | |
* The handels the activator of the edit plugin of the considered metamodel | |
*/ | |
protected EMFPlugin editPlugin; | |
protected Combo initialObjectField; | |
protected List<String> encodings; | |
protected Combo encodingField; | |
/** | |
* Caches the names of the types that can be created as the root object. | |
*/ | |
protected Collection<String> initialObjectNames; | |
/** | |
* Constructor of the wizard's first page | |
* | |
* @param pageId | |
* @param objectNames | |
* returned by {@link AbstractModelNewWizard} | |
* @param plugin | |
* : The Edit plugin generated by EMF | |
*/ | |
public AbstractModelInitialObjectCreationPage(String pageId, Collection<String> objectNames, EMFPlugin plugin) { | |
super(pageId); | |
initialObjectNames = objectNames; | |
editPlugin = plugin; | |
} | |
@Override | |
public void createControl(Composite parent) { | |
Composite composite = new Composite(parent, SWT.NONE); | |
{ | |
GridLayout layout = new GridLayout(); | |
layout.numColumns = 1; | |
layout.verticalSpacing = 12; | |
composite.setLayout(layout); | |
GridData data = new GridData(); | |
data.verticalAlignment = GridData.FILL; | |
data.grabExcessVerticalSpace = true; | |
data.horizontalAlignment = GridData.FILL; | |
composite.setLayoutData(data); | |
} | |
Label containerLabel = new Label(composite, SWT.LEFT); | |
{ | |
containerLabel.setText(Messages._UI_ModelObject); | |
GridData data = new GridData(); | |
data.horizontalAlignment = GridData.FILL; | |
containerLabel.setLayoutData(data); | |
} | |
initialObjectField = new Combo(composite, SWT.BORDER); | |
{ | |
GridData data = new GridData(); | |
data.horizontalAlignment = GridData.FILL; | |
data.grabExcessHorizontalSpace = true; | |
initialObjectField.setLayoutData(data); | |
} | |
for (String objectName : initialObjectNames) { | |
initialObjectField.add(getLabel(objectName)); | |
} | |
if (initialObjectField.getItemCount() == 1) { | |
initialObjectField.select(0); | |
} | |
initialObjectField.addModifyListener(validator); | |
Label encodingLabel = new Label(composite, SWT.LEFT); | |
{ | |
encodingLabel.setText(Messages._UI_XMLEncoding); | |
GridData data = new GridData(); | |
data.horizontalAlignment = GridData.FILL; | |
encodingLabel.setLayoutData(data); | |
} | |
encodingField = new Combo(composite, SWT.BORDER); | |
{ | |
GridData data = new GridData(); | |
data.horizontalAlignment = GridData.FILL; | |
data.grabExcessHorizontalSpace = true; | |
encodingField.setLayoutData(data); | |
} | |
for (String encoding : getEncodings()) { | |
encodingField.add(encoding); | |
} | |
encodingField.select(0); | |
encodingField.addModifyListener(validator); | |
setPageComplete(validatePage()); | |
setControl(composite); | |
} | |
protected ModifyListener validator = new ModifyListener() { | |
@Override | |
public void modifyText(ModifyEvent e) { | |
setPageComplete(validatePage()); | |
} | |
}; | |
/** | |
* Validates initial page | |
* | |
* @return | |
*/ | |
protected boolean validatePage() { | |
return getInitialObjectName() != null && getEncodings().contains(encodingField.getText()); | |
} | |
@Override | |
public void setVisible(boolean visible) { | |
super.setVisible(visible); | |
if (visible) { | |
if (initialObjectField.getItemCount() == 1) { | |
initialObjectField.clearSelection(); | |
encodingField.setFocus(); | |
} else { | |
encodingField.clearSelection(); | |
initialObjectField.setFocus(); | |
} | |
} | |
} | |
/** | |
* Returns initial object names in the drop down list | |
* | |
* @return | |
*/ | |
public String getInitialObjectName() { | |
String label = initialObjectField.getText(); | |
for (String name : initialObjectNames) { | |
if (getLabel(name).equals(label)) { | |
return name; | |
} | |
} | |
return null; | |
} | |
public String getEncoding() { | |
return encodingField.getText(); | |
} | |
/** | |
* Obtain the default labels generated in the Edit plugin of the metamodel | |
* | |
* @param typeName | |
* @return | |
*/ | |
protected String getLabel(String typeName) { | |
try { | |
return editPlugin.getString("_UI_" + typeName + "_type"); //$NON-NLS-1$ //$NON-NLS-2$ | |
} catch (MissingResourceException mre) { | |
Activator.INSTANCE.log(mre); | |
} | |
return typeName; | |
} | |
protected Collection<String> getEncodings() { | |
if (encodings == null) { | |
encodings = new ArrayList<String>(); | |
for (StringTokenizer stringTokenizer = new StringTokenizer(Messages._UI_XMLEncodingChoices); stringTokenizer.hasMoreTokens();) { | |
encodings.add(stringTokenizer.nextToken()); | |
} | |
} | |
return encodings; | |
} | |
} |