]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.logging.ui/src/org/simantics/logging/ui/handlers/SaveLogFilesHandler.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.logging.ui / src / org / simantics / logging / ui / handlers / SaveLogFilesHandler.java
1 package org.simantics.logging.ui.handlers;
2
3 import java.nio.file.Files;
4 import java.nio.file.Paths;
5
6 import javax.inject.Named;
7
8 import org.eclipse.e4.core.di.annotations.Execute;
9 import org.eclipse.e4.ui.services.IServiceConstants;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.widgets.FileDialog;
12 import org.eclipse.swt.widgets.Shell;
13 import org.simantics.logging.LogCollector;
14 import org.simantics.utils.ui.ExceptionUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class SaveLogFilesHandler {
19
20     private static final Logger LOGGER = LoggerFactory.getLogger(SaveLogFilesHandler.class);
21
22     private static final String[] FILTER_NAMES = { Messages.SaveLogFilesHandler_FilterZipArchive, Messages.SaveLogFilesHandler_FilterAllFiles };
23     private static final String[] FILTER_EXTENSIONS = { "*.zip", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$
24     private static final String USER_HOME = System.getProperty("user.home"); //$NON-NLS-1$
25
26     @Execute
27     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
28         FileDialog dialog = new FileDialog(shell, SWT.SAVE);
29         dialog.setFilterNames(FILTER_NAMES);
30         dialog.setFilterExtensions(FILTER_EXTENSIONS);
31         if (USER_HOME != null) {
32             if (Files.exists(Paths.get(USER_HOME))) {
33                 dialog.setFilterPath(USER_HOME);
34             }
35         }
36         String actualFileName = LogCollector.archiveFileName();
37         dialog.setFileName(actualFileName);
38
39         String destination = dialog.open();
40         if (destination != null) {
41             if (LOGGER.isDebugEnabled())
42                 LOGGER.debug("Destination for saving log files is {}", destination); //$NON-NLS-1$
43             try {
44                 LogCollector.archiveLogs(destination);
45             } catch (Throwable t) {
46                 LOGGER.error("Could not save log files to ZIP", t); //$NON-NLS-1$
47                 ExceptionUtils.logAndShowError("Could not save log files to ZIP", t); //$NON-NLS-1$
48             }
49         } else {
50             if (LOGGER.isDebugEnabled()) {
51                 LOGGER.debug("No destination selected for saving logs"); //$NON-NLS-1$
52             }
53         }
54     }
55
56 }