001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (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.input; 029 030import org.opencms.gwt.client.ui.I_CmsTruncable; 031import org.opencms.gwt.client.ui.css.I_CmsConstantsBundle; 032import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle; 033 034import com.google.gwt.canvas.client.Canvas; 035import com.google.gwt.canvas.dom.client.Context2d; 036 037/** 038 * This class represents a single select option in the selector of the select box. 039 */ 040public class CmsLabelSelectCell extends A_CmsSelectCell implements I_CmsTruncable { 041 042 /** Canvas used for measuring the option size. */ 043 private static final Canvas canvas = Canvas.createIfSupported(); 044 045 /** The value of the select option. */ 046 protected String m_value; 047 048 /** The label of which this select cell consists. */ 049 private CmsLabel m_label = new CmsLabel(); 050 051 /** The label width last used in a truncate()-call. */ 052 private int m_labelWidth; 053 054 /** The opener text. */ 055 private String m_openerText; 056 057 /** The text of the select option. */ 058 private String m_text; 059 060 /** The text metrics key last used in a truncate()-call. */ 061 private String m_textMetricsKey; 062 063 /** 064 * Creates a new select cell.<p> 065 * 066 * @param value the value of the select option 067 * @param text the text to display for the select option 068 */ 069 public CmsLabelSelectCell(String value, String text) { 070 071 this(value, text, null); 072 } 073 074 /** 075 * Creates a new select cell.<p> 076 * 077 * @param value the value of the select option 078 * @param text the text to display for the select option 079 * @param title the title to display on mouseover 080 */ 081 public CmsLabelSelectCell(String value, String text, String title) { 082 083 super(); 084 m_value = value; 085 m_text = text; 086 m_openerText = text; 087 initWidget(m_label); 088 addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().selectBoxCell()); 089 m_label.setText(m_text); 090 m_label.setTitle(title != null ? title : m_text); 091 } 092 093 /** 094 * Gets the opener text.<p> 095 * 096 * @return the opener text 097 */ 098 public String getOpenerText() { 099 100 return m_openerText; 101 } 102 103 /** 104 * Measures the required width for this cell.<p> 105 * 106 * @return the required width 107 */ 108 @Override 109 public int getRequiredWidth() { 110 111 Context2d context = canvas.getContext2d(); 112 String fontStr = I_CmsConstantsBundle.INSTANCE.css().fontSize() 113 + " " 114 + I_CmsConstantsBundle.INSTANCE.css().fontFamily(); 115 context.setFont(fontStr); 116 double width = context.measureText(getText()).getWidth(); 117 return (int)(Math.ceil(width)); 118 } 119 120 /** 121 * Returns the text as which the select option should be displayed to the user.<p> 122 * 123 * @return the text of the select option 124 */ 125 public String getText() { 126 127 return m_text; 128 } 129 130 /** 131 * @see org.opencms.gwt.client.ui.input.A_CmsSelectCell#getValue() 132 */ 133 @Override 134 public String getValue() { 135 136 return m_value; 137 } 138 139 /** 140 * Sets the opener text.<p> 141 * 142 * @param openerText the new opener text 143 */ 144 public void setOpenerText(String openerText) { 145 146 m_openerText = openerText; 147 } 148 149 /** 150 * Sets the text of the label.<p> 151 * 152 * @param text the new text 153 */ 154 public void setText(String text) { 155 156 m_label.setText(text); 157 m_label.setTitle(text); 158 m_text = text; 159 if (m_textMetricsKey != null) { 160 truncate(m_textMetricsKey, m_labelWidth); 161 } 162 } 163 164 /** 165 * @see org.opencms.gwt.client.ui.I_CmsTruncable#truncate(java.lang.String, int) 166 */ 167 public void truncate(String textMetricsKey, int labelWidth) { 168 169 m_textMetricsKey = textMetricsKey; 170 m_labelWidth = labelWidth; 171 m_label.truncate(textMetricsKey, labelWidth); 172 } 173 174}