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;
19  
20  import org.vectomatic.client.UIConstants;
21  import org.vectomatic.client.UIMessages;
22  import org.vectomatic.client.rep.command.ChangeOrderingCommand;
23  import org.vectomatic.client.rep.command.CommandHistory;
24  import org.vectomatic.client.rep.controller.AboutPanel;
25  import org.vectomatic.client.rep.controller.ContextualMenuVisitor;
26  import org.vectomatic.client.rep.controller.CopyController;
27  import org.vectomatic.client.rep.controller.CutController;
28  import org.vectomatic.client.rep.controller.DeleteController;
29  import org.vectomatic.client.rep.controller.EditPathController;
30  import org.vectomatic.client.rep.controller.EditPolylineController;
31  import org.vectomatic.client.rep.controller.ExportController;
32  import org.vectomatic.client.rep.controller.GroupController;
33  import org.vectomatic.client.rep.controller.LineWidthController;
34  import org.vectomatic.client.rep.controller.MouseControllerSelector;
35  import org.vectomatic.client.rep.controller.NewEllipseController;
36  import org.vectomatic.client.rep.controller.NewPathController;
37  import org.vectomatic.client.rep.controller.NewPolylineController;
38  import org.vectomatic.client.rep.controller.NewRectController;
39  import org.vectomatic.client.rep.controller.OrderingController;
40  import org.vectomatic.client.rep.controller.PasteController;
41  import org.vectomatic.client.rep.controller.RedoController;
42  import org.vectomatic.client.rep.controller.RepresentationController;
43  import org.vectomatic.client.rep.controller.ResizeController;
44  import org.vectomatic.client.rep.controller.SelectShapeController;
45  import org.vectomatic.client.rep.controller.ShapeSelection;
46  import org.vectomatic.client.rep.controller.StyleController;
47  import org.vectomatic.client.rep.controller.UndoController;
48  import org.vectomatic.client.rep.controller.UngroupController;
49  import org.vectomatic.client.rep.view.DrawingView;
50  import org.vectomatic.common.model.Attribute;
51  import org.vectomatic.common.model.DrawingModel;
52  import org.vectomatic.common.model.FloatAttributeValue;
53  import org.vectomatic.common.model.style.Color;
54  import org.vectomatic.common.model.style.Palette;
55  import org.vectomatic.common.model.style.PaletteList;
56  
57  import com.google.gwt.core.client.EntryPoint;
58  import com.google.gwt.core.client.GWT;
59  import com.google.gwt.user.client.Command;
60  import com.google.gwt.user.client.DOM;
61  import com.google.gwt.user.client.Event;
62  import com.google.gwt.user.client.EventListener;
63  import com.google.gwt.user.client.ui.FormPanel;
64  import com.google.gwt.user.client.ui.Grid;
65  import com.google.gwt.user.client.ui.HorizontalPanel;
66  import com.google.gwt.user.client.ui.MenuBar;
67  import com.google.gwt.user.client.ui.RootPanel;
68  import com.google.gwt.user.client.ui.TextArea;
69  import com.google.gwt.user.client.ui.VerticalPanel;
70  
71  
72  /**
73   * Application root class
74   */
75  public class RepApplication implements EntryPoint,EventListener {
76  	public static RepApplication app;
77  	public UIConstants _constants;
78  	public UIMessages _messages;
79  	public UIImageBundle _icons;
80  	public TextArea debugArea;
81  	private DrawingView _view;
82  	private DrawingModel _model;
83  	private CommandHistory _history;
84  	private ShapeSelection _selection;
85  	private ClipBoard _clipBoard;
86  	private MouseControllerSelector _selector;
87  	private PaletteList _paletteList;
88  	private StyleController _lineStyleController;
89  	private StyleController _fillStyleController;
90  	private LineWidthController _lineWidthController;
91  	
92  	public void debugPrint(String s) {
93  		//debugArea.setText(debugArea.getText() + s + "\n");
94  	}
95  	
96  	public CommandHistory getHistory() {
97  		return _history;
98  	}
99  	
100 	public DrawingModel getModel() {
101 		return _model;
102 	}
103 	
104 	public ShapeSelection getSelection() {
105 		return _selection;
106 	}
107 	
108 	public ClipBoard getClipboard() {
109 		return _clipBoard;
110 	}
111 	
112 	public DrawingView getView() {
113 		return _view;
114 	}
115 
116 	public PaletteList getPalettes() {
117 		return _paletteList;
118 	}
119 
120 	public MouseControllerSelector getSelector() {
121 		return _selector;
122 	}
123 	
124 	public UIConstants getConstants() {
125 		return _constants;
126 	}
127 	
128 	public UIMessages getMessages() {
129 		return _messages;
130 	}
131 	
132 	public UIImageBundle getIcons() {
133 		return _icons;
134 	}
135 	
136 	public StyleController getLineStyleController() {
137 		return _lineStyleController;
138 	}
139 	
140 	public StyleController getFillStyleController() {
141 		return _fillStyleController;
142 	}
143 	
144 	public LineWidthController getLineWidthController() {
145 		return _lineWidthController;
146 	}
147 
148 	public void onModuleLoad() {
149 		_constants = (UIConstants)GWT.create(UIConstants.class);
150 		_messages = (UIMessages)GWT.create(UIMessages.class);
151 		_icons = (UIImageBundle)GWT.create(UIImageBundle.class);
152 		
153 		app = this;
154 		//debugArea = new TextArea();
155 		_model = new DrawingModel();
156 		_selection = new ShapeSelection(_model);
157 		_history = new CommandHistory(20);
158 	    _paletteList = new PaletteList();
159 	    _paletteList.addPalette(Palette.getEGA());
160 	    _paletteList.addPalette(Palette.getGOLD());
161 	    
162 		RepresentationController repController = new RepresentationController(this);
163 		_view = new DrawingView(repController, _model, 400, 400);
164 		_view.setTabIndex(1);
165 		_view.setFocus(true);
166 		_selector = new MouseControllerSelector(_view);	
167 		
168 		Attribute.LINE_STYLE.setName(_constants.lineStyleAttribute());
169 		Attribute.LINE_OPACITY.setName(_constants.lineOpacityAttribute());
170 		Attribute.LINE_PATTERN.setName(_constants.linePatternAttribute());
171 		Attribute.LINE_CAP.setName(_constants.lineCapAttribute());
172 		Attribute.LINE_WIDTH.setName(_constants.lineWidthAttribute());
173 		Attribute.FILL_STYLE.setName(_constants.fillStyleAttribute());
174 		Attribute.FILL_OPACITY.setName(_constants.fillOpacityAttribute());
175 
176 	    _lineStyleController = new StyleController(this, _constants.lineLabel(), Attribute.LINE_STYLE, Attribute.LINE_OPACITY, Color.BLACK, new FloatAttributeValue(1f));
177 		_fillStyleController = new StyleController(this, _constants.fillLabel(), Attribute.FILL_STYLE, Attribute.FILL_OPACITY, Color.fromRGB(0, 128, 255), new FloatAttributeValue(1f));
178 		_lineWidthController = new LineWidthController(this);
179 		_clipBoard = new ClipBoard();
180 		UndoController undoController = new UndoController(this);
181 		RedoController redoController = new RedoController(this);
182 		CopyController copyController = new CopyController(this);
183 		CutController cutController = new CutController(this);
184 		PasteController pasteController = new PasteController(this);
185 		GroupController groupController = new GroupController(this);
186 		UngroupController ungroupController = new UngroupController(this);
187 		DeleteController deleteController = new DeleteController(this);
188 		OrderingController orderingController = new OrderingController(this);
189 		ResizeController resizeController = new ResizeController(this, repController);
190 		
191 		FormPanel form = new FormPanel("export");
192 		ExportController exportController = new ExportController(this, form);
193 		
194 		ContextualMenuVisitor contextVisitor = new ContextualMenuVisitor(deleteController, ungroupController, groupController, orderingController, _view);
195 		
196 		SelectShapeController selectShapeController = new SelectShapeController(this, deleteController, contextVisitor);
197 		NewRectController newRectController = new NewRectController(this);
198 		NewEllipseController newEllipseController = new NewEllipseController(this);
199 		NewPolylineController newPolylineController = new NewPolylineController(this);
200 		EditPolylineController editPolylineController = new EditPolylineController(this);
201 		NewPathController newPathController = new NewPathController(this);
202 		EditPathController editPathController = new EditPathController(this);
203 		
204 		_selector.add(selectShapeController.getButton());
205 		_selector.add(newRectController.getButton());
206 		_selector.add(newEllipseController.getButton());
207 		_selector.add(newPolylineController.getButton());
208 		_selector.add(editPolylineController.getButton());
209 		_selector.add(newPathController.getButton());
210 		_selector.add(editPathController.getButton());
211 		_selector.selectController(newRectController.getButton());
212 
213 		final Grid commandButtons = new Grid(5, 2);
214 		commandButtons.setWidget(0, 0, selectShapeController.getButton());
215 		commandButtons.setWidget(0, 1, repController.getSaveButton());
216 		commandButtons.setWidget(1, 0, newRectController.getButton());
217 		commandButtons.setWidget(1, 1, newEllipseController.getButton());
218 		commandButtons.setWidget(2, 0, newPolylineController.getButton());
219 		commandButtons.setWidget(2, 1, editPolylineController.getButton());
220 		commandButtons.setWidget(3, 0, newPathController.getButton());
221 		commandButtons.setWidget(3, 1, editPathController.getButton());
222 		commandButtons.setWidget(4, 0, undoController.getUndoButton());
223 		commandButtons.setWidget(4, 1, redoController.getRedoButton());
224 
225 		final VerticalPanel toolBar = new VerticalPanel();
226 		toolBar.add(commandButtons);
227 
228 		HorizontalPanel row1 = new HorizontalPanel();
229 		row1.add(toolBar);
230 
231 		HorizontalPanel row2 = new HorizontalPanel();
232 	    row2.add(_lineStyleController.getWidget());
233 	    row2.add(_lineWidthController.getWidget());
234 	    row2.add(_fillStyleController.getWidget());
235 
236 		
237 		VerticalPanel col1 = new VerticalPanel();
238 		col1.add(row2);
239 		col1.add(_view);
240 		row1.add(col1);
241 		//debugArea.setCharacterWidth(80);
242 		//debugArea.setVisibleLines(20);
243 		//row1.add(debugArea);
244 		
245 		VerticalPanel col2 = new VerticalPanel();
246 		MenuBar editMenu = new MenuBar(true);
247 		editMenu.addItem(_constants.resetViewCommand(), new Command() {
248 			public void execute() {
249 				_view.reset();
250 			}			
251 		});
252 		editMenu.addItem(undoController.getUndoMenuItem());
253 		editMenu.addItem(redoController.getRedoMenuItem());
254 		editMenu.addItem(cutController.getCutMenuItem());
255 		editMenu.addItem(copyController.getCopyMenuItem());
256 		editMenu.addItem(pasteController.getPasteMenuItem());
257 		editMenu.addItem(exportController.getExportSVG11Item());
258 		editMenu.addItem(exportController.getExportSVG12Item());
259 		MenuBar modifyMenu = new MenuBar(true);
260 		modifyMenu.addItem(groupController.getGroupMenuItem());
261 		modifyMenu.addItem(ungroupController.getUngroupMenuItem());
262 		modifyMenu.addItem(orderingController.newMenuItem(_view, ChangeOrderingCommand.BRING_TO_FRONT));
263 		modifyMenu.addItem(orderingController.newMenuItem(_view, ChangeOrderingCommand.SEND_TO_BACK));
264 		modifyMenu.addItem(orderingController.newMenuItem(_view, ChangeOrderingCommand.BRING_FORWARD));
265 		modifyMenu.addItem(orderingController.newMenuItem(_view, ChangeOrderingCommand.SEND_BACKWARD));
266 		modifyMenu.addItem(resizeController.getResizeMenuItem(_view));
267 		MenuBar helpMenu = new MenuBar(true);
268 		helpMenu.addItem(_constants.helpCommand() , new Command() {
269 			public void execute() {
270 			}			
271 		});
272 		final AboutPanel panel = new AboutPanel();
273 		helpMenu.addItem(_constants.aboutCommand() , new Command() {
274 			public void execute() {
275 				panel.show();
276 			}			
277 		});
278 		MenuBar menuBar = new MenuBar();
279 		menuBar.addItem(_constants.editMenu(), editMenu);
280 		menuBar.addItem(_constants.modifyMenu(), modifyMenu);
281 		menuBar.addItem(_constants.helpMenu(), helpMenu);
282 		col2.add(menuBar);
283 		col2.add(row1);
284 		col2.add(form);
285 		
286 		//EventPanel eventPanel = new EventPanel(_view);
287 		//eventPanel.add(col1);
288 		//RootPanel.get().add(eventPanel);
289 		RootPanel.get().add(col2);
290 		repController.open();
291 		RootPanel rootPanel = RootPanel.get();
292 		rootPanel.sinkEvents(0xFFFFFFFF);
293 		DOM.setEventListener(RootPanel.getBodyElement(), this);
294 		_view.render();
295 	}
296 	
297 	public void onBrowserEvent(Event event) {
298 		_view.render();
299 	}
300 
301 
302 }