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.PortletManagerBean;
030: import org.jboss.portal.core.admin.ui.InstanceManagerBean;
031: import org.jboss.portal.core.admin.ui.DisplayNameBean;
032: import org.jboss.portal.core.model.instance.Instance;
033: import org.jboss.portal.core.model.portal.PortalObject;
034:
035: import javax.faces.application.FacesMessage;
036: import javax.faces.context.FacesContext;
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.Locale;
043: import java.util.Map;
044: import java.util.SortedMap;
045: import java.util.TreeMap;
046:
047: /**
048: * @author <a href="mailto:whales@redhat.com">Wesley Hales</a>
049: * @version $Revision: 630 $
050: */
051: public class PortalInstanceDisplayNameAction {
052: private Instance selectedInstance;
053:
054: public Object bean;
055:
056: private String newLocale;
057:
058: private String newText;
059:
060: private SortedMap locales;
061:
062: private String displayNameByLocale;
063:
064: private InstanceManagerBean instanceManager;
065:
066: private DisplayNameBean displayNameBean;
067:
068: public void addDisplayName() throws ConversionException {
069: if (newText != null) {
070: Instance instance = getSelectedInstance();
071: Map map = getDisplayNameBean().createLocalizedStringMap(
072: LocaleFormat.DEFAULT.getLocale(newLocale),
073: instance.getDisplayName(), newText);
074: LocalizedString newLocalizedString = new LocalizedString(
075: map, Locale.ENGLISH);
076: selectedInstance.setDisplayName(newLocalizedString);
077: } else {
078: FacesMessage message = new FacesMessage(
079: FacesMessage.SEVERITY_ERROR,
080: "You must enter a name", "You must enter a name");
081: FacesContext.getCurrentInstance().addMessage("status",
082: message);
083: }
084: }
085:
086: public Instance getSelectedInstance() {
087: if (instanceManager instanceof InstanceManagerBean) {
088: selectedInstance = instanceManager.getSelectedInstance();
089: }
090: return selectedInstance;
091: }
092:
093: public void setSelectedInstance(Instance selectedInstance) {
094: this .selectedInstance = selectedInstance;
095: }
096:
097: public String getNewText() {
098: return newText;
099: }
100:
101: public void setNewText(String newText) {
102: this .newText = newText;
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 String getNewLocale() {
114: return newLocale;
115: }
116:
117: public void setNewLocale(String newLocale) {
118: this .newLocale = newLocale;
119: }
120:
121: public List<DisplayNameBean.Value> getCurrentLocales() {
122: selectedInstance = getSelectedInstance();
123: if (selectedInstance != null
124: && selectedInstance.getDisplayName() != null) {
125: List<DisplayNameBean.Value> currentLocales = getDisplayNameBean()
126: .populateCurrentLocales(
127: selectedInstance.getDisplayName()
128: .getValues());
129:
130: return currentLocales;
131: }
132: return null;
133: }
134:
135: public void editDisplayName() {
136: String locale = getFacesParam("locale");
137: String editAction = getFacesParam("editAction");
138:
139: Instance selectedInstance = getSelectedInstance();
140: LocalizedString displayName = selectedInstance.getDisplayName();
141: Map<Locale, String> map = new HashMap<Locale, String>();
142: if (displayName != null) {
143: Map<Locale, LocalizedString.Value> oldMap = displayName
144: .getValues();
145: Collection<LocalizedString.Value> values = oldMap.values();
146: if (editAction.equals("delete")) {
147: for (LocalizedString.Value value : values) {
148: if (!value.getLocale().toString().equals(
149: locale.toString())) {
150: map.put(value.getLocale(), value.getString());
151: }
152: }
153: }
154: if (editAction.equals("rename")) {
155: for (LocalizedString.Value value : values) {
156: map.put(value.getLocale(), value.getString());
157: if (value.getLocale().toString().equals(
158: locale.toString())) {
159: newText = value.getString();
160: newLocale = value.getLocale().getLanguage();
161: }
162: }
163: }
164: }
165: LocalizedString newLocalizedString = new LocalizedString(map,
166: Locale.ENGLISH);
167: selectedInstance.setDisplayName(newLocalizedString);
168: }
169:
170: public String getFacesParam(String param) {
171: return (String) FacesContext.getCurrentInstance()
172: .getExternalContext().getRequestParameterMap().get(
173: param);
174: }
175:
176: public String editDisplayNames() throws ConversionException {
177: getSelectedInstance();
178: getCurrentLocales();
179: return "editDisplayNames";
180: }
181:
182: public InstanceManagerBean getInstanceManager() {
183: return instanceManager;
184: }
185:
186: public void setInstanceManager(InstanceManagerBean instanceManager) {
187: this .instanceManager = instanceManager;
188: }
189:
190: public DisplayNameBean getDisplayNameBean() {
191: if (displayNameBean == null) {
192: displayNameBean = new DisplayNameBean();
193: }
194: return displayNameBean;
195: }
196:
197: public void setDisplayNameBean(DisplayNameBean displayNameBean) {
198: this.displayNameBean = displayNameBean;
199: }
200: }
|