1   /*
2    * Created on Sep 5, 2007
3    * 
4    * Copyright 2005 CafeSip.org 
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * You may obtain a copy of the License at 
9    *
10   *	http://www.apache.org/licenses/LICENSE-2.0 
11   *
12   * Unless required by applicable law or agreed to in writing, software 
13   * distributed under the License is distributed on an "AS IS" BASIS, 
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15   * See the License for the specific language governing permissions and 
16   * limitations under the License.
17   *
18   */
19  package org.cafesip.gwtcomp.examples.client;
20  
21  import org.cafesip.gwtcomp.client.ui.PopupMenu;
22  import org.cafesip.gwtcomp.client.ui.SuperTree;
23  import org.cafesip.gwtcomp.client.ui.SuperTreeItem;
24  import org.cafesip.gwtcomp.client.ui.SuperTreeListener;
25  import org.cafesip.gwtcomp.client.ui.TitleBar;
26  import org.cafesip.gwtcomp.client.utils.HTMLHelper;
27  
28  import com.google.gwt.event.dom.client.ClickEvent;
29  import com.google.gwt.event.dom.client.ClickHandler;
30  import com.google.gwt.user.client.Window;
31  import com.google.gwt.user.client.ui.CheckBox;
32  import com.google.gwt.user.client.ui.FlowPanel;
33  import com.google.gwt.user.client.ui.HTML;
34  import com.google.gwt.user.client.ui.HorizontalSplitPanel;
35  import com.google.gwt.user.client.ui.SimplePanel;
36  import com.google.gwt.user.client.ui.TreeItem;
37  
38  /**
39   * @author Amit Chatterjee
40   * 
41   */
42  public class SuperTreeSample extends FlowPanel
43  {
44      private static final int WORLD = 1;
45  
46      private static final int COUNTRY = 2;
47  
48      private static final int STATE = 3;
49  
50      private SuperTree tree;
51  
52      private HTML addState;
53  
54      private HTML viewInfo;
55  
56      private HTML listStates;
57  
58      private HTML deleteCountry;
59  
60      private PopupMenu countryPopup;
61  
62      /**
63       * A constructor for this class.
64       * 
65       * 
66       */
67      public SuperTreeSample()
68      {
69          super();
70          setSize("100%", "100%");
71  
72          init();
73      }
74  
75      private void init()
76      {
77          TitleBar superTreeTitle = new TitleBar("Super Tree", null, 1);
78          add(superTreeTitle);
79  
80          add(new HTML("<p>"));
81  
82          HorizontalSplitPanel hsp = new HorizontalSplitPanel();
83          add(hsp);
84                 
85          SimplePanel sp = new SimplePanel();
86          sp.setSize("100%", "100%");
87          hsp.setLeftWidget(sp);
88          hsp.setSplitPosition("25%");
89          hsp.setSize("100%", "500px");
90          
91          tree = new SuperTree();
92          tree.setMultiSelectionEnabled(true);
93          sp.setWidget(tree);
94  
95          initPopupMenu();
96  
97          SuperTreeItem world = new SuperTreeItem(HTMLHelper.imageWithText(
98                  "icons/html.png", "World"), WORLD);
99          tree.addItem(world);
100 
101         SuperTreeItem us = new SuperTreeItem(HTMLHelper.imageWithText(
102                 "icons/us.png", "USA"), COUNTRY);
103         us.setUserObject("us");
104         world.addItem(us);
105 
106         SuperTreeItem nc = new SuperTreeItem("North Carolina", STATE);
107         us.addItem(nc);
108 
109         SuperTreeItem va = new SuperTreeItem("Viriginia", STATE);
110         us.addItem(va);
111 
112         SuperTreeItem in = new SuperTreeItem(HTMLHelper.imageWithText(
113                 "icons/in.png", "India"), COUNTRY);
114         in.setUserObject("in");
115         world.addItem(in);
116 
117         SuperTreeItem wb = new SuperTreeItem("West Bengal", STATE);
118         in.addItem(wb);
119 
120         SuperTreeItem up = new SuperTreeItem("Punjab", STATE);
121         in.addItem(up);
122 
123         SuperTreeItem ca = new SuperTreeItem(HTMLHelper.imageWithText(
124                 "icons/ca.png", "Canada"), COUNTRY);
125         ca.setUserObject("ca");
126         world.addItem(ca);
127 
128         SuperTreeItem bc = new SuperTreeItem("British Columbia", STATE);
129         ca.addItem(bc);
130 
131         SuperTreeItem ab = new SuperTreeItem("Alberta", STATE);
132         ca.addItem(ab);
133 
134         initListener();
135     }
136 
137     private void initListener()
138     {
139         tree.addTreeListener(new SuperTreeListener()
140         {
141             public void onPopup(String level, PopupMenu menu,
142                     TreeItem[] treeItems)
143             {
144                 if (level.equals(WORLD + "." + COUNTRY))
145                 {
146                     if (treeItems.length > 1)
147                     {
148                         addState.setVisible(false);
149                         viewInfo.setVisible(false);
150                         return;
151                     }
152 
153                     addState.setVisible(true);
154                     viewInfo.setVisible(true);
155 
156                     TreeItem country = treeItems[0];
157                     String code = (String) country.getUserObject();
158                     if (code.equals("ca"))
159                     {
160                         // change the reference to the states to province
161                         addState.setHTML(HTMLHelper.imageWithText(
162                                 "gwtcomp-icons/edit_add.png", "Add Province"));
163                     }
164                     else
165                     {
166                         addState.setHTML(HTMLHelper.imageWithText(
167                                 "gwtcomp-icons/edit_add.png", "Add State"));
168                     }
169                 }
170             }
171 
172             public void onSelect(String level, TreeItem[] treeItems)
173             {
174                 Window.alert(treeItems.length + " item(s) selected at level "
175                         + level);
176             }
177 
178             public void onCollapse(TreeItem item)
179             {
180                 // System.out.println("Tree item collapsed");
181 
182             }
183 
184             public void onExpand(TreeItem item)
185             {
186                 // System.out.println("Tree item expanded");
187             }
188         });
189     }
190 
191     private void initPopupMenu()
192     {
193         countryPopup = new PopupMenu();
194         addState = new HTML(HTMLHelper.imageWithText(
195                 "gwtcomp-icons/edit_add.png", "Add State"));
196         countryPopup.addMenuItem(addState);
197 
198         viewInfo = new HTML(HTMLHelper.imageWithText("gwtcomp-icons/info.png",
199                 "View Information"));
200         countryPopup.addMenuItem(viewInfo);
201 
202         listStates = new HTML(HTMLHelper.imageWithText(
203                 "gwtcomp-icons/view_detailed.png", "List"));
204         countryPopup.addMenuItem(listStates);
205         countryPopup.addSeparator();
206 
207         deleteCountry = new HTML(HTMLHelper.imageWithText(
208                 "gwtcomp-icons/editdelete.png", "Delete"));
209         countryPopup.addMenuItem(deleteCountry);
210         
211         deleteCountry.addClickHandler(new ClickHandler(){
212 
213             public void onClick(ClickEvent event)
214             {
215                 countryPopup.hideAll();
216                 if (Window
217                         .confirm("Are you sure you want to delete the selected items?"))
218                 {
219                     Window.alert("This feature is not yet implemented. Sorry!");
220                 }
221                 
222             }});
223 
224         tree.setContextMenu(WORLD + "." + COUNTRY, countryPopup);
225 
226         PopupMenu trans = new PopupMenu();
227         trans.addMenuItem(new HTML("Roads"));
228         trans.addMenuItem(new HTML("Railway"));
229         trans.addMenuItem(new HTML("Aviation"));
230 
231         PopupMenu info = new PopupMenu();
232         info.addMenuItem(new HTML("Agriculture"));
233         info.addMenuItem(new HTML("Wildlife"));
234         info.addMenuItem(new HTML("Transportation"), trans);
235         info.addMenuItem(new CheckBox("Classified"));
236 
237         PopupMenu state = new PopupMenu();
238         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
239                 "gwtcomp-icons/edit_add.png", "Add City")));
240         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
241                 "gwtcomp-icons/info.png", "View Information")), info);
242         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
243                 "gwtcomp-icons/view_detailed.png", "List Cities")));
244         state.addSeparator();
245         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
246                 "gwtcomp-icons/editdelete.png", "Delete")));
247 
248         tree.setContextMenu(WORLD + "." + COUNTRY + "." + STATE, state);
249 
250     }
251 }