View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.gxt.form;
19  
20  import org.vectomatic.svg.edit.client.AppConstants;
21  import org.vectomatic.svg.edit.client.gxt.panels.DashArrayEditor;
22  import org.vectomatic.svg.edit.client.gxt.widget.DashArrayCell;
23  import org.vectomatic.svg.edit.client.gxt.widget.SVGButton;
24  import org.vectomatic.svg.edit.client.model.svg.CssContextModel;
25  import org.vectomatic.svg.edit.client.model.svg.DashArray;
26  
27  import com.extjs.gxt.ui.client.event.ComponentEvent;
28  import com.extjs.gxt.ui.client.event.Events;
29  import com.extjs.gxt.ui.client.event.FieldEvent;
30  import com.extjs.gxt.ui.client.event.MenuEvent;
31  import com.extjs.gxt.ui.client.event.SelectionListener;
32  import com.extjs.gxt.ui.client.store.ListStore;
33  import com.extjs.gxt.ui.client.widget.LayoutContainer;
34  import com.extjs.gxt.ui.client.widget.form.AdapterField;
35  import com.extjs.gxt.ui.client.widget.layout.FitLayout;
36  import com.extjs.gxt.ui.client.widget.menu.AdapterMenuItem;
37  import com.extjs.gxt.ui.client.widget.menu.Menu;
38  import com.extjs.gxt.ui.client.widget.menu.MenuItem;
39  import com.google.gwt.core.client.GWT;
40  import com.google.gwt.event.dom.client.ClickEvent;
41  import com.google.gwt.event.dom.client.ClickHandler;
42  import com.google.gwt.event.logical.shared.CloseEvent;
43  import com.google.gwt.event.logical.shared.CloseHandler;
44  import com.google.gwt.event.logical.shared.ValueChangeEvent;
45  import com.google.gwt.event.logical.shared.ValueChangeHandler;
46  import com.google.gwt.event.shared.HandlerRegistration;
47  
48  /**
49   * Field subclass to edit DashArray values
50   * @author laaglu
51   */
52  public class DashArrayField extends AdapterField {
53  	private static class DashArrayMenuItem extends AdapterMenuItem implements ClickHandler {
54  		public DashArrayMenuItem(DashArrayCell cell) {
55  			super(cell);
56  			cell.addClickHandler(this);
57  		}
58  		
59  		@Override
60  		public void onClick(ClickEvent event) {
61  			ComponentEvent be = new ComponentEvent(this);
62  		    be.stopEvent();
63  		    MenuEvent me = new MenuEvent(parentMenu);
64  		    me.setItem(this);
65  		    me.setEvent(be.getEvent());
66  			if (!disabled && fireEvent(Events.Select, me)) {
67  				handleClick(be);
68  			}
69  		}
70  		
71  		public DashArrayCell getCell() {
72  			return (DashArrayCell)widget;
73  		}
74  		
75  	}
76  	
77  	private class DashArrayFieldPanel extends LayoutContainer {
78  		private DashArrayCell dashArrayCell;
79  		private SVGButton menuButton;
80  		private MenuItem editItem;
81  		private Menu menu;
82  		
83  		private SelectionListener<MenuEvent> menuListener = new SelectionListener<MenuEvent>() {
84  			@Override
85  			public void componentSelected(MenuEvent ce) {
86  				DashArrayCell cell = ((DashArrayMenuItem)ce.getItem()).getCell();
87  				setValue(cell.getDashArray());
88  				DashArrayField.this.fireEvent(Events.AfterEdit, new FieldEvent(DashArrayField.this));
89  			}
90  		};
91  		public DashArrayFieldPanel() {
92  			editItem = new MenuItem(AppConstants.INSTANCE.editDashArray());
93  			editItem.addSelectionListener(new SelectionListener<MenuEvent>() {
94  				private HandlerRegistration changeReg;
95  				private HandlerRegistration closeReg;
96  				@Override
97  				public void componentSelected(MenuEvent ce) {
98  					final DashArrayEditor editor = new DashArrayEditor();
99  					final DashArray oldValue = (DashArray) getValue();
100 					changeReg = editor.addValueChangeHandler(new ValueChangeHandler<DashArray>() {
101 						@Override
102 						public void onValueChange(ValueChangeEvent<DashArray> event) {
103 							GWT.log("DashArrayField.onValueChange(" + event.getValue() + ")");
104 							setValue(event.getValue());
105 						}					
106 					});
107 					closeReg = editor.addCloseHandler(new CloseHandler<DashArrayEditor>() {
108 						@Override
109 						public void onClose(CloseEvent<DashArrayEditor> event) {
110 							GWT.log("DashArrayField.onClose()");
111 							changeReg.removeHandler();
112 							closeReg.removeHandler();
113 							if (!event.isAutoClosed()) {
114 								// The user clicked the commit button in the dash array editor
115 								// Save the new default dash arrays
116 								CssContextModel.setDefaultDashArrays(editor.getDashArrays());
117 								// Record the change as a command
118 								DashArrayField.this.fireEvent(Events.AfterEdit, new FieldEvent(DashArrayField.this));
119 								// Update the button menu
120 								updateMenu();
121 							} else {
122 								// Restore the previous dash array
123 								setValue(oldValue);
124 							}
125 						}
126 					});
127 					editor.show();
128 				}
129 			});
130 			dashArrayCell = new DashArrayCell();
131 			menuButton = new SVGButton(dashArrayCell);
132 			setLayout(new FitLayout());
133 			add(menuButton);
134 			updateMenu();
135 		}
136 		public void update(DashArray value) {
137 			dashArrayCell.setDashArray(value);
138 		}
139 		private void updateMenu() {
140 			menu = new Menu();
141 			ListStore<DashArray> dashArrays = CssContextModel.getDefaultDashArrays();
142 			for (DashArray dashArray : dashArrays.getModels()) {
143 				DashArrayCell cell = new DashArrayCell();
144 				cell.setDashArray(dashArray);
145 				DashArrayMenuItem item = new DashArrayMenuItem(cell);
146 				item.addSelectionListener(menuListener);
147 				menu.add(item);
148 			}
149 			menu.add(editItem);
150 			menuButton.setMenu(menu);
151 		}
152 		
153 		@Override
154 		protected void onResize(int width, int height) {
155 			super.onResize(width, height);
156 			menu.setWidth(width);
157 		}
158 	}
159 	
160 	
161 	public DashArrayField() {
162 		super(null);
163 		widget = new DashArrayFieldPanel();
164 		setResizeWidget(true);
165 		setFireChangeEventOnSetValue(true);
166 	}
167 	
168 	@Override
169 	public void setValue(Object value) {
170 		((DashArrayFieldPanel)widget).update((DashArray)value);
171 		super.setValue(value);
172 	}
173 	
174 	@Override
175 	public Object getValue() {
176 		return value;
177 	}
178 }