2
2
3
3
import java .util .HashSet ;
4
4
import java .util .List ;
5
+ import java .util .StringTokenizer ;
6
+
7
+ import javax .swing .Box ;
8
+ import javax .swing .JLabel ;
9
+ import javax .swing .JOptionPane ;
10
+ import javax .swing .JPanel ;
11
+ import javax .swing .JTextField ;
5
12
6
13
import org .eclipse .core .commands .AbstractHandler ;
7
14
import org .eclipse .core .commands .ExecutionEvent ;
19
26
import programslicerplugin .configs .DefaultStyle ;
20
27
import slicer .Criterion ;
21
28
import slicer .Slicer ;
22
- import org .eclipse .jface .dialogs .InputDialog ;
23
29
import org .eclipse .jface .dialogs .MessageDialog ;
24
30
import org .eclipse .jface .text .IDocument ;
25
31
import org .eclipse .jface .text .IRegion ;
26
32
import org .eclipse .jface .text .ITextOperationTarget ;
27
33
import org .eclipse .jface .text .ITextViewer ;
28
- import org .eclipse .jface .window .Window ;
29
34
import org .eclipse .swt .SWT ;
30
35
import org .eclipse .swt .custom .StyleRange ;
31
36
import org .eclipse .swt .custom .StyledText ;
@@ -40,79 +45,131 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
40
45
IWorkbenchPage iPage = window .getActivePage ();
41
46
IEditorPart iPart = iPage .getActiveEditor ();
42
47
IEditorInput input = iPart .getEditorInput ();
43
-
48
+
44
49
String pathname = null ;
45
-
46
- if (input instanceof FileStoreEditorInput ) {
50
+
51
+ if (input instanceof FileStoreEditorInput ) {
47
52
FileStoreEditorInput fStoreEditorInput = input .getAdapter (FileStoreEditorInput .class );
48
53
pathname = fStoreEditorInput .getURI ().getPath ();
49
54
} else if (input instanceof FileEditorInput ) {
50
55
FileEditorInput fEditorInput = input .getAdapter (FileEditorInput .class );
51
56
pathname = fEditorInput .getURI ().getPath ();
52
57
}
53
58
54
- InputDialog inputDialog = new InputDialog (window .getShell (), "Program Slicing" , "Enter slicing line number" ,
55
- null , null );
59
+
56
60
ITextEditor iTextEditor = (ITextEditor ) iPage .getActiveEditor ();
57
61
IDocumentProvider iDocumentProvider = iTextEditor .getDocumentProvider ();
58
62
IDocument document = iDocumentProvider .getDocument (input );
59
63
int totalLine = document .getNumberOfLines ();
60
64
61
- if (inputDialog .open () == Window .OK ) {
62
- Integer line = Integer .parseInt (inputDialog .getValue ());
63
- if (line .intValue () > totalLine ) {
64
- MessageDialog .openError (window .getShell (), "Error" , "Invalid line number" );
65
+ Integer line = null ;
66
+
67
+ JTextField lineNumInput = new JTextField (10 );
68
+ JTextField variablesInput = new JTextField (30 );
69
+
70
+ JPanel myPanel = new JPanel ();
71
+ myPanel .add (new JLabel ("Line Number:" ));
72
+ myPanel .add (lineNumInput );
73
+ myPanel .add (Box .createVerticalStrut (15 ));
74
+ myPanel .add (new JLabel ("Variables:" ));
75
+ myPanel .add (variablesInput );
76
+
77
+ int result = JOptionPane .showConfirmDialog (null , myPanel , "Please Enter Slicing Criterion" ,
78
+ JOptionPane .OK_CANCEL_OPTION );
79
+ if (result == JOptionPane .OK_OPTION ) {
80
+
81
+ if (lineNumInput .getText ().equals ("" ) || variablesInput .getText ().equals ("" )) {
82
+ MessageDialog .openError (window .getShell (), "Error" , "Invalid input" );
65
83
return null ;
84
+ } else {
85
+ try {
86
+ line = Integer .parseInt (lineNumInput .getText ());
87
+ } catch (Exception e ) {
88
+ // TODO: handle exception
89
+ e .printStackTrace ();
90
+ MessageDialog .openError (window .getShell (), "Error" , "Invalid line number" );
91
+ return null ;
92
+
93
+ }
94
+
95
+ if (line .intValue () > totalLine ) {
96
+ MessageDialog .openError (window .getShell (), "Error" , "Invalid line number" );
97
+ return null ;
98
+ }
99
+
66
100
}
67
-
68
- List <Integer > slicedLines = null ;
101
+ } else {
102
+ MessageDialog .openError (window .getShell (), "Error" , "Invalid input" );
103
+ return null ;
104
+ }
105
+
106
+ List <Integer > slicedLines = null ;
69
107
108
+ try {
109
+ Slicer slicer = new Slicer ();
110
+ HashSet <String > variableSet = new HashSet <>();
70
111
try {
71
- Slicer slicer = new Slicer ( );
72
- HashSet < String > variableSet = new HashSet <>();
73
- variableSet . add ( "product " );
74
-
75
- Criterion criterion = new Criterion ( line , variableSet );
76
- slicedLines = slicer . getBackwardSlice ( pathname , criterion );
112
+ StringTokenizer stTokenizer = new StringTokenizer ( variablesInput . getText () );
113
+ while ( stTokenizer . hasMoreTokens ()) {
114
+ String token = stTokenizer . nextToken ( ", " );
115
+ variableSet . add ( token );
116
+
117
+ }
77
118
} catch (Exception e ) {
78
119
// TODO: handle exception
79
- MessageDialog .openError (window .getShell (), "Error" , "Internal Error Occured" );
80
- }
81
-
82
- if (slicedLines == null ) {
83
- MessageDialog .openError (window .getShell (), "Error" , "Internal Error Occured" );
120
+ e .printStackTrace ();
121
+ MessageDialog .openError (window .getShell (), "Error" , "Invalid variable" );
84
122
return null ;
123
+
85
124
}
86
-
87
- ITextViewer viewer = (ITextViewer ) iPart .getAdapter (ITextOperationTarget .class );
88
- StyledText styledText = viewer .getTextWidget ();
89
- DefaultStyle .DEFAULTSTYLEDTEXTRANGES = styledText .getStyleRanges ();
90
-
91
- Display display = Display .getDefault ();
92
- StyleRange style = new StyleRange ();
93
- style .background = display .getSystemColor (SWT .COLOR_DARK_GRAY );
94
- style .foreground = display .getSystemColor (SWT .COLOR_GREEN );
95
- style .fontStyle = SWT .BOLD ;
96
- style .borderColor = display .getSystemColor (SWT .COLOR_WHITE );
97
- style .borderStyle = SWT .BORDER_SOLID ;
98
- StyleRange [] styles = {style };
99
-
100
- for (Integer lineNum : slicedLines ) {
101
- try {
102
- IRegion lineInfo = document .getLineInformation (lineNum -1 );
103
- styledText .setStyleRanges (0 , 0 , new int [] {lineInfo .getOffset (),lineInfo .getLength ()}, styles );
104
- } catch (Exception e ) {
105
- // TODO: handle exception
106
- e .printStackTrace ();
107
- MessageDialog .openError (window .getShell (), "Error" , "Internal Error Occured" );
108
125
109
- }
126
+ if (variableSet .isEmpty ()) {
127
+ MessageDialog .openError (window .getShell (), "Error" , "Invalid variable" );
128
+ return null ;
110
129
}
111
130
112
- MessageDialog .openInformation (window .getShell (), "Program Slicing" , "Slicing Complete" );
131
+ Criterion criterion = new Criterion (line .intValue (), variableSet );
132
+ slicedLines = slicer .getBackwardSlice (pathname , criterion );
133
+ } catch (Exception e ) {
134
+ // TODO: handle exception
135
+ e .printStackTrace ();
136
+ MessageDialog .openError (window .getShell (), "Error" , "Internal Error Occured" );
137
+ return null ;
138
+ }
113
139
140
+ if (slicedLines == null ) {
141
+ MessageDialog .openError (window .getShell (), "Error" , "Internal Error Occured" );
142
+ return null ;
143
+ }
114
144
145
+ ITextViewer viewer = (ITextViewer ) iPart .getAdapter (ITextOperationTarget .class );
146
+ StyledText styledText = viewer .getTextWidget ();
147
+ DefaultStyle .DEFAULTSTYLEDTEXTRANGES = styledText .getStyleRanges ();
148
+
149
+ Display display = Display .getDefault ();
150
+ StyleRange style = new StyleRange ();
151
+ style .background = display .getSystemColor (SWT .COLOR_DARK_GRAY );
152
+ style .foreground = display .getSystemColor (SWT .COLOR_GREEN );
153
+ style .fontStyle = SWT .BOLD ;
154
+ style .borderColor = display .getSystemColor (SWT .COLOR_WHITE );
155
+ style .borderStyle = SWT .BORDER_SOLID ;
156
+ StyleRange [] styles = { style };
157
+
158
+ for (Integer lineNum : slicedLines ) {
159
+ try {
160
+ IRegion lineInfo = document .getLineInformation (lineNum - 1 );
161
+ styledText .setStyleRanges (0 , 0 , new int [] { lineInfo .getOffset (), lineInfo .getLength () }, styles );
162
+ } catch (Exception e ) {
163
+ // TODO: handle exception
164
+ e .printStackTrace ();
165
+ MessageDialog .openError (window .getShell (), "Error" , "Internal Error Occured" );
166
+ return null ;
167
+
168
+ }
115
169
}
170
+
171
+ MessageDialog .openInformation (window .getShell (), "Program Slicing" , "Slicing Complete" );
172
+
116
173
return null ;
117
174
}
118
175
}
0 commit comments