KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > p2p > peerconfiguration > RowModification


1 package org.objectweb.proactive.p2p.peerconfiguration;
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.GregorianCalendar JavaDoc;
6
7 public class RowModification {
8
9     protected PeerSetupGUI panel;
10     protected int row;
11     
12     protected JFrame frame = null;
13     protected JCheckBox sundayCheck = new JCheckBox("Sunday");
14     protected JCheckBox mondayCheck = new JCheckBox("Monday");
15     protected JCheckBox tuesdayCheck = new JCheckBox("Tuesday");
16     protected JCheckBox wednesdayCheck = new JCheckBox("Wednesday");
17     protected JCheckBox thursdayCheck = new JCheckBox("Thursday");
18     protected JCheckBox fridayCheck = new JCheckBox("Friday");
19     protected JCheckBox saturdayCheck = new JCheckBox("Saturday");
20     protected JSpinner spinnerStartHour = null;
21     protected JSpinner spinnerStartMinutes = null;
22     protected JSpinner spinnerStopHour = null;
23     protected JSpinner spinnerStopMinutes = null;
24     protected JSlider slider = new JSlider();
25     protected JCheckBox permanentCheck = new JCheckBox("Permanent");
26     protected SpinnerSet spdStop = new SpinnerSet(true);
27     protected SpinnerSet spdStart = new SpinnerSet(true);
28
29     public RowModification(JFrame frame, PeerSetupGUI panel, int row) {
30         this.frame=frame;
31         this.panel = panel;
32         this.row=row;
33         }
34
35     public Component createComponents() {
36
37         //The main panel.
38
JPanel pane = new JPanel();
39         pane.setBorder(BorderFactory.createEmptyBorder(
40                                                        30,
41                                                        30,
42                                                        10,
43                                                        30)
44         );
45         pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
46
47         //Save button.
48
JButton buttonS = new JButton("Save");
49         buttonS.setMnemonic('s');
50         buttonS.addActionListener(new ActionListener() {
51             public void actionPerformed(ActionEvent e) {
52                       Integer JavaDoc appInt1 = null;
53                       Integer JavaDoc appInt2 = null;
54                       Integer JavaDoc appInt3 = null;
55                       Integer JavaDoc appInt4 = null;
56                       Integer JavaDoc appInt5 = null;
57                       Integer JavaDoc appInt6 = null;
58                    //Control #1: minimum one day of te week selected
59
if (!(mondayCheck.isSelected())&&
60                        !(tuesdayCheck.isSelected())&&
61                        !(wednesdayCheck.isSelected())&&
62                        !(thursdayCheck.isSelected())&&
63                        !(fridayCheck.isSelected())&&
64                        !(saturdayCheck.isSelected())&&
65                        !(sundayCheck.isSelected())
66                       )
67                    {
68                     //Error message
69
JOptionPane.showConfirmDialog(frame,
70                                                   "Please select at least one day",
71                                                   "Input error",
72                                                   JOptionPane.DEFAULT_OPTION,
73                                                   JOptionPane.ERROR_MESSAGE);
74                     return;
75                    }
76
77 // //Control #2: stop-time < stop-time
78
Integer JavaDoc startHour = new Integer JavaDoc((String JavaDoc) spinnerStartHour.getValue());
79                    Integer JavaDoc stopHour = new Integer JavaDoc((String JavaDoc) spinnerStopHour.getValue());
80                    Integer JavaDoc startMinutes = new Integer JavaDoc((String JavaDoc) spinnerStartMinutes.getValue());
81                    Integer JavaDoc stopMinutes = new Integer JavaDoc((String JavaDoc) spinnerStopMinutes.getValue());
82 //
83
// if (startHour.intValue() > stopHour.intValue()) {
84
// JOptionPane.showConfirmDialog(frame,
85
// "Start hour must be <= Stop hour",
86
// "Input error",
87
// JOptionPane.DEFAULT_OPTION,
88
// JOptionPane.ERROR_MESSAGE);
89
// return;
90
// }
91
// if (startHour.intValue() == stopHour.intValue()) {
92
// if (startMinutes.intValue() >= stopMinutes.intValue()) {
93
// JOptionPane.showConfirmDialog(frame,
94
// "In this case, Start minutes must be < Stop minutes",
95
// "Input error",
96
// JOptionPane.DEFAULT_OPTION,
97
// JOptionPane.ERROR_MESSAGE);
98
// return;
99
// }
100
// }
101
if (!(permanentCheck.isSelected())) {
102                       JSpinner[] stopSpinners = spdStop.getSpinners();
103                       JSpinner[] startSpinners = spdStart.getSpinners();
104                       appInt1 = new Integer JavaDoc((String JavaDoc) stopSpinners[0].getValue());
105                       appInt2 = new Integer JavaDoc((String JavaDoc) stopSpinners[1].getValue());
106                       appInt3 = new Integer JavaDoc(((Integer JavaDoc) stopSpinners[2].getValue()).intValue());
107                       appInt4 = new Integer JavaDoc((String JavaDoc) startSpinners[0].getValue());
108                       appInt5 = new Integer JavaDoc((String JavaDoc) startSpinners[1].getValue());
109                       appInt6 = new Integer JavaDoc(((Integer JavaDoc) startSpinners[2].getValue()).intValue());
110
111                       //Control #3: validity stop and stop dates
112
if (
113                           !(checkDate(0,0,appInt1.intValue(), appInt2.intValue(), appInt3.intValue(), 0))||
114                           !(checkDate(0,0,appInt4.intValue(), appInt5.intValue(), appInt6.intValue(), 0))
115                          ) {
116                     JOptionPane.showConfirmDialog(frame,
117                                                   "One or more dates are not correct",
118                                                   "Input error",
119                                                   JOptionPane.DEFAULT_OPTION,
120                                                   JOptionPane.ERROR_MESSAGE);
121                     return;
122                       }
123
124 // //Control #4: start date <= stop date
125
GregorianCalendar JavaDoc stopGC = new GregorianCalendar JavaDoc(appInt3.intValue(), appInt2.intValue(), appInt1.intValue());
126                         GregorianCalendar JavaDoc startGC = new GregorianCalendar JavaDoc( appInt6.intValue(), appInt5.intValue(), appInt4.intValue());
127                       if (!(startGC.before(stopGC))) {
128                     JOptionPane.showConfirmDialog(frame,
129                                                   "Start date must be before Stop date",
130                                                   "Input error",
131                                                   JOptionPane.DEFAULT_OPTION,
132                                                   JOptionPane.ERROR_MESSAGE);
133                     return;
134                       }
135
136                    }
137
138                    String JavaDoc recordString = "";
139                     if (sundayCheck.isSelected()) {
140                        recordString = recordString + "Sunday ";
141                     }
142                    if (mondayCheck.isSelected()) {
143                       recordString = recordString + "Monday ";
144                    }
145                    if (tuesdayCheck.isSelected()) {
146                       recordString = recordString + "Tuesday ";
147                    }
148                    if (wednesdayCheck.isSelected()) {
149                       recordString = recordString + "Wednesday ";
150                    }
151                    if (thursdayCheck.isSelected()) {
152                       recordString = recordString + "Thursday ";
153                    }
154                    if (fridayCheck.isSelected()) {
155                       recordString = recordString + "Friday ";
156                    }
157                    if (saturdayCheck.isSelected()) {
158                       recordString = recordString + "Saturday";
159                    }
160
161                 String JavaDoc date = recordString;
162                 String JavaDoc validity="";
163                  if (permanentCheck.isSelected()) {
164                                                      validity = "Permanent";
165                    } else {
166                            validity = validity + appInt4.intValue() + "/" + appInt5.intValue() + "/" + appInt6.intValue() + " - ";
167                            validity = validity + appInt1.intValue() + "/" + appInt2.intValue() + "/" + appInt3.intValue();
168                    }
169
170                /*******************************************************
171                  At this point, if the user confirm, we must add or update the record in the xml file
172                  and update the table in the configuration frame
173                */

174
175                 String JavaDoc start = startHour.intValue() + ":" + startMinutes.intValue();
176                 String JavaDoc stop = stopHour.intValue() + ":" + stopMinutes.intValue();
177                 String JavaDoc load = slider.getValue() + "%";
178                 
179                 panel.table.setValueAt(date,row,0);
180                 panel.table.setValueAt(start,row,1);
181                 panel.table.setValueAt(stop,row,2);
182                 panel.table.setValueAt(load,row,3);
183                 panel.table.setValueAt(validity,row,4);
184                 
185                 panel.table.repaint();
186             }
187             });
188
189
190         //Quit button.
191
JButton buttonQ = new JButton("Quit");
192         buttonQ.setMnemonic('q');
193         buttonQ.addActionListener(new ActionListener() {
194             public void actionPerformed(ActionEvent e) {
195                 frame.dispose();
196             }
197         });
198
199         //The button panel.
200
JPanel buttonPanel = new JPanel();
201         buttonPanel.setBorder(BorderFactory.createEmptyBorder(
202                                                        30,
203                                                        30,
204                                                        10,
205                                                        30)
206         );
207         buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
208         buttonPanel.add(buttonS);
209         buttonPanel.add(buttonQ);
210
211         //The days panel.
212
JPanel daysPanel = new JPanel();
213         daysPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
214         daysPanel.setLayout(new BoxLayout(daysPanel, BoxLayout.Y_AXIS));
215         daysPanel.setBorder(BorderFactory.createTitledBorder("Days"));
216         daysPanel.add(mondayCheck);
217         daysPanel.add(tuesdayCheck);
218         daysPanel.add(wednesdayCheck);
219         daysPanel.add(thursdayCheck);
220         daysPanel.add(fridayCheck);
221         daysPanel.add(saturdayCheck);
222         daysPanel.add(sundayCheck);
223         JButton allDays = new JButton("All days");
224         daysPanel.add(allDays);
225         JCheckBox[] cbx = {mondayCheck, tuesdayCheck, wednesdayCheck, thursdayCheck, fridayCheck, saturdayCheck, sundayCheck};
226         DaysCheckCangeListener l = new DaysCheckCangeListener(cbx);
227         allDays.addChangeListener(l);
228
229         //Time panels
230
String JavaDoc[] hour = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20", "21","22","23","24"};
231         String JavaDoc[] minutes = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
232                             "21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40",
233                     "41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
234         JFormattedTextField ftf = null;
235         JComponent editor = null;
236
237         JPanel intermediatePanel = new JPanel();
238         intermediatePanel.setLayout(new BoxLayout(intermediatePanel, BoxLayout.Y_AXIS));
239         //Beginning time panel.
240
JPanel bTimePanel = new JPanel();
241         bTimePanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
242         bTimePanel.setLayout(new FlowLayout());
243         bTimePanel.setBorder(BorderFactory.createTitledBorder("Start time"));
244         SpinnerModel hourStartModel = new CyclingSpinnerListModel(hour);
245         spinnerStartHour = SpinnerSet.addLabeledSpinner(bTimePanel, "H", hourStartModel);
246         SpinnerModel minutesStartModel = new CyclingSpinnerListModel(minutes);
247         spinnerStartMinutes = SpinnerSet.addLabeledSpinner(bTimePanel, "M", minutesStartModel);
248         ((CyclingSpinnerListModel)minutesStartModel).setLinkedModel(hourStartModel);
249         editor = spinnerStartMinutes.getEditor();
250         ftf = ((JSpinner.DefaultEditor)editor).getTextField();
251         if (ftf != null ) {
252                    ftf.setColumns(2); //specify more width than we need
253
ftf.setHorizontalAlignment(JTextField.RIGHT);
254         }
255         editor = spinnerStartHour.getEditor();
256         ftf = ((JSpinner.DefaultEditor)editor).getTextField();
257         if (ftf != null ) {
258                    ftf.setColumns(2); //specify more width than we need
259
ftf.setHorizontalAlignment(JTextField.RIGHT);
260         }
261
262         //Finish time panel.
263
JPanel fTimePanel = new JPanel();
264         fTimePanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
265         fTimePanel.setLayout(new FlowLayout());
266         fTimePanel.setBorder(BorderFactory.createTitledBorder("Work time"));
267         SpinnerModel hourStopModel = new CyclingSpinnerListModel(hour);
268         spinnerStopHour = SpinnerSet.addLabeledSpinner(fTimePanel, "H", hourStopModel);
269         SpinnerModel minutesStopModel = new CyclingSpinnerListModel(minutes);
270         spinnerStopMinutes = SpinnerSet.addLabeledSpinner(fTimePanel, "M", minutesStopModel);
271         ((CyclingSpinnerListModel)minutesStopModel).setLinkedModel(hourStopModel);
272         editor = spinnerStopMinutes.getEditor();
273         ftf = ((JSpinner.DefaultEditor)editor).getTextField();
274         if (ftf != null ) {
275                    ftf.setColumns(2); //specify more width than we need
276
ftf.setHorizontalAlignment(JTextField.RIGHT);
277         }
278         editor = spinnerStopHour.getEditor();
279         ftf = ((JSpinner.DefaultEditor)editor).getTextField();
280         if (ftf != null ) {
281                    ftf.setColumns(2); //specify more width than we need
282
ftf.setHorizontalAlignment(JTextField.RIGHT);
283         }
284
285         //Max CPU panel.
286
JPanel mCpuPanel = new JPanel();
287         mCpuPanel.setLayout(new FlowLayout());
288         mCpuPanel.setBorder(BorderFactory.createTitledBorder("MAX CPU Load"));
289         slider.setMajorTickSpacing(9);
290         slider.setMinorTickSpacing(3);
291         slider.setMinimum(1);
292         slider.setMaximum(100);
293         slider.setPaintTicks(true);
294         slider.setPaintLabels(true);
295         slider.setOrientation(1);
296         mCpuPanel.add(slider);
297         JTextField currentValueTXT = new JTextField();
298         currentValueTXT.setColumns(3);
299         currentValueTXT.setBorder(BorderFactory.createTitledBorder("Value"));
300         currentValueTXT.setEditable(false);
301         Integer JavaDoc CPULoad = new Integer JavaDoc(slider.getValue());
302         SliderValueUpdate cListener = new SliderValueUpdate(slider, currentValueTXT);
303         slider.addChangeListener(cListener);
304         currentValueTXT.setText(CPULoad.toString() + "%");
305         mCpuPanel.add(currentValueTXT);
306
307         //Validity panel.
308
JPanel validityPanel = new JPanel();
309         validityPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
310         validityPanel.setLayout(new BoxLayout(validityPanel, BoxLayout.X_AXIS));
311         validityPanel.setBorder(BorderFactory.createTitledBorder("Validity"));
312         validityPanel.add(permanentCheck);
313         JPanel startPanel = new JPanel(new BorderLayout());
314         JPanel stopPanel = new JPanel(new BorderLayout());
315         JSpinner[] startSArraY = spdStart.getSpinners();
316         JSpinner[] stopSArraY = spdStop.getSpinners();
317         JSpinner[] spinnerArray = new JSpinner[startSArraY.length * 2];
318         int j=0;
319         while (j<spinnerArray.length) {
320             for (int k=0; k<startSArraY.length; k++) {
321                        spinnerArray[j] = startSArraY[k];
322                        j++;
323             }
324             for (int k=0; k<stopSArraY.length; k++) {
325                        spinnerArray[j] = stopSArraY[k];
326                        j++;
327             }
328         }
329         validityCheckCangeListener vcl = new validityCheckCangeListener(spinnerArray);
330         permanentCheck.addChangeListener(vcl);
331         startPanel.add(spdStart);
332         stopPanel.add(spdStop);
333         startPanel.setBorder(BorderFactory.createTitledBorder("Start"));
334         stopPanel.setBorder(BorderFactory.createTitledBorder("Stop"));
335         validityPanel.add(startPanel);
336         validityPanel.add(stopPanel);
337
338         //The editing panel.
339
JPanel editingPanel = new JPanel();
340         editingPanel.setBorder(BorderFactory.createEmptyBorder(
341                                                        30,
342                                                        30,
343                                                        10,
344                                                        30)
345         );
346         editingPanel.setLayout(new FlowLayout());
347         editingPanel.add(daysPanel);
348         intermediatePanel.add(bTimePanel);
349         intermediatePanel.add(fTimePanel);
350         editingPanel.add(intermediatePanel);
351         editingPanel.add(mCpuPanel);
352         editingPanel.add(validityPanel);
353
354
355         pane.add(editingPanel);
356         pane.add(buttonPanel);
357
358         return pane;
359     }
360
361     public boolean checkDate( int MIN, int HH, int GG, int MM, int AA, int SS) {
362         boolean bis = false;
363
364         bis=((AA % 4)==0);
365         if ((AA % 100)==0)
366          bis=((AA % 400)==0);
367
368             switch (MM) {
369
370                 case 1:
371                 case 3:
372                 case 5:
373                 case 7:
374                 case 8:
375                 case 10:
376                 case 12:
377                  if (!((GG>=1)&&(GG<=31))){
378                      return(false);
379                  }
380                 break;
381
382                 case 2:
383                  if (bis) {
384                   if (!((GG>=1)&&(GG<=29))){
385                      return(false);
386                   }
387                  } else {
388                   if (!((GG>=1)&&(GG<=28))){
389                      return(false);
390                   }
391                  }
392                 break;
393
394                 case 4:
395                 case 6:
396                 case 9:
397                 case 11:
398                  if (!((GG>=1)&&(GG<=30))){
399                      return(false);
400                  }
401                 break;
402
403                 default:
404                  return(false);
405             }
406
407         return(true);
408     }
409
410 }
411
Popular Tags