| | |
| | | package de.micromata.borgbutler.server.rest; |
| | | |
| | | import de.micromata.borgbutler.json.JsonUtils; |
| | | import de.micromata.borgbutler.server.RunningMode; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | import javax.ws.rs.QueryParam; |
| | | import javax.ws.rs.core.Context; |
| | | import javax.ws.rs.core.MediaType; |
| | | import java.awt.*; |
| | | import java.io.File; |
| | | |
| | | @Path("/files") |
| | |
| | | log.info(msg); |
| | | return msg; |
| | | } |
| | | if (chooser != null) { |
| | | if (fileDialog != null || fileChooser != null) { |
| | | log.warn("Cannot call already opened file choose twice. Close file chooser first."); |
| | | return "{\"directory\": \"\"}"; |
| | | } |
| | |
| | | if (frame == null) { |
| | | frame = new JFrame("BorgButler"); |
| | | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| | | JLabel label = new JLabel("Hello World"); |
| | | frame.getContentPane().add(label); |
| | | frame.pack(); |
| | | frame.setSize(300, 100); |
| | | frame.setResizable(false); |
| | | frame.setLocationRelativeTo(null); |
| | | frame.setBackground(Color.WHITE); |
| | | frame.getContentPane().setBackground(Color.WHITE); |
| | | JLabel label = new JLabel("Click for choosing directory...", SwingConstants.CENTER); |
| | | frame.add(label); |
| | | } |
| | | try { |
| | | if (StringUtils.isNotBlank(current)) { |
| | | chooser = new JFileChooser(current); |
| | | } else { |
| | | chooser = new JFileChooser(); |
| | | } |
| | | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| | | |
| | | frame.setVisible(true); |
| | | if (RunningMode.getOSType() == RunningMode.OSType.MAC_OS) { |
| | | // The JFileChooser will hang after several calls, use AWT file dialog instead for Mac OS: |
| | | System.setProperty("apple.awt.fileDialogForDirectories", "true"); |
| | | frame.setAlwaysOnTop(true); |
| | | int returnCode = chooser.showDialog(frame, "Choose"); |
| | | frame.setVisible(false); |
| | | frame.setAlwaysOnTop(false); |
| | | if (returnCode == JFileChooser.APPROVE_OPTION) { |
| | | file = chooser.getSelectedFile(); |
| | | frame.setVisible(true); |
| | | try { |
| | | fileDialog = new FileDialog(frame, "Choose a directory", FileDialog.LOAD); |
| | | if (StringUtils.isNotBlank(current)) { |
| | | fileDialog.setDirectory(current); |
| | | } |
| | | fileDialog.toFront(); |
| | | fileDialog.setVisible(true); |
| | | String filename = fileDialog.getFile(); |
| | | String directory = fileDialog.getDirectory(); |
| | | frame.setVisible(false); |
| | | if (filename == null) { |
| | | return ""; |
| | | } |
| | | file = new File(directory, filename); |
| | | if (!file.isDirectory()) { |
| | | file = new File(directory); |
| | | } |
| | | } finally { |
| | | fileDialog = null; |
| | | } |
| | | } finally { |
| | | chooser = null; |
| | | } else { |
| | | try { |
| | | if (StringUtils.isNotBlank(current)) { |
| | | fileChooser = new JFileChooser(current); |
| | | } else { |
| | | fileChooser = new JFileChooser(); |
| | | } |
| | | fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| | | frame.setVisible(true); |
| | | frame.setAlwaysOnTop(true); |
| | | int returnCode = fileChooser.showDialog(frame, "Choose"); |
| | | frame.setVisible(false); |
| | | frame.setAlwaysOnTop(false); |
| | | if (returnCode == JFileChooser.APPROVE_OPTION) { |
| | | file = fileChooser.getSelectedFile(); |
| | | } |
| | | } finally { |
| | | fileChooser = null; |
| | | } |
| | | } |
| | | } |
| | | String filename = file != null ? JsonUtils.toJson(file.getAbsolutePath()) : ""; |
| | |
| | | } |
| | | |
| | | private static JFrame frame; |
| | | private static JFileChooser chooser; |
| | | private static FileDialog fileDialog; |
| | | private static JFileChooser fileChooser; |
| | | } |