View Javadoc
1   // Copyright (C) 2010-2019 Yozons, Inc.
2   // CKEditor for Vaadin - Widget linkage for using CKEditor within a Vaadin application.
3   //
4   // This software is released under the Apache License 2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
5   //
6   // This software was originally based on the Vaadin incubator component TinyMCEEditor written by Matti Tahvonen.
7   //
8   package org.vaadin.openesignforms.ckeditor;
9   
10  //import org.vaadin.openesignforms.ckeditor.CKEditorTextField.SelectionChangeEvent;
11  //import org.vaadin.openesignforms.ckeditor.CKEditorTextField.SelectionChangeListener;
12  
13  import com.vaadin.server.VaadinRequest;
14  import com.vaadin.v7.shared.ui.label.ContentMode;
15  import com.vaadin.ui.UI;
16  import com.vaadin.annotations.Theme;
17  import com.vaadin.v7.data.Property;
18  import com.vaadin.v7.data.Property.ValueChangeEvent;
19  import com.vaadin.ui.Button;
20  import com.vaadin.v7.ui.HorizontalLayout;
21  import com.vaadin.v7.ui.Label;
22  import com.vaadin.ui.Notification;
23  //import com.vaadin.ui.Notification.Type;
24  import com.vaadin.v7.ui.TextField;
25  import com.vaadin.v7.ui.VerticalLayout;
26  import com.vaadin.ui.Window;
27  import com.vaadin.ui.Button.ClickEvent;
28  import com.vaadin.ui.Button.ClickListener;
29  
30  /**
31   * HISTORY:
32   * This is a port of VaadinCKEditorApplication 1.8.2 built using Vaadin 6.8.10, and we're giving it version 7.8.2
33   * to show it's the same basic code, just ported to Vaadin 7, but using the new VaadinCKEditorUI name.
34   * It is essentially a "legacy port" and does not yet take advantage of any of the new capabilities of Vaadin 7.0.5.
35   * @author Yozons, Inc.
36   */
37  
38  @Theme("ckexample") // not needed, but here to show if you wanted to style the v-ckeditortextfield style defined in VAADIN/themes/ckexample/styles.css
39  public class VaadinCKEditorUI extends UI {
40  	private static final long serialVersionUID = 1445049298116746927L;
41  
42  	@Override
43  	public void init(VaadinRequest request) {
44  		
45  		getPage().setTitle("Vaadin7 CKEditor UI");
46  		
47  		VerticalLayout mainView = new VerticalLayout();
48  		setContent(mainView);
49  		
50  		mainView.addComponent(new Button("Hit server"));
51  		
52  		Label separator = new Label("&nbsp;");
53  		separator.setContentMode(ContentMode.HTML);
54  		mainView.addComponent(separator); 
55  
56  
57  		/* See http://ckeditor.com/latest/samples/plugins/toolbar/toolbar.html for the official info.
58  		 * This is the full list as we know it in CKEditor 4.x
59  	[
60      { name: 'document', items : [ 'Source','-','NewPage','Preview','Print','-','Templates' ] },
61  	{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
62  	{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
63  	{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },
64  	'/',
65  	{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
66  	{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
67  	{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
68  	{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },
69  	'/',
70  	{ name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
71  	{ name: 'colors', items : [ 'TextColor','BGColor' ] },
72  	{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
73  	]
74  		 */
75  		
76  		final String editor1InitialValue = 
77  				"<p>Thanks TinyMCEEditor for getting us started on the CKEditor integration.</p>\n\n<h1>Like TinyMCEEditor said, &quot;Vaadin rocks!&quot;</h1>\n\n<h1>And CKEditor is no slouch either.</h1>\n";
78  
79  		CKEditorConfig config1 = new CKEditorConfig();
80  		config1.useCompactTags();
81  		config1.disableElementsPath();
82  		config1.setResizeDir(CKEditorConfig.RESIZE_DIR.HORIZONTAL);
83  		config1.disableSpellChecker();
84  		config1.setHeight("300px");
85  		
86  		final CKEditorTextFieldd.html#CKEditorTextField">CKEditorTextField ckEditorTextField1 = new CKEditorTextField(config1);
87  		ckEditorTextField1.setHeight("440px"); // account for 300px editor plus toolbars
88  		mainView.addComponent(ckEditorTextField1);
89  		
90  		ckEditorTextField1.setValue(editor1InitialValue);
91  		ckEditorTextField1.addValueChangeListener(new Property.ValueChangeListener() {
92  			private static final long serialVersionUID = -761434593559159149L;
93  
94  			public void valueChange(ValueChangeEvent event) {
95  				Notification.show("CKEditor v" + ckEditorTextField1.getVersion() + "/" + getVersion() + " - #1 contents: " + event.getProperty().getValue().toString());
96  			}
97  		});
98  		
99  		// This selection change listener is commented out for general use, but it does appear to work in preliminary testing as of 
100 		// version 7.10.2 (15 July 2015) if you need it.
101 		/*
102 		ckEditorTextField1.addSelectionChangeListener(new SelectionChangeListener() {
103 			private static final long serialVersionUID = 1270295222444271706L;
104 
105 			public void selectionChange(SelectionChangeEvent event) {
106 				if ( event.hasSelectedHtml() ) {
107 					Notification.show("CKEditor selected HTML: " + event.getSelectedHtml(), Type.ERROR_MESSAGE);
108 					ckEditorTextField1.focus();
109 				} else {
110 					Notification.show("CKEditor un-select reported", Type.ERROR_MESSAGE);
111 				}
112 			}
113 		});
114 		*/
115 		
116 		Button resetTextButton1 = new Button("Reset editor #1");
117 		resetTextButton1.addClickListener( new Button.ClickListener() {			
118 			private static final long serialVersionUID = 2872667648717255301L;
119 
120 			@Override
121 			public void buttonClick(ClickEvent event) {
122 				if ( ! ckEditorTextField1.isReadOnly() ) {
123 					ckEditorTextField1.setValue(editor1InitialValue);
124 				}
125 			}
126 		});
127 		
128 		Button toggleReadOnlyButton1 = new Button("Toggle read-only editor #1");
129 		toggleReadOnlyButton1.addClickListener( new Button.ClickListener() {			
130 			private static final long serialVersionUID = 8462908141468254844L;
131 
132 			@Override
133 			public void buttonClick(ClickEvent event) {
134 				ckEditorTextField1.setReadOnly( ! ckEditorTextField1.isReadOnly() );
135 			}
136 		});
137 
138 		Button toggleViewWithoutEditorButton1 = new Button("Toggle view-without-editor #1");
139 		toggleViewWithoutEditorButton1.addClickListener( new Button.ClickListener() {			
140 			private static final long serialVersionUID = 8122286299515325693L;
141 
142 			@Override
143 			public void buttonClick(ClickEvent event) {
144 				ckEditorTextField1.setViewWithoutEditor( ! ckEditorTextField1.isViewWithoutEditor() );
145 			}
146 		});
147 
148 		Button toggleVisibleButton1 = new Button("Toggle visible editor #1");
149 		toggleVisibleButton1.addClickListener( new Button.ClickListener() {			
150 			private static final long serialVersionUID = -6715135605688427318L;
151 
152 			@Override
153 			public void buttonClick(ClickEvent event) {
154 				ckEditorTextField1.setVisible( ! ckEditorTextField1.isVisible() );
155 			}
156 		});
157 
158 		HorizontalLayout buttonsLayout = new HorizontalLayout(resetTextButton1,toggleReadOnlyButton1,toggleViewWithoutEditorButton1,toggleVisibleButton1);
159 		buttonsLayout.setSpacing(true);
160 		mainView.addComponent( buttonsLayout );
161 
162 		separator = new Label("&nbsp;");
163 		separator.setContentMode(ContentMode.HTML);
164 		mainView.addComponent(separator); 
165 		
166 		// Now add in a second editor....
167 		final String editor2InitialValue = 
168 			"<p>Here is editor #2.</p>\n\n<p>Hope you find this useful in your Vaadin projects.</p>\n";
169 
170 		final CKEditorTextFieldd.html#CKEditorTextField">CKEditorTextField ckEditorTextField2 = new CKEditorTextField();
171 		ckEditorTextField2.setWidth("600px");
172 		mainView.addComponent(ckEditorTextField2);
173 		
174 		CKEditorConfig config2 = new CKEditorConfig();
175 		config2.addCustomToolbarLine("{ items : ['Source','Styles','Bold','VaadinSave','-','Undo','Redo','-','NumberedList','BulletedList'] }");
176 		config2.enableCtrlSWithVaadinSavePlugin();
177 		config2.addToRemovePlugins("scayt");
178 		ckEditorTextField2.setConfig(config2);
179 		ckEditorTextField2.setValue(editor2InitialValue);
180 		
181 		ckEditorTextField2.addValueChangeListener(new Property.ValueChangeListener() {
182 			private static final long serialVersionUID = 1522230917891035997L;
183 
184 			public void valueChange(ValueChangeEvent event) {
185 				Notification.show("CKEditor v" + ckEditorTextField2.getVersion() + "/" + getVersion() + " - #2 contents: " + event.getProperty().getValue().toString());
186 			}
187 		});
188 
189 		ckEditorTextField2.addVaadinSaveListener( new CKEditorTextField.VaadinSaveListener() {
190 			private static final long serialVersionUID = 3763779235559050613L;
191 
192 			@Override
193 			public void vaadinSave(CKEditorTextField editor) {
194 				Notification.show("CKEditor v" + ckEditorTextField2.getVersion() + "/" + getVersion() + " - #2 VaadinSave button pressed." + " - #2 contents: " + editor.getValue().toString());
195 			}
196 			
197 		});
198 
199 		Button resetTextButton2 = new Button("Reset editor #2");
200 		resetTextButton2.addClickListener( new Button.ClickListener() {			
201 			private static final long serialVersionUID = 4877506990872691752L;
202 
203 			@Override
204 			public void buttonClick(ClickEvent event) {
205 				if ( ! ckEditorTextField2.isReadOnly() ) {
206 					ckEditorTextField2.setValue(editor2InitialValue);
207 				}
208 			}
209 		});
210 		
211 		Button toggleReadOnlyButton2 = new Button("Toggle read-only editor #2");
212 		toggleReadOnlyButton2.addClickListener( new Button.ClickListener() {			
213 			private static final long serialVersionUID = 7388801260896778551L;
214 
215 			@Override
216 			public void buttonClick(ClickEvent event) {
217 				ckEditorTextField2.setReadOnly( ! ckEditorTextField2.isReadOnly() );
218 			}
219 		});
220 
221 		Button toggleViewWithoutEditorButton2 = new Button("Toggle view-without-editor #2");
222 		toggleViewWithoutEditorButton2.addClickListener( new Button.ClickListener() {			
223 			private static final long serialVersionUID = 6042124118599379679L;
224 
225 			@Override
226 			public void buttonClick(ClickEvent event) {
227 				ckEditorTextField2.setViewWithoutEditor( ! ckEditorTextField2.isViewWithoutEditor() );
228 			}
229 		});
230 
231 		Button toggleVisibleButton2 = new Button("Toggle visible editor #2");
232 		toggleVisibleButton2.addClickListener( new Button.ClickListener() {			
233 			private static final long serialVersionUID = -3804977370320346348L;
234 
235 			@Override
236 			public void buttonClick(ClickEvent event) {
237 				ckEditorTextField2.setVisible( ! ckEditorTextField2.isVisible() );
238 			}
239 		});
240 		
241 		buttonsLayout = new HorizontalLayout(resetTextButton2,toggleReadOnlyButton2,toggleViewWithoutEditorButton2,toggleVisibleButton2);
242 		buttonsLayout.setSpacing(true);
243 		mainView.addComponent( buttonsLayout );
244 
245 		separator = new Label("&nbsp;");
246 		separator.setContentMode(ContentMode.HTML);
247 		mainView.addComponent(separator); 
248 
249 		buttonsLayout = new HorizontalLayout();
250 		buttonsLayout.setSpacing(true);
251 		mainView.addComponent( buttonsLayout );
252 		
253 		buttonsLayout.addComponent(new Button("Open Modal Subwindow", new ClickListener() {                      
254 			private static final long serialVersionUID = 7661931879334525618L;
255 
256 			@Override
257             public void buttonClick(ClickEvent event) {
258                     Window sub = new Window("Subwindow modal");
259                     VerticalLayout subLayout = new VerticalLayout();
260                     sub.setContent(subLayout);
261                     
262                     CKEditorConfigKEditorConfig.html#CKEditorConfig">CKEditorConfig config = new CKEditorConfig();
263                     config.useCompactTags();
264                     config.disableElementsPath();
265                     config.disableSpellChecker();
266                     config.enableCtrlSWithVaadinSavePlugin();
267                     //config.enableVaadinSavePlugin();
268                     // set BaseFloatZIndex 1000 higher than CKEditor's default of 10000; probably a result of an editor opening
269                     // in a window that's on top of the main two editors of this demo app
270                     config.setBaseFloatZIndex(11000); 
271                     config.setHeight("150px");
272                     
273                     final CKEditorTextFieldld.html#CKEditorTextField">CKEditorTextField ckEditorTextField = new CKEditorTextField(config);
274 	                ckEditorTextField.addValueChangeListener(new Property.ValueChangeListener() {
275 						private static final long serialVersionUID = -1308863170484877239L;
276 
277 						public void valueChange(ValueChangeEvent event) {
278 							Notification.show("CKEditor v" + ckEditorTextField.getVersion() + "/" + getVersion() + " - POPUP MODAL contents: " + event.getProperty().getValue().toString());
279 	        			}
280 	        		});
281 	                ckEditorTextField.addVaadinSaveListener( new CKEditorTextField.VaadinSaveListener() {
282 						private static final long serialVersionUID = 5179094557786207192L;
283 
284 						@Override
285 	        			public void vaadinSave(CKEditorTextField editor) {
286 	        				Notification.show("CKEditor v" + ckEditorTextField.getVersion() + "/" + getVersion() + " - POPUP MODAL VaadinSave button pressed." + " - #2 contents: " + editor.getValue().toString());
287 	        			}
288 	        		});
289 	                ckEditorTextField.focus();
290                     
291 	                subLayout.addComponent(ckEditorTextField);
292                     
293                     sub.setWidth("80%");
294                     sub.setModal(true);
295                     sub.center();
296                     
297                     event.getButton().getUI().addWindow(sub);
298             }
299         }));
300 
301 		buttonsLayout.addComponent(new Button("Open Non-Modal Subwindow with 100% Height", new ClickListener() {                      
302 			private static final long serialVersionUID = 8895747367120494167L;
303 
304 			@Override
305 	        public void buttonClick(ClickEvent event) {
306 	                Window sub = new Window("Subwindow non-modal 100% height");
307 	                VerticalLayout subLayout = new VerticalLayout();
308 	                sub.setContent(subLayout);
309 	                sub.setWidth("80%");
310 	                sub.setHeight("500px");
311 
312 	                subLayout.setSizeFull();
313 	                
314 	                CKEditorConfigKEditorConfig.html#CKEditorConfig">CKEditorConfig config = new CKEditorConfig();
315 	                config.useCompactTags();
316 	                config.disableElementsPath();
317 	                config.disableSpellChecker();
318                     config.enableCtrlSWithVaadinSavePlugin();
319                     // set BaseFloatZIndex 1000 higher than CKEditor's default of 10000; probably a result of an editor opening
320                     // in a window that's on top of the main two editors of this demo app
321                     config.setBaseFloatZIndex(11000); 
322                     config.setStartupFocus(true);
323                     //config.setReadOnly(true);
324 	                
325 	                final CKEditorTextFieldld.html#CKEditorTextField">CKEditorTextField ckEditorTextField = new CKEditorTextField(config);
326 	                ckEditorTextField.setHeight("100%");
327 	                ckEditorTextField.addValueChangeListener(new Property.ValueChangeListener() {
328 						private static final long serialVersionUID = 5592423527258867304L;
329 
330 						public void valueChange(ValueChangeEvent event) {
331 							Notification.show("CKEditor v" + ckEditorTextField.getVersion() + "/" + getVersion() + " - POPUP NON-MODAL 100% HEIGHT contents: " + event.getProperty().getValue().toString());
332 	        			}
333 	        		});
334 	                ckEditorTextField.addVaadinSaveListener( new CKEditorTextField.VaadinSaveListener() {
335 						private static final long serialVersionUID = 2521954866396213777L;
336 
337 						@Override
338 	        			public void vaadinSave(CKEditorTextField editor) {
339 	        				Notification.show("CKEditor v" + ckEditorTextField.getVersion() + "/" + getVersion() + " - POPUP NON-MODAL 100% HEIGHT VaadinSave button pressed." + " - #2 contents: " + editor.getValue().toString());
340 	        			}
341 	        			
342 	        		});
343 	                subLayout.addComponent(ckEditorTextField);
344 	                subLayout.setExpandRatio(ckEditorTextField,10);
345 	                
346 	                final TextField textField = new TextField("TextField");
347 	                textField.addValueChangeListener(new Property.ValueChangeListener() {
348 						private static final long serialVersionUID = 6686202497483757206L;
349 
350 						public void valueChange(ValueChangeEvent event) {
351 							Notification.show("TextField - POPUP NON-MODAL 100% HEIGHT contents: " + event.getProperty().getValue().toString());
352 	        			}
353 	        		});
354 	                subLayout.addComponent(textField);
355 	                
356 	                sub.center();
357 	                
358 	                event.getButton().getUI().addWindow(sub);
359 	        }
360         }));
361 	}
362 	
363 	public String getVersion() {
364 		return "7.12.9";
365 	}
366 
367 }