Skip to content

Commit 2aee155

Browse files
committed
Refactor history
1 parent 2a52352 commit 2aee155

File tree

8 files changed

+146
-197
lines changed

8 files changed

+146
-197
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ The `Component` is responsible for:
163163
1. Initializing and deinitializing the component.
164164
2. Providing component data and related objects that directly belong to the component:
165165
- Structural data (parent/children references);
166-
- Persistence data (component history);
167-
- Configuration data (component settings);
168166
- Lifecycle data (component state);
169167
- Metadata (component ID, type, version, etc.).
170168
3. Creating, initializing, adding to the component tree, removing from the component tree, and deinitializing
@@ -220,8 +218,8 @@ In addition to the four main classes, a component may include a `ComponentHistor
220218
state across its lifecycle. In the default implementation, the `ComponentHistory` instance is lazily provided via a
221219
`HistoryProvider` that is set before initialization. During the `preInitialize()` phase, the provider’s `provide()`
222220
method is called to obtain the history. After the history is obtained, the provider is cleared (set to null), and the
223-
component uses the history. State restoration occurs in the `preInitialize()` phase via the
224-
`AbstractComponentViewModel#restoreHistory()` method. State saving occurs in the `postDeinitialize()` phase via the
221+
component uses the history. State restoration occurs in the `initialize()` phase via the
222+
`AbstractComponentViewModel#restoreHistory()` method. State saving occurs in the `deinitialize()` phase via the
225223
`AbstractComponentViewModel#saveHistory()` method. The volume and type of state information that is restored and
226224
persisted are determined by the `HistoryPolicy` enum.
227225

patternfx-core/src/main/java/com/techsenger/patternfx/core/AbstractComponent.java

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,6 @@ public ReadOnlyObjectProperty<ComponentState> stateProperty() {
7979
return component.stateProperty();
8080
}
8181

82-
@Override
83-
public ObjectProperty<HistoryPolicy> historyPolicyProperty() {
84-
return component.historyPolicyProperty();
85-
}
86-
87-
@Override
88-
public HistoryPolicy getHistoryPolicy() {
89-
return component.getHistoryPolicy();
90-
}
91-
92-
@Override
93-
public void setHistoryPolicy(HistoryPolicy policy) {
94-
component.setHistoryPolicy(policy);
95-
}
96-
97-
@Override
98-
public ComponentHistory<?> getHistory() {
99-
return component.getHistory();
100-
}
101-
10282
@Override
10383
public ComponentGroup getGroup() {
10484
return component.getGroup();
@@ -130,14 +110,8 @@ public void deinitialize() {
130110

131111
private final ReadOnlyObjectWrapper<ComponentState> state = new ReadOnlyObjectWrapper<>(ComponentState.CREATING);
132112

133-
private final ObjectProperty<HistoryPolicy> historyPolicy = new SimpleObjectProperty<>(HistoryPolicy.NONE);
134-
135113
private final ObjectProperty<ComponentGroup> group = new SimpleObjectProperty<>();
136114

137-
private HistoryProvider<?> historyProvider;
138-
139-
private ComponentHistory<?> history;
140-
141115
public AbstractComponent(T view) {
142116
this.view = view;
143117
this.view.setComponent(this);
@@ -177,26 +151,6 @@ public ReadOnlyObjectProperty<ComponentState> stateProperty() {
177151
return state.getReadOnlyProperty();
178152
}
179153

180-
@Override
181-
public ObjectProperty<HistoryPolicy> historyPolicyProperty() {
182-
return historyPolicy;
183-
}
184-
185-
@Override
186-
public HistoryPolicy getHistoryPolicy() {
187-
return historyPolicy.get();
188-
}
189-
190-
@Override
191-
public void setHistoryPolicy(HistoryPolicy policy) {
192-
historyPolicy.set(policy);
193-
}
194-
195-
@Override
196-
public ComponentHistory<?> getHistory() {
197-
return history;
198-
}
199-
200154
@Override
201155
public ComponentGroup getGroup() {
202156
return group.get();
@@ -266,11 +220,7 @@ protected String resolveLogPrefix() {
266220
protected void preInitialize() {
267221
var mediator = createMediator();
268222
this.view.getViewModel().setMediator(mediator);
269-
if (this.historyProvider != null) {
270-
this.history = this.historyProvider.provide();
271-
this.historyProvider = null;
272-
}
273-
this.view.getViewModel().restoreHistory();
223+
this.view.getViewModel().prepareHistory();
274224
}
275225

276226
/**
@@ -286,15 +236,7 @@ protected void preDeinitialize() { }
286236
/**
287237
* The last method called in deinitialization.
288238
*/
289-
protected void postDeinitialize() {
290-
if (this.history != null) {
291-
this.view.getViewModel().saveHistory();
292-
}
293-
}
294-
295-
protected void setHistoryProvider(HistoryProvider<? extends ComponentHistory<?>> historyProvider) {
296-
this.historyProvider = historyProvider;
297-
}
239+
protected void postDeinitialize() { }
298240

299241
protected abstract Mediator createMediator();
300242
}

patternfx-core/src/main/java/com/techsenger/patternfx/core/AbstractComponentViewModel.java

Lines changed: 109 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static com.techsenger.patternfx.core.HistoryPolicy.APPEARANCE;
2121
import static com.techsenger.patternfx.core.HistoryPolicy.DATA;
2222
import static com.techsenger.patternfx.core.HistoryPolicy.NONE;
23+
import javafx.beans.property.ObjectProperty;
24+
import javafx.beans.property.SimpleObjectProperty;
2325
import org.slf4j.Logger;
2426
import 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
}

patternfx-core/src/main/java/com/techsenger/patternfx/core/AbstractHistory.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Pavel Castornii
2222
*/
23-
public abstract class AbstractHistory<T extends AbstractComponentViewModel<?>> implements ComponentHistory<T> {
23+
public abstract class AbstractHistory implements ComponentHistory {
2424

2525
private boolean fresh = true;
2626

@@ -38,23 +38,7 @@ public void preSerialize() {
3838

3939
}
4040

41-
@Override
42-
public void restoreData(T viewModel) {
43-
44-
}
45-
46-
@Override
47-
public void saveData(T viewModel) {
48-
this.fresh = false;
49-
}
50-
51-
@Override
52-
public void restoreAppearance(T viewModel) {
53-
54-
}
55-
56-
@Override
57-
public void saveAppearance(T viewModel) {
58-
this.fresh = false;
41+
void setFresh(boolean fresh) {
42+
this.fresh = fresh;
5943
}
6044
}

patternfx-core/src/main/java/com/techsenger/patternfx/core/ComponentDescriptor.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,6 @@ public interface ComponentDescriptor {
6969
*/
7070
ReadOnlyObjectProperty<ComponentState> stateProperty();
7171

72-
/**
73-
* Returns the property for the history policy.
74-
*
75-
* @return
76-
*/
77-
ObjectProperty<HistoryPolicy> historyPolicyProperty();
78-
79-
/**
80-
* Returns the history policy.
81-
*
82-
* @return
83-
*/
84-
HistoryPolicy getHistoryPolicy();
85-
86-
/**
87-
* Sets the history policy to the specified value.
88-
*
89-
* @param policy the history policy to set.
90-
*/
91-
void setHistoryPolicy(HistoryPolicy policy);
92-
93-
/**
94-
* Returns the history of the component.
95-
* @return
96-
*/
97-
ComponentHistory<?> getHistory();
98-
9972
/**
10073
* Returns the current {@link ComponentGroup}.
10174
*

0 commit comments

Comments
 (0)