Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dr2 1966 add ability to drag and drop files into text boxes #510

Merged
merged 7 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, macos-13, windows-latest]
jdk: [8, 11, 17]
runs-on: ${{ matrix.os }}
steps:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ project/plugins/project/
# IntelliJ specific
.idea/*
*.iml

# MacOS specific
**/.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ import uk.gov.nationalarchives.csv.validator.ui.ScalaSwingHelpers._
import uk.gov.nationalarchives.csv.validator.{EOL, FailMessage, ProgressCallback}

import java.awt.Cursor
import java.io.IOException
import java.awt.datatransfer.DataFlavor
import java.io.{File, IOException}
import java.net.URL
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.{Files, Path, Paths, StandardOpenOption}
import java.util
import java.util.Properties
import java.util.jar.{Attributes, Manifest}
import javax.swing._
import javax.swing.filechooser.FileNameExtensionFilter
import javax.swing.table.DefaultTableModel
import scala.jdk.CollectionConverters.CollectionHasAsScala
import scala.language.reflectiveCalls
import scala.swing.GridBagPanel.Anchor
import scala.swing.PopupMenuImplicits._
import scala.swing._
import scala.util.Using
import scala.util.{Failure, Success, Try, Using}

/**
* Simple GUI for the CSV Validator
Expand Down Expand Up @@ -229,7 +232,39 @@ object CsvValidatorUi extends SimpleSwingApplication {
private val layout = new DesignGridLayout(peer)

private val lblCsvFile = new Label("CSV file:")
private val txtCsvFile = new TextField(30)
private val txtCsvFile = new JTextField(30)

final class FileDropHandler(fileExt: String, label: Label) extends TransferHandler {
override def canImport(support: TransferHandler.TransferSupport): Boolean = support.getDataFlavors.exists(_.isFlavorJavaFileListType)

@SuppressWarnings(Array("unchecked"))
override def importData(support: TransferHandler.TransferSupport): Boolean = {
if (!this.canImport(support)) false
else {
val potentialFiles = Try {
support.getTransferable.getTransferData(DataFlavor.javaFileListFlavor).asInstanceOf[util.List[File]].asScala.toList
}
potentialFiles match {
case Failure(ex) => false
case Success(files) =>
lazy val pathOfFirstFile = files.head.getAbsolutePath
if(files.length != 1) {
outputToReport(s"Error: Please drag only 1 file into the '${label.text}' text box.")
false
}
else if(!pathOfFirstFile.endsWith(fileExt)) {
outputToReport(s"Error: Please drag only '$fileExt' files into the '${label.text}' text box.")
false
}
else {
(if(fileExt == ".csv") txtCsvFile else txtCsvSchemaFile).setText(pathOfFirstFile)
true
}
}
}
}
}
txtCsvFile.setTransferHandler(new FileDropHandler(".csv", lblCsvFile))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please move this line above the FileDropDownHandler class definition, just so that it stays close to the textBox creation from readability perspective

private val csvFileChooser = new FileChooser(loadSettings match {
case Some(s) =>
s.lastCsvPath.toFile
Expand All @@ -253,7 +288,8 @@ object CsvValidatorUi extends SimpleSwingApplication {
}

private val lblCsvSchemaFile = new Label("CSV Schema file:")
private val txtCsvSchemaFile = new TextField(30)
private val txtCsvSchemaFile = new JTextField(30)
txtCsvSchemaFile.setTransferHandler(new FileDropHandler(".csvs", lblCsvSchemaFile))
private val csvSchemaFileChooser = new FileChooser(loadSettings match {
case Some(s) =>
s.lastCsvSchemaPath.toFile
Expand Down Expand Up @@ -322,9 +358,9 @@ object CsvValidatorUi extends SimpleSwingApplication {
action = {
txtArReport.text = ""
CsvValidatorUi.this.validate(
txtCsvFile.text,
txtCsvFile.getText,
settingsPanel.csvEncoding,
txtCsvSchemaFile.text,
txtCsvSchemaFile.getText,
settingsPanel.csvSchemaEncoding,
settingsPanel.failOnFirstError,
settingsPanel.pathSubstitutions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.beans.{PropertyChangeEvent, PropertyChangeListener}
import java.nio.file.Path
import scala.swing.Dialog.Message
import java.io.IOException
import javax.swing.JTextField

/**
* Some simple helpers to ease
Expand All @@ -30,8 +31,8 @@ object ScalaSwingHelpers {
* @param output A text component which displays the absolute path of the chosen file
* @param locateOver A component over which the FileChooser dialog should be located
*/
def chooseFile(fileChooser: FileChooser, output: TextComponent, locateOver: Component) : Unit = {
chooseFile(fileChooser, {f => output.text = f.toAbsolutePath.toString; None}, locateOver)
def chooseFile(fileChooser: FileChooser, output: JTextField, locateOver: Component) : Unit = {
chooseFile(fileChooser, {f => output.setText(f.toAbsolutePath.toString); None}, locateOver)
}

/**
Expand Down
Loading