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:it.jnrpe.plugin.CCheckOracle.java

/**
 * Checks cache hit rates/*from w w w. j  a  v  a  2 s  .  c  om*/
 * @param c
 * @param cl
 * @return
 */
private CReturnValue checkCache(Connection c, CCommandLine cl) {
    String sWarning = cl.getOptionValue("warning", "70");
    String sCritical = cl.getOptionValue("critical", "80");

    String sQry1 = "select (1-(pr.value/(dbg.value+cg.value)))*100"
            + " from v$sysstat pr, v$sysstat dbg, v$sysstat cg" + " where pr.name='physical reads'"
            + " and dbg.name='db block gets'" + " and cg.name='consistent gets'";

    String sQry2 = "select sum(lc.pins)/(sum(lc.pins)+sum(lc.reloads))*100 from v$librarycache lc";

    Statement stmt = null;
    ResultSet rs = null;

    try {
        stmt = c.createStatement();

        rs = stmt.executeQuery(sQry1);
        rs.next();

        BigDecimal buf_hr = rs.getBigDecimal(1);

        rs = stmt.executeQuery(sQry2);
        rs.next();

        BigDecimal lib_hr = rs.getBigDecimal(1);

        String sMessage = "{0} {1} - Cache Hit Rates: {2,number,0.#}% Lib -- {3,number,0.#}% Buff|lib={4,number,0.#}%;{5};{6};0;100 buffer={3,number,0.#};{5};{6};0;100";

        MessageFormat mf = new MessageFormat(sMessage);

        Object[] vValues = new Object[7];
        vValues[0] = cl.getOptionValue("db");
        vValues[1] = "OK";
        vValues[2] = lib_hr;
        vValues[3] = buf_hr;
        vValues[4] = lib_hr;
        vValues[5] = sWarning;
        vValues[6] = sCritical;

        //if (buf_hr.compareTo(new BigDecimal(iCritical.intValue())) == -1)
        if (ThresholdUtil.isValueInRange(sCritical, buf_hr)) {
            vValues[1] = "CRITICAL";

            CReturnValue rv = new CReturnValue(IJNRPEConstants.STATE_CRITICAL, mf.format(vValues));
            return rv;
        }

        //if (buf_hr.compareTo(new BigDecimal(iWarning.intValue())) == -1)
        if (ThresholdUtil.isValueInRange(sWarning, buf_hr)) {
            vValues[1] = "WARNING";

            CReturnValue rv = new CReturnValue(IJNRPEConstants.STATE_WARNING, mf.format(vValues));

            return rv;
        }

        CReturnValue rv = new CReturnValue(IJNRPEConstants.STATE_WARNING, mf.format(vValues));

        return rv;

    } catch (Exception e) {
        return new CReturnValue(IJNRPEConstants.STATE_CRITICAL, "CHECK_ORACLE : CRITICAL - " + e.getMessage());
    } finally {
        try {
            stmt.close();
            rs.close();
        } catch (Exception e) {
        }
    }

}

From source file:libepg.epg.section.descriptor.servicedescriptor.SERVICE_ID.java

@Override
public synchronized String toString() {
    StringBuilder s = new StringBuilder();
    for (int i : this.serviceIds) {
        s.append("[");
        s.append(Integer.toHexString(i));
        s.append("]");
    }//  w  ww. j av  a 2 s .  co m
    String set = s.toString();
    MessageFormat msg = new MessageFormat("{0}({1},serviceIds={2})");
    Object[] parameters = { super.toString(), this.getServiceType(), set };
    return msg.format(parameters);
}

From source file:org.alfresco.extension.scripting.javascript.JavascriptHelper.java

public static String getMessage(String messageId, Object[] args) {
    Context cx = Context.getCurrentContext();
    Locale locale = cx == null ? Locale.getDefault() : cx.getLocale();

    // ResourceBundle does cacheing.
    ResourceBundle rb = ResourceBundle.getBundle("org.alfresco.extension.scripting.javascript.Messages",
            locale);//  w ww .  j av  a2 s . c o m

    String formatString;
    try {
        formatString = rb.getString(messageId);
    } catch (java.util.MissingResourceException mre) {
        throw new RuntimeException("no message resource found for message property " + messageId);
    }

    if (args == null) {
        return formatString;
    } else {
        MessageFormat formatter = new MessageFormat(formatString);
        return formatter.format(args);
    }
}

From source file:uk.ac.cam.caret.sakai.rwiki.tool.command.AddAttachmentReturnCommand.java

private String singleRefContent(char[] charContent, int startPos, int endPos, Reference ref) {
    ResourceProperties refProps = ref.getProperties();
    String contentType = refProps.getProperty(refProps.getNamePropContentType());
    String template = getTemplate(contentType);
    Object[] args = getTemplateArgs(ref);

    if (startPos != endPos) {
        // We have to use a different name for the "link"
        args[2] = new String(charContent, startPos, endPos - startPos);
    }/* w w  w.  java 2 s.c om*/

    // Work out length properly
    StringBuffer newContent = new StringBuffer(charContent.length + template.length());

    newContent.append(charContent, 0, startPos);

    (new MessageFormat(template)).format(args, newContent, null);

    if (endPos < charContent.length) {
        newContent.append(charContent, endPos, charContent.length - endPos);
    }
    return newContent.toString();
}

From source file:org.web4thejob.print.CsvPrinter.java

@Override
public File print(String title, List<RenderScheme> renderSchemes, Query query, List<Entity> entities) {
    Assert.notNull(renderSchemes);//from www .  j  ava  2  s .  c  o m

    int i = 0;
    File file;
    try {
        String crlf = System.getProperty("line.separator");
        file = createTempFile();
        BufferedWriter writer = createFileStream(file);
        writer.write(title + crlf);
        writer.newLine();

        CSVWriter csv = new CSVWriter(writer);

        for (Entity entity : entities) {

            if (entity == null) {
                Assert.notNull(query);
                entity = ContextUtil.getDRS().findUniqueByQuery(query);
            }

            if (query != null && query.hasMasterCriterion()) {
                writer.write(describeMasterCriteria(query));
                writer.newLine();
            }

            if (query != null) {
                writer.write(describeCriteria(query));
                writer.newLine();
            }

            writeLine(csv, ContextUtil.getBean(ConversionService.class), entity, renderSchemes.get(i));
            writer.newLine();
            writer.newLine();
            i++;
        }

        //timestamp
        List<String> line = new ArrayList<String>();
        line.add(L10nMessages.L10N_LABEL_TIMESTAMP.toString());
        MessageFormat df = new MessageFormat("");
        df.setLocale(CoreUtil.getUserLocale());
        df.applyPattern("{0,date,yyyy-MM-dd hh:mm:ss}");
        line.add(df.format(new Object[] { new Date() }));
        csv.writeNext(line.toArray(new String[line.size()]));

        writer.newLine();

        writer.write("powered by web4thejob.org");
        writer.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return file;

}

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

/**
 * Class Constructor.// w w  w  .j a  v  a  2  s.c  o m
 *
 * @param model
 *
 * @see
 */
public RegistryObjectsTable(TableModel model) {
    // Gives a TableColumnModel so that AutoCreateColumnsFromModel will be false.
    super(model, new DefaultTableColumnModel());

    this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    if (model instanceof RegistryObjectsTableModel) {
        tableModel = (RegistryObjectsTableModel) model;
    } else if (model instanceof TableSorter) {
        tableModel = (RegistryObjectsTableModel) (((TableSorter) model).getModel());
    } else {
        Object[] unexpectedTableModelArgs = { model };
        MessageFormat form = new MessageFormat(resourceBundle.getString("error.unexpectedTableModel"));
        throw new IllegalArgumentException(form.format(unexpectedTableModelArgs));
    }

    setToolTipText(resourceBundle.getString("tip.registryObjectsTable"));

    setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    ListSelectionModel rowSM = getSelectionModel();
    stdRowHeight = getRowHeight();
    setRowHeight(stdRowHeight * 3);

    rowSM.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();

            if (!lsm.isSelectionEmpty()) {
                setSelectedRow(lsm.getMinSelectionIndex());
            } else {
                setSelectedRow(-1);
            }
        }
    });

    createPopup();

    addRenderers();

    // Add listener to self so that I can bring up popup menus on right mouse click
    popupListener = new PopupListener();
    addMouseListener(popupListener);

    //add listener for 'authenticated' bound property
    RegistryBrowser.getInstance().addPropertyChangeListener(RegistryBrowser.PROPERTY_AUTHENTICATED, this);

    //add listener for 'locale' bound property
    RegistryBrowser.getInstance().addPropertyChangeListener(RegistryBrowser.PROPERTY_LOCALE, this);
}

From source file:libepg.epg.section.descriptor.DESCRIPTOR_TAG.java

@Override
public synchronized String toString() {
    StringBuilder s = new StringBuilder();
    for (int i : this.tags) {
        s.append("[");
        s.append(Integer.toHexString(i));
        s.append("]");
    }/*from ww w  .jav a  2 s  . c o  m*/
    String set = s.toString();
    MessageFormat msg = new MessageFormat("{0}(tagName={1},TAGs={2},dataType={3})");
    Object[] parameters = { super.toString(), this.getTagName(), set, dataType };
    return msg.format(parameters);
}

From source file:com.dbschools.quickquiz.client.taker.MainWindow.java

final void setFrameTitle() {
    final Object[] messageArguments = { takerName, getQuizName(), "?" };
    final MessageFormat formatter = new MessageFormat(Resources.getString("clientTitle"));
    setTitle(formatter.format(messageArguments));
}

From source file:org.wrml.runtime.format.text.html.WrmldocFormatter.java

public MessageFormat getTemplate(String templateName) throws IOException {

    if (!_Templates.containsKey(templateName)) {
        final InputStream templateStream = getClass().getResourceAsStream(templateName);
        final String templateSource = IOUtils.toString(templateStream);

        _Templates.put(templateName, new MessageFormat(templateSource));
    }/*from www . jav a2s.c  o m*/

    return _Templates.get(templateName);

}

From source file:org.eclipse.swt.examples.accessibility.BarChart.java

void addListeners() {
    addPaintListener(e -> {//from  w w  w.  j  ava  2 s  .c  om
        GC gc = e.gc;
        Rectangle rect = getClientArea();
        Display display = getDisplay();
        int count = data.size();
        Point valueSize = gc.stringExtent(Integer.valueOf(valueMax).toString());
        int leftX = rect.x + 2 * GAP + valueSize.x;
        int bottomY = rect.y + rect.height - 2 * GAP - valueSize.y;
        int unitWidth = (rect.width - 4 * GAP - valueSize.x - AXIS_WIDTH) / count - GAP;
        int unitHeight = (rect.height - 3 * GAP - AXIS_WIDTH - 2 * valueSize.y)
                / ((valueMax - valueMin) / valueIncrement);
        // draw the title
        int titleWidth = gc.stringExtent(title).x;
        int center = (Math.max(titleWidth, count * (unitWidth + GAP) + GAP) - titleWidth) / 2;
        gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
        gc.drawString(title, leftX + AXIS_WIDTH + center, rect.y + GAP);
        // draw the y axis and value labels
        gc.setLineWidth(AXIS_WIDTH);
        gc.drawLine(leftX, rect.y + GAP + valueSize.y, leftX, bottomY);
        for (int i1 = valueMin; i1 <= valueMax; i1 += valueIncrement) {
            int y = bottomY - i1 * unitHeight;
            gc.drawLine(leftX, y, leftX - GAP, y);
            gc.drawString(Integer.valueOf(i1).toString(), rect.x + GAP, y - valueSize.y);
        }
        // draw the x axis and item labels
        gc.drawLine(leftX, bottomY, rect.x + rect.width - GAP, bottomY);
        for (int i2 = 0; i2 < count; i2++) {
            Object[] dataItem1 = data.get(i2);
            String itemLabel = (String) dataItem1[0];
            int x1 = leftX + AXIS_WIDTH + GAP + i2 * (unitWidth + GAP);
            gc.drawString(itemLabel, x1, bottomY + GAP);
        }
        // draw the bars
        gc.setBackground(display.getSystemColor(color));
        for (int i3 = 0; i3 < count; i3++) {
            Object[] dataItem2 = data.get(i3);
            int itemValue1 = ((Integer) dataItem2[1]).intValue();
            int x2 = leftX + AXIS_WIDTH + GAP + i3 * (unitWidth + GAP);
            gc.fillRectangle(x2, bottomY - AXIS_WIDTH - itemValue1 * unitHeight, unitWidth,
                    itemValue1 * unitHeight);
        }
        if (isFocusControl()) {
            if (selectedItem == -1) {
                // draw the focus rectangle around the whole bar chart
                gc.drawFocus(rect.x, rect.y, rect.width, rect.height);
            } else {
                // draw the focus rectangle around the selected item
                Object[] dataItem3 = data.get(selectedItem);
                int itemValue2 = ((Integer) dataItem3[1]).intValue();
                int x3 = leftX + AXIS_WIDTH + GAP + selectedItem * (unitWidth + GAP);
                gc.drawFocus(x3, bottomY - itemValue2 * unitHeight - AXIS_WIDTH, unitWidth,
                        itemValue2 * unitHeight + AXIS_WIDTH + GAP + valueSize.y);
            }
        }
    });

    addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            redraw();
        }

        @Override
        public void focusLost(FocusEvent e) {
            redraw();
        }
    });

    addMouseListener(MouseListener.mouseDownAdapter(e -> {
        if (getClientArea().contains(e.x, e.y)) {
            setFocus();
            int item = -1;
            int count = data.size();
            for (int i = 0; i < count; i++) {
                if (itemBounds(i).contains(e.x, e.y)) {
                    item = i;
                    break;
                }
            }
            if (item != selectedItem) {
                selectedItem = item;
                redraw();
                getAccessible().setFocus(item);
                getAccessible().selectionChanged();
            }
        }
    }));

    addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            boolean change = false;
            switch (e.keyCode) {
            case SWT.ARROW_DOWN:
            case SWT.ARROW_RIGHT:
                selectedItem++;
                if (selectedItem >= data.size())
                    selectedItem = 0;
                change = true;
                break;
            case SWT.ARROW_UP:
            case SWT.ARROW_LEFT:
                selectedItem--;
                if (selectedItem <= -1)
                    selectedItem = data.size() - 1;
                change = true;
                break;
            case SWT.HOME:
                selectedItem = 0;
                change = true;
                break;
            case SWT.END:
                selectedItem = data.size() - 1;
                change = true;
                break;
            }
            if (change) {
                redraw();
                getAccessible().setFocus(selectedItem);
                getAccessible().selectionChanged();
            }
        }
    });

    addTraverseListener(e -> {
        switch (e.detail) {
        case SWT.TRAVERSE_TAB_NEXT:
        case SWT.TRAVERSE_TAB_PREVIOUS:
            e.doit = true;
            break;
        }
    });

    getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            MessageFormat formatter = new MessageFormat(""); //$NON_NLS$
            formatter.applyPattern(bundle.getString("name")); //$NON_NLS$
            int childID = e.childID;
            if (childID == ACC.CHILDID_SELF) {
                e.result = title;
            } else {
                Object[] item = data.get(childID);
                e.result = formatter.format(item);
            }
        }

        @Override
        public void getDescription(AccessibleEvent e) {
            int childID = e.childID;
            if (childID != ACC.CHILDID_SELF) {
                Object[] item = data.get(childID);
                String value = item[1].toString();
                String colorName = bundle.getString("color" + color); //$NON_NLS$
                MessageFormat formatter = new MessageFormat(""); //$NON_NLS$
                formatter.applyPattern(bundle.getString("color_value")); //$NON_NLS$
                e.result = formatter.format(new String[] { colorName, value });
            }
        }
    });

    getAccessible().addAccessibleControlListener(new AccessibleControlAdapter() {
        @Override
        public void getRole(AccessibleControlEvent e) {
            if (e.childID == ACC.CHILDID_SELF) {
                e.detail = ACC.ROLE_LIST;
            } else {
                e.detail = ACC.ROLE_LISTITEM;
            }
        }

        @Override
        public void getChildCount(AccessibleControlEvent e) {
            e.detail = data.size();
        }

        @Override
        public void getChildren(AccessibleControlEvent e) {
            int count = data.size();
            Object[] children = new Object[count];
            for (int i = 0; i < count; i++) {
                children[i] = Integer.valueOf(i);
            }
            e.children = children;
        }

        @Override
        public void getChildAtPoint(AccessibleControlEvent e) {
            Point testPoint = toControl(e.x, e.y);
            int childID = ACC.CHILDID_NONE;
            if (getClientArea().contains(testPoint)) {
                childID = ACC.CHILDID_SELF;
                int count = data.size();
                for (int i = 0; i < count; i++) {
                    if (itemBounds(i).contains(testPoint)) {
                        childID = i;
                        break;
                    }
                }
            }
            e.childID = childID;
        }

        @Override
        public void getLocation(AccessibleControlEvent e) {
            Rectangle location = null;
            Point pt = null;
            int childID = e.childID;
            if (childID == ACC.CHILDID_SELF) {
                location = getClientArea();
                pt = getParent().toDisplay(location.x, location.y);
            } else {
                location = itemBounds(childID);
                pt = toDisplay(location.x, location.y);
            }
            e.x = pt.x;
            e.y = pt.y;
            e.width = location.width;
            e.height = location.height;
        }

        @Override
        public void getFocus(AccessibleControlEvent e) {
            int childID = ACC.CHILDID_NONE;
            if (isFocusControl()) {
                if (selectedItem == -1) {
                    childID = ACC.CHILDID_SELF;
                } else {
                    childID = selectedItem;
                }
            }
            e.childID = childID;

        }

        @Override
        public void getSelection(AccessibleControlEvent e) {
            e.childID = (selectedItem == -1) ? ACC.CHILDID_NONE : selectedItem;
        }

        @Override
        public void getValue(AccessibleControlEvent e) {
            int childID = e.childID;
            if (childID != ACC.CHILDID_SELF) {
                Object[] dataItem = data.get(childID);
                e.result = ((Integer) dataItem[1]).toString();
            }
        }

        @Override
        public void getState(AccessibleControlEvent e) {
            int childID = e.childID;
            e.detail = ACC.STATE_FOCUSABLE;
            if (isFocusControl())
                e.detail |= ACC.STATE_FOCUSED;
            if (childID != ACC.CHILDID_SELF) {
                e.detail |= ACC.STATE_SELECTABLE;
                if (childID == selectedItem)
                    e.detail |= ACC.STATE_SELECTED;
            }
        }
    });
}