Skip to content

Commit ff6a3ea

Browse files
Slicing criterion taken from user
1 parent 6c7d43e commit ff6a3ea

File tree

2 files changed

+204
-91
lines changed

2 files changed

+204
-91
lines changed

Program-Slicer-Plugin/src/programslicerplugin/handlers/BackwardSliceHandler.java

Lines changed: 105 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import java.util.HashSet;
44
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;
512

613
import org.eclipse.core.commands.AbstractHandler;
714
import org.eclipse.core.commands.ExecutionEvent;
@@ -19,13 +26,11 @@
1926
import programslicerplugin.configs.DefaultStyle;
2027
import slicer.Criterion;
2128
import slicer.Slicer;
22-
import org.eclipse.jface.dialogs.InputDialog;
2329
import org.eclipse.jface.dialogs.MessageDialog;
2430
import org.eclipse.jface.text.IDocument;
2531
import org.eclipse.jface.text.IRegion;
2632
import org.eclipse.jface.text.ITextOperationTarget;
2733
import org.eclipse.jface.text.ITextViewer;
28-
import org.eclipse.jface.window.Window;
2934
import org.eclipse.swt.SWT;
3035
import org.eclipse.swt.custom.StyleRange;
3136
import org.eclipse.swt.custom.StyledText;
@@ -40,79 +45,131 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
4045
IWorkbenchPage iPage = window.getActivePage();
4146
IEditorPart iPart = iPage.getActiveEditor();
4247
IEditorInput input = iPart.getEditorInput();
43-
48+
4449
String pathname = null;
45-
46-
if(input instanceof FileStoreEditorInput) {
50+
51+
if (input instanceof FileStoreEditorInput) {
4752
FileStoreEditorInput fStoreEditorInput = input.getAdapter(FileStoreEditorInput.class);
4853
pathname = fStoreEditorInput.getURI().getPath();
4954
} else if (input instanceof FileEditorInput) {
5055
FileEditorInput fEditorInput = input.getAdapter(FileEditorInput.class);
5156
pathname = fEditorInput.getURI().getPath();
5257
}
5358

54-
InputDialog inputDialog = new InputDialog(window.getShell(), "Program Slicing", "Enter slicing line number",
55-
null, null);
59+
5660
ITextEditor iTextEditor = (ITextEditor) iPage.getActiveEditor();
5761
IDocumentProvider iDocumentProvider = iTextEditor.getDocumentProvider();
5862
IDocument document = iDocumentProvider.getDocument(input);
5963
int totalLine = document.getNumberOfLines();
6064

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");
6583
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+
66100
}
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;
69107

108+
try {
109+
Slicer slicer = new Slicer();
110+
HashSet<String> variableSet = new HashSet<>();
70111
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+
}
77118
} catch (Exception e) {
78119
// 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");
84122
return null;
123+
85124
}
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");
108125

109-
}
126+
if (variableSet.isEmpty()) {
127+
MessageDialog.openError(window.getShell(), "Error", "Invalid variable");
128+
return null;
110129
}
111130

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+
}
113139

140+
if (slicedLines == null) {
141+
MessageDialog.openError(window.getShell(), "Error", "Internal Error Occured");
142+
return null;
143+
}
114144

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+
}
115169
}
170+
171+
MessageDialog.openInformation(window.getShell(), "Program Slicing", "Slicing Complete");
172+
116173
return null;
117174
}
118175
}

0 commit comments

Comments
 (0)