Search in sources :

Example 76 with Format

use of java.text.Format in project dbeaver by dbeaver.

the class StatisticsNavigatorNodeRenderer method renderObjectStatistics.

// /////////////////////////////////////////////////////////////////
// Statistics renderer
private void renderObjectStatistics(DBNDatabaseNode element, Tree tree, GC gc, Event event) {
    DBSObject object = element.getObject();
    if (object instanceof DBPObjectStatistics) {
        String sizeText;
        int percentFull;
        boolean statsWasRead = false;
        DBNNode parentNode = getParentItem(element);
        DBSObject parentObject = parentNode instanceof DBNDatabaseNode ? DBUtils.getPublicObject(((DBNDatabaseNode) parentNode).getObject()) : null;
        if (parentObject instanceof DBPObjectStatisticsCollector) {
            // && !((DBPObjectStatisticsCollector) parentObject).isStatisticsCollected()
            statsWasRead = ((DBPObjectStatisticsCollector) parentObject).isStatisticsCollected();
        } else {
            // If there is no stats collector then do not check for stats presence
            // Because it will trigger stats read job which won't read any statistics (as there is no way to load it for individual object).
            // ((DBPObjectStatistics) object).hasStatistics();
            statsWasRead = true;
        }
        long maxObjectSize = statsWasRead ? getMaxObjectSize((TreeItem) event.item) : -1;
        if (statsWasRead && maxObjectSize >= 0) {
            long statObjectSize = ((DBPObjectStatistics) object).getStatObjectSize();
            if (statObjectSize <= 0) {
                // Empty or no size - nothing to show
                return;
            }
            percentFull = maxObjectSize == 0 ? 0 : (int) (statObjectSize * 100 / maxObjectSize);
            if (percentFull < 0 || percentFull > 100) {
                log.debug("Object stat > 100%!");
                percentFull = 100;
            }
            Format format;
            synchronized (classFormatMap) {
                format = classFormatMap.get(object.getClass().getName());
                if (format == null) {
                    try {
                        Method getStatObjectSizeMethod = object.getClass().getMethod("getStatObjectSize");
                        Property propAnnotation = getStatObjectSizeMethod.getAnnotation(Property.class);
                        if (propAnnotation != null) {
                            Class<? extends Format> formatterClass = propAnnotation.formatter();
                            if (formatterClass != Format.class) {
                                format = formatterClass.getConstructor().newInstance();
                            }
                        }
                    } catch (Exception e) {
                        log.debug(e);
                    }
                    if (format == null) {
                        format = numberFormat;
                    }
                    classFormatMap.put(object.getClass().getName(), format);
                }
            }
            sizeText = format.format(statObjectSize);
        } else {
            sizeText = "...";
            percentFull = 0;
            if (parentNode instanceof DBNDatabaseNode) {
                DBSObject realParentObject = DBUtils.getPublicObject(((DBNDatabaseNode) parentNode).getObject());
                if (!readObjectStatistics(element.getParentNode(), realParentObject, ((TreeItem) event.item).getParentItem())) {
                    return;
                }
            }
        }
        Point textSize = gc.stringExtent(sizeText);
        textSize.x += 4;
        // int caWidth = tree.getClientArea().width;
        int occupiedWidth = event.x + event.width + 4;
        int xWidth = getTreeWidth(tree);
        if (xWidth - occupiedWidth > Math.max(PERCENT_FILL_WIDTH, textSize.x)) {
            {
                CTabFolder tabFolder = UIUtils.getParentOfType(tree, CTabFolder.class);
                Color fillColor = tabFolder == null ? UIStyles.getDefaultWidgetBackground() : tabFolder.getBackground();
                gc.setBackground(fillColor);
                int fillWidth = PERCENT_FILL_WIDTH * percentFull / 100 + 1;
                int x = xWidth - fillWidth - 2;
                gc.fillRectangle(x, event.y + 2, fillWidth, event.height - 4);
            }
            gc.setForeground(tree.getForeground());
            int x = xWidth - textSize.x - 2;
            Font oldFont = gc.getFont();
            gc.setFont(tree.getFont());
            gc.drawText(sizeText, x + 2, event.y + (event.height - textSize.y) / 2, true);
            gc.setFont(oldFont);
        }
    }
}
Also used : CTabFolder(org.eclipse.swt.custom.CTabFolder) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) TreeItem(org.eclipse.swt.widgets.TreeItem) Method(java.lang.reflect.Method) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) Format(java.text.Format) ByteNumberFormat(org.jkiss.utils.ByteNumberFormat) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) Property(org.jkiss.dbeaver.model.meta.Property)

Example 77 with Format

use of java.text.Format in project spf4j by zolyfarkas.

the class MessageFormat method subformat.

/**
 * Internal routine used by format. If <code>characterIterators</code> is non-null, AttributedCharacterIterator will
 * be created from the sub-formats as necessary. If <code>characterIterators</code> is null and <code>fp</code> is
 * non-null and identifies <code>Field.MESSAGE_ARGUMENT</code>, the location of the first replaced argument will be
 * set in it.
 *
 * @exception IllegalArgumentException if an argument in the <code>arguments</code> array is not of the type expected
 * by the format element(s) that use it.
 */
// Unfortunately I have no other way to write this
@SuppressFBWarnings({ "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS" })
private <// without code duplication to work for StringBuilder and StringBuffer....
T extends Appendable & CharSequence> void subformat(@Nullable Object[] arguments, @Nonnull T result, @Nullable FieldPosition fp, @Nullable List<AttributedCharacterIterator> characterIterators) throws IOException {
    int lastOffset = 0;
    int last = result.length();
    for (int i = 0; i <= maxOffset; ++i) {
        FormatInfo finfo = formats[i];
        int offset = finfo.getOffset();
        result.append(pattern, lastOffset, offset);
        lastOffset = offset;
        int argumentNumber = finfo.getArgumentNumber();
        if (arguments == null || argumentNumber >= arguments.length) {
            result.append('{').append(Integer.toString(argumentNumber)).append('}');
            continue;
        }
        Object obj = arguments[argumentNumber];
        String arg = null;
        Format subFormatter = null;
        Format fmt = finfo.getFormat();
        if (obj == null) {
            arg = "null";
        } else if (fmt != null) {
            subFormatter = fmt;
            if (subFormatter instanceof ChoiceFormat) {
                arg = subFormatter.format(obj);
                if (arg.indexOf('{') >= 0) {
                    subFormatter = new MessageFormat(arg, locale);
                    obj = arguments;
                    arg = null;
                }
            }
        } else if (obj instanceof Number) {
            // format number if can
            subFormatter = NumberFormat.getInstance(locale);
        } else if (obj instanceof Date) {
            // format a Date if can
            subFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, // fix
            locale);
        } else if (obj instanceof String) {
            arg = (String) obj;
        } else {
            arg = obj.toString();
        }
        // or arg is non-null and we should use it as the value.
        if (characterIterators != null) {
            // If characterIterators is non-null, it indicates we need
            // to get the CharacterIterator from the child formatter.
            int cl = result.length();
            if (last != cl) {
                characterIterators.add(createAttributedCharacterIterator(result.subSequence(last, cl).toString()));
                last = cl;
            }
            if (subFormatter != null) {
                AttributedCharacterIterator subIterator = subFormatter.formatToCharacterIterator(obj);
                append(result, subIterator);
                int cl2 = result.length();
                if (last != cl2) {
                    characterIterators.add(createAttributedCharacterIterator(subIterator, java.text.MessageFormat.Field.ARGUMENT, Integer.valueOf(argumentNumber)));
                    last = cl2;
                }
                arg = null;
            }
            if (arg != null && arg.length() > 0) {
                result.append(arg);
                characterIterators.add(createAttributedCharacterIterator(arg, java.text.MessageFormat.Field.ARGUMENT, Integer.valueOf(argumentNumber)));
                last = result.length();
            }
        } else {
            if (subFormatter != null) {
                arg = subFormatter.format(obj);
            }
            last = result.length();
            result.append(arg);
            if (i == 0 && fp != null && java.text.MessageFormat.Field.ARGUMENT.equals(fp.getFieldAttribute())) {
                fp.setBeginIndex(last);
                fp.setEndIndex(result.length());
            }
            last = result.length();
        }
    }
    result.append(pattern, lastOffset, pattern.length());
    if (characterIterators != null && last != result.length()) {
        characterIterators.add(createAttributedCharacterIterator(result.subSequence(last, result.length()).toString()));
    }
}
Also used : Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) NumberFormat(java.text.NumberFormat) DateFormat(java.text.DateFormat) DecimalFormat(java.text.DecimalFormat) ChoiceFormat(java.text.ChoiceFormat) ChoiceFormat(java.text.ChoiceFormat) Date(java.util.Date) AttributedCharacterIterator(java.text.AttributedCharacterIterator) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 78 with Format

use of java.text.Format in project mafscaling by vimsh.

the class ClosedLoop method createAfrDataTable.

private JTable createAfrDataTable(JPanel panel, String tableName, int gridy) {
    final JTable afrTable = new JTable() {

        private static final long serialVersionUID = 6526901361175099297L;

        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    DefaultTableColumnModel afrModel = new DefaultTableColumnModel();
    final TableColumn afrColumn = new TableColumn(0, 250);
    afrColumn.setHeaderValue(tableName);
    afrModel.addColumn(afrColumn);
    JTableHeader lblAfrTableName = afrTable.getTableHeader();
    lblAfrTableName.setColumnModel(afrModel);
    lblAfrTableName.setReorderingAllowed(false);
    DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer) lblAfrTableName.getDefaultRenderer();
    headerRenderer.setHorizontalAlignment(SwingConstants.LEFT);
    GridBagConstraints gbc_lblAfrTableName = new GridBagConstraints();
    gbc_lblAfrTableName.insets = new Insets((gridy == 0 ? 0 : 5), 0, 0, 0);
    gbc_lblAfrTableName.anchor = GridBagConstraints.PAGE_START;
    gbc_lblAfrTableName.fill = GridBagConstraints.HORIZONTAL;
    gbc_lblAfrTableName.gridx = 0;
    gbc_lblAfrTableName.gridy = gridy;
    panel.add(lblAfrTableName, gbc_lblAfrTableName);
    afrTable.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            afrColumn.setWidth(afrTable.getWidth());
        }
    });
    Utils.addTableHeaderHighlight(afrTable);
    afrTable.getTableHeader().setReorderingAllowed(false);
    afrTable.setColumnSelectionAllowed(true);
    afrTable.setCellSelectionEnabled(true);
    afrTable.setBorder(new LineBorder(new Color(0, 0, 0)));
    afrTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    afrTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    afrTable.setModel(new DefaultTableModel(AfrTableRowCount, AfrTableColumnCount));
    Utils.initializeTable(afrTable, ColumnWidth);
    if (tableName.equals(Afr1TableName)) {
        Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") } };
        NumberFormatRenderer renderer = (NumberFormatRenderer) afrTable.getDefaultRenderer(Object.class);
        renderer.setFormats(formatMatrix);
    } else if (tableName.equals(Afr2TableName)) {
        Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") }, { new DecimalFormat("#"), new DecimalFormat("#") } };
        NumberFormatRenderer renderer = (NumberFormatRenderer) afrTable.getDefaultRenderer(Object.class);
        renderer.setFormats(formatMatrix);
    }
    GridBagConstraints gbc_afrTable = new GridBagConstraints();
    gbc_afrTable.insets = new Insets(0, 0, 0, 0);
    gbc_afrTable.anchor = GridBagConstraints.PAGE_START;
    gbc_afrTable.gridx = 0;
    gbc_afrTable.gridy = gridy + 1;
    panel.add(afrTable, gbc_afrTable);
    excelAdapter.addTable(afrTable, true, false);
    return afrTable;
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) LineBorder(javax.swing.border.LineBorder) Color(java.awt.Color) DefaultTableModel(javax.swing.table.DefaultTableModel) DecimalFormat(java.text.DecimalFormat) JTableHeader(javax.swing.table.JTableHeader) TableColumn(javax.swing.table.TableColumn) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) Format(java.text.Format) DecimalFormat(java.text.DecimalFormat) JTable(javax.swing.JTable) ComponentEvent(java.awt.event.ComponentEvent) ComponentAdapter(java.awt.event.ComponentAdapter)

Example 79 with Format

use of java.text.Format in project mzmine2 by mzmine.

the class IntensityPlotTooltipGenerator method generateToolTip.

/**
 * @see org.jfree.chart.labels.CategoryToolTipGenerator#generateToolTip(org.jfree.data.category.CategoryDataset,
 *      int, int)
 */
public String generateToolTip(CategoryDataset dataset, int row, int column) {
    Format intensityFormat = MZmineCore.getConfiguration().getIntensityFormat();
    Feature[] peaks = ((IntensityPlotDataset) dataset).getPeaks(row, column);
    RawDataFile[] files = ((IntensityPlotDataset) dataset).getFiles(column);
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < files.length; i++) {
        sb.append(files[i].getName());
        sb.append(": ");
        if (peaks[i] != null) {
            sb.append(peaks[i].toString());
            sb.append(", height: ");
            sb.append(intensityFormat.format(peaks[i].getHeight()));
        } else {
            sb.append("N/A");
        }
        sb.append("\n");
    }
    return sb.toString();
}
Also used : Format(java.text.Format) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) Feature(net.sf.mzmine.datamodel.Feature)

Example 80 with Format

use of java.text.Format in project mzmine2 by mzmine.

the class SimplePeakListRow method toString.

@Override
public String toString() {
    StringBuffer buf = new StringBuffer();
    Format mzFormat = MZmineCore.getConfiguration().getMZFormat();
    Format timeFormat = MZmineCore.getConfiguration().getRTFormat();
    buf.append("#" + myID + " ");
    buf.append(mzFormat.format(getAverageMZ()));
    buf.append(" m/z @");
    buf.append(timeFormat.format(getAverageRT()));
    if (preferredIdentity != null)
        buf.append(" " + preferredIdentity.getName());
    if ((comment != null) && (comment.length() > 0))
        buf.append(" (" + comment + ")");
    return buf.toString();
}
Also used : Format(java.text.Format)

Aggregations

Format (java.text.Format)160 SimpleDateFormat (java.text.SimpleDateFormat)63 DecimalFormat (java.text.DecimalFormat)39 Test (org.junit.Test)38 DateFormat (java.text.DateFormat)34 Date (java.util.Date)32 NumberFormat (java.text.NumberFormat)29 DateTimeFormatter (java.time.format.DateTimeFormatter)27 ChoiceFormat (java.text.ChoiceFormat)24 ParsePosition (java.text.ParsePosition)22 MessageFormat (java.text.MessageFormat)20 FieldPosition (java.text.FieldPosition)16 Test (org.testng.annotations.Test)16 IOException (java.io.IOException)15 ParseException (java.text.ParseException)11 ArrayList (java.util.ArrayList)11 Locale (java.util.Locale)10 AttributedString (java.text.AttributedString)9 TemporalAccessor (java.time.temporal.TemporalAccessor)7 Calendar (java.util.Calendar)7