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.OMSVGDocument;
21  import org.vectomatic.dom.svg.OMSVGElement;
22  import org.vectomatic.dom.svg.OMSVGGElement;
23  import org.vectomatic.dom.svg.OMSVGSVGElement;
24  import org.vectomatic.dom.svg.impl.SVGElement;
25  import org.vectomatic.svg.edit.client.SvgrealApp;
26  import org.vectomatic.svg.edit.client.command.CommandFactoryBase;
27  import org.vectomatic.svg.edit.client.command.FactoryInstantiatorBase;
28  import org.vectomatic.svg.edit.client.command.GenericAddCommand;
29  import org.vectomatic.svg.edit.client.command.ICommand;
30  import org.vectomatic.svg.edit.client.command.IFactoryInstantiator;
31  import org.vectomatic.svg.edit.client.engine.SVGModel;
32  import org.vectomatic.svg.edit.client.model.ModelConstants;
33  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
34  
35  /**
36   * Command factory to add new groups to the the SVG model.
37   * @author laaglu
38   */
39  public class AddGroupCommandFactory extends CommandFactoryBase {
40  	@SuppressWarnings("serial")
41  	public static final IFactoryInstantiator<AddGroupCommandFactory> INSTANTIATOR = new FactoryInstantiatorBase<AddGroupCommandFactory>(ModelConstants.INSTANCE.addGroupCmdFactory(), ModelConstants.INSTANCE.addGroupCmdFactoryDesc()) {
42  		@Override
43  		public AddGroupCommandFactory create() {
44  			return new AddGroupCommandFactory();
45  		}
46  	};
47  
48  	@Override
49  	public IFactoryInstantiator<?> getInstantiator() {
50  		return INSTANTIATOR;
51  	}
52  
53  	@Override
54  	public void start(Object requester) {
55  		SVGModel owner = SvgrealApp.getApp().getActiveModel();
56  		OMSVGElement parent = owner.getTwinGroup();
57  		OMSVGSVGElement svg = parent.getOwnerSVGElement();
58  		OMSVGDocument document = (OMSVGDocument) svg.getOwnerDocument();
59  		OMSVGGElement group = document.createSVGGElement();
60  		parent.appendChild(group);
61  		
62  		SVGElement twin = (SVGElement) group.getElement();
63  		SVGElement element = (SVGElement) twin.cloneNode(true);
64  		owner.getElementGroup().getElement().appendChild(element);
65  		
66  		SVGElementModel model = owner.create(element, twin);
67  		SVGElementModel parentModel = owner.convert((SVGElement) owner.getElementGroup().getElement());
68  		owner.add(parentModel, model);
69  		ICommand command = new GenericAddCommand(this, model);
70  		owner.getCommandStore().addCommand(command);
71  	}
72  }