Search in sources :

Example 1 with PreferencesPanel

use of jmri.swing.PreferencesPanel in project JMRI by JMRI.

the class TabbedPreferences method init.

/**
     * Initialize, including loading classes provided by a
     * {@link java.util.ServiceLoader}.
     * <p>
     * Keeps a current state to prevent doing its work twice.
     *
     * @return The current state, which should be INITIALISED if all is well.
     */
@SuppressWarnings("rawtypes")
public synchronized int init() {
    if (initialisationState == INITIALISED) {
        return INITIALISED;
    }
    if (initialisationState != UNINITIALISED) {
        return initialisationState;
    }
    this.setInitalisationState(INITIALISING);
    list = new JList<>();
    listScroller = new JScrollPane(list);
    listScroller.setPreferredSize(new Dimension(100, 100));
    buttonpanel = new JPanel();
    buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.Y_AXIS));
    buttonpanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 3));
    detailpanel = new JPanel();
    detailpanel.setLayout(new CardLayout());
    detailpanel.setBorder(BorderFactory.createEmptyBorder(6, 3, 6, 6));
    save = new JButton(ConfigBundle.getMessage("ButtonSave"), new ImageIcon(FileUtil.findURL("program:resources/icons/misc/gui3/SaveIcon.png", FileUtil.Location.INSTALLED)));
    save.addActionListener((ActionEvent e) -> {
        savePressed(invokeSaveOptions());
    });
    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    Set<PreferencesPanel> delayed = new HashSet<>();
    for (PreferencesPanel panel : InstanceManager.getList(jmri.swing.PreferencesPanel.class)) {
        if (panel instanceof PreferencesSubPanel) {
            String parent = ((PreferencesSubPanel) panel).getParentClassName();
            if (!this.getPreferencesPanels().containsKey(parent)) {
                delayed.add(panel);
            } else {
                ((PreferencesSubPanel) panel).setParent(this.getPreferencesPanels().get(parent));
            }
        }
        if (!delayed.contains(panel)) {
            this.addPreferencesPanel(panel);
        }
    }
    for (PreferencesPanel panel : ServiceLoader.load(PreferencesPanel.class)) {
        if (panel instanceof PreferencesSubPanel) {
            String parent = ((PreferencesSubPanel) panel).getParentClassName();
            if (!this.getPreferencesPanels().containsKey(parent)) {
                delayed.add(panel);
            } else {
                ((PreferencesSubPanel) panel).setParent(this.getPreferencesPanels().get(parent));
            }
        }
        if (!delayed.contains(panel)) {
            this.addPreferencesPanel(panel);
        }
    }
    while (!delayed.isEmpty()) {
        Set<PreferencesPanel> iterated = new HashSet<>(delayed);
        iterated.stream().filter((panel) -> (panel instanceof PreferencesSubPanel)).forEach((panel) -> {
            String parent = ((PreferencesSubPanel) panel).getParentClassName();
            if (this.getPreferencesPanels().containsKey(parent)) {
                ((PreferencesSubPanel) panel).setParent(this.getPreferencesPanels().get(parent));
                delayed.remove(panel);
                this.addPreferencesPanel(panel);
            }
        });
    }
    preferencesArray.stream().forEach((preferences) -> {
        detailpanel.add(preferences.getPanel(), preferences.getPrefItem());
    });
    updateJList();
    add(buttonpanel);
    add(new JSeparator(JSeparator.VERTICAL));
    add(detailpanel);
    list.setSelectedIndex(0);
    selection(preferencesArray.get(0).getPrefItem());
    this.setInitalisationState(INITIALISED);
    return initialisationState;
}
Also used : JScrollPane(javax.swing.JScrollPane) ListSelectionModel(javax.swing.ListSelectionModel) CardLayout(java.awt.CardLayout) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConfigBundle(apps.ConfigBundle) JTabbedPane(javax.swing.JTabbedPane) ImageIcon(javax.swing.ImageIcon) BorderLayout(java.awt.BorderLayout) ListSelectionEvent(javax.swing.event.ListSelectionEvent) PreferencesSubPanel(jmri.swing.PreferencesSubPanel) BoxLayout(javax.swing.BoxLayout) PreferencesPanel(jmri.swing.PreferencesPanel) AppConfigBase(apps.AppConfigBase) JComponent(javax.swing.JComponent) InstanceManager(jmri.InstanceManager) JButton(javax.swing.JButton) Logger(org.slf4j.Logger) JList(javax.swing.JList) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) BorderFactory(javax.swing.BorderFactory) ActionEvent(java.awt.event.ActionEvent) JScrollPane(javax.swing.JScrollPane) Dimension(java.awt.Dimension) List(java.util.List) FileUtil(jmri.util.FileUtil) JLabel(javax.swing.JLabel) JSeparator(javax.swing.JSeparator) JPanel(javax.swing.JPanel) Element(org.jdom2.Element) JPanel(javax.swing.JPanel) CardLayout(java.awt.CardLayout) ImageIcon(javax.swing.ImageIcon) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) PreferencesPanel(jmri.swing.PreferencesPanel) JButton(javax.swing.JButton) Dimension(java.awt.Dimension) JSeparator(javax.swing.JSeparator) PreferencesSubPanel(jmri.swing.PreferencesSubPanel) HashSet(java.util.HashSet)

Aggregations

AppConfigBase (apps.AppConfigBase)1 ConfigBundle (apps.ConfigBundle)1 BorderLayout (java.awt.BorderLayout)1 CardLayout (java.awt.CardLayout)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ServiceLoader (java.util.ServiceLoader)1 Set (java.util.Set)1 BorderFactory (javax.swing.BorderFactory)1 BoxLayout (javax.swing.BoxLayout)1 ImageIcon (javax.swing.ImageIcon)1 JButton (javax.swing.JButton)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1 JList (javax.swing.JList)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1