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 java.util.ArrayList;
21  import java.util.List;
22  
23  import org.vectomatic.client.rep.view.DrawingView;
24  
25  import com.google.gwt.user.client.ui.ClickListener;
26  import com.google.gwt.user.client.ui.Widget;
27  
28  /**
29   * Class to manage an array of buttons which
30   * determine which controller is catching events on the 2D view
31   */
32  public class MouseControllerSelector implements ClickListener {
33  	private DrawingView _view;
34  	private List<MouseControllerButton> _buttons;
35  
36  
37  	public MouseControllerSelector(DrawingView view) {
38  		_view = view;
39  		_buttons = new ArrayList<MouseControllerButton>();
40  	}
41  	
42  	public void add(MouseControllerButton button) {
43  		_buttons.add(button);
44  		button.addClickListener(this);
45  	}
46  	
47  	
48  	/**
49  	 * Invoked when a MouseControllerButton is clicked by the user
50  	 */
51  	public void onClick(Widget sender) {
52  		for (int i = 0, size = _buttons.size(); i < size; i ++) {
53  			MouseControllerButton button = _buttons.get(i);
54  			if (button == sender) {
55  				if (button.isDown()) {
56  					// Select the new button
57  					_view.getController().deactivate(_view);
58  					_view.setController(button.controller);
59  					button.controller.activate(_view);
60  				} else {
61  					 // The button is already selected
62  					button.setDown(true);
63  				}
64  			} else if (button.isDown()) {
65  				// Deselect the previously selected button
66  				button.setDown(false);
67  			}
68  		}
69  	}
70  	
71  	public void selectController(MouseControllerButton button) {
72  		if (!button.isDown()) {
73  			if (_view.getController() != null) {
74  				_view.getController().deactivate(_view);				
75  			}
76  			for (int i = 0, size = _buttons.size(); i < size; i ++) {
77  				MouseControllerButton currentButton = _buttons.get(i);
78  				if (currentButton.isDown()) {
79  					currentButton.setDown(false);
80  					break;
81  				}
82  			}
83  			button.setDown(true);
84  			_view.setController(button.controller);
85  			button.controller.activate(_view);
86  		}
87  	}
88  }