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;
19  
20  import java.util.List;
21  
22  import org.vectomatic.svg.edit.client.SvgrealApp;
23  import org.vectomatic.svg.edit.client.engine.SVGModel;
24  import org.vectomatic.svg.edit.client.event.SelectionChangedProcessor;
25  import org.vectomatic.svg.edit.client.gxt.widget.CommandFactoryMenuItem;
26  import org.vectomatic.svg.edit.client.model.ModelConstants;
27  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
28  import org.vectomatic.svg.edit.client.model.svg.SVGNamedElementModel;
29  
30  import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
31  import com.extjs.gxt.ui.client.util.Format;
32  import com.google.gwt.core.client.GWT;
33  
34  /**
35   * Command factory to build commands which remove elements from the model
36   * @author laaglu
37   */
38  public class RemoveElementsCommandFactory extends CommandFactoryBase implements SelectionChangedProcessor<SVGElementModel> {
39  	@SuppressWarnings("serial")
40  	public static final IFactoryInstantiator<RemoveElementsCommandFactory> INSTANTIATOR = new FactoryInstantiatorBase<RemoveElementsCommandFactory>(ModelConstants.INSTANCE.removeElementsCmdFactory(), ModelConstants.INSTANCE.removeElementsCmdFactoryDesc()) {
41  		@Override
42  		public RemoveElementsCommandFactory create() {
43  			return new RemoveElementsCommandFactory();
44  		}
45  	};
46  
47  	@Override
48  	public IFactoryInstantiator<?> getInstantiator() {
49  		return INSTANTIATOR;
50  	}
51  
52  	@Override
53  	public void start(Object requester) {
54  		GWT.log("RemoveElementsCommandFactory.start(" + requester + ")");
55  		SVGModel svgModel = SvgrealApp.getApp().getActiveModel();
56  		if (requester instanceof SVGModel || requester instanceof CommandFactoryMenuItem) {
57  			// The request comes from the DEL key or the context menu.
58  			// Erase the elements, but do not push the factory on the stack.
59  			createCommand(svgModel);
60  			return;
61  		} 
62  		super.start(requester);
63  
64  		// The request comes from the command factory combo
65  		// Clear the selection and listen to selection events
66  		if (svgModel != null) {
67  			svgModel.getSelectionModel().deselectAll();
68  		}
69  		
70  		// Become the target command factory.
71  		updateStatus(ModelConstants.INSTANCE.removeElementsCmdFactory1());
72  	}
73  
74  	@Override
75  	public boolean processSelectionChanged(SelectionChangedEvent<SVGElementModel> se) {
76  		List<SVGElementModel> models = se.getSelection();
77  		GWT.log("RemoveElementsCommandFactory.processSelectionChanged: " + models);
78  		if (models.size() > 0) {
79  			createCommand(models.get(0).getOwner());
80  			return true;
81  		}
82  		return false;
83  	}
84  	
85  	private void createCommand(SVGModel svgModel) {
86  		GWT.log("RemoveElementsCommandFactory.createCommand(" + svgModel + ")");
87  		if (svgModel != null) {
88  			List<SVGElementModel> models = svgModel.getSelectionModel().getSelectedItems();
89  			// Prevent the viewBox from being removed
90  			models.remove(svgModel.getViewBox());
91  			if (models.size() > 0) {
92  				GenericRemoveCommand command = new GenericRemoveCommand(this, models, Format.substitute(ModelConstants.INSTANCE.removeCmd(), SVGNamedElementModel.getNames(models)));
93  				command.commit();
94  				svgModel.getCommandStore().addCommand(command);
95  			}
96  		}
97  	}
98  }