KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > command > RemoveFromAreaCommand


1 /*
2  * Copyright 2005 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.ensmer.input.command;
18
19 import com.buchuki.ensmer.Area;
20 import com.buchuki.ensmer.AreaManager;
21 import com.buchuki.ensmer.EnsmerManager;
22 import com.buchuki.ensmer.SceneGraphUtils;
23 import com.buchuki.ensmer.SelectionManager;
24 import com.buchuki.ensmer.input.event.EnsmerInputEvent;
25
26 /**
27  * Command to remove all selected objects from a specific area and place them
28  * in the previous area, assuming the previous area is not read only and assuming
29  * that the area being removed from is the current area.
30  *
31  * @author Dusty Phillips [dusty@buchuki.com]
32  */

33 public class RemoveFromAreaCommand implements Command {
34     
35     /**
36      * Creates a new instance of RemoveFromAreaCommand
37      */

38     public RemoveFromAreaCommand(Long JavaDoc areaID) {
39         this.areaID = areaID;
40     }
41     
42     /**
43      * Execute the command on the specific AreaID
44      *
45      * @param event the event that invoked the command
46      */

47     public boolean execute(EnsmerInputEvent event) {
48         EnsmerManager ens = EnsmerManager.instance();
49         AreaManager man = ens.getAreaManager();
50         SelectionManager sel = ens.getSelectionManager();
51
52         Area area = man.getArea(areaID);
53         if (man.getCurrentArea() != area) {
54             return false;
55         }
56         Area prev = man.getArea(man.getHistoricalAreaID(1));
57         if (prev == null || prev.isReadOnly()) {
58             return false;
59         }
60         Object JavaDoc[] objs = sel.getSelectedObjects();
61         for (Object JavaDoc obj : objs) {
62             Long JavaDoc id = (Long JavaDoc) obj;
63             area.removeObject(id);
64             prev.addObject(id);
65             prev.setObjectPosition(id, SceneGraphUtils.randomPositionInFrontOfUser(prev));
66         }
67         man.previousArea();
68         for (Object JavaDoc obj : objs) {
69             sel.toggleSelection((Long JavaDoc) obj);
70         }
71         return true;
72     }
73     
74     /**
75      * The identifier of the area that objects should be removed from and placed
76      * in previous.
77      */

78     private Long JavaDoc areaID;
79 }
80
Popular Tags