public void destroy() throws IOException { FileObject parent = dataFolder.getPrimaryFile().getParent(); // First; delete all files except packages 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 { empty = false; } } // If empty delete itself if (empty) { super.destroy(); } // Second; delete empty super packages while (!parent.equals(root) && parent.getChildren().length == 0) { FileObject newParent = parent.getParent(); parent.delete(); parent = newParent; } }
public DataObject[] getChildren() { DataObject[] arr = folder.getChildren(); ArrayList list = new ArrayList(arr.length); for (int i = 0; i < arr.length; i++) { if (arr[i] instanceof DataFolder) continue; list.add(arr[i]); } return list.size() == arr.length ? arr : (DataObject[]) list.toArray(new DataObject[0]); }
public void setName(String name) { PackageRenameHandler handler = getRenameHandler(); if (handler != null) { handler.handleRename(this, name); return; } if (isDefaultPackage) { return; } String oldName = getName(); if (oldName.equals(name)) { return; } if (!isValidPackageName(name)) { DialogDisplayer.getDefault() .notify( new NotifyDescriptor.Message( NbBundle.getMessage(PackageViewChildren.class, "MSG_InvalidPackageName"), NotifyDescriptor.INFORMATION_MESSAGE)); return; } name = name.replace('.', '/') + '/'; // NOI18N oldName = oldName.replace('.', '/') + '/'; // NOI18N int i; for (i = 0; i < oldName.length() && i < name.length(); i++) { if (oldName.charAt(i) != name.charAt(i)) { break; } } i--; int index = oldName.lastIndexOf('/', i); // NOI18N String commonPrefix = index == -1 ? null : oldName.substring(0, index); String toCreate = (index + 1 == name.length()) ? "" : name.substring(index + 1); // NOI18N try { FileObject commonFolder = commonPrefix == null ? this.root : this.root.getFileObject(commonPrefix); FileObject destination = commonFolder; StringTokenizer dtk = new StringTokenizer(toCreate, "/"); // NOI18N while (dtk.hasMoreTokens()) { String pathElement = dtk.nextToken(); FileObject tmp = destination.getFileObject(pathElement); if (tmp == null) { tmp = destination.createFolder(pathElement); } destination = tmp; } FileObject source = this.dataFolder.getPrimaryFile(); DataFolder sourceFolder = DataFolder.findFolder(source); DataFolder destinationFolder = DataFolder.findFolder(destination); DataObject[] children = sourceFolder.getChildren(); for (int j = 0; j < children.length; j++) { if (children[j].getPrimaryFile().isData()) { children[j].move(destinationFolder); } } while (!commonFolder.equals(source)) { if (source.getChildren().length == 0) { FileObject tmp = source; source = source.getParent(); tmp.delete(); } else { break; } } } catch (IOException ioe) { ErrorManager.getDefault().notify(ioe); } }