Mercurial > cgi-bin > hgweb.cgi > ImagePrep
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 |
rev | line source |
---|---|
0 | 1 /* |
2 * Entry point, etc. | |
3 */ | |
4 package name.blackcap.imageprep | |
5 | |
4
5234e4500d45
Command-line arguments (only partially tested).
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
6 import java.io.File |
0 | 7 import java.util.logging.Level |
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 | 17 |
18 object Application { | |
19 /* name we call ourselves */ | |
20 val MYNAME = "ImagePrep" | |
21 | |
22 /* global UI objects */ | |
23 var mainFrame: MainFrame by setOnce() | |
24 var helpDialog: HelpDialog by setOnce() | |
25 | |
26 fun initialize() { | |
27 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
28 mainFrame = MainFrame() /* must be created first */ | |
29 mainFrame.jMenuBar = MyMenuBar() | |
30 setMacMenus() /* always safe to call; no-op if not a Mac */ | |
31 helpDialog = HelpDialog() | |
32 mainFrame.setVisible(true) | |
33 } | |
34 } | |
35 | |
36 fun main(args: Array<String>) { | |
5
884f1415a330
Rationalized directory management.
David Barts <n5jrn@me.com>
parents:
4
diff
changeset
|
37 /* start up */ |
0 | 38 LOGGER.log(Level.INFO, "beginning execution") |
39 if (OS.type == OS.MAC) { | |
40 System.setProperty("apple.laf.useScreenMenuBar", "true") | |
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 | 95 inSwingThread { |
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 | 99 } |
100 } |