File: GLPsearchProgressPanel.java

package info (click to toggle)
cupsys 1.2.7-4%2Betch9
  • links: PTS
  • area: main
  • in suites: etch
  • size: 20,436 kB
  • ctags: 10,404
  • sloc: ansic: 97,130; cpp: 49,167; java: 6,199; sh: 4,723; makefile: 1,918; lisp: 232; perl: 145; python: 119; php: 28
file content (195 lines) | stat: -rw-r--r-- 6,283 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.easysw.cups.*;

public class GLPsearchProgressPanel 
{
    private JProgressBar progressBar;
    private Timer timer;
    private JButton searchButton;
    private JLabel  progressLabel;
    private GLPsearch  tasks[];
    private JPanel     panel = null;

    public GLPsearchProgressPanel() 
    {
        //Create the demo's UI.
        searchButton = new JButton("Search");
        searchButton.setActionCommand("Search");
        searchButton.addActionListener(new ButtonListener());

        progressLabel = new JLabel("Search your local subnet for CUPS servers");
        progressLabel.setBackground(GLPcolors.backgroundColor);
        progressLabel.setForeground(GLPcolors.foregroundColor);

        progressBar = new JProgressBar(0, 254);
        progressBar.setValue(0);
        progressBar.setBorderPainted(true);
        progressBar.setOrientation(JProgressBar.HORIZONTAL);
        progressBar.setBackground(GLPcolors.backgroundColor);
        progressBar.setForeground( Color.blue );
        progressBar.setStringPainted(true);

        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(GLPcolors.backgroundColor);

        panel.add(progressLabel,BorderLayout.NORTH);
        panel.add(progressBar,  BorderLayout.CENTER);
        panel.add(searchButton, BorderLayout.EAST);

        //Create a timer.
        timer = new Timer(300, new ActionListener() 
        {

            public void actionPerformed(ActionEvent evt) 
            {
                int n = 0;
                for (int i=0; i < 8; i++)
                {
                  if (tasks[i] != null)
                    n += tasks[i].getValue();
                }
                progressBar.setValue(n);


                //
                //  See if all the threads completed yet.
                //
                int d = 0;
                for (int j=0; j < 8; j++ ) 
                {
                  if (tasks[j] != null)
                  {
                    if (tasks[j].done())
                    {
                      d++;
                    }
                  }
                  else d++;  //  Thread removed ???
                }

                if (d >= 8) 
                {
                    timer.stop();
                    progressBar.setValue(progressBar.getMinimum());
                    searchButton.setActionCommand("Search");
                    searchButton.setText("Search");
                    progressLabel.setText("Search local subnet for CUPS servers");

                    String[] servers = GLPvars.getServerList();
                    if ((servers != null) && (servers.length > 0))
                    {
                      GLPvars.searchTM = new GLPjobTableModel(servers.length,1);
                      GLPvars.searchTM.setColumnName(0,"Search Results");
                      for (int i=0; i < servers.length; i++)
                        GLPvars.searchTM.setValueAt(servers[i],i,0);
                      GLPvars.searchTable = new JTable(GLPvars.searchTM);


                      GLPvars.searchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                      ListSelectionModel rowSM = GLPvars.searchTable.getSelectionModel();
                      rowSM.addListSelectionListener(new ListSelectionListener()
                      {
                        public void valueChanged(ListSelectionEvent e) 
                        {
                          //Ignore extra messages.
                          if (e.getValueIsAdjusting()) return;
        
                          ListSelectionModel lsm =
                            (ListSelectionModel)e.getSource();
                          if (lsm.isSelectionEmpty()) 
                          {
                            //no rows are selected
                          } 
                          else 
                          {
                            int selectedRow = lsm.getMinSelectionIndex();
                            String newServer = (String)GLPvars.searchTM.getValueAt(selectedRow,0);
                            GLPvars.setServerName(newServer);
                            GLPvars.tabs.updateServerPanel(GLPvars.cupsServerName);
                          }
                        }
                      });
                      GLPvars.tabs.updateServerPanel(GLPvars.cupsServerName);
                    }
                    //  DEBUG

                } //  threads complete?



            }
        });
    }




    /**
     * The actionPerformed method in this class
     * is called when the user presses the start button.
     */
    class ButtonListener implements ActionListener 
    {

      public void actionPerformed(ActionEvent evt) 
      {

          if (evt.getActionCommand().equals("Search"))
          {
            progressLabel.setText("Searching .....");
            //
            //  Create the search threads .... 
            //
            tasks = new GLPsearch[8];
            for (int i=0; i < 8; i++)
              tasks[i] = new GLPsearch(i+1);

            searchButton.setActionCommand("Stop");
            searchButton.setText("Stop");
            for (int i=0; i < 8; i++)
            {
              if (tasks[i] != null)
              {
                tasks[i].start();
              }
            }
            timer.start();
          }
          else if (evt.getActionCommand().equals("Stop"))
          {
            progressLabel.setText("Search local subnet for CUPS servers");

            for (int i=0; i < 8; i++)
            {
              if (tasks[i] != null)
              {
                tasks[i].interrupt();
              }
              // tasks[i] = null;
            }

            searchButton.setActionCommand("Search");
            searchButton.setText("Search");

          }  // Stop event

      }  //  actionPerformed
    }  // end of class

    public JPanel getPanel()
    {
      return(panel);
    }
    

}