Search in sources :

Example 1 with SwingRenderer

use of com.codename1.ui.resource.util.SwingRenderer 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)

Aggregations

SwingRenderer (com.codename1.ui.resource.util.SwingRenderer)1 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)1 Component (java.awt.Component)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 EventObject (java.util.EventObject)1 Hashtable (java.util.Hashtable)1 StringTokenizer (java.util.StringTokenizer)1 DefaultCellEditor (javax.swing.DefaultCellEditor)1 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)1 JButton (javax.swing.JButton)1 JCheckBox (javax.swing.JCheckBox)1 JTable (javax.swing.JTable)1 JTextField (javax.swing.JTextField)1 CellEditorListener (javax.swing.event.CellEditorListener)1 AbstractTableModel (javax.swing.table.AbstractTableModel)1 TableCellEditor (javax.swing.table.TableCellEditor)1