added null check for attachment entry and move button state management to the auto preview method.

This commit is contained in:
Heng Sin Low 2011-01-19 13:11:01 +08:00
parent 4aef583e4f
commit 8675e83423
1 changed files with 125 additions and 118 deletions

View File

@ -314,25 +314,38 @@ public class WAttachment extends Window implements EventListener
private boolean autoPreview(int index, boolean immediate)
{
MAttachmentEntry entry = m_attachment.getEntry(index);
String mimeType = entry.getContentType();
byte[] data = entry.getData();
String unit = " KB";
BigDecimal size = new BigDecimal(data != null ? data.length : 0);
size = size.divide(new BigDecimal("1024"));
if (size.compareTo(new BigDecimal("1024")) >= 0)
if (entry != null)
{
String mimeType = entry.getContentType();
byte[] data = entry.getData();
String unit = " KB";
BigDecimal size = new BigDecimal(data != null ? data.length : 0);
size = size.divide(new BigDecimal("1024"));
unit = " MB";
}
size = size.setScale(2, BigDecimal.ROUND_HALF_EVEN);
sizeLabel.setText(size.toPlainString() + unit);
if (autoPreviewList.contains(mimeType))
{
displayData(index, immediate);
return true;
if (size.compareTo(new BigDecimal("1024")) >= 0)
{
size = size.divide(new BigDecimal("1024"));
unit = " MB";
}
size = size.setScale(2, BigDecimal.ROUND_HALF_EVEN);
sizeLabel.setText(size.toPlainString() + unit);
bSave.setEnabled(true);
bDelete.setEnabled(true);
if (autoPreviewList.contains(mimeType))
{
displayData(index, immediate);
return true;
}
else
{
return false;
}
}
else
{
bSave.setEnabled(false);
bDelete.setEnabled(false);
return false;
}
}
@ -347,9 +360,6 @@ public class WAttachment extends Window implements EventListener
// Reset UI
preview.setSrc(null);
bDelete.setEnabled(false);
bSave.setEnabled(false);
displayIndex = index;
if (immediate)
@ -372,9 +382,6 @@ public class WAttachment extends Window implements EventListener
log.config("Index=" + displayIndex + " - " + entry);
if (entry != null && entry.getData() != null)
{
bSave.setEnabled(true);
bDelete.setEnabled(true);
log.config(entry.toStringX());
try