this.okCommand = okCommand;
}
public void show() {
final FormStylePopup pop = new FormStylePopup();
pop.setTitle( constants.Name() );
HorizontalPanel changeName = new HorizontalPanel();
final TextBox name = new TextBox();
name.setText( factModel.getName() );
changeName.add( name );
int selectedIndex = 0;
lstSuperTypes.addItem( constants.DoesNotExtend() );
for ( FactMetaModel fmm : factModels ) {
if ( !fmm.getName().equals( factModel.getName() ) ) {
lstSuperTypes.addItem( fmm.getName() );
if ( factModel.getSuperType() != null && factModel.getSuperType().equals( fmm.getName() ) ) {
selectedIndex = lstSuperTypes.getItemCount() - 1;
}
}
}
lstSuperTypes.setSelectedIndex( selectedIndex );
if ( lstSuperTypes.getItemCount() == 1 ) {
lstSuperTypes.setEnabled( false );
}
lstSuperTypes.addChangeHandler( new ChangeHandler() {
public void onChange(ChangeEvent event) {
if ( lstSuperTypes.getSelectedIndex() <= 0 ) {
factModel.setSuperType( null );
} else {
String oldSuperType = factModel.getSuperType();
String newSuperType = lstSuperTypes.getItemText( lstSuperTypes.getSelectedIndex() );
factModel.setSuperType( newSuperType );
if ( createsCircularDependency( newSuperType ) ) {
Window.alert( constants.CreatesCircularDependency( name.getText() ) );
factModel.setSuperType( oldSuperType );
lstSuperTypes.setSelectedIndex( getSelectedIndex( oldSuperType ) );
return;
} else {
factModel.setSuperType( newSuperType );
}
}
}
} );
Button nameButton = new Button( constants.OK() );
nameButton.addKeyPressHandler( new NoSpaceKeyPressHandler() );
nameButton.addClickHandler( new ClickHandler() {
public void onClick(ClickEvent event) {
if ( doesTheNameExist() ) {
Window.alert( constants.NameTakenForModel( name.getText() ) );
return;
}
if ( factModelAlreadyHasAName( name.getText() ) ) {
if ( isTheUserSureHeWantsToChangeTheName() ) {
setNameAndClose();
}
} else {
setNameAndClose();
}
}
private boolean factModelAlreadyHasAName(String name) {
return factModel.getName() != null && !factModel.getName().equals( name );
}
private void setNameAndClose() {
String oldName = factModel.getName();
String newName = name.getText();
modelNameHelper.changeNameInModelNameHelper( oldName,
newName );
factModel.setName( newName );
okCommand.execute();
pop.hide();
}
private boolean isTheUserSureHeWantsToChangeTheName() {
return Window.confirm( constants.ModelNameChangeWarning() );
}
private boolean doesTheNameExist() {
if ( factModel.getName() == null ) {
return false;
}
//The name may not have changed
if ( factModel.getName().equals( name.getText() ) ) {
return false;
}
return !modelNameHelper.isUniqueName( name.getText() );
}
} );
pop.addAttribute( constants.Name(),
changeName );
pop.addAttribute( constants.TypeExtends(),
lstSuperTypes );
pop.addRow( nameButton );
pop.show();
}