blob: c66d5954160473ca9096be0d9e52b00f6543789f [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010 BSI Business Systems Integration AG.
* 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:
* BSI Business Systems Integration AG - initial API and implementation
******************************************************************************/
package org.eclipse.scout.sdk.ui.wizard.tablecolumn;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.Signature;
import org.eclipse.scout.commons.StringUtility;
import org.eclipse.scout.nls.sdk.model.INlsEntry;
import org.eclipse.scout.nls.sdk.model.workspace.project.INlsProject;
import org.eclipse.scout.sdk.Texts;
import org.eclipse.scout.sdk.extensions.runtime.classes.IRuntimeClasses;
import org.eclipse.scout.sdk.operation.form.field.table.TableColumnNewOperation;
import org.eclipse.scout.sdk.ui.fields.StyledTextField;
import org.eclipse.scout.sdk.ui.fields.buttongroup.ButtonGroup;
import org.eclipse.scout.sdk.ui.fields.buttongroup.IButtonGroupListener;
import org.eclipse.scout.sdk.ui.fields.proposal.ContentProposalEvent;
import org.eclipse.scout.sdk.ui.fields.proposal.IProposalAdapterListener;
import org.eclipse.scout.sdk.ui.fields.proposal.ProposalTextField;
import org.eclipse.scout.sdk.ui.fields.proposal.SiblingProposal;
import org.eclipse.scout.sdk.ui.internal.ScoutSdkUi;
import org.eclipse.scout.sdk.ui.wizard.AbstractWorkspaceWizardPage;
import org.eclipse.scout.sdk.ui.wizard.ScoutWizardDialog;
import org.eclipse.scout.sdk.ui.wizard.tablecolumn.TableColumnNewWizard.CONTINUE_OPERATION;
import org.eclipse.scout.sdk.util.NamingUtility;
import org.eclipse.scout.sdk.util.ScoutUtility;
import org.eclipse.scout.sdk.util.SdkProperties;
import org.eclipse.scout.sdk.util.signature.SignatureCache;
import org.eclipse.scout.sdk.util.type.TypeUtility;
import org.eclipse.scout.sdk.util.typecache.IWorkingCopyManager;
import org.eclipse.scout.sdk.workspace.type.IStructuredType;
import org.eclipse.scout.sdk.workspace.type.IStructuredType.CATEGORIES;
import org.eclipse.scout.sdk.workspace.type.ScoutTypeUtility;
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.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
/**
* <h3>DefaultTableColumnNewWizardPage</h3> ...
*/
public class DefaultTableColumnNewWizardPage extends AbstractWorkspaceWizardPage {
private INlsEntry m_nlsName;
private String m_typeName;
private String m_genericSignature;
private SiblingProposal m_sibling;
private CONTINUE_OPERATION m_continueOperation;
private ProposalTextField m_nlsNameField;
private StyledTextField m_typeNameField;
private ProposalTextField m_genericTypeField;
private ProposalTextField m_siblingField;
// process members
private final IType m_declaringType;
private IType m_superType;
private IType m_createdColumn;
public DefaultTableColumnNewWizardPage(IType declaringType, CONTINUE_OPERATION op) {
super(DefaultTableColumnNewWizardPage.class.getName());
setTitle(Texts.get("NewTableColumn"));
setDescription(Texts.get("CreateANewTableColumn"));
// default values
m_genericSignature = SignatureCache.createTypeSignature(Long.class.getName());
m_declaringType = declaringType;
m_sibling = SiblingProposal.SIBLING_END;
m_continueOperation = op;
}
@Override
protected void createContent(Composite parent) {
createFieldGroup(parent);
createNextStepsGroup(parent);
parent.setLayout(new GridLayout(1, false));
}
private void createNextStepsGroup(Composite p) {
Group g = new Group(p, SWT.NONE);
g.setText(Texts.get("NextStep"));
g.setLayout(new GridLayout(1, false));
g.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
ButtonGroup<CONTINUE_OPERATION> nextStepOptions = new ButtonGroup<CONTINUE_OPERATION>(g, SWT.RADIO);
nextStepOptions.createButton(Texts.get("CreateMoreColumn"), CONTINUE_OPERATION.ADD_MORE_COLUMNS);
nextStepOptions.createButton(Texts.get("FinishWizard"), CONTINUE_OPERATION.FINISH);
nextStepOptions.addButtonGroupListener(new IButtonGroupListener<CONTINUE_OPERATION>() {
@Override
public void handleSelectionChanged(List<CONTINUE_OPERATION> newSelection) {
m_continueOperation = newSelection.get(0);
}
});
nextStepOptions.setValue(m_continueOperation);
nextStepOptions.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
}
private void createFieldGroup(Composite p) {
Group fieldGroup = new Group(p, SWT.NONE);
fieldGroup.setText(Texts.get("Column"));
INlsProject nlsProject = ScoutTypeUtility.findNlsProject(m_declaringType);
m_nlsNameField = getFieldToolkit().createNlsProposalTextField(fieldGroup, nlsProject, Texts.get("PropHeaderText"));
m_nlsNameField.acceptProposal(m_nlsName);
m_nlsNameField.addProposalAdapterListener(new IProposalAdapterListener() {
@Override
public void proposalAccepted(ContentProposalEvent event) {
try {
setStateChanging(true);
INlsEntry oldEntry = getNlsName();
m_nlsName = (INlsEntry) event.proposal;
if (m_nlsName != null) {
if (oldEntry == null || oldEntry.getKey().equals(m_typeNameField.getModifiableText()) || StringUtility.isNullOrEmpty(m_typeNameField.getModifiableText())) {
m_typeNameField.setText(NamingUtility.toJavaCamelCase(m_nlsName.getKey(), false));
}
}
}
finally {
setStateChanging(false);
}
}
});
m_typeNameField = getFieldToolkit().createStyledTextField(fieldGroup, Texts.get("TypeName"));
m_typeNameField.setReadOnlySuffix(SdkProperties.SUFFIX_TABLE_COLUMN);
m_typeNameField.setText(m_typeName);
m_typeNameField.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
m_typeName = m_typeNameField.getText();
pingStateChanging();
}
});
m_genericTypeField = getFieldToolkit().createSignatureProposalField(fieldGroup, Texts.get("GenericType"), ScoutTypeUtility.getScoutBundle(m_declaringType));
m_genericTypeField.acceptProposal(getGenericSignature());
m_genericTypeField.setEnabled(TypeUtility.isGenericType(getSuperType()));
m_genericTypeField.addProposalAdapterListener(new IProposalAdapterListener() {
@Override
public void proposalAccepted(ContentProposalEvent event) {
m_genericSignature = (String) event.proposal;
pingStateChanging();
}
});
m_siblingField = getFieldToolkit().createSiblingProposalField(fieldGroup, m_declaringType, TypeUtility.getType(IRuntimeClasses.IColumn));
m_siblingField.acceptProposal(m_sibling);
m_siblingField.addProposalAdapterListener(new IProposalAdapterListener() {
@Override
public void proposalAccepted(ContentProposalEvent event) {
m_sibling = (SiblingProposal) event.proposal;
pingStateChanging();
}
});
// layout
fieldGroup.setLayout(new GridLayout(1, false));
fieldGroup.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
m_nlsNameField.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
m_typeNameField.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
m_genericTypeField.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
m_siblingField.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
}
@Override
public boolean performFinish(IProgressMonitor monitor, IWorkingCopyManager workingCopyManager) throws CoreException {
if (CONTINUE_OPERATION.ADD_MORE_COLUMNS == m_continueOperation) {
// start another wizard if one additional column should be created.
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
TableColumnNewWizard wizard = new TableColumnNewWizard(m_continueOperation);
wizard.initWizard(m_declaringType);
ScoutWizardDialog wizardDialog = new ScoutWizardDialog(wizard);
wizardDialog.open();
}
});
}
TableColumnNewOperation operation = new TableColumnNewOperation(getTypeName(), m_declaringType, true);
// write back members
IType superTypeProp = getSuperType();
if (superTypeProp != null) {
String sig = null;
if (getGenericSignature() != null) {
sig = SignatureCache.createTypeSignature(superTypeProp.getFullyQualifiedName() + "<" + Signature.toString(getGenericSignature()) + ">");
}
else {
sig = SignatureCache.createTypeSignature(superTypeProp.getFullyQualifiedName());
}
operation.setSuperTypeSignature(sig);
}
operation.setNlsEntry(getNlsName());
if (getSibling() == SiblingProposal.SIBLING_END) {
IStructuredType structuredType = ScoutTypeUtility.createStructuredTable(m_declaringType);
operation.setSibling(structuredType.getSibling(CATEGORIES.TYPE_COLUMN));
}
else {
operation.setSibling(getSibling().getElement());
}
operation.validate();
operation.run(monitor, workingCopyManager);
m_createdColumn = operation.getCreatedColumn();
return true;
}
@Override
protected void validatePage(MultiStatus multiStatus) {
multiStatus.add(getStatusNameField());
multiStatus.add(getStatusGenericType());
if (isControlCreated()) {
m_genericTypeField.setEnabled(TypeUtility.isGenericType(getSuperType()));
}
}
protected IStatus getStatusNameField() {
IStatus javaFieldNameStatus = ScoutUtility.validateJavaName(getTypeName(), SdkProperties.SUFFIX_TABLE_COLUMN);
if (javaFieldNameStatus.getSeverity() > IStatus.WARNING) {
return javaFieldNameStatus;
}
if (TypeUtility.exists(m_declaringType.getType(getTypeName()))) {
return new Status(IStatus.ERROR, ScoutSdkUi.PLUGIN_ID, Texts.get("Error_nameAlreadyUsed"));
}
return javaFieldNameStatus;
}
/**
* @return the createdColumn
*/
public IType getCreatedColumn() {
return m_createdColumn;
}
protected IStatus getStatusGenericType() {
if (TypeUtility.isGenericType(getSuperType())) {
if (getGenericSignature() == null) {
return new Status(IStatus.ERROR, ScoutSdkUi.PLUGIN_ID, "Generic type can not be null!");
}
}
return Status.OK_STATUS;
}
public void setSuperType(IType superType) {
try {
setStateChanging(true);
if (TypeUtility.isGenericType(getSuperType())) {
setGenericSignature(SignatureCache.createTypeSignature(Long.class.getName()));
}
else {
setGenericSignature(null);
}
m_superType = superType;
}
finally {
setStateChanging(false);
}
}
public IType getSuperType() {
return m_superType;
}
public INlsEntry getNlsName() {
return m_nlsName;
}
public void setNlsName(INlsEntry nlsName) {
try {
setStateChanging(true);
m_nlsName = nlsName;
if (isControlCreated()) {
m_nlsNameField.acceptProposal(nlsName);
}
}
finally {
setStateChanging(false);
}
}
public String getTypeName() {
return m_typeName;
}
public void setTypeName(String typeName) {
try {
setStateChanging(true);
m_typeName = typeName;
if (isControlCreated()) {
m_typeNameField.setText(typeName);
}
}
finally {
setStateChanging(false);
}
}
public void setGenericSignature(String genericSignature) {
try {
setStateChanging(true);
m_genericSignature = genericSignature;
if (isControlCreated()) {
m_genericTypeField.acceptProposal(genericSignature);
}
}
finally {
setStateChanging(false);
}
}
public String getGenericSignature() {
return m_genericSignature;
}
public SiblingProposal getSibling() {
return m_sibling;
}
public void setSibling(SiblingProposal sibling) {
try {
setStateChanging(true);
m_sibling = sibling;
if (isControlCreated()) {
m_siblingField.acceptProposal(sibling);
}
}
finally {
setStateChanging(false);
}
}
}