Skip to content

Commit

Permalink
Added display of implementation version (JAR only).
Browse files Browse the repository at this point in the history
  • Loading branch information
imagingbook committed Jan 27, 2024
1 parent c08218c commit e1b74fd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Renamed GoPro files can be **restored** to their original names if necessary.
This should open a GUI window for choosing the root directory of the video files, which includes
a few options to select. For example (on Win11):

![img.png](docs/images/renamer-gui-data.png)
![img.png](docs/images/renamer-gui-data-11.png)

* Click `Find` to select the start (root) directory of your GoPro files.
* Activate `Recursive` to traverse the directory tree recursively (otherwise only the start directory is processed).
Expand Down
Binary file modified assets/renamer.jar
Binary file not shown.
Binary file added docs/images/renamer-gui-data-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@
<configuration>
<archive>
<manifest>
<mainClass>
imagingbook.gopro.GoProFileRenamer
</mainClass>
<mainClass>imagingbook.gopro.GoProFileRenamer</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<!-- <Build-Date>${buildDate}</Build-Date> -->
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
Expand All @@ -94,6 +98,7 @@
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand Down
70 changes: 67 additions & 3 deletions src/main/java/imagingbook/gopro/GoProFileRenamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import java.io.FileFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.regex.Pattern;

import static javax.swing.GroupLayout.Alignment.BASELINE;
Expand Down Expand Up @@ -67,6 +70,8 @@
public class GoProFileRenamer extends JFrame {

private static final String appTitle = "GoPro File Renamer";
private static final String implVersion = getImplementationVersion();

private static final String helpUrl = "https://github.com/imagingbook/gopro-file-renamer?tab=readme-ov-file#gopro-file-renamer";
private static final Color renameButtonColor = Color.red.darker();
private static final Color revertButtonColor = Color.green.darker();
Expand All @@ -92,7 +97,6 @@ public class GoProFileRenamer extends JFrame {
private int renamedCount = 0;
private int errorCount = 0;


private final JLabel startDirLabel;
private final JTextField startDirField;
private final JCheckBox checkDryRun, checkRecursive, checkVerbose, checkAbsDirs;
Expand All @@ -102,7 +106,7 @@ public class GoProFileRenamer extends JFrame {


public GoProFileRenamer() {
super(appTitle);
super(appTitle + (implVersion != null ? " (" + implVersion + ")" : "")); // version number only shows when run from JAR
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Expand Down Expand Up @@ -211,6 +215,8 @@ public void actionPerformed(ActionEvent e) {
makeLayout();
pack();
setLocationRelativeTo(null);


}

private void makeLayout() {
Expand Down Expand Up @@ -292,7 +298,10 @@ private void updateSettings() {
}

private void log(String msg) {
this.outputArea.append(msg + "\n");
if (msg == null)
this.outputArea.append("null\n");
else
this.outputArea.append(msg + "\n");
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -572,4 +581,59 @@ String getFileRawName(String fileName) {
return fileName.substring(0, lastIndex);
}

//----------------------------------------------------------------------------

/**
* Finds the manifest (from META-INF/MANIFEST.MF) of the JAR file
* from which {@literal clazz} was loaded.
*
* See: http://stackoverflow.com/a/1273432
* @param clazz A class in the JAR file of interest.
* @return A {@link Manifest} object or {@literal null} if {@literal clazz}
* was not loaded from a JAR file.
*/
static Manifest getJarManifest(Class<?> clazz) {
String className = clazz.getSimpleName() + ".class";
String classPath = clazz.getResource(className).toString();
//IJ.log("classPath = " + classPath);
if (!classPath.startsWith("jar")) { // Class not from JAR
return null;
}
String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
Manifest manifest = null;
try {
manifest = new Manifest(new URL(manifestPath).openStream());
} catch (IOException ignore) { }
return manifest;
}

String getVersionInfo() {
Manifest mf = getJarManifest(this.getClass());
if (mf == null) {
return "UNKNOWN";
}
//IJ.log("listing attributes");
Attributes attr = mf.getMainAttributes();
String version = null;
String buildTime = null;
try {
version = attr.getValue("Implementation-Version");
buildTime = attr.getValue("Build-Time");
} catch (IllegalArgumentException e) { }
return version + " (" + buildTime + ")";
}

static String getImplementationVersion() {
Manifest mf = getJarManifest(GoProFileRenamer.class);
if (mf == null) {
return null;
}
Attributes attr = mf.getMainAttributes();
String version = null;
try {
version = attr.getValue("Implementation-Version");
} catch (IllegalArgumentException e) { }
return version;
}

}

0 comments on commit e1b74fd

Please sign in to comment.