package gr.grnet.pithos.web.client.foldertree;
+import gr.grnet.pithos.web.client.FolderContextMenu;
+import gr.grnet.pithos.web.client.Pithos;
+import gr.grnet.pithos.web.client.foldertree.FolderTreeView.Templates;
+import gr.grnet.pithos.web.client.rest.GetRequest;
+import gr.grnet.pithos.web.client.rest.RestException;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ContextMenuEvent;
+import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
+import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;
-import gr.grnet.pithos.web.client.FolderContextMenu;
-import gr.grnet.pithos.web.client.Pithos;
-import gr.grnet.pithos.web.client.foldertree.FolderTreeView.Templates;
-import gr.grnet.pithos.web.client.rest.GetRequest;
-import gr.grnet.pithos.web.client.rest.RestException;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
public class FolderTreeViewModel implements TreeViewModel {
+ protected Pithos app;
+
private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) {
@Override
- public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
+ public void render(@SuppressWarnings("unused") Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
String html;
- if (folder.isTrash())
- html = AbstractImagePrototype.create(FolderTreeView.images.emptyTrash()).getHTML();
- else
- html = AbstractImagePrototype.create(FolderTreeView.images.folderYellow()).getHTML();
+ SafeHtml name;
+ if (folder.isHome()) {
+ html = AbstractImagePrototype.create(FolderTreeView.images.home()).getHTML();
+ name = Templates.INSTANCE.nameSpan("Home");
+ }
+ else if (folder.isTrash()) {
+ html = AbstractImagePrototype.create(FolderTreeView.images.trash()).getHTML();
+ name = Templates.INSTANCE.nameSpan("Trash");
+ }
+ else {
+ html = AbstractImagePrototype.create(FolderTreeView.images.folderYellow()).getHTML();
+ name = Templates.INSTANCE.nameSpan(folder.getName());
+ }
safeHtmlBuilder.appendHtmlConstant(html);
- safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
+ safeHtmlBuilder.append(name);
}
@Override
- public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, Folder folder, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<Folder> valueUpdater) {
+ public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Folder folder, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<Folder> valueUpdater) {
if (event.getType().equals(ContextMenuEvent.getType().getName())) {
FolderTreeViewModel.this.selectionModel.setSelected(folder, true);
- if (!folder.isTrash()) {
- FolderContextMenu menu = new FolderContextMenu(FolderTreeView.images, folder);
- menu.setPopupPosition(event.getClientX(), event.getClientY());
- menu.show();
- }
+ FolderContextMenu menu = new FolderContextMenu(app, FolderTreeView.images, app.getSelectedTree(), folder);
+ menu.setPopupPosition(event.getClientX(), event.getClientY());
+ menu.show();
}
}
};
- private ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>();
+ protected ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>();
private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>();
- private SingleSelectionModel<Folder> selectionModel;
+ protected SingleSelectionModel<Folder> selectionModel;
- public FolderTreeViewModel(SingleSelectionModel<Folder> selectionModel) {
+ public FolderTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
+ app = _app;
this.selectionModel = selectionModel;
}
rootDataProvider.getList().add(f);
return new DefaultNodeInfo<Folder>(rootDataProvider, folderCell, selectionModel, null);
}
- else {
- final Folder f = (Folder) value;
- if (dataProviderMap.get(f) == null) {
- dataProviderMap.put(f, new ListDataProvider<Folder>());
- }
- final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
- fetchFolder(f, dataProvider);
- return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
- }
+ final Folder f = (Folder) value;
+ if (dataProviderMap.get(f) == null) {
+ dataProviderMap.put(f, new ListDataProvider<Folder>());
+ }
+ final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
+ fetchFolder(f, dataProvider, false, null);
+ return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
}
@Override
return false;
}
- private void fetchFolder(final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Set<Folder> folders) {
- final Pithos app = Pithos.get();
+ protected void fetchFolder(final Iterator<Folder> iter, final Command callback) {
if (iter.hasNext()) {
final Folder f = iter.next();
String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
- GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), app.getUsername(), path, f) {
+ GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
@Override
- public void onSuccess(Folder result) {
- fetchFolder(iter, dataProvider, folders);
+ public void onSuccess(@SuppressWarnings("unused") Folder _result) {
+ fetchFolder(iter, callback);
}
@Override
public void onError(Throwable t) {
GWT.log("Error getting folder", t);
if (t instanceof RestException)
- Pithos.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
+ app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
else
- Pithos.get().displayError("System error fetching folder: " + t.getMessage());
+ app.displayError("System error fetching folder: " + t.getMessage());
}
};
getFolder.setHeader("X-Auth-Token", app.getToken());
Scheduler.get().scheduleDeferred(getFolder);
}
- else {
- dataProvider.getList().clear();
- dataProvider.getList().addAll(folders);
- if (dataProvider.equals(rootDataProvider)) {
- selectionModel.setSelected(dataProvider.getList().get(0), true);
- Folder f = new Folder("Trash");
- f.setTrash(true);
- f.setContainer("trash");
- dataProvider.getList().add(f);
- Pithos.get().updateTags();
- }
- }
+ else if (callback != null)
+ callback.execute();
}
- public void initialize(AccountResource account) {
+ public void initialize(final AccountResource account) {
Iterator<Folder> iter = account.getContainers().iterator();
- fetchFolder(iter, rootDataProvider, account.getContainers());
+ fetchFolder(iter, new Command() {
+ @Override
+ public void execute() {
+ rootDataProvider.getList().clear();
+ rootDataProvider.getList().addAll(account.getContainers());
+ selectionModel.setSelected(rootDataProvider.getList().get(0), true);
+ }
+ });
}
public Folder getSelection() {
return selectionModel.getSelectedObject();
}
- public void updateFolder(Folder folder) {
+ public void updateFolder(Folder folder, boolean showfiles, Command callback) {
if (dataProviderMap.get(folder) == null) {
dataProviderMap.put(folder, new ListDataProvider<Folder>());
}
final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
- if (!folder.isTrash())
- fetchFolder(folder, dataProvider);
- else
- Pithos.get().showFiles(folder);
+ fetchFolder(folder, dataProvider, showfiles, callback);
}
- public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider) {
- dataProvider.flush();
+ public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles, final Command callback) {
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
- final Pithos app = Pithos.get();
String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
- GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), app.getUsername(), path, f) {
+ GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
@Override
- public void onSuccess(Folder result) {
- app.showFiles(result);
- Iterator<Folder> iter = result.getSubfolders().iterator();
- fetchFolder(iter, dataProvider, result.getSubfolders());
+ public void onSuccess(final Folder _result) {
+ if (showfiles)
+ app.showFiles(_result);
+ Iterator<Folder> iter = _result.getSubfolders().iterator();
+ fetchFolder(iter, new Command() {
+ @Override
+ public void execute() {
+ dataProvider.getList().clear();
+ dataProvider.getList().addAll(_result.getSubfolders());
+ app.getFolderTreeView().updateChildren(f);
+ if (callback != null)
+ callback.execute();
+ }
+ });
}
@Override
public void onError(Throwable t) {
GWT.log("Error getting folder", t);
if (t instanceof RestException)
- Pithos.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
+ app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
else
- Pithos.get().displayError("System error fetching folder: " + t.getMessage());
+ app.displayError("System error fetching folder: " + t.getMessage());
}
};
getFolder.setHeader("X-Auth-Token", app.getToken());