Example #1
0
 public void lostFocus() {
   setBorder(null);
   context.getTextEditor().setText("");
   context.getTextEditor().setEnabled(false);
   context.getTextEditor().removeFocusListener(this);
   context.getTextEditor().removeKeyListener(this);
   context.setFocused(null);
 }
Example #2
0
  /**
   * 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);
  }
Example #3
0
 public void keyPressed(KeyEvent event) {
   if (context.getFocused() != this) return;
   applyShortcut(event.keyCode, event.stateMask);
   if (event.character == '\r' && event.stateMask == 0) {
     replaceContent();
   } else if (event.stateMask == (SWT.ALT | SWT.SHIFT)) {
     FigureNavigator nav = navigator();
     try {
       VisualTerm other = null;
       switch (event.keyCode) {
         case SWT.ARROW_UP:
           other =
               (VisualTerm)
                   nav.getLastSmallestDecscendant(
                       nav.getFirstDescendantOfPrevSibling(
                           this, VerticalFlow.class, VisualTerm.class),
                       VisualTerm.class);
           break;
         case SWT.ARROW_DOWN:
           other =
               (VisualTerm)
                   nav.getFirstSmallestDecscendant(
                       nav.getFirstDescendantOfNextSibling(
                           this, VerticalFlow.class, VisualTerm.class),
                       VisualTerm.class);
           break;
         case SWT.ARROW_LEFT:
           other =
               (VisualTerm)
                   nav.getLastSmallestDecscendant(
                       nav.getFirstDescendantOfPrevSibling(
                           this, HorizontalFlow.class, VisualTerm.class),
                       VisualTerm.class);
           break;
         case SWT.ARROW_RIGHT:
           other =
               (VisualTerm)
                   nav.getFirstSmallestDecscendant(
                       nav.getFirstDescendantOfNextSibling(
                           this, HorizontalFlow.class, VisualTerm.class),
                       VisualTerm.class);
           break;
         case SWT.PAGE_UP:
           other = (VisualTerm) nav.getAncestor(getParent(), VisualTerm.class);
           break;
         case SWT.PAGE_DOWN:
         case SWT.HOME:
           other = (VisualTerm) nav.getFirstDescendant(this, VisualTerm.class, false);
           break;
         case SWT.END:
           other = (VisualTerm) nav.getLastDescendant(this, VisualTerm.class);
           break;
       }
       if (other != null) other.focusGained(null);
     } catch (FigureNotFoundException e) {
       // No navigation...
     }
   }
 }
Example #4
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();
   }
 }
Example #5
0
 public void mousePressed(MouseEvent me) {
   if (me.button == 3) {
     // Context menu
     try {
       createContextMenu(me);
     } catch (TermInstantiationException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   } else {
     // Gain focus
     if (canFocus()) {
       requestFocus();
       context.getTextEditor().setEnabled(true);
       context.getTextEditor().addFocusListener(this);
       context.getTextEditor().setFocus();
     } else {
       context.handleClick(me);
     }
   }
 }
Example #6
0
 public void updateFigure() throws TermVisualizationException, TermInstantiationException {
   contentFigure.dispose();
   if (contentFigure != null) {
     contentFigure.erase();
     remove(contentFigure);
   }
   contentFigure = createContentFigure(descriptor);
   add(contentFigure);
   requestFocus();
   setFocus();
   context.figureUpdated();
   navigator().refresh();
 }
Example #7
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();
   }
 }
Example #8
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();
      }
    }
  }
Example #9
0
 public Color getColor() {
   return context.getColor();
 }
Example #10
0
 public void performDefaultAction() {
   context.performDefaultAction();
 }
Example #11
0
 public String getResource() {
   return context.getResource();
 }
Example #12
0
 @Override
 public void setFocused(VisualTerm visualTerm) {
   context.setFocused(visualTerm);
 }
Example #13
0
 @Override
 public Image getImage(String imageName) throws IOException {
   return context.getImage(imageName);
 }
Example #14
0
 public Control getCanvas() {
   return context.getCanvas();
 }
Example #15
0
 public void setFocus() {
   context.setFocus();
 }
Example #16
0
 public void selectionChanged(TermFigure figure) {
   context.selectionChanged(figure);
 }
Example #17
0
 public String getPackage() {
   return context.getPackage();
 }
Example #18
0
 public IWorkbenchPart getWorkbenchPart() {
   return context.getWorkbenchPart();
 }
Example #19
0
 public Font getFont(int fontType) {
   return context.getFont(fontType);
 }
Example #20
0
 @Override
 public VisualTerm getFocused() {
   return context.getFocused();
 }
Example #21
0
 public void figureUpdated() {
   context.figureUpdated();
 }
Example #22
0
 public void unregisterTermFigure(Object termID, TermFigure figure) {
   context.unregisterTermFigure(termID, figure);
 }
Example #23
0
 public Text getTextEditor() {
   return context.getTextEditor();
 }