annotate src/name/blackcap/imageprep/Main.kt @ 13:3feeb953d9ae

val -> var
author David Barts <n5jrn@me.com>
date Sat, 18 Jul 2020 14:23:02 -0700
parents 884f1415a330
children 5fa5d15b4a7b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
1 /*
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
2 * Entry point, etc.
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
3 */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
4 package name.blackcap.imageprep
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
5
4
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
6 import java.io.File
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
7 import java.util.logging.Level
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
8 import java.util.logging.Logger
4
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
9 import javax.swing.UIManager
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
10 import name.blackcap.kcli.CommandLine
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
11 import name.blackcap.kcli.InvalidArgumentException
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
12 import name.blackcap.kcli.Option
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
13 import name.blackcap.kcli.PromptingParser
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
14 import org.apache.commons.cli.HelpFormatter
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
15 import org.apache.commons.cli.Options
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
16 import org.apache.commons.cli.ParseException
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
17
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
18 object Application {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
19 /* name we call ourselves */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
20 val MYNAME = "ImagePrep"
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
21
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
22 /* global UI objects */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
23 var mainFrame: MainFrame by setOnce()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
24 var helpDialog: HelpDialog by setOnce()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
25
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
26 fun initialize() {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
27 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
28 mainFrame = MainFrame() /* must be created first */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
29 mainFrame.jMenuBar = MyMenuBar()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
30 setMacMenus() /* always safe to call; no-op if not a Mac */
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
31 helpDialog = HelpDialog()
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
32 mainFrame.setVisible(true)
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
33 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
34 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
35
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
36 fun main(args: Array<String>) {
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
37 /* start up */
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
38 LOGGER.log(Level.INFO, "beginning execution")
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
39 if (OS.type == OS.MAC) {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
40 System.setProperty("apple.laf.useScreenMenuBar", "true")
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
41 }
4
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
42
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
43 /* parse command line */
4
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
44 if (Settings.outputToInputDir)
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
45 Settings.outputTo = System.getProperty("user.dir")
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
46 val options = Options().apply {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
47 addOption(Option<Unit>("?", "help", false, "Print this help message."))
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
48 addOption(Option<Int>("m", "maxdim", true, "Maximum dimension (default: ${Settings.maxDimension})").apply {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
49 default = Settings.maxDimension
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
50 interpolater = {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
51 val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
52 if (ret < 1)
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
53 throw InvalidArgumentException("Dimension must be a positive integer.")
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
54 Settings.maxDimension = ret
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
55 ret
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
56 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
57 })
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
58 addOption(Option<File>("o", "output", true, "Output directory (default: ${Settings.outputTo})").apply {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
59 default = File(Settings.outputTo)
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
60 interpolater = {
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
61 val ret = File(tilde(it))
4
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
62 if (!ret.exists())
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
63 throw InvalidArgumentException("'${it}' does not exist")
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
64 if (!ret.isDirectory())
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
65 throw InvalidArgumentException("'${it}' is not a directory")
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
66 Settings.outputTo = ret.canonicalPath
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
67 ret
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
68 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
69 })
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
70 addOption(Option<Int>("q", "quality", true, "JPEG quality for output (0-100, default ${Settings.outputQuality})").apply {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
71 default = Settings.outputQuality
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
72 interpolater = {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
73 val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
74 if (ret < 0 || ret > 100)
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
75 throw InvalidArgumentException("Quality must be an integer from 0 to 100, inclusive.")
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
76 Settings.outputQuality = ret
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
77 ret
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
78 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
79 })
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
80 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
81 val cmdLine: CommandLine? = try {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
82 PromptingParser().parse(options, args)
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
83 } catch (e: ParseException) {
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
84 System.err.println("${Application.MYNAME}: Syntax error - ${e.message}")
4
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
85 System.exit(2)
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
86 null
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
87 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
88 if (cmdLine!!.hasOption("help")) {
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
89 val usage = Application.MYNAME + " [--maxdim=integer] [--output=directory] [--quality=integer] file [...]"
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
90 HelpFormatter().printHelp(usage, options, false);
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
91 System.exit(0);
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
92 }
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
93
5
884f1415a330 Rationalized directory management.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
94 /* launch GUI */
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
95 inSwingThread {
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
96 Application.initialize()
4
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
97 for (fileName in cmdLine!!.args)
5234e4500d45 Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents: 0
diff changeset
98 RotateDialog.makeDialog(File(fileName))
0
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
99 }
e0efe7848130 Initial commit. Untested!
David Barts <davidb@stashtea.com>
parents:
diff changeset
100 }