Search in sources :

Example 1 with Messages

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);
    }
}
Also used : Messages(com.haulmont.cuba.core.global.Messages) Instance(com.haulmont.chile.core.model.Instance) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 2 with Messages

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();
}
Also used : CubaWindow(com.haulmont.cuba.web.toolkit.ui.CubaWindow) Messages(com.haulmont.cuba.core.global.Messages) CubaLabel(com.haulmont.cuba.web.toolkit.ui.CubaLabel) Configuration(com.haulmont.cuba.core.global.Configuration) WebButton(com.haulmont.cuba.web.gui.components.WebButton) CubaLabel(com.haulmont.cuba.web.toolkit.ui.CubaLabel) Icons(com.haulmont.cuba.gui.icons.Icons) ClientConfig(com.haulmont.cuba.client.ClientConfig) AppUI(com.haulmont.cuba.web.AppUI)

Example 3 with Messages

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;
    }
}
Also used : Messages(com.haulmont.cuba.core.global.Messages)

Example 4 with Messages

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;
}
Also used : Messages(com.haulmont.cuba.core.global.Messages) Configuration(com.haulmont.cuba.core.global.Configuration) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) CubaFileUpload(com.haulmont.cuba.web.toolkit.ui.CubaFileUpload) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) FileOutputStream(java.io.FileOutputStream) ClientConfig(com.haulmont.cuba.client.ClientConfig) File(java.io.File)

Example 5 with Messages

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;
}
Also used : Messages(com.haulmont.cuba.core.global.Messages) Configuration(com.haulmont.cuba.core.global.Configuration) WebConfig(com.haulmont.cuba.web.WebConfig) IOException(java.io.IOException) VersionedThemeResource(com.haulmont.cuba.web.toolkit.VersionedThemeResource) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) ThemeConstants(com.haulmont.cuba.gui.theme.ThemeConstants) CubaMultiUpload(com.haulmont.cuba.web.toolkit.ui.CubaMultiUpload) WebWindowManager(com.haulmont.cuba.web.WebWindowManager) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

Messages (com.haulmont.cuba.core.global.Messages)56 ClientConfig (com.haulmont.cuba.client.ClientConfig)7 Configuration (com.haulmont.cuba.core.global.Configuration)7 Notifications (com.haulmont.cuba.gui.Notifications)5 Icons (com.haulmont.cuba.gui.icons.Icons)4 CubaButton (com.haulmont.cuba.web.widgets.CubaButton)4 File (java.io.File)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)3 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)3 WindowManager (com.haulmont.cuba.gui.WindowManager)3 ThemeConstants (com.haulmont.cuba.gui.theme.ThemeConstants)3 FileUploadingAPI (com.haulmont.cuba.gui.upload.FileUploadingAPI)3 UserSession (com.haulmont.cuba.security.global.UserSession)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 Locale (java.util.Locale)3 MetaProperty (com.haulmont.chile.core.annotations.MetaProperty)2 Metadata (com.haulmont.cuba.core.global.Metadata)2 Dialogs (com.haulmont.cuba.gui.Dialogs)2