Esempio n. 1
0
  public void testBufferWithLargeFile() throws Exception {
    final char[] chars = "0123456789abcdef".toCharArray();
    final char[] manyChars = new char[10000];

    for (int i = 0; i < manyChars.length; ++i) {
      manyChars[i] = chars[i % chars.length];
    }

    final String s0 = new String(manyChars);
    final StringTextSource textSource = new StringTextSource(s0);
    assertSame(s0, textSource.getText());

    final File file = new File(getDirectory(), "myfile.txt");

    final Buffer buffer = new BufferImplementation(s_resources, textSource, file);

    assertEquals(Buffer.TEXT_BUFFER, buffer.getType());
    assertTrue(!buffer.isDirty());
    assertTrue(!buffer.isUpToDate());
    assertEquals(file, buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());

    buffer.save();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertSame(s0, textSource.getText());

    buffer.load();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertEquals(canonicaliseLineEndings(s0), textSource.getText());
    assertNotSame(s0, textSource.getText());
  }
Esempio n. 2
0
  public void testBufferWithAssociatedFile() throws Exception {

    final String s0 = "A shield for your eyes\na beast in the well on your hand";

    final String s1 =
        "Catch the mean beast\nin the well in the hell on the back\n"
            + "Watch out! You've got no shield\n"
            + "Break up! He's got no peace";

    final StringTextSource textSource = new StringTextSource(s0);
    assertSame(s0, textSource.getText());

    final File file = new File(getDirectory(), "myfile.txt");

    final Buffer buffer = new BufferImplementation(s_resources, textSource, file);

    assertEquals(Buffer.TEXT_BUFFER, buffer.getType());
    assertTrue(!buffer.isDirty());
    assertTrue(!buffer.isUpToDate());
    assertEquals(file, buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());

    buffer.save();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());

    assertSame(s0, textSource.getText());

    textSource.setText(s1);
    textSource.markDirty();

    assertTrue(buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertSame(s1, textSource.getText());

    buffer.load();

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertEquals(canonicaliseLineEndings(s0), textSource.getText());
    assertNotSame(s0, textSource.getText());

    // Add an error margin, as Linux does not support setting the modification
    // date with millisecond precision.
    assertTrue(file.setLastModified(System.currentTimeMillis() + 1000));

    assertTrue(!buffer.isUpToDate());

    buffer.load();

    assertTrue(buffer.isUpToDate());
    assertEquals(textSource, buffer.getTextSource());
  }
Esempio n. 3
0
  public void testBufferWithBadAssociatedFile() throws Exception {

    final StringTextSource textSource = new StringTextSource("");

    final Buffer buffer = new BufferImplementation(s_resources, textSource, getDirectory());

    try {
      buffer.load();
      fail("Expected DisplayMessageConsoleException");
    } catch (DisplayMessageConsoleException e) {
      assertTrue(e.getCause() instanceof IOException);
    }

    try {
      buffer.save();
      fail("Expected DisplayMessageConsoleException");
    } catch (DisplayMessageConsoleException e) {
      assertTrue(e.getCause() instanceof IOException);
    }
  }
Esempio n. 4
0
  public void testBufferWithNoFile() throws Exception {
    final String text = "Some text for testing with";

    final TextSource textSource = new StringTextSource(text);

    assertEquals(text, textSource.getText());

    final Buffer buffer = new BufferImplementation(s_resources, textSource, "My Buffer");

    assertNotNull(textSource.getText());
    assertEquals(text, textSource.getText());
    assertEquals("My Buffer", buffer.getDisplayName());
    assertTrue(buffer.toString().indexOf(buffer.getDisplayName()) >= 0);

    try {
      buffer.load();
      fail("Expected EditorException");
    } catch (EditorException e) {
    }

    try {
      buffer.save();
      fail("Expected EditorException");
    } catch (EditorException e) {
    }

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertNull(buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());

    assertEquals(Buffer.UNKNOWN_BUFFER, buffer.getType());

    assertTrue(!buffer.isDirty());
    assertTrue(buffer.isUpToDate());
    assertNull(buffer.getFile());
    assertEquals(textSource, buffer.getTextSource());
  }
  /**
   * Replaces all occurrences of the search string with the replacement string.
   *
   * @param view The view
   * @param dontOpenChangedFiles Whether to open changed files or to autosave them quietly
   * @return the number of modified files
   */
  public static boolean replaceAll(View view, boolean dontOpenChangedFiles) {
    // component that will parent any dialog boxes
    Component comp = SearchDialog.getSearchDialog(view);
    if (comp == null) comp = view;

    if (fileset.getFileCount(view) == 0) {
      GUIUtilities.error(comp, "empty-fileset", null);
      return false;
    }

    record(view, "replaceAll(view)", true, true);

    view.showWaitCursor();

    boolean smartCaseReplace = getSmartCaseReplace();

    int fileCount = 0;
    int occurCount = 0;
    try {
      SearchMatcher matcher = getSearchMatcher();
      if (matcher == null) return false;

      initReplace();

      String path = fileset.getFirstFile(view);
      loop:
      while (path != null) {
        Buffer buffer = jEdit.openTemporary(view, null, path, false);

        /* this is stupid and misleading.
         * but 'path' is not used anywhere except
         * the above line, and if this is done
         * after the 'continue', then we will
         * either hang, or be forced to duplicate
         * it inside the buffer == null, or add
         * a 'finally' clause. you decide which one's
         * worse. */
        path = fileset.getNextFile(view, path);

        if (buffer == null) continue loop;

        // Wait for buffer to finish loading
        if (buffer.isPerformingIO()) VFSManager.waitForRequests();

        if (!buffer.isEditable()) continue loop;

        // Leave buffer in a consistent state if
        // an error occurs
        int retVal = 0;

        try {
          buffer.beginCompoundEdit();
          retVal = _replace(view, buffer, matcher, 0, buffer.getLength(), smartCaseReplace);
        } finally {
          buffer.endCompoundEdit();
        }

        if (retVal != 0) {
          fileCount++;
          occurCount += retVal;
          if (dontOpenChangedFiles) {
            buffer.save(null, null);
          } else {
            jEdit.commitTemporary(buffer);
            jEdit.getBufferSetManager().addBuffer(view, buffer);
          }
        }
      }
    } catch (Exception e) {
      handleError(comp, e);
    } finally {
      view.hideWaitCursor();
    }

    /* Don't do this when playing a macro, cos it's annoying */
    if (!BeanShell.isScriptRunning()) {
      Object[] args = {Integer.valueOf(occurCount), Integer.valueOf(fileCount)};
      view.getStatus().setMessageAndClear(jEdit.getProperty("view.status.replace-all", args));
      if (occurCount == 0) view.getToolkit().beep();
    }

    return (fileCount != 0);
  } // }}}