Search in sources :

Example 21 with Table

use of com.codename1.ui.table.Table in project CodenameOne by codenameone.

the class FaceBookAccess method getUserNotifications.

/**
 * Gets the user notifications (this method uses the legacy rest api see http://developers.facebook.com/docs/reference/rest/)
 *
 * @param userId the user id
 * @param startTime Indicates the earliest time to return a notification.
 * This equates to the updated_time field in the notification FQL table. If not specified, this call returns all available notifications.
 * @param includeRead Indicates whether to include notifications that have already been read.
 * By default, notifications a user has read are not included.
 * @param notifications store notifications results into the given model,
 * each entry is an Hashtable Object contaning the Object data
 * @param callback the callback that should be updated when the data arrives
 */
public void getUserNotifications(String userId, String startTime, boolean includeRead, DefaultListModel notifications, final ActionListener callback) throws IOException {
    checkAuthentication();
    final FacebookRESTService con = new FacebookRESTService(token, "https://api.facebook.com/method/notifications.getList", false);
    con.addArgument("start_time", startTime);
    con.addArgument("include_read", new Boolean(includeRead).toString());
    con.addArgument("format", "json");
    con.setResponseDestination(notifications);
    con.addResponseListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (!con.isAlive()) {
                return;
            }
            if (callback != null) {
                callback.actionPerformed(evt);
            }
        }
    });
    if (slider != null) {
        SliderBridge.bindProgress(con, slider);
    }
    for (int i = 0; i < responseCodeListeners.size(); i++) {
        con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
    }
    current = con;
    NetworkManager.getInstance().addToQueue(con);
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 22 with Table

use of com.codename1.ui.table.Table in project CodenameOne by codenameone.

the class FaceBookAccess method createObjectsModel.

/**
 * This is a utility method that transforms a DefaultListModel that contains Hashtable entries
 * into a DefaultListModel that will contain FBObject objects that will be initialized with the Hashtable entries
 *
 * @param hashtablesModel the model to transform, this model should hold it's data has Hashtable entries
 * @param fbObjectClass this is the class of the entries to be created, this class should be a FBObject type
 * @return a DefaultListModel with fbObjectClass objects entries
 * @throws IllegalAccessException if the fbObjectClass.newInstance() fails
 * @throws InstantiationExceptionif the fbObjectClass.newInstance() fails
 */
public static DefaultListModel createObjectsModel(DefaultListModel hashtablesModel, Class fbObjectClass) throws IllegalAccessException, InstantiationException {
    DefaultListModel model = new DefaultListModel();
    for (int i = 0; i < hashtablesModel.getSize(); i++) {
        Hashtable table = (Hashtable) hashtablesModel.getItemAt(i);
        FBObject obj = (FBObject) fbObjectClass.newInstance();
        obj.copy(table);
        model.addItem(obj);
    }
    return model;
}
Also used : Hashtable(java.util.Hashtable) DefaultListModel(com.codename1.ui.list.DefaultListModel)

Example 23 with Table

use of com.codename1.ui.table.Table in project CodenameOne by codenameone.

the class Oauth2 method handleURL.

private void handleURL(String url, WebBrowser web, final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {
    if ((url.startsWith(redirectURI))) {
        if (progress != null && Display.getInstance().getCurrent() == progress) {
            progress.dispose();
        }
        if (web != null) {
            web.stop();
        }
        // remove the browser component.
        if (login != null) {
            login.removeAll();
            login.revalidate();
        }
        if (url.indexOf("code=") > -1) {
            Hashtable params = getParamsFromURL(url);
            handleRedirectURLParams(params);
            class TokenRequest extends ConnectionRequest {

                boolean callbackCalled;

                protected void readResponse(InputStream input) throws IOException {
                    byte[] tok = Util.readInputStream(input);
                    String t = new String(tok);
                    boolean expiresRelative = true;
                    if (t.startsWith("{")) {
                        JSONParser p = new JSONParser();
                        Map map = p.parseJSON(new StringReader(t));
                        handleTokenRequestResponse(map);
                    } else {
                        handleTokenRequestResponse(t);
                    }
                    if (login != null) {
                        login.dispose();
                    }
                }

                protected void handleException(Exception err) {
                    if (backToForm != null && !callbackCalled) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        if (!callbackCalled) {
                            callbackCalled = true;
                            al.actionPerformed(new ActionEvent(err, ActionEvent.Type.Exception));
                        }
                    }
                }

                protected void postResponse() {
                    if (backToParent && backToForm != null && !callbackCalled) {
                        backToForm.showBack();
                    }
                    if (al != null) {
                        if (!callbackCalled) {
                            callbackCalled = true;
                            if (getResponseCode() >= 200 && getResponseCode() < 300) {
                                al.actionPerformed(new ActionEvent(new AccessToken(token, expires, refreshToken, identityToken), ActionEvent.Type.Response));
                            } else {
                                al.actionPerformed(new ActionEvent(new IOException(getResponseErrorMessage()), ActionEvent.Type.Exception));
                            }
                        }
                    }
                }
            }
            ;
            final TokenRequest req = new TokenRequest();
            req.setReadResponseForErrors(true);
            req.setUrl(tokenRequestURL);
            req.setPost(true);
            req.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.addArgument("client_id", clientId);
            req.addArgument("redirect_uri", redirectURI);
            req.addArgument("client_secret", clientSecret);
            if (params.containsKey("cn1_refresh_token")) {
                req.addArgument("grant_type", "refresh_token");
                req.addArgument("refresh_token", (String) params.get("code"));
            } else {
                req.addArgument("code", (String) params.get("code"));
                req.addArgument("grant_type", "authorization_code");
            }
            NetworkManager.getInstance().addToQueue(req);
        } else if (url.indexOf("error_reason=") > -1) {
            Hashtable table = getParamsFromURL(url);
            String error = (String) table.get("error_reason");
            if (login != null) {
                login.dispose();
            }
            if (backToForm != null) {
                backToForm.showBack();
            }
            if (al != null) {
                al.actionPerformed(new ActionEvent(new IOException(error), ActionEvent.Type.Exception));
            }
        } else {
            boolean success = url.indexOf("#") > -1;
            if (success) {
                String accessToken = url.substring(url.indexOf("#") + 1);
                if (accessToken.indexOf("&") > 0) {
                    token = accessToken.substring(accessToken.indexOf("=") + 1, accessToken.indexOf("&"));
                } else {
                    token = accessToken.substring(accessToken.indexOf("=") + 1);
                }
                if (login != null) {
                    login.dispose();
                }
                if (backToParent && backToForm != null) {
                    backToForm.showBack();
                }
                if (al != null) {
                    al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
                }
            }
        }
    } else {
        if (frm != null && Display.getInstance().getCurrent() != frm) {
            progress.dispose();
            frm.show();
        }
    }
}
Also used : Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) IOException(java.io.IOException) StringReader(com.codename1.util.regex.StringReader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with Table

use of com.codename1.ui.table.Table in project CodenameOne by codenameone.

the class L10nEditor method initTable.

private void initTable() {
    bundleTable.setModel(new AbstractTableModel() {

        public int getRowCount() {
            return keys.size();
        }

        public int getColumnCount() {
            return 1 + localeList.size();
        }

        public boolean isCellEditable(int row, int col) {
            boolean b = col != 0;
            if (b) {
                String s = (String) getValueAt(row, col);
                return s == null || !s.contains("\n");
            }
            return b;
        }

        public String getColumnName(int columnIndex) {
            if (columnIndex == 0) {
                return "Key";
            }
            return (String) localeList.get(columnIndex - 1);
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                return keys.get(rowIndex);
            }
            Hashtable h = res.getL10N(localeName, (String) localeList.get(columnIndex - 1));
            return h.get(keys.get(rowIndex));
        }

        public void setValueAt(Object val, int rowIndex, int columnIndex) {
            res.setModified();
            if (columnIndex == 0) {
                if (!keys.contains(val)) {
                // ...
                }
                return;
            }
            // Hashtable h = (Hashtable)bundle.get(localeList.get(columnIndex - 1));
            // h.put(keys.get(rowIndex), val);
            String currentKey = (String) keys.get(rowIndex);
            res.setLocaleProperty(localeName, (String) localeList.get(columnIndex - 1), currentKey, val);
            if (currentKey.equals("@im")) {
                StringTokenizer tok = new StringTokenizer((String) val, "|");
                boolean modified = false;
                while (tok.hasMoreTokens()) {
                    String currentIm = tok.nextToken();
                    if ("ABC".equals(currentIm) || "123".equals(currentIm) || "Abc".equals(currentIm) || "abc".equals(currentIm)) {
                        continue;
                    }
                    String prop = "@im-" + currentIm;
                    if (!keys.contains(prop)) {
                        keys.add(prop);
                        for (Object locale : localeList) {
                            res.setLocaleProperty(localeName, (String) locale, prop, "");
                        }
                        modified = true;
                    }
                }
                if (modified) {
                    fireTableDataChanged();
                }
                return;
            }
            if (currentKey.equals("@vkb")) {
                boolean modified = false;
                StringTokenizer tok = new StringTokenizer((String) val, "|");
                while (tok.hasMoreTokens()) {
                    String currentIm = tok.nextToken();
                    if ("ABC".equals(currentIm) || "123".equals(currentIm) || ".,123".equals(currentIm) || ".,?".equals(currentIm)) {
                        continue;
                    }
                    String prop = "@vkb-" + currentIm;
                    if (!keys.contains(prop)) {
                        keys.add(prop);
                        for (Object locale : localeList) {
                            res.setLocaleProperty(localeName, (String) locale, prop, "");
                        }
                        modified = true;
                    }
                }
                if (modified) {
                    fireTableDataChanged();
                }
            }
        }
    });
    bundleTable.setDefaultRenderer(Object.class, new SwingRenderer() {

        private JCheckBox chk = new JCheckBox();

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (column > 0) {
                // constant value
                String key = (String) keys.get(row);
                if (key.startsWith("@")) {
                    if (key.equalsIgnoreCase("@rtl")) {
                        chk.setSelected(value != null && "true".equalsIgnoreCase(value.toString()));
                        updateComponentSelectedState(chk, isSelected, table, row, column, hasFocus);
                        return chk;
                    }
                    if (key.startsWith("@vkb") || key.startsWith("@im")) {
                        JButton b = new JButton("...");
                        updateComponentSelectedState(b, isSelected, table, row, column, hasFocus);
                        return b;
                    }
                }
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    });
    bundleTable.setDefaultEditor(Object.class, new DefaultCellEditor(new JTextField()) {

        private Object currentValue;

        String editedKey;

        private DefaultCellEditor standardEditor = new DefaultCellEditor(new JTextField());

        private DefaultCellEditor buttonEditor = new DefaultCellEditor(new JTextField()) {

            private JButton button = new JButton("...");

            {
                button.setBorderPainted(false);
                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        if (editedKey.equals("@vkb") || editedKey.equals("@im")) {
                            currentValue = editInputModeOrder((String) currentValue, editedKey.equals("@vkb"));
                            fireEditingStoppedExt();
                            return;
                        }
                        /*if(editedKey.startsWith("@vkb")) {
                                VKBEditor v = new VKBEditor(button, editedKey.substring(5), (String)currentValue);
                                currentValue = v.getValue();
                                fireEditingStoppedExt();
                                return;
                            }*/
                        if (editedKey.startsWith("@im")) {
                            currentValue = editTextFieldInputMode((String) currentValue);
                            fireEditingStoppedExt();
                            return;
                        }
                    }
                });
            }

            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                editedKey = (String) keys.get(row);
                return button;
            }
        };

        private DefaultCellEditor checkBoxEditor = new DefaultCellEditor(new JCheckBox()) {

            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                return super.getTableCellEditorComponent(table, new Boolean("true".equalsIgnoreCase((String) value)), isSelected, row, column);
            }

            public Object getCellEditorValue() {
                Boolean b = (Boolean) super.getCellEditorValue();
                if (b.booleanValue()) {
                    return "true";
                }
                return "false";
            }
        };

        private TableCellEditor current = standardEditor;

        {
            buttonEditor.setClickCountToStart(1);
            checkBoxEditor.setClickCountToStart(1);
        }

        private void updateEditor(int row) {
            // constant value
            final String key = (String) keys.get(row);
            if (key.startsWith("@")) {
                if (key.equalsIgnoreCase("@rtl")) {
                    current = checkBoxEditor;
                    return;
                }
                if (key.startsWith("@vkb") || key.startsWith("@im")) {
                    current = buttonEditor;
                    return;
                }
            }
            current = standardEditor;
        }

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            updateEditor(row);
            currentValue = value;
            return current.getTableCellEditorComponent(table, value, isSelected, row, column);
        }

        public void fireEditingStoppedExt() {
            fireEditingStopped();
        }

        public Object getCellEditorValue() {
            if (current == buttonEditor) {
                return currentValue;
            }
            return current.getCellEditorValue();
        }

        public boolean stopCellEditing() {
            return current.stopCellEditing();
        }

        public void cancelCellEditing() {
            current.cancelCellEditing();
        }

        public void addCellEditorListener(CellEditorListener l) {
            current.addCellEditorListener(l);
            super.addCellEditorListener(l);
        }

        public void removeCellEditorListener(CellEditorListener l) {
            current.removeCellEditorListener(l);
            super.removeCellEditorListener(l);
        }

        public boolean isCellEditable(EventObject anEvent) {
            return current.isCellEditable(anEvent);
        }

        public boolean shouldSelectCell(EventObject anEvent) {
            return current.shouldSelectCell(anEvent);
        }
    });
    locales.setModel(new DefaultComboBoxModel(localeList.toArray()));
    removeLocale.setEnabled(localeList.size() > 1);
}
Also used : Hashtable(java.util.Hashtable) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) CellEditorListener(javax.swing.event.CellEditorListener) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) JTextField(javax.swing.JTextField) SwingRenderer(com.codename1.ui.resource.util.SwingRenderer) EventObject(java.util.EventObject) DefaultCellEditor(javax.swing.DefaultCellEditor) JCheckBox(javax.swing.JCheckBox) StringTokenizer(java.util.StringTokenizer) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable) AbstractTableModel(javax.swing.table.AbstractTableModel) EventObject(java.util.EventObject) TableCellEditor(javax.swing.table.TableCellEditor) Component(java.awt.Component) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride)

Example 25 with Table

use of com.codename1.ui.table.Table 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) Constraint(com.codename1.ui.validation.Constraint)

Aggregations

Component (com.codename1.ui.Component)7 Hashtable (java.util.Hashtable)7 BorderLayout (com.codename1.ui.layouts.BorderLayout)5 Cursor (com.codename1.db.Cursor)4 Form (com.codename1.ui.Form)4 TableLayout (com.codename1.ui.table.TableLayout)4 ArrayList (java.util.ArrayList)4 Database (com.codename1.db.Database)3 FieldNode (com.codename1.rad.nodes.FieldNode)3 Container (com.codename1.ui.Container)3 Label (com.codename1.ui.Label)3 TextArea (com.codename1.ui.TextArea)3 IOException (java.io.IOException)3 Vector (java.util.Vector)3 Entity (com.codename1.rad.models.Entity)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 BoxLayout (com.codename1.ui.layouts.BoxLayout)2 Border (com.codename1.ui.plaf.Border)2 Style (com.codename1.ui.plaf.Style)2 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)2