2020import static com .techsenger .patternfx .core .HistoryPolicy .APPEARANCE ;
2121import static com .techsenger .patternfx .core .HistoryPolicy .DATA ;
2222import static com .techsenger .patternfx .core .HistoryPolicy .NONE ;
23+ import javafx .beans .property .ObjectProperty ;
24+ import javafx .beans .property .SimpleObjectProperty ;
2325import org .slf4j .Logger ;
2426import org .slf4j .LoggerFactory ;
2527
@@ -31,34 +33,79 @@ public abstract class AbstractComponentViewModel<T extends ComponentMediator> im
3133
3234 private static final Logger logger = LoggerFactory .getLogger (AbstractComponentViewModel .class );
3335
36+ private final ObjectProperty <HistoryPolicy > historyPolicy = new SimpleObjectProperty <>(HistoryPolicy .NONE );
37+
38+ private HistoryProvider <? extends AbstractHistory > historyProvider ;
39+
40+ private AbstractHistory history ;
41+
3442 private T mediator ;
3543
44+ public AbstractComponentViewModel () {
45+
46+ }
47+
48+ @ Override
49+ public ObjectProperty <HistoryPolicy > historyPolicyProperty () {
50+ return historyPolicy ;
51+ }
52+
53+ @ Override
54+ public HistoryPolicy getHistoryPolicy () {
55+ return historyPolicy .get ();
56+ }
57+
58+ @ Override
59+ public void setHistoryPolicy (HistoryPolicy policy ) {
60+ historyPolicy .set (policy );
61+ }
62+
3663 public T getMediator () {
3764 return this .mediator ;
3865 }
3966
40- protected void restoreHistory () {
41- var policy = getMediator ().getHistoryPolicy ();
67+ protected void setHistoryProvider (HistoryProvider <? extends AbstractHistory > historyProvider ) {
68+ this .historyProvider = historyProvider ;
69+ }
70+
71+ /**
72+ * Returns the history of the component.
73+ * @return
74+ */
75+ protected AbstractHistory getHistory () {
76+ return history ;
77+ }
78+
79+ protected void setMediator (ComponentMediator mediator ) {
80+ this .mediator = (T ) mediator ;
81+ }
82+
83+ /**
84+ * Initializes the view model.
85+ */
86+ protected void initialize () {
87+ if (this .history != null ) {
88+ restoreHistory ();
89+ }
90+ }
91+
92+ protected final void restoreHistory () {
93+ var policy = getHistoryPolicy ();
4294 logger .debug ("{} History policy during restore: {}" , getMediator ().getLogPrefix (), policy );
43- ComponentHistory localHistory = null ;
4495 if (policy != NONE ) {
45- localHistory = getMediator ().getHistory ();
46- if (localHistory .isFresh ()) {
96+ if (history .isFresh ()) {
4797 logger .debug ("{} History is fresh. Skipping restoration" , getMediator ().getLogPrefix ());
4898 } else {
4999 switch (policy ) {
50100 case DATA :
51- localHistory .restoreData (this );
52- postHistoryRestore ();
101+ restoreData ();
53102 break ;
54103 case APPEARANCE :
55- localHistory .restoreAppearance (this );
56- postHistoryRestore ();
104+ restoreAppearance ();
57105 break ;
58106 case ALL :
59- localHistory .restoreData (this );
60- localHistory .restoreAppearance (this );
61- postHistoryRestore ();
107+ restoreData ();
108+ restoreAppearance ();
62109 break ;
63110 default :
64111 throw new AssertionError ();
@@ -67,23 +114,44 @@ protected void restoreHistory() {
67114 }
68115 }
69116
70- protected void saveHistory () {
71- var policy = getMediator ().getHistoryPolicy ();
117+ /**
118+ * Method copies all data from history to view model. This method is called at the beginning of initialization
119+ * when the policy is {@link HistoryPolicy#ALL} or {@link HistoryPolicy#DATA}.
120+ *
121+ * @param viewModel
122+ */
123+ protected void restoreData () { }
124+
125+ /**
126+ * Method copies all appearance information from history to view model. This method is called at the beginning
127+ * of initialization when the policy is {@link HistoryPolicy#ALL} or {@link HistoryPolicy#APPEARANCE}.
128+ *
129+ * @param viewModel
130+ */
131+ protected void restoreAppearance () { }
132+
133+ /**
134+ * Deinitializes the view model.
135+ */
136+ protected void deinitialize () {
137+ if (this .history != null ) {
138+ saveHistory ();
139+ }
140+ }
141+
142+ protected final void saveHistory () {
143+ var policy = getHistoryPolicy ();
72144 logger .debug ("{} History policy during save: {}" , getMediator ().getLogPrefix (), policy );
73145 switch (policy ) {
74146 case DATA :
75- preHistorySave ();
76- ((ComponentHistory ) getMediator ().getHistory ()).saveData (this );
147+ saveData ();
77148 break ;
78149 case APPEARANCE :
79- preHistorySave ();
80- ((ComponentHistory ) getMediator ().getHistory ()).saveAppearance (this );
150+ saveAppearance ();
81151 break ;
82152 case ALL :
83- preHistorySave ();
84- var h = (ComponentHistory ) getMediator ().getHistory ();
85- h .saveData (this );
86- h .saveAppearance (this );
153+ saveData ();
154+ saveAppearance ();
87155 break ;
88156 case NONE :
89157 break ;
@@ -92,22 +160,30 @@ protected void saveHistory() {
92160 }
93161 }
94162
95- // todo: remove
96- protected void postHistoryRestore () { }
97-
98- protected void preHistorySave () { }
99-
100163 /**
101- * Initializes the view model.
164+ * Method copies all data from view model to history. This method is called at the beginning of deinitialization
165+ * when the policy is {@link HistoryPolicy#ALL} or {@link HistoryPolicy#DATA}.
166+ *
167+ * @param viewModel
102168 */
103- protected void initialize () { }
169+ protected void saveData () {
170+ getHistory ().setFresh (false );
171+ }
104172
105173 /**
106- * Deinitializes the view model.
174+ * Method copies all data from view model to history. This method is called at the beginning of deinitialization
175+ * when the policy is {@link HistoryPolicy#ALL} or {@link HistoryPolicy#APPEARANCE}.
176+ *
177+ * @param viewModel
107178 */
108- protected void deinitialize () { }
179+ protected void saveAppearance () {
180+ getHistory ().setFresh (false );
181+ }
109182
110- protected void setMediator (ComponentMediator mediator ) {
111- this .mediator = (T ) mediator ;
183+ void prepareHistory () {
184+ if (this .historyProvider != null ) {
185+ this .history = this .historyProvider .provide ();
186+ this .historyProvider = null ;
187+ }
112188 }
113189}
0 commit comments