Package clips.directory.dialogs

Source Code of clips.directory.dialogs.DirectoryDialogService

/*
* DirectoryDialogDefault.java
*
* Created on 16 Январь 2008 г., 20:45
*/

package clips.directory.dialogs;

import cli_fmw.delegate.directory.DirectoryItem;
import clips.administrator.expenditure.TreeModelServices;
import cli_fmw.delegate.directory.DirectoryItemRO;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import clips.delegate.directory.filtered.DirectoryServiceItem;
import clips.delegate.directory.complex.DirectoryServicesGroup;
import cli_fmw.directory.dialogs.GenericChooseDialog;
import cli_fmw.main.ClipsException;
import cli_fmw.report.panels.model.EnterKeyListener;
import cli_fmw.report.panels.model.FilteredComboBoxModel;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.Selector;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.directory.complex.DirectoryServicesGroupItem;
import clips.delegate.directory.filtered.DirectoryService;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
*
* @author  Axe Ilshat
*/
public class DirectoryDialogService extends  GenericChooseDialog {

    private DirectoryServicesGroup dir;
    private DirectoryService dirServ;
   
    /** Creates new form DirectoryDialogDefault
     * @param multiplySelected
     * @param selected
     * @throws ClipsException
     */
    public DirectoryDialogService(boolean multiplySelected, DirectoryServiceItem selected)
            throws ClipsException {
        super(multiplySelected, selected, "Услуги", null);
        initComponents();
       
         dir = DirectoryLocator.getDirectory(DirectoryServicesGroup.class, false);
        setData(dir.toArray(true, false), tree, selectBtn, cancelBtn);

        dirServ = DirectoryLocator.getDirectory(DirectoryService.class);
        DefaultComboBoxModel model = new DefaultComboBoxModel(dirServ.toArray(false));
        model.insertElementAt("", 0);
        model.setSelectedItem("");
        FilteredComboBoxModel filteredModel = new FilteredComboBoxModel(cbSearch, new FilteredComboBoxModel.MaskChecker(), false) {

            @Override
            public Object addItem(String txt) {
                //throw new UnsupportedOperationException("Not supported yet.");
                return null;
            }
        };
        filteredModel.setContainModel(model);
        filteredModel.addEnterListener(new EnterKeyListener() {

            @Override
            public void onEnterPressed(KeyEvent e) {
                if (cbSearch.getSelectedItem() instanceof DirectoryItem){
                    try {
                        onSearchFieldChanged(((DirectoryItem)cbSearch.getSelectedItem()).getTitle());
                    } catch (ClipsException ex) {
                        MessageBox.showException(ex);
                    }
                }
            }

        });


    StateSaver.attachTo(this);
    }

    @Override
    protected TreeModel getModel(Object[] items, String rootTitle) throws ClipsException {
        return new TreeModelServices((DirectoryItemRO[]) items, rootTitle);
    }

    @Override
    protected boolean isFit(Object item) throws ClipsException {
        if(item instanceof DirectoryServiceItem) {
            DirectoryServiceItem si  =(DirectoryServiceItem) item;
            if(si.isVisible()) {
                return true;
            }
        }
        return false;
    }
   
    private void onSearchFieldChanged(String value) throws ClipsException {
        if (value.isEmpty()) {
            return;
        }
        value = value.toLowerCase();

        Selector sel = dir.getItems();
        for (int i = 0; i < sel.size(); i++) {
            DirectoryItem item = (DirectoryItem) sel.get(i);
            List found = findMatch(item, value);
            if (found != null) {
                found.add(tree.getModel().getRoot());
                ArrayList reordered = new ArrayList();
                for (int j = found.size() - 1; j >= 0; j--) {
                    reordered.add(found.get(j));
                }
                TreePath fullPath = new TreePath(reordered.toArray());
                tree.setSelectionPath(fullPath);
                tree.scrollPathToVisible(fullPath);
                tree.updateUI();
                return;
            }
        }
    }

    private List<Object> findMatch(DirectoryItem item, String pattern) throws ClipsException {
        int pos = (item.getTitle().toLowerCase()).indexOf(pattern.toLowerCase());
        if (pos == 0) {
            ArrayList path = new ArrayList();
            path.add(item);
            return path;
        }
        if (item instanceof DirectoryServicesGroupItem) {
            DirectoryServicesGroupItem parent = (DirectoryServicesGroupItem) item;
            Selector ii = parent.getItems();
            for (int i = 0; i < ii.size(); i++) {
                DirectoryItem child = (DirectoryItem) ii.get(i);
                List path = findMatch(child, pattern);
                if (path != null) {
                    path.add(parent);
                    return path;
                }
            }
            Selector<DirectoryServiceItem> si = dirServ.getFilteredItems(parent);
            for (int i = 0; i < si.size(); i++) {
                DirectoryServiceItem child = si.get(i);
                List path = findMatch(child, pattern);
                if (path != null) {
                    path.add(parent);
                    return path;
                }
            }
        }
        return null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainTreePane = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();
        jPanel1 = new javax.swing.JPanel();
        selectBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        cbSearch = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        mainTreePane.setViewportView(tree);

        selectBtn.setText("Выбрать");
        selectBtn.setEnabled(false);
        jPanel1.add(selectBtn);

        cancelBtn.setText("Отмена");
        jPanel1.add(cancelBtn);

        cbSearch.setEditable(true);
        cbSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Поиск");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(mainTreePane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbSearch, 0, 476, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainTreePane, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JComboBox cbSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane mainTreePane;
    private javax.swing.JButton selectBtn;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables


}
TOP

Related Classes of clips.directory.dialogs.DirectoryDialogService

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.