1 package fr.ifremer.quadrige3.ui.swing.action;
2
3 /*-
4 * #%L
5 * Quadrige3 Core :: Quadrige3 UI Common
6 * $Id:$
7 * $HeadURL:$
8 * %%
9 * Copyright (C) 2017 Ifremer
10 * %%
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * #L%
24 */
25
26
27 import fr.ifremer.quadrige3.ui.swing.ApplicationUIContext;
28 import fr.ifremer.quadrige3.ui.swing.Screen;
29 import fr.ifremer.quadrige3.ui.swing.content.AbstractMainUIHandler;
30
31 /**
32 * Action to change the screen.
33 * <p/>
34 * Will just check that the current screen can be quit via the {@link AbstractMainUIHandler#quitCurrentScreen()}.
35 * <p/>
36 *
37 * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
38 */
39 public abstract class AbstractChangeScreenAction extends AbstractMainUIAction {
40
41 /**
42 * Screen where to go.
43 *
44 * @since 1.0
45 */
46 protected Screen screen;
47
48 /**
49 * Flag to skip the check of current screen.
50 *
51 * @since 1.1
52 */
53 protected boolean skipCheckCurrentScreen;
54
55 /**
56 * <p>Constructor for AbstractChangeScreenAction.</p>
57 *
58 * @param handler a {@link AbstractMainUIHandler} object.
59 * @param hideBody a boolean.
60 * @param screen a {@link Screen} object.
61 */
62 protected AbstractChangeScreenAction(AbstractMainUIHandler handler,
63 boolean hideBody,
64 Screen screen) {
65 super(handler, hideBody);
66 this.screen = screen;
67 }
68
69 /**
70 * <p>Setter for the field <code>skipCheckCurrentScreen</code>.</p>
71 *
72 * @param skipCheckCurrentScreen a boolean.
73 */
74 public void setSkipCheckCurrentScreen(boolean skipCheckCurrentScreen) {
75 this.skipCheckCurrentScreen = skipCheckCurrentScreen;
76 }
77
78 /**
79 * <p>Setter for the field <code>screen</code>.</p>
80 *
81 * @param screen a {@link Screen} object.
82 */
83 protected void setScreen(Screen screen) {
84 this.screen = screen;
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 public boolean prepareAction() throws Exception {
90 boolean result = super.prepareAction();
91 result &= skipCheckCurrentScreen || getHandler().quitCurrentScreen();
92 return result;
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public void doAction() throws Exception {
98
99 ApplicationUIContext context = getContext();
100 Screen previousScreen = context.getScreen();
101
102 // add previous screen to breadcrumb if different to next screen
103 if (previousScreen != screen) {
104
105 if (previousScreen == null || Screen.HOME.equals(screen)) {
106
107 // reset breadcrumb
108 context.getScreenBreadcrumb().clear();
109
110 } else if (doNotTrackThisScreen()) {
111
112 // remove last screen before return to previous screen, except HOME
113 if (!Screen.HOME.equals(context.getScreenBreadcrumb().peekLast())) {
114 context.getScreenBreadcrumb().pollLast();
115 }
116
117 } else {
118
119 // add the previous screen to breadcrumb
120 if (!previousScreen.equals(context.getScreenBreadcrumb().peekLast())) {
121 context.getScreenBreadcrumb().addLast(previousScreen);
122 }
123
124 }
125 }
126
127 // correctly set the default screen
128 if (screen == null && !(this instanceof CloseApplicationAction)) {
129 screen = Screen.HOME;
130 }
131
132 // change screen
133 context.setScreen(screen);
134
135 }
136
137 /** {@inheritDoc} */
138 @Override
139 public void postFailedAction(Throwable error) {
140 if (error != null) {
141 getContext().setFallBackScreen();
142 }
143 }
144
145 /**
146 * used to indicate the action to not track the next screen in the breadcrumb override this method in inherited action to specify the behavior
147 *
148 * @return false (default)
149 */
150 protected boolean doNotTrackThisScreen() {
151 return false;
152 }
153
154 }