Exemple #1
0
  private void search(int direction) {
    String searchText = searchField.getText();
    if (searchText == null || searchText.length() == 0 || rTextArea.getText() == null) {
      return;
    }

    boolean forward = direction >= 0;
    boolean matchCase = matchCaseCB.isSelected();
    boolean regex = regexCB.isSelected();
    boolean wholeWord = wholeWordCB.isSelected();

    if (markAllCB.isSelected()) {
      rTextArea.markAll(searchText, matchCase, wholeWord, regex);
    } else {
      rTextArea.clearMarkAllHighlights();
    }

    SearchContext context = new SearchContext();
    context.setSearchFor(searchText);
    context.setMatchCase(matchCase);
    context.setRegularExpression(regex);
    context.setSearchForward(forward);
    context.setWholeWord(wholeWord);

    // TODO hack: move cursor before previous search for not jump to next occurrence
    if (direction == 0 && !COLOR_BG_ERROR.equals(searchField.getBackground())) {
      try {
        int caretPos = rTextArea.getCaretPosition();
        int lineNum = rTextArea.getLineOfOffset(caretPos) - 1;
        if (lineNum > 1) {
          rTextArea.setCaretPosition(rTextArea.getLineStartOffset(lineNum));
        }
      } catch (BadLocationException e) {
        LOG.error("Caret move error", e);
      }
    }

    boolean found = SearchEngine.find(rTextArea, context);
    if (!found) {
      int pos =
          SearchEngine.getNextMatchPos(
              searchText, rTextArea.getText(), forward, matchCase, wholeWord);
      if (pos != -1) {
        rTextArea.setCaretPosition(forward ? 0 : rTextArea.getDocument().getLength() - 1);
        search(direction);
        searchField.setBackground(COLOR_BG_WARN);
        return;
      }
      searchField.setBackground(COLOR_BG_ERROR);
    } else {
      searchField.setBackground(COLOR_BG_NORMAL);
    }
  }
 /**
  * Marque une ligne du texte avec une icône.
  *
  * @param line Numéro de la ligne du texte.
  * @param icon Nom de l'icône à utiliser (une icône indiquant une erreur par défaut).
  */
 public void signalLine(int line, String icon) {
   Gutter gutter = scrollPane.getGutter();
   gutter.setBookmarkingEnabled(true);
   try {
     textArea.setCaretPosition(textArea.getLineStartOffset(line - 1));
     scrollPane
         .getGutter()
         .addLineTrackingIcon(
             line - 1,
             Macros.getIcon(icon == null ? "org/javascool/widgets/icons/error.png" : icon));
   } catch (Exception e) {
   }
 }
 /** Construit un panneau d'édition. */
 public TextEditor() {
   setLayout(new BorderLayout());
   // Creation de la barre de commande
   {
     toolBar = new ToolBar();
     add(toolBar, BorderLayout.NORTH);
   }
   // Creation de la zone d'édition
   {
     textArea = new RSyntaxTextArea(25, 70);
     textArea.setCaretPosition(0);
     textArea.requestFocusInWindow();
     textArea.setMarkOccurrences(true);
     textArea.setText("");
     scrollPane = new RTextScrollPane(textArea, true);
     Gutter gutter = scrollPane.getGutter();
     gutter.setBorderColor(Color.BLUE);
     add(scrollPane, BorderLayout.CENTER);
   }
   // Définition du mécanisme de complétion
   {
     completionsProvider = new DefaultCompletionProvider();
     LanguageAwareCompletionProvider lacp =
         new LanguageAwareCompletionProvider(completionsProvider);
     AutoCompletion ac =
         new AutoCompletion(lacp) {
           @Override
           public void doCompletion() {
             if (isAutoCompleteEnabled()) {
               super.doCompletion();
             }
           }
         };
     ac.install(textArea);
     ac.setAutoCompleteSingleChoices(false);
     ac.setAutoActivationEnabled(true);
     ac.setAutoActivationDelay(1500);
     ac.setShowDescWindow(true);
   }
   // Ajout de l'aide à l'édition
   {
     JPopupMenu j = toolBar.addRightTool("Aide");
     j.add(new JLabel("<html>\n<b>Commandes d'édition</b><br><table>\n" + helpText + "</table>"));
   }
 }
  /**
   * Sets whether this <code>Fold</code> is collapsed. Calling this method will update both the text
   * area and all <code>Gutter</code> components.
   *
   * @param collapsed Whether this fold should be collapsed.
   * @see #isCollapsed()
   * @see #toggleCollapsedState()
   */
  public void setCollapsed(boolean collapsed) {

    if (collapsed != this.collapsed) {

      // Change our fold state and cached info about folded line count.
      int lineCount = getLineCount();
      int linesToCollapse = lineCount - childCollapsedLineCount;
      if (!collapsed) { // If we're expanding
        linesToCollapse = -linesToCollapse;
      }
      // System.out.println("Hiding lines: " + linesToCollapse +
      //		" (" + lineCount + ", " + linesToCollapse + ")");
      this.collapsed = collapsed;
      if (parent != null) {
        parent.updateChildCollapsedLineCount(linesToCollapse);
      }

      // If an end point of the selection is being hidden, move the caret
      // "out" of the fold.
      if (collapsed) {
        int dot = textArea.getSelectionStart(); // Forgive variable name
        Element root = textArea.getDocument().getDefaultRootElement();
        int dotLine = root.getElementIndex(dot);
        boolean updateCaret = containsLine(dotLine);
        if (!updateCaret) {
          int mark = textArea.getSelectionEnd();
          if (mark != dot) {
            int markLine = root.getElementIndex(mark);
            updateCaret = containsLine(markLine);
          }
        }
        if (updateCaret) {
          dot = root.getElement(getStartLine()).getEndOffset() - 1;
          textArea.setCaretPosition(dot);
        }
      }

      textArea.foldToggled(this);
    }
  }
Exemple #5
0
 public void setText(String text, boolean resetCaret) {
   xmlViewTA.setText(text);
   if (resetCaret) {
     xmlViewTA.setCaretPosition(0);
   }
 }
  public void setContents() {
    String name = this.name.toLowerCase();
    panelArea.setCodeFoldingEnabled(true);
    panelArea.setAntiAliasingEnabled(true);
    RTextScrollPane scrollPane = new RTextScrollPane(panelArea);
    panelArea.addKeyListener(
        new KeyListener() {
          public void keyPressed(KeyEvent e) {
            if ((e.getKeyCode() == KeyEvent.VK_F)
                && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
              field.requestFocus();
            }

            BytecodeViewer.checkHotKey(e);
          }

          @Override
          public void keyReleased(KeyEvent arg0) {}

          @Override
          public void keyTyped(KeyEvent arg0) {}
        });

    String contentsS = new String(contents);

    if (!isPureAscii(contentsS)) {
      if (name.endsWith(".png")
          || name.endsWith(".jpg")
          || name.endsWith(".jpeg")
          || name.endsWith(".gif")
          || name.endsWith(".tif")
          || name.endsWith(".bmp")) {
        canRefresh = true;
        try {
          image = ImageIO.read(new ByteArrayInputStream(contents)); // gifs fail cause of this
          JLabel label = new JLabel("", new ImageIcon(image), JLabel.CENTER);
          panel2.add(label, BorderLayout.CENTER);
          panel2.addMouseWheelListener(
              new MouseWheelListener() {
                @Override
                public void mouseWheelMoved(MouseWheelEvent e) {
                  int notches = e.getWheelRotation();
                  if (notches < 0) {
                    image =
                        Scalr.resize(
                            image,
                            Scalr.Method.SPEED,
                            image.getWidth() + 10,
                            image.getHeight() + 10);
                  } else {
                    image =
                        Scalr.resize(
                            image,
                            Scalr.Method.SPEED,
                            image.getWidth() - 10,
                            image.getHeight() - 10);
                  }
                  panel2.removeAll();
                  JLabel label = new JLabel("", new ImageIcon(image), JLabel.CENTER);
                  panel2.add(label, BorderLayout.CENTER);
                  panel2.updateUI();
                }
              });
          return;
        } catch (Exception e) {
          new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
        }
      } else {
        JHexEditor hex = new JHexEditor(contents);
        panel2.add(hex);
        return;
      }
    }

    if (name.endsWith(".xml") || contentsS.startsWith("<?xml") || contentsS.startsWith(("<xml"))) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".py") || name.endsWith(".python")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".rb") || name.endsWith(".ruby")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_RUBY);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".java")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".html")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_HTML);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".css")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CSS);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".properties") || name.endsWith(".mf") || name.endsWith(".sf")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".php") || contentsS.startsWith("<?php")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PHP);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".js")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".bat")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_WINDOWS_BATCH);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".sh")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".c")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_C);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".cpp")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CPLUSPLUS);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".scala")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SCALA);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".clojure")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CLOJURE);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".groovy")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_GROOVY);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".lua")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_LUA);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".sql")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".json")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
      panelArea.setText(contentsS);
    } else if (name.endsWith(".jsp")) {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSP);
      panelArea.setText(contentsS);
    } else {
      panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
      panelArea.setText(contentsS);
    }

    panelArea.setCaretPosition(0);
    scrollPane.setColumnHeaderView(panel);
    panel2.add(scrollPane);
  }
  private Component createEntryViewer(JarEntry entry) {
    if (entry.getType().equals(JarEntryType.classEntry)
        || entry.getType().equals(JarEntryType.javaEntry)
        || entry.getType().equals(JarEntryType.fileEntry)) {
      String ext = FileUtils.getFileExtPart(entry.getName(), false).toLowerCase();
      if (GlobalConstants.IMAGE_FORMATS.contains(ext)) {
        // todo A better image viewer (actually a new component - WebImageViewer)

        // Image file viewer
        WebImage image = new WebImage();
        image.setIcon(ImageUtils.loadImage(getEntryInputStream(entry)));

        // Image scroll
        WebScrollPane imageScroll = new WebScrollPane(image, false);
        imageScroll.setVerticalScrollBarPolicy(WebScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        imageScroll.setHorizontalScrollBarPolicy(WebScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        return imageScroll;
      } else {
        // Source code viewer
        final RSyntaxTextArea source = new RSyntaxTextArea();

        // Syntax style
        boolean libraryCode = false;
        if (ext.equals("java") || ext.equals("class")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
          libraryCode = true;
        } else if (ext.equals("xml")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
        } else if (ext.equals("html")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_HTML);
        } else if (ext.equals("css")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CSS);
        } else if (ext.equals("js")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
        } else if (ext.equals("php")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PHP);
        } else if (ext.equals("sql")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
        } else {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
        }

        // Settings
        source.setEditable(false);
        source.setMargin(new Insets(0, 5, 0, 0));
        source.setAntiAliasingEnabled(true);
        source.setUseFocusableTips(true);
        source.setTabSize(4);
        // source.setLineWrap ( true );
        // source.setWrapStyleWord ( true );
        source.setCodeFoldingEnabled(allowCodeFolding.isSelected());
        source.setPaintTabLines(paintTabLines.isSelected());
        source.setWhitespaceVisible(showWhitespaces.isSelected());
        source.setEOLMarkersVisible(showEol.isSelected());
        source.addHyperlinkListener(
            new HyperlinkListener() {
              @Override
              public void hyperlinkUpdate(HyperlinkEvent e) {
                WebUtils.browseSiteSafely(e.getURL().toExternalForm());
              }
            });
        ((RSyntaxTextAreaHighlighter) source.getHighlighter()).setDrawsLayeredHighlights(false);

        // Source code
        source.setText(libraryCode ? loadSource(entry) : loadString(entry));
        source.setCaretPosition(0);

        // "Jump to source"-like action
        source.addMouseListener(
            new MouseAdapter() {
              @Override
              public void mousePressed(MouseEvent e) {
                // todo Fix when clicked in class "MyName" on string "MyName"
                // Additional feature to dive into related classes
                if (SwingUtilities.isMiddleMouseButton(e)
                    || SwingUtils.isCtrl(e) && SwingUtilities.isLeftMouseButton(e)) {
                  int pos = source.getUI().viewToModel(source, e.getPoint());
                  String word = TextUtils.getWord(source.getText(), pos);
                  if (word != null) {
                    JarEntry classByName = jarStructure.findEntryByName(word);
                    if (classByName != null
                        && (classByName.getType().equals(JarEntryType.classEntry)
                            || classByName.getType().equals(JarEntryType.javaEntry))) {
                      updateClassPath(classByName, true);
                    }
                  }
                }
              }
            });

        // Saving opened editor
        synchronized (activeEditorsLock) {
          activeEditors.put(entry, source);
        }

        // Special code viewer scroll pane
        RTextScrollPane sourceScroll = new RTextScrollPane(source);
        sourceScroll.setVerticalScrollBarPolicy(WebScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        ((WebScrollPaneUI) sourceScroll.getUI()).setDrawBorder(false);

        // Source code viewer theme
        loadTheme(theme.getSelectedItem().toString().toLowerCase(), source);

        return sourceScroll;
      }
    }
    return new WebLabel();
  }