KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.proactive.p2p.peerconfiguration;
2 import javax.swing.*;
3 import javax.swing.table.*;
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.beans.*;
8 import java.io.*;
9 import java.util.*;
10
11 public class PeerSetupGUI {
12     static JFrame frame = null;
13     public int rows=-1;
14     public JTable table;
15     public Object JavaDoc[][] grid;
16     public Object JavaDoc[][] lookupValues;
17     public String JavaDoc lookup;
18     public String JavaDoc protocol;
19     
20     protected PeerSetupGUI getThis;
21     
22     public Component createComponents() {
23
24
25         // this reference
26

27         getThis = this;
28         lookupValues = new Object JavaDoc[1][2];
29         //The main panel.
30

31         JPanel pane = new JPanel();
32         pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 20, 40));
33         // Title
34
JLabel title = new JLabel("ProActive P2P System");
35         title.setFont(new Font(null,Font.BOLD,16));
36         title.setLabelFor(frame);
37         
38         // Subtitle
39
JLabel subtitle = new JLabel("Peer Configuration");
40
41         pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
42
43 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
44

45          //The configuration table.
46
String JavaDoc[] columnNames = {"Days",
47                                 "Start time",
48                                 "Work time",
49                                 "Maximum CPU load",
50                                 "Validity"};
51         Object JavaDoc[][] tableData = new Object JavaDoc[7][5];
52         grid = tableData;
53          table = new JTable(tableData, columnNames);
54         JLabel tableLabel = new JLabel("Add or Remove authorized intervals for participation in the P2P System");
55 // TableColumn column = null;
56
// for (int i = 0; i < 5; i++) {
57
// column = table.getColumnModel().getColumn(i);
58
// if (i == 0) {
59
// column.setPreferredWidth(250);
60
// }
61
// if (i == 3) {
62
// column.setPreferredWidth(150);
63
// }
64
// if (i == 4) {
65
// column.setPreferredWidth(150);
66
// }
67
// if ((i!=0)&&(i!=4))
68
// column.setResizable(false) ;
69
// }
70

71         JScrollPane scrollPane = new JScrollPane(table);
72         table.setPreferredScrollableViewportSize(new Dimension(600, 120));
73
74         JPanel tablePanel = new JPanel();
75         tablePanel.setLayout(new GridLayout(1,0));
76
77         tablePanel.add(scrollPane);
78
79
80         //Add button.
81
JButton buttonA = new JButton("Add");
82         buttonA.setMnemonic('a');
83         buttonA.addActionListener(new ActionListener() {
84             public void actionPerformed(ActionEvent e) {
85                     rows++;
86                     if (rows > 10) {rows=10; return;}
87                    JFrame frame = new JFrame("ProActive daemon configuration panel -> Adding one record");
88                    RowModification mPanel = new RowModification(frame, getThis, rows);
89                    frame.setResizable(false);
90                    Component contents = mPanel.createComponents();
91                    frame.getContentPane().add(contents, BorderLayout.CENTER);
92                    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
93                    frame.pack();
94                    frame.setVisible(true);
95             }
96         });
97
98         //Remove button.
99
JButton buttonR = new JButton("Remove");
100         buttonR.setMnemonic('r');
101         buttonR.addActionListener(new ActionListener() {
102             public void actionPerformed(ActionEvent e) {
103             }
104         });
105
106         //Modify button.
107
JButton buttonM = new JButton("Modify");
108         buttonM.setMnemonic('m');
109         buttonM.addActionListener(new ActionListener() {
110             public void actionPerformed(ActionEvent e) {
111                    JFrame frame = new JFrame("ProActive daemon configuration panel -> Modifing one record");
112                    RowModification mPanel = new RowModification(frame, getThis, table.getSelectedRow());
113                    frame.setResizable(false);
114                    Component contents = mPanel.createComponents();
115                    frame.getContentPane().add(contents, BorderLayout.CENTER);
116                    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
117                    frame.pack();
118                    frame.setVisible(true);
119             }
120         });
121
122         //Save button.
123
JButton buttonS = new JButton("Save");
124         buttonS.setMnemonic('s');
125         buttonS.addActionListener(new ActionListener() {
126             public void actionPerformed(ActionEvent e) {
127
128                 ScheduleBean[] schedule = new ScheduleBean[rows+1];
129                 
130                 for (int k=0; k<= rows; k++) {
131             
132                 // Init time
133
schedule[k] = new ScheduleBean();
134                                 
135                 String JavaDoc aux = (String JavaDoc) grid[k][1];
136                 String JavaDoc[] auxArray= aux.split(":");
137                 int startTime = Integer.parseInt(auxArray[0])*100 + Integer.parseInt(auxArray[1]);
138                 
139                 // Finish time
140
aux = (String JavaDoc) grid[k][2];
141                 auxArray= aux.split(":");
142                 int stopTime = Integer.parseInt(auxArray[0])*100 + Integer.parseInt(auxArray[1]);
143                 
144                 // Worktime
145

146                 int workTime = 0;
147                 if (stopTime > startTime) workTime = stopTime - startTime;
148                 else workTime = (startTime + 2400) - stopTime;
149                  
150                 // Max Load
151
aux = (String JavaDoc) grid[k][3];
152                 auxArray = aux.split("%");
153                 int maxLoad = Integer.parseInt(auxArray[0]);
154                 
155                 
156                 schedule[k].setDays(schedule[k].Days2Byte((String JavaDoc) grid[k][0]));
157                 schedule[k].setStartTime(startTime);
158                 schedule[k].setWorkTime(workTime);
159                 schedule[k].setMaxLoad(maxLoad/100.00);
160
161                 /* VALIDITY */
162                 
163                 aux = (String JavaDoc) grid[k][4];
164                 if ("Permanent".equals(aux)) {
165                     schedule[k].setBegin(null);
166                     schedule[k].setEnd(null);
167                 } else {
168                     auxArray = aux.split(" - ");
169                     String JavaDoc[] beginDate = auxArray[0].split("/");
170                     String JavaDoc[] endDate = auxArray[1].split("/");
171                     
172                     int day = Integer.parseInt(beginDate[0]);
173                     int month = Integer.parseInt(beginDate[1]) - 1; // FIRST MONTH = 0
174
int year = Integer.parseInt(beginDate[2]);
175                     GregorianCalendar begin = new GregorianCalendar(year,month,day);
176                     
177                     day = Integer.parseInt(endDate[0]);
178                     month = Integer.parseInt(endDate[1]) -1;
179                     year = Integer.parseInt(endDate[2]);
180                     GregorianCalendar end = new GregorianCalendar(year,month,day);
181                     // add +1 day to be comparable...
182
end.add(Calendar.DAY_OF_YEAR,1);
183                     schedule[k].setBegin(begin);
184                     schedule[k].setEnd(end);
185                 }
186                 }
187                 /* END VALIDITY */
188                 
189                 String JavaDoc machineName = (String JavaDoc) lookupValues[0][0];
190                 String JavaDoc protocol = (String JavaDoc) lookupValues[0][1];
191                 
192                 XMLEncoder encoder;
193                 try {
194                     encoder =
195                         new XMLEncoder(
196                             new BufferedOutputStream(
197                                 new FileOutputStream("ProActiveP2PSchedule.xml")));
198                     encoder.writeObject(schedule);
199                     encoder.writeObject(machineName);
200                     encoder.writeObject(protocol);
201                     encoder.close();
202                 } catch (FileNotFoundException e1) {
203                     System.err.println("[ERROR] Cannot write Schedule.xml");
204                 }
205             }
206         });
207
208
209         //Quit button.
210
JButton buttonQ = new JButton("Quit");
211         buttonQ.setMnemonic('q');
212         buttonQ.addActionListener(new ActionListener() {
213             public void actionPerformed(ActionEvent e) {
214              System.exit(0);
215             }
216         });
217
218         //The button panel.
219
JPanel buttonPanel = new JPanel();
220         buttonPanel.setBorder(BorderFactory.createEmptyBorder(
221                                                        30,
222                                                        30,
223                                                        10,
224                                                        30)
225         );
226         buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
227         buttonPanel.add(buttonA);
228         buttonPanel.add(buttonR);
229         buttonPanel.add(buttonM);
230         buttonPanel.add(buttonS);
231         buttonPanel.add(buttonQ);
232         //The configuration table.
233
String JavaDoc[] colNames = {"Machine Name", "Protocol"};
234         lookupValues[0][0]=new String JavaDoc("//sakuraii.inria.fr/P2PRegistry");
235         lookupValues[0][1]=new String JavaDoc("RMI");
236         
237         JTable lookupTable = new JTable(lookupValues, colNames);
238                 
239         JLabel lookupLabel = new JLabel("Registry Lookup");
240
241         lookupTable.setPreferredScrollableViewportSize(new Dimension(600, 20));
242         TableColumnModel tcm = lookupTable.getColumnModel();
243         JScrollPane jsp = new JScrollPane(lookupTable);
244
245         pane.add(title);
246         pane.add(subtitle);
247
248         pane.add(buttonPanel);
249         pane.add(tableLabel);
250         pane.add(tablePanel);
251
252         pane.add(lookupLabel);
253         pane.add(jsp);
254         
255         return pane;
256     }
257
258     public static void main(String JavaDoc[] args) {
259         try {
260              UIManager.setLookAndFeel(
261              UIManager.getCrossPlatformLookAndFeelClassName());
262         } catch (Exception JavaDoc e) {
263     }
264
265         //Create the top-level container and add contents to it.
266

267         frame = new JFrame("ProActive P2P System");
268         frame.setResizable(false);
269         PeerSetupGUI app = new PeerSetupGUI();
270
271         Component contents = app.createComponents();
272
273         frame.getContentPane().add(contents, BorderLayout.CENTER);
274         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
275         frame.pack();
276         frame.setVisible(true);
277     }
278     
279 }
Popular Tags