KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > riot > command > EditPageCommand


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) 2007
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.pages.riot.command;
25
26 import java.util.Locale JavaDoc;
27
28 import org.riotfamily.pages.Page;
29 import org.riotfamily.pages.PageValidationUtils;
30 import org.riotfamily.pages.dao.PageDao;
31 import org.riotfamily.riot.list.command.CommandContext;
32 import org.riotfamily.riot.list.command.CommandResult;
33 import org.riotfamily.riot.list.command.core.EditCommand;
34 import org.riotfamily.riot.list.command.result.ShowListResult;
35
36 public class EditPageCommand extends EditCommand {
37
38     public static final String JavaDoc ACTION_TRANSLATE = "translate";
39
40     private PageDao pageDao;
41
42
43     public EditPageCommand(PageDao pageDao) {
44         this.pageDao = pageDao;
45     }
46
47     protected String JavaDoc getAction(CommandContext context) {
48         return PageCommandUtils.isTranslated(context)
49                 ? super.getAction(context)
50                 : ACTION_TRANSLATE;
51     }
52
53     protected boolean isEnabled(CommandContext context, String JavaDoc action) {
54         if (action == ACTION_TRANSLATE) {
55             Page page = PageCommandUtils.getPage(context);
56             Locale JavaDoc locale = PageCommandUtils.getParentLocale(context);
57             return PageValidationUtils.isTranslatable(page, locale);
58         }
59         return true;
60     }
61
62     protected String JavaDoc getItemStyleClass(CommandContext context, String JavaDoc action) {
63         StringBuffer JavaDoc styleClasses = new StringBuffer JavaDoc();
64         if (action == ACTION_TRANSLATE) {
65             appendStyleClass(styleClasses, "foreign-page");
66         }
67         /* TODO: It would be better to set these styles in a different location. */
68         if (PageCommandUtils.isSystemPage(context)) {
69             appendStyleClass(styleClasses, "system-page");
70         }
71         if (PageCommandUtils.isHiddenNode(context)) {
72             appendStyleClass(styleClasses, "hidden-node");
73         }
74         if (PageCommandUtils.isHiddenPage(context)) {
75             appendStyleClass(styleClasses, "hidden-page");
76         }
77         if (PageCommandUtils.isFolder(context)) {
78             appendStyleClass(styleClasses, "folder");
79         }
80         return styleClasses.toString();
81     }
82
83     private void appendStyleClass(StringBuffer JavaDoc buffer, String JavaDoc styleClass) {
84         if (buffer.length() > 0) {
85             buffer.append(" ");
86         }
87         buffer.append(styleClass);
88     }
89
90     public CommandResult execute(CommandContext context) {
91         if (PageCommandUtils.isTranslated(context)) {
92             return super.execute(context);
93         }
94         Page page = PageCommandUtils.getPage(context);
95         Locale JavaDoc locale = PageCommandUtils.getParentLocale(context);
96         pageDao.addTranslation(page, locale);
97         return new ShowListResult(context);
98     }
99
100 }
101
Popular Tags