Search in sources :

Example 16 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class TextArea method initRowString.

private void initRowString() {
    if (!Display.getInstance().isEdt()) {
        if (rowStrings == null) {
            rowStrings = new ArrayList();
            rowStrings.add(getText());
            return;
        }
    }
    Style style = getUnselectedStyle();
    rowStrings = new ArrayList();
    widthForRowCalculations = getWidth() - style.getHorizontalPadding();
    // to allow subclasses to override it
    if (isSingleLineTextArea()) {
        rowStrings.add(getText());
        return;
    }
    if (widthForRowCalculations <= 0) {
        rowStrings.add(getText());
        setShouldCalcPreferredSize(true);
        return;
    }
    if (text == null || text.equals("")) {
        return;
    }
    Font font = style.getFont();
    if (actAsLabel && text.length() <= columns && text.indexOf('\n') < 0) {
        int w = font.stringWidth(text);
        if (w <= getWidth()) {
            if (rowStrings == null) {
                rowStrings = new ArrayList();
                rowStrings.add(getText());
                return;
            } else {
                rowStrings.clear();
                rowStrings.add(getText());
                return;
            }
        }
    }
    char[] text = preprocess(getText());
    int rows = this.rows;
    if (growByContent) {
        rows = Math.max(rows, getLines());
    }
    int charWidth = font.charWidth(widestChar);
    Style selectedStyle = getSelectedStyle();
    if (selectedStyle.getFont() != style.getFont()) {
        int cw = selectedStyle.getFont().charWidth(widestChar);
        if (cw > charWidth) {
            charWidth = cw;
            font = selectedStyle.getFont();
        }
    }
    style = getStyle();
    int tPadding = style.getHorizontalPadding();
    int textAreaWidth = getWidth() - tPadding;
    /*if(textAreaWidth <= 0) {
            if(columns < 1) {
                textAreaWidth = Math.min(Display.getInstance().getDisplayWidth() - tPadding, getText().length()) * charWidth;
            } else {
                textAreaWidth = Math.min(Display.getInstance().getDisplayWidth() - tPadding, columns) * charWidth;
            }
        }*/
    if (textAreaWidth <= charWidth) {
        if (!isInitialized()) {
            rowStrings.add(getText());
        } else {
            // special case for the edge case of "no room".
            // Its important since sometimes this case occurs in the GUI builder by accident
            int tlen = text.length;
            for (int iter = 0; iter < tlen; iter++) {
                rowStrings.add("" + text[iter]);
            }
        }
        return;
    }
    int minCharactersInRow = Math.max(1, textAreaWidth / charWidth);
    int from = 0;
    int to = from + minCharactersInRow;
    int textLength = text.length;
    String rowText = null;
    int i, spaceIndex;
    // width to accommodate it
    if (textLength / minCharactersInRow > Math.max(2, rows)) {
        textAreaWidth -= getUIManager().getLookAndFeel().getVerticalScrollWidth();
        textAreaWidth -= charWidth / 2;
    }
    String unsupported = getUnsupportedChars();
    /*
        iteration over the string using indexes, from - the beginning of the row , to - end of a row
        for each row we will try to search for a "space" character at the end of the row ( row is text area available width)
        indorder to improve the efficiency we do not search an entire row but we start from minCharactersInRow which indicates
        what is the minimum amount of characters that can feet in the text area width.
        if we dont find we will go backwards and search for the first space available,
        if there is no space in the entire row we will cut the line inorder to fit in.
         */
    // Don't rely on the fact that short text has no newline character. we always have to parse the text.
    to = Math.max(Math.min(textLength - 1, to), 0);
    while (to < textLength) {
        if (to > textLength) {
            to = textLength;
        }
        spaceIndex = -1;
        rowText = "";
        int maxLength = to;
        if (useStringWidth || actAsLabel) {
            // fix for an infinite loop issue: http://forums.java.net/jive/thread.jspa?messageID=482802
            // currentRowWidth = 0;
            String currentRow = "";
            // search for "space" character at close as possible to the end of the row
            for (i = to; i < textLength && fastCharWidthCheck(text, from, i - from + 1, textAreaWidth, charWidth, font); i++) {
                char c = text[i];
                /*if(updateRowWidth(c, font) >= textAreaWidth) {
                        break;
                    }*/
                currentRow += c;
                if (font.stringWidth(currentRow) >= textAreaWidth) {
                    break;
                }
                if (unsupported.indexOf(c) > -1) {
                    text[i] = ' ';
                    c = ' ';
                }
                if (c == ' ' || c == '\n') {
                    spaceIndex = i;
                    // newline has been found. We can end the loop here as the line cannot grow more
                    if (c == '\n')
                        break;
                }
                maxLength++;
            }
        } else {
            currentRowWidth = 0;
            if (to != from) {
                currentRowWidth = font.charsWidth(text, from, to - from);
            }
            // search for "space" character at close as possible to the end of the row
            for (i = to; i < textLength; i++) {
                char c = text[i];
                if (updateRowWidth(c, font) >= textAreaWidth) {
                    break;
                }
                if (unsupported.indexOf(c) > -1) {
                    text[i] = ' ';
                    c = ' ';
                }
                if (c == ' ' || c == '\n') {
                    spaceIndex = i;
                    // newline has been found. We can end the loop here as the line cannot grow more
                    if (c == '\n')
                        break;
                }
                maxLength++;
            }
        }
        // also if space is next character (in the next row) we can cut the line
        if (i == textLength || text[i] == ' ' || text[i] == '\n') {
            spaceIndex = i;
        }
        // if we found space in the limit width of the row (searched only from minCharactersInRow)
        if (spaceIndex != -1) {
            // make sure that if we have a newline character before the end of the line we should
            // break there instead
            int newLine = indexOf(text, '\n', from, spaceIndex - from);
            if (newLine > -1 && newLine < spaceIndex) {
                spaceIndex = newLine;
            }
            rowText = new String(text, from, spaceIndex - from);
            from = spaceIndex + 1;
        } else // if there is no space from minCharactersInRow to limit need to search backwards
        {
            for (i = to; spaceIndex == -1 && i >= from; i--) {
                char chr = text[i];
                if (chr == ' ' || chr == '\n' || chr == '\t') {
                    spaceIndex = i;
                    // don't forget to search for line breaks in the
                    // remaining part. otherwise we overlook possible
                    // line breaks!
                    int newLine = indexOf(text, '\n', from, i - from);
                    if (newLine > -1 && newLine < spaceIndex) {
                        spaceIndex = newLine;
                    }
                    rowText = new String(text, from, spaceIndex - from);
                    from = spaceIndex + 1;
                }
            }
            if (spaceIndex == -1) {
                // from = to + 1;
                if (maxLength <= 0) {
                    maxLength = 1;
                }
                spaceIndex = maxLength;
                rowText = new String(text, from, spaceIndex - from);
                from = spaceIndex;
            }
        }
        if (rowText.length() == 0) {
            // This happens due to a race condition or something, no idea why???
            if (textAreaWidth <= charWidth) {
                if (!isInitialized()) {
                    rowStrings.add(getText());
                } else {
                    // special case for the edge case of "no room".
                    // Its important since sometimes this case occurs in the GUI builder by accident
                    int tlen = text.length;
                    for (int iter = 0; iter < tlen; iter++) {
                        rowStrings.add("" + text[iter]);
                    }
                }
                return;
            }
        }
        rowStrings.add(rowText);
        // adding minCharactersInRow doesn't work if what is left is less
        // then minCharactersInRow
        // +minCharactersInRow;
        to = from;
    }
    if (text[text.length - 1] == '\n') {
        rowStrings.add("");
    }
}
Also used : ArrayList(java.util.ArrayList) Style(com.codename1.ui.plaf.Style)

Example 17 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class SQLMap method select.

/**
 * Fetches the components from the database matching the given cmp description, the fields that aren't
 * null within the cmp will match the where clause
 * @param cmp the component to match
 * @param orderBy the column to order by, can be null to ignore order
 * @param ascending true to indicate ascending order
 * @param maxElements the maximum number of elements returned can be 0 or lower to ignore
 * @param page  the page within the query to match the max elements value
 * @return the result of the query
 */
public java.util.List<PropertyBusinessObject> select(PropertyBusinessObject cmp, Property orderBy, boolean ascending, int maxElements, int page) throws IOException, InstantiationException {
    String tableName = getTableName(cmp);
    StringBuilder createStatement = new StringBuilder("SELECT * FROM ");
    createStatement.append(tableName);
    ArrayList<Object> params = new ArrayList<Object>();
    createStatement.append(" WHERE ");
    boolean found = false;
    for (PropertyBase p : cmp.getPropertyIndex()) {
        if (p instanceof Property) {
            if (((Property) p).get() != null) {
                if (found) {
                    createStatement.append(" AND ");
                }
                found = true;
                params.add(((Property) p).get());
                createStatement.append(getColumnName(p));
                createStatement.append(" = ?");
            }
        }
    }
    // all properties are null undo the where append
    if (!found) {
        createStatement = new StringBuilder("SELECT * FROM ");
        createStatement.append(tableName);
    }
    if (orderBy != null) {
        createStatement.append(" ORDER BY ");
        createStatement.append(getColumnName(orderBy));
        if (!ascending) {
            createStatement.append(" DESC");
        }
    }
    if (maxElements > 0) {
        createStatement.append(" LIMIT ");
        createStatement.append(maxElements);
        if (page > 0) {
            createStatement.append(" OFFSET ");
            createStatement.append(page * maxElements);
        }
    }
    Cursor c = null;
    try {
        ArrayList<PropertyBusinessObject> response = new ArrayList<PropertyBusinessObject>();
        c = executeQuery(createStatement.toString(), params.toArray());
        while (c.next()) {
            PropertyBusinessObject pb = (PropertyBusinessObject) cmp.getClass().newInstance();
            for (PropertyBase p : pb.getPropertyIndex()) {
                Row currentRow = c.getRow();
                SqlType t = getSqlType(p);
                if (t == SqlType.SQL_EXCLUDE) {
                    continue;
                }
                Object value = t.getValue(currentRow, c.getColumnIndex(getColumnName(p)), p);
                if (p instanceof Property) {
                    ((Property) p).set(value);
                }
            }
            response.add(pb);
        }
        c.close();
        return response;
    } catch (Throwable t) {
        Log.e(t);
        if (c != null) {
            c.close();
        }
        if (t instanceof IOException) {
            throw ((IOException) t);
        } else {
            throw new IOException(t.toString());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(com.codename1.db.Cursor) Row(com.codename1.db.Row)

Example 18 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class Table method paintGlass.

/**
 * {@inheritDoc}
 */
protected void paintGlass(Graphics g) {
    if ((drawBorder) && (innerBorder != INNER_BORDERS_NONE)) {
        int xPos = getAbsoluteX();
        int yPos = getAbsoluteY();
        g.translate(xPos, yPos);
        int rows = model.getRowCount();
        int cols = model.getColumnCount();
        if (includeHeader) {
            rows++;
        }
        g.setColor(getStyle().getFgColor());
        TableLayout t = (TableLayout) getLayout();
        int actualWidth = Math.max(getWidth(), getScrollDimension().getWidth());
        int actualHeight = Math.max(getHeight(), getScrollDimension().getHeight());
        if (// inner borders cols/rows are supported only in collapsed mode
        (collapseBorder) || (innerBorder != INNER_BORDERS_ALL) || (t.hasHorizontalSpanning()) || (t.hasVerticalSpanning())) {
            // TODO - We currently don't support separate borders for tables with spanned cells
            if ((innerBorder == INNER_BORDERS_ALL) || (innerBorder == INNER_BORDERS_ROWS)) {
                if (t.hasVerticalSpanning()) {
                    // the components other than the ones that are at the last column.
                    for (int cellRow = 0; cellRow < rows - 1; cellRow++) {
                        for (int cellColumn = 0; cellColumn < cols; cellColumn++) {
                            // if this isn't the last row
                            if (cellRow + t.getCellVerticalSpan(cellRow, cellColumn) - 1 != rows - 1) {
                                // if this is a spanned through cell we don't want to draw a line here
                                if (t.isCellSpannedThroughHorizontally(cellRow, cellColumn)) {
                                    continue;
                                }
                                int x = t.getColumnPosition(cellColumn);
                                int y = t.getRowPosition(cellRow);
                                int rowHeight = t.getRowPosition(cellRow + t.getCellVerticalSpan(cellRow, cellColumn)) - y;
                                int columnWidth;
                                if (cellColumn < getModel().getColumnCount() - 1) {
                                    columnWidth = t.getColumnPosition(cellColumn + 1) - x;
                                } else {
                                    columnWidth = getWidth() - y;
                                }
                                if ((innerBorder != INNER_BORDERS_ROWS) || (shouldDrawInnerBorderAfterRow(cellRow))) {
                                    g.drawLine(x, y + rowHeight, x + columnWidth, y + rowHeight);
                                }
                            }
                        }
                    }
                } else {
                    // this is much faster since we don't need to check spanning
                    for (int row = 1; row < rows; row++) {
                        int y = t.getRowPosition(row);
                        if ((innerBorder != INNER_BORDERS_ROWS) || (shouldDrawInnerBorderAfterRow(row - 1))) {
                            g.drawLine(0, y, actualWidth, y);
                        }
                    // g.drawLine(0+2, y+2, actualWidth-2, y+2);
                    }
                }
            }
            if ((innerBorder == INNER_BORDERS_ALL) || (innerBorder == INNER_BORDERS_COLS)) {
                if (t.hasHorizontalSpanning()) {
                    // the components other than the ones that are at the last column.
                    for (int cellRow = 0; cellRow < rows; cellRow++) {
                        for (int cellColumn = 0; cellColumn < cols - 1; cellColumn++) {
                            // if this isn't the last column
                            if (cellColumn + t.getCellHorizontalSpan(cellRow, cellColumn) - 1 != cols - 1) {
                                // if this is a spanned through cell we don't want to draw a line here
                                if (t.isCellSpannedThroughVertically(cellRow, cellColumn)) {
                                    continue;
                                }
                                int x = t.getColumnPosition(cellColumn);
                                int y = t.getRowPosition(cellRow);
                                int rowHeight;
                                int columnWidth = t.getColumnPosition(cellColumn + t.getCellHorizontalSpan(cellRow, cellColumn)) - x;
                                if (cellRow < getModel().getRowCount() - 1) {
                                    rowHeight = t.getRowPosition(cellRow + 1) - y;
                                } else {
                                    rowHeight = getHeight() - y;
                                }
                                g.drawLine(x + columnWidth, y, x + columnWidth, y + rowHeight);
                            }
                            if (t.getCellHorizontalSpan(cellRow, cellColumn) > 1) {
                                cellColumn += t.getCellHorizontalSpan(cellRow, cellColumn) - 1;
                            }
                        }
                    }
                } else {
                    for (int col = 1; col < cols; col++) {
                        int x = t.getColumnPosition(col);
                        g.drawLine(x, 0, x, actualHeight);
                    // g.drawLine(x+2, 0+2, x+2, actualHeight-2);
                    }
                }
            }
        } else {
            // if ((!t.hasHorizontalSpanning()) && (!t.hasVerticalSpanning())) {
            for (int row = 0; row < rows; row++) {
                int y = t.getRowPosition(row);
                int h;
                if (row + 1 < rows) {
                    h = t.getRowPosition(row + 1) - y;
                } else {
                    h = getY() + actualHeight - y - 2;
                }
                for (int col = 0; col < cols; col++) {
                    int x = t.getColumnPosition(col);
                    int w;
                    if (col + 1 < cols) {
                        w = t.getColumnPosition(col + 1) - x;
                    } else {
                        w = getX() + actualWidth - x - 2;
                    }
                    Component comp = t.getComponentAt(row, col);
                    if ((comp.isVisible()) && ((drawEmptyCellsBorder) || ((comp.getWidth() - comp.getStyle().getPaddingRightNoRTL() - comp.getStyle().getPaddingLeftNoRTL() > 0) && (comp.getHeight() - comp.getStyle().getPaddingTop() - comp.getStyle().getPaddingBottom() > 0)))) {
                        int rightMargin = comp.getStyle().getMarginRightNoRTL();
                        int bottomMargin = comp.getStyle().getMarginBottom();
                        if (col == 0) {
                            // Since the first cell includes margins from both sides (left/right) so the next cell location is farther away - but we don't want to paint the border up to it
                            rightMargin *= 2;
                        }
                        if (row == 0) {
                            bottomMargin *= 2;
                        }
                        g.drawRect(x + comp.getStyle().getMarginLeftNoRTL(), y + comp.getStyle().getMarginTop(), w - 2 - rightMargin, h - 2 - bottomMargin);
                    }
                }
            }
        }
        g.translate(-xPos, -yPos);
    }
}
Also used : Component(com.codename1.ui.Component)

Example 19 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class Table method updateModel.

private void updateModel() {
    int selectionRow = -1, selectionColumn = -1;
    Form f = getComponentForm();
    if (f != null) {
        Component c = f.getFocused();
        if (c != null) {
            selectionRow = getCellRow(c);
            selectionColumn = getCellColumn(c);
        }
    }
    removeAll();
    int columnCount = model.getColumnCount();
    // another row for the table header
    if (includeHeader) {
        setLayout(new TableLayout(model.getRowCount() + 1, columnCount));
        for (int iter = 0; iter < columnCount; iter++) {
            String name = model.getColumnName(iter);
            Component header = createCellImpl(name, -1, iter, false);
            TableLayout.Constraint con = createCellConstraint(name, -1, iter);
            addComponent(con, header);
        }
    } else {
        setLayout(new TableLayout(model.getRowCount(), columnCount));
    }
    for (int r = 0; r < model.getRowCount(); r++) {
        for (int c = 0; c < columnCount; c++) {
            Object value = model.getValueAt(r, c);
            // null should be returned for spanned over values
            if (value != null || includeNullValues()) {
                boolean e = model.isCellEditable(r, c);
                Component cell = createCellImpl(value, r, c, e);
                if (cell != null) {
                    TableLayout.Constraint con = createCellConstraint(value, r, c);
                    // returns the current row we iterate about
                    int currentRow = ((TableLayout) getLayout()).getNextRow();
                    if (r > model.getRowCount()) {
                        return;
                    }
                    addComponent(con, cell);
                    if (r == selectionRow && c == selectionColumn) {
                        cell.requestFocus();
                    }
                }
            }
        }
    }
}
Also used : Form(com.codename1.ui.Form) Component(com.codename1.ui.Component)

Example 20 with Row

use of com.codename1.db.Row in project CodenameOne by codenameone.

the class Table method updateMargins.

private void updateMargins() {
    TableLayout t = (TableLayout) getLayout();
    int hSpace = horizontalBorderSpacing;
    int vSpace = verticalBorderSpacing;
    if (collapseBorder) {
        // not relevant for collapse border
        hSpace = 0;
        vSpace = 0;
    }
    if ((!t.hasHorizontalSpanning()) && (!t.hasVerticalSpanning())) {
        for (int row = 0; row < t.getRows(); row++) {
            for (int col = 0; col < t.getColumns(); col++) {
                Component cmp = null;
                try {
                    cmp = t.getComponentAt(row, col);
                } catch (Exception e) {
                // parent of cmp can be null as well - TODO - check why
                }
                if (cmp != null) {
                    int leftMargin = (col == 0) ? hSpace : 0;
                    int topMargin = (row == 0) ? vSpace : 0;
                    cmp.getUnselectedStyle().setMargin(topMargin, vSpace, leftMargin, hSpace);
                    cmp.getSelectedStyle().setMargin(topMargin, vSpace, leftMargin, hSpace);
                }
            }
        }
    }
    repaint();
}
Also used : Component(com.codename1.ui.Component)

Aggregations

Component (com.codename1.ui.Component)8 Style (com.codename1.ui.plaf.Style)5 ArrayList (java.util.ArrayList)4 Point (java.awt.Point)3 EventObject (java.util.EventObject)3 Hashtable (java.util.Hashtable)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 TableLayout (com.codename1.ui.table.TableLayout)2 EmbeddedContainer (com.codename1.ui.util.EmbeddedContainer)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 List (java.util.List)2 StringTokenizer (java.util.StringTokenizer)2 JButton (javax.swing.JButton)2 JList (javax.swing.JList)2 Cursor (com.codename1.db.Cursor)1 Row (com.codename1.db.Row)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 Button (com.codename1.ui.Button)1 Container (com.codename1.ui.Container)1