Search in sources :

Example 1 with PopupRowWrapper

use of com.ramussoft.gui.attribute.OtherElementTableCellEditor.PopupRowWrapper in project ramus by Vitaliy-Yakovchuk.

the class TableCellEditorFactory method createModelEditor.

private TableCellEditor createModelEditor() {
    Qualifier qualifier = getModelTree(engine);
    Attribute nameAttribute = StandardAttributesPlugin.getAttributeNameAttribute(engine);
    RowSet rowSet = new RowSet(engine, qualifier, new Attribute[] { nameAttribute }, null, true);
    OtherElementTableCellEditor editor = new OtherElementTableCellEditor(rowSet, nameAttribute, framework, null) {

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

        private OtherElementListModel model;

        private void updateValue() {
            if (list.getSelectedIndex() == 0)
                value = null;
            else if (list.getSelectedIndex() == 1)
                value = "[ALL MODELS]";
            else {
                if (model.checked.size() == 0) {
                    value = model.wrappers[list.getSelectedIndex() - 2].row.getName();
                } else {
                    StringBuffer sb = null;
                    for (PopupRowWrapper w : model.checked.keySet()) {
                        if (sb == null)
                            sb = new StringBuffer(w.row.getName());
                        else {
                            sb.append(com.ramussoft.report.editor.xml.Attribute.QUALIFIER_DELIMETER);
                            sb.append(w.row.getName());
                        }
                    }
                    value = sb.toString();
                }
            }
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int columnIndex) {
            try {
                this.table = table;
                if (value instanceof String) {
                    Row r = rowSet.findRow((String) value);
                    if (r != null) {
                        return super.getTableCellEditorComponent(table, value, isSelected, rowIndex, columnIndex);
                    } else {
                        SwingUtilities.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                codeField.selectAll();
                                codeField.requestFocus();
                            }
                        });
                        return component;
                    }
                }
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        codeField.selectAll();
                        codeField.requestFocus();
                    }
                });
                return super.getTableCellEditorComponent(table, value, isSelected, rowIndex, columnIndex);
            } finally {
                this.value = value;
            }
        }

        protected JPopupMenu getMenu(Attribute attribute) {
            if (menu == null) {
                menu = new JPopupMenu(attribute.getName());
                menu.setMaximumSize(new Dimension(600, 500));
                JScrollPane pane = new JScrollPane();
                List<Row> allRows = rowSet.getAllRows();
                wrappers = new PopupRowWrapper[allRows.size()];
                for (int i = 0; i < wrappers.length; i++) {
                    wrappers[i] = new PopupRowWrapper(allRows.get(i), attribute);
                }
                model = new OtherElementListModel(wrappers);
                list = new JList(model) {

                    /**
                     */
                    private static final long serialVersionUID = 3924839890645563320L;

                    CheckCellRenderer cellRenderer;

                    public ListCellRenderer getCellRenderer() {
                        if (cellRenderer == null)
                            cellRenderer = new CheckCellRenderer(model, super.getCellRenderer());
                        return cellRenderer;
                    }
                };
                Dimension size = list.getPreferredSize();
                if (size.width > 600)
                    list.setPreferredSize(new Dimension(600, size.height));
                if (value == null)
                    list.setSelectedIndex(0);
                else {
                    List<String> slist = new ArrayList<String>();
                    String val = ((String) value);
                    StringTokenizer st = new StringTokenizer(val, com.ramussoft.report.editor.xml.Attribute.QUALIFIER_DELIMETER);
                    slist.add(val);
                    while (st.hasMoreTokens()) slist.add(st.nextToken());
                    if ("[ALL MODELS]".equals(val))
                        list.setSelectedIndex(1);
                    else {
                        List<Integer> seleted = new ArrayList<Integer>();
                        for (int i = 0; i < wrappers.length; i++) {
                            if (slist.contains(wrappers[i].row.getName())) {
                                model.select(wrappers[i]);
                                seleted.add(i + 2);
                            }
                        }
                        int[] is = new int[seleted.size()];
                        for (int i = 0; i < is.length; i++) is[i] = seleted.get(i);
                        if (is.length > 0)
                            list.setSelectedIndices(is);
                    }
                }
                list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        list.ensureIndexIsVisible(list.getSelectedIndex());
                    }
                });
                list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

                    private boolean rec = false;

                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (rec)
                            return;
                        rec = true;
                        int index = list.getSelectedIndex();
                        PopupRowWrapper wrapper = null;
                        if (index < 0)
                            return;
                        updateValue();
                        if (index == 0)
                            value = null;
                        else if (index > 1) {
                            wrapper = wrappers[index - 2];
                        }
                        if (updateFields) {
                            codeField.setBackground(fieldDefaultBackground);
                            valueField.setBackground(fieldDefaultBackground);
                            if (wrapper == null) {
                                codeField.setText("");
                                valueField.setText("");
                            } else {
                                codeField.setText(wrapper.code);
                                codeField.setCaretPosition(0);
                                valueField.setText(wrapper.value);
                                valueField.setCaretPosition(0);
                            }
                        }
                        rec = false;
                    }
                });
                list.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (e.getX() <= 20 && e.getClickCount() < 2) {
                            int index = list.getSelectedIndex();
                            if (index > 1) {
                                index -= 2;
                                model.select(wrappers[index]);
                                updateValue();
                                list.repaint();
                                return;
                            }
                        }
                        updateValue();
                        stopCellEditing();
                    }
                });
                InputMap inputMap = list.getInputMap();
                KeyStroke[] allKeys = inputMap.allKeys();
                for (Object actionName : actionsToReplace) {
                    for (KeyStroke stroke : allKeys) {
                        Object value = inputMap.get(stroke);
                        if (actionName.equals(value)) {
                            codeField.getInputMap().put(stroke, actionName);
                            valueField.getInputMap().put(stroke, actionName);
                            final Action source = list.getActionMap().get(actionName);
                            if (source != null) {
                                Action action = new AbstractAction() {

                                    /**
                                     */
                                    private static final long serialVersionUID = 4806926801192964440L;

                                    @Override
                                    public void actionPerformed(ActionEvent e) {
                                        if (list != null) {
                                            updateFields = true;
                                            e.setSource(list);
                                            source.actionPerformed(e);
                                            updateFields = false;
                                        }
                                    }
                                };
                                valueField.getActionMap().put(actionName, action);
                                codeField.getActionMap().put(actionName, action);
                            }
                        }
                    }
                }
                pane.setViewportView(list);
                menu.add(pane);
                menu.pack();
            }
            return menu;
        }

        @Override
        protected void edit() {
            if (menu != null) {
                menu.setVisible(false);
                menu = null;
            }
            final SelectableTableView view = new SelectableTableView(framework, engine, framework.getAccessRules(), rowSet.getQualifier()) {

                @Override
                public String getPropertiesPrefix() {
                    return "xml_edit";
                }
            };
            JComponent rc = view.createComponent();
            final RowTreeTableComponent component = view.getComponent();
            component.getTable().setComponentPopupMenu(null);
            component.getTable().setTableHeader(null);
            List<String> slist = new ArrayList<String>();
            String val = ((String) value);
            if (val != null) {
                StringTokenizer st = new StringTokenizer(val, com.ramussoft.report.editor.xml.Attribute.QUALIFIER_DELIMETER);
                slist.add(val);
                while (st.hasMoreTokens()) slist.add(st.nextToken());
                for (Row row : component.getRowSet().getAllRows()) if (slist.contains(row.getName()))
                    component.getModel().setSelectedRow(row, true);
            }
            view.setSelectType(SelectType.CHECK);
            BaseDialog dialog = new BaseDialog(framework.getMainFrame()) {

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

                @Override
                protected void onOk() {
                    List<Row> rows = view.getSelectedRows();
                    if (rows.size() == 0)
                        value = null;
                    else {
                        StringBuffer sb = null;
                        for (Row w : rows) {
                            if (sb == null)
                                sb = new StringBuffer(w.getName());
                            else {
                                sb.append(com.ramussoft.report.editor.xml.Attribute.QUALIFIER_DELIMETER);
                                sb.append(w.getName());
                            }
                        }
                        value = sb.toString();
                    }
                    stopCellEditing();
                    super.onOk();
                }
            };
            dialog.setMainPane(rc);
            dialog.setTitle(ReportResourceManager.getString("ReportAttribute.model"));
            dialog.setLocationRelativeTo(null);
            Options.loadOptions("IDEF0_models", dialog);
            dialog.setModal(true);
            dialog.setVisible(true);
            Options.saveOptions("IDEF0_models", dialog);
            try {
                view.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    return editor;
}
Also used : BaseDialog(com.ramussoft.gui.common.BaseDialog) OtherElementTableCellEditor(com.ramussoft.gui.attribute.OtherElementTableCellEditor) AbstractAction(javax.swing.AbstractAction) Action(javax.swing.Action) Attribute(com.ramussoft.common.Attribute) ActionEvent(java.awt.event.ActionEvent) RowTreeTableComponent(com.ramussoft.gui.qualifier.table.RowTreeTableComponent) RowSet(com.ramussoft.database.common.RowSet) ArrayList(java.util.ArrayList) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ListCellRenderer(javax.swing.ListCellRenderer) Qualifier(com.ramussoft.common.Qualifier) AbstractAction(javax.swing.AbstractAction) SelectableTableView(com.ramussoft.gui.qualifier.table.SelectableTableView) JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JComponent(javax.swing.JComponent) Dimension(java.awt.Dimension) JPopupMenu(javax.swing.JPopupMenu) ListSelectionListener(javax.swing.event.ListSelectionListener) StringTokenizer(java.util.StringTokenizer) JTable(javax.swing.JTable) PopupRowWrapper(com.ramussoft.gui.attribute.OtherElementTableCellEditor.PopupRowWrapper) KeyStroke(javax.swing.KeyStroke) InputMap(javax.swing.InputMap) Row(com.ramussoft.database.common.Row) JList(javax.swing.JList)

Aggregations

Attribute (com.ramussoft.common.Attribute)1 Qualifier (com.ramussoft.common.Qualifier)1 Row (com.ramussoft.database.common.Row)1 RowSet (com.ramussoft.database.common.RowSet)1 OtherElementTableCellEditor (com.ramussoft.gui.attribute.OtherElementTableCellEditor)1 PopupRowWrapper (com.ramussoft.gui.attribute.OtherElementTableCellEditor.PopupRowWrapper)1 BaseDialog (com.ramussoft.gui.common.BaseDialog)1 RowTreeTableComponent (com.ramussoft.gui.qualifier.table.RowTreeTableComponent)1 SelectableTableView (com.ramussoft.gui.qualifier.table.SelectableTableView)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 AbstractAction (javax.swing.AbstractAction)1 Action (javax.swing.Action)1 InputMap (javax.swing.InputMap)1 JComponent (javax.swing.JComponent)1 JList (javax.swing.JList)1