Java Code Examples for org.openide.loaders.DataFolder#getChildren()

The following examples show how to use org.openide.loaders.DataFolder#getChildren() . 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: Util.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Adds source to source map (I18N wizard settings). If there is already no change is done.
 * If it's added anew then it is tried to find correspondin reousrce, i.e.
 * first resource from the same folder.
 * @param sourceMap settings where to add teh sources
 * @param source source to add */
public static void addSource(Map<DataObject,SourceData> sourceMap,
                             DataObject source) {
    if (sourceMap.containsKey(source)) {
        return;
    }
    
    DataFolder folder = source.getFolder();
    
    if (folder == null) {
        sourceMap.put(source, null);
        return;
    }

    // try to associate Bundle file

    for (DataObject child : folder.getChildren()) {
        if (child instanceof PropertiesDataObject) { // PENDING 
            sourceMap.put(source, new SourceData(child));
            return;
        }
    }
    
    // No resource found in the same folder.
    sourceMap.put(source, null);
}
 
Example 2
Source File: PackageDeleteRefactoringPlugin.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Problem preparePackageDelete(NonRecursiveFolder folder, RefactoringElementsBag refactoringElements) {
    DataFolder dataFolder = DataFolder.findFolder(folder.getFolder());
    // First; delete all files except packages
    DataObject children[] = dataFolder.getChildren();
    boolean empty = true;
    for( int i = 0; children != null && i < children.length; i++ ) {
        FileObject fileObject = children[i].getPrimaryFile();
        if ( !fileObject.isFolder() ) {
            refactoringElements.addFileChange(refactoring, new DeleteFile(fileObject, refactoringElements));
        }
        else {
            empty = false;
        }
    }

    // If empty delete itself
    if ( empty ) {
        refactoringElements.addFileChange(refactoring, new PackageDeleteElem(folder));
    }
        
    return null;
}
 
Example 3
Source File: DiffPresenter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void setDefaultDiffService(Object ds, String folder) {
    //System.out.println("setDefaultDiffService("+ds+")");
    FileObject services = FileUtil.getConfigFile(folder);
    DataFolder df = DataFolder.findFolder(services);
    DataObject[] children = df.getChildren();
    //System.out.println("  Got children.");
    for (int i = 0; i < children.length; i++) {
        if (children[i] instanceof InstanceDataObject) {
            InstanceDataObject ido = (InstanceDataObject) children[i];
            if (ido.instanceOf(ds.getClass())) {
                //System.out.println("  Found an instance of my class.");
                try {
                    if (ds.equals(ido.instanceCreate())) {
                        //System.out.println("  Have it, settings the order.");
                        df.setOrder(new DataObject[] { ido });
                        break;
                    }
                } catch (java.io.IOException ioex) {
                } catch (ClassNotFoundException cnfex) {}
            }
        }
    }
}
 
Example 4
Source File: ActionPasteType.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean canBePasted(final DataObject[] dataObjects, final DataFolder targetFolder, final int operation) throws FileStateInvalidException {
    final Set<DataObject> pasteableDataObjects = new HashSet<DataObject> ();
    final FileObject folder = targetFolder.getPrimaryFile();
    
    DataObject[] folderChildren = targetFolder.getChildren();
    
    for (int j = 0; j < dataObjects.length; j++) {
        final DataObject dataObject = dataObjects[j];
        final FileObject fo = dataObject.getPrimaryFile ();
        
        if (!isAction(dataObject) || !fo.getFileSystem().isDefault()) {
            break;    
        }

        final boolean isCopyPaste = operation == LoaderTransfer.CLIPBOARD_COPY && dataObject.isCopyAllowed();
        final boolean isCutPaste = operation == LoaderTransfer.CLIPBOARD_CUT && dataObject.isMoveAllowed() && 
                !(fo.getParent() == folder);//prevents from cutting into the same folder where it was 
                        
        if (isCopyPaste || isCutPaste) {
            
            boolean isDuplicate = false;
            for( int i=0; i<folderChildren.length; i++ ) {
                if( 0 == folderChildren[i].getName().compareTo( dataObject.getName() ) ) {
                    isDuplicate = true;
                    break;
                }
            }
            if( !isDuplicate )
                pasteableDataObjects.add(dataObject);                        
        }
    }
    return (pasteableDataObjects.size() == dataObjects.length);
}
 
Example 5
Source File: PropertiesDataObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Creates new name for this instance when moving/copying to new folder destination. 
 * @param folder new folder destination. */
private String createPasteSuffix(DataFolder folder) {
    String basicName = getPrimaryFile().getName();

    DataObject[] children = folder.getChildren();


    // Repeat until there is not such file name.
    for(int i = 0; ; i++) {
        String newName;

        if (i == 0) {
            newName = basicName;
        } else {
            newName = basicName + i;
        }
        boolean exist = false;

        for(int j = 0; j < children.length; j++) {
            if(children[j] instanceof PropertiesDataObject && newName.equals(children[j].getName())) {
                exist = true;
                break;
            }
        }

        if(!exist) {
            if (i == 0) {
                return ""; // NOI18N
            } else {
                return "" + i; // NOI18N
            }
        }
    }
}
 
Example 6
Source File: GUIRegistrationPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void getFolders(DataFolder folder, List<DataFolder> folders) {
    for (DataObject d : folder.getChildren()) {
        if (d instanceof DataFolder) {
            DataFolder f = (DataFolder) d;
            folders.add(f);
            getFolders(f, folders);
        }
    }
}
 
Example 7
Source File: Actions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static DataShadow findShadow (DataFolder f, DataObject dobj) {
    FileObject fo = dobj.getPrimaryFile();
    if (fo != null) {
        DataObject [] arr = f.getChildren();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] instanceof DataShadow) {
                DataShadow obj = (DataShadow) arr[i];
                if (fo.equals(obj.getOriginal().getPrimaryFile())) {
                    return obj;
                }
            }
        }
    }
    return null;
}
 
Example 8
Source File: Favorites.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Removes given file objects from Favorites if they are found as roots.
 * Files that are not found are silently ignored.
 * @param toRemove file objects to be removed.
 * @throws IOException When corresponding entry in userdir could not be deleted
 * @throws NullPointerException When any of <code>toRemove</code> parameters
 *         is <code>null</code>. It is undefined whether some of other non-null
 *         parameters were removed or not.
 */
public synchronized void remove(FileObject... toRemove) throws IOException, NullPointerException {
    DataFolder f = FavoritesNode.getFolder();
    DataObject [] arr = f.getChildren();
    for (DataObject obj : arr) {
        if (obj instanceof DataShadow) {
            FileObject root = ((DataShadow) obj).getOriginal().getPrimaryFile();
            for (FileObject rem : toRemove) {
                if (rem.equals(root)) {
                    obj.delete();
                }
            }
        }
    }
}
 
Example 9
Source File: Favorites.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private DataShadow findShadow(FileObject fo) {
    DataFolder f = FavoritesNode.getFolder();
    DataObject [] arr = f.getChildren();
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] instanceof DataShadow) {
            DataShadow obj = (DataShadow) arr[i];
            if (fo.equals(obj.getOriginal().getPrimaryFile())) {
                return obj;
            }
        }
    }
    return null;
}
 
Example 10
Source File: Favorites.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Removes references to nonexistent files.
 * Package-private for tests.
 */
void clearBrokenShadows() throws IOException {
    DataFolder f = FavoritesNode.getFolder();
    DataObject [] arr = f.getChildren();
    for (DataObject obj : arr) {
        if (! (obj instanceof DataShadow)) {
            obj.delete();
        }
    }
}
 
Example 11
Source File: FavoritesNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void setName(String name) {
    // #113859 - keep order of children in favorites folder after rename
    final DataFolder favoritesFolder = FavoritesNode.getFolder();
    final DataObject[] children = favoritesFolder.getChildren();
    super.setName(name);
    try {
        favoritesFolder.setOrder(children);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 12
Source File: JDBCDriverConvertor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the file describing the specified JDBC driver.
 */
public static void remove(JDBCDriver drv) throws IOException {
    String name = drv.getName();
    FileObject fo = FileUtil.getConfigFile(DRIVERS_PATH); //NOI18N
    // If DRIVERS_PATH can't be found (getConfigFile returns null)
    // its useless to try to delete any driver
    if(fo == null) {
        return;
    }
    DataFolder folder = DataFolder.findFolder(fo);
    DataObject[] objects = folder.getChildren();
    
    for (int i = 0; i < objects.length; i++) {
        InstanceCookie ic = (InstanceCookie)objects[i].getCookie(InstanceCookie.class);
        if (ic != null) {
            try {
                Object obj = ic.instanceCreate();
                if (obj instanceof JDBCDriver) {
                    JDBCDriver driver = (JDBCDriver) obj;
                    if (driver.getName().equals(name)) {
                        objects[i].delete();
                        break;
                    }
                }
            } catch (ClassNotFoundException e) {
                continue;
            }

        }
    }
}
 
Example 13
Source File: ConfigureToolbarPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void removeNotify() {
    super.removeNotify();

    ToolbarPool pool = ToolbarPool.getDefault();
    final ToolbarConfiguration tc = ToolbarConfiguration.findConfiguration( pool.getConfiguration() );
    if( null != tc ) {
        tc.setToolbarButtonDragAndDropAllowed( false );
    }
    //remove empty toolbars
    DataFolder folder = pool.getFolder();
    DataObject[] children = folder.getChildren();
    for( int i=0; i<children.length; i++ ) {
        final DataFolder subFolder = children[i].getCookie( DataFolder.class );
        if( null != subFolder && subFolder.getChildren().length == 0 ) {
            SwingUtilities.invokeLater( new Runnable() {

                @Override
                public void run() {
                    try {
                        subFolder.delete();
                        ToolbarPool.getDefault().waitFinished();
                        if( null != tc ) {
                            tc.removeEmptyRows();
                            tc.save();
                        }
                    }
                    catch (IOException e) {
                        LOG.log(Level.WARNING, null, e);
                    }
                }
            });
        }
    }
}
 
Example 14
Source File: DnDSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Add a new toolbar button represented by the given DataObject.
 */
private boolean addButton( DataObject dobj, int dropIndex, boolean dropBefore ) throws IOException {
    if( null == dobj )
        return false;
    //check if the dropped button (action) already exists in this toolbar
    String objName = dobj.getName();
    DataFolder backingFolder = getBackingFolder(currentToolbar);
    DataObject[] children = backingFolder.getChildren();
    for( int i=0; i<children.length; i++ ) {
        //TODO is comparing DataObject names ok?
        if( objName.equals( children[i].getName() ) ) {
            //user dropped to toolbat a new button that already exists in this toolbar
            //just move the existing button to a new position
            return moveButton( children[i], dropIndex, dropBefore );
        }
    }

    DataObject objUnderCursor = getDataObjectUnderDropCursor( dropIndex-1, dropBefore );

    DataShadow shadow = DataShadow.create( backingFolder, dobj );
    // use some fake position, so getChildren don't complain
    shadow.getPrimaryFile().setAttribute("position", 100001); // NOI18N

    //find the added object
    DataObject newObj = null;
    children = backingFolder.getChildren();
    for( int i=0; i<children.length; i++ ) {
        if( objName.equals( children[i].getName() ) ) {
            newObj = children[i];
            break;
        }
    }

    if( null != newObj )
        reorderButtons( newObj, objUnderCursor ); //put the button to its proper position

    return true;
}
 
Example 15
Source File: JFXRunPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void fillWebBrowsersCombo(List<String> list, String select) {
    // PENDING need to get rid of this filtering
    FileObject fo = FileUtil.getConfigFile (BROWSERS_FOLDER);
    if (fo != null) {
        DataFolder folder = DataFolder.findFolder (fo);
        DataObject [] dobjs = folder.getChildren ();
        for (int i = 0; i<dobjs.length; i++) {
            // Must not be hidden and have to provide instances (we assume instance is HtmlBrowser.Factory)
            if (Boolean.TRUE.equals(dobjs[i].getPrimaryFile().getAttribute(EA_HIDDEN)) ||
                    dobjs[i].getLookup().lookup(InstanceCookie.class) == null) {
                FileObject fo2 = dobjs[i].getPrimaryFile();
                String n = fo2.getName();
                try {
                    n = fo2.getFileSystem().getDecorator().annotateName(n, dobjs[i].files());
                } catch (FileStateInvalidException e) {
                    // Never mind.
                }
                list.remove(n);
            }
        }
    }
    comboBoxWebBrowser.removeAllItems ();
    if (!list.isEmpty()) {
        for (String tag : list) {
            comboBoxWebBrowser.addItem(tag);
        }
        if(select != null) {
            comboBoxWebBrowser.setSelectedItem(select);
        }
        labelWebBrowser.setEnabled(true);
        comboBoxWebBrowser.setEnabled(true);
        jSeparator2.setEnabled(true);
    } else {
        labelWebBrowser.setEnabled(false);
        comboBoxWebBrowser.setEnabled(false);
        jSeparator2.setEnabled(false);
    }
}
 
Example 16
Source File: DatabaseConnectionConvertor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the file describing the specified database connection.
 */
public static void remove(DatabaseConnection dbconn) throws IOException {
    String name = dbconn.getName();
    FileObject fo = FileUtil.getConfigFile(CONNECTIONS_PATH); //NOI18N
    // If CONNECTIONS_PATH can't be found (getConfigFile returns null)
    // its useless to try to delete any connection
    if (fo == null) {
        return;
    }
    DataFolder folder = DataFolder.findFolder(fo);
    DataObject[] objects = folder.getChildren();
    
    for (int i = 0; i < objects.length; i++) {
        InstanceCookie ic = objects[i].getCookie(InstanceCookie.class);
        if (ic != null) {
            Object obj;
            try {
                obj = ic.instanceCreate();
            } catch (ClassNotFoundException e) {
                continue;
            }
            if (obj instanceof DatabaseConnection) {
                DatabaseConnection connection = (DatabaseConnection)obj;
                if (connection.getName().equals(name)) {
                    objects[i].delete();
                    break;
                }
            }
        }
    }
}
 
Example 17
Source File: SafeDeleteUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void deletePackage(FileObject source) {
    ClassPath classPath = ClassPath.getClassPath(source, ClassPath.SOURCE);
    FileObject root = classPath != null ? classPath.findOwnerRoot(source) : null;

    DataFolder dataFolder = DataFolder.findFolder(source);

    FileObject parent = dataFolder.getPrimaryFile().getParent();
    // First; delete all files except packages

    try {
        DataObject ch[] = dataFolder.getChildren();
        boolean empty = true;
        for (int i = 0; ch != null && i < ch.length; i++) {
            if (!ch[i].getPrimaryFile().isFolder()) {
                ch[i].delete();
            }
            else if (empty && VisibilityQuery.getDefault().isVisible(ch[i].getPrimaryFile())) {
                // 156529: hidden folders should be considered as empty content
                empty = false;
            }
        }

        // If empty delete itself
        if ( empty ) {
            dataFolder.delete();
        }

        // Second; delete empty super packages, or empty folders when there is not root
        while (!parent.equals(root) && parent.getChildren().length == 0) {
            FileObject newParent = parent.getParent();
            parent.delete();
            parent = newParent;
        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

}
 
Example 18
Source File: TextImporter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void reorder( String fileName, FileObject categoryFolder, int dropIndex ) throws IOException {
    if( dropIndex < 0 )
        return;
    FileObject itemFile = categoryFolder.getFileObject(fileName, "xml" ); //NOI18N
    if( null == itemFile )
        return;
    DataFolder catDob = DataFolder.findFolder(categoryFolder);
    DataObject[] children = catDob.getChildren();
    
    DataObject dob = DataObject.find(itemFile);
    if( null == dob )
        return;
    
    int curIndex = -1;
    for( int i=0; i<children.length; i++ ) {
        if( children[i].equals(dob) ) {
            curIndex = i;
            break;
        } 
    }
    if( curIndex < 0 )
        return;
    
    DataObject[] sortedChildren = new DataObject[children.length];
    if( dropIndex >= sortedChildren.length )
        dropIndex = sortedChildren.length-1;
    sortedChildren[dropIndex] = dob;
    int index = 0;
    for( int i=0; i<sortedChildren.length; i++ ) {
        if( sortedChildren[i] != null ) {
            continue;
        }
        DataObject tmp = children[index++];
        if( dob.equals(tmp) ) {
            i--;
            continue;
        }
        sortedChildren[i] = tmp;
    }
    
    catDob.setOrder(sortedChildren);
}
 
Example 19
Source File: PackageRenameHandlerImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRename(Node node, String newName) {
    DataFolder dob = (DataFolder) node.getCookie(DataObject.class);
    FileObject fo = dob.getPrimaryFile();
    if (node.isLeaf()) {
        //rename empty package and don't try to do any refactoring
        try {
            if (!RefactoringUtils.isValidPackageName(newName)) {
                String msg = new MessageFormat(NbBundle.getMessage(RenameRefactoringPlugin.class,"ERR_InvalidPackage")).format(
                        new Object[] {newName}
                );
                
                DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
                    msg, NotifyDescriptor.INFORMATION_MESSAGE));
                return;
            }
            ClassPath sourcepath = ClassPath.getClassPath(fo, ClassPath.SOURCE);
            if (sourcepath == null) {
                throw new IOException("no sourcepath for " + fo);
            }
            FileObject root = sourcepath.findOwnerRoot(fo);
            if (root == null) {
                throw new IOException(fo + " not in its own sourcepath " + sourcepath);
            }
            FileObject newFolder = FileUtil.createFolder(root, newName.replace('.','/'));
            while (dob.getChildren().length == 0 && dob.isDeleteAllowed() && !dob.getPrimaryFile().equals(newFolder)) {
                DataFolder parent = dob.getFolder();
                dob.delete();
                dob = parent;
            }
        } catch (IOException ioe) {
            ErrorManager.getDefault().notify(ioe);
        }
        return;
    }

    InstanceContent ic = new InstanceContent();
    ic.add(node);
    ExplorerContext d = new ExplorerContext();
    d.setNewName(newName);
    ic.add(d);
    final Lookup l = new AbstractLookup(ic);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Action a = RefactoringActionsFactory.renameAction().createContextAwareInstance(l);
            if (Boolean.TRUE.equals(a.getValue("applicable"))) { //NOI18N
                a.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
            }
        }
    });
}
 
Example 20
Source File: WebBrowsersOptionsModel.java    From netbeans with Apache License 2.0 3 votes vote down vote up
public WebBrowsersOptionsModel() {
    
    FileObject servicesBrowsers = FileUtil.getConfigFile(BROWSERS_FOLDER);
        
    if (servicesBrowsers != null) {
        
        DataFolder folder = DataFolder.findFolder(servicesBrowsers);
        DataObject[] browserSettings = folder.getChildren();
        
        for (DataObject browserSetting : browserSettings) {
            
            InstanceCookie cookie = browserSetting.getCookie(InstanceCookie.class);
            FileObject primaryFile = browserSetting.getPrimaryFile();
            
            if (cookie != null && !Boolean.TRUE.equals(primaryFile.getAttribute(EA_HIDDEN))) {
                WebBrowserDesc browserDesc = new WebBrowserDesc(browserSetting);
                browsersList.add(browserDesc);
            }
            
        }
        
    }
    
    int index = 0;
    for (WebBrowserDesc desc : browsersList) {
        addElement(desc.getOrigName());
        index2desc.put(index++, desc);
    }
    
}