1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.ui;
18
19 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
20 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
21 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
22 import io.github.tonywasher.joceanus.metis.ui.MetisIcon;
23 import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
24 import io.github.tonywasher.joceanus.prometheus.views.PrometheusUIEvent;
25 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
26 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButton;
27 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButtonFactory;
28 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
29 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
30 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIPaneFactory;
31
32
33
34
35 public class PrometheusActionButtons
36 implements OceanusEventProvider<PrometheusUIEvent>, TethysUIComponent {
37
38
39
40 protected static final int STRUT_LENGTH = 5;
41
42
43
44
45 private static final String NLS_TITLE = PrometheusUIResource.ACTION_TITLE_SAVE.getValue();
46
47
48
49
50 private final OceanusEventManager<PrometheusUIEvent> theEventManager;
51
52
53
54
55 private final PrometheusEditSet theUpdateSet;
56
57
58
59
60 private final TethysUIBoxPaneManager thePanel;
61
62
63
64
65 private final TethysUIButton theCommitButton;
66
67
68
69
70 private final TethysUIButton theUndoButton;
71
72
73
74
75 private final TethysUIButton theResetButton;
76
77
78
79
80
81
82
83 public PrometheusActionButtons(final TethysUIFactory<?> pFactory,
84 final PrometheusEditSet pUpdateSet) {
85 this(pFactory, pUpdateSet, true);
86 }
87
88
89
90
91
92
93
94
95 public PrometheusActionButtons(final TethysUIFactory<?> pFactory,
96 final PrometheusEditSet pUpdateSet,
97 final boolean pHorizontal) {
98
99 theUpdateSet = pUpdateSet;
100
101
102 theEventManager = new OceanusEventManager<>();
103
104
105 final TethysUIButtonFactory<?> myButtons = pFactory.buttonFactory();
106 theCommitButton = myButtons.newButton();
107 theUndoButton = myButtons.newButton();
108 theResetButton = myButtons.newButton();
109
110
111 MetisIcon.configureCommitIconButton(theCommitButton);
112 MetisIcon.configureUndoIconButton(theUndoButton);
113 MetisIcon.configureResetIconButton(theResetButton);
114
115
116 theCommitButton.getEventRegistrar().addEventListener(e -> theEventManager.fireEvent(PrometheusUIEvent.OK));
117 theUndoButton.getEventRegistrar().addEventListener(e -> theEventManager.fireEvent(PrometheusUIEvent.UNDO));
118 theResetButton.getEventRegistrar().addEventListener(e -> theEventManager.fireEvent(PrometheusUIEvent.RESET));
119
120
121 final TethysUIPaneFactory myPanes = pFactory.paneFactory();
122 thePanel = pHorizontal
123 ? myPanes.newHBoxPane()
124 : myPanes.newVBoxPane();
125
126
127 if (!pHorizontal) {
128 thePanel.addNode(pFactory.controlFactory().newLabel(NLS_TITLE));
129 }
130 thePanel.addNode(theCommitButton);
131 thePanel.addNode(theUndoButton);
132 thePanel.addNode(theResetButton);
133
134
135 if (pHorizontal) {
136 thePanel.setBorderTitle(NLS_TITLE);
137 }
138
139
140 setEnabled(false);
141 }
142
143 @Override
144 public TethysUIComponent getUnderlying() {
145 return thePanel;
146 }
147
148 @Override
149 public OceanusEventRegistrar<PrometheusUIEvent> getEventRegistrar() {
150 return theEventManager.getEventRegistrar();
151 }
152
153 @Override
154 public void setEnabled(final boolean bEnabled) {
155
156 if (!bEnabled) {
157 theCommitButton.setEnabled(false);
158 theUndoButton.setEnabled(false);
159 theResetButton.setEnabled(false);
160
161
162 } else {
163
164 switch (theUpdateSet.getEditState()) {
165 case CLEAN:
166 theCommitButton.setEnabled(false);
167 theUndoButton.setEnabled(false);
168 theResetButton.setEnabled(theUpdateSet.hasUpdates());
169 break;
170 case DIRTY:
171 case ERROR:
172 theCommitButton.setEnabled(false);
173 theUndoButton.setEnabled(true);
174 theResetButton.setEnabled(true);
175 break;
176 case VALID:
177 theCommitButton.setEnabled(true);
178 theUndoButton.setEnabled(true);
179 theResetButton.setEnabled(true);
180 break;
181 default:
182 break;
183 }
184 }
185 }
186 }