private String termToText() throws IOException, PrologException, TermInstantiationException, ExecutionContextException { ExecutionContext exe = new ExecutionContext(); return (String) exe.evaluate( Compound.createCompound( "cpi#termAsString", path, Compound.createCompound("cpi#constExpr", 5)), new Variable()); }
/** * Construct a new instance * * @param term the term containing the descriptor and possibly the visualization mode * @param parent the figure in which this figure is placed * @throws TermVisualizationException if visualization of the term has failed, e.g., if the query * failed. * @throws TermInstantiationException if a visualization object could not be instantiated */ public VisualTerm(Compound term, TermContext parent) throws TermVisualizationException, TermInstantiationException { context = parent; // Register this object with the content context.registerTermFigure(path, this); try { // The first argument is the descriptor, containing the path and additional information descriptor = (Compound) term.arg(1); if (!descriptor.name().equals("::")) System.err.println("Bad descriptor: " + descriptor.name()); path = ((Compound) descriptor.arg(1)).arg(1); if (term.arity() > 1) { projType = (Compound) term.arg(2); } else { projType = Compound.createCompound("cpi#default"); } // Set up the GUI setLayoutManager(new FlowLayout()); setRequestFocusEnabled(true); // Create the child figures contentFigure = createContentFigure(descriptor); add(contentFigure); unreg = Notifier.instance() .register( new Compound("::", path, Compound.createCompound("cpi#path")), new Runnable() { @Override public void run() { try { updateFigure(); } catch (TermVisualizationException e) { e.printStackTrace(); } catch (TermInstantiationException e) { e.printStackTrace(); } } }); } catch (TermVisualizationException e) { e.printStackTrace(); Label label = new Label("<<<" + e.getMessage() + ">>>"); label.setForegroundColor(new Color(context.getTextEditor().getDisplay(), 255, 0, 0)); add(label); } catch (ClassCastException e) { e.printStackTrace(); Label label = new Label("<<<" + e.getMessage() + ">>>"); label.setForegroundColor(new Color(context.getTextEditor().getDisplay(), 128, 128, 0)); add(label); } pathOwner.put(path, this); }
private boolean canModify() { try { ExecutionContext exe = new ExecutionContext(); return exe.isProcDefined( Compound.createCompound("cpi#edit", path, new Variable(), new Variable())); } catch (PrologException e) { e.printStackTrace(); return false; } }
private TermFigure createContentFigure(Object path) throws TermVisualizationException, TermInstantiationException { // Query for the annotated term's visualization Variable vis = new Variable(); Compound q = Compound.createCompound("cpi#visualizeDescriptor", path, projType, vis); // If successful, build the GUI try { Map<Variable, Object> s = PrologProxy.instance().getSolution(q); return (TermFigure) TermInstantiator.instance().instantiate((Compound) s.get(vis), this); } catch (PrologException e) { throw new TermVisualizationException(e); } }
private void setContentFromString(String text) throws TermVisualizationException, PrologException, TermInstantiationException, ExecutionContextException { if (text.startsWith("\"")) { text = "!('" + text.substring(1) + "')"; } ExecutionContext exe = new ExecutionContext(); exe.runProcedure( Compound.createCompound( "cpi#editFromString", path, Compound.createCompound("cpi#constExpr", text))); figureUpdated(); // Schedule a re-focusing of this object getCanvas() .getDisplay() .asyncExec( new Runnable() { @Override public void run() { VisualTerm newVT = pathOwner.get(path); if (newVT != null) newVT.focusGained(null); } }); }
/** * Returns a list of proposals for auto-completion. Each proposal contains a string with the * textual representation of the proposal, and an alias by which this proposal is to be selected * * @param substring the string to match the beginning of the alias to be matched * @param pos ignored * @return an array of proposals */ public IContentProposal[] getProposals(String substring, int pos) { List<IContentProposal> proposals = new ArrayList<IContentProposal>(); try { Variable varCompletion = new Variable(); Variable varAlias = new Variable(); Iterator<Map<Variable, Object>> solutions = PrologProxy.instance() .getSolutions( Compound.createCompound( "cpi#autocomplete", descriptor, substring, varCompletion, varAlias)); while (solutions.hasNext()) { Map<Variable, Object> solution = solutions.next(); final String completion = (String) solution.get(varCompletion); final String alias = (String) solution.get(varAlias); proposals.add( new IContentProposal() { public String getContent() { return completion; } public int getCursorPosition() { int pos; for (pos = 0; pos < completion.length(); pos++) { if (completion.charAt(pos) == '(') return pos + 1; } return pos; } public String getDescription() { return null; } public String getLabel() { return alias + "\t[" + completion + "]"; } }); } } catch (PrologException e) { e.printStackTrace(); } return proposals.toArray(new IContentProposal[] {}); }
private void createContextMenu(MouseEvent me) throws TermInstantiationException { System.out.println("Right button click"); Display display = context.getCanvas().getDisplay(); Menu menu = new Menu(context.getCanvas().getShell(), SWT.POP_UP); try { Variable varAction = new Variable("Action"); Iterator<Map<Variable, Object>> results = PrologProxy.instance() .getSolutions(Compound.createCompound("cpi#contextMenuEntry", descriptor, varAction)); int count = 0; while (results.hasNext()) { Map<Variable, Object> result = (Map<Variable, Object>) results.next(); Compound action = (Compound) result.get(varAction); TermInstantiator.instance().instantiate(action, menu, context); if (count++ > MAX_MENU_ENTRIES) { MenuItem errItem = new MenuItem(menu, SWT.NONE); errItem.setText("<too many results>"); break; } } } catch (PrologException e1) { e1.printStackTrace(); } Point absLocation = me.getLocation().getCopy(); translateToAbsolute(absLocation); org.eclipse.swt.graphics.Point point = display.map( context.getCanvas(), null, new org.eclipse.swt.graphics.Point(absLocation.x, absLocation.y)); menu.setLocation(point); menu.setVisible(true); while (!menu.isDisposed() && menu.isVisible()) { if (!display.readAndDispatch()) display.sleep(); } }