001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.admin.ui.actions;
023:
024: import org.jboss.portal.common.i18n.LocaleFormat;
025: import org.jboss.portal.common.i18n.LocaleManager;
026: import org.jboss.portal.common.i18n.LocalizedString;
027: import org.jboss.portal.common.util.ConversionException;
028: import org.jboss.portal.core.admin.ui.PortalObjectManagerBean;
029: import org.jboss.portal.core.admin.ui.DisplayNameBean;
030: import org.jboss.portal.core.impl.model.portal.PortalObjectImpl;
031: import org.jboss.portal.core.model.portal.PortalObject;
032: import org.jboss.portal.core.model.instance.Instance;
033:
034: import javax.faces.application.FacesMessage;
035: import javax.faces.context.FacesContext;
036: import java.util.*;
037:
038: /**
039: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
040: * @author <a href="mailto:whales@redhat.com">Wesley Hales</a>
041: * @version $Revision$
042: */
043: public class PortalObjectDisplayNameAction {
044:
045: private PortalObject selectedObject;
046:
047: public Object bean;
048:
049: private String newLocale;
050:
051: private String newText;
052:
053: private SortedMap locales;
054:
055: private String displayNameByLocale;
056:
057: private DisplayNameBean displayNameBean;
058:
059: public void addDisplayName() throws ConversionException {
060: if (newText != null) {
061: PortalObject portalObject = getSelectedObject();
062: Map map = getDisplayNameBean().createLocalizedStringMap(
063: LocaleFormat.DEFAULT.getLocale(newLocale),
064: portalObject.getDisplayName(), newText);
065: LocalizedString newLocalizedString = new LocalizedString(
066: map, Locale.ENGLISH);
067: portalObject.setDisplayName(newLocalizedString);
068: } else {
069: FacesMessage message = new FacesMessage(
070: FacesMessage.SEVERITY_ERROR,
071: "You must enter a name", "You must enter a name");
072: FacesContext.getCurrentInstance().addMessage("status",
073: message);
074: }
075: }
076:
077: public PortalObject getSelectedObject() {
078: if (bean instanceof PortalObjectManagerBean) {
079: selectedObject = ((PortalObjectManagerBean) bean)
080: .getSelectedObject();
081: }
082: return selectedObject;
083: }
084:
085: public void setSelectedObject(PortalObject selectedObject) {
086: this .selectedObject = selectedObject;
087: }
088:
089: public String getNewText() {
090: return newText;
091: }
092:
093: public void setNewText(String newText) {
094: this .newText = newText;
095: }
096:
097: public String getNewLocale() {
098: return newLocale;
099: }
100:
101: public void setNewLocale(String newLocale) {
102: this .newLocale = newLocale;
103: }
104:
105: public SortedMap getLocales() {
106: return getDisplayNameBean().getLocales();
107: }
108:
109: public void setLocales(SortedMap locales) {
110: this .locales = locales;
111: }
112:
113: public List<DisplayNameBean.Value> getCurrentLocales() {
114: selectedObject = getSelectedObject();
115: if (selectedObject != null
116: && selectedObject.getDisplayName() != null) {
117: List<DisplayNameBean.Value> currentLocales = getDisplayNameBean()
118: .populateCurrentLocales(
119: selectedObject.getDisplayName().getValues());
120: return currentLocales;
121: }
122: return null;
123: }
124:
125: public void editDisplayName() {
126: String locale = getFacesParam("locale");
127: String editAction = getFacesParam("editAction");
128:
129: PortalObject portalObject = getSelectedObject();
130: LocalizedString displayName = portalObject.getDisplayName();
131: Map<Locale, String> map = new HashMap<Locale, String>();
132: if (displayName != null) {
133: Map<Locale, LocalizedString.Value> oldMap = displayName
134: .getValues();
135: Collection<LocalizedString.Value> values = oldMap.values();
136: if (editAction.equals("delete")) {
137: for (LocalizedString.Value value : values) {
138: if (!value.getLocale().toString().equals(
139: locale.toString())) {
140: map.put(value.getLocale(), value.getString());
141: }
142: }
143: }
144: if (editAction.equals("rename")) {
145: for (LocalizedString.Value value : values) {
146: map.put(value.getLocale(), value.getString());
147: if (value.getLocale().toString().equals(
148: locale.toString())) {
149: newText = value.getString();
150: newLocale = value.getLocale().getLanguage();
151: }
152: }
153: }
154: }
155: LocalizedString newLocalizedString = new LocalizedString(map,
156: Locale.ENGLISH);
157: selectedObject.setDisplayName(newLocalizedString);
158: }
159:
160: public String getFacesParam(String param) {
161: return (String) FacesContext.getCurrentInstance()
162: .getExternalContext().getRequestParameterMap().get(
163: param);
164: }
165:
166: public String editDisplayNames() throws ConversionException {
167: getSelectedObject();
168: getCurrentLocales();
169: return "editDisplayNames";
170: }
171:
172: public DisplayNameBean getDisplayNameBean() {
173: if (displayNameBean == null) {
174: displayNameBean = new DisplayNameBean();
175: }
176: return displayNameBean;
177: }
178:
179: public void setDisplayNameBean(DisplayNameBean displayNameBean) {
180: this.displayNameBean = displayNameBean;
181: }
182:
183: }
|