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);
}
Aggregations