Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / foldertree / FolderTreeView.java @ 9e8e14e4

History | View | Annotate | Download (4.8 kB)

1 9e8e14e4 Christos Stathis
/*
2 9e8e14e4 Christos Stathis
 * Copyright (c) 2011 Greek Research and Technology Network
3 9e8e14e4 Christos Stathis
 */
4 9e8e14e4 Christos Stathis
5 9e8e14e4 Christos Stathis
package gr.grnet.pithos.web.client.foldertree;
6 9e8e14e4 Christos Stathis
7 9e8e14e4 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
8 9e8e14e4 Christos Stathis
import com.google.gwt.cell.client.Cell;
9 9e8e14e4 Christos Stathis
import com.google.gwt.core.client.GWT;
10 9e8e14e4 Christos Stathis
import com.google.gwt.resources.client.ClientBundle;
11 9e8e14e4 Christos Stathis
import com.google.gwt.resources.client.ImageResource;
12 9e8e14e4 Christos Stathis
import com.google.gwt.resources.client.ImageResource.ImageOptions;
13 9e8e14e4 Christos Stathis
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
14 9e8e14e4 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtml;
15 9e8e14e4 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
16 9e8e14e4 Christos Stathis
import com.google.gwt.user.cellview.client.CellTree;
17 9e8e14e4 Christos Stathis
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
18 9e8e14e4 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
19 9e8e14e4 Christos Stathis
import com.google.gwt.user.client.Event;
20 9e8e14e4 Christos Stathis
import com.google.gwt.user.client.IncrementalCommand;
21 9e8e14e4 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
22 9e8e14e4 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
23 9e8e14e4 Christos Stathis
import com.google.gwt.user.client.ui.Tree;
24 9e8e14e4 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
25 9e8e14e4 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
26 9e8e14e4 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
27 9e8e14e4 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
28 9e8e14e4 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
29 9e8e14e4 Christos Stathis
import com.google.gwt.view.client.TreeViewModel.NodeInfo;
30 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.CellTreeViewModel;
31 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.CellTreeViewUtils;
32 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.FolderContextMenu;
33 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.GSS;
34 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
35 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.RestResource;
36 9e8e14e4 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
37 9e8e14e4 Christos Stathis
import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTree;
38 9e8e14e4 Christos Stathis
import java.util.Arrays;
39 9e8e14e4 Christos Stathis
40 9e8e14e4 Christos Stathis
public class FolderTreeView extends Composite {
41 9e8e14e4 Christos Stathis
42 9e8e14e4 Christos Stathis
    static interface BasicResources extends CellTree.Resources {
43 9e8e14e4 Christos Stathis
44 9e8e14e4 Christos Stathis
        @ImageOptions(flipRtl = true)
45 9e8e14e4 Christos Stathis
        @Source("cellTreeClosedItem.gif")
46 9e8e14e4 Christos Stathis
        ImageResource cellTreeClosedItem();
47 9e8e14e4 Christos Stathis
48 9e8e14e4 Christos Stathis
        @ImageOptions(flipRtl = true)
49 9e8e14e4 Christos Stathis
        @Source("cellTreeLoadingBasic.gif")
50 9e8e14e4 Christos Stathis
        ImageResource cellTreeLoading();
51 9e8e14e4 Christos Stathis
52 9e8e14e4 Christos Stathis
        @ImageOptions(flipRtl = true)
53 9e8e14e4 Christos Stathis
        @Source("cellTreeOpenItem.gif")
54 9e8e14e4 Christos Stathis
        ImageResource cellTreeOpenItem();
55 9e8e14e4 Christos Stathis
56 9e8e14e4 Christos Stathis
        @Source({"GssCellTreeBasic.css"})
57 9e8e14e4 Christos Stathis
        CellTree.Style cellTreeStyle();
58 9e8e14e4 Christos Stathis
    }
59 9e8e14e4 Christos Stathis
60 9e8e14e4 Christos Stathis
    static interface Images extends ClientBundle,Tree.Resources, FolderContextMenu.Images {
61 9e8e14e4 Christos Stathis
62 9e8e14e4 Christos Stathis
        @Source("gr/grnet/pithos/resources/folder_home.png")
63 9e8e14e4 Christos Stathis
        ImageResource home();
64 9e8e14e4 Christos Stathis
65 9e8e14e4 Christos Stathis
        @Source("gr/grnet/pithos/resources/folder_yellow.png")
66 9e8e14e4 Christos Stathis
        ImageResource folderYellow();
67 9e8e14e4 Christos Stathis
    }
68 9e8e14e4 Christos Stathis
69 9e8e14e4 Christos Stathis
    private static Images images = GWT.create(Images.class);
70 9e8e14e4 Christos Stathis
71 9e8e14e4 Christos Stathis
    static interface Templates extends SafeHtmlTemplates {
72 9e8e14e4 Christos Stathis
        Templates INSTANCE = GWT.create(Templates.class);
73 9e8e14e4 Christos Stathis
74 9e8e14e4 Christos Stathis
        @Template("<span>{0}</span>")
75 9e8e14e4 Christos Stathis
        public SafeHtml nameSpan(String name);
76 9e8e14e4 Christos Stathis
      }
77 9e8e14e4 Christos Stathis
78 9e8e14e4 Christos Stathis
    static class FolderCell extends AbstractCell<Folder> {
79 9e8e14e4 Christos Stathis
80 9e8e14e4 Christos Stathis
        @Override
81 9e8e14e4 Christos Stathis
        public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
82 9e8e14e4 Christos Stathis
            String html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
83 9e8e14e4 Christos Stathis
            safeHtmlBuilder.appendHtmlConstant(html);
84 9e8e14e4 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
85 9e8e14e4 Christos Stathis
        }
86 9e8e14e4 Christos Stathis
    }
87 9e8e14e4 Christos Stathis
88 9e8e14e4 Christos Stathis
    private SingleSelectionModel<Folder> selectionModel = new SingleSelectionModel<Folder>();
89 9e8e14e4 Christos Stathis
90 9e8e14e4 Christos Stathis
    public FolderTreeView() {
91 9e8e14e4 Christos Stathis
        final FolderTreeViewModel model = new FolderTreeViewModel(selectionModel);
92 9e8e14e4 Christos Stathis
        /*
93 9e8e14e4 Christos Stathis
         * Create the tree using the model. We use <code>null</code> as the default
94 9e8e14e4 Christos Stathis
         * value of the root node. The default value will be passed to
95 9e8e14e4 Christos Stathis
         * CustomTreeModel#getNodeInfo();
96 9e8e14e4 Christos Stathis
         */
97 9e8e14e4 Christos Stathis
        CellTree.Resources res = GWT.create(BasicResources.class);
98 9e8e14e4 Christos Stathis
        DragAndDropCellTree tree = new DragAndDropCellTree(model,null, res);
99 9e8e14e4 Christos Stathis
100 9e8e14e4 Christos Stathis
        tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
101 9e8e14e4 Christos Stathis
102 9e8e14e4 Christos Stathis
        Handler selectionHandler = new SelectionChangeEvent.Handler() {
103 9e8e14e4 Christos Stathis
            @Override
104 9e8e14e4 Christos Stathis
            public void onSelectionChange(com.google.gwt.view.client.SelectionChangeEvent event) {
105 9e8e14e4 Christos Stathis
                NodeInfo<Folder> nodeInfo = (NodeInfo<Folder>) model.getNodeInfo(selectionModel.getSelectedObject());
106 9e8e14e4 Christos Stathis
                if(nodeInfo == null || nodeInfo.getValueUpdater() == null) {
107 9e8e14e4 Christos Stathis
                    //GSS.get().showFileList(selectionModel.getSelectedObject());
108 9e8e14e4 Christos Stathis
                }
109 9e8e14e4 Christos Stathis
                else
110 9e8e14e4 Christos Stathis
                    nodeInfo.getValueUpdater().update(selectionModel.getSelectedObject());
111 9e8e14e4 Christos Stathis
                GSS.get().setCurrentSelection(selectionModel.getSelectedObject());
112 9e8e14e4 Christos Stathis
113 9e8e14e4 Christos Stathis
114 9e8e14e4 Christos Stathis
            }
115 9e8e14e4 Christos Stathis
        };
116 9e8e14e4 Christos Stathis
        selectionModel.addSelectionChangeHandler(selectionHandler);
117 9e8e14e4 Christos Stathis
        sinkEvents(Event.ONCONTEXTMENU);
118 9e8e14e4 Christos Stathis
        sinkEvents(Event.ONMOUSEUP);
119 9e8e14e4 Christos Stathis
        initWidget(tree);
120 9e8e14e4 Christos Stathis
    }
121 9e8e14e4 Christos Stathis
}