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.command.add;
19  
20  import org.vectomatic.dom.svg.OMSVGElement;
21  import org.vectomatic.dom.svg.impl.SVGElement;
22  import org.vectomatic.dom.svg.utils.SVGConstants;
23  import org.vectomatic.svg.edit.client.SvgrealApp;
24  import org.vectomatic.svg.edit.client.command.CommandFactoryBase;
25  import org.vectomatic.svg.edit.client.command.GenericAddCommand;
26  import org.vectomatic.svg.edit.client.command.ICommand;
27  import org.vectomatic.svg.edit.client.engine.SVGModel;
28  import org.vectomatic.svg.edit.client.gxt.panels.CommandFactoryToolBar;
29  import org.vectomatic.svg.edit.client.model.IMetadata;
30  import org.vectomatic.svg.edit.client.model.MetaModel;
31  import org.vectomatic.svg.edit.client.model.ModelCategory;
32  import org.vectomatic.svg.edit.client.model.svg.CssContextModel;
33  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
34  
35  import com.google.gwt.core.client.GWT;
36  
37  
38  /**
39   * Base class for command factories which add elements
40   * to the model.
41   */
42  public abstract class AddCommandFactoryBase extends CommandFactoryBase {
43  	/**
44  	 * The SVG model to which the element will be added
45  	 */
46  	protected SVGModel owner;
47  	/**
48  	 * If true, the command should create just one element
49  	 * before auto-stopping, otherwise, it should keep building
50  	 * commands until explicitely stopped
51  	 */
52  	protected boolean oneShot;
53  
54  	@Override
55  	public void start(Object requester) {
56  		GWT.log("AddCommandFactoryBase.start(" + requester + ")");
57  		super.start(requester);
58  		if (requester instanceof CommandFactoryToolBar) {
59  			// The request comes from the command factory combo		
60  			// Become the target command factory.
61  			oneShot = false;
62  		} else {
63  			// The request comes from the context menu. Create
64  			// just one element
65  			oneShot = true;
66  		}
67  	}
68  	
69  	protected void createCommand(OMSVGElement svgElement) {
70  		GWT.log("AddCommandFactoryBase.createCommand(" + svgElement + ")");
71  		SVGElement twin = (SVGElement) svgElement.getElement();
72  		SVGElement element = (SVGElement) twin.cloneNode(true);
73  		owner.getElementGroup().getElement().appendChild(element);
74  		SVGElementModel model = owner.create(element, twin);
75  		SVGElementModel parentModel = owner.convert((SVGElement) owner.getElementGroup().getElement());
76  		owner.add(parentModel, model);
77  		ICommand command = new GenericAddCommand(this, model);
78  		owner.getCommandStore().addCommand(command);
79  		svgElement.getStyle().clearSVGProperty(SVGConstants.CSS_VISIBILITY_PROPERTY);		
80  		if (oneShot) {
81  			stop();
82  		}
83  	}
84  	
85  	protected void applyCssContextStyle(SVGElement element) {
86  		MetaModel<SVGElement> metamodel = SVGModel.getMetamodel(element);
87  		CssContextModel cssContext = SvgrealApp.getApp().getCssContext();
88  		for (IMetadata metadata : metamodel.getCategory(ModelCategory.STROKEFILL).getMetadata()) {
89  			metadata.set(element, cssContext.get(metadata.getName()));
90  		}
91  	}
92  }