final ArrayList<Message> errors = new ArrayList<Message>();
setPrimaryValid(useFile);
setPrimaryValid(useBase64);
final BinaryValue oldValue = value;
if (closeAndUpdateValue)
{
value = null;
}
if (useFile.isSelected())
{
String f = file.getText();
if (f.trim().length() == 0)
{
if (hasImageSyntax(attrName.getText()) && (oldValue != null) &&
!updateImage)
{
// Do nothing. We do not want to regenerate the image and we
// are on the case where the user simply did not change the image.
}
else
{
errors.add(ERR_CTRL_PANEL_FILE_NOT_PROVIDED.get());
setPrimaryInvalid(useFile);
setPrimaryInvalid(lFile);
}
}
else
{
File theFile = new File(f);
if (!theFile.exists())
{
errors.add(ERR_CTRL_PANEL_FILE_DOES_NOT_EXIST.get(f));
setPrimaryInvalid(useFile);
setPrimaryInvalid(lFile);
}
else if (theFile.isDirectory())
{
errors.add(ERR_CTRL_PANEL_PATH_IS_A_DIRECTORY.get(f));
setPrimaryInvalid(useFile);
setPrimaryInvalid(lFile);
}
else if (!theFile.canRead())
{
errors.add(ERR_CTRL_PANEL_CANNOT_READ_FILE.get(f));
setPrimaryInvalid(useFile);
setPrimaryInvalid(lFile);
}
}
}
else
{
String b = base64.getText();
if (b.length() == 0)
{
errors.add(ERR_CTRL_PANEL_VALUE_IN_BASE_64_REQUIRED.get());
setPrimaryInvalid(useBase64);
}
}
if (errors.size() == 0)
{
// Read the file or encode the base 64 content.
BackgroundTask<BinaryValue> worker = new BackgroundTask<BinaryValue>()
{
/**
* {@inheritDoc}
*/
public BinaryValue processBackgroundTask() throws Throwable
{
try
{
Thread.sleep(1000);
}
catch (Throwable t)
{
}
BinaryValue returnValue;
if (useBase64.isSelected())
{
returnValue = BinaryValue.createBase64(base64.getText());
}
else if (file.getText().trim().length() > 0)
{
File f = new File(file.getText());
FileInputStream in = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = new byte[2 * 1024];
try
{
in = new FileInputStream(f);
boolean done = false;
while (!done)
{
int len = in.read(bytes);
if (len == -1)
{
done = true;
}
else
{
out.write(bytes, 0, len);
}
}
returnValue = BinaryValue.createFromFile(out.toByteArray(), f);
}
finally
{
if (in != null)
{
in.close();
}
out.close();
}
}
else
{
// We do not want to regenerate the image and we
// are on the case where the user simply did not change the image.
returnValue = oldValue;
}
if (closeAndUpdateValue)
{
valueChanged = !returnValue.equals(oldValue);
}
if (updateImage)
{
updateImage(lImage, returnValue.getBytes());
}
return returnValue;
}
/**
* {@inheritDoc}