org.openide.cookies.EditorCookie类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.7k)|赞(0)|评价(0)|浏览(125)

本文整理了Java中org.openide.cookies.EditorCookie类的一些代码示例,展示了EditorCookie类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EditorCookie类的具体详情如下:
包路径:org.openide.cookies.EditorCookie
类名称:EditorCookie

EditorCookie介绍

[英]Cookie defining standard operations with a text document and an editor that can display it. The cookie extends LineCookie because all implementations of editors should support access by lines.

The cookie provides interfaces for opening the file, closing the editor, background loading, saving of the document, and notification of modification.

Warning: it is not guaranteed that the document returned from this cookie will persist for the full lifetime of the cookie. That is, if the editor window is closed and then reopened, it is possible for the document to change. The EditorCookie.Observable allows listening to changes of the state of the document. You should do this if you are listening to changes in the document itself, as otherwise you would get no notifications from a reopened document.
[中]Cookie使用文本文档和可显示文本文档的编辑器定义标准操作。cookie扩展了LineCookie,因为所有编辑器的实现都应该支持按行访问。
cookie提供用于打开文件、关闭编辑器、后台加载、保存文档以及通知修改的接口。
警告:不能保证从此cookie返回的文档在cookie的整个生命周期内保持不变。也就是说,如果编辑器窗口关闭然后重新打开,文档可能会发生更改。EditorCookie.Observable允许监听文档状态的更改。如果您正在侦听文档本身的更改,则应该执行此操作,否则您将不会从重新打开的文档中收到任何通知。

代码示例

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-apisupport-wizards

private static BaseDocument getDocument(final FileObject fo) throws DataObjectNotFoundException, IOException {
  BaseDocument doc = null;
  DataObject dObj = DataObject.find(fo);
  if (dObj != null) {
    EditorCookie editor = dObj.getLookup().lookup(EditorCookie.class);
    if (editor != null) {
      doc = (BaseDocument) editor.openDocument();
    }
  }
  return doc;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-groovy-editor

dobj = DataObject.find(fileObject);
EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
  throw new IOException("Can't open " + fileObject.getNameExt());
  document = ec.openDocument();
} else {
  document = ec.getDocument();
    Exceptions.printStackTrace(ex);
Exceptions.printStackTrace(ioe);

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-maven-junit

@Override
  public void run() {
    try {
      EditorCookie ed = dob.getLookup().lookup(EditorCookie.class);
      if (ed != null && /* not true e.g. for *_ja.properties */
          file == dob.getPrimaryFile()) {
        if (lineNum == -1) {
          // OK, just open it.
          ed.open();
        } else {
          ed.openDocument();//XXX getLineSet doesn't do it for you
          try {
            Line l = ed.getLineSet().getOriginal(lineNum - 1);
            if (!l.isDeleted()) {
              l.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS);
            }
          } catch (IndexOutOfBoundsException ioobe) {
            // Probably harmless. Bogus line number.
            ed.open();
          }
        }
      } else {
        java.awt.Toolkit.getDefaultToolkit().beep();
      }
    } catch (Exception ex2) {
      // XXX see above, should not be necessary to call openDocument
      // at all
    }
  }
});

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project

public boolean save() throws IOException {
  if (editorCookie == null) {
    return false;
  }
  if (editorCookie.isModified()) {
    editorCookie.saveDocument();
    return true;
  }
  return false;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-tomcat5

public static void updateDocument(DataObject dobj, org.w3c.dom.Document doc)
  throws javax.swing.text.BadLocationException, java.io.IOException {
  org.openide.cookies.EditorCookie editor = (EditorCookie)dobj.getCookie(EditorCookie.class);
  javax.swing.text.Document textDoc = editor.getDocument();
  if (textDoc == null) {
    textDoc = editor.openDocument();
  }
  TomcatInstallUtil.updateDocument(textDoc,TomcatInstallUtil.getDocumentText(doc),"<Server"); //NOI18N
  SaveCookie savec = (SaveCookie) dobj.getCookie(SaveCookie.class);
  if (savec!=null) savec.save();
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-modelutil

/**
 * opens document even if it is very big by silently confirming open
 * @param cookie
 * @return
 */
public static StyledDocument openDocument(EditorCookie cookie) {
  if (cookie == null) {
    return null;
  }
  StyledDocument document = null;
  try {
    try {
      document = cookie.openDocument();
    } catch (UserQuestionException e) {
      e.confirmed();
      document = cookie.openDocument();
    }
  } catch(UserQuestionException e) {
    // no need to report
  } catch (IOException ex) {
    Exceptions.printStackTrace(ex);
  }
  cookie.prepareDocument().waitFinished();
  return document;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-editor-global-format

for (PathRecognizer pr : Lookup.getDefault().lookupAll(PathRecognizer.class)) {
  Set<String> ids = pr.getSourcePathIds();
Set<FileObject> nonRecursiveRoots = new HashSet<FileObject>();
for (DataObject d : context.lookupAll(DataObject.class)) {
  if (cancel.get()) return;
  if (nonRecursiveRoots.contains(d.getPrimaryFile())) continue;
  addRecursivelly(d.getPrimaryFile(), toFormat, sourceIds, null, null, cancel);
  if (cancel.get()) break ;
  try {
    DataObject d = DataObject.find(current);
    EditorCookie ec = d.getLookup().lookup(EditorCookie.class);
      sd = ec.openDocument();
    } catch (UserQuestionException uqe) {
      uqe.confirmed();
      sd = ec.openDocument();
    ec.saveDocument();
  } catch (UserQuestionException uqe) {
    uqe.confirmed();
  } catch (IOException ex) {
    Exceptions.printStackTrace(ex);
  } finally {
    handle.progress(++done);

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-ruby-railsprojects

static Document getDatabaseYml(FileObject projectDir) {
  FileObject fo = projectDir.getFileObject("config/database.yml"); // NOI18N
  if (fo == null) {
    return null;
  }
  try {
    DataObject dobj = DataObject.find(fo);
    EditorCookie ec = dobj.getCookie(EditorCookie.class);
    if (ec != null) {
      try {
        return ec.openDocument();
      } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
      }
    }
  } catch (DataObjectNotFoundException donfe) {
    Exceptions.printStackTrace(donfe);
  }
  return null;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf

public static void reformat(DataObject dob) {
  try {
    EditorCookie ec = dob.getLookup().lookup(EditorCookie.class);
    if (ec == null) {
      return;
    final StyledDocument doc = ec.openDocument();
    final Reformat reformat = Reformat.get(doc);
      Exceptions.printStackTrace(ex);
    } finally {
      reformat.unlock();
      ec.saveDocument();
    Exceptions.printStackTrace(ex);

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-modelimpl

public static FileBuffer createFileBuffer(FileObject fo) {
  Parameters.notNull("null file object", fo); // NOI18N
  if (fo.isValid()) {
    try {
      DataObject dao = DataObject.find(fo);
      if (dao.isModified()) {
        EditorCookie editor = dao.getLookup().lookup(EditorCookie.class);
        if (editor != null) {
          Document doc = editor.getDocument();
          if (doc != null) {
            return new FileBufferDoc(fo, doc);
          }
        }
      }
    } catch (DataObjectNotFoundException e) {
      // nothing
    }
    return new FileBufferFile(fo);
  } else {
    FileSystem fs;
    try {
      fs = fo.getFileSystem();
    } catch (FileStateInvalidException ex) {
      Exceptions.printStackTrace(ex);
      fs = InvalidFileObjectSupport.getDummyFileSystem();
    }
    return new FileBufferFile(InvalidFileObjectSupport.getInvalidFileObject(fs, fo.getPath()));
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-modelutil

public static Document getDocument(FileObject fo) {
  if (fo != null && fo.isValid()) {
    try {
      DataObject dob = DataObject.find(fo);
      if (dob != null && dob.isValid()) {
        EditorCookie ec = dob.getLookup().lookup(EditorCookie.class);
        if (ec != null) {
          return ec.getDocument();
        }
      }
    } catch (IOException ex) {
      // file can be removed or became invalid
      // we catch IOException, because FileStateInvalidException is IOException
      // but is not declared to be thrown from DataObject.find
      if (fo.isValid() && !fo.isVirtual()) {
        Exceptions.printStackTrace(ex);
      }
    }
  }
  return null;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-navigation

/**
 * Saves file.
 *
 * @param fo - file object
 */
private static void saveFileAndMarkAsReadOnly(FileObject fo) {
  try {
    DataObject dob = DataObject.find(fo);
    EditorCookie ec = dob.getLookup().lookup(EditorCookie.class);
    ec.saveDocument();
    ReadOnlySupport ro = dob.getLookup().lookup(ReadOnlySupport.class);
    if (ro != null) {
      ro.setReadOnly(true);
    }
  } catch (DataObjectNotFoundException e) {
    //do nothing, memory file already deleted
  } catch (FileStateInvalidException e) {
    //do nothing, memory file already deleted
  } catch (IOException ex) {
    Exceptions.printStackTrace(ex);
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-testng-ui

@Override
protected void performAction(Node[] activatedNodes) {
  Lookup l = activatedNodes[0].getLookup();
  FileObject fo = l.lookup(FileObject.class);
  EditorCookie ec = l.lookup(EditorCookie.class);
  if (fo == null && ec == null) {
    throw new UnsupportedOperationException();
    JEditorPane[] panes = ec.getOpenedPanes();
    if (panes.length > 0) {
      final int cursor = panes[0].getCaret().getDot();
      JavaSource js = JavaSource.forDocument(panes[0].getDocument());
      if(js == null) {
        return;

代码示例来源:origin: dcaoyuan/nbscala

/** Add the line offset into the jump history */
private void addPositionToJumpList(String url, Line l, int column) {
  DataObject dataObject = getDataObject(url);
  if (dataObject != null) {
    EditorCookie ec = dataObject.getLookup().lookup(EditorCookie.class);
    if (ec != null) {
      try {
        StyledDocument doc = ec.openDocument();
        JEditorPane[] eps = ec.getOpenedPanes();
        if (eps != null && eps.length > 0) {
          JumpList.addEntry(eps[0], NbDocument.findLineOffset(doc, l.getLineNumber()) + column);
        }
      } catch (java.io.IOException ioex) {
        ErrorManager.getDefault().notify(ioex);
      }
    }
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-toolchain-ui

@Override
public void outputLineCleared(OutputEvent ev) {
  try {
    DataObject dob = DataObject.find(file);
    StyledDocument doc = null;
    if (dob.isValid()) {
      EditorCookie ec = dob.getLookup().lookup(EditorCookie.class);
      if (ec != null) {
        doc = ec.getDocument();
      }
    }
    if (doc != null) {
      HintsController.setErrors(doc, CC_compiler_errors, Collections.<ErrorDescription>emptyList());
    }
  } catch (DataObjectNotFoundException ex) {
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-mercurial

private Document getSourceDocument(StreamSource ss) {
  Document sdoc = null;
  FileObject fo = ss.getLookup().lookup(FileObject.class);
  if (fo != null) {
    try {
      DataObject dao = DataObject.find(fo);
      if (dao.getPrimaryFile() == fo) {
        EditorCookie ec = dao.getCookie(EditorCookie.class);
        if (ec != null) {
          sdoc = ec.openDocument();
        }
      }
    } catch (Exception e) {
      // fallback to other means of obtaining the source
    }
  } else {
    sdoc = ss.getLookup().lookup(Document.class);
  }
  return sdoc;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-terminal-nb

private void doWork() {
  if (url == null) {
    return;
  }
  try {
    FileObject fo;
    if (url.getProtocol().equals("file")) { //NOI18N
      fo = FileUtil.toFileObject(new File(url.getPath()));
    } else {
      fo = URLMapper.findFileObject(url); //NOI18N
    }
    DataObject dobj = DataObject.find(fo);
    EditorCookie ed = dobj.getLookup().lookup(EditorCookie.class);
    if (ed != null && fo == dobj.getPrimaryFile()) {
      if (lineNumber == -1) {
        ed.open();
      } else {
        lc = (LineCookie) dobj.getLookup().lookup(LineCookie.class);
        SwingUtilities.invokeLater(this);
      }
    } else {
      Toolkit.getDefaultToolkit().beep();
    }
  } catch (Exception ex) {
    // ignore
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-highlight

/*package*/ static void navigateToOccurrence(boolean next) {
    final Node[] activatedNodes = TopComponent.getRegistry().getActivatedNodes();
    // check whether current file is C/C++
    DataObject dobj = activatedNodes[0].getLookup().lookup(DataObject.class);
    FileObject fo = (dobj == null) ? null : dobj.getPrimaryFile();
    String mime = (fo == null) ? "" : fo.getMIMEType();
    if (MIMENames.isHeaderOrCppOrC(mime)) {
      EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
      Document doc = ec.getDocument();

      int position = CaretAwareCsmFileTaskFactory.getLastPosition(dobj.getPrimaryFile());
      int goTo = findOccurrencePosition(next, doc, position);
      if (goTo > 0) {
        JEditorPane pane = NbDocument.findRecentEditorPane(ec);
        pane.setCaretPosition(goTo);
      } else {
        StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(SemanticUtils.class, "cpp-no-marked-occurrence"));
      }
    }

  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-modelimpl

private static FileBuffer createFileBuffer(DataObject dao) {
  FileObject fo = dao.getPrimaryFile();
  if (fo.isValid()) {
    if (dao.isModified()) {
      EditorCookie editor = dao.getLookup().lookup(EditorCookie.class);
      if (editor != null) {
        Document doc = editor.getDocument();
        if (doc != null) {
          return new FileBufferDoc(fo, doc);
        }
      }
    }
  }
  return new FileBufferFile(fo);
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-javascript2-debug-ui

doc = ec.openDocument();
} catch (IOException ex) {
  return ;
if (ep == null || ep.getDocument() != doc) {
  return ;
  return;
final FileObject fo = line.getLookup().lookup(FileObject.class);
if (isFunctionPtr[0]) {

相关文章