public boolean openHyperLink() {
    String localiz = getLocalization();
    if (localiz == null) {
      return false;
    } else if (fBase.getUnderlyingResource() == null) {
      return false;
    } else if (fElement.length() == 0 || fElement.charAt(0) != '%') {
      return false;
    }

    IProject proj = fBase.getUnderlyingResource().getProject();
    IFile file = proj.getFile(localiz + ".properties"); // $NON-NLS-1$
    if (!file.exists()) return false;

    try {
      IEditorPart editor = IDE.openEditor(PDEPlugin.getActivePage(), file);
      if (!(editor instanceof TextEditor)) return false;
      TextEditor tEditor = (TextEditor) editor;
      IDocument doc = tEditor.getDocumentProvider().getDocument(tEditor.getEditorInput());
      if (doc == null) return false;

      try {
        String key = fElement.substring(1);
        int keyLen = key.length();
        int length = doc.getLength();
        int start = 0;
        IRegion region = null;
        FindReplaceDocumentAdapter docSearch = new FindReplaceDocumentAdapter(doc);
        while ((region = docSearch.find(start, key, true, false, false, false)) != null) {
          int offset = region.getOffset();
          if (offset > 0) {
            // check for newline before
            char c = doc.getChar(offset - 1);
            if (c != '\n' && c != '\r') {
              start += keyLen;
              continue;
            }
          }
          if (offset + keyLen < length) {
            // check for whitespace / assign symbol after
            char c = doc.getChar(offset + keyLen);
            if (!Character.isWhitespace(c) && c != '=' && c != ':') {
              start += keyLen;
              continue;
            }
          }
          tEditor.selectAndReveal(offset, keyLen);
          break;
        }
      } catch (BadLocationException e) {
        PDEPlugin.log(e);
      }

    } catch (PartInitException e) {
      return false;
    }
    return true;
  }
Пример #2
0
  /** @see org.eclipse.jface.wizard.IWizard#performFinish() */
  public boolean performFinish() {
    IContainer dest = dest();
    if (dest == null) {
      mainPage.setErrorMessage("Must select an existing destination folder.");
      return false;
    }
    String name = name();
    String suffix = suffix();
    if (name.length() == 0) {
      mainPage.setErrorMessage("Empty file name.");
      return false;
    }
    if (!Character.isJavaIdentifierStart(name.charAt(0))) {
      mainPage.setErrorMessage(
          "Invalid character \'"
              + name.charAt(0)
              + "\' at index "
              + 0
              + " for Clojure namespace file \'"
              + name
              + "'");
      return false;
    }
    for (int i = 1; i < name.length(); i++)
      if (!Character.isJavaIdentifierPart(name.charAt(i))) {
        mainPage.setErrorMessage(
            "Invalid character \'"
                + name.charAt(i)
                + "\' at index "
                + i
                + " for Clojure namespace file \'"
                + name
                + "'");
        return false;
      }

    // check if file already exists.
    IFile file;
    if (mainPage.dest instanceof IProject) {
      file = ((IProject) mainPage.dest).getFile(name + suffix);
    } else if (mainPage.dest instanceof IFolder) {
      file = ((IFolder) mainPage.dest).getFile(name + suffix);
    } else {
      return false;
    }

    if (file.exists()) {
      mainPage.setErrorMessage("File " + file.getName() + " already exists.");
      return false;
    }

    try {
      String namespace =
          ((StringUtils.isEmpty(mainPage.packageName) ? "" : mainPage.packageName + ".") + name)
              .replaceAll("_", "-");
      String contents = "(ns " + namespace + ")\n\n";
      file.create(new StringBufferInputStream(contents), true, null);
      IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
      if (dw != null) {
        IWorkbenchPage page = dw.getActivePage();
        if (page != null) {
          TextEditor editor = (TextEditor) IDE.openEditor(page, file, true);
          editor.selectAndReveal(contents.length(), 0);
        }
      }
    } catch (CoreException e) {
      CCWPlugin.logError(e);
      return false;
    }

    return true;
  }