/**
* Attempt to use the current avatar. Close the window if so.
*/
private void use() {
final AvatarRegistry registry = AvatarRegistry.getAvatarRegistry();
// Make sure the name text field is not empty.
final String newAvatarName = nameTextField.getText().trim();
if (newAvatarName == null || newAvatarName.equals("") == true) {
String msg = BUNDLE.getString("PLEASE_ENTER_AVATAR_NAME");
String title = BUNDLE.getString("AVATAR_NAME");
JOptionPane.showMessageDialog(this, msg, title, JOptionPane.ERROR_MESSAGE);
return;
}
// Make sure there are no spaces in the avatar name.
// XXX Workaround for bug in content repo XXX
if (newAvatarName.indexOf(" ") != -1) {
String msg = BUNDLE.getString("AVATAR_NAME_SPACES");
String title = BUNDLE.getString("AVATAR_NAME");
JOptionPane.showMessageDialog(this, msg, title, JOptionPane.ERROR_MESSAGE);
return;
}
// Check to see that the avatar name is not already taken. We only check
// if the name has actually changed.
AvatarSPI oldAvatar = registry.getAvatarByName(newAvatarName);
if (newAvatarName.equals(originalAvatarName) == false && oldAvatar != null) {
String msg = BUNDLE.getString("THE_AVATAR_NAME_TAKEN");
msg = MessageFormat.format(msg, newAvatarName);
String title = BUNDLE.getString("AVATAR_NAME");
JOptionPane.showMessageDialog(this, msg, title, JOptionPane.ERROR_MESSAGE);
return;
}
// If we are not changing the name of the avatar, then we just save the
// avatar, close the window and return. We do this in a thread to make
// sure the UI does not block without indication.
if (newAvatarName.equals(originalAvatarName) == true) {
setBusy(true);
new Thread() {
@Override
public void run() {
avatar.setAvatarParams(currentParams);
save(avatar);
registry.setAvatarInUse(avatar, false);
// Close the dialog in the AWT Event Thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setBusy(false);
setVisible(false);
}
});
}
}.start();
return;
}
// If we are changing the name of the avatar, things get much more
// complicated. We want to delete the old avatar, but we need to save
// and use the new avatar first. (Because if we delete the old avatar
// first, the system will default back to the "Default" avatar to use
// perhaps). First, construct a new avatar, save it and use. We do
// all of this in a thread so that we do not block the GUI
setBusy(true);
new Thread() {
@Override
public void run() {
ImiAvatar newAvatar = ImiAvatar.createAvatar(newAvatarName);
newAvatar.setAvatarParams(currentParams);
save(newAvatar);
registry.setAvatarInUse(newAvatar, false);
// Next, delete the old avatar and close the dialog. We only
// want to delete it if the old avatar is really new
if (registry.getAvatarByName(originalAvatarName) != null) {
avatar.delete();
}
// Close the dialog in the AWT Event Thread
SwingUtilities.invokeLater(new Runnable() {