Search in sources :

Example 1 with CurrencyPropertyPersistent

use of com.ramussoft.core.attribute.simple.CurrencyPropertyPersistent in project ramus by Vitaliy-Yakovchuk.

the class CurrencyAttributePlugin method getTableCellRenderer.

@Override
public TableCellRenderer getTableCellRenderer(Engine engine, AccessRules rules, Attribute attribute) {
    CurrencyPropertyPersistent p = null;
    if (attribute.getId() >= 0)
        p = (CurrencyPropertyPersistent) engine.getAttribute(null, attribute);
    if ((p != null) && (p.getCode() != null)) {
        final DecimalFormat format;
        if ((p.getCode().equals("UAH")) && (Locale.getDefault().getLanguage().equals("uk")))
            format = new DecimalFormat("###,###.## \u0433\u0440\u043d\'.\'");
        else {
            Currency currency = Currency.getInstance(p.getCode());
            String s;
            if (p.getCode().equals("EUR"))
                s = currency.getSymbol(Locale.UK);
            else if (p.getCode().equals("USD"))
                s = currency.getSymbol(Locale.US);
            else
                s = currency.getSymbol();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '.')
                    sb.append("\'.\'");
                else if (c == ',')
                    sb.append("\',\'");
                else if (c == '#')
                    sb.append("\'#\'");
                else
                    sb.append(c);
            }
            s = sb.toString();
            String b = "###,###";
            String e = ".##";
            int digits = currency.getDefaultFractionDigits();
            if (digits > 0) {
                e = ".";
                while (digits > 0) {
                    digits--;
                    e += "#";
                }
            } else
                e = "";
            if (s.length() == 1)
                format = new DecimalFormat(s + " " + b + e);
            else
                format = new DecimalFormat(b + e + " " + s);
        }
        if (Locale.getDefault().getLanguage().equals("uk")) {
            format.setDecimalSeparatorAlwaysShown(false);
            DecimalFormatSymbols newSymbols = new DecimalFormatSymbols();
            newSymbols.setGroupingSeparator(' ');
            newSymbols.setDecimalSeparator(',');
            format.setDecimalFormatSymbols(newSymbols);
        }
        return new DefaultTableCellRenderer() {

            /**
             */
            private static final long serialVersionUID = -7922052040779840252L;

            {
                setHorizontalAlignment(SwingConstants.RIGHT);
            }

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (value != null)
                    ((JLabel) component).setText(format.format(value));
                return component;
            }
        };
    }
    return super.getTableCellRenderer(engine, rules, attribute);
}
Also used : CurrencyPropertyPersistent(com.ramussoft.core.attribute.simple.CurrencyPropertyPersistent) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) Currency(java.util.Currency) JTable(javax.swing.JTable) JComponent(javax.swing.JComponent) Component(java.awt.Component) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 2 with CurrencyPropertyPersistent

use of com.ramussoft.core.attribute.simple.CurrencyPropertyPersistent in project ramus by Vitaliy-Yakovchuk.

the class CurrencyAttributePlugin method getAttributePreferenciesEditor.

@Override
public AttributePreferenciesEditor getAttributePreferenciesEditor() {
    return new AttributePreferenciesEditor() {

        private JComboBox comboBox = new JComboBox();

        @Override
        public JComponent createComponent(Attribute attribute, Engine engine, AccessRules accessRules) {
            CurrencyPropertyPersistent p = null;
            if (attribute != null)
                p = (CurrencyPropertyPersistent) engine.getAttribute(null, attribute);
            comboBox.addItem(GlobalResourcesManager.getString("ByDefault"));
            int i = 1;
            int index = 0;
            for (Currency currency : getCurrencies()) {
                comboBox.addItem(currency);
                if ((p != null) && (p.getCode() != null) && (p.getCode().equals(currency.getCurrencyCode())))
                    index = i;
                i++;
            }
            comboBox.setSelectedIndex(index);
            double[][] size = { { 5, TableLayout.MINIMUM, 5, TableLayout.FILL, 5 }, { 5, TableLayout.MINIMUM, 5 } };
            TableLayout layout = new TableLayout(size);
            JPanel panel = new JPanel(layout);
            panel.add(new JLabel(GlobalResourcesManager.getString("Attribute.CurrencyCode")), "1,1");
            panel.add(comboBox, "3,1");
            return panel;
        }

        @Override
        public boolean canApply() {
            return true;
        }

        @Override
        public void apply(Attribute attribute, Engine engine, AccessRules accessRules) {
            if (comboBox.getSelectedIndex() == 0) {
                engine.setAttribute(null, attribute, null);
            } else {
                CurrencyPropertyPersistent p = new CurrencyPropertyPersistent();
                p.setCode(((Currency) comboBox.getSelectedItem()).getCurrencyCode());
                engine.setAttribute(null, attribute, p);
            }
        }
    };
}
Also used : CurrencyPropertyPersistent(com.ramussoft.core.attribute.simple.CurrencyPropertyPersistent) JPanel(javax.swing.JPanel) JComboBox(javax.swing.JComboBox) Attribute(com.ramussoft.common.Attribute) Currency(java.util.Currency) AccessRules(com.ramussoft.common.AccessRules) JLabel(javax.swing.JLabel) TableLayout(info.clearthought.layout.TableLayout) AttributePreferenciesEditor(com.ramussoft.gui.common.AttributePreferenciesEditor) Engine(com.ramussoft.common.Engine)

Aggregations

CurrencyPropertyPersistent (com.ramussoft.core.attribute.simple.CurrencyPropertyPersistent)2 Currency (java.util.Currency)2 AccessRules (com.ramussoft.common.AccessRules)1 Attribute (com.ramussoft.common.Attribute)1 Engine (com.ramussoft.common.Engine)1 AttributePreferenciesEditor (com.ramussoft.gui.common.AttributePreferenciesEditor)1 TableLayout (info.clearthought.layout.TableLayout)1 Component (java.awt.Component)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 JComboBox (javax.swing.JComboBox)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JTable (javax.swing.JTable)1 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)1