-
Notifications
You must be signed in to change notification settings - Fork 5
/
ViewVariantsController.cs
181 lines (174 loc) · 9.53 KB
/
ViewVariantsController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Utils;
using DevExpress.ExpressApp.Model;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Templates;
namespace ViewSettingsEF.Module.BusinessObjects {
public class ViewVariantsController : ViewController {
private SimpleAction DeleteViewVariantAction;
private PopupWindowShowAction CreateViewVariantAction;
private SingleChoiceAction SelectViewVariantAction;
private SimpleAction UpdateCurrentViewVariantAction;
private SimpleAction UpdateDefaultSettingsWithSelectedVariantAction;
private string defaultUserSettings;
private Boolean isLayoutProcessed = false;
private Boolean isDefaultViewSelected = false;
private ChoiceActionItem lastSelectedItem = null;
private void SetDifferences(string xml, IModelView model) {
Dictionary<string, string> differences = new Dictionary<string, string>();
differences.Add("", xml);
UserDifferencesHelper.SetUserDifferences(model, differences);
}
private Boolean TryLoadViewVariantFromXML(string xml) {
Boolean result = false;
View savedView = Frame.View;
isLayoutProcessed = true;
foreach(ISupportUpdate controller in Frame.Controllers) {
controller.BeginUpdate();
}
try {
if(Frame.SetView(null, true, null, false)) {
if(isDefaultViewSelected) {
UpdateDefaultSettings(savedView.Model);
isDefaultViewSelected = false;
}
SetDifferences(xml, savedView.Model);
savedView.LoadModel(false);
Frame.SetView(savedView);
result = true;
}
}
finally {
foreach(ISupportUpdate controller in Frame.Controllers) {
controller.EndUpdate();
}
}
isLayoutProcessed = false;
return result;
}
private void SaveViewVariantToXML(SettingsStore store) {
isLayoutProcessed = true;
View.SaveModel();
isLayoutProcessed = false;
store.Xml = UserDifferencesHelper.GetUserDifferences(View.Model)[""];
((IObjectSpaceLink)store).ObjectSpace.CommitChanges();
}
private void UpdateDefaultSettings(IModelView model) {
defaultUserSettings = UserDifferencesHelper.GetUserDifferences(model)[""];
}
private void UpdateActions(string itemToSelectCaption) {
SelectViewVariantAction.Items.Clear();
lastSelectedItem = null;
CriteriaOperator criteria = CriteriaOperator.Parse("([ViewId] = ?) And ([IsShared] Or [OwnerId] is null Or [OwnerId] = ?)", View.Id, SecuritySystem.CurrentUserId.ToString());
IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(SettingsStore));
var tst = objectSpace.GetObjects<SettingsStore>(criteria);
foreach (SettingsStore item in objectSpace.GetObjects<SettingsStore>(criteria)) {
SelectViewVariantAction.Items.Add(new ChoiceActionItem(item.Name, item));
}
if(SelectViewVariantAction.Items.Count > 0) {
ChoiceActionItem defaultItem = new ChoiceActionItem("Default", null);
SelectViewVariantAction.Items.Add(defaultItem);
ChoiceActionItem itemToSelect = SelectViewVariantAction.Items.FindItemByID(itemToSelectCaption);
SelectViewVariantAction.SelectedItem = (itemToSelect != null) ? itemToSelect : defaultItem;
lastSelectedItem = SelectViewVariantAction.SelectedItem;
}
UpdateActionsActive();
}
private void UpdateActionsActive() {
Boolean isActive = SelectViewVariantAction.Items.Count > 0 && SelectViewVariantAction.SelectedItem.Data != null;
DeleteViewVariantAction.Active["HasVariants"] = isActive;
UpdateCurrentViewVariantAction.Active["HasVariants"] = isActive;
UpdateDefaultSettingsWithSelectedVariantAction.Active["HasVariants"] = isActive;
}
private void DeleteViewVariantAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
IObjectSpaceLink currentLayoutItem = SelectViewVariantAction.SelectedItem.Data as IObjectSpaceLink;
if(TryLoadViewVariantFromXML(defaultUserSettings)) {
IObjectSpace os = currentLayoutItem.ObjectSpace;
os.Delete(currentLayoutItem);
os.CommitChanges();
isDefaultViewSelected = true;
UpdateActions(null);
}
}
private void CreateViewVariantAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
IObjectSpace objectSpace = e.Application.CreateObjectSpace(typeof(SettingsStore));
e.View = e.Application.CreateDetailView(objectSpace, objectSpace.CreateObject<SettingsStore>());
}
private void CreateViewVariantAction_Execute(object sender, PopupWindowShowActionExecuteEventArgs e) {
SettingsStore store = (SettingsStore)e.PopupWindowViewCurrentObject;
if(SecuritySystem.CurrentUserId != null) {
store.OwnerId = SecuritySystem.CurrentUserId.ToString();
}
store.ViewId = View.Id;
SaveViewVariantToXML(store);
string itemToSelectCaption = store.Name;
isDefaultViewSelected = false;
UpdateActions(itemToSelectCaption);
}
private void SelectViewVariantAction_Execute(object sender, SingleChoiceActionExecuteEventArgs e) {
bool isVariantChanged = false;
ChoiceActionItem currentItem = SelectViewVariantAction.SelectedItem;
if(currentItem.Data != null) {
if(TryLoadViewVariantFromXML(((SettingsStore)currentItem.Data).Xml)) {
isVariantChanged = true;
}
} else {
if(TryLoadViewVariantFromXML(defaultUserSettings)) {
isDefaultViewSelected = true;
isVariantChanged = true;
}
}
SelectViewVariantAction.SelectedItem = isVariantChanged ? currentItem : lastSelectedItem;
UpdateActionsActive();
}
private void UpdateCurrentViewVariantAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
SaveViewVariantToXML(SelectViewVariantAction.SelectedItem.Data as SettingsStore);
}
private void UpdateDefaultSettingsWithSelectedVariantAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
SaveViewVariantToXML(SelectViewVariantAction.SelectedItem.Data as SettingsStore);
UpdateDefaultSettings(View.Model);
}
private void View_ModelSaving(object sender, System.ComponentModel.CancelEventArgs e) {
if(!isLayoutProcessed && !isDefaultViewSelected) {
SetDifferences(defaultUserSettings, View.Model);
e.Cancel = true;
}
}
protected override void OnActivated() {
base.OnActivated();
View.ModelSaving += View_ModelSaving;
if(!isLayoutProcessed) {
UpdateDefaultSettings(View.Model);
isDefaultViewSelected = true;
UpdateActions(null);
}
}
protected override void OnDeactivated() {
View.ModelSaving -= View_ModelSaving;
base.OnDeactivated();
}
public ViewVariantsController() {
this.TargetViewNesting = Nesting.Root;
this.DeleteViewVariantAction = new SimpleAction(this, "DeleteViewVariant", "Edit");
this.DeleteViewVariantAction.Execute += new SimpleActionExecuteEventHandler(this.DeleteViewVariantAction_Execute);
this.CreateViewVariantAction = new PopupWindowShowAction(this, "SaveAsNewViewVariant", "Edit");
this.CreateViewVariantAction.CustomizePopupWindowParams += new CustomizePopupWindowParamsEventHandler(this.CreateViewVariantAction_CustomizePopupWindowParams);
this.CreateViewVariantAction.Execute += new PopupWindowShowActionExecuteEventHandler(this.CreateViewVariantAction_Execute);
this.SelectViewVariantAction = new SingleChoiceAction(this, "SelectViewVariant", "Edit");
this.SelectViewVariantAction.PaintStyle = ActionItemPaintStyle.Caption;
this.SelectViewVariantAction.Execute += new SingleChoiceActionExecuteEventHandler(this.SelectViewVariantAction_Execute);
this.UpdateCurrentViewVariantAction = new SimpleAction(this, "UpdateCurrentViewVariant", "Edit");
this.UpdateCurrentViewVariantAction.Execute += new SimpleActionExecuteEventHandler(this.UpdateCurrentViewVariantAction_Execute);
this.UpdateDefaultSettingsWithSelectedVariantAction = new SimpleAction(this, "UpdateDefaultViewVariant", "Edit");
UpdateDefaultSettingsWithSelectedVariantAction.Execute += UpdateDefaultSettingsWithSelectedVariantAction_Execute;
this.Actions.Add(this.DeleteViewVariantAction);
this.Actions.Add(this.CreateViewVariantAction);
this.Actions.Add(this.SelectViewVariantAction);
this.Actions.Add(this.UpdateCurrentViewVariantAction);
this.Actions.Add(this.UpdateDefaultSettingsWithSelectedVariantAction);
}
}
}