use of com.haulmont.cuba.core.global.Messages in project cuba by cuba-platform.
the class InstanceUtils method getInstanceName.
/**
* @return Instance name as defined by {@link com.haulmont.chile.core.annotations.NamePattern}
* or <code>toString()</code>.
* @param instance instance
*/
public static String getInstanceName(Instance instance) {
checkNotNullArgument(instance, "instance is null");
NamePatternRec rec = parseNamePattern(instance.getMetaClass());
if (rec == null) {
return instance.toString();
} else {
if (rec.methodName != null) {
try {
Method method = instance.getClass().getMethod(rec.methodName);
Object result = method.invoke(instance);
return (String) result;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException("Error getting instance name", e);
}
}
// lazy initialized messages, used only for enum values
Messages messages = null;
Object[] values = new Object[rec.fields.length];
for (int i = 0; i < rec.fields.length; i++) {
Object value = instance.getValue(rec.fields[i]);
if (value == null) {
values[i] = "";
} else if (value instanceof Instance) {
values[i] = getInstanceName((Instance) value);
} else if (value instanceof EnumClass) {
if (messages == null) {
messages = AppBeans.get(Messages.NAME);
}
values[i] = messages.getMessage((Enum) value);
} else {
values[i] = value;
}
}
return String.format(rec.format, values);
}
}
use of com.haulmont.cuba.core.global.Messages in project cuba by cuba-platform.
the class NoUserSessionHandler method showNoUserSessionDialog.
protected void showNoUserSessionDialog(App app) {
Messages messages = AppBeans.get(Messages.NAME);
Window dialog = new NoUserSessionExceptionDialog();
dialog.setStyleName("c-nousersession-dialog");
dialog.setCaption(messages.getMainMessage("dialogs.Information", locale));
dialog.setClosable(false);
dialog.setResizable(false);
dialog.setModal(true);
AppUI ui = app.getAppUI();
if (ui.isTestMode()) {
dialog.setCubaId("optionDialog");
dialog.setId(ui.getTestIdManager().getTestId("optionDialog"));
}
Label messageLab = new CubaLabel();
messageLab.setWidthUndefined();
messageLab.setValue(messages.getMainMessage("noUserSession.message", locale));
VerticalLayout layout = new VerticalLayout();
layout.setSpacing(true);
layout.setWidthUndefined();
layout.setStyleName("c-nousersession-dialog-layout");
layout.setSpacing(true);
dialog.setContent(layout);
Button reloginBtn = new Button();
if (ui.isTestMode()) {
reloginBtn.setCubaId("reloginBtn");
reloginBtn.setId(ui.getTestIdManager().getTestId("reloginBtn"));
}
reloginBtn.addStyleName(WebButton.ICON_STYLE);
reloginBtn.addStyleName("c-primary-action");
reloginBtn.addClickListener(event -> relogin());
reloginBtn.setCaption(messages.getMainMessage(Type.OK.getMsgKey()));
String iconName = AppBeans.get(Icons.class).get(Type.OK.getIconKey());
reloginBtn.setIcon(AppBeans.get(IconResolver.class).getIconResource(iconName));
ClientConfig clientConfig = AppBeans.get(Configuration.class).getConfig(ClientConfig.class);
setClickShortcut(reloginBtn, clientConfig.getCommitShortcut());
reloginBtn.focus();
layout.addComponent(messageLab);
layout.addComponent(reloginBtn);
layout.setComponentAlignment(reloginBtn, Alignment.BOTTOM_RIGHT);
ui.addWindow(dialog);
dialog.center();
}
use of com.haulmont.cuba.core.global.Messages in project cuba by cuba-platform.
the class WebDatePicker method handleDateOutOfRange.
protected void handleDateOutOfRange(Date value) {
if (getFrame() != null) {
Messages messages = AppBeans.get(Messages.NAME);
getFrame().showNotification(messages.getMainMessage("datePicker.dateOutOfRangeMessage"), Frame.NotificationType.TRAY);
}
updatingInstance = true;
try {
component.setValue((Date) prevValue);
} finally {
updatingInstance = false;
}
}
use of com.haulmont.cuba.core.global.Messages in project cuba by cuba-platform.
the class WebFileMultiUploadField method initComponent.
protected void initComponent() {
CubaFileUpload impl = createComponent();
impl.setMultiSelect(true);
Messages messages = AppBeans.get(Messages.NAME);
impl.setProgressWindowCaption(messages.getMainMessage("upload.uploadingProgressTitle"));
impl.setUnableToUploadFileMessage(messages.getMainMessage("upload.unableToUploadFile"));
impl.setCancelButtonCaption(messages.getMainMessage("upload.cancel"));
impl.setCaption(messages.getMainMessage("upload.submit"));
impl.setDropZonePrompt(messages.getMainMessage("upload.dropZonePrompt"));
impl.setDescription(null);
Configuration configuration = AppBeans.get(Configuration.NAME);
final int maxUploadSizeMb = configuration.getConfig(ClientConfig.class).getMaxUploadSizeMb();
final int maxSizeBytes = maxUploadSizeMb * BYTES_IN_MEGABYTE;
impl.setFileSizeLimit(maxSizeBytes);
impl.setReceiver((fileName, MIMEType) -> {
FileOutputStream outputStream;
try {
FileUploadingAPI.FileInfo fileInfo = fileUploading.createFile();
tempFileId = fileInfo.getId();
File tmpFile = fileInfo.getFile();
outputStream = new FileOutputStream(tmpFile);
} catch (Exception e) {
throw new RuntimeException("Unable to receive file", e);
}
return outputStream;
});
impl.addStartedListener(event -> fireFileUploadStart(event.getFileName(), event.getContentLength()));
impl.addQueueUploadFinishedListener(event -> fireQueueUploadComplete());
impl.addSucceededListener(event -> {
files.put(tempFileId, event.getFileName());
fireFileUploadFinish(event.getFileName(), event.getContentLength());
});
impl.addFailedListener(event -> {
try {
// close and remove temp file
fileUploading.deleteFile(tempFileId);
tempFileId = null;
} catch (Exception e) {
if (e instanceof FileStorageException) {
FileStorageException fse = (FileStorageException) e;
if (fse.getType() != FileStorageException.Type.FILE_NOT_FOUND) {
LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Could not remove temp file {} after broken uploading", tempFileId);
}
}
LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Error while delete temp file {}", tempFileId);
}
fireFileUploadError(event.getFileName(), event.getContentLength(), event.getReason());
});
impl.addFileSizeLimitExceededListener(e -> {
String warningMsg = messages.formatMessage(WebFileMultiUploadField.class, "multiupload.filesizeLimitExceed", e.getFileName(), getFileSizeLimitString());
getFrame().showNotification(warningMsg, Frame.NotificationType.WARNING);
});
impl.addFileExtensionNotAllowedListener(e -> {
String warningMsg = messages.formatMainMessage("upload.fileIncorrectExtension.message", e.getFileName());
getFrame().showNotification(warningMsg, Frame.NotificationType.WARNING);
});
component = impl;
}
use of com.haulmont.cuba.core.global.Messages in project cuba by cuba-platform.
the class WebFileMultiUploadField method initOldComponent.
protected void initOldComponent() {
CubaMultiUpload impl = createOldComponent();
ThemeConstants theme = App.getInstance().getThemeConstants();
String width = theme.get("cuba.web.WebFileMultiUploadField.upload.width");
String height = theme.get("cuba.web.WebFileMultiUploadField.upload.height");
impl.setWidth(width);
impl.setHeight(height);
int buttonTextLeft = theme.getInt("cuba.web.WebFileMultiUploadField.buttonText.left");
int buttonTextTop = theme.getInt("cuba.web.WebFileMultiUploadField.buttonText.top");
impl.setButtonTextLeft(buttonTextLeft);
impl.setButtonTextTop(buttonTextTop);
impl.setButtonWidth(Integer.parseInt(width.replace("px", "")));
impl.setButtonHeight(Integer.parseInt(height.replace("px", "")));
Messages messages = AppBeans.get(Messages.NAME);
impl.setCaption(messages.getMessage(AppConfig.getMessagesPack(), "multiupload.submit"));
Configuration configuration = AppBeans.get(Configuration.NAME);
impl.setFileSizeLimitMB(configuration.getConfig(ClientConfig.class).getMaxUploadSizeMb());
WebConfig webConfig = configuration.getConfig(WebConfig.class);
if (!webConfig.getUseFontIcons()) {
impl.setButtonImage(new VersionedThemeResource("components/multiupload/images/multiupload-button.png"));
} else {
impl.setButtonImage(new VersionedThemeResource("components/multiupload/images/multiupload-button-font-icon.png"));
}
impl.setButtonStyles(theme.get("cuba.web.WebFileMultiUploadField.button.style"));
impl.setButtonDisabledStyles(theme.get("cuba.web.WebFileMultiUploadField.button.disabled.style"));
impl.setBootstrapFailureHandler(new CubaMultiUpload.BootstrapFailureHandler() {
@Override
public void loadWebResourcesFailed() {
Messages messages = AppBeans.get(Messages.NAME);
String resourcesLoadFailed = messages.getMessage(WebFileMultiUploadField.class, "multiupload.resources.notLoaded");
WebWindowManager wm = App.getInstance().getWindowManager();
wm.showNotification(resourcesLoadFailed, Frame.NotificationType.ERROR);
}
@Override
public void flashNotInstalled() {
Messages messages = AppBeans.get(Messages.NAME);
String swfNotSupported = messages.getMessage(WebFileMultiUploadField.class, "multiupload.resources.swfNotSupported");
WebWindowManager wm = App.getInstance().getWindowManager();
wm.showNotification(swfNotSupported, Frame.NotificationType.ERROR);
}
});
impl.setReceiver((filename, mimeType) -> {
FileOutputStream outputStream;
try {
FileUploadingAPI.FileInfo fileInfo = fileUploading.createFile();
tempFileId = fileInfo.getId();
File tmpFile = fileInfo.getFile();
outputStream = new FileOutputStream(tmpFile);
} catch (Exception e) {
throw new RuntimeException("Unable to open stream for file uploading", e);
}
return outputStream;
});
impl.addUploadListener(new CubaMultiUpload.UploadListener() {
@Override
public void fileUploadStart(String fileName, long contentLength) {
fireFileUploadStart(fileName, contentLength);
}
@Override
public void fileUploaded(String fileName, long contentLength) {
files.put(tempFileId, fileName);
fireFileUploadFinish(fileName, contentLength);
}
@Override
public void queueUploadComplete() {
fireQueueUploadComplete();
}
@Override
public void errorNotify(String fileName, String message, CubaMultiUpload.UploadErrorType errorCode, long contentLength) {
LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Error while uploading file '{}' with code '{}': {}", fileName, errorCode.getId(), message);
Messages messages = AppBeans.get(Messages.NAME);
WebWindowManager wm = App.getInstance().getWindowManager();
switch(errorCode) {
case QUEUE_LIMIT_EXCEEDED:
wm.showNotification(messages.getMessage(WebFileMultiUploadField.class, "multiupload.queueLimitExceed"), Frame.NotificationType.WARNING);
break;
case INVALID_FILETYPE:
String invalidFiletypeMsg = messages.formatMainMessage("upload.fileIncorrectExtension.message", fileName);
wm.showNotification(invalidFiletypeMsg, Frame.NotificationType.WARNING);
break;
case FILE_EXCEEDS_SIZE_LIMIT:
String warningMsg = messages.formatMessage(WebFileMultiUploadField.class, "multiupload.filesizeLimitExceed", fileName, getFileSizeLimitString());
wm.showNotification(warningMsg, Frame.NotificationType.WARNING);
break;
case SECURITY_ERROR:
wm.showNotification(messages.getMessage(WebFileMultiUploadField.class, "multiupload.securityError"), Frame.NotificationType.WARNING);
break;
case ZERO_BYTE_FILE:
wm.showNotification(messages.formatMessage(WebFileMultiUploadField.class, "multiupload.zerobyteFile", fileName), Frame.NotificationType.WARNING);
break;
default:
String uploadError = messages.formatMessage(WebFileMultiUploadField.class, "multiupload.uploadError", fileName);
wm.showNotification(uploadError, Frame.NotificationType.ERROR);
fireFileUploadError(fileName, contentLength, new IOException("Upload error " + errorCode.name()));
break;
}
}
});
impl.setDescription(null);
component = impl;
}
Aggregations