View Javadoc

1   package org.kuali.student.common.ui.client.configurable.mvc.layouts;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.kuali.student.common.ui.client.application.Application;
9   import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
10  import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView;
11  import org.kuali.student.common.ui.client.mvc.Callback;
12  import org.kuali.student.common.ui.client.mvc.View;
13  import org.kuali.student.common.ui.client.widgets.KSButton;
14  import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
15  import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData;
16  
17  import com.google.gwt.event.dom.client.ClickEvent;
18  import com.google.gwt.event.dom.client.ClickHandler;
19  import com.google.gwt.user.client.ui.FlowPanel;
20  
21  public class MenuEditableSectionController extends MenuSectionController{
22  	
23  	private boolean editMode = false;
24  	private boolean isEditable = true;
25  	
26  	//better way to do this other than 2 maps?
27  	private Map<Enum<?>, Enum<?>> readOnlyToEditMap = new HashMap<Enum<?>, Enum<?>>();
28  	private Map<Enum<?>, Enum<?>> editToReadOnlyMap = new HashMap<Enum<?>, Enum<?>>();
29  	
30  	private List<View> readOnlyViews = new ArrayList<View>();
31  	private List<KSButton> editLinks = new ArrayList<KSButton>();
32  	
33  	private Callback<Boolean> editLinkCallback = new Callback<Boolean>(){
34  
35  		@Override
36  		public void exec(Boolean result) {
37  			if(result == true){
38  				KSMenuItemData item = viewMenuItemMap.get(getCurrentView().getViewEnum());
39  				if(item == null){
40  					Enum<?> readOnlyEnum = editToReadOnlyMap.get(getCurrentView().getViewEnum());
41  					if(readOnlyEnum != null){
42  						item = viewMenuItemMap.get(readOnlyEnum);
43  					}
44  				}
45  				if(item != null && !item.isSelected()){
46  					item.setSelected(true, false);
47  				}
48  			}
49  		}
50  	};
51  
52  	public MenuEditableSectionController() {
53  		super();
54  	}
55  	
56  	public void addMenuItem(String parentMenu, final View readOnlyView, final View editView){
57  		super.addMenuItem(parentMenu, readOnlyView);
58  		this.addView(editView);
59  		readOnlyToEditMap.put(readOnlyView.getViewEnum(), editView.getViewEnum());
60  		editToReadOnlyMap.put(editView.getViewEnum(), readOnlyView.getViewEnum());
61  		attachEditLink(readOnlyView);
62  	}
63  	
64  	public void attachEditLink(final View v){
65  		readOnlyViews.add(v);
66  		createEditLink(v);
67  	}
68  	
69  	public KSButton generateEditLink(final View v){
70  		KSButton editLink = new KSButton(Application.getApplicationContext().getMessage("edit"), ButtonStyle.DEFAULT_ANCHOR, new ClickHandler(){
71  			
72  			@Override
73  			public void onClick(ClickEvent event) {
74  				editMode = true;
75  				showView(v.getViewEnum(), editLinkCallback);
76  			}
77  		});
78  		editLinks.add(editLink);
79  		
80  		editLink.addStyleName("ks-header-edit-link");
81  		
82  		return editLink;
83  	}
84  	
85  	private void createEditLink(final View v){
86  		if(isEditable){
87  			KSButton editLink = generateEditLink(v);
88  			
89  			if(v instanceof SectionView){
90  				//TODO Change to put in section header
91  				SectionTitle title = ((SectionView) v).getLayout().getLayoutTitle();
92  				if(title != null){
93  					title.add(editLink);
94  				}
95  				else{
96  					((SectionView) v).getLayout().insert(editLink, 0);
97  				}
98  			}
99  			else if(v.asWidget() instanceof FlowPanel){
100 				((FlowPanel)v.asWidget()).insert(editLink, 0);
101 			}
102 		}
103 	}
104 	
105 	private void detachEditLinks(){
106 		for(KSButton b: editLinks){
107 			b.removeFromParent();
108 		}
109 	}
110 	
111 	@Override
112 	public <V extends Enum<?>> void showView(V viewType, Callback<Boolean> onReadyCallback){
113 		//show the edit view only
114 		if(editMode && isEditable){
115 			Enum<?> editViewEnum = readOnlyToEditMap.get(viewType);
116 			if(editViewEnum != null){
117 				super.showView(editViewEnum, onReadyCallback);
118 			}
119 			else{
120 				super.showView(viewType, onReadyCallback);
121 			}
122 		}
123 		//show the read only view only
124 		else{
125 			Enum<?> readOnlyEnum = editToReadOnlyMap.get(viewType);
126 			if(readOnlyEnum != null){
127 				super.showView(readOnlyEnum, onReadyCallback);
128 			}
129 			else{
130 				super.showView(viewType, onReadyCallback);
131 			}
132 		}
133 	}
134 	
135 	public void setEditable(boolean editable){
136 		if(editable && this.isEditable != true){
137 			this.isEditable = editable;
138 			for(View v: readOnlyViews){
139 				createEditLink(v);
140 			}
141 		}
142 		else if(!editable && this.isEditable == true){
143 			this.isEditable = editable;
144 			detachEditLinks();
145 		}
146 	}
147 	
148 	public void setEditMode(final boolean editMode){
149 		if(this.editMode != editMode){
150 			this.editMode = editMode;
151 			this.showView(this.getCurrentViewEnum(), new Callback<Boolean>(){
152 
153 				@Override
154 				public void exec(Boolean result) {
155 					if(false){
156 						MenuEditableSectionController.this.editMode = !editMode;
157 					}
158 				}
159 			});
160 		}
161 	}
162 	
163 	public void addCommonEditButton(String parentMenu, KSButton button){
164 		if(parentMenu != null){
165 			List<View> views = menuViewMap.get(parentMenu);
166 			if(views != null){
167 				for(int i=0; i < views.size(); i++){
168 					Enum<?> editEnum = readOnlyToEditMap.get(views.get(i).getViewEnum());
169 					if(editEnum != null){
170 						addButtonForView(editEnum, button);
171 					}
172 				}
173 			}
174 		}
175 	}
176 	
177 }