View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic 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   * Vectomatic 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 Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.client.rep.controller;
19  
20  import org.vectomatic.client.rep.RepApplication;
21  import org.vectomatic.client.rep.view.DrawingView;
22  import org.vectomatic.common.format.ISVGExporter;
23  import org.vectomatic.common.format.IOutputStream;
24  import org.vectomatic.common.format.SVG11Visitor;
25  import org.vectomatic.common.format.SVG12Visitor;
26  
27  import com.google.gwt.dom.client.Element;
28  import com.google.gwt.user.client.DOM;
29  import com.google.gwt.user.client.ui.FormPanel;
30  import com.google.gwt.user.client.ui.Hidden;
31  import com.google.gwt.user.client.ui.MenuItem;
32  
33  public class ExportController extends ControllerBase {
34  	private ControllerMenuItem _exportSVG11MenuItem;
35  	private ControllerMenuItem _exportSVG12MenuItem;
36  	private FormPanel _form;
37  	private Hidden _hidden;
38  
39  	public ExportController(RepApplication app, FormPanel form) {
40  		super(app);
41  		_form = form;
42  		form.setMethod(FormPanel.METHOD_POST);
43  		Element svgexport = DOM.getElementById("svgexport");
44  		String uri = "";
45  		if (svgexport != null) {
46  			uri = svgexport.getAttribute("content");
47  		}
48  		
49  		form.setAction(uri);
50  		_hidden = new Hidden();
51  		_hidden.setName("data");
52  		form.add(_hidden);
53  		_exportSVG11MenuItem = new ControllerMenuItem(app.getView(), app.getConstants().exportSVG11Command(), new ControllerBase(_app) {
54  			@Override
55  			public void activate(DrawingView view) {
56  				export(new SVG11Visitor());
57  			}			
58  		});
59  		_exportSVG12MenuItem = new ControllerMenuItem(app.getView(), app.getConstants().exportSVG12Command(), new ControllerBase(_app) {
60  			@Override
61  			public void activate(DrawingView view) {
62  				export(new SVG12Visitor());
63  			}			
64  		});
65  	}
66  	
67  	private void export(ISVGExporter exporter) {
68  		final StringBuffer buffer = new StringBuffer();
69  		exporter.writeSVG(new IOutputStream() {
70  			public void write(String str) {
71  				buffer.append(str);
72  			}
73  		}, _app.getModel().toShapeArray(), _app.getPalettes(), _app.getView().getWidth(), _app.getView().getHeight());
74  		_hidden.setValue(buffer.toString());
75  		_form.submit();
76  	}
77  	
78  
79  	public MenuItem getExportSVG11Item() {
80  		return _exportSVG11MenuItem;
81  	}
82  
83  	public MenuItem getExportSVG12Item() {
84  		return _exportSVG12MenuItem;
85  	}
86  
87  
88  }