|
40 | 40 | import org.eclipse.nebula.widgets.nattable.util.PlatformHelper; |
41 | 41 | import org.eclipse.swt.SWT; |
42 | 42 | import org.eclipse.swt.graphics.Rectangle; |
| 43 | +import org.eclipse.swt.widgets.Display; |
43 | 44 | import org.eclipse.swt.widgets.ProgressBar; |
44 | 45 | import org.eclipse.swt.widgets.Shell; |
45 | 46 | import org.slf4j.Logger; |
@@ -107,6 +108,16 @@ public class NatExporter { |
107 | 108 | */ |
108 | 109 | private boolean useProgressDialog = false; |
109 | 110 |
|
| 111 | + /** |
| 112 | + * The {@link Runnable} that should be executed after the export finished |
| 113 | + * successfully. Useful in case {@link #openResult} is set to |
| 114 | + * <code>false</code> so an alternative for reporting the export success can |
| 115 | + * be configured. |
| 116 | + * |
| 117 | + * @since 2.6 |
| 118 | + */ |
| 119 | + private Runnable successRunnable; |
| 120 | + |
110 | 121 | /** |
111 | 122 | * Create a new {@link NatExporter}. |
112 | 123 | * |
@@ -160,13 +171,56 @@ public NatExporter(Shell shell, boolean executeSynchronously) { |
160 | 171 | * is made based on whether a {@link Shell} is set or not. If a |
161 | 172 | * {@link Shell} is set and this flag is set to <code>true</code> |
162 | 173 | * the execution is performed synchronously. |
| 174 | + * @param useProgressDialog |
| 175 | + * Configure whether the progress should be reported via |
| 176 | + * {@link ProgressMonitorDialog}. If set to <code>false</code> a |
| 177 | + * custom shell with a {@link ProgressBar} will be shown if the |
| 178 | + * shell parameter is not <code>null</code>. |
163 | 179 | * |
164 | 180 | * @since 2.3 |
165 | 181 | */ |
166 | 182 | public NatExporter(Shell shell, boolean executeSynchronously, boolean useProgressDialog) { |
| 183 | + this(shell, executeSynchronously, useProgressDialog, true, null); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Create a new {@link NatExporter}. |
| 188 | + * |
| 189 | + * @param shell |
| 190 | + * The {@link Shell} that should be used to open sub-dialogs and |
| 191 | + * perform export operations in a background thread. Can be |
| 192 | + * <code>null</code> but could lead to |
| 193 | + * {@link NullPointerException}s if {@link IExporter} are |
| 194 | + * configured, that use a {@link FileOutputStreamProvider}. |
| 195 | + * @param executeSynchronously |
| 196 | + * Configure whether the export should be performed |
| 197 | + * asynchronously or synchronously. By default the decision |
| 198 | + * whether the execution should be performed synchronously or not |
| 199 | + * is made based on whether a {@link Shell} is set or not. If a |
| 200 | + * {@link Shell} is set and this flag is set to <code>true</code> |
| 201 | + * the execution is performed synchronously. |
| 202 | + * @param useProgressDialog |
| 203 | + * Configure whether the progress should be reported via |
| 204 | + * {@link ProgressMonitorDialog}. If set to <code>false</code> a |
| 205 | + * custom shell with a {@link ProgressBar} will be shown if the |
| 206 | + * shell parameter is not <code>null</code>. |
| 207 | + * @param openResult |
| 208 | + * Configure if the created export result should be opened after |
| 209 | + * the export is finished. |
| 210 | + * @param successRunnable |
| 211 | + * The {@link Runnable} that should be executed after the export |
| 212 | + * finished successfully. Useful in case {@link #openResult} is |
| 213 | + * set to <code>false</code> so an alternative for reporting the |
| 214 | + * export success can be configured. |
| 215 | + * |
| 216 | + * @since 2.6 |
| 217 | + */ |
| 218 | + public NatExporter(Shell shell, boolean executeSynchronously, boolean useProgressDialog, boolean openResult, Runnable successRunnable) { |
167 | 219 | this.shell = shell; |
168 | 220 | this.runAsynchronously = !executeSynchronously; |
169 | 221 | this.useProgressDialog = useProgressDialog; |
| 222 | + this.openResult = openResult; |
| 223 | + this.successRunnable = successRunnable; |
170 | 224 | } |
171 | 225 |
|
172 | 226 | /** |
@@ -946,17 +1000,29 @@ protected void setClientAreaToMaximum(ILayer layer) { |
946 | 1000 | * @since 1.5 |
947 | 1001 | */ |
948 | 1002 | protected void openExport(IExporter exporter) { |
949 | | - if (this.exportSucceeded |
950 | | - && this.openResult |
951 | | - && exporter.getResult() != null |
952 | | - && exporter.getResult() instanceof File) { |
| 1003 | + if (this.exportSucceeded) { |
953 | 1004 |
|
954 | | - try { |
955 | | - Class<?> program = Class.forName("org.eclipse.swt.program.Program"); //$NON-NLS-1$ |
956 | | - Method launch = program.getMethod("launch", String.class); //$NON-NLS-1$ |
957 | | - launch.invoke(null, ((File) exporter.getResult()).getAbsolutePath()); |
958 | | - } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
959 | | - LOG.info("Could not open the export because org.eclipse.swt.program.Program, you are probably running a RAP application."); //$NON-NLS-1$ |
| 1005 | + if (this.successRunnable != null) { |
| 1006 | + if (this.shell != null) { |
| 1007 | + this.shell.getDisplay().syncExec(() -> { |
| 1008 | + this.successRunnable.run(); |
| 1009 | + }); |
| 1010 | + } else { |
| 1011 | + this.successRunnable.run(); |
| 1012 | + } |
| 1013 | + } |
| 1014 | + |
| 1015 | + if (this.openResult |
| 1016 | + && exporter.getResult() != null |
| 1017 | + && exporter.getResult() instanceof File) { |
| 1018 | + |
| 1019 | + try { |
| 1020 | + Class<?> program = Class.forName("org.eclipse.swt.program.Program"); //$NON-NLS-1$ |
| 1021 | + Method launch = program.getMethod("launch", String.class); //$NON-NLS-1$ |
| 1022 | + launch.invoke(null, ((File) exporter.getResult()).getAbsolutePath()); |
| 1023 | + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
| 1024 | + LOG.info("Could not open the export because org.eclipse.swt.program.Program, you are probably running a RAP application."); //$NON-NLS-1$ |
| 1025 | + } |
960 | 1026 | } |
961 | 1027 | } |
962 | 1028 | } |
@@ -1061,4 +1127,20 @@ public boolean isUseProgressDialog() { |
1061 | 1127 | public void setUseProgressDialog(boolean useProgressDialog) { |
1062 | 1128 | this.useProgressDialog = useProgressDialog; |
1063 | 1129 | } |
| 1130 | + |
| 1131 | + /** |
| 1132 | + * Configure a {@link Runnable} that should be executed after a successful |
| 1133 | + * export operation. If a {@link #shell} is set, the {@link Runnable} is |
| 1134 | + * executed using {@link Display#syncExec(Runnable)}. |
| 1135 | + * |
| 1136 | + * @param successRunnable |
| 1137 | + * The {@link Runnable} that should be executed after the export |
| 1138 | + * finished successfully. Useful in case {@link #openResult} is |
| 1139 | + * set to <code>false</code> so an alternative for reporting the |
| 1140 | + * export success can be configured. |
| 1141 | + * @since 2.6 |
| 1142 | + */ |
| 1143 | + public void setSuccessRunnable(Runnable successRunnable) { |
| 1144 | + this.successRunnable = successRunnable; |
| 1145 | + } |
1064 | 1146 | } |
0 commit comments