Java Code Examples for org.openide.cookies.EditorCookie#Observable

The following examples show how to use org.openide.cookies.EditorCookie#Observable . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DiffFileTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void setModel (DiffNode[] nodes, EditorCookie[] editorCookies, Object modelData) {
    this.editorCookies = editorCookies;
    tableModel.setNodes(this.nodes = nodes);
    changeListener = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent e) {
            Object source = e.getSource();
            String propertyName = e.getPropertyName();
            if (EditorCookie.Observable.PROP_MODIFIED.equals(propertyName)
                    && (source instanceof EditorCookie.Observable)) {
                statusModifiedChanged((EditorCookie.Observable) source);
            }
        }
    };
    for (EditorCookie editorCookie : this.editorCookies) {
        if (editorCookie instanceof EditorCookie.Observable) {
            ((EditorCookie.Observable) editorCookie).addPropertyChangeListener(WeakListeners.propertyChange(changeListener, editorCookie));
        }
    }
}
 
Example 2
Source File: RelationshipMappingWhereUsed.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
 public PositionBounds getPosition() {
    try {
        DataObject dobj = DataObject.find(getParentFile());
        if (dobj != null) {
            EditorCookie.Observable obs = (EditorCookie.Observable)dobj.getLookup().lookup(EditorCookie.Observable.class);
            if (obs != null && obs instanceof CloneableEditorSupport) {
                CloneableEditorSupport supp = (CloneableEditorSupport)obs;

            PositionBounds bounds = new PositionBounds(
                    supp.createPositionRef(loc[0], Position.Bias.Forward),
                    supp.createPositionRef(Math.max(loc[0], loc[1]), Position.Bias.Forward)
                    );

            return bounds;
        }
        }
    } catch (DataObjectNotFoundException ex) {
        LOG.log(Level.INFO, "Can't resolve", ex);//NOI18N
    }
    return null;
}
 
Example 3
Source File: DiffLookup.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void validateData(Object[] data) throws IllegalArgumentException {
    super.validateData(data);

    boolean observableEditorCookiePresent = false;
    boolean fileObjectPresent = false;
    for (Object o : data) {
        if (o instanceof EditorCookie.Observable) {
            if (observableEditorCookiePresent) {
                throw new IllegalArgumentException(
                        "multiple instances of EditorCookie.Observable in the data"); //NOI18N
            }
            observableEditorCookiePresent = true;
        }
        if (o instanceof FileObject) {
            if (fileObjectPresent) {
                throw new IllegalArgumentException(
                        "multiple instances of FileObject in the data"); //NOI18N
            }
            fileObjectPresent = true;
        }
    }
}
 
Example 4
Source File: EditorAnnotationsHelper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static EditorAnnotationsHelper getInstance( FileObject fileObject ){
    try {
        DataObject dataObject = DataObject.find(fileObject);
        EditorAnnotationsHelper helper = HELPERS.get(dataObject);

        if (helper != null) {
            return helper;
        }

        EditorCookie.Observable observable = dataObject.getLookup().lookup(
                EditorCookie.Observable.class);

        if (observable == null) {
            return null;
        }

        helper = new EditorAnnotationsHelper( dataObject , observable );
        HELPERS.put(dataObject, helper );

        return helper;
    } catch (IOException ex) {
        Logger.getLogger( EditorAnnotationsHelper.class.getName() ).
            log(Level.INFO, null, ex);
        return null;
    }
}
 
Example 5
Source File: AnnotationsHolder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static synchronized AnnotationsHolder get(FileObject file) {
    try {
        DataObject od = DataObject.find(file);
        AnnotationsHolder a = file2Annotations.get(od);

        if (a != null) {
            return a;
        }

        EditorCookie.Observable ec = od.getLookup().lookup(EditorCookie.Observable.class);
        
        if (ec == null) {
            return null;
        }
        
        file2Annotations.put(od, a = new AnnotationsHolder(od, ec));
        
        return a;
    } catch (IOException ex) {
        LOGGER.log(Level.INFO, null, ex);
        
        return null;
    }
}
 
Example 6
Source File: AnnotationsHolder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static synchronized AnnotationsHolder get(FileObject file) {
    try {
        DataObject od = DataObject.find(file);
        AnnotationsHolder a = file2Annotations.get(od);

        if (a != null) {
            return a;
        }

        EditorCookie.Observable ec = od.getLookup().lookup(EditorCookie.Observable.class);
        
        if (ec == null) {
            return null;
        }
        
        file2Annotations.put(od, a = new AnnotationsHolder(od, ec));
        
        return a;
    } catch (IOException ex) {
        LOGGER.log(Level.INFO, null, ex);
        
        return null;
    }
}
 
Example 7
Source File: DiffFileTable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void setEditorCookies (Map<File, EditorCookie> editorCookies) {
    this.editorCookies = editorCookies;
    changeListener = new PropertyChangeListener() {
        @Override
        public void propertyChange (PropertyChangeEvent e) {
            Object source = e.getSource();
            String propertyName = e.getPropertyName();
            if (EditorCookie.Observable.PROP_MODIFIED.equals(propertyName) && (source instanceof EditorCookie.Observable)) {
                final EditorCookie.Observable cookie = (EditorCookie.Observable) source;
                Mutex.EVENT.readAccess(new Runnable () {
                    @Override
                    public void run() {
                        for (int i = 0; i < tableModel.getRowCount(); ++i) {
                            if (DiffFileTable.this.editorCookies.get(tableModel.getNode(i).getFile()) == cookie) {
                                tableModel.fireTableCellUpdated(i, 0);
                                break;
                            }
                        }
                    }
                });
            }
        }
    };
    for (Map.Entry<File, EditorCookie> e : editorCookies.entrySet()) {
        EditorCookie editorCookie = e.getValue();
        if (editorCookie instanceof EditorCookie.Observable) {
            ((EditorCookie.Observable) editorCookie).addPropertyChangeListener(WeakListeners.propertyChange(changeListener, editorCookie));
        }
    }
}
 
Example 8
Source File: NavigatorContent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void openAndFocusElement(final TreeNodeAdapter selected, final boolean selectLineOnly) {
    BaseDocument bdoc = (BaseDocument)selected.getDocumentElement().getDocument();
    DataObject dobj = NbEditorUtilities.getDataObject(bdoc);
    if(dobj == null) return ;
    
    final EditorCookie.Observable ec = (EditorCookie.Observable)dobj.getCookie(EditorCookie.Observable.class);
    if(ec == null) return ;
    
    try {
        final Document doc = ec.openDocument(); //wait to editor to open
    }catch(IOException ioe) {
        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
    }
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JEditorPane[] panes = ec.getOpenedPanes();
            if (panes != null && panes.length > 0) {
                // editor already opened, so just select
                selectElementInPane(panes[0], selected, !selectLineOnly);
            } else if(!selectLineOnly) {
                // editor not opened yet
                ec.open();
                panes = ec.getOpenedPanes();
                if (panes != null && panes.length > 0) {
                    selectElementInPane(panes[0], selected, true);
                }
            }
        }
    });
}
 
Example 9
Source File: EditorAnnotationsHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private EditorAnnotationsHelper( DataObject dataObject , 
        EditorCookie.Observable observable )
{
    myDataObject = dataObject;
    myObservable = observable;
    myModelAnnotations = new AtomicReference<List<CDIAnnotation>>(
            Collections.<CDIAnnotation>emptyList());
    myAnnotations = new AtomicReference<List<CDIAnnotation>>(
            Collections.<CDIAnnotation>emptyList());
    
    observable.addPropertyChangeListener(this );
}
 
Example 10
Source File: PerformanceTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private StyledDocument prepare(String fileName) throws Exception {
    File testFile = new File(getDataDir(), fileName);
    FileObject testObject = FileUtil.createData(testFile);
    DataObject dataObj = DataObject.find(testObject);
    EditorCookie.Observable ed = dataObj.getCookie(Observable.class);
    waitTimeout();
    ed.openDocument();
    ed.open();
    waitTimeout();
    return ed.getDocument();
}
 
Example 11
Source File: DefaultOpenFileImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private SetCursorTask(EditorCookie editorCookie, int offset) {
    this.editorCookie = editorCookie;
    this.observable = (editorCookie instanceof EditorCookie.Observable)
                      ? (EditorCookie.Observable) editorCookie
                      : null;
    this.offset = offset;

    if (log.isLoggable(FINEST)) {
        log.finest("SetCursorTask.<init>");                     //NOI18N
        log.log(FINEST, " - observable: {0}", (observable != null));//NOI18N
    }
}
 
Example 12
Source File: AnnotationsHolder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private AnnotationsHolder(DataObject file, EditorCookie.Observable ec) {
   this.file = file;
   this.ec   = ec;
   this.annotations = new ArrayList<IsOverriddenAnnotation>();
   
   ec.addPropertyChangeListener(this);
   
   SwingUtilities.invokeLater(new Runnable() {
       public void run() {
           checkForReset();
       }
   });
   
   Logger.getLogger("TIMER").log(Level.FINE, "Overridden AnnotationsHolder", new Object[] {file.getPrimaryFile(), this}); //NOI18N
}
 
Example 13
Source File: DiffStreamSource.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static EditorCookie.Observable getEditableCookie (File file) {
    EditorCookie.Observable editorCookie = null;
    if (file == null) {
        return null;
    }
    FileObject fileObj = FileUtil.toFileObject(file);
    if (fileObj != null) {
        try {
            DataObject dao = DataObject.find(fileObj);
            if (dao instanceof MultiDataObject) {
                MultiDataObject mdao = (MultiDataObject) dao;
                for (MultiDataObject.Entry entry : mdao.secondaryEntries()) {
                    if (fileObj == entry.getFile() && entry instanceof CookieSet.Factory) {
                        CookieSet.Factory factory = (CookieSet.Factory) entry;
                        EditorCookie ec = factory.createCookie(EditorCookie.class);
                        if (ec instanceof EditorCookie.Observable) {
                            editorCookie = (EditorCookie.Observable) ec;
                        }
                    }
                }
            }
            if (editorCookie == null) {
                EditorCookie cookie = dao.getCookie(EditorCookie.class);
                if (cookie instanceof EditorCookie.Observable) {
                    editorCookie = (EditorCookie.Observable) cookie;
                }
            }
        } catch (DataObjectNotFoundException ex) {
        }
    }
    return editorCookie;
}
 
Example 14
Source File: DiffUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @since 1.9.1
 */
public static EditorCookie getEditorCookie(Document doc) {
    if (doc == null) {
        return null;
    }

    DataObject dataObj = (DataObject) doc.getProperty(
                                        Document.StreamDescriptionProperty);
    if (dataObj == null) {
        return null;
    }

    EditorCookie plain = null;

    if (dataObj instanceof MultiDataObject) {
        MultiDataObject multiDataObj = (MultiDataObject) dataObj;
        for (MultiDataObject.Entry entry : multiDataObj.secondaryEntries()) {
            if (entry instanceof CookieSet.Factory) {
                CookieSet.Factory factory = (CookieSet.Factory) entry;
                EditorCookie ec = factory.createCookie(EditorCookie.class);
                if (ec.getDocument() == doc) {
                    if (ec instanceof EditorCookie.Observable) {
                        return (EditorCookie.Observable) ec;
                    }

                    if (plain == null) {
                        plain = ec;
                    }
                }
            }
        }
    }

    return chooseBetterEditorCookie(getEditorCookie(dataObj, false), plain);
}
 
Example 15
Source File: ResourceStringLoader.java    From netbeans with Apache License 2.0 5 votes vote down vote up
Holder(Cache c, FileObject f) {
    this.cache = c;
    this.file = f;
    
    try {
        DataObject d = DataObject.find(f);
        EditorCookie.Observable obs = d.getLookup().lookup(EditorCookie.Observable.class);
        if (obs != null) {
            obs.addPropertyChangeListener(WeakListeners.propertyChange(this, obs));
        }
    } catch (DataObjectNotFoundException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 16
Source File: AnnotationsHolder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private AnnotationsHolder(DataObject file, EditorCookie.Observable ec) {
   this.file = file;
   this.ec   = ec;
   this.annotations = new ArrayList<IsOverriddenAnnotation>();
   
   ec.addPropertyChangeListener(this);
   
   SwingUtilities.invokeLater(new Runnable() {
       public void run() {
           checkForReset();
       }
   });
   
   Logger.getLogger("TIMER").log(Level.FINE, "Overridden AnnotationsHolder", new Object[] {file.getPrimaryFile(), this}); //NOI18N
}
 
Example 17
Source File: JShellEnvironment.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void init(FileObject workRoot) throws IOException {
    this.workRoot = workRoot;
    workRoot.setAttribute("jshell.scratch", true);
    consoleFile = workRoot.createData("console.jsh");
    
    EditorCookie.Observable eob = consoleFile.getLookup().lookup(EditorCookie.Observable.class);
    inst = new L();
    eob.addPropertyChangeListener(WeakListeners.propertyChange(inst, eob));

    platform = org.netbeans.modules.jshell.project.ShellProjectUtils.findPlatform(project);
}
 
Example 18
Source File: NavigatorContent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void attachEditorObservableListener(DataObject d) {
    EditorCookie.Observable obs = d.getCookie(EditorCookie.Observable.class);
    if (obs == null) {
        ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "The DataObject " + d.getName() + "(class=" + d.getClass().getName() + ") has no EditorCookie.Observable!");
    } else {
        obs.addPropertyChangeListener(peerWL = WeakListeners.propertyChange(NavigatorContent.this, obs));
    }
}
 
Example 19
Source File: EditableDiffView.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void setSource2 (StreamSource ss, Document sdoc) throws IOException {
    secondSourceAvailable = false;
    EditorKit kit = jEditorPane2.getEditorPane().getEditorKit();
    if (kit == null) throw new IOException("Missing Editor Kit"); // NOI18N
    
    modifiedDocument = sdoc;
    if (sdoc != null && ss.isEditable()) {
        DataObject dao = (DataObject) sdoc.getProperty(Document.StreamDescriptionProperty);
        if (dao != null) {
            if (dao instanceof MultiDataObject) {
                MultiDataObject mdao = (MultiDataObject) dao;
                for (MultiDataObject.Entry entry : mdao.secondaryEntries()) {
                    if (entry instanceof CookieSet.Factory) {
                        CookieSet.Factory factory = (CookieSet.Factory) entry;
                        EditorCookie ec = factory.createCookie(EditorCookie.class);
                        Document entryDocument = ec.getDocument();
                        if (entryDocument == sdoc && ec instanceof EditorCookie.Observable) {
                            editableCookie = (EditorCookie.Observable) ec;
                            editableDocument = sdoc;
                            editorUndoRedo = getUndoRedo(ec);
                        }
                    }
                }
            }
            if (editableCookie == null) {
                EditorCookie cookie = dao.getCookie(EditorCookie.class);
                if (cookie instanceof EditorCookie.Observable) {
                    editableCookie = (EditorCookie.Observable) cookie;
                    editableDocument = sdoc;
                    editorUndoRedo = getUndoRedo(cookie);
                }
            }
        }
    }
    Document doc = sdoc != null ? sdoc : kit.createDefaultDocument();
    if (sdoc != null || !Boolean.TRUE.equals(skipFile)) {
        if (jEditorPane2.getEditorPane().getUI() instanceof BaseTextUI) {
            if (sdoc == null) {
                Reader r = ss.createReader();
                if (r != null) {
                    secondSourceAvailable = true;
                    try {
                        kit.read(r, doc, 0);
                    } catch (javax.swing.text.BadLocationException e) {
                        throw new IOException("Can not locate the beginning of the document."); // NOI18N
                    } finally {
                        r.close();
                    }
                }
            } else {
                secondSourceAvailable = true;
            }
        } else {
            secondSourceUnsupportedTextUI = true;
        }
    }
    jEditorPane2.initActions();
    view.putClientProperty(UndoRedo.class, editorUndoRedo);
    jEditorPane2.getEditorPane().setDocument(doc);
    jEditorPane2.getEditorPane().setEditable(editableCookie != null);
    if (doc instanceof NbDocument.CustomEditor) {
        Component c = ((NbDocument.CustomEditor)doc).createEditor(jEditorPane2.getEditorPane());
        if (c instanceof JComponent) {
            jEditorPane2.setCustomEditor((JComponent)c);
        }
    }
    
    customizeEditor(jEditorPane2.getEditorPane());
    jViewport2 = jEditorPane2.getScrollPane().getViewport();
    joinScrollBars();
}
 
Example 20
Source File: EventSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void assignDocumentListener(final DataObject od) {
    EditorCookie.Observable ec = od.getCookie(EditorCookie.Observable.class);
    if (ec != null) {
        docListener = new DocListener (ec);
    }
}