/** * Gets the mime type of a document. If the mime type can't be determined * this method will return <code>null</code>. This method should work reliably * for Netbeans documents that have their mime type stored in a special * property. For any other documents it will probably just return <code>null</code>. * * @param doc The document to get the mime type for. * * @return The mime type of the document or <code>null</code>. * @see NbEditorDocument#MIME_TYPE_PROP */ public static String getMimeType(Document doc) { return DocumentUtilities.getMimeType(doc); }
/** * Returns true if bracket completion is enabled in options. */ static boolean completionSettingEnabled(Document doc) { Preferences prefs = MimeLookup.getLookup(DocumentUtilities.getMimeType(doc)).lookup(Preferences.class); return prefs.getBoolean(SimpleValueNames.COMPLETION_PAIR_CHARACTERS, true); }
@Override public void caretUpdate(final CaretEvent event) { final JTextComponent lastEditor = lastEditorRef == null ? null : lastEditorRef.get(); if (lastEditor != null) { Document doc = lastEditor.getDocument (); String mimeType = DocumentUtilities.getMimeType (doc); if (doc != null && mimeType != null) { Source source = Source.create(doc); if (source != null) { ((EventSupport)SourceEnvironment.forSource(source)).resetState(false, false, -1, -1, false); } } } }
@Override public void propertyChange(PropertyChangeEvent evt) { Document docToReparse = null; if (EditorRegistry.FOCUS_GAINED_PROPERTY.equals(evt.getPropertyName())) { docToReparse = ((JTextComponent) evt.getNewValue()).getDocument(); } else if (EditorRegistry.FOCUSED_DOCUMENT_PROPERTY.equals(evt.getPropertyName())) { docToReparse = (Document) evt.getNewValue(); } if (docToReparse != null && MIMENames.isHeaderOrCppOrC(DocumentUtilities.getMimeType(docToReparse))) { CppMetaModel.getDefault().scheduleParsing(docToReparse); } } }
@Override public void propertyChange(final PropertyChangeEvent evt) { String propName = evt.getPropertyName(); if ("completion-active".equals(propName)) { //NOI18N Source source = null; final JTextComponent lastEditor = lastEditorRef == null ? null : lastEditorRef.get(); final Document doc = lastEditor == null ? null : lastEditor.getDocument(); if (doc != null) { String mimeType = DocumentUtilities.getMimeType (doc); if (mimeType != null) { source = Source.create(doc); } } if (source != null) { handleCompletionActive(source, evt.getNewValue()); } } }
protected void init(Document doc){ String mimeType = DocumentUtilities.getMimeType(doc); CsmFontColorManager.instance().addListener(mimeType, this); }
private void checkClosed(Object o){ if (o instanceof Set<?>) { Set<EditorCookie> editorCookies = new HashSet<EditorCookie>(); for(Object top : (Set<?>) o){ if (top instanceof EditorCookie){ EditorCookie cookie = (EditorCookie) top; if (MIMENames.isFortranOrHeaderOrCppOrC( DocumentUtilities.getMimeType(cookie.getDocument()))) { editorCookies.add(cookie); } } } checkClosed(editorCookies); } }
@Override public Preferences forDocument(Document doc, String mimeType) { if (mimeType == null) { mimeType = DocumentUtilities.getMimeType(doc); } if (MIMENames.C_MIME_TYPE.equals(mimeType)) { return NbPreferences.forModule(CodeStyle.class); //return MimeLookup.getLookup(MimePath.parse(MIMENames.C_MIME_TYPE)).lookup(Preferences.class); } else if (MIMENames.CPLUSPLUS_MIME_TYPE.equals(mimeType)) { return NbPreferences.forModule(CodeStyle.class); //return MimeLookup.getLookup(MimePath.parse(MIMENames.CPLUSPLUS_MIME_TYPE)).lookup(Preferences.class); } else if (MIMENames.HEADER_MIME_TYPE.equals(mimeType)) { return NbPreferences.forModule(CodeStyle.class); //return MimeLookup.getLookup(MimePath.parse(MIMENames.HEADER_MIME_TYPE)).lookup(Preferences.class); } return null; } };
public synchronized static CodeStyle getDefault(Document doc) { String mimeType = DocumentUtilities.getMimeType(doc); if (mimeType == null) { System.out.println("Undefined MIME type of document "+doc); // NOI18N //if (doc instanceof BaseDocument) { // if (CKit.class.equals(((BaseDocument)doc).getKitClass())) { // return getDefault(Language.C); // } //} } else { if (mimeType.equals(MIMENames.C_MIME_TYPE)) { return getDefault(Language.C, doc); } else if (mimeType.equals(MIMENames.HEADER_MIME_TYPE)) { return getDefault(Language.HEADER, doc); } } return getDefault(Language.CPP, doc); }
public void init(FoldOperation operation) { this.operation = operation; String mimeType = DocumentUtilities.getMimeType(operation.getHierarchy().getComponent()); prefs = MimeLookup.getLookup(mimeType).lookup(Preferences.class); }
public static String getLineHtml(int startLine, int endLine, final int stToken, final int endToken, BaseDocument doc) throws BadLocationException { int startBold = stToken - startLine; int endBold = endToken - startLine; String content = doc.getText(startLine, endLine - startLine); String mime = DocumentUtilities.getMimeType(doc); if (startBold >= 0 && endBold >= 0 && startBold <= content.length() && endBold <= content.length() && startBold < endBold) { StringBuilder buf = new StringBuilder(); buf.append(getHtml(mime, trimStart(content.substring(0, startBold)))); buf.append("<b>"); //NOI18N buf.append(getHtml(mime, content.substring(startBold, endBold))); buf.append("</b>");//NOI18N buf.append(getHtml(mime, trimEnd(content.substring(endBold)))); return buf.toString(); } else { return getHtml(mime, content); } }
/** * Sets mime type. * * @param doc - document */ public static void setupMimeType(Document doc, Document mainDoc) { String mimeType = DocumentUtilities.getMimeType(mainDoc); if (mimeType != null) { if (MIMENames.isHeaderOrCppOrC(mimeType)) { doc.putProperty(BaseDocument.MIME_TYPE_PROP, mimeType); } else { doc.putProperty(BaseDocument.MIME_TYPE_PROP, MIMENames.CPLUSPLUS_MIME_TYPE); } } else { doc.putProperty(BaseDocument.MIME_TYPE_PROP, MIMENames.CPLUSPLUS_MIME_TYPE); } }
private void editorRegistryChanged() { final JTextComponent editor = EditorRegistry.lastFocusedComponent(); final JTextComponent lastEditor = lastEditorRef == null ? null : lastEditorRef.get(); if (lastEditor != editor && (editor == null || editor.getClientProperty("AsTextField") == null)) { if (lastEditor != null) { lastEditor.removeCaretListener(this); lastEditor.removePropertyChangeListener(this); k24.set(false); } lastEditorRef = new WeakReference<JTextComponent>(editor); if (editor != null) { editor.addCaretListener(this); editor.addPropertyChangeListener(this); } final JTextComponent focused = EditorRegistry.focusedComponent(); if (focused != null) { final Document doc = editor.getDocument(); final String mimeType = DocumentUtilities.getMimeType (doc); if (doc != null && mimeType != null) { final Source source = Source.create (doc); if (source != null) { ((EventSupport)SourceEnvironment.forSource(source)).resetState(true, false, -1, -1, true); } } } } }
public FoldViewFactory(View documentView) { super(documentView); foldHierarchy = FoldHierarchy.get(textComponent()); foldHierarchy.addFoldHierarchyListener(this); viewFoldsExpanded = Boolean.TRUE.equals(textComponent().getClientProperty(VIEW_FOLDS_EXPANDED_PROPERTY)); String mime = DocumentUtilities.getMimeType(document()); Lookup lkp = MimeLookup.getLookup(mime); colorSource = lkp.lookupResult(FontColorSettings.class); colorSource.addLookupListener(WeakListeners.create(LookupListener.class, this, colorSource)); colorSettings = (FontColorSettings)colorSource.allInstances().iterator().next(); prefs = lkp.lookup(Preferences.class); prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, this, prefs)); initViewFlags(); }
/** * Initializes document of expanded context pane. * * @param doc - document */ public void setContextExpansionDocument(Document doc) { String mimeType = DocumentUtilities.getMimeType(doc); if (mimeType == null) { mimeType = MIMENames.CPLUSPLUS_MIME_TYPE; } jCodeExpansionEditorPane.setCaretPosition(0); jCodeExpansionEditorPane.setContentType(mimeType); jCodeExpansionEditorPane.setDocument(doc); jCodeExpansionEditorPane.putClientProperty("HelpID","MacroExpansionWindow"); //NOI18N doc.putProperty(JEditorPane.class, jCodeExpansionEditorPane); }
private void addHighlightsToBag(PositionsBag bag, List<? extends CsmOffsetable> blocks, SemanticEntity entity) { Document doc = getDocument(); if (doc != null) { String mimeType = DocumentUtilities.getMimeType(doc); if (mimeType == null) { mimeType = MIMENames.CPLUSPLUS_MIME_TYPE; } for (CsmOffsetable block : blocks) { int startOffset = getDocumentOffset(doc, block.getStartOffset()); int endOffset = block.getEndOffset(); endOffset = getDocumentOffset(doc, endOffset == Integer.MAX_VALUE ? doc.getLength() + 1 : endOffset); if (startOffset < doc.getLength() && endOffset > 0) { final AttributeSet attributes = entity.getAttributes(block, mimeType); if (attributes == null) { assert false : "Color attributes set is not found for MIME "+mimeType+". Document "+doc; return; } addHighlightsToBag(bag, startOffset, endOffset, attributes, entity.getName()); } } } }
/** * Creates new instance initializing final fields. */ public AnnotationBar(JTextComponent target) { this.textComponent = target; this.editorUI = Utilities.getEditorUI(target); this.foldHierarchy = FoldHierarchy.get(editorUI.getComponent()); this.doc = editorUI.getDocument(); this.caret = textComponent.getCaret(); setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); elementAnnotationsSubstitute = ""; //NOI18N if (textComponent instanceof JEditorPane) { String mimeType = org.netbeans.lib.editor.util.swing.DocumentUtilities.getMimeType(textComponent); FontColorSettings fcs = MimeLookup.getLookup(mimeType).lookup(FontColorSettings.class); renderingHints = (Map) fcs.getFontColors(FontColorNames.DEFAULT_COLORING).getAttribute(EditorStyleConstants.RenderingHints); } else { renderingHints = null; } }
setOpaque(true); prefs = MimeLookup.getLookup(org.netbeans.lib.editor.util.swing.DocumentUtilities.getMimeType(component)).lookup(Preferences.class); prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, prefsListener, prefs)); prefsListener.preferenceChange(null);
private Coloring getColoring() { if (attribs == null) { if (fcsLookupResult == null) { fcsLookupResult = MimeLookup.getLookup(org.netbeans.lib.editor.util.swing.DocumentUtilities.getMimeType(component)) .lookupResult(FontColorSettings.class); fcsLookupResult.addLookupListener(WeakListeners.create(LookupListener.class, fcsTracker, fcsLookupResult)); } FontColorSettings fcs = fcsLookupResult.allInstances().iterator().next(); AttributeSet attr = fcs.getFontColors(FontColorNames.CODE_FOLDING_BAR_COLORING); if (attr == null) { attr = fcs.getFontColors(FontColorNames.DEFAULT_COLORING); } else { attr = AttributesUtilities.createComposite(attr, fcs.getFontColors(FontColorNames.DEFAULT_COLORING)); } attribs = attr; } return Coloring.fromAttributeSet(attribs); }
public static CsmFile getCsmFile(Document bDoc, boolean waitParsing, boolean snapShot) { CsmFile csmFile = null; if (bDoc != null) { try { csmFile = (CsmFile) bDoc.getProperty(CsmFile.class); if (csmFile == null) { csmFile = getCsmFile(NbEditorUtilities.getDataObject(bDoc), waitParsing, snapShot); } if (csmFile == null) { String mimeType = DocumentUtilities.getMimeType(bDoc); if ("text/x-dialog-binding".equals(mimeType)) { // NOI18N // this is context from dialog InputAttributes inputAttributes = (InputAttributes) bDoc.getProperty(InputAttributes.class); if (inputAttributes != null) { LanguagePath path = LanguagePath.get(MimeLookup.getLookup(mimeType).lookup(Language.class)); FileObject fileObject = (FileObject) inputAttributes.getValue(path, "dialogBinding.fileObject"); //NOI18N csmFile = CsmUtilities.getCsmFile(fileObject, waitParsing, snapShot); if (csmFile == null) { Document d = (Document) inputAttributes.getValue(path, "dialogBinding.document"); //NOI18N csmFile = d == null ? null : CsmUtilities.getCsmFile(d, waitParsing, snapShot); } } } } } catch (NullPointerException exc) { exc.printStackTrace(System.err); } } return csmFile; }