View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.configurable.mvc.multiplicity;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
23  import org.kuali.student.common.ui.client.mvc.Callback;
24  
25  import com.google.gwt.user.client.ui.Composite;
26  import com.google.gwt.user.client.ui.FlowPanel;
27  import com.google.gwt.user.client.ui.Widget;
28  
29  /**
30   * A multiplicity composite allows a users to add/remove/display a list of 
31   * item widgets. The item widget to be used must be provided by the createItem()
32   * method, which can be a MultiplicitySection, another MultiplicityComposite, 
33   * or any widget that supports the HasModelDTOValue interface.  
34   * 
35   * @author Kuali Student Team
36   *
37   */
38  
39  /**
40   * @deprecated
41   */
42  public abstract class MultiplicityComposite extends Composite {
43  
44      protected FlowPanel mainPanel = new FlowPanel();
45      protected FlowPanel itemsPanel = new FlowPanel();
46      protected boolean loaded = false;
47      protected int itemCount = 0;
48      protected int visualItemCount = 0;
49      protected int minEmptyItems = 0;
50      protected List<MultiplicityItem> items = new ArrayList<MultiplicityItem>();
51      protected List<MultiplicityItem> removed = new ArrayList<MultiplicityItem>();
52      protected Set<Integer> itemKeys;
53      public static enum StyleType{TOP_LEVEL, SUB_LEVEL};
54      protected StyleType style;
55      protected SectionTitle titleWidget = SectionTitle.generateEmptyTitle();
56      
57      public MultiplicityComposite(StyleType style){
58      	this.style = style;
59          initWidget(mainPanel);
60      }
61          
62      protected Callback<MultiplicityItem> removeCallback = new Callback<MultiplicityItem>(){
63  
64          public void exec(MultiplicityItem itemToRemove) {
65              //items.remove(itemToRemove);
66          	visualItemCount--;
67              itemToRemove.setDeleted(true);
68              removed.add(itemToRemove);
69              itemsPanel.remove(itemToRemove);
70          }
71      };
72          
73      
74      /**
75       * This adds an item to the multiplicity composite by calling createItem.
76       * 
77       * @return
78       */
79  	public MultiplicityItem addItem(){
80  		itemCount++;
81  		visualItemCount++;
82  	    //itemCount = itemsPanel.getWidgetCount();
83  	    MultiplicityItem item = getItemDecorator(style);
84  	    Widget itemWidget = createItem();
85      	
86  	    if (item != null){
87  		    item.setItemKey(new Integer(itemCount -1));
88  		    item.setItemWidget(itemWidget);
89  		    item.setRemoveCallback(removeCallback);
90  	    } else if (itemWidget instanceof MultiplicityItem){
91  	    	item = (MultiplicityItem)itemWidget;
92  	    	item.setItemKey(new Integer(itemCount -1));
93  	    }
94  	    items.add(item);
95  	    item.redraw();
96  	    itemsPanel.add(item);
97  	    
98  	    return item;
99  	}
100 	
101 	/**
102 	 * This returns the index key for the model for the item currently being added by addItem
103 	 * This is useful, if you need to refer to the index in the createItem method
104 	 * @return
105 	 */
106 	public int getAddItemKey(){
107 		return itemCount-1;
108 	}
109 	
110 	public void incrementItemKey(){
111 		itemCount++;
112 	}
113        
114     public void onLoad() {
115         if (!loaded) {            
116             //mainPanel.addStyleName("KS-Multiplicity-Composite");
117         	//mainPanel.add(titleWidget);
118             mainPanel.add(itemsPanel);
119            
120             Widget addWidget = generateAddWidget();
121             if (addWidget != null){
122                 mainPanel.add(addWidget);
123             }
124             
125             loaded = true;
126         }
127         
128         if (!loaded || itemCount == 0){
129             for (int i=0; i < minEmptyItems; i++){
130             	addItem();
131             }        	
132         }
133     }
134 
135     public List<MultiplicityItem> getItems() {
136         return items;
137     }
138 
139     public List<MultiplicityItem> getRemovedItems() {
140         return removed;
141     }
142 
143     public void clear(){
144         itemsPanel.clear();
145         items.clear();
146         removed.clear();
147         itemCount = 0;
148     }
149    	
150     public void redraw(){
151         for (MultiplicityItem item:items){
152             item.redraw();
153         }
154     }
155     
156     /**
157      * This method will set the number of minimum empty rows to display on initial display of widget.   
158      */
159     public void setMinEmptyItems(int minCount){
160     	this.minEmptyItems = minCount;
161     }
162 
163     /**
164      * Method used to get the item decorator for each multiplicity item 
165      */
166     public abstract MultiplicityItem getItemDecorator(StyleType style);
167 
168     
169     /**
170      * Method used to create a instance of the multiplicity item
171      */
172     public abstract Widget createItem();
173 
174     /**
175      * Method used to get the add button to add additional multiplicity item
176      * 
177      * @return
178      */
179     public abstract Widget generateAddWidget();
180 
181 }