示例#1
1
    // TODO: make this a method of SikuliDocument, no need to pass document as argument
    private void changeIndentation(DefaultStyledDocument doc, int linenum, int columns)
        throws BadLocationException {
      PreferencesUser pref = PreferencesUser.getInstance();
      boolean expandTab = pref.getExpandTab();
      int tabWidth = pref.getTabWidth();

      if (linenum < 0) {
        throw new BadLocationException("Negative line", -1);
      }
      Element map = doc.getDefaultRootElement();
      if (linenum >= map.getElementCount()) {
        throw new BadLocationException("No such line", doc.getLength() + 1);
      }
      if (columns == 0) {
        return;
      }

      Element lineElem = map.getElement(linenum);
      int lineStart = lineElem.getStartOffset();
      int lineLength = lineElem.getEndOffset() - lineStart;
      String line = doc.getText(lineStart, lineLength);

      // determine current indentation and number of whitespace characters
      int wsChars;
      int indentation = 0;
      for (wsChars = 0; wsChars < line.length(); wsChars++) {
        char c = line.charAt(wsChars);
        if (c == ' ') {
          indentation++;
        } else if (c == '\t') {
          indentation += tabWidth;
        } else {
          break;
        }
      }

      int newIndentation = indentation + columns;
      if (newIndentation <= 0) {
        doc.remove(lineStart, wsChars);
        return;
      }

      // build whitespace string for new indentation
      StringBuilder newWs = new StringBuilder(newIndentation / tabWidth + tabWidth - 1);
      int ind = 0;
      if (!expandTab) {
        for (; ind + tabWidth <= newIndentation; ind += tabWidth) {
          newWs.append('\t');
        }
      }
      for (; ind < newIndentation; ind++) {
        newWs.append(' ');
      }
      doc.replace(lineStart, wsChars, newWs.toString(), null);
    }
示例#2
0
  private boolean replaceWithImage(int startOff, int endOff, Pattern ptn)
      throws BadLocationException {
    Document doc = getDocument();
    String imgStr = doc.getText(startOff, endOff - startOff);
    JComponent comp = null;

    if (ptn == patPatternStr || ptn == patPngStr) {
      if (pref.getPrefMoreImageThumbs()) {
        comp = EditorPatternButton.createFromString(this, imgStr, null);
      } else {
        comp = EditorPatternLabel.labelFromString(this, imgStr);
      }
    } else if (ptn == patRegionStr) {
      if (pref.getPrefMoreImageThumbs()) {
        comp = EditorRegionButton.createFromString(this, imgStr);
      } else {
        comp = EditorRegionLabel.labelFromString(this, imgStr);
      }
    } else if (ptn == patCaptureBtn) {
      comp = EditorPatternLabel.labelFromString(this, "");
    }
    if (comp != null) {
      this.select(startOff, endOff);
      this.insertComponent(comp);
      return true;
    }
    return false;
  }
示例#3
0
 private void writeSrcFile() throws IOException {
   Debug.log(3, "IDE: writeSrcFile: " + _editingFile.getName());
   writeFile(_editingFile.getAbsolutePath());
   if (PreferencesUser.getInstance().getAtSaveMakeHTML()) {
     convertSrcToHtml(getSrcBundle());
   } else {
     String snameDir = new File(_editingFile.getAbsolutePath()).getParentFile().getName();
     String sname = snameDir.replace(".sikuli", "") + ".html";
     (new File(snameDir, sname)).delete();
   }
   if (PreferencesUser.getInstance().getAtSaveCleanBundle()) {
     cleanBundle(getSrcBundle());
   }
   setDirty(false);
 }
  public EditorConsolePane() {
    super();
    textArea = new JTextPane();
    textArea.setEditorKit(new HTMLEditorKit());
    textArea.setTransferHandler(new JTextPaneHTMLTransferHandler());
    String css = PreferencesUser.getInstance().getConsoleCSS();
    ((HTMLEditorKit) textArea.getEditorKit()).getStyleSheet().addRule(css);
    textArea.setEditable(false);

    setLayout(new BorderLayout());
    add(new JScrollPane(textArea), BorderLayout.CENTER);

    if (ENABLE_IO_REDIRECT) {
      Debug.log(3, "EditorConsolePane: starting redirection to message area");
      int npipes = 2;
      NUM_PIPES = npipes * ScriptRunner.scriptRunner.size();
      pin = new PipedInputStream[NUM_PIPES];
      reader = new Thread[NUM_PIPES];
      for (int i = 0; i < NUM_PIPES; i++) {
        pin[i] = new PipedInputStream();
      }

      int irunner = 0;
      for (IScriptRunner srunner : ScriptRunner.scriptRunner.values()) {
        Debug.log(3, "EditorConsolePane: redirection for %s", srunner.getName());
        if (srunner.doSomethingSpecial(
            "redirect", Arrays.copyOfRange(pin, irunner * npipes, irunner * npipes + 2))) {
          Debug.log(3, "EditorConsolePane: redirection success for %s", srunner.getName());
          quit = false; // signals the Threads that they should exit
          // TODO Hack to avoid repeated redirect of stdout/err
          ScriptRunner.systemRedirected = true;

          // Starting two seperate threads to read from the PipedInputStreams
          for (int i = irunner * npipes; i < irunner * npipes + npipes; i++) {
            reader[i] = new Thread(this);
            reader[i].setDaemon(true);
            reader[i].start();
          }
          irunner++;
        }
      }
    }

    // Create the popup menu.
    popup = new JPopupMenu();
    JMenuItem menuItem = new JMenuItem("Clear messages");
    // Add ActionListener that clears the textArea
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            textArea.setText("");
          }
        });
    popup.add(menuItem);

    // Add listener to components that can bring up popup menus.
    MouseListener popupListener = new PopupListener(popup);
    textArea.addMouseListener(popupListener);
  }
 public static String getFilenameFromImage(BufferedImage img) {
   TextRecognizer tr = TextRecognizer.getInstance();
   if (!PreferencesUser.getInstance().getPrefMoreTextOCR() || tr == null) {
     return "";
   }
   String text = tr.recognize(img);
   text = text.replaceAll("\\W", "");
   if (text.length() > MAX_OCR_TEXT_LENGTH) {
     return text.substring(0, MAX_OCR_TEXT_LENGTH);
   }
   return text;
 }
示例#6
0
    public void actionPerformed(JTextComponent text) {
      indentationLogic = ((EditorPane) text).getIndentationLogic();
      boolean indentError = false;
      Document doc = text.getDocument();
      Element map = doc.getDefaultRootElement();
      String tabWhitespace = PreferencesUser.getInstance().getTabWhitespace();
      Caret c = text.getCaret();
      int dot = c.getDot();
      int mark = c.getMark();
      int dotLine = map.getElementIndex(dot);
      int markLine = map.getElementIndex(mark);

      if (dotLine != markLine) {
        int first = Math.min(dotLine, markLine);
        int last = Math.max(dotLine, markLine);
        Element elem;
        int start;
        try {
          for (int i = first; i < last; i++) {
            elem = map.getElement(i);
            start = elem.getStartOffset();
            doc.insertString(start, tabWhitespace, null);
          }
          elem = map.getElement(last);
          start = elem.getStartOffset();
          if (Math.max(c.getDot(), c.getMark()) != start) {
            doc.insertString(start, tabWhitespace, null);
          }
        } catch (BadLocationException ble) {
          Debug.error(me + "Problem while indenting line\n%s", ble.getMessage());
          UIManager.getLookAndFeel().provideErrorFeedback(text);
        }
      } else {
        text.replaceSelection(tabWhitespace);
      }
    }
示例#7
0
  public void initBeforeLoad(String scriptType, boolean reInit) {
    String scrType = null;
    boolean paneIsEmpty = false;

    if (scriptType == null) {
      scriptType = Settings.EDEFAULT;
      paneIsEmpty = true;
    }

    if (Settings.EPYTHON.equals(scriptType)) {
      scrType = Settings.CPYTHON;
      _indentationLogic = SikuliIDE.getIDESupport(scriptType).getIndentationLogic();
      _indentationLogic.setTabWidth(pref.getTabWidth());
    } else if (Settings.ERUBY.equals(scriptType)) {
      scrType = Settings.CRUBY;
      _indentationLogic = null;
    }

    if (scrType != null) {
      sikuliContentType = scrType;
      editorKit = new SikuliEditorKit();
      editorViewFactory = (EditorViewFactory) editorKit.getViewFactory();
      setEditorKit(editorKit);
      setContentType(scrType);

      if (_indentationLogic != null) {
        pref.addPreferenceChangeListener(
            new PreferenceChangeListener() {
              @Override
              public void preferenceChange(PreferenceChangeEvent event) {
                if (event.getKey().equals("TAB_WIDTH")) {
                  _indentationLogic.setTabWidth(Integer.parseInt(event.getNewValue()));
                }
              }
            });
      }
    }

    initKeyMap();

    if (transferHandler == null) {
      transferHandler = new MyTransferHandler();
    }
    setTransferHandler(transferHandler);
    _highlighter = new EditorCurrentLineHighlighter(this);

    addCaretListener(_highlighter);

    setFont(new Font(pref.getFontName(), Font.PLAIN, pref.getFontSize()));
    setMargin(new Insets(3, 3, 3, 3));
    setBackground(Color.WHITE);
    if (!Settings.isMac()) {
      setSelectionColor(new Color(170, 200, 255));
    }

    updateDocumentListeners();

    addKeyListener(this);
    addCaretListener(this);
    popMenuImage = new SikuliIDEPopUpMenu("POP_IMAGE", this);
    if (!popMenuImage.isValidMenu()) {
      popMenuImage = null;
    }

    if (paneIsEmpty || reInit) {
      //			this.setText(String.format(Settings.TypeCommentDefault, getSikuliContentType()));
      this.setText("");
    }
    SikuliIDE.getStatusbar().setCurrentContentType(getSikuliContentType());
    Debug.log(3, "InitTab: (%s)", getSikuliContentType());
    if (!Settings.hasTypeRunner(getSikuliContentType())) {
      SikuliX.popup(
          "No installed runner supports ("
              + getSikuliContentType()
              + ")\n"
              + "Trying to run the script will crash IDE!",
          "... serious problem detected!");
    }
  }
示例#8
0
 // <editor-fold defaultstate="collapsed" desc="Initialization">
 public EditorPane(SikuliIDE ide) {
   pref = PreferencesUser.getInstance();
   showThumbs = !pref.getPrefMorePlainText();
 }