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 196 197 198 199 200 201 202 203 204 205 206 207 208
| private class EditDialog extends JDialog implements ActionListener {
private JButton runButton, closeButton, saveButton, checkButton ;
private JTextArea textArea ;
private Error error = new Error() ;
/*
a class for handle error in scenario editing
*/
private class Error {
private int line ;
private String code ;
private String message ;
private String type ;
private boolean exist ;
Error() {
setNoError() ;
}
private void setNoError() {
this.line = 0 ;
this.code = "" ;
this.message = "No Error" ;
this.type = "No Error" ;
this.exist = false ;
}
private void setError(int line, String code, String message, String type) {
this.line =line ;
this.code = code ;
this.message = message ;
this.type = type ;
this.exist = true ;
}
private void showError(Component parent) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(parent, this.type+" at line "+(this.line+1)+"\n"+this.message, this.type, JOptionPane.ERROR_MESSAGE) ;
}
}
/*
return a string with the line numbers
used by EditDialog for display row header
*/
private String getLineNumbers() {
String lineNumbers = "";
for (int count = 1; count<500 ; count++)
lineNumbers=lineNumbers+count+"\n" ;
return lineNumbers ;
}
/*
edit the scenario
*/
public EditDialog() {
super(chartManager,Scheduler.this.fileName,true);
Container panel = getContentPane() ;
panel.setLayout(new BorderLayout());
runButton = new JButton("Run") ;
runButton.addActionListener(this) ;
closeButton = new JButton("Close") ;
closeButton.addActionListener(this) ;
saveButton = new JButton("Save as...") ;
saveButton.addActionListener(this) ;
checkButton = new JButton("Check") ;
checkButton.addActionListener(this) ;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(closeButton) ;
buttonPanel.add(saveButton) ;
buttonPanel.add(checkButton) ;
buttonPanel.add(runButton) ;
panel.add(buttonPanel, BorderLayout.SOUTH);
textArea = new JTextArea();
textArea.setText(Scheduler.this.doc) ;
textArea.setMargin(new Insets(0,20,0,0)) ;
JScrollPane scrollPane = new JScrollPane(textArea) ;
JTextPane rowHeader = new JTextPane();
rowHeader.setText(getLineNumbers()) ;
rowHeader.setEditable(false) ;
rowHeader.setEnabled(false) ;
scrollPane.setRowHeaderView(rowHeader) ;
panel.add(scrollPane, BorderLayout.CENTER);
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().width/3 ;
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().height*3/4 ;
setSize(width, height) ;
setLocationRelativeTo(chartManager) ;
setVisible(true) ;
}
/*
handle button click
*/
public void actionPerformed (ActionEvent e) {
if (e.getSource()==runButton) {
if (checkScenario(textArea.getText())) {
if (Scheduler.this.doc.equals(textArea.getText())) dispose() ;
else {
int rep = JOptionPane.showConfirmDialog(
this,
"Your changes will be automaticly saved before runing.\nSave and run scenario ?",
"Scenario modified",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE
) ;
switch(rep) {
case JOptionPane.YES_OPTION :
Scheduler.this.doc = textArea.getText() ;
saveScenario(Scheduler.this.file) ;
dispose() ;
break ;
case JOptionPane.NO_OPTION : break ;
default : fatalError("Unexpected option") ;
}
}
}
else error.showError(this) ;
}
else if (e.getSource()==closeButton) {
if (Scheduler.this.doc.equals(textArea.getText())) System.exit(0) ;
else {
int rep = JOptionPane.showConfirmDialog(this, "Save changes before closing ?", "Scenario modified", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) ;
switch(rep) {
case JOptionPane.YES_OPTION :
Scheduler.this.doc = textArea.getText() ;
saveScenario(Scheduler.this.file) ;
case JOptionPane.NO_OPTION :
System.exit(0) ;
case JOptionPane.CANCEL_OPTION :
break ;
default : fatalError("Unexpected option") ;
}
}
}
else if (e.getSource()==saveButton) {
Scheduler.this.doc = textArea.getText() ;
if (saveScenarioAs(this,Scheduler.this.file)) {
setTitle(Scheduler.this.fileName) ;
validate() ;
}
}
else if (e.getSource()==checkButton) {
error.setNoError() ;
if (checkScenario(textArea.getText())) JOptionPane.showMessageDialog(this, "This scenario seems ok.", "Checking Scenario", JOptionPane.INFORMATION_MESSAGE) ;
else error.showError(this) ;
}
}
/*
check scenario
*/
private boolean checkScenario(String scenario) {
error.setNoError() ;
Scheduler.this.schedule = new LinkedList<Event>() ;
String[] line = scenario.split("\n") ;
int lastPeriod = 0 ;
for (int i=0; i<line.length ; i++) {
String[] part = line[i].split("//") ;
if (part[0].length()>0) {
String code = part[0].trim() ;
String[] word = code.split(" ") ;
if ((word.length<2) | (word.length>3)) {
error.setError(i,part[0],"Right Syntax is:\nDate Sector.event Parameters","Syntax Error") ;
return false ;
}
if (!Event.isValidPeriod(word[0])) {
error.setError(i,word[0],"Date Missing or Bad Date Format.\nDate Format must be YYYY-MM","Date Error") ;
return false ;
}
int period = Event.getPeriod(word[0]) ;
if (period<lastPeriod) {
error.setError(i,word[0],"Events must be sorted by date","Date Error") ;
return false ;
}
lastPeriod = period ;
String[] sectorEvent = word[1].split("\\.") ;
if (sectorEvent.length!=2) {
error.setError(i,word[1],"Right Syntax is:\nSector.event","Syntax Error") ;
return false ;
}
if (!Sector.isValid(sectorEvent[0])) {
error.setError(i,sectorEvent[0],"Invalid Sector","Sector Error") ;
return false ;
}
Event event = Event.getNewEvent(sectorEvent[0]) ;
event.setPeriod(period) ;
if (!event.isValid(sectorEvent[1])) {
error.setError(i,sectorEvent[1],"Invalid Event","Event Error") ;
return false ;
}
event.setEvent(sectorEvent[1]) ;
String param ;
if (word.length==2) param = "" ;
else param = word[2] ;
if (!event.isValidParameter(param)) {
error.setError(i,param,"Invalid(s) Parameter(s)","Parameter Error") ;
return false ;
}
event.setParameter(param) ;
Scheduler.this.schedule.add(event) ;
}
}
return true ;
}
} |
Partager