using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Controls.Table.Editors;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using log4net;
namespace Core.Common.Controls.Table
{
public static class XtraGridRepositoryItemBuilder
{
private static readonly ILog Log = LogManager.GetLogger(typeof(TableViewColumn));
public static RepositoryItem CreateFromTypeEditor(Editors.ITypeEditor typeEditor, GridControl gridControl, GridColumn column, string caption)
{
if (typeEditor is MultiLineTextEdior)
{
// do not use wordwrap
column.AppearanceCell.Options.UseTextOptions = true;
column.AppearanceCell.TextOptions.WordWrap = WordWrap.NoWrap;
column.AppearanceCell.TextOptions.Trimming = Trimming.EllipsisWord;
return new RepositoryItemMemoEdit
{
WordWrap = false
};
}
var comboBoxTypeEditor = typeEditor as ComboBoxTypeEditor;
if (comboBoxTypeEditor != null)
{
return CreateComboBoxRepositoryItem(comboBoxTypeEditor, gridControl, caption);
}
var buttonEditor = typeEditor as ButtonTypeEditor;
if (buttonEditor != null)
{
var buttonTypeEditorRepositoryItem = new RepositoryItemButtonEdit
{
TextEditStyle = TextEditStyles.HideTextEditor
};
var editorButton = buttonTypeEditorRepositoryItem.Buttons[0];
editorButton.ToolTip = buttonEditor.Tooltip;
if (buttonEditor.Caption != null || buttonEditor.Image != null)
{
editorButton.Kind = ButtonPredefines.Glyph;
editorButton.Caption = buttonEditor.Caption;
editorButton.Image = buttonEditor.Image;
}
buttonTypeEditorRepositoryItem.ButtonClick += (s, e) => ButtonTypeEditorRepositoryItemButtonClick(e, typeEditor);
return buttonTypeEditorRepositoryItem;
}
// the mechanisms for validating do not work as expected; perhaps some of the encapsulating layers
// consumes events?
var repositoryItem = new RepositoryItemPopupContainerEdit
{
CloseOnOuterMouseClick = false
};
var popupControl = new PopupContainerControl
{
AutoSize = true
};
var editorControl = (Control) typeEditor;
popupControl.Controls.Add(editorControl);
repositoryItem.PopupControl = popupControl;
repositoryItem.QueryPopUp += RepositoryItemQueryPopUp;
repositoryItem.QueryResultValue += RepositoryItemQueryResultValue;
repositoryItem.Validating += RepositoryItemValidating;
repositoryItem.CloseUp += (sender, e) => RepositoryItemCloseUp(e, typeEditor);
//close and open with enter
repositoryItem.CloseUpKey = new KeyShortcut(Keys.Enter);
return repositoryItem;
}
///
/// Creates a ComboBox editor as . The requested width for rendering
/// is determined by the rendersize of the largest string in the selection set or the caption.
///
/// It has been assumed the final caption is already set before calling this function.
private static RepositoryItem CreateComboBoxRepositoryItem(ComboBoxTypeEditor comboBoxEditor, GridControl dxGridControl, string caption)
{
RepositoryItem comboBoxRepositoryItem;
// TODO: make it check if Items is evented, if yes - refresh
var items = comboBoxEditor.Items.Cast