View Javadoc

1   package org.lsst.ccs.plugin.jas3.rest;
2   
3   import javax.swing.event.DocumentEvent;
4   import javax.swing.event.DocumentListener;
5   
6   /**
7    * A preferences panel for allowing a user to control which rest server/port to 
8    * connect to.
9    * @author tonyj
10   */
11  class RestServerPreferencesPanel extends javax.swing.JPanel {
12      private final RestPreferences prefs;
13  
14      /**
15       * Creates new form RestServerPreferencesPanel
16       */
17      RestServerPreferencesPanel(RestPreferences prefs) {
18          this.prefs = prefs;
19          initComponents();
20          updateURL();
21          final DocumentListener documentListener = new DocumentListener() {
22              @Override
23              public void insertUpdate(DocumentEvent e) {
24                  updateURL();
25              }
26  
27              @Override
28              public void removeUpdate(DocumentEvent e) {
29                  updateURL();
30              }
31  
32              @Override
33              public void changedUpdate(DocumentEvent e) {
34                  updateURL();
35              }
36          };
37          serverTextField.getDocument().addDocumentListener(documentListener);  
38      }
39      /**
40       * Write any changes back into the preferences
41       */
42      void apply() {
43          prefs.setRestPort(((Number) portSpinner.getValue()).intValue());
44          prefs.setRestServer(serverTextField.getText());
45          prefs.setConnectOnStartup(connectOnStartupCheckBox.isSelected());
46          prefs.firePropertyChanged();
47      }
48  
49      private void updateURL() {
50          urlTextField.setText(prefs.getURLForServerAndPort(serverTextField.getText(), ((Number) portSpinner.getValue()).intValue()));
51      }
52      
53      /**
54       * This method is called from within the constructor to initialize the form.
55       * WARNING: Do NOT modify this code. The content of this method is always
56       * regenerated by the Form Editor.
57       */
58      @SuppressWarnings("unchecked")
59      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
60      private void initComponents() {
61  
62          javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
63          serverTextField = new javax.swing.JTextField();
64          javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
65          portSpinner = new javax.swing.JSpinner();
66          javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
67          urlTextField = new javax.swing.JTextField();
68          connectOnStartupCheckBox = new javax.swing.JCheckBox();
69  
70          jLabel1.setText("Server:");
71  
72          serverTextField.setText(prefs.getRestServer());
73  
74          jLabel2.setText("Port:");
75  
76          portSpinner.setModel(new javax.swing.SpinnerNumberModel(prefs.getRestPort(),1,65536,1));
77          portSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
78              public void stateChanged(javax.swing.event.ChangeEvent evt) {
79                  portSpinnerStateChanged(evt);
80              }
81          });
82  
83          jLabel3.setText("URL:");
84  
85          urlTextField.setEnabled(false);
86  
87          connectOnStartupCheckBox.setSelected(prefs.isConnectOnStartup());
88          connectOnStartupCheckBox.setText("connect on startup");
89  
90          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
91          this.setLayout(layout);
92          layout.setHorizontalGroup(
93              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
94              .addGroup(layout.createSequentialGroup()
95                  .addContainerGap()
96                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
97                      .addGroup(layout.createSequentialGroup()
98                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
99                              .addComponent(jLabel3)
100                             .addComponent(jLabel1)
101                             .addComponent(jLabel2))
102                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
103                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
104                             .addComponent(portSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
105                             .addComponent(serverTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
106                             .addComponent(urlTextField)))
107                     .addComponent(connectOnStartupCheckBox))
108                 .addContainerGap())
109         );
110         layout.setVerticalGroup(
111             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
112             .addGroup(layout.createSequentialGroup()
113                 .addContainerGap()
114                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
115                     .addComponent(jLabel1)
116                     .addComponent(serverTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
117                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
118                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
119                     .addComponent(jLabel2)
120                     .addComponent(portSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
121                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
122                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
123                     .addComponent(jLabel3)
124                     .addComponent(urlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
125                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
126                 .addComponent(connectOnStartupCheckBox)
127                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
128         );
129     }// </editor-fold>//GEN-END:initComponents
130 
131     private void portSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_portSpinnerStateChanged
132         updateURL();
133     }//GEN-LAST:event_portSpinnerStateChanged
134 
135     // Variables declaration - do not modify//GEN-BEGIN:variables
136     private javax.swing.JCheckBox connectOnStartupCheckBox;
137     private javax.swing.JSpinner portSpinner;
138     private javax.swing.JTextField serverTextField;
139     private javax.swing.JTextField urlTextField;
140     // End of variables declaration//GEN-END:variables
141 
142 }