Blame view

circus/src/circusRefine/gui/ErrorDialog.java 7.86 KB
8d0dc533f   Madiel de Souza Conserva Filho   first
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  package circusRefine.gui;
  
  
  import java.awt.Color;
  import java.awt.Font;
  import java.awt.SystemColor;
  import java.awt.event.ActionEvent;
  import java.io.IOException;
  
  import javax.swing.JButton;
  import javax.swing.JDialog;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JScrollPane;
  import javax.swing.JSeparator;
  import javax.swing.JTextArea;
  import javax.swing.JTextField;
  import javax.swing.SwingConstants;
  
  import net.sourceforge.czt.parser.util.ParseException;
  
  import circusRefine.core.ExternalManager;
  import circusRefine.core.crules.CRulesException;
  import circusRefine.util.CRefineException;
  import circusRefine.util.SchemaUtils;
  
  /**
   * Classe de interface que exibi os erros revelados
   * @author Alessandro Gurgel
   *
   */
  public class ErrorDialog extends JDialog {
  	/**
  	 * 
  	 */
  	private static final long serialVersionUID = 1L;
  	private String errorMessage = "";
  	private String path;
  	private String errorStackTrace = "";
  	private String titleCod;
  	private boolean withStackDetails = false;
  	private JFrame traceFrame;
  
  	ExternalManager gerInterface;
  
  	JLabel arquivo;
  	JLabel erros;
  
  	JTextField  campoArquivo;
  	JTextArea campoErros;
  	JScrollPane errorField;
  
  	JButton ok;
  	JButton details;
  
  	JLabel stackTrace;
  	JTextArea detailsField;
  
  	
  	
  	public ErrorDialog(ExternalManager gerExt,  Exception e) {
  		super(gerExt.retornarTelaPrincipal());
  
  		path = "";
  		gerInterface = gerExt;
  		errorMessage = e.getMessage();
  		errorStackTrace = e.getLocalizedMessage();
  		titleCod = "ErrOr";
  		jbInit();
  	}
  	
  	public ErrorDialog(ExternalManager gerExt, String arquivo, CRefineException e) {
  		super(gerExt.retornarTelaPrincipal());
  
  		path = arquivo;
  		gerInterface = gerExt;
  		errorMessage = getErrorMessage(e);
  		errorStackTrace = getStackErrorMessage(e);
  		titleCod = getTitle(e);
  		jbInit();
  	}
  	
  	public ErrorDialog(ExternalManager gerExt, String arquivo, CRefineException e, int teste) {
  		super(gerExt.retornarTelaPrincipal());
  
  		path = arquivo;
  		gerInterface = gerExt;
  		errorMessage = getErrorMessage(e);
  		if (errorMessage.contains("java.lang.NullPointerException")){
  			errorMessage = "The Open option is used to open a previously saved refinement."
  				+ "
  To start a new refinement choose the option: Refinement -> New";
  		}
  		errorStackTrace = getStackErrorMessage(e);
  		titleCod = getTitle(e);
  		jbInit();
  	}
  
  	public ErrorDialog(ExternalManager gerExt, String arquivo, CRulesException e) {
  		super(gerExt.retornarTelaPrincipal());
  
  		path = arquivo;
  		gerInterface = gerExt;
  		errorMessage = getErrorMessage(e);
  		errorStackTrace = getStackErrorMessage(e);
  		titleCod = getTitle(e);
  		jbInit();
  	}
  
  
  	private String getTitle(CRulesException e) {
  		return "CRules Error";
  	}
  
  	private String getErrorMessage(CRulesException e) {
  		String result = "";
  		result = e.getMessage();
  		return result;
  	}
  
  	private String getTitle(CRefineException e) {
  		if (e.getTitleCode() != null){
  			return gerInterface.getMessage(e.getTitleCode());
  		}
  		return "Error";
  	}
  
  	private String getErrorMessage(CRefineException e) {
  		String result = "";
  		if (!(e.getCause() instanceof CRefineException)){
  			result = e.getMessage();
  		}
  		else{
  			result = gerInterface.getMessage(e.getCodeOfMessageToTheUser());
  		}
  
  		return result;
  	}
  
  	private String getStackErrorMessage(Exception e) {
  		String strStackTrace = "";
  		StackTraceElement[] errorsTrace = e.getStackTrace();
  		for (int i = 0; i< errorsTrace.length; i++) {
  			strStackTrace += errorsTrace[i].toString() + "
  ";
  		}
  		return strStackTrace;
  	}
  
  	/**
  	 * M�todo que inicializa os diversos compenentes do Frame
  	 */
  	private void jbInit() {
  
  		int tam = getMaxTam();
  		if (tam > (0.7)*(gerInterface.getTamanhoHorizontal())) {
  			tam = (7*(gerInterface.getTamanhoHorizontal()))/10;
  		}
  
  		tam = Math.max(tam, 3*(gerInterface.getTamanhoHorizontal())/10);
  		this.setSize(tam + 30,  400);
  		this.setTitle(titleCod);
  		this.setLocation((this.gerInterface.getTamanhoHorizontal()*(5))/100, (this.gerInterface.getTamanhoVertical()*10)/100);
  		
  		this.setResizable(false);
  		this.setLayout(null);
  
  		arquivo = new JLabel(this.gerInterface.getMessage("COD0511"));
  		arquivo.setSize(90, 20);
  		arquivo.setLocation(10, 10);
  
  		campoArquivo = new JTextField(this.path);
  		campoArquivo.setEditable(false);
  		campoArquivo.setSize(tam + 5, 25);
  		campoArquivo.setLocation(10,35);
  		campoArquivo.setBackground(SystemColor.WHITE);
  
  		erros = new JLabel(this.gerInterface.getMessage("COD0517"));
  		erros.setSize(130,20);
  		erros.setLocation(10, 60);
  
  		campoErros = new JTextArea(this.exibirErrors());
  		campoErros.setEditable(false);
  		campoErros.setBackground(SystemColor.WHITE);
  		campoErros.setForeground(SystemColor.RED);
  
  		errorField = new JScrollPane(campoErros);
  		errorField.setLocation(10, 85);
  		errorField.setSize(tam + 5, 210);
  
  		JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
  		sep.setSize(tam + 5, 20);
  		sep.setLocation(10, 310);
  		sep.setForeground(Color.BLACK);
  
  		ok = new JButton(this.gerInterface.getMessage("COD0072"));
  		ok.setVisible(true);
  		ok.setBackground(this.getBackground());
  		ok.setFont(new Font("Dialog", 1, 12));
  		ok.setSize(70,20);
  		ok.setLocation(this.getWidth() - 210, sep.getY() + sep.getHeight() );
  		ok.addActionListener(new java.awt.event.ActionListener() {
  			public void actionPerformed(ActionEvent e) {
  				ok_actionPerformed(e);
  			}
  		});
  
  		details = new JButton(this.gerInterface.getMessage("COD0518"));
  		details.setVisible(true);
  		details.setSize(110, 20);
  		details.setLocation(ok.getX() + 80, ok.getY());
  		details.addActionListener(new java.awt.event.ActionListener() {
  			public void actionPerformed(ActionEvent e) {
  				details_actionPerformed(e);
  			}
  		});
  
  		
  		this.add(arquivo);
  		this.add(campoArquivo);
  		this.add(erros);
  		this.add(errorField);
  		this.add(sep);
  		this.add(ok);
  		this.add(details);
  	}
  
  	/**
  	 * Metodo que expande o dialog para conter informa��es de implementa��o,
  	 * o StackTrace
  	 * @param e
  	 */
  	protected void details_actionPerformed(ActionEvent e) {
  		if (!withStackDetails) {
  			campoErros.setText(errorStackTrace);
  			erros.setText(this.gerInterface.getMessage("COD0728"));
  			withStackDetails = true;
  			this.details.setText(this.gerInterface.getMessage("COD0519"));
  		}
  		else {
  			campoErros.setText(errorMessage);
  			erros.setText(this.gerInterface.getMessage("COD0517"));
  			withStackDetails = false;
  			this.details.setText(this.gerInterface.getMessage("COD0518"));
  		}
  	}
  
  	/**
  	 * Fecha a janela
  	 * @param e
  	 */
  	protected void ok_actionPerformed(ActionEvent e) {
  		this.setVisible(false);
  	}
  
  	/**
  	 * Metodo que ira retorna o maior tamanho ocupados pelas string de path e de
  	 * erros
  	 * @return
  	 */
  	private int getMaxTam() {
  		int max = 0;
  		max = SchemaUtils.getWidthOfString(path, this.getFont());
  		String errors = this.exibirErrors();
  		String[] strs = errors.split("
  ");
  		for (int i=0; i< strs.length;i++) {
  			if (SchemaUtils.getWidthOfString(strs[i], this.getFont()) > max){
  				max = SchemaUtils.getWidthOfString(strs[i], this.getFont());
  			}
  		}
  		return max;
  	}
  
  	/**
  	 * Metodo que exibi os erros de forma a nao parecer o arquivo de entrada
  	 */
  	protected String exibirErrors() {
  		String text = this.gerInterface.getMessage("COD0511");
  		text += path + "
  ";
  
  		String completeText = "";
  		if (errorMessage != null) {
  			for (int i=0;i < errorMessage.length();i++) {
  				if (errorMessage.charAt(i) == '\"') {
  
  					String split = errorMessage.substring(i+1);
  
  					if (split.startsWith(path)) {
  						i += path.length() + 1;
  					}
  					else {
  						completeText += errorMessage.charAt(i);
  					}
  				}
  				else {
  					completeText += errorMessage.charAt(i);
  				}
  			}
  		}
  		text += completeText;
  		return completeText;
  
  	}
  
  }