|
| 1 | +/* |
| 2 | + * Copyright 2018 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.utplsql.sqldev |
| 17 | + |
| 18 | +import java.io.File |
| 19 | +import java.util.logging.Logger |
| 20 | +import javax.swing.JComboBox |
| 21 | +import javax.swing.JFileChooser |
| 22 | +import javax.swing.JFrame |
| 23 | +import javax.swing.JTextField |
| 24 | + |
| 25 | +class DirectoryChooser { |
| 26 | + val static Logger logger = Logger.getLogger(DirectoryChooser.name) |
| 27 | + |
| 28 | + def static choose (JFrame parentFrame, String title, String initialDirectory) { |
| 29 | + logger.finest('''parantFrame: «parentFrame»''') |
| 30 | + var String ret = null |
| 31 | + val chooser = new JFileChooser() |
| 32 | + chooser.currentDirectory = new File(initialDirectory) |
| 33 | + chooser.dialogTitle = title |
| 34 | + chooser.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY |
| 35 | + chooser.acceptAllFileFilterUsed = false |
| 36 | + if (chooser.showOpenDialog(parentFrame) == JFileChooser.APPROVE_OPTION) { |
| 37 | + ret = chooser.selectedFile.absolutePath |
| 38 | + } |
| 39 | + return ret |
| 40 | + } |
| 41 | + |
| 42 | + def static void choose (JFrame parentFrame, String title, JTextField textField) { |
| 43 | + val dir = choose(parentFrame, title, textField.text) |
| 44 | + if (dir !== null) { |
| 45 | + textField.text = dir |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + def static void choose (JFrame parentFrame, String title, JComboBox<String> comboBox) { |
| 50 | + val dir = choose(parentFrame, title, comboBox.editor.item as String); |
| 51 | + if (dir !== null) { |
| 52 | + comboBox.editor.item = dir |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments