📄 qdatefieldviewimpl.java
字号:
/*
* Copyright 2006-2007 Queplix Corp.
*
* Licensed under the Queplix Public License, Version 1.1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.queplix.core.client.controls.datefield;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.queplix.core.client.app.vo.uisettings.DialogUISettings;
import com.queplix.core.client.common.OrderedHashMap;
import com.queplix.core.client.common.StringUtil;
import com.queplix.core.client.common.event.Event;
import com.queplix.core.client.common.event.EventListener;
import com.queplix.core.client.common.ui.ButtonData;
import com.queplix.core.client.common.ui.DialogHelper;
import com.queplix.core.client.common.ui.OkayCancelPopup;
import java.util.ArrayList;
import java.util.Date;
/**
* Class represents internal view realisatio~n for the date field widget.
*
* @author Sergey Kozmin, Aliaksandr Melnik
* @since 14 Sep 2006
*/
class QDateFieldViewImpl extends QDateFieldView implements ChangeListener, ClickListener, MouseRightClickListener {
private static final String BUTTON_IMAGE_PATH = "datefield/button.gif";
private static final String BUTTON_CSS_STYLE = "simple_button";
public final static String DATEFIELD_STYLE_NAME_PREFIX = "dateField_";
private static final String CALENDAR = " : Calendar";
private static final String INCORRECT_DATE_FORMAT_ERROR_MESSAGE = "Incorrect date format";
private QDateFieldModelImpl model;
private ButtonWithRightClick pickButton;
private TextBox dateTimeTextBox;
private boolean userUpdate;
private OkayCancelPopup datePickerDialog;
private DateTimeSelector calendar;
private boolean showDatePickerDialog;
private OkayCancelPopup hotDatesDialog;
private VerticalPanel hotDatesPanel;
private OrderedHashMap hotDates;
private static final String SEPARATOR_ITEM_TEXT = "__________________________";
private static final int NOT_ACTIVE_ELEMENT = -1,
CURRENT_MONTH = 0,
CURRENT_YEAR = 1,
MONTH_TO_DATE = 2,
YEAR_TO_DATE = 3,
PREVIOUS_MONTH = 4,
PREVIOUS_YEAR = 5,
LAST_30_DAYS = 6,
LAST_3_MONTH = 7,
LAST_6_MONTH = 8,
LAST_12_MONTH = 9,
NEXT_30_DAYS = 10,
NEXT_3_MONTH = 11,
NEXT_6_MONTH = 12,
NEXT_12_MONTH = 13,
CUSTOM_DATES = 14,
NULL_DATE = 15;
// =========== User actions notification for controller implementation
public static interface UserActionsListener {
public void onUserActions(String stringToParse);
public void onUserActions(Date startDate, Date endDate);
}
private UserActionsListener userActionsListener; // the only listener will be the controller implementation
void addUserActionsListener(UserActionsListener listener) {
userActionsListener = listener;
}
private void fireUserActions(String stringToParse) {
if (userActionsListener != null) {
userActionsListener.onUserActions(stringToParse);
}
}
private void fireUserActions(Date startDate, Date endDate) {
if (userActionsListener != null) {
userActionsListener.onUserActions(startDate, endDate);
}
}
// ========== End of user actions event
/**
* Constuct datefield view with given parameters.
* @param model model to render
* @param layout element layout
*/
QDateFieldViewImpl(QDateFieldModelImpl model, int layout) {
super(model, layout);
this.model = model;
initializeGUI(model.getMeta().getCaption(), StringUtil.imgSrc(BUTTON_IMAGE_PATH));
}
public void onModelMetaChanged() {
dataChanged();
}
public void onModelDataChanged() {
super.onModelDataChanged();
dataChanged();
}
private void initializeGUI(String fieldCaption, String buttonValue) {
setCaption(fieldCaption);
dateTimeTextBox = new TextBox();
dateTimeTextBox.setMaxLength(24);
dateTimeTextBox.addChangeListener(this);
dateTimeTextBox.addKeyboardListener(new KeyboardListener() {
public void onKeyDown(Widget sender, char keyCode, int modifiers) {
if (keyCode == KeyboardListener.KEY_ENTER) {
onChange(dateTimeTextBox);
}
if(model.getData().getStartDate() != null) {
switch (keyCode) {
case KeyboardListener.KEY_UP:
model.getData().setStartDate(new Date(model.getData().getStartDate().getTime() + DateHelper.DAY));
if (isSearchMode()) {
model.getData().setEndDate((Date) model.getData().getStartDate().clone());
}
fireUserActions(model.getData().getStartDate(), isSearchMode() ? model.getData().getEndDate() : null);
break;
case KeyboardListener.KEY_DOWN:
model.getData().setStartDate(new Date(model.getData().getStartDate().getTime() - DateHelper.DAY));
if (isSearchMode()) {
model.getData().setEndDate((Date) model.getData().getStartDate().clone());
}
fireUserActions(model.getData().getStartDate(), isSearchMode() ? model.getData().getEndDate() : null);
break;
}
}
}
public void onKeyPress(Widget sender, char keyCode, int modifiers) {
}
public void onKeyUp(Widget sender, char keyCode, int modifiers) {
}
});
dateTimeTextBox.addStyleName("styled_input");
Panel internalPanel = new HorizontalPanel();
internalPanel.add(dateTimeTextBox);
pickButton = new ButtonWithRightClick(buttonValue);
pickButton.setStyleName(BUTTON_CSS_STYLE);
pickButton.addClickListener(this);
pickButton.addRightClickListener(this);
internalPanel.add(pickButton);
addToPanel(internalPanel);
initPanel();
}
public void formated() {
dateTimeTextBox.setText(model.getData().getFormatedDate());
}
/**
* Implementation of the view string.
* Expression "model.getData().getFormatedDate() == null" - indicates if clear was performed.
* Used for clear DataField with out error message. (enter invalida date, click clear => data field cleared with out error msg)
*/
public void dataChanged() {
if (!userUpdate) {
if (model.getData().getFormatedDate() == null) {
model.getData().setFormatedDate("");
}
dateTimeTextBox.setText(model.getData().getFormatedDate());
}
}
protected void onModeChanged(int newMode) {
if (calendar != null) {
switch(newMode) {
case MODE_EDIT: {
calendar.setMode(MODE_EDIT);
}
case MODE_DISABLED: {
calendar.setMode(MODE_DISABLED);
}
}
}
}
protected void setEnabled(boolean isEnabled) {
String readOnly = isEnabled ? null : "readonly";
DOM.setAttribute(dateTimeTextBox.getElement(), "readOnly", readOnly);
if (!isEnabled && datePickerDialog != null && datePickerDialog.isVisible()) {
datePickerDialog.setVisible(false);
}
}
/**
* Updates model, when user updates it in dateTimeTextBox
* @param sender dateTimeTextBox component
*/
public void onChange(Widget sender) {
String stringToParse = dateTimeTextBox.getText().trim();
if (stringToParse.equalsIgnoreCase("null")) {
getModel().getBaseData().clear();
model.getData().setFormatedDate("null");
} else if (stringToParse.equals("")) {
dateTimeTextBox.setText("");
getModel().getBaseData().clear();
userUpdate = false;
} else {
fireUserActions(stringToParse);
userUpdate = true;
}
}
/**
* Expression "model.getData().getFormatedDate() == null" - indicates if clear was performed.
* Used for clear DataField without error message. (enter invalida date, click clear => data field cleared with out error msg)
*/
void parseFailed() {
if (model.getData().getFormatedDate() != null) {
DialogHelper.showModalMessageDialog(INCORRECT_DATE_FORMAT_ERROR_MESSAGE);
} else {
model.getData().setFormatedDate("");
}
dateTimeTextBox.setText(model.getData().getFormatedDate());
userUpdate = false;
}
void parseSucceed() {
model.getData().setFormatedDate(dateTimeTextBox.getText());
if (showDatePickerDialog) {
showDatePickerDialog();
}
userUpdate = false;
}
public void onRightClick(Widget sender) {
showDatePickerDialog = true;
if (!isReportDesignMode() && !isDisabled()) {
showHotDatesDialog();
}
}
private void showHotDatesDialog() {
initDropDown();
hotDatesDialog.show(pickButton);
hotDatesPanel.setWidth(hotDatesDialog.getDialogOffetWidth() - 10 + "px");
}
private void initDropDown() {
if (hotDatesDialog != null) {
return;
}
hotDatesDialog = new OkayCancelPopup(model.getMeta().getCaption() + " : Select Date...");
hotDatesPanel = new VerticalPanel();
ArrayList labels = getLabels();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -