Home
Categories
Dictionary
Download
Project Details
Changes Log
License

Gutter



The Gutter class handles the Gutter component in the Code Editor component.
gutter

Gutter customization

Setting a Gutter handler

It is possible to customize the gutter, as for example:
  • Add additional images or drawings in the gutter alongside the line numbers
  • Show a Popup by right clicking on the gutter
This is achieved by implementing the GutterHandler interface.

For example:
      JEditor ta = new JEditor(defaults);
      ta.setGutterHandler(new TestGutterHandler());      
and:
      public class MyGutterHandler implements GutterHandler {
        private Image image = null;

        public MyGutterHandler() {
          URL url = thi.getClass().getResource("breakpoint.png");
          try {
            image = ImageIO.read(url);
          } catch (IOException ex) {
          }
        }

        @Override
        public boolean isPainting(int line) {
          // only the line of number 103 will have the image
          return line == 103;
        }

        @Override
        public boolean handleClick(Gutter gutter, int mouseButton, int x, int y) {
          JPopupMenu menu = new JPopupMenu();
          int number = gutter.getLineNumber(y);
          menu.add(new JMenuItem("An Action on " + number));
          menu.add(new JMenuItem("Another Action"));
          menu.show(gutter, x, y);
          return true;
        }

        @Override
        public void paint(Graphics gfx, int line, int x, int y) {
          gfx.drawImage(image, x - 16, y - 10, null);
        }
      }
We will have the following result:
customizegutter

Abstract Gutter handler

The AbstractGutterHandler is a GutterHandler which allows to:
  • Show a specific image on specific lines
  • Have a custom behavior upon clicking on the Gutter
  • Have a custom behavior upon hovering on a line the gutter, such as for example showing a tooltip
For example, the following handler:
  • Handles a right-click on the gutter and shows a menu for each line
  • Shows a tooltip on lines which paint an image
For example:
      JEditor ta = new JEditor(defaults);
      MyGutterHandler handler = new MyGutterHandler();
      URL debugURL = ...
      handler.addImageResource("debug", debugURL);
      handler.addImage(103, "debug");
      ta.setGutterHandler(handler);      
and:
      private class MyGutterHandler extends AbstractGutterHandler {
        public boolean handleClick(Gutter gutter, int mouseButton, int x, int y) {
          if (mouseButton == MouseEvent.BUTTON3) {
            JPopupMenu menu = new JPopupMenu();
            int number = gutter.getLineNumber(y);
            menu.add(new JMenuItem("An Action on " + number));
            menu.show(gutter, x, y);
            return true;
          } else {
            return false;
          }
        }

        public void handleMoved(Gutter gutter, int x, int y) {
          int number = gutter.getLineNumber(y);
          if (isPainting(number)) {
            this.showTooltip("The tooltip on line" + number);
          } else {
            this.hideTooltip();
          }
        }
      }

See also


Categories: component

Copyright 2016-2019 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence