SuperTreeSample.java
001 /*
002  * Created on Sep 5, 2007
003  
004  * Copyright 2005 CafeSip.org 
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License"); 
007  * you may not use this file except in compliance with the License. 
008  * You may obtain a copy of the License at 
009  *
010  *  http://www.apache.org/licenses/LICENSE-2.0 
011  *
012  * Unless required by applicable law or agreed to in writing, software 
013  * distributed under the License is distributed on an "AS IS" BASIS, 
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
015  * See the License for the specific language governing permissions and 
016  * limitations under the License.
017  *
018  */
019 package org.cafesip.gwtcomp.examples.client;
020 
021 import org.cafesip.gwtcomp.client.ui.PopupMenu;
022 import org.cafesip.gwtcomp.client.ui.SuperTree;
023 import org.cafesip.gwtcomp.client.ui.SuperTreeItem;
024 import org.cafesip.gwtcomp.client.ui.SuperTreeListener;
025 import org.cafesip.gwtcomp.client.ui.TitleBar;
026 import org.cafesip.gwtcomp.client.utils.HTMLHelper;
027 
028 import com.google.gwt.user.client.Window;
029 import com.google.gwt.user.client.ui.CheckBox;
030 import com.google.gwt.user.client.ui.ClickListener;
031 import com.google.gwt.user.client.ui.FlowPanel;
032 import com.google.gwt.user.client.ui.HTML;
033 import com.google.gwt.user.client.ui.TreeItem;
034 import com.google.gwt.user.client.ui.Widget;
035 
036 /**
037  @author Amit Chatterjee
038  
039  */
040 public class SuperTreeSample extends FlowPanel
041 {
042     private static final int WORLD = 1;
043 
044     private static final int COUNTRY = 2;
045 
046     private static final int STATE = 3;
047 
048     private static final int CITY = 4;
049 
050     private SuperTree tree;
051 
052     private HTML addState;
053 
054     private HTML viewInfo;
055 
056     private HTML listStates;
057 
058     private HTML deleteCountry;
059 
060     private PopupMenu countryPopup;
061 
062     /**
063      * A constructor for this class.
064      
065      
066      */
067     public SuperTreeSample()
068     {
069         super();
070         setWidth("100%");
071 
072         init();
073     }
074 
075     private void init()
076     {
077         TitleBar superTreeTitle = new TitleBar("Super Tree", null, 1);
078         add(superTreeTitle);
079 
080         add(new HTML("<p>"));
081         
082         tree = new SuperTree();
083         tree.setMultiSelectionEnabled(true);
084         add(tree);
085 
086         initPopupMenu();
087 
088         SuperTreeItem world = new SuperTreeItem(HTMLHelper.imageWithText(
089                 "icons/html.png""World"), WORLD);
090         tree.addItem(world);
091 
092         SuperTreeItem us = new SuperTreeItem(HTMLHelper.imageWithText(
093                 "icons/us.png""USA"), COUNTRY);
094         us.setUserObject("us");
095         world.addItem(us);
096 
097         SuperTreeItem nc = new SuperTreeItem("North Carolina", STATE);
098         us.addItem(nc);
099 
100         SuperTreeItem va = new SuperTreeItem("Viriginia", STATE);
101         us.addItem(va);
102 
103         SuperTreeItem in = new SuperTreeItem(HTMLHelper.imageWithText(
104                 "icons/in.png""India"), COUNTRY);
105         in.setUserObject("in");
106         world.addItem(in);
107 
108         SuperTreeItem wb = new SuperTreeItem("West Bengal", STATE);
109         in.addItem(wb);
110 
111         SuperTreeItem up = new SuperTreeItem("Punjab", STATE);
112         in.addItem(up);
113 
114         SuperTreeItem ca = new SuperTreeItem(HTMLHelper.imageWithText(
115                 "icons/ca.png""Canada"), COUNTRY);
116         ca.setUserObject("ca");
117         world.addItem(ca);
118 
119         SuperTreeItem bc = new SuperTreeItem("British Columbia", STATE);
120         ca.addItem(bc);
121 
122         SuperTreeItem ab = new SuperTreeItem("Alberta", STATE);
123         ca.addItem(ab);
124 
125         initListener();
126     }
127 
128     private void initListener()
129     {
130         tree.addTreeListener(new SuperTreeListener()
131         {
132             public void onPopup(String level, PopupMenu menu,
133                     TreeItem[] treeItems)
134             {
135                 if (level.equals(WORLD + "." + COUNTRY))
136                 {
137                     if (treeItems.length > 1)
138                     {
139                         addState.setVisible(false);
140                         viewInfo.setVisible(false);
141                         return;
142                     }
143 
144                     addState.setVisible(true);
145                     viewInfo.setVisible(true);
146 
147                     TreeItem country = treeItems[0];
148                     String code = (Stringcountry.getUserObject();
149                     if (code.equals("ca"))
150                     {
151                         // change the reference to the states to province
152                         addState.setHTML(HTMLHelper.imageWithText(
153                                 "gwtcomp-icons/edit_add.png""Add Province"));
154                     }
155                     else
156                     {
157                         addState.setHTML(HTMLHelper.imageWithText(
158                                 "gwtcomp-icons/edit_add.png""Add State"));
159                     }
160                 }
161             }
162 
163             public void onSelect(String level, TreeItem[] treeItems)
164             {
165                 Window.alert(treeItems.length + " item(s) selected at level "
166                         + level);
167             }
168 
169             public void onCollapse(TreeItem item)
170             {
171                 // System.out.println("Tree item collapsed");
172 
173             }
174 
175             public void onExpand(TreeItem item)
176             {
177                 // System.out.println("Tree item expanded");
178             }
179         });
180     }
181 
182     private void initPopupMenu()
183     {
184         countryPopup = new PopupMenu();
185         addState = new HTML(HTMLHelper.imageWithText(
186                 "gwtcomp-icons/edit_add.png""Add State"));
187         countryPopup.addMenuItem(addState);
188 
189         viewInfo = new HTML(HTMLHelper.imageWithText("gwtcomp-icons/info.png",
190                 "View Information"));
191         countryPopup.addMenuItem(viewInfo);
192 
193         listStates = new HTML(HTMLHelper.imageWithText(
194                 "gwtcomp-icons/view_detailed.png""List"));
195         countryPopup.addMenuItem(listStates);
196         countryPopup.addSeparator();
197 
198         deleteCountry = new HTML(HTMLHelper.imageWithText(
199                 "gwtcomp-icons/editdelete.png""Delete"));
200         countryPopup.addMenuItem(deleteCountry);
201         deleteCountry.addClickListener(new ClickListener()
202         {
203             public void onClick(Widget sender)
204             {
205                 countryPopup.hide();
206                 if (Window
207                         .confirm("Are you sure you want to delete the selected items?"))
208                 {
209                     Window.alert("This feature is not yet implemented. Sorry!");
210                 }
211             }
212         });
213 
214         tree.setContextMenu(WORLD + "." + COUNTRY, countryPopup);
215 
216         PopupMenu trans = new PopupMenu();
217         trans.addMenuItem(new HTML("Roads"));
218         trans.addMenuItem(new HTML("Railway"));
219         trans.addMenuItem(new HTML("Aviation"));
220 
221         PopupMenu info = new PopupMenu();
222         info.addMenuItem(new HTML("Agriculture"));
223         info.addMenuItem(new HTML("Wildlife"));
224         info.addMenuItem(new HTML("Transportation"), trans);
225         info.addMenuItem(new CheckBox("Classified"));
226 
227         PopupMenu state = new PopupMenu();
228         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
229                 "gwtcomp-icons/edit_add.png""Add City")));
230         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
231                 "gwtcomp-icons/info.png""View Information")), info);
232         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
233                 "gwtcomp-icons/view_detailed.png""List Cities")));
234         state.addSeparator();
235         state.addMenuItem(new HTML(HTMLHelper.imageWithText(
236                 "gwtcomp-icons/editdelete.png""Delete")));
237 
238         tree.setContextMenu(WORLD + "." + COUNTRY + "." + STATE, state);
239 
240     }
241 }