001/*============================================================================*
002 * WARNING      This class contains automatically modified code.      WARNING *
003 *                                                                            *
004 * The method initComponents() and the variable declarations between the      *
005 * "// Variables declaration - do not modify" and                             *
006 * "// End of variables declaration" comments will be overwritten if modified *
007 * by hand. Using the NetBeans IDE to edit this file is strongly recommended. *
008 *                                                                            *
009 * See http://jmri.org/help/en/html/doc/Technical/NetBeansGUIEditor.shtml for *
010 * more information.                                                          *
011 *============================================================================*/
012package jmri.web.server;
013
014import java.beans.PropertyChangeEvent;
015import java.beans.PropertyChangeListener;
016import java.util.ArrayList;
017import java.util.Enumeration;
018import javax.swing.ButtonGroup;
019import javax.swing.GroupLayout;
020import javax.swing.JCheckBox;
021import javax.swing.JComponent;
022import javax.swing.JLabel;
023import javax.swing.JPanel;
024import javax.swing.JRadioButton;
025import javax.swing.JScrollPane;
026import javax.swing.JSpinner;
027import javax.swing.JTextField;
028import javax.swing.LayoutStyle;
029import javax.swing.ListSelectionModel;
030import javax.swing.SpinnerNumberModel;
031import javax.swing.event.ListDataEvent;
032import javax.swing.event.ListDataListener;
033import jmri.InstanceManager;
034import jmri.swing.DefaultEditableListModel;
035import jmri.swing.DefaultListCellEditor;
036import jmri.swing.EditableList;
037import jmri.swing.PreferencesPanel;
038import org.jdesktop.beansbinding.AutoBinding;
039import org.jdesktop.beansbinding.BeanProperty;
040import org.jdesktop.beansbinding.BindingGroup;
041import org.jdesktop.beansbinding.Bindings;
042import org.jdesktop.beansbinding.ELProperty;
043import org.openide.util.lookup.ServiceProvider;
044
045/**
046 *
047 * @author Randall Wood
048 */
049@ServiceProvider(service = PreferencesPanel.class)
050public class FrameServletPreferencesPanel extends JPanel implements ListDataListener, PreferencesPanel {
051
052    /**
053     * Creates new form FrameServletPreferencesPanel
054     */
055    public FrameServletPreferencesPanel() {
056        initComponents();
057    }
058
059    /**
060     * This method is called from within the constructor to initialize the form.
061     * WARNING: Do NOT modify this code. The content of this method is always
062     * regenerated by the Form Editor.
063     */
064    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
065    private void initComponents() {
066        bindingGroup = new BindingGroup();
067
068        framesEnabled = new ButtonGroup();
069        preferences = InstanceManager.getDefault(WebServerPreferences.class);
070        enableFramesPnl = new JPanel();
071        enableFrames = new JRadioButton();
072        clickDelay = new JSpinner();
073        refreshDelay = new JSpinner();
074        useAjax = new JCheckBox();
075        refreshDelayLbl = new JLabel();
076        clickDelayLbl = new JLabel();
077        disallowedFramesLbl = new JLabel();
078        jScrollPane1 = new JScrollPane();
079        disallowedFrames = new EditableList<String>();
080        disableFramesPnl = new JPanel();
081        disableFrames = new JRadioButton();
082        redirectToPanels = new JCheckBox();
083
084        framesEnabled.add(enableFrames);
085        framesEnabled.add(disableFrames);
086
087        preferences.addPropertyChangeListener(new PropertyChangeListener() {
088            public void propertyChange(PropertyChangeEvent evt) {
089                preferencesPropertyChange(evt);
090            }
091        });
092
093        enableFrames.setText(Bundle.getMessage("LabelEnableFrames")); // NOI18N
094
095        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, preferences, ELProperty.create("${!disableFrames}"), enableFrames, BeanProperty.create("selected")));
096
097        clickDelay.setModel(new SpinnerNumberModel(1, 0, 999, 1));
098        clickDelay.setToolTipText(Bundle.getMessage("ToolTipClickDelay")); // NOI18N
099
100        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, preferences, ELProperty.create("${clickDelay}"), clickDelay, BeanProperty.create("value")));
101        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, enableFrames, ELProperty.create("${selected}"), clickDelay, BeanProperty.create("enabled")));
102
103        refreshDelay.setModel(new SpinnerNumberModel(5, 0, 999, 1));
104        refreshDelay.setToolTipText(Bundle.getMessage("ToolTipRefreshDelay")); // NOI18N
105
106        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, preferences, ELProperty.create("${refreshDelay}"), refreshDelay, BeanProperty.create("value")));
107        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, enableFrames, ELProperty.create("${selected}"), refreshDelay, BeanProperty.create("enabled")));
108
109        useAjax.setText(Bundle.getMessage("LabelUseAjax")); // NOI18N
110        useAjax.setToolTipText(Bundle.getMessage("ToolTipUseAjax")); // NOI18N
111
112        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, preferences, ELProperty.create("${useAjax}"), useAjax, BeanProperty.create("selected")));
113        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, enableFrames, ELProperty.create("${selected}"), useAjax, BeanProperty.create("enabled")));
114
115        refreshDelayLbl.setText(Bundle.getMessage("LabelRefreshDelay")); // NOI18N
116        refreshDelayLbl.setToolTipText(Bundle.getMessage("ToolTipRefreshDelay")); // NOI18N
117
118        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, enableFrames, ELProperty.create("${selected}"), refreshDelayLbl, BeanProperty.create("enabled")));
119
120        clickDelayLbl.setText(Bundle.getMessage("LabelClickDelay")); // NOI18N
121        clickDelayLbl.setToolTipText(Bundle.getMessage("ToolTipClickDelay")); // NOI18N
122
123        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, enableFrames, ELProperty.create("${selected}"), clickDelayLbl, BeanProperty.create("enabled")));
124
125        disallowedFramesLbl.setText(Bundle.getMessage("LabelDisallowedFrames")); // NOI18N
126        disallowedFramesLbl.setToolTipText(Bundle.getMessage("ToolTipDisallowedFrames")); // NOI18N
127
128        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, enableFrames, ELProperty.create("${selected}"), disallowedFramesLbl, BeanProperty.create("enabled")));
129
130        disallowedFrames.setModel(new DefaultEditableListModel<String>());
131        disallowedFrames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
132        disallowedFrames.setToolTipText(Bundle.getMessage("ToolTipDisallowedFrames")); // NOI18N
133        disallowedFrames.setListCellEditor(new DefaultListCellEditor<>(new JTextField()));
134
135        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, enableFrames, ELProperty.create("${selected}"), disallowedFrames, BeanProperty.create("enabled")));
136
137        DefaultEditableListModel<String> model = (DefaultEditableListModel<String>) this.disallowedFrames.getModel();
138        model.addListDataListener(this);
139        for (String frame : this.preferences.getDisallowedFrames()) {
140            model.addElement(frame);
141        }
142        model.addElement(" ");
143        jScrollPane1.setViewportView(disallowedFrames);
144
145        GroupLayout enableFramesPnlLayout = new GroupLayout(enableFramesPnl);
146        enableFramesPnl.setLayout(enableFramesPnlLayout);
147        enableFramesPnlLayout.setHorizontalGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
148            .addGroup(enableFramesPnlLayout.createSequentialGroup()
149                .addContainerGap()
150                .addGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
151                    .addGroup(enableFramesPnlLayout.createSequentialGroup()
152                        .addGap(29, 29, 29)
153                        .addGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
154                            .addGroup(enableFramesPnlLayout.createSequentialGroup()
155                                .addComponent(clickDelay, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
156                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
157                                .addComponent(clickDelayLbl, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE))
158                            .addGroup(enableFramesPnlLayout.createSequentialGroup()
159                                .addComponent(refreshDelay, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
160                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
161                                .addComponent(refreshDelayLbl, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE))
162                            .addComponent(useAjax, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE))
163                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
164                        .addGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
165                            .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
166                            .addComponent(disallowedFramesLbl, GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)))
167                    .addComponent(enableFrames, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
168                .addContainerGap())
169        );
170        enableFramesPnlLayout.setVerticalGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
171            .addGroup(enableFramesPnlLayout.createSequentialGroup()
172                .addContainerGap()
173                .addComponent(enableFrames)
174                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
175                .addGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
176                    .addComponent(clickDelay, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
177                    .addComponent(clickDelayLbl)
178                    .addComponent(disallowedFramesLbl))
179                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
180                .addGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
181                    .addGroup(enableFramesPnlLayout.createSequentialGroup()
182                        .addGroup(enableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
183                            .addComponent(refreshDelay, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
184                            .addComponent(refreshDelayLbl))
185                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
186                        .addComponent(useAjax)
187                        .addContainerGap(108, Short.MAX_VALUE))
188                    .addComponent(jScrollPane1)))
189        );
190
191        disableFrames.setText(Bundle.getMessage("LabelDisableFrames")); // NOI18N
192        disableFrames.setToolTipText(Bundle.getMessage("ToolTipDisableFrames")); // NOI18N
193
194        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, preferences, ELProperty.create("${disableFrames}"), disableFrames, BeanProperty.create("selected")));
195
196        redirectToPanels.setText(Bundle.getMessage("LabelRedirectFramesToPanels")); // NOI18N
197        redirectToPanels.setToolTipText(Bundle.getMessage("ToolTipRedirectFramesToPanels")); // NOI18N
198
199        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, preferences, ELProperty.create("${redirectFramesToPanels}"), redirectToPanels, BeanProperty.create("selected")));
200        bindingGroup.addBinding(Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, disableFrames, ELProperty.create("${selected}"), redirectToPanels, BeanProperty.create("enabled")));
201
202        GroupLayout disableFramesPnlLayout = new GroupLayout(disableFramesPnl);
203        disableFramesPnl.setLayout(disableFramesPnlLayout);
204        disableFramesPnlLayout.setHorizontalGroup(disableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
205            .addGroup(disableFramesPnlLayout.createSequentialGroup()
206                .addGroup(disableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
207                    .addGroup(disableFramesPnlLayout.createSequentialGroup()
208                        .addContainerGap()
209                        .addComponent(disableFrames, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
210                    .addGroup(disableFramesPnlLayout.createSequentialGroup()
211                        .addGap(35, 35, 35)
212                        .addComponent(redirectToPanels, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
213                .addContainerGap())
214        );
215        disableFramesPnlLayout.setVerticalGroup(disableFramesPnlLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
216            .addGroup(disableFramesPnlLayout.createSequentialGroup()
217                .addContainerGap()
218                .addComponent(disableFrames, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
219                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
220                .addComponent(redirectToPanels)
221                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
222        );
223
224        GroupLayout layout = new GroupLayout(this);
225        this.setLayout(layout);
226        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
227            .addComponent(enableFramesPnl, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
228            .addComponent(disableFramesPnl, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
229        );
230        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
231            .addGroup(layout.createSequentialGroup()
232                .addComponent(enableFramesPnl, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
233                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
234                .addComponent(disableFramesPnl, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
235        );
236
237        bindingGroup.bind();
238    }// </editor-fold>//GEN-END:initComponents
239
240    private void preferencesPropertyChange(PropertyChangeEvent evt) {//GEN-FIRST:event_preferencesPropertyChange
241        if (evt.getPropertyName().equals(WebServerPreferences.DISALLOWED_FRAMES)) {
242            DefaultEditableListModel<String> model = (DefaultEditableListModel<String>) disallowedFrames.getModel();
243            model.removeListDataListener(this);
244            model.removeAllElements();
245            for (String frame : preferences.getDisallowedFrames()) {
246                model.addElement(frame);
247            }
248            model.addElement(" ");
249            model.addListDataListener(this);
250        }
251    }//GEN-LAST:event_preferencesPropertyChange
252
253
254    // Variables declaration - do not modify//GEN-BEGIN:variables
255    private JSpinner clickDelay;
256    private JLabel clickDelayLbl;
257    private JRadioButton disableFrames;
258    private JPanel disableFramesPnl;
259    private EditableList<String> disallowedFrames;
260    private JLabel disallowedFramesLbl;
261    private JRadioButton enableFrames;
262    private JPanel enableFramesPnl;
263    private ButtonGroup framesEnabled;
264    private JScrollPane jScrollPane1;
265    private WebServerPreferences preferences;
266    private JCheckBox redirectToPanels;
267    private JSpinner refreshDelay;
268    private JLabel refreshDelayLbl;
269    private JCheckBox useAjax;
270    private BindingGroup bindingGroup;
271    // End of variables declaration//GEN-END:variables
272
273    @Override
274    public String getPreferencesItem() {
275        return Bundle.getMessage("PreferencesItem");
276    }
277
278    @Override
279    public String getPreferencesItemText() {
280        return Bundle.getMessage("PreferencesItemTitle");
281    }
282
283    @Override
284    public String getTabbedPreferencesTitle() {
285        return Bundle.getMessage("TitleDelayPanel");
286    }
287
288    @Override
289    public String getLabelKey() {
290        return null;
291    }
292
293    @Override
294    public JComponent getPreferencesComponent() {
295        return this;
296    }
297
298    @Override
299    public boolean isPersistant() {
300        return false;
301    }
302
303    @Override
304    public String getPreferencesTooltip() {
305        return null;
306    }
307
308    @Override
309    public void savePreferences() {
310        if (this.isDirty()) {
311            this.preferences.save();
312        }
313    }
314
315    @Override
316    public boolean isDirty() {
317        return this.preferences.isDirty();
318    }
319
320    @Override
321    public boolean isRestartRequired() {
322        return this.preferences.isRestartRequired();
323    }
324
325    @Override
326    public boolean isPreferencesValid() {
327        return true; // no validity checking performed
328
329    }
330
331    @Override
332    public int getSortOrder() {
333        return 1110;
334    }
335
336    @Override
337    public void intervalAdded(ListDataEvent e) {
338        // do nothing
339    }
340
341    @Override
342    public void intervalRemoved(ListDataEvent e) {
343        // do nothing
344    }
345
346    @Override
347    public void contentsChanged(ListDataEvent e) {
348        DefaultEditableListModel<String> model = (DefaultEditableListModel<String>) disallowedFrames.getModel();
349        if (!model.getElementAt(model.getSize() - 1).equals(" ")) {
350            model.addElement(" ");
351        } else if (model.getElementAt(e.getIndex0()).isEmpty()) {
352            model.removeElementAt(e.getIndex0());
353        }
354        ArrayList<String> frames = new ArrayList<>();
355        Enumeration<String> elements = model.elements();
356        while (elements.hasMoreElements()) {
357            String frame = elements.nextElement().trim();
358            if (!frame.isEmpty()) {
359                frames.add(frame);
360            }
361        }
362        this.preferences.setDisallowedFrames(frames.toArray(new String[frames.size()]));
363    }
364}