]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | import javax.swing.JTable; |
2 | import javax.swing.ListSelectionModel; | |
3 | import javax.swing.event.ListSelectionListener; | |
4 | import javax.swing.event.ListSelectionEvent; | |
5 | import javax.swing.JScrollPane; | |
6 | import javax.swing.JPanel; | |
7 | import javax.swing.JFrame; | |
8 | import javax.swing.*; | |
9 | import java.awt.*; | |
10 | import java.awt.event.*; | |
11 | import com.easysw.cups.*; | |
12 | ||
13 | public class GLPsearchProgressPanel | |
14 | { | |
15 | private JProgressBar progressBar; | |
16 | private Timer timer; | |
17 | private JButton searchButton; | |
18 | private JLabel progressLabel; | |
19 | private GLPsearch tasks[]; | |
20 | private JPanel panel = null; | |
21 | ||
22 | public GLPsearchProgressPanel() | |
23 | { | |
24 | //Create the demo's UI. | |
25 | searchButton = new JButton("Search"); | |
26 | searchButton.setActionCommand("Search"); | |
27 | searchButton.addActionListener(new ButtonListener()); | |
28 | ||
29 | progressLabel = new JLabel("Search your local subnet for CUPS servers"); | |
30 | progressLabel.setBackground(GLPcolors.backgroundColor); | |
31 | progressLabel.setForeground(GLPcolors.foregroundColor); | |
32 | ||
33 | progressBar = new JProgressBar(0, 254); | |
34 | progressBar.setValue(0); | |
35 | progressBar.setBorderPainted(true); | |
36 | progressBar.setOrientation(JProgressBar.HORIZONTAL); | |
37 | progressBar.setBackground(GLPcolors.backgroundColor); | |
38 | progressBar.setForeground( Color.blue ); | |
39 | progressBar.setStringPainted(true); | |
40 | ||
41 | panel = new JPanel(); | |
42 | panel.setLayout(new BorderLayout()); | |
43 | panel.setBackground(GLPcolors.backgroundColor); | |
44 | ||
45 | panel.add(progressLabel,BorderLayout.NORTH); | |
46 | panel.add(progressBar, BorderLayout.CENTER); | |
47 | panel.add(searchButton, BorderLayout.EAST); | |
48 | ||
49 | //Create a timer. | |
50 | timer = new Timer(300, new ActionListener() | |
51 | { | |
52 | ||
53 | public void actionPerformed(ActionEvent evt) | |
54 | { | |
55 | int n = 0; | |
56 | for (int i=0; i < 8; i++) | |
57 | { | |
58 | if (tasks[i] != null) | |
59 | n += tasks[i].getValue(); | |
60 | } | |
61 | progressBar.setValue(n); | |
62 | ||
63 | ||
64 | // | |
65 | // See if all the threads completed yet. | |
66 | // | |
67 | int d = 0; | |
68 | for (int j=0; j < 8; j++ ) | |
69 | { | |
70 | if (tasks[j] != null) | |
71 | { | |
72 | if (tasks[j].done()) | |
73 | { | |
74 | d++; | |
75 | } | |
76 | } | |
77 | else d++; // Thread removed ??? | |
78 | } | |
79 | ||
80 | if (d >= 8) | |
81 | { | |
82 | timer.stop(); | |
83 | progressBar.setValue(progressBar.getMinimum()); | |
84 | searchButton.setActionCommand("Search"); | |
85 | searchButton.setText("Search"); | |
86 | progressLabel.setText("Search local subnet for CUPS servers"); | |
87 | ||
88 | String[] servers = GLPvars.getServerList(); | |
89 | if ((servers != null) && (servers.length > 0)) | |
90 | { | |
91 | GLPvars.searchTM = new GLPjobTableModel(servers.length,1); | |
92 | GLPvars.searchTM.setColumnName(0,"Search Results"); | |
93 | for (int i=0; i < servers.length; i++) | |
94 | GLPvars.searchTM.setValueAt(servers[i],i,0); | |
95 | GLPvars.searchTable = new JTable(GLPvars.searchTM); | |
96 | ||
97 | ||
98 | GLPvars.searchTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); | |
99 | ListSelectionModel rowSM = GLPvars.searchTable.getSelectionModel(); | |
100 | rowSM.addListSelectionListener(new ListSelectionListener() | |
101 | { | |
102 | public void valueChanged(ListSelectionEvent e) | |
103 | { | |
104 | //Ignore extra messages. | |
105 | if (e.getValueIsAdjusting()) return; | |
106 | ||
107 | ListSelectionModel lsm = | |
108 | (ListSelectionModel)e.getSource(); | |
109 | if (lsm.isSelectionEmpty()) | |
110 | { | |
111 | //no rows are selected | |
112 | } | |
113 | else | |
114 | { | |
115 | int selectedRow = lsm.getMinSelectionIndex(); | |
116 | String newServer = (String)GLPvars.searchTM.getValueAt(selectedRow,0); | |
117 | GLPvars.setServerName(newServer); | |
118 | GLPvars.tabs.updateServerPanel(GLPvars.cupsServerName); | |
119 | } | |
120 | } | |
121 | }); | |
122 | GLPvars.tabs.updateServerPanel(GLPvars.cupsServerName); | |
123 | } | |
124 | // DEBUG | |
125 | ||
126 | } // threads complete? | |
127 | ||
128 | ||
129 | ||
130 | } | |
131 | }); | |
132 | } | |
133 | ||
134 | ||
135 | ||
136 | ||
137 | /** | |
138 | * The actionPerformed method in this class | |
139 | * is called when the user presses the start button. | |
140 | */ | |
141 | class ButtonListener implements ActionListener | |
142 | { | |
143 | ||
144 | public void actionPerformed(ActionEvent evt) | |
145 | { | |
146 | ||
147 | if (evt.getActionCommand().equals("Search")) | |
148 | { | |
149 | progressLabel.setText("Searching ....."); | |
150 | // | |
151 | // Create the search threads .... | |
152 | // | |
153 | tasks = new GLPsearch[8]; | |
154 | for (int i=0; i < 8; i++) | |
155 | tasks[i] = new GLPsearch(i+1); | |
156 | ||
157 | searchButton.setActionCommand("Stop"); | |
158 | searchButton.setText("Stop"); | |
159 | for (int i=0; i < 8; i++) | |
160 | { | |
161 | if (tasks[i] != null) | |
162 | { | |
163 | tasks[i].start(); | |
164 | } | |
165 | } | |
166 | timer.start(); | |
167 | } | |
168 | else if (evt.getActionCommand().equals("Stop")) | |
169 | { | |
170 | progressLabel.setText("Search local subnet for CUPS servers"); | |
171 | ||
172 | for (int i=0; i < 8; i++) | |
173 | { | |
174 | if (tasks[i] != null) | |
175 | { | |
176 | tasks[i].interrupt(); | |
177 | } | |
178 | // tasks[i] = null; | |
179 | } | |
180 | ||
181 | searchButton.setActionCommand("Search"); | |
182 | searchButton.setText("Search"); | |
183 | ||
184 | } // Stop event | |
185 | ||
186 | } // actionPerformed | |
187 | } // end of class | |
188 | ||
189 | public JPanel getPanel() | |
190 | { | |
191 | return(panel); | |
192 | } | |
193 | ||
194 | ||
195 | } |