KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > command > CreateTextCommand


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.command;
29
30 import java.awt.Font JavaDoc;
31
32 import org.eclipse.draw2d.geometry.Rectangle;
33 import org.eclipse.gef.commands.Command;
34
35 import org.nightlabs.editor2d.DrawComponentContainer;
36 import org.nightlabs.editor2d.EditorPlugin;
37 import org.nightlabs.editor2d.TextDrawComponent;
38 import org.nightlabs.editor2d.impl.TextDrawComponentImpl;
39 import org.nightlabs.editor2d.request.TextCreateRequest;
40
41 public class CreateTextCommand
42 extends Command
43 {
44   /** DrawComponentContainer to add to. */
45   protected DrawComponentContainer parent;
46   public void setParent(DrawComponentContainer parent) {
47     this.parent = parent;
48   }
49   
50   protected Rectangle rect;
51   public void setLocation(Rectangle rect) {
52     this.rect = rect;
53   }
54   protected Rectangle getLocation() {
55     return rect;
56   }
57
58   protected boolean shapeAdded;
59   protected int drawOrderIndex;
60   protected TextCreateRequest request;
61   
62   public CreateTextCommand(TextCreateRequest request)
63   {
64     super();
65     setLabel(EditorPlugin.getResourceString("command.create.text"));
66     this.request = request;
67     this.textDrawComponent = (TextDrawComponent) request.getNewObject();
68   }
69
70   protected TextDrawComponent textDrawComponent;
71   public void execute()
72   {
73     int x = getLocation().x;
74     int y = getLocation().y;
75     Font JavaDoc newFont = new Font JavaDoc(request.getFontName(), request.getFontStyle(), request.getFontSize());
76     textDrawComponent = new TextDrawComponentImpl(request.getText(), newFont, x, y);
77     
78     parent.addDrawComponent(textDrawComponent);
79     textDrawComponent.setName(request.getText());
80     shapeAdded = true;
81     drawOrderIndex = parent.getDrawComponents().indexOf(textDrawComponent);
82   }
83   
84   public void redo()
85   {
86     parent.addDrawComponent(textDrawComponent, drawOrderIndex);
87   }
88   
89   /* (non-Javadoc)
90    * @see org.eclipse.gef.commands.Command#undo()
91    */

92   public void undo()
93   {
94     parent.removeDrawComponent(textDrawComponent);
95   }
96      
97 }
98
Popular Tags