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.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.vectomatic.client.rep.RepApplication;
26  import org.vectomatic.client.rep.command.ChangeOrderingCommand;
27  import org.vectomatic.client.rep.events.IShapeSelectionListener;
28  import org.vectomatic.client.rep.view.DrawingView;
29  import org.vectomatic.common.model.Shape;
30  
31  import com.google.gwt.user.client.ui.MenuItem;
32  
33  /**
34   * Controller to respond to shape ordering requests and turn
35   * them into ChangeOrderingCommand
36   */
37  public class OrderingController implements IShapeSelectionListener {
38  	private RepApplication _app;
39  	private Map<String,List<ControllerMenuItem>> _directionToMenuItemList;
40  	private String[] _descriptions;
41  
42  	public OrderingController(RepApplication app) {
43  		_app = app;
44  		_app.getSelection().addShapeSelectionListener(this);
45  		_directionToMenuItemList = new HashMap<String,List<ControllerMenuItem>>();
46  		_descriptions = new String[] {
47  				_app.getConstants().bringToFrontCommand(),
48  				_app.getConstants().sendToBackCommand(),
49  				_app.getConstants().bringForwardCommand(),
50  				_app.getConstants().sendBackwardCommand()
51  		};
52  		selectionChanged(_app.getSelection());
53  	}
54  
55  	public void selectionChanged(ShapeSelection selection) {
56  		List<Shape> selectedShapes = selection.getSelectedShapes();
57  		for (int i = 0; i < 4; i++) {
58  			boolean enabled = false;
59  			switch(i) {
60  				case ChangeOrderingCommand.BRING_TO_FRONT:
61  					enabled = _app.getModel().canBringToFront(selectedShapes);
62  					break;
63  				case ChangeOrderingCommand.SEND_TO_BACK:
64  					enabled = _app.getModel().canSendToBack(selectedShapes);
65  					break;
66  				case ChangeOrderingCommand.BRING_FORWARD:
67  					enabled = _app.getModel().canBringForward(selectedShapes);
68  					break;
69  				case ChangeOrderingCommand.SEND_BACKWARD:
70  					enabled = _app.getModel().canSendBackward(selectedShapes);
71  					break;
72  			}
73  			List<ControllerMenuItem> menuItemList = _directionToMenuItemList.get(_descriptions[i]);
74  			if (menuItemList != null) {
75  				for (int j = 0, size = menuItemList.size(); j < size; j++) {
76  					menuItemList.get(j).setEnabled(enabled);
77  				}
78  			}
79  		}
80  	}
81  
82  	public MenuItem newMenuItem(final DrawingView view, final int direction) {
83  		List<ControllerMenuItem> menuItemList = _directionToMenuItemList.get(_descriptions[direction]);
84  		if (menuItemList == null) {
85  			menuItemList = new ArrayList<ControllerMenuItem>();
86  			_directionToMenuItemList.put(_descriptions[direction], menuItemList);
87  		}
88  		ControllerMenuItem menuItem = new ControllerMenuItem(view, _descriptions[direction], new ControllerBase(_app) {
89  			@Override
90  			public void activate(DrawingView view) {
91  				ChangeOrderingCommand changeOrderingCommand = new ChangeOrderingCommand(_app, direction);
92  				changeOrderingCommand.execute();
93  				_app.getHistory().addCommand(changeOrderingCommand);
94  			}
95  		});
96  		menuItem.setEnabled(false);
97  		menuItemList.add(menuItem);
98  		return menuItem;
99  	}
100 
101 	public MenuItem newContextItem(DrawingView view, final int direction) {
102 		List<ControllerMenuItem> menuItemList = _directionToMenuItemList.get(_descriptions[direction]);
103 		if (menuItemList == null) {
104 			menuItemList = new ArrayList<ControllerMenuItem>();
105 			_directionToMenuItemList.put(_descriptions[direction], menuItemList);
106 		}
107 		ControllerContextItem menuItem = new ControllerContextItem(view, _descriptions[direction], new ControllerBase(_app) {
108 			@Override
109 			public void activate(DrawingView view) {
110 				ChangeOrderingCommand changeOrderingCommand = new ChangeOrderingCommand(_app, direction);
111 				changeOrderingCommand.execute();
112 				_app.getHistory().addCommand(changeOrderingCommand);
113 			}
114 		});
115 		menuItem.setEnabled(false);
116 		menuItemList.add(menuItem);
117 		return menuItem;
118 	}
119 
120 }