Package cli_fmw.address

Source Code of cli_fmw.address.DialogAddressM$BasicComboBoxRendererImpl

/*
* DialogAddress.java
*
* Created on 24 Январь 2008 г., 10:08
*/
package cli_fmw.address;

import cli_fmw.delegate.client.Address;
import cli_fmw.delegate.config.ConfigLocalAbstract;
import cli_fmw.delegate.directory.complex.DirectoryKladr;
import cli_fmw.delegate.directory.complex.DirectoryKladrItem;
import cli_fmw.delegate.directory.complex.DirectoryKladrType;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.main.DirectoryItemNotFoundException;
import cli_fmw.main.audit.AuditManager;
import cli_fmw.report.panels.model.FilteredComboBoxModel;
import cli_fmw.utils.DefaultColors;
import cli_fmw.utils.Log;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.ModalDialog;
import cli_fmw.utils.SelectorEditableExceptional;
import cli_fmw.utils.autocomplete.HasColors;
import cli_fmw.utils.table_config_states.StateSaver;
import framework.beans.address.entities.AddressDetails;
import framework.beans.address.entities.AddressObject;
import framework.beans.address.entities.AddressObjectDetails;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

/**
* Диалог, позволяющий редактировать адрес (либо клиента либо предприятия)
* @author lacoste
*/
public class DialogAddressM extends ModalDialog implements ActionListener {

    private Address address;
    private DirectoryKladrItem block;
    private AddressDetails addressDetailsOriginal;
    private DirectoryKladr directoryKladr;
    private boolean focusLostListenerActiv = true;
//    private JComboBoxAutoComplete[] combos;
    private JComboBox[] combos;
    private boolean senceAction = true;

    /**
     * Внимание данный элемент не должен быть выделен (кроме случая пока отображается диалог создания нового)
     * т.к. это фиктивный элемент который существует только в модели комбобокса
     * никакой связи со справочником иметь не должен
     * всегда стоит в списке последним
     */
    public DialogAddressM(Window window, Address ad, Address addDefault, AuditManager am) throws ClipsException {
        super(window, "Адрес", am);
        boolean addressReset = false;
        initComponents();

        Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle dlgSize = this.getBounds();
        dlgSize.setSize((int) (scrSize.width * 0.4), (int) (scrSize.height * 0.5));
        this.setBounds(dlgSize);
        this.setLocation((scrSize.width - dlgSize.width) / 2, (scrSize.height
                - dlgSize.height) / 2);

        directoryKladr = DirectoryLocator.getDirectory(DirectoryKladr.class);
        if (ad == null || ad.getDetails().objectId == 0) {
            if (addDefault != null && addDefault.getDetails().objectId != 0) {
                DirectoryKladrItem city = addDefault.getCity();
                if (city != null) {
                    ad = new Address(new AddressDetails(city.getID()));
                }
            }
        }

        if (ad == null || ad.getDetails().objectId == 0) {
            ad = new Address(new AddressDetails(directoryKladr.getRussia().getID()));
            addressReset = true;
        }
        this.address = ad;
        this.addressDetailsOriginal = address.getDetails().clone();
        combos = new JComboBox[6];
        combos[0] = cb0;
        combos[1] = cb1;
        combos[2] = cb2;
        combos[3] = cb3;
        combos[4] = cb4;
        combos[5] = cb5;
//        combos[0] = ja0;
//        combos[1] = ja1;
//        combos[2] = ja2;
//        combos[3] = ja3;
//        combos[4] = ja4;
//        combos[5] = ja5;

        createModel(cb0, directoryKladr.toArray(new DirectoryKladrItem[directoryKladr.getItems().size()], false), false);
        if (addressReset) {
            cb0.setSelectedItem(directoryKladr.getRussia());
        } else {
            cb0.setSelectedItem(ad.getObjectByLevel(0));
        }
        BasicComboBoxRendererImpl renderer = new BasicComboBoxRendererImpl();

        for (JComboBox jcbac : combos) {
            jcbac.addActionListener(this);
            jcbac.setRenderer(renderer);
        }

        revalidateCombos(directoryKladr.getItemFromID(address.getDetails().objectId), cb0);

        StateSaver.attachTo(this);
    }

    private void block() {
        for (JComboBox jComboBox : combos) {
            jComboBox.setEnabled(false);
        }
        btOK.setEnabled(false);
    }

    private void correctAddress() {
        try {
            DirectoryKladrItem street = address.getStreet();
            if (street != null) {
                block = null;
                SelectorEditableExceptional<DirectoryKladrItem> items = street.getItems();
                for (int i = 0; i < items.size(); i++) {
                    if (include(items.get(i), address.getDetails().building)) {
                        block = items.get(i);
                        tfPostIndex.setText("" + block.getPostIndex());
                        break;
                    }
                }
            }
            if (block == null) {
                tfPostIndex.setText("" + directoryKladr.getItemFromID(address.getDetails().objectId).getPostIndex());
            }
        } catch (ClipsException ex) {
            this.block();
            MessageBox.showException(ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        btOK = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBlocks = new cli_fmw.utils.sorted_table.SortedTable();
        pnlCombo1 = new javax.swing.JPanel();
        cb0 = new javax.swing.JComboBox();
        jPanel14 = new javax.swing.JPanel();
        cb1 = new javax.swing.JComboBox();
        jPanel15 = new javax.swing.JPanel();
        cb2 = new javax.swing.JComboBox();
        jPanel16 = new javax.swing.JPanel();
        cb3 = new javax.swing.JComboBox();
        jPanel17 = new javax.swing.JPanel();
        cb4 = new javax.swing.JComboBox();
        jPanel18 = new javax.swing.JPanel();
        cb5 = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        lbPostIndex = new javax.swing.JLabel();
        tfPostIndex = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tfBuilding = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        lbFlat = new javax.swing.JLabel();
        tfFlat = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel5.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        btOK.setText("     Да     ");
        btOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOKActionPerformed(evt);
            }
        });
        jPanel5.add(btOK);

        btCancel.setText("Отмена");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });
        jPanel5.add(btCancel);

        getContentPane().add(jPanel5, java.awt.BorderLayout.PAGE_END);

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel2.setLayout(new java.awt.BorderLayout(0, 5));

        jPanel9.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel9.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setPreferredSize(new java.awt.Dimension(0, 0));

        tblBlocks.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tblBlocks);

        jPanel9.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pnlCombo1.setLayout(new java.awt.BorderLayout(0, 5));

        cb0.setEditable(true);
        cb0.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        pnlCombo1.add(cb0, java.awt.BorderLayout.PAGE_START);

        jPanel14.setLayout(new java.awt.BorderLayout(0, 5));

        cb1.setEditable(true);
        cb1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel14.add(cb1, java.awt.BorderLayout.PAGE_START);

        jPanel15.setLayout(new java.awt.BorderLayout(0, 5));

        cb2.setEditable(true);
        cb2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel15.add(cb2, java.awt.BorderLayout.PAGE_START);

        jPanel16.setLayout(new java.awt.BorderLayout(0, 5));

        cb3.setEditable(true);
        cb3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel16.add(cb3, java.awt.BorderLayout.PAGE_START);

        jPanel17.setLayout(new java.awt.BorderLayout(0, 5));

        cb4.setEditable(true);
        cb4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel17.add(cb4, java.awt.BorderLayout.PAGE_START);

        jPanel18.setLayout(new java.awt.BorderLayout(0, 5));

        cb5.setEditable(true);
        cb5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel18.add(cb5, java.awt.BorderLayout.PAGE_START);

        jPanel17.add(jPanel18, java.awt.BorderLayout.CENTER);

        jPanel16.add(jPanel17, java.awt.BorderLayout.CENTER);

        jPanel15.add(jPanel16, java.awt.BorderLayout.CENTER);

        jPanel14.add(jPanel15, java.awt.BorderLayout.CENTER);

        pnlCombo1.add(jPanel14, java.awt.BorderLayout.CENTER);

        jPanel9.add(pnlCombo1, java.awt.BorderLayout.NORTH);

        jPanel2.add(jPanel9, java.awt.BorderLayout.CENTER);

        lbPostIndex.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbPostIndex.setText("почт. индекс");
        jPanel4.add(lbPostIndex);

        tfPostIndex.setEditable(false);
        tfPostIndex.setPreferredSize(new java.awt.Dimension(80, 21));
        jPanel4.add(tfPostIndex);

        jPanel7.setLayout(new java.awt.GridLayout(1, 0));

        jLabel1.setText("Дом");

        tfBuilding.setPreferredSize(new java.awt.Dimension(50, 19));
        tfBuilding.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                tfBuildingCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(5, 5, 5)
                .addComponent(tfBuilding, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jLabel1))
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(tfBuilding, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel7.add(jPanel6);

        lbFlat.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbFlat.setText("Квартира");
        jPanel3.add(lbFlat);

        tfFlat.setPreferredSize(new java.awt.Dimension(50, 19));
        jPanel3.add(tfFlat);

        jPanel7.add(jPanel3);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.add(jPanel1, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOKActionPerformed
        if (block != null) {
            address.getDetails().objectId = block.getID();
        }
        if (tfFlat.isEnabled()) {
            address.getDetails().flat = tfFlat.getText().trim();
        } else {
            address.getDetails().flat = "";
        }
        System.out.println(address.toString());

        setDlgResult(DLG_OK);
        dispose();
    }//GEN-LAST:event_btOKActionPerformed

//    private boolean isFlatAndBuildingEnabled(){
//        return tfBuilding.isEnabled() && tfFlat.isEnabled();
//    }
    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        setDlgResult(DLG_CANCEL);
        address = null;
        dispose();
    }//GEN-LAST:event_btCancelActionPerformed

    private void tfBuildingCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_tfBuildingCaretUpdate
        if (tfBuilding.isEnabled() && !tfBuilding.getText().trim().equals(address.getBuilding())) {
//            System.out.println("tfBuildingCaretUpdate");
            address.getDetails().building = tfBuilding.getText().trim();
            correctAddress();
        }
    }//GEN-LAST:event_tfBuildingCaretUpdate
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btOK;
    private javax.swing.JComboBox cb0;
    private javax.swing.JComboBox cb1;
    private javax.swing.JComboBox cb2;
    private javax.swing.JComboBox cb3;
    private javax.swing.JComboBox cb4;
    private javax.swing.JComboBox cb5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbFlat;
    private javax.swing.JLabel lbPostIndex;
    private javax.swing.JPanel pnlCombo1;
    private cli_fmw.utils.sorted_table.SortedTable tblBlocks;
    private javax.swing.JTextField tfBuilding;
    private javax.swing.JTextField tfFlat;
    private javax.swing.JTextField tfPostIndex;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        JComboBox source = (JComboBox) e.getSource();
        Object select = source.getSelectedItem();
        revalidateCombos(select, source);
    }

//    private DefaultComboBoxModel createModel(DirectoryKladrItem item) throws ClipsException{
//        if (item == null){
//            return new DefaultComboBoxModel();
//        }else{
//            DefaultComboBoxModel model = new DefaultComboBoxModel(getChildren(item));
//            return model;
//        }
//    }
    private void createModel(final JComboBox cb, DirectoryKladrItem[] items, boolean addEmpty) {
        ComboBoxModel cbModel = cb.getModel();
        FilteredComboBoxModel filteredModel;
        if (cbModel != null && cbModel instanceof FilteredComboBoxModel) {
            filteredModel = (FilteredComboBoxModel) cbModel;
        } else {
            filteredModel = new FilteredComboBoxModel(cb, true) {
                @Override
                public Object addItem(String txt) {
                    try {
//                    System.out.println("CREATE NEW ITEM");
                        int number = getNumber(cb);
                        AddressObjectDetails details = new AddressObjectDetails();
                        details.title = (String) txt;
                        Object parent = combos[number - 1].getSelectedItem();
                        if (!(parent instanceof DirectoryKladrItem)) {
                            throw new ClipsException("Родительский элемент не является элементом справочника");
                        } else {
                            DirectoryKladrItem localParent = (DirectoryKladrItem) parent;
                            int level = localParent.getType().getLevel() + 1;
                            DialogNewKladrItem dnki = new DialogNewKladrItem(DialogAddressM.this, details, level, getAuditManager());
                            dnki.setVisible(true);
                            if (dnki.getDlgResult() == ModalDialog.DLG_OK) {
                                DirectoryKladrItem kladrItem = dnki.getKladrItem();

                                localParent.getItems().append(kladrItem);
                                createModel(cb, getChildren(localParent), true);
//                                cb.setSelectedItem(kladrItem);
                                return kladrItem;
                            } else {
//                            System.out.println("ОТМЕНА");
                            }
                        }
                    } catch (ClipsException ce) {
                        MessageBox.showException(ce);
                    }

                    return null;

                }
            };
        }
        DefaultComboBoxModel model = new DefaultComboBoxModel(items);
        if (addEmpty) {
            model.insertElementAt("", 0);
        }
        filteredModel.setContainModel(model);
//        filteredModel.addEnterListener(new EnterKeyListener() {
//
//            @Override
//            public void onEnterPressed(KeyEvent e) {
//                try {
//                    onSearchFieldChanged(cb.getSelectedItem().toString());
//                } catch (ClipsException ex) {
//                    MessageBox.showException(ex);
//                }
//            }
//        });
    }

    private DirectoryKladrItem[] getChildren(DirectoryKladrItem item) throws DirectoryItemNotFoundException, ClipsException {
        int childCount = item.getItems().size();
        DirectoryKladrItem[] children = new DirectoryKladrItem[childCount];
        if (childCount > 0) {
            children = item.toArray(children);
        }
        return children;
    }

    private void checkTextInput(DirectoryKladrItem item) throws ClipsException {
        boolean active = item.getType().getLevel() >= DirectoryKladrType.MIN_LEVEL_HAS_BUILDINGS
                || item.getItems().size() == 0;
        tfBuilding.setEnabled(active);
        tfFlat.setEnabled(active);
    }

    /**
     * Проверяет входит ли дом с данным номером в квартал
     * @param block
     * @param buildingNumber
     * @return
     */
    private boolean include(DirectoryKladrItem block, String buildingNumberString) throws ClipsException {
        if (block.getType().getLevel() <= DirectoryKladrType.LEVEL_STREET) {
            throw new IllegalArgumentException("Переданный адресуемый объект не является кварталом: " + block);
        } else {
            int buildingNumber = 0;
            if (Pattern.matches("[0-9]+", buildingNumberString)) {
                buildingNumber = Integer.parseInt(buildingNumberString);
            }

            ArrayList<String> numbers = createNumbers(block);
            String s;
            for (int i = 0; i < numbers.size(); i++) {
                s = numbers.get(i);
                if (buildingNumber > 0) {//только в этом случае есть смысл искать по диапазонам
                    if (Pattern.matches(AddressObject.pattern1, s)) {//указана сторона
                        int breakPos = s.indexOf("-");
                        int start = Integer.parseInt(s.substring(2, breakPos));
                        int end = Integer.parseInt(s.substring(breakPos + 1, s.length() - 1));
                        if (buildingNumber % 2 == start % 2 && start <= buildingNumber && buildingNumber <= end) {
                            return true;
                        }
                    } else if (Pattern.matches(AddressObject.pattern3, s)) {//без указания стороны
                        int breakPos = s.indexOf("-");
                        int start = Integer.parseInt(s.substring(0, breakPos));
                        int end = Integer.parseInt(s.substring(breakPos + 1, s.length()));
                        if (start <= buildingNumber && buildingNumber <= end) {
                            return true;
                        }
                    }
                } else if (!Pattern.matches(AddressObject.pattern1, s) && !Pattern.matches(AddressObject.pattern3, s)) {//s не диапазон
                    if (s.equalsIgnoreCase(buildingNumberString)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private ArrayList<String> createNumbers(DirectoryKladrItem block) throws ClipsException {
        if (block.getType().getLevel() <= DirectoryKladrType.LEVEL_STREET) {
            throw new IllegalArgumentException("Переданный адресуемый объект не является кварталом");
        } else {
            String title = block.getTitle().trim();
            ArrayList<String> res = new ArrayList<String>(0);
            String s = "";
            for (int i = 0; i < title.length(); i++) {
                if (title.charAt(i) == ',') {
                    res.add(s.trim());
                    s = "";
                } else {
                    s += title.charAt(i);
                }
            }
            res.add(s.trim());
            return res;
        }
    }

    public Address getAddress() {
        return address;
    }

    private int getNumber(JComboBox comboBox) {
        for (int i = 0; i < combos.length; i++) {
            if (comboBox == combos[i]) {
                return i;
            }
        }
        return -1;
    }

    private void revalidateCombos(Object select, JComboBox source) {
//        System.out.println("REVALIDATE COMBOS");
        if (senceAction) {
            if (select instanceof String && !((String) select).trim().isEmpty()) {
            } else if (select instanceof DirectoryKladrItem) {
                senceAction = false;
                try {
                    DirectoryKladrItem item = (DirectoryKladrItem) select;
                    if (item.getType().getLevel() == DirectoryKladrType.LEVEL_BLOCK) {
                        item = item.getParent();
                    }
                    address.getDetails().objectId = item.getID();

                    correctAddress();

                    if (item.getType().getLevel() >= DirectoryKladrType.MIN_LEVEL_HAS_BUILDINGS) {
                        tfBuilding.setEnabled(true);
                        tfFlat.setEnabled(true);
                        tfBuilding.setText(address.getBuilding());
                        tfFlat.setText(address.getFlat());
                    } else {
                        tfBuilding.setEnabled(false);
                        tfFlat.setEnabled(false);
                        tfBuilding.setText("");
                        tfFlat.setText("");
                    }

                    checkTextInput(item);
                    ArrayList<DirectoryKladrItem> path = new ArrayList<DirectoryKladrItem>();
                    if (item.getType().getLevel() < DirectoryKladrType.LAST_USING_LEVEL) {
                        path.add(null);
                    }
                    if (item.getType().isType("улица")) {
                        tblBlocks.setModel(new TableModelBlocks(item.toArray(new DirectoryKladrItem[item.getItems().size()])));
                        tblBlocks.setVisible(true);
                    } else {
                        tblBlocks.setVisible(false);
                    }

                    while (item != null) {
                        path.add(0, item);
                        item = item.getParent();
                    }
                    for (int i = 1; i < path.size(); i++) {
                        combos[i].setVisible(true);
                        DirectoryKladrItem parent = path.get(i - 1);
//                        combos[i].setModel(createModel(parent));
                        createModel(combos[i], getChildren(parent), true);
                        combos[i].setSelectedItem(path.get(i) != null ? path.get(i) : "");
                    }
                    for (int i = path.size(); i < combos.length; i++) {
                        combos[i].setVisible(false);
                        createModel(combos[i], new DirectoryKladrItem[0], false);
                    }
                } catch (Exception ex) {
                    block();
                    MessageBox.showException(ex);
                } finally {
                    senceAction = true;
                }

                for (JComboBox jComboBox : combos) {
                    if (jComboBox.getSelectedItem() instanceof HasColors) {
                        jComboBox.setForeground(((HasColors) jComboBox.getSelectedItem()).getForeground());
                    } else {
                        jComboBox.setForeground(DefaultColors.TEXT);
                    }
                }
            } else {
                Log.printlnAnsPos("Неизвестный элемент: " + select);
            }
        }
    }

    private class BasicComboBoxRendererImpl extends BasicComboBoxRenderer {

        public BasicComboBoxRendererImpl() {
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof HasColors) {
                Color background = ((HasColors) value).getBackground();
                Color foreground = ((HasColors) value).getForeground();
                if (background != null) {
                    if (isSelected) {
                        label.setBackground(DefaultColors.middle(label.getBackground(), background));
                    } else {
                        label.setBackground(background);
                    }
                }
                if (foreground != null) {
                    if (isSelected) {
                        label.setForeground(DefaultColors.middle(label.getForeground(), foreground));
                    } else {
                        label.setForeground(foreground);
                    }
                }
            }
            return label;
        }
    }
}
TOP

Related Classes of cli_fmw.address.DialogAddressM$BasicComboBoxRendererImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.