unit ScheduleItemEditor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, Planner;

type
  TEditState = (esEdit, esNew);

type
  TScheduleItemEditorForm = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    btnCancle: TButton;
    btnOk: TButton;
    Label1: TLabel;
    edtFileName: TEdit;
    Button3: TButton;
    Label2: TLabel;
    Label3: TLabel;
    dtpStart: TDateTimePicker;
    dtpEnd: TDateTimePicker;
    Label4: TLabel;
    coboxPositions: TComboBox;
    OpenDlgPlay: TOpenDialog;
    procedure btnOkClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure dtpEndChange(Sender: TObject);
    procedure dtpStartChange(Sender: TObject);
    procedure coboxPositionsChange(Sender: TObject);
  private
    FEditState: TEditState;
    FPlannerItem: TPlannerItem;
  protected
    procedure AssignFromPlannerItem(PlannerItem: TPlannerItem); virtual;
    procedure AssignToPlannerItem(PlannerItem: TPlannerItem); virtual;
    procedure InternalEditModal(PlannerItem: TPlannerItem); virtual;
//    procedure ProcessWarnings; virtual;
    procedure SetEditState(const Value: TEditState); virtual;
  public
    procedure EditModal(PlannerItem: TPlannerItem); virtual;
    property EditState: TEditState read FEditState write SetEditState;
  end;

  TGCScheItemItemEditor = class(TCustomItemEditor)
  private
    FEditForm: TScheduleItemEditorForm;
    FCenter: Boolean;
    FFormLeft: Integer;
    FFormTop: Integer;
    FEditState: TEditState;
  public
    constructor Create(AOwner: TComponent); override;
    procedure CreateEditor(AOwner: TComponent); override;
    procedure DestroyEditor; override;
    function Execute: Integer; override;
    procedure PlannerItemToEdit(APlannerItem: TPlannerItem); override;
    procedure EditToPlannerItem(APlannerItem: TPlannerItem); override;
  published
    property EditState: TEditState read FEditState write FEditState;
    property CenterOnScreen: Boolean read FCenter write FCenter default True;
    property FormLeft: Integer read FFormLeft write FFormLeft;
    property FormTop: Integer read FFormTop write FFormTop;
  end;

implementation

{$R *.dfm}

{ TGCScheItemItemEditor }

constructor TGCScheItemItemEditor.Create(AOwner: TComponent);
begin
  inherited;
  FCenter := True;
end;

procedure TGCScheItemItemEditor.CreateEditor(AOwner: TComponent);
begin
  inherited;
  FEditForm := TScheduleItemEditorForm.Create(AOwner);
  FEditForm.Top := FFormTop;
  FEditForm.Left := FFormLeft;
  FEditForm.EditState := FEditState;

  if CenterOnScreen then
    FEditForm.Position := poScreenCenter;
  if FEditState = esNew then
    Caption := '新播放计划'
  else
    Caption := '修改播放计划';
  FEditForm.Caption := Caption;
end;

procedure TGCScheItemItemEditor.DestroyEditor;
begin
  inherited;
  FEditForm.Free;
end;

procedure TGCScheItemItemEditor.EditToPlannerItem(
  APlannerItem: TPlannerItem);
begin
  inherited;
  FEditForm.AssignToPlannerItem(APlannerItem);
end;

function TGCScheItemItemEditor.Execute: Integer;
begin
  Result := FEditForm.ShowModal;
  ModalResult := Result;
end;

procedure TGCScheItemItemEditor.PlannerItemToEdit(
  APlannerItem: TPlannerItem);
begin
  inherited;
  FEditForm.AssignFromPlannerItem(APlannerItem);
end;

{ TScheduleItemEditorForm }

procedure TScheduleItemEditorForm.AssignFromPlannerItem(
  PlannerItem: TPlannerItem);
var
  i: Integer;
begin
  coboxPositions.Items.Clear;
  coboxPositions.Items.BeginUpdate;
  for i:= 1 to PlannerItem.Planner.Positions do
    coboxPositions.Items.Add(inttostr(i));
  coboxPositions.Items.EndUpdate;
  coboxPositions.ItemIndex := PlannerItem.ItemPos;
  edtFileName.Text := PlannerItem.Attachement;

  dtpStart.Time := PlannerItem.ItemStartTime;
  dtpEnd.Time := PlannerItem.ItemEndTime;
  FPlannerItem := PlannerItem;
end;

procedure TScheduleItemEditorForm.AssignToPlannerItem(
  PlannerItem: TPlannerItem);
begin
  PlannerItem.ItemStartTime := dtpStart.Time;
  PlannerItem.ItemEndTime := dtpEnd.Time;
  if PlannerItem.ItemEndTime = 0 then
    PlannerItem.ItemEnd := PlannerItem.Planner.Display.ActiveEnd;
  PlannerItem.Attachement := edtFileName.Text;
  PlannerItem.Text.Text := edtFileName.Text;
  PlannerItem.ItemPos := coboxPositions.ItemIndex;
  PlannerItem.Update;
  FPlannerItem := nil;
end;

procedure TScheduleItemEditorForm.EditModal(PlannerItem: TPlannerItem);
begin
  Assert(Assigned(PlannerItem), 'Must pass a non-nil planner item for editing');
  EditState := esEdit;
  InternalEditModal(PlannerItem);
end;

procedure TScheduleItemEditorForm.InternalEditModal(
  PlannerItem: TPlannerItem);
begin
  AssignFromPlannerItem(PlannerItem);
  if (ShowModal = mrOK) then
    AssignToPlannerItem(PlannerItem);
end;

procedure TScheduleItemEditorForm.SetEditState(const Value: TEditState);
begin
  if Value <> EditState then
  begin
    FEditState := Value;
    if Value = esNew then
    begin
      Caption := '新播放计划';
    end
    else
    begin
      Caption := '修改播放计划';
    end;
  end;
end;

procedure TScheduleItemEditorForm.btnOkClick(Sender: TObject);
var
  i: integer;
  oldstart: Integer;
  OldEnd: Integer;
begin
  //check
  if FPlannerItem<>nil then
  for i := FPlannerItem.Planner.Items.Count-1 downto 0 do
  begin
    if FPlannerItem = FPlannerItem.Planner.Items[i] then
      continue;
    with FPlannerItem.Planner.Items[i] do
    begin
      if coboxPositions.ItemIndex<>ItemPos then
        continue;

      oldstart := FPlannerItem.ItemBegin;
      OldEnd := FPlannerItem.ItemEnd;
      FPlannerItem.ItemStartTime := dtpStart.Time;
      FPlannerItem.ItemEndTime := dtpEnd.Time;

      if FPlannerItem.ItemEndTimeStr='0:00:00' then
        FPlannerItem.ItemEnd := FPlannerItem.Planner.Display.ActiveEnd;
      if (FPlannerItem.ItemStartTime >= ItemEndTime) or
          ((FPlannerItem.ItemEndTimeStr<>'0:00:00') and (FPlannerItem.ItemEndTime <= ItemStartTime)) then
        Continue;
    end;
    FPlannerItem.ItemBegin := Oldstart;
    FPlannerItem.ItemEnd := OldEnd;
    Showmessage('　　所选的时间段已被其他播'+#13#10+
                '放计划占用，请重新选择。');
    exit;
  end;
  ModalResult := mrOK;
end;

procedure TScheduleItemEditorForm.Button3Click(Sender: TObject);
begin
  if edtFileName.Text<>'' then
    OpenDlgPlay.FileName := edtFileName.Text;
  if OpenDlgPlay.Execute then
  begin
    edtFileName.Text := OpenDlgPlay.FileName;
  end;
end;

procedure TScheduleItemEditorForm.dtpEndChange(Sender: TObject);
begin
  if TimeToStr(dtpEnd.Time)<>'0:00:00' then
    if dtpEnd.Time < dtpStart.Time then
      dtpEnd.Time := dtpStart.Time;
end;

procedure TScheduleItemEditorForm.dtpStartChange(Sender: TObject);
begin
  if TimeToStr(dtpEnd.Time)<>'0:00:00' then
    if dtpStart.Time > dtpEnd.Time then
      dtpStart.Time := dtpEnd.Time;
end;

procedure TScheduleItemEditorForm.coboxPositionsChange(Sender: TObject);
begin
  FPlannerItem.ItemPos := coboxPositions.ItemIndex;
end;

end.
