Search in sources :

Example 16 with DefaultListModel

use of javax.swing.DefaultListModel in project JMRI by JMRI.

the class SensorGroupFrame method deleteGroup.

void deleteGroup(boolean showMsg) {
    String group = _nameField.getText();
    if (group == null || group.equals("")) {
        if (showMsg) {
            javax.swing.JOptionPane.showMessageDialog(this, "'View' the group or enter the group name in the 'Group Name' field before selecting 'Undo Group'", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
        return;
    }
    String prefix = (namePrefix + group + nameDivider).toUpperCase();
    // remove the old routes
    RouteManager rm = InstanceManager.getDefault(jmri.RouteManager.class);
    List<String> l = rm.getSystemNameList();
    for (int i = 0; i < l.size(); i++) {
        String name = l.get(i);
        if (name.startsWith(prefix)) {
            // OK, kill this one
            Route r = rm.getBySystemName(l.get(i));
            r.deActivateRoute();
            rm.deleteRoute(r);
        }
    }
    String cSystemName = (ConditionalSystemPrefix + group).toUpperCase();
    String cUserName = ConditionalUserPrefix + group;
    Logix logix = getSystemLogix();
    for (int i = 0; i < logix.getNumConditionals(); i++) {
        String name = logix.getConditionalByNumberOrder(i);
        if (cSystemName.equals(name) || cUserName.equals(name)) {
            Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(name);
            if (c == null) {
                log.error("Conditional \"" + name + "\" expected but NOT found in Logix " + logix.getSystemName());
            } else {
                logix.deleteConditional(cSystemName);
                break;
            }
        }
    }
    DefaultListModel<String> model = (DefaultListModel<String>) _sensorGroupList.getModel();
    int index = model.indexOf(group);
    if (index > -1) {
        model.remove(index);
    }
    index = _sensorGroupList.getSelectedIndex();
    if (index > -1) {
        String sysName = ConditionalSystemPrefix + model.elementAt(index);
        String[] msgs = logix.deleteConditional(sysName);
        if (msgs != null) {
            if (showMsg) {
                javax.swing.JOptionPane.showMessageDialog(this, "Conditional " + msgs[0] + " (" + msgs[1] + ") is a Conditional Variable in the Conditional,\n" + msgs[2] + " (" + msgs[3] + "), of Logix, " + msgs[4] + " (" + msgs[5] + ").\nPlease remove that variable first.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
            }
        } else {
            model.remove(index);
        }
    }
}
Also used : Logix(jmri.Logix) DefaultListModel(javax.swing.DefaultListModel) Conditional(jmri.Conditional) SensorGroupConditional(jmri.implementation.SensorGroupConditional) Route(jmri.Route) RouteManager(jmri.RouteManager)

Example 17 with DefaultListModel

use of javax.swing.DefaultListModel in project JMRI by JMRI.

the class Maintenance method findOrphansPressed.

/**
     * Find orphaned elements in the various Manager Objects.
     *
     * @param parent Frame to check
     */
public static void findOrphansPressed(Frame parent) {
    Vector<String> display = new Vector<String>();
    Vector<String> names = new Vector<String>();
    Iterator<String> iter = InstanceManager.sensorManagerInstance().getSystemNameList().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        if (!search(name, null) && !name.equals("ISCLOCKRUNNING")) {
            display.add(MessageFormat.format(rbm.getString("OrphanName"), (Object[]) getTypeAndNames(name)));
            names.add(name);
        }
    }
    iter = InstanceManager.turnoutManagerInstance().getSystemNameList().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        if (!search(name, null)) {
            display.add(MessageFormat.format(rbm.getString("OrphanName"), (Object[]) getTypeAndNames(name)));
            names.add(name);
        }
    }
    iter = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSystemNameList().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        if (!search(name, null)) {
            display.add(MessageFormat.format(rbm.getString("OrphanName"), (Object[]) getTypeAndNames(name)));
            names.add(name);
        }
    }
    iter = InstanceManager.lightManagerInstance().getSystemNameList().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        if (!search(name, null)) {
            display.add(MessageFormat.format(rbm.getString("OrphanName"), (Object[]) getTypeAndNames(name)));
            names.add(name);
        }
    }
    iter = InstanceManager.getDefault(jmri.ConditionalManager.class).getSystemNameList().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        if (!search(name, null)) {
            display.add(MessageFormat.format(rbm.getString("OrphanName"), (Object[]) getTypeAndNames(name)));
            names.add(name);
        }
    }
    iter = InstanceManager.getDefault(jmri.SectionManager.class).getSystemNameList().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        if (!search(name, null)) {
            display.add(MessageFormat.format(rbm.getString("OrphanName"), (Object[]) getTypeAndNames(name)));
            names.add(name);
        }
    }
    iter = InstanceManager.getDefault(jmri.BlockManager.class).getSystemNameList().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        if (!search(name, null)) {
            display.add(MessageFormat.format(rbm.getString("OrphanName"), (Object[]) getTypeAndNames(name)));
            names.add(name);
        }
    }
    DefaultListModel<String> listModel = new DefaultListModel<String>();
    for (int i = 0; i < display.size(); i++) {
        listModel.addElement(display.get(i));
    }
    JList<String> list = new JList<String>(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    JButton button = new JButton(Bundle.getMessage("ButtonDelete"));
    button.setToolTipText(rbm.getString("OrphanDeleteHint"));
    class SearchListener implements ActionListener {

        JList<String> list;

        Vector<String> n;

        SearchListener(JList<String> list, Vector<String> name) {
            this.list = list;
            this.n = name;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int index = list.getMaxSelectionIndex();
            if (index < 0) {
                javax.swing.JOptionPane.showMessageDialog(null, rbm.getString("OrphanDeleteHint"), rbm.getString("DeleteTitle"), javax.swing.JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            int min = list.getMinSelectionIndex();
            DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
            while (index >= min) {
                String[] names = getTypeAndNames(n.get(index));
                if (names[0].equals("Sensor")) {
                    // NOI18N
                    Sensor s = InstanceManager.sensorManagerInstance().getBySystemName(names[2]);
                    if (s == null) {
                        s = InstanceManager.sensorManagerInstance().getBySystemName(names[1]);
                    }
                    InstanceManager.sensorManagerInstance().deregister(s);
                } else if (names[0].equals("Turnout")) {
                    // NOI18N
                    Turnout t = InstanceManager.turnoutManagerInstance().getBySystemName(names[2]);
                    if (t == null) {
                        t = InstanceManager.turnoutManagerInstance().getBySystemName(names[1]);
                    }
                    InstanceManager.turnoutManagerInstance().deregister(t);
                } else if (names[0].equals("SignalHead")) {
                    // NOI18N
                    SignalHead sh = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(names[2]);
                    if (sh == null) {
                        sh = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(names[1]);
                    }
                    InstanceManager.getDefault(jmri.SignalHeadManager.class).deregister(sh);
                } else if (names[0].equals("Light")) {
                    // NOI18N
                    Light l = InstanceManager.lightManagerInstance().getBySystemName(names[2]);
                    if (l == null) {
                        l = InstanceManager.lightManagerInstance().getBySystemName(names[1]);
                    }
                    InstanceManager.lightManagerInstance().deregister(l);
                } else if (names[0].equals("Conditional")) {
                    // NOI18N
                    Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(names[2]);
                    if (c == null) {
                        c = InstanceManager.getDefault(jmri.ConditionalManager.class).getBySystemName(names[1]);
                    }
                    InstanceManager.getDefault(jmri.ConditionalManager.class).deregister(c);
                } else if (names[0].equals("Section")) {
                    // NOI18N
                    jmri.Section sec = InstanceManager.getDefault(jmri.SectionManager.class).getBySystemName(names[2]);
                    if (sec == null) {
                        sec = InstanceManager.getDefault(jmri.SectionManager.class).getBySystemName(names[1]);
                    }
                    InstanceManager.getDefault(jmri.SectionManager.class).deregister(sec);
                } else if (names[0].equals("Block")) {
                    // NOI18N
                    jmri.Block b = InstanceManager.getDefault(jmri.BlockManager.class).getBySystemName(names[2]);
                    if (b == null) {
                        b = InstanceManager.getDefault(jmri.BlockManager.class).getBySystemName(names[1]);
                    }
                    InstanceManager.getDefault(jmri.BlockManager.class).deregister(b);
                }
                model.remove(index);
                n.remove(index);
                index--;
            }
            index++;
            if (index >= model.getSize()) {
                index = model.getSize() - 1;
            }
            if (index >= 0) {
                list.setSelectedIndex(index);
            }
        }
    }
    JScrollPane scrollPane = new JScrollPane(list);
    button.addActionListener(new SearchListener(list, names));
    button.setMaximumSize(button.getPreferredSize());
    makeDialog(scrollPane, button, parent, rbm.getString("OrphanTitle"));
}
Also used : ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) DefaultListModel(javax.swing.DefaultListModel) SignalHead(jmri.SignalHead) Conditional(jmri.Conditional) Light(jmri.Light) Vector(java.util.Vector) JScrollPane(javax.swing.JScrollPane) Block(jmri.Block) ActionListener(java.awt.event.ActionListener) LayoutBlockManager(jmri.jmrit.display.layoutEditor.LayoutBlockManager) Turnout(jmri.Turnout) JList(javax.swing.JList) Sensor(jmri.Sensor)

Example 18 with DefaultListModel

use of javax.swing.DefaultListModel in project jabref by JabRef.

the class ManageKeywordsAction method createDialog.

private void createDialog() {
    if (diag != null) {
        return;
    }
    // keyword to add
    JTextField keyword = new JTextField();
    keywordListModel = new DefaultListModel<>();
    JList<Keyword> keywordList = new JList<>(keywordListModel);
    keywordList.setVisibleRowCount(8);
    JScrollPane kPane = new JScrollPane(keywordList);
    diag = new JDialog(frame, Localization.lang("Manage keywords"), true);
    JButton ok = new JButton(Localization.lang("OK"));
    JButton cancel = new JButton(Localization.lang("Cancel"));
    JButton add = new JButton(Localization.lang("Add"));
    JButton remove = new JButton(Localization.lang("Remove"));
    keywordList.setVisibleRowCount(10);
    intersectKeywords = new JRadioButton(Localization.lang("Display keywords appearing in ALL entries"));
    mergeKeywords = new JRadioButton(Localization.lang("Display keywords appearing in ANY entry"));
    ButtonGroup group = new ButtonGroup();
    group.add(intersectKeywords);
    group.add(mergeKeywords);
    ActionListener stateChanged = e -> fillKeyWordList();
    intersectKeywords.addActionListener(stateChanged);
    mergeKeywords.addActionListener(stateChanged);
    intersectKeywords.setSelected(true);
    FormBuilder builder = FormBuilder.create().layout(new FormLayout("fill:200dlu:grow, 4dlu, fill:pref", "pref, 2dlu, pref, 1dlu, pref, 2dlu, fill:100dlu:grow, 4dlu, pref, 4dlu, pref, "));
    builder.addSeparator(Localization.lang("Keywords of selected entries")).xyw(1, 1, 3);
    builder.add(intersectKeywords).xyw(1, 3, 3);
    builder.add(mergeKeywords).xyw(1, 5, 3);
    builder.add(kPane).xywh(1, 7, 1, 3);
    builder.add(remove).xy(3, 9);
    builder.add(keyword).xy(1, 11);
    builder.add(add).xy(3, 11);
    ButtonBarBuilder bb = new ButtonBarBuilder();
    bb.addGlue();
    bb.addButton(ok);
    bb.addButton(cancel);
    bb.addGlue();
    builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    bb.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    ok.addActionListener(e -> {
        canceled = false;
        diag.dispose();
    });
    Action cancelAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            canceled = true;
            diag.dispose();
        }
    };
    cancel.addActionListener(cancelAction);
    final ActionListener addActionListener = arg0 -> addButtonActionListener(keyword);
    add.addActionListener(addActionListener);
    final ActionListener removeActionListenter = arg0 -> {
        List<Keyword> values = keywordList.getSelectedValuesList();
        for (Keyword val : values) {
            keywordListModel.removeElement(val);
        }
    };
    remove.addActionListener(removeActionListenter);
    keywordList.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent arg0) {
        // Do nothing
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
        // Do nothing
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_DELETE) {
                removeActionListenter.actionPerformed(null);
            }
        }
    });
    AutoCompleter<String> autoComp = JabRefGUI.getMainFrame().getCurrentBasePanel().getAutoCompleters().get(FieldName.KEYWORDS);
    AutoCompleteListener acl = new AutoCompleteListener(autoComp);
    keyword.addKeyListener(acl);
    keyword.addFocusListener(acl);
    keyword.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        // Do nothing
        }

        @Override
        public void keyReleased(KeyEvent e) {
        // Do nothing
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                addActionListener.actionPerformed(null);
            }
        }
    });
    // Key bindings:
    ActionMap am = builder.getPanel().getActionMap();
    InputMap im = builder.getPanel().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    am.put("close", cancelAction);
    diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
    diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
}
Also used : KeyListener(java.awt.event.KeyListener) ActionListener(java.awt.event.ActionListener) NamedCompound(org.jabref.gui.undo.NamedCompound) AutoCompleter(org.jabref.logic.autocompleter.AutoCompleter) Enumeration(java.util.Enumeration) JDialog(javax.swing.JDialog) JTextField(javax.swing.JTextField) FieldName(org.jabref.model.entry.FieldName) StringUtil(org.jabref.model.strings.StringUtil) ButtonBarBuilder(com.jgoodies.forms.builder.ButtonBarBuilder) KeywordList(org.jabref.model.entry.KeywordList) Action(javax.swing.Action) BasePanel(org.jabref.gui.BasePanel) JabRefFrame(org.jabref.gui.JabRefFrame) Localization(org.jabref.logic.l10n.Localization) Keyword(org.jabref.model.entry.Keyword) BorderLayout(java.awt.BorderLayout) SpecialFieldsUtils(org.jabref.logic.specialfields.SpecialFieldsUtils) ActionMap(javax.swing.ActionMap) JComponent(javax.swing.JComponent) FormBuilder(com.jgoodies.forms.builder.FormBuilder) JButton(javax.swing.JButton) ButtonGroup(javax.swing.ButtonGroup) BibEntry(org.jabref.model.entry.BibEntry) JList(javax.swing.JList) BorderFactory(javax.swing.BorderFactory) JabRefGUI(org.jabref.JabRefGUI) KeyEvent(java.awt.event.KeyEvent) ActionEvent(java.awt.event.ActionEvent) JRadioButton(javax.swing.JRadioButton) Globals(org.jabref.Globals) JScrollPane(javax.swing.JScrollPane) FieldChange(org.jabref.model.FieldChange) List(java.util.List) DefaultListModel(javax.swing.DefaultListModel) AbstractAction(javax.swing.AbstractAction) FormLayout(com.jgoodies.forms.layout.FormLayout) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) Optional(java.util.Optional) AutoCompleteListener(org.jabref.gui.autocompleter.AutoCompleteListener) InputMap(javax.swing.InputMap) KeyBinding(org.jabref.gui.keyboard.KeyBinding) Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) JRadioButton(javax.swing.JRadioButton) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) KeyEvent(java.awt.event.KeyEvent) ButtonBarBuilder(com.jgoodies.forms.builder.ButtonBarBuilder) KeywordList(org.jabref.model.entry.KeywordList) JList(javax.swing.JList) List(java.util.List) AbstractAction(javax.swing.AbstractAction) JScrollPane(javax.swing.JScrollPane) FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) Keyword(org.jabref.model.entry.Keyword) ActionMap(javax.swing.ActionMap) AutoCompleteListener(org.jabref.gui.autocompleter.AutoCompleteListener) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) KeyListener(java.awt.event.KeyListener) InputMap(javax.swing.InputMap) JList(javax.swing.JList) JDialog(javax.swing.JDialog)

Example 19 with DefaultListModel

use of javax.swing.DefaultListModel in project jabref by JabRef.

the class ContentSelectorDialog method applyChanges.

private void applyChanges() {
    // Watch if we need to rebuild entry editors
    boolean changedFieldSet = false;
    // Watch if we should mark as there is data changed
    boolean anythingChanged = false;
    // cause any harm to remove them here.
    for (String fieldName : removedFields) {
        metaData.clearContentSelectors(fieldName);
        changedFieldSet = true;
        anythingChanged = true;
    }
    // Cycle through all fields that we have created listmodels for:
    for (Map.Entry<String, DefaultListModel<String>> entry : wordListModels.entrySet()) {
        // For each field name, store the values:
        if ((entry.getKey() == null) || FIELD_FIRST_LINE.equals(entry.getKey())) {
            continue;
        }
        DefaultListModel<String> lm = entry.getValue();
        int start = 0;
        // Avoid storing the <new word> marker if it is there:
        if (!lm.isEmpty()) {
            while ((start < lm.size()) && lm.get(start).equals(WORD_FIRSTLINE_TEXT)) {
                start++;
            }
        }
        Set<String> data = new HashSet<>();
        for (int wrd = start; wrd < lm.size(); wrd++) {
            String word = lm.get(wrd);
            data.add(word);
        }
        // Check if any words have been added
        if (!data.equals(new HashSet<>(metaData.getContentSelectorValuesForField(entry.getKey())))) {
            anythingChanged = true;
        }
        // Check if there are words to be added and previously there were no content selector for the field
        if (!data.isEmpty() && metaData.getContentSelectorValuesForField(entry.getKey()).isEmpty()) {
            changedFieldSet = true;
        }
        metaData.addContentSelector(new ContentSelector(entry.getKey(), new ArrayList<>(data)));
    }
    // Update all selectors in the current BasePanel.
    if (changedFieldSet) {
        // We have added or removed content selectors, update the entry editor
        panel.rebuildAllEntryEditors();
    } else if (anythingChanged) {
        // Enough to update the content selectors, if anything changed
        panel.updateAllContentSelectors();
    }
    if (anythingChanged) {
        // Mark the database updated so changes are not lost
        panel.markNonUndoableBaseChanged();
    }
    panel.getAutoCompleters().addContentSelectorValuesToAutoCompleters(panel.getBibDatabaseContext().getMetaData());
}
Also used : ContentSelector(org.jabref.model.metadata.ContentSelector) ArrayList(java.util.ArrayList) DefaultListModel(javax.swing.DefaultListModel) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 20 with DefaultListModel

use of javax.swing.DefaultListModel in project jabref by JabRef.

the class DetectOpenOfficeInstallation method chooseAmongInstallations.

private Optional<Path> chooseAmongInstallations(List<Path> installDirs) {
    if (installDirs.isEmpty()) {
        return Optional.empty();
    }
    if (installDirs.size() == 1) {
        return Optional.of(installDirs.get(0).toAbsolutePath());
    }
    // Otherwise more than one installation was found, select among them
    DefaultListModel<File> mod = new DefaultListModel<>();
    for (Path tmpfile : installDirs) {
        mod.addElement(tmpfile.toFile());
    }
    JList<File> fileList = new JList<>(mod);
    fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    fileList.setSelectedIndex(0);
    FormBuilder builder = FormBuilder.create().layout(new FormLayout("pref:grow", "pref, 2dlu, pref, 4dlu, pref"));
    builder.add(Localization.lang("Found more than one OpenOffice/LibreOffice executable.")).xy(1, 1);
    builder.add(Localization.lang("Please choose which one to connect to:")).xy(1, 3);
    builder.add(fileList).xy(1, 5);
    int answer = JOptionPane.showConfirmDialog(null, builder.getPanel(), Localization.lang("Choose OpenOffice/LibreOffice executable"), JOptionPane.OK_CANCEL_OPTION);
    if (answer == JOptionPane.CANCEL_OPTION) {
        return Optional.empty();
    } else {
        return Optional.of(fileList.getSelectedValue().toPath());
    }
}
Also used : Path(java.nio.file.Path) FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) DefaultListModel(javax.swing.DefaultListModel) File(java.io.File) JList(javax.swing.JList)

Aggregations

DefaultListModel (javax.swing.DefaultListModel)41 JList (javax.swing.JList)21 ArrayList (java.util.ArrayList)16 JScrollPane (javax.swing.JScrollPane)13 List (java.util.List)8 JButton (javax.swing.JButton)8 ActionEvent (java.awt.event.ActionEvent)5 ActionListener (java.awt.event.ActionListener)5 JPanel (javax.swing.JPanel)5 Conditional (jmri.Conditional)5 Dimension (java.awt.Dimension)4 GridBagConstraints (java.awt.GridBagConstraints)4 GridBagLayout (java.awt.GridBagLayout)4 Insets (java.awt.Insets)4 Logix (jmri.Logix)4 Component (java.awt.Component)3 JLabel (javax.swing.JLabel)3 SensorGroupConditional (jmri.implementation.SensorGroupConditional)3 FormBuilder (com.jgoodies.forms.builder.FormBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2