/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.alkacon.opencms.v8.calendar.client.input.serialdate;
import com.alkacon.opencms.v8.calendar.client.widget.css.I_CmsLayoutBundle;
import org.opencms.gwt.client.ui.input.CmsCheckBox;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
/**
*
* */
public class CmsPatternPanelWeekly extends FlowPanel implements HasValueChangeHandlers<String> {
/** The panel for all values of 'every'. */
private FlowPanel m_everyPanel = new FlowPanel();
/** The panel for all values of the day selection. */
private FlowPanel m_dayPanel = new FlowPanel();
/** The text box for the date input. */
private TextBox m_everyDay = new TextBox();
/** The array of all checkboxes. */
List<CmsCheckBox> m_checkboxes = new ArrayList<CmsCheckBox>();
/** The handler. */
private ValueChangeHandler<String> m_handler;
/** JSON of all needed labels. */
private JSONObject m_labels;
/**
* Default constructor to create the panel.<p>
* @param labels JSON of all needed labels
*/
public CmsPatternPanelWeekly(JSONObject labels) {
m_labels = labels;
addStyleName(I_CmsLayoutBundle.INSTANCE.widgetCss().serialDateWeek());
createEverPanel();
this.add(m_everyPanel);
createDayPanel();
for (CmsCheckBox box : m_checkboxes) {
box.addStyleName(I_CmsLayoutBundle.INSTANCE.widgetCss().serialDateCheckBox());
m_dayPanel.add(box);
}
this.add(m_dayPanel);
}
/**
* @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
*/
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
m_handler = handler;
m_everyDay.addValueChangeHandler(m_handler);
for (CmsCheckBox box : m_checkboxes) {
box.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
public void onValueChange(ValueChangeEvent<Boolean> event) {
fireValueChange();
}
});
}
return addHandler(handler, ValueChangeEvent.getType());
}
/**
* Represents a value change event.<p>
*/
public void fireValueChange() {
ValueChangeEvent.fire(this, getWeekDays());
}
/**
* Returns the interval.<p>
* @return the interval
* */
public String getInterval() {
return m_everyDay.getText();
}
/**
* Returns all selected days.<p>
* @return all selected days
* */
public String getWeekDays() {
String result = "";
int i = 0;
for (CmsCheckBox box : m_checkboxes) {
if (box.isChecked()) {
if (i > 0) {
result += ",";
}
result += box.getInternalValue();
i++;
}
}
return result;
}
/**
* @see com.google.gwt.user.client.ui.HasWidgets#iterator()
*/
@Override
public Iterator<Widget> iterator() {
Iterator<Widget> result = getChildren().iterator();
return result;
}
/**
* @see com.google.gwt.user.client.ui.Panel#remove(com.google.gwt.user.client.ui.Widget)
*/
@Override
public boolean remove(Widget child) {
return remove(child);
}
/**
* Sets the interval.<p>
* @param intervalStr the interval
* */
public void setInterval(String intervalStr) {
m_everyDay.setText(intervalStr);
}
/**
* Selects all days.<p>
* @param weekDaysStrList List of selected days
* */
public void setWeekDays(List<String> weekDaysStrList) {
List<CmsCheckBox> checked = new ArrayList<CmsCheckBox>();
for (String day : weekDaysStrList) {
for (CmsCheckBox box : m_checkboxes) {
if (box.getInternalValue().equals(day)) {
checked.add(box);
}
}
}
for (CmsCheckBox box : m_checkboxes) {
if (checked.contains(box)) {
box.setChecked(true);
} else {
box.setChecked(false);
}
}
}
/**
* Creates the day selection view.<p>
* */
private void createDayPanel() {
CmsCheckBox test = new CmsCheckBox(m_labels.get("GUI_SERIALDATE_DAY_MONDAY_0").isString().stringValue());
test.setInternalValue("2");
m_checkboxes.add(test);
test = new CmsCheckBox(m_labels.get("GUI_SERIALDATE_DAY_TUESDAY_0").isString().stringValue());
test.setInternalValue("3");
m_checkboxes.add(test);
test = new CmsCheckBox(m_labels.get("GUI_SERIALDATE_DAY_WEDNESDAY_0").isString().stringValue());
test.setInternalValue("4");
m_checkboxes.add(test);
test = new CmsCheckBox(m_labels.get("GUI_SERIALDATE_DAY_THURSDAY_0").isString().stringValue());
test.setInternalValue("5");
m_checkboxes.add(test);
test = new CmsCheckBox(m_labels.get("GUI_SERIALDATE_DAY_FRIDAY_0").isString().stringValue());
test.setInternalValue("6");
m_checkboxes.add(test);
test = new CmsCheckBox(m_labels.get("GUI_SERIALDATE_DAY_SATURDAY_0").isString().stringValue());
test.setInternalValue("7");
m_checkboxes.add(test);
test = new CmsCheckBox(m_labels.get("GUI_SERIALDATE_DAY_SUNDAY_0").isString().stringValue());
test.setInternalValue("1");
m_checkboxes.add(test);
}
/**
* Creates the 'every' selection view.<p>
*
* */
private void createEverPanel() {
m_everyPanel.add(new Label(m_labels.get("GUI_SERIALDATE_WEEKLY_EVERY_0").isString().stringValue()));
m_everyPanel.add(m_everyDay);
m_everyDay.setStyleName(I_CmsLayoutBundle.INSTANCE.widgetCss().textBoxSerialDate());
m_everyDay.setText("1");
m_everyDay.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
fireValueChange();
}
});
m_everyPanel.add(new Label(m_labels.get("GUI_SERIALDATE_WEEKLY_WEEK_AT_0").isString().stringValue()));
}
}