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 [][] grid; 16 public Object [][] lookupValues; 17 public String lookup; 18 public String protocol; 19 20 protected PeerSetupGUI getThis; 21 22 public Component createComponents() { 23 24 25 27 getThis = this; 28 lookupValues = new Object [1][2]; 29 31 JPanel pane = new JPanel(); 32 pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 20, 40)); 33 JLabel title = new JLabel("ProActive P2P System"); 35 title.setFont(new Font(null,Font.BOLD,16)); 36 title.setLabelFor(frame); 37 38 JLabel subtitle = new JLabel("Peer Configuration"); 40 41 pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); 42 43 45 String [] columnNames = {"Days", 47 "Start time", 48 "Work time", 49 "Maximum CPU load", 50 "Validity"}; 51 Object [][] tableData = new Object [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 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 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 JButton buttonR = new JButton("Remove"); 100 buttonR.setMnemonic('r'); 101 buttonR.addActionListener(new ActionListener() { 102 public void actionPerformed(ActionEvent e) { 103 } 104 }); 105 106 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 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 schedule[k] = new ScheduleBean(); 134 135 String aux = (String ) grid[k][1]; 136 String [] auxArray= aux.split(":"); 137 int startTime = Integer.parseInt(auxArray[0])*100 + Integer.parseInt(auxArray[1]); 138 139 aux = (String ) grid[k][2]; 141 auxArray= aux.split(":"); 142 int stopTime = Integer.parseInt(auxArray[0])*100 + Integer.parseInt(auxArray[1]); 143 144 146 int workTime = 0; 147 if (stopTime > startTime) workTime = stopTime - startTime; 148 else workTime = (startTime + 2400) - stopTime; 149 150 aux = (String ) grid[k][3]; 152 auxArray = aux.split("%"); 153 int maxLoad = Integer.parseInt(auxArray[0]); 154 155 156 schedule[k].setDays(schedule[k].Days2Byte((String ) grid[k][0])); 157 schedule[k].setStartTime(startTime); 158 schedule[k].setWorkTime(workTime); 159 schedule[k].setMaxLoad(maxLoad/100.00); 160 161 162 163 aux = (String ) 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 [] beginDate = auxArray[0].split("/"); 170 String [] endDate = auxArray[1].split("/"); 171 172 int day = Integer.parseInt(beginDate[0]); 173 int month = Integer.parseInt(beginDate[1]) - 1; 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 end.add(Calendar.DAY_OF_YEAR,1); 183 schedule[k].setBegin(begin); 184 schedule[k].setEnd(end); 185 } 186 } 187 188 189 String machineName = (String ) lookupValues[0][0]; 190 String protocol = (String ) 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 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 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 String [] colNames = {"Machine Name", "Protocol"}; 234 lookupValues[0][0]=new String ("//sakuraii.inria.fr/P2PRegistry"); 235 lookupValues[0][1]=new String ("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 [] args) { 259 try { 260 UIManager.setLookAndFeel( 261 UIManager.getCrossPlatformLookAndFeelClassName()); 262 } catch (Exception e) { 263 } 264 265 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 |