mirror of
https://gitlab.com/annimon/imagetagger.git
synced 2024-09-19 14:34:21 +03:00
Initial commit
This commit is contained in:
commit
385f6d7c13
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.gradle
|
||||
.idea
|
||||
build
|
22
build.gradle
Normal file
22
build.gradle
Normal file
@ -0,0 +1,22 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'application'
|
||||
id "com.github.johnrengelman.shadow" version "5.2.0"
|
||||
}
|
||||
|
||||
group 'com.annimon'
|
||||
version '1.0-SNAPSHOT'
|
||||
mainClassName = 'com.annimon.imagetagger.Main'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
ext.jacksonVersion = '2.12.0'
|
||||
|
||||
dependencies {
|
||||
implementation "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
|
||||
implementation "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"
|
||||
implementation "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
|
||||
testImplementation 'junit:junit:4.13.1'
|
||||
}
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
183
gradlew
vendored
Normal file
183
gradlew
vendored
Normal file
@ -0,0 +1,183 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
case $i in
|
||||
0) set -- ;;
|
||||
1) set -- "$args0" ;;
|
||||
2) set -- "$args0" "$args1" ;;
|
||||
3) set -- "$args0" "$args1" "$args2" ;;
|
||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=`save "$@"`
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
exec "$JAVACMD" "$@"
|
100
gradlew.bat
vendored
Normal file
100
gradlew.bat
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
2
settings.gradle
Normal file
2
settings.gradle
Normal file
@ -0,0 +1,2 @@
|
||||
rootProject.name = 'imagetagger'
|
||||
|
60
src/main/java/com/annimon/imagetagger/Main.java
Normal file
60
src/main/java/com/annimon/imagetagger/Main.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.annimon.imagetagger;
|
||||
|
||||
import com.annimon.imagetagger.beans.Config;
|
||||
import com.annimon.imagetagger.logic.ImageProcessor;
|
||||
import com.annimon.imagetagger.logic.KeyProcessor;
|
||||
import com.annimon.imagetagger.views.ImagePanel;
|
||||
import com.annimon.imagetagger.views.TagPanel;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.swing.*;
|
||||
|
||||
public class Main extends JFrame {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
final var file = new File("imagetagger.json");
|
||||
final var mapper = new ObjectMapper();
|
||||
final var config = mapper.readValue(file, Config.class);
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("validate")) {
|
||||
config.validate();
|
||||
return;
|
||||
}
|
||||
|
||||
var main = new Main(config);
|
||||
main.setVisible(true);
|
||||
}
|
||||
|
||||
public Main(Config config) {
|
||||
super("ImageTagger");
|
||||
|
||||
final var tagButtons = config.getButtons(config.getProfile());
|
||||
final var tagPanel = new TagPanel(tagButtons);
|
||||
final var imageProcessor = new ImageProcessor(config.getDir(), config.getFilter(), config.getSort());
|
||||
final var imagePanel = new ImagePanel(imageProcessor);
|
||||
final var keyProcessor = new KeyProcessor(tagButtons);
|
||||
keyProcessor.setTagPanel(tagPanel);
|
||||
keyProcessor.setImagePanel(imagePanel);
|
||||
keyProcessor.setImageProcessor(imageProcessor);
|
||||
imageProcessor.populateFiles();
|
||||
tagPanel.enableSupportedTags(imageProcessor.getTags());
|
||||
|
||||
setPreferredSize(new Dimension(800, 600));
|
||||
setLayout(new BorderLayout());
|
||||
add(tagPanel, BorderLayout.SOUTH);
|
||||
add(imagePanel, BorderLayout.CENTER);
|
||||
addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
keyProcessor.keyPressed(e);
|
||||
}
|
||||
});
|
||||
setResizable(true);
|
||||
setLocationByPlatform(true);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
pack();
|
||||
}
|
||||
}
|
75
src/main/java/com/annimon/imagetagger/beans/Config.java
Normal file
75
src/main/java/com/annimon/imagetagger/beans/Config.java
Normal file
@ -0,0 +1,75 @@
|
||||
package com.annimon.imagetagger.beans;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Config {
|
||||
|
||||
private String dir;
|
||||
private String filter;
|
||||
private String sort;
|
||||
private String profile;
|
||||
private Map<String, List<TagButton>> tags;
|
||||
|
||||
public Config() {
|
||||
}
|
||||
|
||||
public String getDir() {
|
||||
return dir;
|
||||
}
|
||||
|
||||
public String getFilter() {
|
||||
return Objects.requireNonNullElse(filter, "");
|
||||
}
|
||||
|
||||
public String getSort() {
|
||||
return Objects.requireNonNullElse(sort, "");
|
||||
}
|
||||
|
||||
public String getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
public Map<String, List<TagButton>> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public List<TagButton> getButtons(String profile) {
|
||||
return tags.get(profile);
|
||||
}
|
||||
|
||||
public void validate() {
|
||||
final var errors = new HashSet<String>();
|
||||
final var uniqueTags = new HashMap<String, String>();
|
||||
final var uniqueKeys = new HashSet<String>();
|
||||
for (var entry : tags.entrySet()) {
|
||||
final var profile = entry.getKey();
|
||||
final var tagButtons = entry.getValue();
|
||||
|
||||
uniqueKeys.clear();
|
||||
for (var tb : tagButtons) {
|
||||
// Detect duplicate keys in profile
|
||||
boolean isUnique = uniqueKeys.add(tb.getKey());
|
||||
if (!isUnique) {
|
||||
errors.add(String.format("Duplicate key %s in profile %s", tb.getKey(), profile));
|
||||
}
|
||||
|
||||
// Detect duplicate tags globally
|
||||
String previousProfile = uniqueTags.put(tb.getTag(), profile);
|
||||
if (previousProfile != null) {
|
||||
errors.add(String.format("Tag %s in profile %s was already defined in %s", tb.getTag(), profile, previousProfile));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (errors.isEmpty()) {
|
||||
System.out.println("All checks passed");
|
||||
} else {
|
||||
errors.forEach(System.err::println);
|
||||
}
|
||||
}
|
||||
}
|
70
src/main/java/com/annimon/imagetagger/beans/ImageInfo.java
Normal file
70
src/main/java/com/annimon/imagetagger/beans/ImageInfo.java
Normal file
@ -0,0 +1,70 @@
|
||||
package com.annimon.imagetagger.beans;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ImageInfo {
|
||||
private static final String PREFIX = "-IPTC:keywords=";
|
||||
|
||||
private final File file;
|
||||
private final Set<String> tags;
|
||||
|
||||
public ImageInfo(File file) {
|
||||
this.file = file;
|
||||
tags = new HashSet<>();
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public int getTagsCount() {
|
||||
return tags.size();
|
||||
}
|
||||
|
||||
public Set<String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Collection<String> newTags) {
|
||||
tags.clear();
|
||||
tags.addAll(newTags);
|
||||
}
|
||||
|
||||
public ImageInfo loadTagFile() {
|
||||
final var tagFilePath = tagFilePath();
|
||||
if (Files.exists(tagFilePath)) {
|
||||
try {
|
||||
final var lines = Files.lines(tagFilePath)
|
||||
.map(s -> s.replace(PREFIX, ""))
|
||||
.collect(Collectors.toList());
|
||||
setTags(lines);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void writeTagFile() {
|
||||
final var lines = tags.stream()
|
||||
.sorted()
|
||||
.map(s -> PREFIX + s)
|
||||
.collect(Collectors.toList());
|
||||
try {
|
||||
Files.write(tagFilePath(), lines);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Path tagFilePath() {
|
||||
return Path.of(file.getParent(), file.getName() + ".txt");
|
||||
}
|
||||
}
|
26
src/main/java/com/annimon/imagetagger/beans/TagButton.java
Normal file
26
src/main/java/com/annimon/imagetagger/beans/TagButton.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.annimon.imagetagger.beans;
|
||||
|
||||
public class TagButton {
|
||||
|
||||
private String key;
|
||||
private String tag;
|
||||
|
||||
public TagButton() {
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void setTag(String tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.annimon.imagetagger.beans;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class TagButtonHolder {
|
||||
|
||||
private final TagButton tagButton;
|
||||
private final JLabel label;
|
||||
|
||||
public TagButtonHolder(TagButton tagButton, JLabel label) {
|
||||
this.tagButton = tagButton;
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public TagButton getTagButton() {
|
||||
return tagButton;
|
||||
}
|
||||
|
||||
public JLabel getLabel() {
|
||||
return label;
|
||||
}
|
||||
}
|
156
src/main/java/com/annimon/imagetagger/logic/ImageProcessor.java
Normal file
156
src/main/java/com/annimon/imagetagger/logic/ImageProcessor.java
Normal file
@ -0,0 +1,156 @@
|
||||
package com.annimon.imagetagger.logic;
|
||||
|
||||
import com.annimon.imagetagger.beans.ImageInfo;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class ImageProcessor {
|
||||
|
||||
private final File dir;
|
||||
private final String filter;
|
||||
private final String sort;
|
||||
private final List<ImageInfo> imageInfos;
|
||||
private int imagesCount;
|
||||
private int index;
|
||||
private ImageInfo currentInfo;
|
||||
private Image image;
|
||||
|
||||
public ImageProcessor(String dir, String filter, String sort) {
|
||||
this.dir = new File(dir);
|
||||
this.filter = filter;
|
||||
this.sort = sort;
|
||||
imageInfos = new ArrayList<>();
|
||||
imagesCount = 0;
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public String getFilename() {
|
||||
if (currentInfo != null) {
|
||||
return currentInfo.getFile().getName();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getIndexInfo() {
|
||||
int tagsCount = (currentInfo == null) ? 0 : currentInfo.getTags().size();
|
||||
return String.format("%d / %d, %d tags",
|
||||
index + 1, imagesCount, tagsCount);
|
||||
}
|
||||
|
||||
public void prevImage() {
|
||||
index--;
|
||||
if (index < 0) {
|
||||
index = imagesCount - 1;
|
||||
}
|
||||
loadImage();
|
||||
}
|
||||
|
||||
public void nextImage() {
|
||||
index++;
|
||||
if (index >= imagesCount) {
|
||||
index = 0;
|
||||
}
|
||||
loadImage();
|
||||
}
|
||||
|
||||
public void writeTagsToFile() {
|
||||
if (currentInfo != null) {
|
||||
currentInfo.writeTagFile();
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> getTags() {
|
||||
if (currentInfo != null) {
|
||||
return currentInfo.getTags();
|
||||
}
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
public void mergeTags(Set<String> tagsToAdd, Set<String> tagsToRemove) {
|
||||
if (currentInfo != null) {
|
||||
final var tags = new HashSet<>(currentInfo.getTags());
|
||||
tags.removeAll(tagsToRemove);
|
||||
tags.addAll(tagsToAdd);
|
||||
currentInfo.setTags(tags);
|
||||
}
|
||||
}
|
||||
|
||||
public void populateFiles() {
|
||||
final var files = dir.listFiles();
|
||||
if (files == null) {
|
||||
throw new RuntimeException("There are no files in directory " + dir);
|
||||
}
|
||||
final var infos = Arrays.stream(files)
|
||||
.filter(this::allowedFiles)
|
||||
.map(ImageInfo::new)
|
||||
.map(ImageInfo::loadTagFile)
|
||||
.filter(this::filterTags)
|
||||
.collect(Collectors.toList());
|
||||
sort(infos);
|
||||
imageInfos.clear();
|
||||
imageInfos.addAll(infos);
|
||||
imagesCount = imageInfos.size();
|
||||
if (imagesCount > 0) {
|
||||
index = 0;
|
||||
loadImage();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadImage() {
|
||||
if (index < 0 || index >= imagesCount) return;
|
||||
|
||||
currentInfo = imageInfos.get(index);
|
||||
try {
|
||||
image = ImageIO.read(currentInfo.getFile());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean allowedFiles(File file) {
|
||||
final String filename = file.getName().toLowerCase();
|
||||
return Stream.of(".jpg", ".png", ".jpeg", ".gif")
|
||||
.anyMatch(filename::endsWith);
|
||||
}
|
||||
|
||||
private boolean filterTags(ImageInfo info) {
|
||||
if (filter.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return Arrays.stream(filter.split(","))
|
||||
.map(String::trim)
|
||||
.filter(s -> s.startsWith("+") || s.startsWith("-"))
|
||||
.map(String::toLowerCase)
|
||||
.allMatch(s -> {
|
||||
final String tag = s.substring(1);
|
||||
final boolean hasTag = info.getTags().contains(tag);
|
||||
return s.startsWith("-") ? !hasTag : hasTag;
|
||||
});
|
||||
}
|
||||
|
||||
private void sort(List<ImageInfo> infos) {
|
||||
if (sort.contains("name")) {
|
||||
infos.sort(Comparator.comparing(ImageInfo::getFile));
|
||||
}
|
||||
if (sort.contains("tags.count")) {
|
||||
infos.sort(Comparator.comparingInt(ImageInfo::getTagsCount));
|
||||
}
|
||||
if (sort.contains("random")) {
|
||||
Collections.shuffle(infos);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.annimon.imagetagger.logic;
|
||||
|
||||
import com.annimon.imagetagger.beans.TagButton;
|
||||
import com.annimon.imagetagger.views.ImagePanel;
|
||||
import com.annimon.imagetagger.views.TagPanel;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class KeyProcessor {
|
||||
|
||||
private final Set<String> keys;
|
||||
private TagPanel tagPanel;
|
||||
private ImagePanel imagePanel;
|
||||
private ImageProcessor imageProcessor;
|
||||
|
||||
public KeyProcessor(List<TagButton> buttons) {
|
||||
keys = buttons.stream()
|
||||
.map(TagButton::getKey)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public void setTagPanel(TagPanel tagPanel) {
|
||||
this.tagPanel = tagPanel;
|
||||
}
|
||||
|
||||
public void setImagePanel(ImagePanel imagePanel) {
|
||||
this.imagePanel = imagePanel;
|
||||
}
|
||||
|
||||
public void setImageProcessor(ImageProcessor imageProcessor) {
|
||||
this.imageProcessor = imageProcessor;
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_LEFT:
|
||||
imageProcessor.mergeTags(tagPanel.getEnabledTags(), tagPanel.getDisabledTags());
|
||||
imageProcessor.prevImage();
|
||||
imagePanel.repaint();
|
||||
tagPanel.enableSupportedTags(imageProcessor.getTags());
|
||||
tagPanel.repaint();
|
||||
break;
|
||||
case KeyEvent.VK_RIGHT:
|
||||
imageProcessor.mergeTags(tagPanel.getEnabledTags(), tagPanel.getDisabledTags());
|
||||
imageProcessor.nextImage();
|
||||
imagePanel.repaint();
|
||||
tagPanel.enableSupportedTags(imageProcessor.getTags());
|
||||
tagPanel.repaint();
|
||||
break;
|
||||
case KeyEvent.VK_ENTER:
|
||||
imageProcessor.mergeTags(tagPanel.getEnabledTags(), tagPanel.getDisabledTags());
|
||||
imageProcessor.writeTagsToFile();
|
||||
break;
|
||||
default:
|
||||
final var key = String.valueOf(e.getKeyChar());
|
||||
if (keys.contains(key)) {
|
||||
tagPanel.toggle(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
66
src/main/java/com/annimon/imagetagger/views/ImagePanel.java
Normal file
66
src/main/java/com/annimon/imagetagger/views/ImagePanel.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.annimon.imagetagger.views;
|
||||
|
||||
import com.annimon.imagetagger.logic.ImageProcessor;
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class ImagePanel extends JPanel {
|
||||
|
||||
private static final Color TEXT_COLOR = new Color(0x80FFFFFF, true);
|
||||
private final ImageProcessor imageProcessor;
|
||||
|
||||
public ImagePanel(ImageProcessor imageProcessor) {
|
||||
this.imageProcessor = imageProcessor;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedAssignment")
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
final var g2d = (Graphics2D) g;
|
||||
final int width = getWidth();
|
||||
final int height = getHeight();
|
||||
var image = imageProcessor.getImage();
|
||||
if ((width <= 5) || (height <= 5) || (image == null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int imgWidth = image.getWidth(this);
|
||||
final int imgHeight = image.getHeight(this);
|
||||
int scaleX = imgWidth;
|
||||
int scaleY = imgHeight;
|
||||
if (imgWidth > width || imgHeight > height) {
|
||||
if (scaleX > width) {
|
||||
// Fit by width
|
||||
scaleX = width;
|
||||
scaleY = imgHeight * scaleX / imgWidth;
|
||||
}
|
||||
if (scaleY > height) {
|
||||
// Fit by height
|
||||
scaleY = height;
|
||||
scaleX = imgWidth * scaleY / imgHeight;
|
||||
}
|
||||
image = image.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
|
||||
}
|
||||
|
||||
final int x = (width - scaleX) / 2;
|
||||
final int y = (height - scaleY) / 2;
|
||||
g2d.drawImage(image, x, y, this);
|
||||
|
||||
int yy = 5;
|
||||
yy = drawString(g2d, imageProcessor.getFilename(), 5, yy);
|
||||
yy = drawString(g2d, imageProcessor.getIndexInfo(), 5, yy);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
private int drawString(Graphics2D g2d, String filename, int x, int y) {
|
||||
final var bounds = g2d.getFontMetrics().getStringBounds(filename, g2d);
|
||||
final var strWidth = (int) bounds.getWidth();
|
||||
final var strHeight = (int) bounds.getHeight();
|
||||
g2d.setColor(TEXT_COLOR);
|
||||
g2d.fillRect(x, y, strWidth + 6, strHeight + 6);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawString(filename, x + 2, y + strHeight + 1);
|
||||
return y + strHeight + 6;
|
||||
}
|
||||
}
|
84
src/main/java/com/annimon/imagetagger/views/TagPanel.java
Normal file
84
src/main/java/com/annimon/imagetagger/views/TagPanel.java
Normal file
@ -0,0 +1,84 @@
|
||||
package com.annimon.imagetagger.views;
|
||||
|
||||
import com.annimon.imagetagger.beans.TagButton;
|
||||
import com.annimon.imagetagger.beans.TagButtonHolder;
|
||||
import java.awt.*;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.swing.*;
|
||||
|
||||
public class TagPanel extends JPanel {
|
||||
|
||||
private static final int MAX_COLORS = 8;
|
||||
|
||||
private final Map<String, TagButtonHolder> tagButtons;
|
||||
|
||||
public TagPanel(List<TagButton> buttons) {
|
||||
tagButtons = new LinkedHashMap<>();
|
||||
|
||||
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
|
||||
for (TagButton button : buttons) {
|
||||
final var key = button.getKey();
|
||||
final var label = new JLabel(key + ": " + button.getTag());
|
||||
label.setBorder(BorderFactory.createEmptyBorder(3, 6, 3, 6));
|
||||
label.setOpaque(true);
|
||||
label.setEnabled(false);
|
||||
setBackground(label, button);
|
||||
add(label);
|
||||
tagButtons.put(key, new TagButtonHolder(button, label));
|
||||
}
|
||||
}
|
||||
|
||||
private void setBackground(JLabel label, TagButton button) {
|
||||
final boolean enabled = label.isEnabled();
|
||||
final float hue = (button.getTag().hashCode() % MAX_COLORS) * (1f / MAX_COLORS);
|
||||
final var color = Color.getHSBColor(hue, enabled ? 0.99f : 0.6f, enabled ? 0.95f : 0.28f);
|
||||
label.setBackground(color);
|
||||
}
|
||||
|
||||
public void toggle(String key) {
|
||||
final var holder = tagButtons.get(key);
|
||||
if (holder != null) {
|
||||
final var label = holder.getLabel();
|
||||
label.setEnabled(!label.isEnabled());
|
||||
setBackground(label, holder.getTagButton());
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> getAllTags() {
|
||||
return filterTags(h -> true);
|
||||
}
|
||||
|
||||
public Set<String> getEnabledTags() {
|
||||
return filterTags(enabledLabelPredicate());
|
||||
}
|
||||
|
||||
public Set<String> getDisabledTags() {
|
||||
return filterTags(enabledLabelPredicate().negate());
|
||||
}
|
||||
|
||||
private Predicate<TagButtonHolder> enabledLabelPredicate() {
|
||||
return h -> h.getLabel().isEnabled();
|
||||
}
|
||||
|
||||
private Set<String> filterTags(Predicate<TagButtonHolder> predicate) {
|
||||
return tagButtons.values().stream()
|
||||
.filter(predicate)
|
||||
.map(h -> h.getTagButton().getTag())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public void enableSupportedTags(Set<String> tags) {
|
||||
tagButtons.forEach((key, holder) -> {
|
||||
final var label = holder.getLabel();
|
||||
final var tagButton = holder.getTagButton();
|
||||
final var containsTag = tags.contains(tagButton.getTag());
|
||||
label.setEnabled(containsTag);
|
||||
setBackground(label, tagButton);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user