001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.ui; 029 030import org.opencms.gwt.client.Messages; 031import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor; 032import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle; 033import org.opencms.gwt.client.ui.input.CmsRadioButton; 034import org.opencms.gwt.client.ui.input.CmsRadioButtonGroup; 035import org.opencms.gwt.shared.CmsListInfoBean; 036import org.opencms.util.CmsUUID; 037 038import java.util.HashMap; 039import java.util.List; 040import java.util.Map; 041 042import com.google.gwt.dom.client.Style.Unit; 043import com.google.gwt.event.dom.client.ClickEvent; 044import com.google.gwt.event.dom.client.ClickHandler; 045import com.google.gwt.user.client.ui.FlowPanel; 046import com.google.gwt.user.client.ui.Label; 047 048/** 049 * Dialog to select a resource model for a new resource.<p> 050 * 051 * @param <INFO> the type of list info bean which should be used as select options 052 * 053 * @since 8.0.3 054 */ 055public abstract class A_CmsListItemSelectDialog<INFO extends CmsListInfoBean> extends CmsPopup { 056 057 /** The text metrics key. */ 058 private static final String TEXT_METRICS_KEY = "CMS_LIST_ITEM_SELECT_DIALOG_METRICS"; 059 060 /** Static counter used for generating ids. */ 061 private static long idCounter; 062 063 /** The close button. */ 064 private CmsPushButton m_cancelButton; 065 066 /** The scroll panel. */ 067 private CmsList<CmsListItem> m_listPanel; 068 069 /** The label to display the dialog message. */ 070 private Label m_messageLabel; 071 072 /** The unlock button. */ 073 private CmsPushButton m_okButton; 074 075 /** The radio button group. */ 076 private CmsRadioButtonGroup m_radioGroup; 077 078 /** Internal map used to keep track of the list info beans. */ 079 private Map<String, INFO> m_infosByName = new HashMap<String, INFO>(); 080 081 /** 082 * Constructor.<p> 083 * 084 * @param itemInfos the list info beans for the possible select options 085 * @param title the title for the model selection dialog 086 * @param message the message to display in the model selection dialog 087 */ 088 public A_CmsListItemSelectDialog(List<INFO> itemInfos, String title, String message) { 089 090 super(title); 091 m_cancelButton = new CmsPushButton(); 092 m_cancelButton.setText(Messages.get().key(Messages.GUI_CANCEL_0)); 093 m_cancelButton.setUseMinWidth(true); 094 m_cancelButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE); 095 m_cancelButton.addClickHandler(new ClickHandler() { 096 097 /** 098 * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent) 099 */ 100 public void onClick(ClickEvent event) { 101 102 hide(); 103 } 104 }); 105 addButton(m_cancelButton); 106 addDialogClose(null); 107 m_okButton = new CmsPushButton(); 108 m_okButton.setText(Messages.get().key(Messages.GUI_OK_0)); 109 m_okButton.setUseMinWidth(true); 110 m_okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED); 111 m_okButton.addClickHandler(new ClickHandler() { 112 113 /** 114 * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent) 115 */ 116 public void onClick(ClickEvent event) { 117 118 onClickOk(); 119 } 120 }); 121 addButton(m_okButton); 122 setGlassEnabled(true); 123 FlowPanel content = new FlowPanel(); 124 m_messageLabel = new Label(message); 125 126 content.add(m_messageLabel); 127 m_radioGroup = new CmsRadioButtonGroup(); 128 m_listPanel = new CmsList<CmsListItem>(); 129 m_listPanel.addStyleName( 130 org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.dialogCss().modelSelectList()); 131 m_listPanel.addStyleName(org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.generalCss().cornerAll()); 132 content.add(m_listPanel); 133 boolean first = true; 134 for (INFO info : itemInfos) { 135 String name = generateName(); 136 m_infosByName.put(name, info); 137 final CmsRadioButton radioButton = new CmsRadioButton(name, ""); 138 radioButton.setGroup(m_radioGroup); 139 ClickHandler clickHandler = new ClickHandler() { 140 141 public void onClick(ClickEvent event) { 142 143 radioButton.setChecked(true); 144 145 } 146 }; 147 if (first) { 148 radioButton.setChecked(true); 149 first = false; 150 } 151 CmsListItemWidget itemWidget = new CmsListItemWidget(info); 152 itemWidget.addClickHandler(clickHandler); 153 CmsListItem listItem = new CmsListItem(itemWidget); 154 listItem.addDecorationWidget(radioButton, 18); 155 //listItem.setId(String.valueOf(modelInfo.getStructureId())); 156 m_listPanel.add(listItem); 157 } 158 setMainContent(content); 159 } 160 161 /** 162 * @see org.opencms.gwt.client.ui.CmsPopup#center() 163 */ 164 @Override 165 public void center() { 166 167 super.center(); 168 adjustListHeight(); 169 // ensure centerd 170 super.center(); 171 } 172 173 /** 174 * @see org.opencms.gwt.client.ui.CmsPopup#show() 175 */ 176 @Override 177 public void show() { 178 179 super.show(); 180 adjustListHeight(); 181 } 182 183 /** 184 * Makes sure the item with the given structure id is checked.<p> 185 * 186 * @param structureId the structure id 187 */ 188 protected void ensureChecked(CmsUUID structureId) { 189 190 CmsRadioButton button = (CmsRadioButton)m_listPanel.getItem( 191 String.valueOf(structureId)).getDecorationWidgets().get(0); 192 if (!button.isChecked()) { 193 button.setChecked(true); 194 } 195 } 196 197 /** 198 * The method that will be called with the selected item when the user clicks on OK.<p> 199 * 200 * @param info the selected item 201 */ 202 protected abstract void handleSelection(INFO info); 203 204 /** 205 * Creates the new element with selected model resource.<p> 206 */ 207 protected void onClickOk() { 208 209 String radioButtonName = m_radioGroup.getSelectedButton().getName(); 210 INFO selectedItemInfo = m_infosByName.get(radioButtonName); 211 handleSelection(selectedItemInfo); 212 hide(); 213 } 214 215 /** 216 * Adjusts the max height setting of the model list.<p> 217 */ 218 private void adjustListHeight() { 219 220 int maxHeight = getAvailableHeight(m_messageLabel.getOffsetHeight() + 5); 221 m_listPanel.getElement().getStyle().setProperty("maxHeight", maxHeight, Unit.PX); 222 m_listPanel.truncate(TEXT_METRICS_KEY, CmsPopup.DEFAULT_WIDTH - 20); 223 } 224 225 /** 226 * Generates internal names for the radio button widgets.<p> 227 * 228 * @return the generated name 229 */ 230 private String generateName() { 231 232 return "dialogselectitem_" + (++idCounter); 233 } 234 235}