public void createFolder(Text title)
// -------------
// -------------
{
// ---------
Folder f = new Folder();
if (title != null && !title.isEmpty())
{
f.title.set(title);
}
else
{
f.title.set(Text.fromString("NewFolder"));
}
subItems.add(f);
sort();
// ---------
}
public void createRegularDoc(Text title)
// -------------
// -------------
{
// ---------
RegularDocument file = new RegularDocument();
if (title != null && !title.isEmpty())
{
file.title.set(title);
}
else
{
file.title.set(Text.fromString("NewFile"));
}
subItems.add(file);
sort();
// ---------
}
public void copy(Folder to)
// -------------
// -------------
{
// ---------
if (this.equals(to)) {
throw new RuntimeException("Cannot copy to myself!");
}
for (Document selectedDoc : selected.read())
{
Document copy = TwoPassCloneManager.clone(selectedDoc, true);
to.subItems.add(copy);
}
to.sort();
// ---------
}
public void move(Folder to)
// -------------
// -------------
{
// ---------
if (this.equals(to)) {
throw new RuntimeException("Cannot move to myself!");
}
for (Document selectedDoc : selected.read())
{
this.selected.remove(selectedDoc);
this.subItems.remove(selectedDoc);
to.subItems.add(selectedDoc);
}
to.sort();
// ---------
}
public void deleteSelected()
// -------------
// -------------
{
// ---------
List toDelete = selected.read();
selected.clear();
for (Document document : toDelete)
{
document.destroy();
}
sort();
// ---------
}
public void clearSelection()
// -------------
// -------------
{
// ---------
selected.clear();
// ---------
}
public void selectAll()
// -------------
// -------------
{
// ---------
List initiallySelected = selected.read();
for (Document subItem : subItems.read())
{
if (!initiallySelected.contains(subItem))
{
selected.add(subItem);
}
}
// ---------
}
public void sort()
// -------------
// -------------
{
// ---------
List folders = new ArrayList();
List files = new ArrayList();
for (Document document : subItems.read()) {
if (document instanceof Folder) {
folders.add(document);
} else {
files.add(document);
}
}
Document.DocumentComparator comparator = new Document.DocumentComparator();
Collections.sort(folders, comparator);
Collections.sort(files, comparator);
List allDocuments = folders;
allDocuments.addAll(files);
subItems.set(allDocuments);
// ---------
}
// -------------
public boolean isParent(Document d)
{
for (Document subItem : subItems.read())
{
if (subItem.equals(d))
{
return true;
}
if (subItem instanceof Folder)
{
Folder subFolder = (Folder)subItem;
if (subFolder.isParent(d))
{
return true;
}
}
}
return false;
}
/**
* In megabytes.
* @return
*/
public double getSize()
{
return getSizeInBytes() / (1024.*1024.);
}
/**
* In bytes.
* @return
*/
public long getSizeInBytes()
{
long total = 0;
for (Document subItem : subItems.read())
{
if (subItem instanceof RegularDocument)
{
RegularDocument rd = (RegularDocument)subItem;
File file = rd.file.val();
if (file != null)
{
total = total + file.getFileSize();
}
}
else if (subItem instanceof PictureFromGallery)
{
PictureFromGallery pfg = (PictureFromGallery)subItem;
Picture picture = pfg.picture.val();
if (picture != null)
{
total = total + picture.getFileSize();
}
}
else if (subItem instanceof Folder)
{
Folder f = (Folder)subItem;
total = total + f.getSizeInBytes();
}
}
return total;
}
// -------------