Commit c8a7aebe authored by Johannes Roith's avatar Johannes Roith

Merge branch 'master' of github.com:jroith/cup-eclipse

parents 8083baf1 b01744c9
......@@ -16,11 +16,16 @@ import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
......@@ -61,25 +66,29 @@ import de.tum.in.www2.cupplugin.model.CupParserLaLrChangeObserver;
import de.tum.in.www2.cupplugin.model.Model;
public class CupConflictGraphView implements CupParserLaLrChangeObserver,
IZoomableWorkbenchPart, CupEditorPageVisibility, RegisterForControllerChanges {
IZoomableWorkbenchPart, CupEditorPageVisibility,
RegisterForControllerChanges {
private static final int BUTTON_WIDTH = 120;
private static final int LABEL_WIDTH = 40;
private CupTextEditor editor;
public GraphViewer graphViewer;
private Model parseModel;
private ParserConflictNodeModelContentProvider nodeModel;
private Composite graphComposite;
private Composite parentComposite;
private Label symbolLabel;
private final ScrolledComposite scroll;
// private final Composite scrollSubContent;
private Combo zoomCombo;
private ZoomManager zoomManager;
private Combo nodeSelection;
private Listener nodeSelectionListener;
private boolean isVisible = false;
private boolean shouldRebuildWhenVisible = false;
private HashMap<Integer, lalr_state> stateHashMap;
......@@ -91,7 +100,7 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
Controller.getInstance(editor).registerObserver(this);
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
this.parseModel = Model.getInstanceForDocument(doc);
this.parseModel.registerModelObserver(this);
this.parentComposite = parent;
......@@ -100,12 +109,12 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
this.searchStack = new Stack<CupConflictGraphView.DFSNode>();
this.visitedStates = new HashSet<lalr_state>();
GridLayout parentLayout = new GridLayout(5, false);
GridLayout parentLayout = new GridLayout(7, false);
this.parentComposite.setLayout(parentLayout);
this.graphComposite = new Composite(parent, SWT.NONE);
this.graphComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true, 5, 1));
true, true, 7, 1));
this.graphComposite.setLayout(new FillLayout());
nodeModel = new ParserConflictNodeModelContentProvider();
......@@ -118,21 +127,32 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
graphViewer.setInput(nodeModel.getNodes());
LayoutAlgorithm layout = new CompositeLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING, new LayoutAlgorithm[] {
new SpringLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING),
new HorizontalShift(
LayoutStyles.NO_LAYOUT_NODE_RESIZING),
new GridLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING) });
graphViewer.setLayoutAlgorithm(layout, true);
graphViewer.applyLayout();
// Setup search Result Label
symbolLabel = new Label(this.parentComposite, SWT.WRAP);
symbolLabel.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true,
false, 5, 1));
scroll = new ScrolledComposite(this.parentComposite, SWT.BORDER
| SWT.V_SCROLL);
GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false, 7, 1);
data.minimumHeight = 60;
data.heightHint = 60;
scroll.setLayoutData(data);
scroll.setLayout(new FillLayout());
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
symbolLabel = new Label(scroll, SWT.WRAP);
// symbolLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
// false, 1, 1));
// symbolLabel.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true,
// false, 7, 1));
scroll.setContent(symbolLabel);
scroll.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle r = scroll.getClientArea();
scroll.setMinSize(symbolLabel.computeSize(r.width, SWT.DEFAULT));
}
});
// Setup Buttons to enable/disable some features
Button labelDisplayButton = new Button(this.parentComposite, SWT.CHECK);
......@@ -208,13 +228,16 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
e1.printStackTrace();
}
if (zoomManager != null) {
Combo combo = new Combo(this.parentComposite, SWT.READ_ONLY);
combo.setLayoutData(new GridData(BUTTON_WIDTH, 20));
Label zoomLabel = new Label(this.parentComposite, SWT.WRAP);
zoomLabel.setLayoutData(new GridData(LABEL_WIDTH, 20));
zoomLabel.setText("Zoom:");
zoomCombo = new Combo(this.parentComposite, SWT.READ_ONLY);
zoomCombo.setLayoutData(new GridData(BUTTON_WIDTH, 20));
String[] levels = zoomManager.getZoomLevelsAsText();
combo.setItems(levels);
zoomCombo.setItems(levels);
combo.select(0);
combo.addListener(SWT.Selection, new Listener() {
zoomCombo.select(0);
zoomCombo.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
if (e.widget instanceof Combo) {
......@@ -225,7 +248,11 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
}
});
}
Label stateLabel = new Label(this.parentComposite, SWT.WRAP);
stateLabel.setLayoutData(new GridData(LABEL_WIDTH, 20));
stateLabel.setText("State:");
nodeSelection = new Combo(this.parentComposite, SWT.READ_ONLY);
nodeSelection.setLayoutData(new GridData(BUTTON_WIDTH, 20));
}
public AbstractZoomableViewer getGraphViewer() {
......@@ -272,12 +299,14 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
Combo c = (Combo) event.widget;
int idx = c.getSelectionIndex();
String[] parts = c.getItem(idx).split(" ");
setModelForDFSAfterState(stateHashMap.get(Integer.parseInt(parts[1])));
setModelForDFSAfterState(stateHashMap.get(Integer
.parseInt(parts[1])));
// System.out.println("Selected state: "+state.index());
}
}
};
nodeSelection.addListener(SWT.Selection, nodeSelectionListener);
nodeSelection.select(0);
}
private void setModelForDFSAfterState(lalr_state state) {
......@@ -289,12 +318,13 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
searchStack.push(firstState);
boolean found = DFSSearch(firstState, state.index());
String resultString = "Way to state " + state.index() + ": ";
if (found) {
nodeModel.rebuildModel(searchStack);
LayoutAlgorithm layout = new CompositeLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING, new LayoutAlgorithm[] {
LayoutStyles.NO_LAYOUT_NODE_RESIZING,
new LayoutAlgorithm[] {
new SpringLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING),
new HorizontalShift(
......@@ -303,7 +333,7 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
LayoutStyles.NO_LAYOUT_NODE_RESIZING) });
graphViewer.setLayoutAlgorithm(layout, true);
graphViewer.applyLayout();
graphViewer.refresh();
for (DFSNode n : searchStack) {
if (n.predecessor != null) {
......@@ -318,9 +348,17 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
// symbolLabel.redraw();
// parentComposite.layout();
symbolLabel.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true,
false, 5, 1));
parentComposite.layout(true, true);
// symbolLabel.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM,
// false,
// false, 5, 1));
// symbolLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
// false, 1, 1));
// Setup the new Content Layout and zoom the graph to the selected
// level
scroll.layout(true, true);
Rectangle r = scroll.getClientArea();
scroll.setMinSize(symbolLabel.computeSize(r.width, SWT.DEFAULT));
}
}
......@@ -328,16 +366,15 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
if (state.getState().index() == searchIndex) {
// TODO: Want to only end with nonTerminals?
/*
if(state.predecessor != null && !state.predTransition.on_symbol().is_non_term()) {
return true;
}
return false;
*/
* if(state.predecessor != null &&
* !state.predTransition.on_symbol().is_non_term()) { return true; }
* return false;
*/
return true;
}
visitedStates.add(state.getState());
lalr_transition trans = state.getState().transitions();
//System.out.println("Trans Symbol is non term: "+trans.on_symbol().is_non_term());
// System.out.println("Trans Symbol is non term: "+trans.on_symbol().is_non_term());
while (trans != null) {
if (visitedStates.contains(trans.to_state())
|| trans.on_symbol().is_non_term()) {
......@@ -378,7 +415,7 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
public void willBecomeVisible() {
// this.graphViewer.refresh();
isVisible = true;
if(!Controller.getInstance(editor).requestJobRun()) {
if (!Controller.getInstance(editor).requestJobRun()) {
// TODO: Handle no rerun!!
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
......@@ -522,23 +559,28 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
public List<ParserConflictNode> getNodes() {
return nodes;
}
public void rebuildModel(Stack<DFSNode> foundNodes) {
connections.clear();
nodes.clear();
nodeMap.clear();
ParserConflictNode old = null;
int counter = 0;
for(DFSNode dfsNode : foundNodes) {
if(old == null) {
old = new ParserConflictNode(dfsNode.getState().index(), "State "+dfsNode.getState().index());
for (DFSNode dfsNode : foundNodes) {
if (old == null) {
old = new ParserConflictNode(dfsNode.getState().index(),
"State " + dfsNode.getState().index());
nodes.add(old);
continue;
}
ParserConflictNode n = new ParserConflictNode(dfsNode.getState().index(), "State "+dfsNode.getState().index());
ParserConflictNode n = new ParserConflictNode(dfsNode
.getState().index(), "State "
+ dfsNode.getState().index());
nodes.add(n);
ParserConflictConnection con = new ParserConflictConnection(""+counter, dfsNode.getPredTransition().on_symbol().toString(), old, n);
ParserConflictConnection con = new ParserConflictConnection(""
+ counter, dfsNode.getPredTransition().on_symbol()
.toString(), old, n);
old.getConnectedTo().add(n);
connections.add(con);
old = n;
......@@ -598,7 +640,7 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
}
return "";
}
@Override
public Color getNodeHighlightColor(Object entity) {
Device device = Display.getCurrent();
......@@ -683,7 +725,7 @@ public class CupConflictGraphView implements CupParserLaLrChangeObserver,
@Override
public EnumSet<JobsToDo> getRequiredJobs() {
EnumSet<JobsToDo> requestedJobs = EnumSet.noneOf(JobsToDo.class);
if(isVisible) {
if (isVisible) {
requestedJobs = EnumSet.of(JobsToDo.buildTable, JobsToDo.parseCode);
}
return requestedJobs;
......
......@@ -3,6 +3,7 @@ package de.tum.in.www2.cupplugin.editors;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
......@@ -73,14 +74,19 @@ import de.in.tum.www2.cup.internal.lalr_state;
import de.in.tum.www2.cup.internal.lalr_transition;
import de.in.tum.www2.cup.internal.parse_reduce_row;
import de.in.tum.www2.cup.internal.parse_reduce_table;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
import de.tum.in.www2.cupplugin.controller.Controller;
import de.tum.in.www2.cupplugin.controller.RegisterForControllerChanges;
import de.tum.in.www2.cupplugin.model.CupParserLaLrChangeObserver;
import de.tum.in.www2.cupplugin.model.Model;
public class CupReduceGraphView implements CupParserLaLrChangeObserver,
IZoomableWorkbenchPart, CupEditorPageVisibility {
IZoomableWorkbenchPart, CupEditorPageVisibility,
RegisterForControllerChanges {
// Some Constants
private static final int BUTTON_WIDTH = 120;
private static final int LABEL_WIDTH = 40;
public static final Object modelBuildLock = new Object();
public GraphViewer graphViewer;
......@@ -94,17 +100,24 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
private boolean isVisible = false;
private boolean shouldRebuildWhenVisible = false;
public CupReduceGraphView(Composite parent, IDocument doc) {
private CupTextEditor editor;
public CupReduceGraphView(Composite parent, CupTextEditor editor) {
this.editor = editor;
Controller.getInstance(editor).registerObserver(this);
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
this.parseModel = Model.getInstanceForDocument(doc);
this.parseModel.registerModelObserver(this);
this.parentComposite = parent;
GridLayout parentLayout = new GridLayout(4, false);
GridLayout parentLayout = new GridLayout(5, false);
this.parentComposite.setLayout(parentLayout);
this.graphComposite = new Composite(parent, SWT.NONE);
this.graphComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true, 4, 1));
true, true, 5, 1));
this.graphComposite.setLayout(new FillLayout());
nodeModel = new ParserNodeModelContentProvider();
......@@ -114,9 +127,9 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
ParserReduceLabelProvider labelProvider = new ParserReduceLabelProvider(
nodeModel.getConnections());
graphViewer.setLabelProvider(labelProvider);
//graphViewer.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
// graphViewer.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
graphViewer.setInput(nodeModel.getNodes());
LayoutAlgorithm layout = new CompositeLayoutAlgorithm(
......@@ -204,17 +217,21 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
e1.printStackTrace();
}
if (zoomManager != null) {
org.eclipse.swt.widgets.Label zoomLabel = new org.eclipse.swt.widgets.Label(
this.parentComposite, SWT.WRAP);
zoomLabel.setLayoutData(new GridData(LABEL_WIDTH, 20));
zoomLabel.setText("Zoom:");
Combo combo = new Combo(this.parentComposite, SWT.READ_ONLY);
combo.setLayoutData(new GridData(BUTTON_WIDTH, 20));
String[] levels = zoomManager.getZoomLevelsAsText();
String[] levels = zoomManager.getZoomLevelsAsText();
combo.setItems(levels);
combo.select(0);
combo.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
if (e.widget instanceof Combo) {
Combo c = (Combo)e.widget;
Combo c = (Combo) e.widget;
int idx = c.getSelectionIndex();
String s = levels[idx];
zoomManager.setZoomAsText(levels[idx]);
......@@ -229,21 +246,29 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
public void willBecomeVisible() {
System.out.println("CupGraphView will become visible.");
isVisible = true;
if (!Controller.getInstance(editor).requestJobRun()) {
// TODO: Handle no rerun!!
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
ModelChange(Model.getInstanceForDocument(doc));
}
}
public void reloadGraph() {
LayoutAlgorithm layout = new CompositeLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING, new LayoutAlgorithm[] {
new GridLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING),
new HorizontalShift(
LayoutStyles.NO_LAYOUT_NODE_RESIZING)//,
//new GridLayoutAlgorithm(
// LayoutStyles.NO_LAYOUT_NODE_RESIZING)
});
LayoutStyles.NO_LAYOUT_NODE_RESIZING) // ,
// new GridLayoutAlgorithm(
// LayoutStyles.NO_LAYOUT_NODE_RESIZING)
});
graphViewer.setLayoutAlgorithm(layout, true);
graphViewer.applyLayout();
synchronized (CupReduceGraphView.modelBuildLock) {
graphViewer.refresh();
}
}
}
@Override
......@@ -254,9 +279,12 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
@Override
public void ModelChange(Model model) {
if (!isVisible) {
return;
}
LALRResult lalrResult = model.getLaLrResult();
CupContext lalrContext = model.getLaLrContext();
nodeModel.rebuildModel(model, lalrResult, lalrContext);
nodeModel.rebuildModel(model, lalrResult, lalrContext, this);
}
public AbstractZoomableViewer getGraphViewer() {
......@@ -353,7 +381,7 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
}
public void rebuildModel(Model model, LALRResult lalrResult,
CupContext lalrContext) {
CupContext lalrContext, CupReduceGraphView reduceGraphView) {
this.lalrContext = lalrContext;
this.lalrResult = lalrResult;
......@@ -362,7 +390,7 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
rebuildJob.cancel();
}
rebuildJob = new ModelRebuildJob(model, lalrResult,
lalrContext, connections, nodes, nodeMap);
lalrContext, connections, nodes, nodeMap, reduceGraphView);
rebuildJob.setSystem(true);
rebuildJob.schedule();
}
......@@ -435,12 +463,14 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
private List<ParserReduceConnection> connections;
private List<ParserReduceNode> nodes;
private HashMap<Integer, ParserReduceNode> nodeMap;
private CupReduceGraphView reduceGraphView;
public ModelRebuildJob(Model model, LALRResult lalrResult,
CupContext lalrContext,
List<ParserReduceConnection> connections,
List<ParserReduceNode> nodes,
HashMap<Integer, ParserReduceNode> nodeMap) {
HashMap<Integer, ParserReduceNode> nodeMap,
CupReduceGraphView reduceGraphView) {
super("Model Rebuild Job");
this.model = model;
......@@ -449,6 +479,7 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
this.connections = connections;
this.nodes = nodes;
this.nodeMap = nodeMap;
this.reduceGraphView = reduceGraphView;
}
@Override
......@@ -502,6 +533,27 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
}
}
}
ModelRebuildJobFinished job = new ModelRebuildJobFinished(reduceGraphView);
job.setSystem(true);
job.schedule();
return Status.OK_STATUS;
}
}
static class ModelRebuildJobFinished extends UIJob {
private CupReduceGraphView reduceGraphView;
public ModelRebuildJobFinished(CupReduceGraphView reduceGraphView) {
super("Model Rebuild Job Finished UI Job");
this.reduceGraphView = reduceGraphView;
}
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
reduceGraphView.reloadGraph();
return Status.OK_STATUS;
}
......@@ -806,4 +858,13 @@ public class CupReduceGraphView implements CupParserLaLrChangeObserver,
public AbstractZoomableViewer getZoomableViewer() {
return getGraphViewer();
}
@Override
public EnumSet<JobsToDo> getRequiredJobs() {
EnumSet<JobsToDo> requestedJobs = EnumSet.noneOf(JobsToDo.class);
if (isVisible) {
requestedJobs = EnumSet.of(JobsToDo.buildTable, JobsToDo.parseCode);
}
return requestedJobs;
}
}
......@@ -85,7 +85,7 @@ public class MultiPageEditor extends MultiPageEditorPart implements
Composite composite = new Composite(getContainer(), SWT.NONE);
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
graphReduceView = new CupReduceGraphView(composite, doc);
graphReduceView = new CupReduceGraphView(composite, getEditor());
int index = addPage(composite);
setPageText(index, "Automaton View (costly)");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment