Search in sources :

Example 1 with CellEditorListener

use of javax.swing.event.CellEditorListener in project intellij-community by JetBrains.

the class DynamicToolWindowWrapper method createTable.

private JScrollPane createTable(final MutableTreeNode myTreeRoot) {
    ColumnInfo[] columnInfos = { new ClassColumnInfo(myColumnNames[CLASS_OR_ELEMENT_NAME_COLUMN]), new PropertyTypeColumnInfo(myColumnNames[TYPE_COLUMN]) };
    myTreeTableModel = new ListTreeTableModelOnColumns(myTreeRoot, columnInfos);
    myTreeTable = new MyTreeTable(myTreeTableModel);
    new TreeTableSpeedSearch(myTreeTable, new Convertor<TreePath, String>() {

        @Override
        public String convert(TreePath o) {
            final Object node = o.getLastPathComponent();
            if (node instanceof DefaultMutableTreeNode) {
                final Object object = ((DefaultMutableTreeNode) node).getUserObject();
                if (object instanceof DNamedElement) {
                    return ((DNamedElement) object).getName();
                }
            }
            return "";
        }
    });
    DefaultActionGroup group = new DefaultActionGroup();
    group.add(ActionManager.getInstance().getAction(RemoveDynamicAction.GROOVY_DYNAMIC_REMOVE));
    PopupHandler.installUnknownPopupHandler(myTreeTable, group, ActionManager.getInstance());
    final MyColoredTreeCellRenderer treeCellRenderer = new MyColoredTreeCellRenderer();
    myTreeTable.setDefaultRenderer(String.class, new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value instanceof String) {
                try {
                    final PsiType type = JavaPsiFacade.getElementFactory(myProject).createTypeFromText((String) value, null);
                    String shortName = type.getPresentableText();
                    return new JLabel(shortName);
                } catch (IncorrectOperationException e) {
                    LOG.debug("Type cannot be created", e);
                }
                return new JLabel(QuickfixUtil.shortenType((String) value));
            }
            return new JLabel();
        }
    });
    myTreeTable.setTreeCellRenderer(treeCellRenderer);
    myTreeTable.setRootVisible(false);
    myTreeTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    final MyPropertyTypeCellEditor typeCellEditor = new MyPropertyTypeCellEditor();
    typeCellEditor.addCellEditorListener(new CellEditorListener() {

        @Override
        public void editingStopped(ChangeEvent e) {
            final TreeTableTree tree = getTree();
            String newTypeValue = ((MyPropertyTypeCellEditor) e.getSource()).getCellEditorValue();
            if (newTypeValue == null || tree == null) {
                myTreeTable.editingStopped(e);
                return;
            }
            try {
                final PsiType type = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(newTypeValue, null);
                String canonical = type.getCanonicalText();
                if (canonical != null)
                    newTypeValue = canonical;
            } catch (IncorrectOperationException ex) {
            //do nothing in case bad string is entered
            }
            final TreePath editingTypePath = tree.getSelectionPath();
            if (editingTypePath == null)
                return;
            final TreePath editingClassPath = editingTypePath.getParentPath();
            Object oldTypeValue = myTreeTable.getValueAt(tree.getRowForPath(editingTypePath), TYPE_COLUMN);
            if (!(oldTypeValue instanceof String)) {
                myTreeTable.editingStopped(e);
                return;
            }
            final Object editingPropertyObject = myTreeTable.getValueAt(tree.getRowForPath(editingTypePath), CLASS_OR_ELEMENT_NAME_COLUMN);
            final Object editingClassObject = myTreeTable.getValueAt(tree.getRowForPath(editingClassPath), CLASS_OR_ELEMENT_NAME_COLUMN);
            if (!(editingPropertyObject instanceof DItemElement) || !(editingClassObject instanceof DClassElement)) {
                myTreeTable.editingStopped(e);
                return;
            }
            final DItemElement dynamicElement = (DItemElement) editingPropertyObject;
            final String name = dynamicElement.getName();
            final String className = ((DClassElement) editingClassObject).getName();
            if (dynamicElement instanceof DPropertyElement) {
                DynamicManager.getInstance(myProject).replaceDynamicPropertyType(className, name, (String) oldTypeValue, newTypeValue);
            } else if (dynamicElement instanceof DMethodElement) {
                final List<ParamInfo> myPairList = ((DMethodElement) dynamicElement).getPairs();
                DynamicManager.getInstance(myProject).replaceDynamicMethodType(className, name, myPairList, (String) oldTypeValue, newTypeValue);
            }
        }

        @Override
        public void editingCanceled(ChangeEvent e) {
            myTreeTable.editingCanceled(e);
        }
    });
    RefactoringListenerManager.getInstance(myProject).addListenerProvider(new RefactoringElementListenerProvider() {

        @Override
        @Nullable
        public RefactoringElementListener getListener(final PsiElement element) {
            if (element instanceof PsiClass) {
                final String qualifiedName = ((PsiClass) element).getQualifiedName();
                return new RefactoringElementListener() {

                    @Override
                    public void elementMoved(@NotNull PsiElement newElement) {
                        renameElement(qualifiedName, newElement);
                    }

                    @Override
                    public void elementRenamed(@NotNull PsiElement newElement) {
                        renameElement(qualifiedName, newElement);
                    }

                    private void renameElement(String oldClassName, PsiElement newElement) {
                        if (newElement instanceof PsiClass) {
                            final String newClassName = ((PsiClass) newElement).getQualifiedName();
                            final DRootElement rootElement = DynamicManager.getInstance(myProject).getRootElement();
                            final DClassElement oldClassElement = rootElement.getClassElement(oldClassName);
                            final TreeNode oldClassNode = TreeUtil.findNodeWithObject((DefaultMutableTreeNode) myTreeRoot, oldClassElement);
                            DynamicManager.getInstance(myProject).replaceClassName(oldClassElement, newClassName);
                            myTreeTableModel.nodeChanged(oldClassNode);
                        }
                    }
                };
            }
            return null;
        }
    });
    myTreeTable.setDefaultEditor(String.class, typeCellEditor);
    myTreeTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            final int selectionRow = myTreeTable.getTree().getLeadSelectionRow();
            myTreeTable.editCellAt(selectionRow, CLASS_OR_ELEMENT_NAME_COLUMN, event);
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), JComponent.WHEN_FOCUSED);
    myTreeTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            final int selectionRow = myTreeTable.getTree().getLeadSelectionRow();
            myTreeTable.editCellAt(selectionRow, TYPE_COLUMN, event);
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_F2, InputEvent.CTRL_MASK), JComponent.WHEN_FOCUSED);
    myTreeTable.getTree().setShowsRootHandles(true);
    myTreeTable.getTableHeader().setReorderingAllowed(false);
    myTreeTable.setPreferredScrollableViewportSize(new Dimension(300, myTreeTable.getRowHeight() * 10));
    myTreeTable.getColumn(myColumnNames[CLASS_OR_ELEMENT_NAME_COLUMN]).setPreferredWidth(200);
    myTreeTable.getColumn(myColumnNames[TYPE_COLUMN]).setPreferredWidth(160);
    JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTreeTable);
    scrollPane.setPreferredSize(JBUI.size(600, 400));
    return scrollPane;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) ColumnInfo(com.intellij.util.ui.ColumnInfo) CellEditorListener(javax.swing.event.CellEditorListener) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) TreeTableTree(com.intellij.ui.treeStructure.treetable.TreeTableTree) TableCellRenderer(javax.swing.table.TableCellRenderer) ListTreeTableModelOnColumns(com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) TreePath(javax.swing.tree.TreePath) ChangeEvent(javax.swing.event.ChangeEvent) RefactoringElementListenerProvider(com.intellij.refactoring.listeners.RefactoringElementListenerProvider) ActionListener(java.awt.event.ActionListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with CellEditorListener

use of javax.swing.event.CellEditorListener in project intellij-community by JetBrains.

the class ImportLayoutPanel method createTableForPackageEntries.

public static JBTable createTableForPackageEntries(final PackageEntryTable packageTable, final ImportLayoutPanel panel) {
    final String[] names = { ApplicationBundle.message("listbox.import.package"), ApplicationBundle.message("listbox.import.with.subpackages") };
    // Create a model of the data.
    TableModel dataModel = new AbstractTableModel() {

        public int getColumnCount() {
            return names.length + (panel.areStaticImportsEnabled() ? 1 : 0);
        }

        public int getRowCount() {
            return packageTable.getEntryCount();
        }

        @Nullable
        public Object getValueAt(int row, int col) {
            PackageEntry entry = packageTable.getEntryAt(row);
            if (entry == null || !isCellEditable(row, col))
                return null;
            col += panel.areStaticImportsEnabled() ? 0 : 1;
            if (col == 0) {
                return entry.isStatic();
            }
            if (col == 1) {
                return entry.getPackageName();
            }
            if (col == 2) {
                return entry.isWithSubpackages();
            }
            throw new IllegalArgumentException(String.valueOf(col));
        }

        public String getColumnName(int column) {
            if (panel.areStaticImportsEnabled() && column == 0)
                return "Static";
            column -= panel.areStaticImportsEnabled() ? 1 : 0;
            return names[column];
        }

        public Class getColumnClass(int col) {
            col += panel.areStaticImportsEnabled() ? 0 : 1;
            if (col == 0) {
                return Boolean.class;
            }
            if (col == 1) {
                return String.class;
            }
            if (col == 2) {
                return Boolean.class;
            }
            throw new IllegalArgumentException(String.valueOf(col));
        }

        public boolean isCellEditable(int row, int col) {
            PackageEntry packageEntry = packageTable.getEntryAt(row);
            return !packageEntry.isSpecial();
        }

        public void setValueAt(Object aValue, int row, int col) {
            PackageEntry packageEntry = packageTable.getEntryAt(row);
            col += panel.areStaticImportsEnabled() ? 0 : 1;
            if (col == 0) {
                PackageEntry newPackageEntry = new PackageEntry((Boolean) aValue, packageEntry.getPackageName(), packageEntry.isWithSubpackages());
                packageTable.setEntryAt(newPackageEntry, row);
            } else if (col == 1) {
                PackageEntry newPackageEntry = new PackageEntry(packageEntry.isStatic(), ((String) aValue).trim(), packageEntry.isWithSubpackages());
                packageTable.setEntryAt(newPackageEntry, row);
            } else if (col == 2) {
                PackageEntry newPackageEntry = new PackageEntry(packageEntry.isStatic(), packageEntry.getPackageName(), ((Boolean) aValue).booleanValue());
                packageTable.setEntryAt(newPackageEntry, row);
            } else {
                throw new IllegalArgumentException(String.valueOf(col));
            }
        }
    };
    // Create the table
    final JBTable result = new JBTable(dataModel);
    result.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    resizeColumns(packageTable, result, panel.areStaticImportsEnabled());
    TableCellEditor editor = result.getDefaultEditor(String.class);
    if (editor instanceof DefaultCellEditor) {
        ((DefaultCellEditor) editor).setClickCountToStart(1);
    }
    TableCellEditor beditor = result.getDefaultEditor(Boolean.class);
    beditor.addCellEditorListener(new CellEditorListener() {

        public void editingStopped(ChangeEvent e) {
            if (panel.areStaticImportsEnabled()) {
                // add/remove static keyword
                result.repaint();
            }
        }

        public void editingCanceled(ChangeEvent e) {
        }
    });
    return result;
}
Also used : CellEditorListener(javax.swing.event.CellEditorListener) JBTable(com.intellij.ui.table.JBTable) ChangeEvent(javax.swing.event.ChangeEvent) PackageEntry(com.intellij.psi.codeStyle.PackageEntry) AbstractTableModel(javax.swing.table.AbstractTableModel) TableCellEditor(javax.swing.table.TableCellEditor) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Example 3 with CellEditorListener

use of javax.swing.event.CellEditorListener in project jdk8u_jdk by JetBrains.

the class XTextFieldEditor method fireEditingCanceled.

protected void fireEditingCanceled() {
    CellEditorListener listener;
    Object[] listeners = evtListenerList.getListenerList();
    for (int i = 0; i < listeners.length; i++) {
        if (listeners[i] == CellEditorListener.class) {
            listener = (CellEditorListener) listeners[i + 1];
            listener.editingCanceled(changeEvent);
        }
    }
}
Also used : CellEditorListener(javax.swing.event.CellEditorListener) EventObject(java.util.EventObject)

Example 4 with CellEditorListener

use of javax.swing.event.CellEditorListener in project cytoscape-api by cytoscape.

the class AbstractCellEditorTest method testFireEditingStopped.

@Test
public void testFireEditingStopped() {
    CellEditorListener l = mock(CellEditorListener.class);
    editor.addCellEditorListener(l);
    editor.fireEditingStopped();
    verify(l, times(1)).editingStopped((ChangeEvent) any());
}
Also used : CellEditorListener(javax.swing.event.CellEditorListener) Test(org.junit.Test)

Example 5 with CellEditorListener

use of javax.swing.event.CellEditorListener in project cytoscape-api by cytoscape.

the class AbstractCellEditorTest method testFireEditingCanceled.

@Test
public void testFireEditingCanceled() {
    CellEditorListener l = mock(CellEditorListener.class);
    editor.addCellEditorListener(l);
    editor.fireEditingCanceled();
    verify(l, times(1)).editingCanceled((ChangeEvent) any());
}
Also used : CellEditorListener(javax.swing.event.CellEditorListener) Test(org.junit.Test)

Aggregations

CellEditorListener (javax.swing.event.CellEditorListener)12 ChangeEvent (javax.swing.event.ChangeEvent)6 EventObject (java.util.EventObject)3 TableCellRenderer (javax.swing.table.TableCellRenderer)3 Test (org.junit.Test)3 ActionEvent (java.awt.event.ActionEvent)2 TableCellEditor (javax.swing.table.TableCellEditor)2 PackageEntry (com.intellij.psi.codeStyle.PackageEntry)1 RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)1 RefactoringElementListenerProvider (com.intellij.refactoring.listeners.RefactoringElementListenerProvider)1 JBTable (com.intellij.ui.table.JBTable)1 ListTreeTableModelOnColumns (com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns)1 TreeTableTree (com.intellij.ui.treeStructure.treetable.TreeTableTree)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ColumnInfo (com.intellij.util.ui.ColumnInfo)1 ActionListener (java.awt.event.ActionListener)1 ArrayList (java.util.ArrayList)1 AbstractAction (javax.swing.AbstractAction)1 Action (javax.swing.Action)1 InputMap (javax.swing.InputMap)1