Esempio n. 1
0
 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;
   }
 }
Esempio n. 2
0
 private void replaceContent() {
   try {
     setContentFromString(context.getTextEditor().getText());
   } catch (TermVisualizationException e) {
     ErrorDialog.openError(
         context.getTextEditor().getShell(),
         "Error Updating Element",
         "The following error has occured: " + e.getMessage(),
         Status.OK_STATUS);
   } catch (PrologException e) {
     e.printStackTrace();
   } catch (TermInstantiationException e) {
     e.printStackTrace();
   } catch (ExecutionContextException e) {
     e.printStackTrace();
   }
 }
Esempio n. 3
0
  public void focusGained(org.eclipse.swt.events.FocusEvent event) {
    VisualTerm previousFocused = context.getFocused();
    if (previousFocused != null) previousFocused.lostFocus();
    context.setFocused(this);

    LineBorder focusBorder = new LineBorder();
    focusBorder.setStyle(Graphics.LINE_DASH);
    focusBorder.setColor(getColor());
    focusBorder.setWidth(1);
    setBorder(focusBorder);
    context.selectionChanged(this);
    if (canModify()) {
      try {
        String text = termToText();
        context.getTextEditor().setText(text);
        context.getTextEditor().setEnabled(true);
        context.getTextEditor().setSelection(0, text.length());
        context.getTextEditor().addKeyListener(this);
        context.getTextEditor().setFocus();
        Viewport viewport = ((FigureCanvas) getCanvas()).getViewport();
        Point p = viewport.getViewLocation();
        if (p.y > getLocation().y) {
          viewport.setViewLocation(new Point(p.x, getLocation().y));
        } else if (p.y + viewport.getSize().height < getLocation().y + getSize().height
            && viewport.getSize().height > getSize().height) {
          viewport.setViewLocation(
              new Point(p.x, getLocation().y + getSize().height - viewport.getSize().height));
        }
        if (p.x > getLocation().x) {
          viewport.setViewLocation(new Point(getLocation().x, p.y));
        } else if (p.x + viewport.getSize().width < getLocation().x + getSize().width
            && viewport.getSize().width > getSize().width) {
          viewport.setViewLocation(
              new Point(getLocation().x + getSize().width - viewport.getSize().width, p.y));
        }
      } catch (IOException e) {
        e.printStackTrace();
      } catch (PrologException e) {
        e.printStackTrace();
      } catch (TermInstantiationException e) {
        e.printStackTrace();
      } catch (ExecutionContextException e) {
        e.printStackTrace();
      }
    }
  }
Esempio n. 4
0
  /**
   * 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[] {});
  }
Esempio n. 5
0
  private void applyShortcut(int keyCode, int stateMask) {
    String key = keyDescription(keyCode, stateMask);

    // Query the command, if exists.
    Variable procVar = new Variable();
    Compound query = new Compound("cpi#shortcutKey", descriptor, key, procVar);
    try {
      Iterator<Map<Variable, Object>> solutions = PrologProxy.instance().getSolutions(query);
      while (solutions.hasNext()) {
        ExecutionContext exe = new ExecutionContext();
        exe.runProcedure((Compound) solutions.next().get(procVar));
      }
    } catch (PrologException e) {
      e.printStackTrace();
    } catch (TermInstantiationException e) {
      e.printStackTrace();
    } catch (ExecutionContextException e) {
      e.printStackTrace();
    }
  }
Esempio n. 6
0
 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();
   }
 }