001package jmri.web.server;
002
003/**
004 * @author Steve Todd Copyright (C) 2011
005 * @author Randall Wood Copyright (C) 2012, 2014, 2016
006 */
007import jmri.util.startup.PerformActionModel;
008import jmri.util.startup.StartupActionsManager;
009
010import java.awt.event.ActionEvent;
011import java.awt.event.ItemEvent;
012import java.awt.event.ItemListener;
013import java.util.Arrays;
014
015import javax.swing.GroupLayout;
016import javax.swing.JCheckBox;
017import javax.swing.JComponent;
018import javax.swing.JLabel;
019import javax.swing.JPanel;
020import javax.swing.JSpinner;
021import javax.swing.LayoutStyle;
022import javax.swing.SpinnerNumberModel;
023
024import jmri.InstanceManager;
025import jmri.swing.PreferencesPanel;
026
027import org.jdesktop.beansbinding.AutoBinding;
028import org.jdesktop.beansbinding.BeanProperty;
029import org.jdesktop.beansbinding.BindingGroup;
030import org.jdesktop.beansbinding.Bindings;
031import org.jdesktop.beansbinding.ELProperty;
032import org.openide.util.lookup.ServiceProvider;
033
034@ServiceProvider(service = PreferencesPanel.class)
035public class WebServerPreferencesPanel extends JPanel implements PreferencesPanel {
036
037    private JSpinner port;
038    private JCheckBox readonlyPower;
039    private JLabel portLabel;
040    private final WebServerPreferences preferences;
041    private JCheckBox startup;
042    private ItemListener startupItemListener;
043    private int startupActionPosition = -1;
044    private BindingGroup bindingGroup;
045
046    public WebServerPreferencesPanel() {
047        preferences = InstanceManager.getDefault(WebServerPreferences.class);
048        initComponents();
049    }
050
051    private void initComponents() {
052        bindingGroup = new BindingGroup();
053        port = new JSpinner();
054        portLabel = new JLabel();
055        readonlyPower = new JCheckBox();
056        startup = new JCheckBox();
057
058        port.setModel(new SpinnerNumberModel(12080, 1, 65535, 1));
059        port.setEditor(new JSpinner.NumberEditor(port, "#"));
060        port.setToolTipText(Bundle.getMessage("ToolTipPort")); // NOI18N
061
062        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, preferences, ELProperty.create("${port}"), port, BeanProperty.create("value")));
063
064        portLabel.setText(Bundle.getMessage("LabelPort")); // NOI18N
065        portLabel.setToolTipText(Bundle.getMessage("ToolTipPort")); // NOI18N
066
067        readonlyPower.setText(Bundle.getMessage("LabelReadonlyPower")); // NOI18N
068        readonlyPower.addActionListener((ActionEvent e) -> {
069            readonlyPower.setToolTipText(Bundle.getMessage(readonlyPower.isSelected() ? "ToolTipReadonlyPowerTrue" : "ToolTipReadonlyPowerFalse"));
070        });
071
072        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, preferences, ELProperty.create("${readonlyPower}"), readonlyPower, BeanProperty.create("selected")));
073
074        startup.setSelected(this.isStartupAction());
075        startup.setText(Bundle.getMessage("LabelStartup")); // NOI18N
076        this.startupItemListener = (ItemEvent e) -> {
077            this.startup.removeItemListener(this.startupItemListener);
078            StartupActionsManager manager = InstanceManager.getDefault(StartupActionsManager.class);
079            if (this.startup.isSelected()) {
080                PerformActionModel model = new PerformActionModel();
081                model.setClassName(WebServerAction.class.getName());
082                if (this.startupActionPosition == -1 || this.startupActionPosition >= manager.getActions().length) {
083                    manager.addAction(model);
084                } else {
085                    manager.setActions(this.startupActionPosition, model);
086                }
087            } else {
088                manager.getActions(PerformActionModel.class).stream().filter((model) -> (WebServerAction.class.getName().equals(model.getClassName()))).forEach((model) -> {
089                    this.startupActionPosition = Arrays.asList(manager.getActions()).indexOf(model);
090                    manager.removeAction(model);
091                });
092            }
093            this.startup.addItemListener(this.startupItemListener);
094        };
095        this.startup.addItemListener(this.startupItemListener);
096
097        GroupLayout layout = new GroupLayout(this);
098        this.setLayout(layout);
099        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
100                .addGroup(layout.createSequentialGroup()
101                        .addContainerGap()
102                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
103                                .addGroup(layout.createSequentialGroup()
104                                        .addComponent(port, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
105                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
106                                        .addComponent(portLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
107                                .addComponent(startup, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
108                                .addComponent(readonlyPower, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
109                        .addContainerGap())
110        );
111        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
112                .addGroup(layout.createSequentialGroup()
113                        .addContainerGap()
114                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
115                                .addComponent(port, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
116                                .addComponent(portLabel))
117                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
118                        .addComponent(readonlyPower)
119                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
120                        .addComponent(startup)
121                        .addContainerGap(198, Short.MAX_VALUE))
122        );
123
124        bindingGroup.bind();
125    }
126
127    @Override
128    public String getPreferencesItem() {
129        return Bundle.getMessage("PreferencesItem");
130    }
131
132    @Override
133    public String getPreferencesItemText() {
134        return Bundle.getMessage("PreferencesItemTitle");
135    }
136
137    @Override
138    public String getTabbedPreferencesTitle() {
139        return Bundle.getMessage("PreferencesItemTitle");
140    }
141
142    @Override
143    public String getLabelKey() {
144        return null;
145    }
146
147    @Override
148    public JComponent getPreferencesComponent() {
149        return this;
150    }
151
152    @Override
153    public boolean isPersistant() {
154        return false;
155    }
156
157    @Override
158    public String getPreferencesTooltip() {
159        return null;
160    }
161
162    @Override
163    public void savePreferences() {
164        this.preferences.save();
165    }
166
167    @Override
168    public boolean isDirty() {
169        return this.preferences.isDirty();
170    }
171
172    @Override
173    public boolean isRestartRequired() {
174        return this.preferences.isRestartRequired();
175    }
176
177    @Override
178    public boolean isPreferencesValid() {
179        return true; // no validity checking performed
180    }
181
182    @Override
183    public int getSortOrder() {
184        return 1100;
185    }
186    
187    private boolean isStartupAction() {
188        return InstanceManager.getDefault(StartupActionsManager.class).getActions(PerformActionModel.class).stream()
189                .anyMatch((model) -> (WebServerAction.class.getName().equals(model.getClassName())));
190    }
191}