1
1
package edu .rice .cs .hpcviewer .ui .dialogs ;
2
2
3
- import java .io .File ;
4
- import java .io .FileInputStream ;
5
3
import java .io .IOException ;
6
4
import java .util .List ;
7
5
8
6
import org .eclipse .core .internal .runtime .Activator ;
9
7
import org .eclipse .core .runtime .Platform ;
10
8
import org .eclipse .jface .dialogs .Dialog ;
11
9
import org .eclipse .jface .dialogs .IDialogConstants ;
10
+ import org .eclipse .jface .dialogs .MessageDialog ;
12
11
import org .eclipse .swt .SWT ;
13
- import org .eclipse .swt .browser .Browser ;
14
12
import org .eclipse .swt .graphics .Point ;
15
13
import org .eclipse .swt .layout .FillLayout ;
16
14
import org .eclipse .swt .widgets .Composite ;
17
15
import org .eclipse .swt .widgets .Control ;
18
16
import org .eclipse .swt .widgets .Display ;
19
17
import org .eclipse .swt .widgets .Shell ;
18
+ import org .eclipse .swt .widgets .Text ;
20
19
21
20
import edu .rice .cs .hpclog .LogProperty ;
21
+ import edu .rice .cs .hpcviewer .ui .util .FileUtility ;
22
22
23
23
public class InfoLogDialog extends Dialog
24
24
{
25
+ private Text wText ;
26
+
25
27
public InfoLogDialog (Shell shell ) {
26
28
super (shell );
27
29
}
@@ -35,34 +37,10 @@ protected boolean isResizable() {
35
37
@ Override
36
38
protected Control createDialogArea (Composite parent ) {
37
39
Composite content = (Composite ) super .createDialogArea (parent );
38
-
39
- String text = "<pre>\n <h3>Log files used by hpcviewer</h3>" ;
40
40
41
- List <String > logUser = LogProperty .getLogFile ();
42
- for (String log : logUser ) {
43
- text += "File: " + log + "\n " ;
44
- try {
45
- text += getFileContent (log );
46
- } catch (IOException e ) {
47
- // do nothing
48
- }
49
- }
50
- text += "\n \n " ;
51
- try {
52
- Activator activator = Activator .getDefault ();
53
- if (activator != null ) {
54
- String locUser = Platform .getLogFileLocation ().toOSString ();
55
- text += "<b>File: " + locUser + "</b>\n " ;
56
- text += getFileContent (locUser );
57
- }
58
- } catch (IOException e ) {
59
- // do nothing
60
- }
61
- text += "</pre>" ;
41
+ wText = new Text (content , SWT .MULTI | SWT .READ_ONLY | SWT .V_SCROLL | SWT .H_SCROLL );
42
+ fillText ();
62
43
63
- Browser browser = new Browser (content , SWT .MULTI );
64
- browser .setText (text );
65
-
66
44
content .setLayout (new FillLayout ());
67
45
68
46
return content ;
@@ -81,30 +59,75 @@ protected void configureShell(Shell newShell) {
81
59
82
60
@ Override
83
61
protected void createButtonsForButtonBar (Composite parent ) {
62
+ createButton (parent , IDialogConstants .HELP_ID , "Clear logs" , true );
84
63
createButton (parent , IDialogConstants .OK_ID , IDialogConstants .OK_LABEL , true );
85
64
}
86
65
87
- /***
88
- * Read the content of the file
89
- * @param filename
90
- * @return String
91
- * @throws IOException
92
- */
93
- private String getFileContent (String filename ) throws IOException {
94
- File file = new File (filename );
95
- if (!file .canRead ())
96
- return "" ;
97
-
98
- FileInputStream fis = new FileInputStream (file );
99
- byte [] data = new byte [(int ) file .length ()];
100
- fis .read (data );
101
-
102
- String content = new String (data , "UTF-8" );
103
- fis .close ();
66
+
67
+ @ Override
68
+ protected void buttonPressed (int buttonId ) {
69
+ if (buttonId == IDialogConstants .HELP_ID ) {
70
+ if (!MessageDialog .openConfirm (getShell (), "Removing log files" , "Are you sure to clear log files?" )) {
71
+ return ;
72
+ }
73
+ List <String > logUser = LogProperty .getLogFile ();
74
+
75
+ // get the content for each log files
76
+ for (String log : logUser ) {
77
+ try {
78
+ FileUtility .clearFileContent (log );
79
+ } catch (IOException e ) {
80
+ }
81
+ }
82
+ Activator activator = Activator .getDefault ();
83
+ if (activator != null ) {
84
+ String locUser = Platform .getLogFileLocation ().toOSString ();
85
+ try {
86
+ FileUtility .clearFileContent (locUser );
87
+ } catch (IOException e ) {
88
+ }
89
+ }
90
+ fillText ();
91
+
92
+ } else {
93
+ super .buttonPressed (buttonId );
94
+ }
95
+ }
96
+
97
+ private void fillText () {
98
+
99
+ // set the title
100
+ String text = "Log files used by hpcviewer\n " ;
101
+
102
+ List <String > logUser = LogProperty .getLogFile ();
103
+
104
+ // get the content for each log files
105
+ for (String log : logUser ) {
106
+ text += "File: " + log + "\n " ;
107
+ try {
108
+ text += FileUtility .getFileContent (log );
109
+ } catch (IOException e ) {
110
+ // do nothing
111
+ }
112
+ }
113
+ text += "\n \n " ;
104
114
105
- return content ;
115
+ try {
116
+ Activator activator = Activator .getDefault ();
117
+ if (activator != null ) {
118
+ String locUser = Platform .getLogFileLocation ().toOSString ();
119
+ text += "File: " + locUser + "\n " ;
120
+ text += FileUtility .getFileContent (locUser );
121
+ }
122
+ } catch (IOException e ) {
123
+ // do nothing
124
+ }
125
+ text += "\n " ;
126
+ wText .setText (text );
127
+
106
128
}
107
129
130
+
108
131
static public void main (String argv []) {
109
132
Display d = new Display ();
110
133
InfoLogDialog dlg = new InfoLogDialog (new Shell (d ));
0 commit comments