Example usage for java.text MessageFormat MessageFormat

List of usage examples for java.text MessageFormat MessageFormat

Introduction

In this page you can find the example usage for java.text MessageFormat MessageFormat.

Prototype

public MessageFormat(String pattern) 

Source Link

Document

Constructs a MessageFormat for the default java.util.Locale.Category#FORMAT FORMAT locale and the specified pattern.

Usage

From source file:org.openmrs.module.initializer.InitializerMessageSource.java

/**
 * @see AbstractMessageSource#resolveCode(String, Locale)
 *//*  w w  w.  j  ava 2 s . co  m*/
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
    if (showMessageCode) {
        return new MessageFormat(code);
    }
    PresentationMessage pm = getPresentation(code, locale); // Check exact match
    if (pm == null) {
        if (locale.getVariant() != null) {
            pm = getPresentation(code, new Locale(locale.getLanguage(), locale.getCountry())); // Try to match
                                                                                               // language and
                                                                                               // country
            if (pm == null) {
                pm = getPresentation(code, new Locale(locale.getLanguage())); // Try to match language only
            }
        }
    }
    if (pm != null) {
        return new MessageFormat(pm.getMessage());
    }
    return null;
}

From source file:modula.parser.io.ModelUpdater.java

/**
 * /*from  w  w w.  j  a v a2  s .c om*/
 */
private static void logAndThrowModelError(final String errType, final Object[] msgArgs) throws ModelException {
    MessageFormat msgFormat = new MessageFormat(errType);
    String errMsg = msgFormat.format(msgArgs);
    org.apache.commons.logging.Log log = LogFactory.getLog(ModelUpdater.class);
    log.error(errMsg);
    throw new ModelException(errMsg);
}

From source file:nl.toolforge.karma.core.boot.WorkingContextConfiguration.java

/**
 * Stores the all configuration items in <code>configuration</code> in the <code>working-context.xml</code> file.
 *
 * @throws WorkingContextException When storing failed.
 *///  w  ww .  j av  a2s.  c  o  m
public void store() throws WorkingContextException {

    // todo this implementation can be made more efficient, but it's ok for now.

    if (configuration == null) {
        throw new NullPointerException("Configuration cannot be null.");
    }

    // Check the (mandatory) configuration
    //

    StringBuffer buffer = new StringBuffer();
    buffer.append("<?xml version=\"1.0\"?>\n");

    buffer.append(
            "<wc:working-context\n" + "  xmlns:wc=\"http://www.toolforge.org/specifications/working-context\"\n"
                    + "  xmlns:loc=\"http://www.toolforge.org/specifications/location\">\n");

    // <properties>-element
    //
    buffer.append("  <wc:properties>\n");

    MessageFormat formatter = new MessageFormat("    <wc:property name=\"{0}\" value=\"{1}\"/>\n");

    Enumeration e = configuration.propertyNames();

    while (e.hasMoreElements()) {

        String key = (String) e.nextElement();
        String value = configuration.getProperty(key);

        buffer.append(formatter.format(new String[] { key, value }));
    }

    buffer.append("  </wc:properties>\n\n");
    //
    // </properties>-element

    if (manifestStore == null && locationStore == null) {
        //
    } else {

        buffer.append("  <loc:locations>\n");

        if (manifestStore != null) {
            buffer.append(manifestStore.getLocation().asXML());
        }
        if (locationStore != null) {
            buffer.append(locationStore.getLocation().asXML());
        }

        buffer.append("  </loc:locations>\n");

    }

    buffer.append("\n");

    buffer.append("</wc:working-context>\n");

    // Write to file `working-context.xml`
    //
    try {
        if (!getConfigFile().exists()) {
            getConfigFile().createNewFile();
        }
        Writer writer = new BufferedWriter(new FileWriter(getConfigFile()));
        writer.write(buffer.toString());
        writer.flush();
    } catch (IOException ioe) {
        logger.error(ioe);
        throw new WorkingContextException(ioe);
    }
}

From source file:libepg.epg.section.sectionreconstructor.SectionReconstructor.java

/**
 * ??????????????????/*from  w  ww . j a  v  a2  s.c om*/
 * ByteBuffer?array()????????????????????
 *
 */
private synchronized void addToReturnObject(ByteBuffer buf, Set<byte[]> dest) {
    byte[] BeforeCutDown = buf.array();
    byte[] AfterCutDown = new byte[buf.position()];
    System.arraycopy(BeforeCutDown, 0, AfterCutDown, 0, AfterCutDown.length);
    if (LOG.isTraceEnabled()) {
        MessageFormat msg1 = new MessageFormat("\n??={0}\n?={1}");
        Object[] parameters1 = { Hex.encodeHexString(BeforeCutDown), Hex.encodeHexString(AfterCutDown) };
        LOG.trace(msg1.format(parameters1));
    }
    if (dest.add(AfterCutDown)) {
        if (LOG.isTraceEnabled()) {
            MessageFormat msg2 = new MessageFormat("\n???={0}");
            Object[] parameters2 = { Hex.encodeHexString(AfterCutDown) };
            LOG.trace(msg2.format(parameters2));
        }
    } else if (LOG.isTraceEnabled()) {
        MessageFormat msg3 = new MessageFormat("\n???????={0}");
        Object[] parameters3 = { Hex.encodeHexString(AfterCutDown) };
        LOG.trace(msg3.format(parameters3));
    }
}

From source file:misc.TextAreaPrintingDemo.java

private MessageFormat createFormat(JTextField source) {
    String text = source.getText();
    if (text != null && text.length() > 0) {
        try {// w  w  w.  j  a  va 2s.  co  m
            return new MessageFormat(text);
        } catch (IllegalArgumentException e) {
            error("Sorry, this format is invalid.");
        }
    }
    return null;
}

From source file:it.cnr.icar.eric.client.ui.swing.RegistryObjectsTable.java

public TableCellRenderer getCellRenderer(int row, int column) {
    if (cachedTableCellRenderers[column] != null) {
        return cachedTableCellRenderers[column];
    }//from   ww  w. j  a v a2 s  .com
    TableCellRenderer renderer = null;
    Class<?> clazz = null;

    try {
        clazz = tableModel.getColumnClass(column);
        renderer = super.getCellRenderer(row, column);

        if (renderer == null) {
            Object[] unsupportedColumnClassArgs = { tableModel.getColumnName(column), clazz };
            MessageFormat form = new MessageFormat(resourceBundle.getString("error.unsupportedColumnClass"));
            RegistryBrowser.displayError(form.format(unsupportedColumnClassArgs));
        }
    } catch (Exception e) {
        Object[] unsupportedColumnClassArgs = { tableModel.getColumnName(column), clazz };
        MessageFormat form = new MessageFormat(resourceBundle.getString("error.unsupportedColumnClass"));
        RegistryBrowser.displayError(form.format(unsupportedColumnClassArgs), e);
    }

    if (renderer instanceof JLabel) {
        ((JLabel) renderer).setHorizontalAlignment(SwingConstants.LEFT);
    }
    cachedTableCellRenderers[column] = renderer;
    return renderer;
}

From source file:libepg.epg.section.Section.java

/**
 * ?CRC?/*from w  w  w .  ja v a 2  s .co m*/
 *
 * @return ?
 */
public final synchronized CRC_STATUS checkCRC() {
    SectionCrcChecker checker = new SectionCrcChecker();

    int res = checker.checkCrc(this.getData());
    if (res == 0) {
        return CRC_STATUS.NO_CRC_ERROR;
    } else {
        if (LOG.isWarnEnabled()) {
            MessageFormat msg = new MessageFormat("CRC???0?????={0}");
            Object[] parameters = { res };
            LOG.warn(msg.format(parameters));
        }
        return CRC_STATUS.CRC_ERROR;
    }
}

From source file:misc.TablePrintDemo1.java

/**
 * Print the grades table./* w  w w  . j  a v a  2 s .c o  m*/
 */
private void printGradesTable() {
    /* Fetch printing properties from the GUI components */

    MessageFormat header = null;

    /* if we should print a header */
    if (headerBox.isSelected()) {
        /* create a MessageFormat around the header text */
        header = new MessageFormat(headerField.getText());
    }

    MessageFormat footer = null;

    /* if we should print a footer */
    if (footerBox.isSelected()) {
        /* create a MessageFormat around the footer text */
        footer = new MessageFormat(footerField.getText());
    }

    boolean fitWidth = fitWidthBox.isSelected();
    boolean showPrintDialog = showPrintDialogBox.isSelected();
    boolean interactive = interactiveBox.isSelected();

    /* determine the print mode */
    JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH : JTable.PrintMode.NORMAL;

    try {
        /* print the table */
        boolean complete = gradesTable.print(mode, header, footer, showPrintDialog, null, interactive, null);

        /* if printing completes */
        if (complete) {
            /* show a success message */
            JOptionPane.showMessageDialog(this, "Printing Complete", "Printing Result",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            /* show a message indicating that printing was cancelled */
            JOptionPane.showMessageDialog(this, "Printing Cancelled", "Printing Result",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (PrinterException pe) {
        /* Printing failed, report to the user */
        JOptionPane.showMessageDialog(this, "Printing Failed: " + pe.getMessage(), "Printing Result",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:org.apache.jmeter.testbeans.gui.GenericTestBeanCustomizer.java

/**
 * Create a customizer for a given test bean type.
 *
 * @param testBeanClass//  w  w w  .  jav  a2 s  . c  om
 *            a subclass of TestBean
 * @see org.apache.jmeter.testbeans.TestBean
 */
GenericTestBeanCustomizer(BeanInfo beanInfo) {
    super();

    this.beanInfo = beanInfo;

    // Get and sort the property descriptors:
    descriptors = beanInfo.getPropertyDescriptors();
    Arrays.sort(descriptors, new PropertyComparator(beanInfo));

    // Obtain the propertyEditors:
    editors = new PropertyEditor[descriptors.length];
    int scriptLanguageIndex = 0;
    int textAreaEditorIndex = 0;
    for (int i = 0; i < descriptors.length; i++) { // Index is also used for accessing editors array
        PropertyDescriptor descriptor = descriptors[i];
        String name = descriptor.getName();

        // Don't get editors for hidden or non-read-write properties:
        if (TestBeanHelper.isDescriptorIgnored(descriptor)) {
            log.debug("Skipping editor for property " + name);
            editors[i] = null;
            continue;
        }

        PropertyEditor propertyEditor;
        Object guiType = descriptor.getValue(GUITYPE);
        if (guiType instanceof TypeEditor) {
            propertyEditor = ((TypeEditor) guiType).getInstance(descriptor);
        } else if (guiType instanceof Class && Enum.class.isAssignableFrom((Class<?>) guiType)) {
            @SuppressWarnings("unchecked") // we check the class type above
            final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) guiType;
            propertyEditor = new EnumEditor(descriptor, enumClass,
                    (ResourceBundle) descriptor.getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE));
        } else {
            Class<?> editorClass = descriptor.getPropertyEditorClass();
            if (log.isDebugEnabled()) {
                log.debug("Property " + name + " has editor class " + editorClass);
            }

            if (editorClass != null) {
                try {
                    propertyEditor = (PropertyEditor) editorClass.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    log.error("Can't create property editor.", e);
                    throw new Error(e.toString());
                }
            } else {
                Class<?> c = descriptor.getPropertyType();
                propertyEditor = PropertyEditorManager.findEditor(c);
            }
        }

        if (propertyEditor == null) {
            log.warn("No editor for property: " + name + " type: " + descriptor.getPropertyType() + " in bean: "
                    + beanInfo.getBeanDescriptor().getDisplayName());
            editors[i] = null;
            continue;
        }

        if (log.isDebugEnabled()) {
            log.debug("Property " + name + " has property editor " + propertyEditor);
        }

        validateAttributes(descriptor, propertyEditor);

        if (!propertyEditor.supportsCustomEditor()) {
            propertyEditor = createWrapperEditor(propertyEditor, descriptor);

            if (log.isDebugEnabled()) {
                log.debug("Editor for property " + name + " is wrapped in " + propertyEditor);
            }
        }
        if (propertyEditor instanceof TestBeanPropertyEditor) {
            ((TestBeanPropertyEditor) propertyEditor).setDescriptor(descriptor);
        }

        if (propertyEditor instanceof TextAreaEditor) {
            textAreaEditorIndex = i;
        }
        if (propertyEditor.getCustomEditor() instanceof JScrollPane) {
            scrollerCount++;
        }

        editors[i] = propertyEditor;

        // Initialize the editor with the provided default value or null:
        setEditorValue(i, descriptor.getValue(DEFAULT));

        if (name.equals("scriptLanguage")) {
            scriptLanguageIndex = i;
        }

    }
    // In case of BSF and JSR elements i want to add textAreaEditor as a listener to scriptLanguage ComboBox.
    String beanName = this.beanInfo.getBeanDescriptor().getName();
    if (beanName.startsWith("BSF") || beanName.startsWith("JSR223")) { // $NON-NLS-1$ $NON-NLS-2$
        WrapperEditor we = (WrapperEditor) editors[scriptLanguageIndex];
        TextAreaEditor tae = (TextAreaEditor) editors[textAreaEditorIndex];
        we.addChangeListener(tae);
    }

    // Obtain message formats:
    propertyFieldLabelMessage = new MessageFormat(JMeterUtils.getResString("property_as_field_label")); //$NON-NLS-1$
    propertyToolTipMessage = new MessageFormat(JMeterUtils.getResString("property_tool_tip")); //$NON-NLS-1$

    // Initialize the GUI:
    init();
}

From source file:org.talend.designer.core.ui.viewer.proposal.TalendCompletionProposalComputer.java

private String getContextDescription(IContextParameter contextParameter, String display) {
    String desc = new String();
    if (!StringUtils.isEmpty(contextParameter.getComment())) {
        desc = contextParameter.getComment();
    } else {//from   w  w w.ja  va2s  .  c  o m
        desc = Messages.getString("ContextParameterProposal.NoCommentAvaiable"); //$NON-NLS-1$
    }

    String message = "<b>" + display + "</b><br><br>" //$NON-NLS-1$ //$NON-NLS-2$
            + Messages.getString("ContextParameterProposal.Description") + "<br>"; //$NON-NLS-1$ //$NON-NLS-2$
    message += Messages.getString("ContextParameterProposal.ContextVariable") + "<br><br>"; //$NON-NLS-1$ //$NON-NLS-2$
    message += Messages.getString("ContextParameterProposal.Type") + "<br><br>"; //$NON-NLS-1$ //$NON-NLS-2$
    message += Messages.getString("ContextParameterProposal.VariableName"); //$NON-NLS-1$

    MessageFormat format = new MessageFormat(message);
    Object[] args = new Object[] { desc, contextParameter.getType(), getContextContent(contextParameter) };
    return format.format(args);
}