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.Stack;
21  
22  import org.vectomatic.svg.edit.client.SvgrealApp;
23  import org.vectomatic.svg.edit.client.event.CommandFactorySelectorChangeEvent;
24  import org.vectomatic.svg.edit.client.event.CommandFactorySelectorChangeHandler;
25  import org.vectomatic.svg.edit.client.event.HasCommandFactorySelectorChangeHandlers;
26  import org.vectomatic.svg.edit.client.event.SelectionChangedProcessor;
27  import org.vectomatic.svg.edit.client.event.SelectionChangedProxy;
28  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
29  
30  import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
31  import com.extjs.gxt.ui.client.event.SelectionService;
32  import com.google.gwt.core.client.GWT;
33  import com.google.gwt.event.shared.GwtEvent;
34  import com.google.gwt.event.shared.HandlerRegistration;
35  
36  /**
37   * Class to manage the command factories and their lifecycle.
38   * @author laaglu
39   */
40  public class CommandFactorySelector implements HasCommandFactorySelectorChangeHandlers, SelectionChangedProcessor<SVGElementModel> {
41  	private Stack<ICommandFactory> factoryStack;
42  	private boolean suspended;
43  	
44  	public CommandFactorySelector() {
45  		factoryStack = new Stack<ICommandFactory>();
46  		SelectionService.get().addListener(new SelectionChangedProxy<SVGElementModel>(this));
47  	}
48  	
49  	/**
50  	 * Returns the currently active factory
51  	 * @return
52  	 */
53  	public ICommandFactory getActiveFactory() {
54  		if (suspended || factoryStack.empty()) {
55  			return null;
56  		}
57  		return factoryStack.peek();
58  	}
59  	
60  	/**
61  	 * Pops the current factory from the stack.
62  	 * @return
63  	 */
64  	public void popFactory() {
65  		GWT.log("CommandFactorySelector.popFactory(" + getActiveFactory().getInstantiator().getName() + ")");
66  		factoryStack.pop();
67  		fireFactoryStackChangedEvent();
68  	}
69  	
70  	/**
71  	 * Pushes the factory on the stack.
72  	 * @return
73  	 */
74  	public void pushFactory(ICommandFactory factory) {
75  		GWT.log("CommandFactorySelector.pushFactory(" + factory.getInstantiator().getName() + ")");
76  		factoryStack.push(factory);
77  		fireFactoryStackChangedEvent();
78  	}
79  	
80  	public int size() {
81  		return factoryStack.size();
82  	}
83  	
84  	public void fireFactoryStackChangedEvent() {
85  		GWT.log("fireFactoryStackChangedEvent: " + toString());
86  		fireEvent(new CommandFactorySelectorChangeEvent(getActiveFactory()));
87  	}
88  	
89  	@Override
90  	public void fireEvent(GwtEvent<?> event) {
91  		SvgrealApp.getApp().getEventBus().fireEventFromSource(event, this);
92  		
93  	}
94  	@Override
95  	public HandlerRegistration addCommandFactoryChangeHandler(CommandFactorySelectorChangeHandler handler) {
96  		return SvgrealApp.getApp().getEventBus().addHandlerToSource(CommandFactorySelectorChangeEvent.getType(), this, handler);
97  	}
98  
99  	public void suspendAll() {
100 		suspended = true;
101 	}
102 
103 	public void resumeAll() {
104 		suspended = false;
105 	}
106 	
107 	public boolean isSuspended() {
108 		return suspended;
109 	}
110 
111 	@Override
112 	public boolean processSelectionChanged(SelectionChangedEvent<SVGElementModel> se) {
113 		GWT.log("CommandFactorySelector.processSelectionChanged(" + se.getSelection() + ")");
114 		ICommandFactory factory = getActiveFactory();
115 		if (factory instanceof SelectionChangedProcessor) {
116 			((SelectionChangedProcessor)factory).processSelectionChanged(se);
117 		}
118 		return true;
119 	}
120 	
121 	@Override
122 	public String toString() {
123 		StringBuilder builder = new StringBuilder("CommandFactorySelector(suspended=");
124 		builder.append(suspended);
125 		builder.append("");
126 		builder.append(factoryStack);
127 		builder.append(")");
128 		return builder.toString();
129 	}
130 
131 }