Search in sources :

Example 1 with ListDataListener

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

the class SpecialAnnotationsUtil method createSpecialAnnotationsListControl.

public static JPanel createSpecialAnnotationsListControl(final List<String> list, final String borderTitle, final boolean acceptPatterns, final Condition<PsiClass> isApplicable) {
    @SuppressWarnings("Convert2Diamond") SortedListModel<String> listModel = new SortedListModel<String>(Comparator.naturalOrder());
    for (String s : list) {
        listModel.add(s);
    }
    listModel.addListDataListener(new ListDataListener() {

        @Override
        public void intervalAdded(ListDataEvent e) {
            listChanged();
        }

        private void listChanged() {
            list.clear();
            for (int i = 0; i < listModel.getSize(); i++) {
                list.add((String) listModel.getElementAt(i));
            }
        }

        @Override
        public void intervalRemoved(ListDataEvent e) {
            listChanged();
        }

        @Override
        public void contentsChanged(ListDataEvent e) {
            listChanged();
        }
    });
    return createSpecialAnnotationsListControl(borderTitle, acceptPatterns, isApplicable, listModel);
}
Also used : ListDataEvent(javax.swing.event.ListDataEvent) ListDataListener(javax.swing.event.ListDataListener)

Example 2 with ListDataListener

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

the class SortedListModelTest method testRemoveViaIterator.

public void testRemoveViaIterator() {
    myModel.setAll(new String[] { "1", "2", "3", "4" });
    Iterator iterator = myModel.iterator();
    final TIntArrayList removed = new TIntArrayList();
    myModel.addListDataListener(new ListDataListener() {

        @Override
        public void contentsChanged(ListDataEvent e) {
            throw new RuntimeException();
        }

        @Override
        public void intervalAdded(ListDataEvent e) {
            throw new RuntimeException();
        }

        @Override
        public void intervalRemoved(ListDataEvent e) {
            assertEquals(e.getIndex0(), e.getIndex1());
            removed.add(e.getIndex0());
        }
    });
    iterator.next();
    iterator.remove();
    assertEquals(1, removed.size());
    assertEquals(0, removed.get(0));
    while (iterator.hasNext()) iterator.next();
    assertFalse(iterator.hasNext());
    iterator.remove();
    assertEquals(2, removed.size());
    assertEquals(2, removed.get(1));
    CHECK.compareAll(new String[] { "2", "3" }, myModel.getItems());
}
Also used : ListDataEvent(javax.swing.event.ListDataEvent) Iterator(java.util.Iterator) ListDataListener(javax.swing.event.ListDataListener) TIntArrayList(gnu.trove.TIntArrayList)

Example 3 with ListDataListener

use of javax.swing.event.ListDataListener in project jabref by JabRef.

the class FieldFormatterCleanupsPanel method buildLayout.

private void buildLayout(List<FieldFormatterCleanup> actionsToDisplay) {
    FormBuilder builder = FormBuilder.create().layout(new FormLayout("left:pref, 13dlu, left:pref:grow, 4dlu, pref, 4dlu, pref", "pref, 2dlu, pref, 2dlu, pref, 4dlu, pref, 2dlu, fill:pref:grow, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu"));
    builder.add(cleanupEnabled).xyw(1, 1, 7);
    actionsList = new JList<>(new CleanupActionsListModel(actionsToDisplay));
    actionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    actionsList.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseMoved(MouseEvent e) {
            super.mouseMoved(e);
            CleanupActionsListModel m = (CleanupActionsListModel) actionsList.getModel();
            int index = actionsList.locationToIndex(e.getPoint());
            if (index > -1) {
                actionsList.setToolTipText(m.getElementAt(index).getFormatter().getDescription());
            }
        }
    });
    actionsList.getModel().addListDataListener(new ListDataListener() {

        @Override
        public void intervalRemoved(ListDataEvent e) {
            //index0 is sufficient, because of SingleSelection
            if (e.getIndex0() == 0) {
                //when an item gets deleted, the next one becomes the new 0
                actionsList.setSelectedIndex(e.getIndex0());
            }
            if (e.getIndex0() > 0) {
                actionsList.setSelectedIndex(e.getIndex0() - 1);
            }
        }

        @Override
        public void intervalAdded(ListDataEvent e) {
        //empty, not needed
        }

        @Override
        public void contentsChanged(ListDataEvent e) {
        //empty, not needed
        }
    });
    builder.add(actionsList).xyw(3, 5, 5);
    resetButton = new JButton(Localization.lang("Reset"));
    resetButton.addActionListener(e -> ((CleanupActionsListModel) actionsList.getModel()).reset(defaultFormatters));
    BibDatabaseContext databaseContext = JabRefGUI.getMainFrame().getCurrentBasePanel().getDatabaseContext();
    recommendButton = new JButton(Localization.lang("Recommended for %0", databaseContext.getMode().getFormattedName()));
    boolean isBiblatex = databaseContext.isBiblatexMode();
    recommendButton.addActionListener(e -> {
        if (isBiblatex) {
            ((CleanupActionsListModel) actionsList.getModel()).reset(Cleanups.RECOMMEND_BIBLATEX_ACTIONS);
        } else {
            ((CleanupActionsListModel) actionsList.getModel()).reset(Cleanups.RECOMMEND_BIBTEX_ACTIONS);
        }
    });
    removeButton = new JButton(Localization.lang("Remove selected"));
    removeButton.addActionListener(e -> ((CleanupActionsListModel) actionsList.getModel()).removeAtIndex(actionsList.getSelectedIndex()));
    builder.add(removeButton).xy(7, 11);
    builder.add(resetButton).xy(3, 11);
    builder.add(recommendButton).xy(5, 11);
    builder.add(getSelectorPanel()).xyw(3, 15, 5);
    makeDescriptionTextAreaLikeJLabel();
    builder.add(descriptionAreaText).xyw(3, 17, 5);
    this.setLayout(new BorderLayout());
    this.add(builder.getPanel(), BorderLayout.WEST);
    updateDescription();
    // make sure the layout is set according to the checkbox
    cleanupEnabled.addActionListener(new EnablementStatusListener(fieldFormatterCleanups.isEnabled()));
    cleanupEnabled.setSelected(fieldFormatterCleanups.isEnabled());
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) MouseEvent(java.awt.event.MouseEvent) JButton(javax.swing.JButton) ListDataListener(javax.swing.event.ListDataListener) ListDataEvent(javax.swing.event.ListDataEvent) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) BorderLayout(java.awt.BorderLayout) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext)

Example 4 with ListDataListener

use of javax.swing.event.ListDataListener in project jabref by JabRef.

the class CleanupActionsListModelTest method resetFiresItemsChanged.

@Test
public void resetFiresItemsChanged() throws Exception {
    CleanupActionsListModel model = new CleanupActionsListModel(Collections.emptyList());
    ListDataListener listener = mock(ListDataListener.class);
    model.addListDataListener(listener);
    FieldFormatterCleanups defaultFormatters = mock(FieldFormatterCleanups.class);
    model.reset(defaultFormatters);
    ArgumentCaptor<ListDataEvent> argument = ArgumentCaptor.forClass(ListDataEvent.class);
    verify(listener).contentsChanged(argument.capture());
    assertEquals(ListDataEvent.CONTENTS_CHANGED, argument.getValue().getType());
}
Also used : ListDataEvent(javax.swing.event.ListDataEvent) CleanupActionsListModel(org.jabref.gui.cleanup.CleanupActionsListModel) FieldFormatterCleanups(org.jabref.model.cleanup.FieldFormatterCleanups) ListDataListener(javax.swing.event.ListDataListener) Test(org.junit.Test)

Example 5 with ListDataListener

use of javax.swing.event.ListDataListener in project jabref by JabRef.

the class CleanupActionsListModelTest method removedAtIndexMinus1DoesNothing.

@Test
public void removedAtIndexMinus1DoesNothing() {
    CleanupActionsListModel model = new CleanupActionsListModel(getDefaultFieldFormatterCleanups());
    ListDataListener listener = mock(ListDataListener.class);
    model.addListDataListener(listener);
    model.removeAtIndex(-1);
    verifyZeroInteractions(listener);
}
Also used : CleanupActionsListModel(org.jabref.gui.cleanup.CleanupActionsListModel) ListDataListener(javax.swing.event.ListDataListener) Test(org.junit.Test)

Aggregations

ListDataListener (javax.swing.event.ListDataListener)10 ListDataEvent (javax.swing.event.ListDataEvent)5 Test (org.junit.Test)5 CleanupActionsListModel (org.jabref.gui.cleanup.CleanupActionsListModel)4 BorderLayout (java.awt.BorderLayout)2 JButton (javax.swing.JButton)2 ButtonBarBuilder (com.jgoodies.forms.builder.ButtonBarBuilder)1 FormBuilder (com.jgoodies.forms.builder.FormBuilder)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 TIntArrayList (gnu.trove.TIntArrayList)1 Container (java.awt.Container)1 GridLayout (java.awt.GridLayout)1 ActionEvent (java.awt.event.ActionEvent)1 MouseEvent (java.awt.event.MouseEvent)1 MouseMotionAdapter (java.awt.event.MouseMotionAdapter)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 AbstractAction (javax.swing.AbstractAction)1 ActionMap (javax.swing.ActionMap)1 DefaultListModel (javax.swing.DefaultListModel)1