KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > catedit > CategoryEditor


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.catedit;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21
22 import com.tonbeller.wcf.changeorder.ChangeOrderMgr;
23 import com.tonbeller.wcf.changeorder.ChangeOrderModel;
24 import com.tonbeller.wcf.changeorder.ChangeOrderUtils;
25 import com.tonbeller.wcf.component.Component;
26 import com.tonbeller.wcf.component.NestableComponentSupport;
27 import com.tonbeller.wcf.controller.Dispatcher;
28 import com.tonbeller.wcf.controller.DispatcherSupport;
29 import com.tonbeller.wcf.controller.RequestContext;
30 import com.tonbeller.wcf.controller.RequestListener;
31 import com.tonbeller.wcf.scroller.Scroller;
32 import com.tonbeller.wcf.utils.DomUtils;
33
34 /**
35  * The editor component. Creates a DOM that shows cat-item elements inside
36  * cat-category elements.
37  *
38  * @author av
39  */

40 public class CategoryEditor extends NestableComponentSupport implements ChangeOrderModel, CategoryModelChangeListener {
41   CategoryModel model;
42   String JavaDoc rootElementName = "cat-edit";
43   String JavaDoc changeCategoryButtonElementName = "cat-button";
44   CategoryElementRenderer categoryRenderer = new DefaultCategoryElementRenderer();
45   ItemElementRenderer itemRenderer = new DefaultItemElementRenderer();
46   Dispatcher dispatcher = new DispatcherSupport();
47
48   ChangeOrderMgr changeOrderMgr;
49
50   Document JavaDoc factory;
51   RequestContext context;
52
53   public CategoryEditor(String JavaDoc id, Component parent) {
54     super(id, parent);
55     super.getDispatcher().addRequestListener(null, null, dispatcher);
56   }
57
58   public CategoryEditor(String JavaDoc id, Component parent, CategoryModel model) {
59     super(id, parent);
60     super.getDispatcher().addRequestListener(null, null, dispatcher);
61     setModel(model);
62   }
63
64   public void initialize(RequestContext context) throws Exception JavaDoc {
65     super.initialize(context);
66     // super.getDispatcher() is not cleared
67
changeOrderMgr = new ChangeOrderMgr(super.getDispatcher(), this, this);
68   }
69
70   public Element JavaDoc render(RequestContext context, Document JavaDoc factory) throws Exception JavaDoc {
71     this.factory = factory;
72     this.context = context;
73
74     itemRenderer.startRendering(context);
75       
76     dispatcher.clear();
77     dispatcher.addRequestListener(null, null, itemRenderer);
78
79     Element JavaDoc rootElem = factory.createElement(rootElementName);
80     List JavaDoc categories = model.getCategories();
81     if (categories != null && categories.size() > 0)
82       renderRootElement(rootElem);
83
84     itemRenderer.stopRendering();
85
86     return rootElem;
87   }
88
89   void renderRootElement(Element JavaDoc rootElem) {
90     changeOrderMgr.startRendering(context);
91
92     for (Iterator JavaDoc it = model.getCategories().iterator(); it.hasNext();) {
93       Category cat = (Category) it.next();
94       Element JavaDoc catElem = renderCategory(cat);
95       rootElem.appendChild(catElem);
96     }
97
98     changeOrderMgr.stopRendering();
99   }
100
101   Element JavaDoc renderCategory(Category cat) {
102     Element JavaDoc catElem = categoryRenderer.render(context, factory, cat);
103     int count = cat.getItems().size();
104     int index = 0;
105     for (Iterator JavaDoc it = cat.getItems().iterator(); it.hasNext();) {
106       Item item = (Item) it.next();
107       Element JavaDoc itemElem = renderItem(item, cat);
108       catElem.appendChild(itemElem);
109       changeOrderMgr.renderButton(itemElem, cat, item, index, count);
110       ++index;
111     }
112     return catElem;
113   }
114
115   Element JavaDoc renderItem(Item item, Category cat) {
116     Element JavaDoc itemElem = itemRenderer.render(context, factory, cat, item);
117     int N = cat.getItems().size();
118
119     // add buttons to move this item to the other categories
120
if (item.isMovable() && (N > 1 || cat.isEmptyAllowed())) {
121       for (Iterator JavaDoc it = model.getCategories().iterator(); it.hasNext();) {
122         Category other = (Category) it.next();
123         if (other.equals(cat)) // dont move to the current category
124
continue;
125         Element JavaDoc buttonElem = renderChangeCategoryButton(item, cat, other);
126         itemElem.appendChild(buttonElem);
127       }
128     }
129     return itemElem;
130   }
131
132   Element JavaDoc renderChangeCategoryButton(Item item, Category source, Category target) {
133     Element JavaDoc buttonElem = factory.createElement(changeCategoryButtonElementName);
134     buttonElem.setAttribute("icon", target.getIcon());
135     String JavaDoc id = DomUtils.randomId();
136     buttonElem.setAttribute("id", id);
137     dispatcher.addRequestListener(id, null, new ChangeCategoryButtonHandler(item, source, target));
138     return buttonElem;
139   }
140
141   class ChangeCategoryButtonHandler implements RequestListener {
142     Item item;
143     Category source;
144     Category target;
145     ChangeCategoryButtonHandler(Item item, Category source, Category target) {
146       this.item = item;
147       this.source = source;
148       this.target = target;
149     }
150     public void request(RequestContext context) throws Exception JavaDoc {
151       Scroller.enableScroller(context);
152       validate(context);
153       source.removeItem(item);
154       target.addItem(item);
155     }
156   }
157
158   /**
159    * Returns the model.
160    * @return CategoryModel
161    */

162   public CategoryModel getModel() {
163     return model;
164   }
165
166   /**
167    * Sets the model.
168    * @param model The model to set
169    */

170   public void setModel(CategoryModel newModel) {
171     if (model != null)
172       model.removeCategoryModelChangeListener(this);
173     model = newModel;
174     if (model != null)
175       model.addCategoryModelChangeListener(this);
176   }
177
178   /**
179    * invalidates hyperlinks
180    * @param event
181    */

182   public void categoryModelChanged(CategoryModelChangeEvent event) {
183     itemRenderer.categoryModelChanged(event);
184     dispatcher.clear();
185   }
186
187   /**
188    * Returns the categoryRenderer.
189    * @return CategoryElementRenderer
190    */

191   public CategoryElementRenderer getCategoryRenderer() {
192     return categoryRenderer;
193   }
194
195   /**
196    * Returns the changeCategoryButtonElementName.
197    * @return String
198    */

199   public String JavaDoc getChangeCategoryButtonElementName() {
200     return changeCategoryButtonElementName;
201   }
202
203   /**
204    * Returns the itemRenderer.
205    * @return ItemElementRenderer
206    */

207   public ItemElementRenderer getItemRenderer() {
208     return itemRenderer;
209   }
210
211   /**
212    * Returns the rootElementName.
213    * @return String
214    */

215   public String JavaDoc getRootElementName() {
216     return rootElementName;
217   }
218
219   /**
220    * Sets the categoryRenderer.
221    * @param categoryRenderer The categoryRenderer to set
222    */

223   public void setCategoryRenderer(CategoryElementRenderer categoryRenderer) {
224     this.categoryRenderer = categoryRenderer;
225   }
226
227   /**
228    * Sets the changeCategoryButtonElementName.
229    * @param changeCategoryButtonElementName The changeCategoryButtonElementName to set
230    */

231   public void setChangeCategoryButtonElementName(String JavaDoc changeCategoryButtonElementName) {
232     this.changeCategoryButtonElementName = changeCategoryButtonElementName;
233   }
234
235   /**
236    * Sets the itemRenderer.
237    * @param itemRenderer The itemRenderer to set
238    */

239   public void setItemRenderer(ItemElementRenderer itemRenderer) {
240     this.itemRenderer = itemRenderer;
241   }
242
243   /**
244    * Sets the rootElementName.
245    * @param rootElementName The rootElementName to set
246    */

247   public void setRootElementName(String JavaDoc rootElementName) {
248     this.rootElementName = rootElementName;
249   }
250
251   /**
252    * @see com.tonbeller.wcf.changeorder.ChangeOrderModel#mayMove(Object)
253    */

254   public boolean mayMove(Object JavaDoc scope, Object JavaDoc item) {
255     Category cat = (Category) scope;
256     if (!cat.isOrderSignificant())
257       return false;
258     return cat.getItems().size() > 0 || cat.isEmptyAllowed();
259   }
260
261   /**
262    * implement ChangeOrderModel
263    */

264   public void move(Object JavaDoc scope, Object JavaDoc item, int oldIndex, int newIndex) {
265     Category cat = (Category) scope;
266     // browser back button etc
267
if (cat == null)
268       return;
269     // change element order in a copy (why?)
270
List JavaDoc items = new ArrayList JavaDoc();
271     items.addAll(cat.getItems());
272     ChangeOrderUtils.move(items, oldIndex, newIndex);
273     // save changes
274
cat.changeOrder(items);
275   }
276
277 }
278
Popular Tags