KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > list > command > core > Clipboard


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.list.command.core;
25
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.riotfamily.riot.dao.CopyAndPasteEnabledDao;
29 import org.riotfamily.riot.dao.CutAndPasteEnabledDao;
30 import org.riotfamily.riot.dao.RiotDao;
31 import org.riotfamily.riot.editor.EditorDefinitionUtils;
32 import org.riotfamily.riot.editor.ListDefinition;
33 import org.riotfamily.riot.editor.ui.EditorReference;
34 import org.riotfamily.riot.list.command.CommandContext;
35 import org.springframework.util.ObjectUtils;
36 import org.springframework.web.util.WebUtils;
37
38 public class Clipboard {
39
40     public static final int MODE_EMPTY = 0;
41
42     public static final int MODE_COPY = 1;
43
44     public static final int MODE_CUT = 2;
45
46     private static final String JavaDoc SESSION_ATTRIBUTE = Clipboard.class.getName();
47
48     private int mode;
49
50     private String JavaDoc objectId;
51
52     private String JavaDoc parentId;
53
54     private ListDefinition listDefinition;
55
56     public static Clipboard get(CommandContext context) {
57         return get(context.getRequest().getSession());
58     }
59
60     public static Clipboard get(HttpSession JavaDoc session) {
61         return (Clipboard) WebUtils.getOrCreateSessionAttribute(
62                 session, SESSION_ATTRIBUTE, Clipboard.class);
63     }
64
65     public void cut(CommandContext context) {
66         put(context, MODE_CUT);
67     }
68
69     public void copy(CommandContext context) {
70         put(context, MODE_COPY);
71     }
72
73     private void put(CommandContext context, int mode) {
74         this.mode = mode;
75         objectId = context.getObjectId();
76         parentId = context.getParentId();
77         listDefinition = context.getListDefinition();
78     }
79
80     public void clear() {
81         mode = MODE_EMPTY;
82         objectId = null;
83         parentId = null;
84         listDefinition = null;
85     }
86
87     public boolean isEmpty() {
88         return mode == MODE_EMPTY;
89     }
90
91     public Object JavaDoc getObject() {
92         if (!isEmpty()) {
93             return listDefinition.getListConfig().getDao().load(objectId);
94         }
95         return null;
96     }
97
98
99     public boolean canCopy(CommandContext context) {
100         return context.getDao() instanceof CopyAndPasteEnabledDao;
101     }
102
103     public boolean canCut(CommandContext context) {
104         return context.getDao() instanceof CutAndPasteEnabledDao
105                 && !isCut(context);
106     }
107
108     public boolean canPaste(CommandContext context) {
109         return !isEmpty()
110                 && isSupportedDao(context)
111                 && isCompatibleEntityClass(context)
112                 && (mode == MODE_COPY || !isSameParent(context))
113                 && !isCutObjectAncestor(context);
114     }
115
116     public void paste(CommandContext context) {
117         if (mode == MODE_CUT) {
118             pasteCut(context);
119         }
120         else if (mode == MODE_COPY) {
121             pasteCopied(context);
122         }
123         clear();
124     }
125
126     private void pasteCut(CommandContext context) {
127         CutAndPasteEnabledDao dao = (CutAndPasteEnabledDao) context.getDao();
128         Object JavaDoc item = dao.load(objectId);
129         Object JavaDoc parent = context.getBean();
130         dao.addChild(item, parent);
131         if (parentId != null) {
132             Object JavaDoc previousParent = EditorDefinitionUtils.loadParent(
133                     listDefinition, parentId);
134
135             CutAndPasteEnabledDao previousDao = (CutAndPasteEnabledDao)
136                     listDefinition.getListConfig().getDao();
137
138             previousDao.removeChild(item, previousParent);
139         }
140     }
141
142     private void pasteCopied(CommandContext context) {
143         CopyAndPasteEnabledDao dao = (CopyAndPasteEnabledDao) context.getDao();
144         Object JavaDoc item = dao.load(objectId);
145         Object JavaDoc parent = context.getBean();
146         dao.addCopy(item, parent);
147     }
148
149
150     public boolean isCut(CommandContext context) {
151         return mode == MODE_CUT && ObjectUtils.nullSafeEquals(
152                 objectId, context.getObjectId());
153     }
154
155     public boolean isCopied(CommandContext context) {
156         return mode == MODE_COPY && ObjectUtils.nullSafeEquals(
157                 objectId, context.getObjectId());
158     }
159
160     private boolean isSameParent(CommandContext context) {
161         return ObjectUtils.nullSafeEquals(parentId, context.getParentId());
162     }
163
164     private boolean isCutObjectAncestor(CommandContext context) {
165         if (mode == MODE_CUT) {
166             EditorReference ref = context.getListDefinition().createEditorPath(
167                     null, context.getParentId(),
168                     context.getMessageResolver()).getParent();
169
170             while (ref != null) {
171                 if (objectId.equals(ref.getObjectId())) {
172                     return true;
173                 }
174                 ref = ref.getParent();
175             }
176         }
177         return false;
178     }
179
180     private boolean isSupportedDao(CommandContext context) {
181         RiotDao dao = context.getDao();
182         return (mode == MODE_CUT && dao instanceof CutAndPasteEnabledDao)
183                 || (mode == MODE_COPY && dao instanceof CopyAndPasteEnabledDao);
184     }
185
186     private boolean isCompatibleEntityClass(CommandContext context) {
187         RiotDao sourceDao = listDefinition.getListConfig().getDao();
188         RiotDao dao = context.getDao();
189         return dao.getEntityClass().equals(sourceDao.getEntityClass());
190     }
191
192 }
193
Popular Tags