001/* 002 * Copyright 2012 GWT-Bootstrap 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.github.gwtbootstrap.client.ui.base; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.List; 021 022import com.github.gwtbootstrap.client.ui.config.ColumnSizeConfigurator; 023import com.github.gwtbootstrap.client.ui.constants.Constants; 024import com.google.gwt.core.client.GWT; 025import com.google.gwt.user.client.ui.UIObject; 026 027/** 028 * THe helper for {@link HasSize} interface. 029 * 030 * @since 2.0.4.0 031 * @author ohashi keisuke 032 * 033 */ 034public class SizeHelper { 035 036 private static List<SizeSpan> SIZE_LIST; 037 038 private static final ColumnSizeConfigurator CONFIGURATOR = GWT.create(ColumnSizeConfigurator.class); 039 040 // create SIZE_LIST 041 static { 042 043 List<SizeSpan> list = new ArrayList<SizeSpan>(); 044 045 for (int i = CONFIGURATOR.getMinimumSpanSize(); i <= CONFIGURATOR.getMaximumSpanSize(); i++) { 046 list.add(new SizeSpan(i)); 047 } 048 049 SIZE_LIST = Collections.unmodifiableList(list); 050 } 051 052 /** 053 * change size style 054 * 055 * @param widget 056 * target widget 057 * @param size 058 * size 059 */ 060 public static void setSize(UIObject widget, int size) { 061 StyleHelper.changeStyle(widget, 062 new SizeSpan(size), 063 SIZE_LIST.toArray(new SizeSpan[SIZE_LIST.size()]) 064 ); 065 } 066 067 private static class SizeSpan implements Style { 068 069 private static final ColumnSizeConfigurator CONFIGURATOR = GWT.create(ColumnSizeConfigurator.class); 070 private static final String SIZE_ERROR_MESSAGE = "The size of the Column has to be between " 071 + CONFIGURATOR.getMinimumSpanSize() + " and " + CONFIGURATOR.getMaximumSpanSize()+ "!"; 072 073 private final int size; 074 075 private SizeSpan(int size) { 076 077 if (size < CONFIGURATOR.getMinimumSpanSize()) 078 throw new IllegalArgumentException(SIZE_ERROR_MESSAGE); 079 080 if (size > CONFIGURATOR.getMaximumSpanSize()) 081 throw new IllegalArgumentException(SIZE_ERROR_MESSAGE); 082 083 this.size = size; 084 } 085 086 @Override 087 public String get() { 088 return Constants.SPAN + size; 089 } 090 091 } 092 093}