Skip to content

Commit

Permalink
Merge pull request #9 from dennisfabri/master
Browse files Browse the repository at this point in the history
Fixed NullPointerException & API-Change
  • Loading branch information
steos authored Apr 5, 2020
2 parents efb0974 + a5430da commit 6f0eb6a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
target/
target/
.project
.settings
.classpath
24 changes: 14 additions & 10 deletions api/src/main/java/jnafilechooser/api/JnaFileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
Expand Down Expand Up @@ -83,8 +84,10 @@ public JnaFileChooser() {
*/
public JnaFileChooser(File currentDirectory) {
this();
this.currentDirectory = currentDirectory.isDirectory() ?
currentDirectory : currentDirectory.getParentFile();
if (currentDirectory != null) {
this.currentDirectory = currentDirectory.isDirectory() ?
currentDirectory : currentDirectory.getParentFile();
}
}

/**
Expand Down Expand Up @@ -215,17 +218,18 @@ private boolean showWindowsFolderBrowser(Window parent) {
/**
* add a filter to the user-selectable list of file filters
*
* @param filter you must pass at least 2 arguments, the first argument
* is the name of this filter and the remaining arguments
* are the file extensions.
*
* @throws IllegalArgumentException if less than 2 arguments are passed
* @param name name of the filter
* @param filter you must pass at least 1 argument, the arguments are the file
* extensions.
*/
public void addFilter(String ... values) {
if (values.length < 2) {
public void addFilter(String name, String... filter) {
if (filter.length < 1) {
throw new IllegalArgumentException();
}
filters.add(values);
ArrayList<String> parts = new ArrayList<String>();
parts.add(name);
Collections.addAll(parts, filter);
filters.add(parts.toArray(new String[parts.size()]));
}

/**
Expand Down
18 changes: 11 additions & 7 deletions api/src/main/java/jnafilechooser/api/WindowsFileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.awt.Window;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import jnafilechooser.win32.Comdlg32;

Expand Down Expand Up @@ -121,15 +122,18 @@ void setFilters(ArrayList<String[]> filters) {
/**
* add a filter to the user-selectable list of file filters
*
* @param filter you must pass at least 2 arguments, the first argument
* is the name of this filter and the remaining arguments
* @param name name of the filter
* @param filter you must pass at least 1 argument, the arguments
* are the file extensions.
*/
public void addFilter(String ... filter) {
if (filter.length < 2) {
public void addFilter(String name, String... filter) {
if (filter.length < 1) {
throw new IllegalArgumentException();
}
filters.add(filter);
ArrayList<String> parts = new ArrayList<String>();
parts.add(name);
Collections.addAll(parts, filter);
filters.add(parts.toArray(new String[parts.size()]));
}

/**
Expand Down Expand Up @@ -176,7 +180,7 @@ boolean showDialog(Window parent, boolean open) {
// enable resizing of the dialog
| Comdlg32.OFN_ENABLESIZING;

params.hwndOwner = Native.getWindowPointer(parent);
params.hwndOwner = parent == null ? null : Native.getWindowPointer(parent);

// lpstrFile contains the selection path after the dialog
// returns. It must be big enough for the path to fit or
Expand Down Expand Up @@ -216,7 +220,7 @@ boolean showDialog(Window parent, boolean open) {
Comdlg32.GetSaveFileNameW(params);

if (approved) {
final String filePath = params.lpstrFile.getString(0, true);
final String filePath = params.lpstrFile.getWideString(0);
selectedFile = new File(filePath);
final File dir = selectedFile.getParentFile();
currentDirectory = dir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public File showDialog(Window parent) {
// be more than big enough
final Pointer path = new Memory(1024 * 4);
Shell32.SHGetPathFromIDListW(pidl, path);
final String filePath = path.getString(0, true);
final String filePath = path.getWideString(0);
final File file = new File(filePath);
Ole32.CoTaskMemFree(pidl);
return file;
Expand Down
2 changes: 1 addition & 1 deletion demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout</artifactId>
<version>3.7.3.1</version>
<version>3.7.4</version>
<type>jar</type>
<classifier>swing</classifier>
<scope>compile</scope>
Expand Down
4 changes: 2 additions & 2 deletions demo/src/main/java/jnafilechooser/demo/PlumbingDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void actionPerformed(ActionEvent e) {
final Pointer path = new Memory(260);
path.clear(260);
SHGetPathFromIDListW(pidl, path);
final String pathStr = path.getString(0, true);
final String pathStr = path.getWideString(0);
System.out.println(pathStr);
}
CoTaskMemFree(pidl);
Expand All @@ -74,7 +74,7 @@ public void actionPerformed(ActionEvent e) {
| OFN_NOCHANGEDIR | OFN_HIDEREADONLY;

if (GetOpenFileNameW(params)) {
System.out.println(params.lpstrFile.getString(0, true));
System.out.println(params.lpstrFile.getWideString(0));
}
else {
int err = CommDlgExtendedError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static void main(String[] args) throws Exception {
}

final JCheckBox enableMultiSelect = new JCheckBox();
final JComboBox selectionMode = new JComboBox(JnaFileChooser.Mode.values());
final JComboBox dialogType = new JComboBox(new String[] { "Open", "Save" });
final JComboBox<JnaFileChooser.Mode> selectionMode = new JComboBox<JnaFileChooser.Mode>(JnaFileChooser.Mode.values());
final JComboBox<String> dialogType = new JComboBox<String>(new String[] { "Open", "Save" });
final JCheckBox useFilter = new JCheckBox();
final JButton choose = new JButton("Choose");
final JFrame frame = new JFrame(PortablePorcelainDemo.class.getName());
Expand Down
2 changes: 1 addition & 1 deletion win32/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.1</version>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down

0 comments on commit 6f0eb6a

Please sign in to comment.