file chooser implemented with default directory location in windows.
This commit is contained in:
+61
-11
@@ -3,10 +3,11 @@ import java.io.File;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JComboBox;
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
|
|
||||||
|
import gui.ComboBox;
|
||||||
|
import gui.FileChooser;
|
||||||
/**
|
/**
|
||||||
* The Nav class represents the Navigation bar at the top of the JFrame window.
|
* The Nav class represents the Navigation bar at the top of the JFrame window.
|
||||||
*
|
*
|
||||||
@@ -85,26 +86,52 @@ public class Nav extends JPanel {
|
|||||||
* debugging purposes.
|
* debugging purposes.
|
||||||
*/
|
*/
|
||||||
private void createGUI() {
|
private void createGUI() {
|
||||||
JComboBox<String> fileOptions = new JComboBox<String>();
|
|
||||||
fileOptions.addItem("New");
|
|
||||||
fileOptions.addItem("Open");
|
|
||||||
fileOptions.addItem("Save");
|
|
||||||
fileOptions.addItem("Exit");
|
|
||||||
|
|
||||||
JLabel elapsedTime = new JLabel("Elapsed Time: 00:00:00");
|
JLabel elapsedTime = new JLabel("Elapsed Time: 00:00:00");
|
||||||
|
|
||||||
JButton solve = new JButton("Solve");
|
JButton solve = new JButton("Solve");
|
||||||
|
|
||||||
add(fileOptions);
|
add(createFileOptions());
|
||||||
add(elapsedTime);
|
add(elapsedTime);
|
||||||
add(solve);
|
add(solve);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the ComboBox for the file options.
|
||||||
|
*
|
||||||
|
* @return ComboBox<String>
|
||||||
|
*/
|
||||||
|
private ComboBox<String> createFileOptions() {
|
||||||
|
ComboBox<String> fileOptions = new ComboBox<>();
|
||||||
|
fileOptions.addItem("New");
|
||||||
|
fileOptions.addItem("Open");
|
||||||
|
fileOptions.addItem("Save");
|
||||||
|
fileOptions.addItem("Exit");
|
||||||
|
|
||||||
|
fileOptions.addActionListener(e -> {
|
||||||
|
String selected = (String) fileOptions.getSelectedItem();
|
||||||
|
switch(selected) {
|
||||||
|
case "New":
|
||||||
|
newFile();
|
||||||
|
break;
|
||||||
|
case "Open":
|
||||||
|
openFile();
|
||||||
|
break;
|
||||||
|
case "Save":
|
||||||
|
saveFile();
|
||||||
|
break;
|
||||||
|
case "Exit":
|
||||||
|
System.exit(0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Invalid selection.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return fileOptions;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given the user's selection from the JComboBox, perform the appropriate
|
* Given the user's selection from the JComboBox, perform the appropriate
|
||||||
* action.
|
* action.
|
||||||
*
|
|
||||||
* @param String
|
|
||||||
*/
|
*/
|
||||||
private void newFile() {
|
private void newFile() {
|
||||||
int defaultOption = s.getNewFileProperties();
|
int defaultOption = s.getNewFileProperties();
|
||||||
@@ -120,8 +147,30 @@ public class Nav extends JPanel {
|
|||||||
// TODO: Create a new .sdku file in the default directory.
|
// TODO: Create a new .sdku file in the default directory.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open an existing .sdku file in the user's file system, using a GUI.
|
||||||
|
*/
|
||||||
|
private void openFile() {
|
||||||
|
File defaultDirectory = new File(s.getDefaultDirectory());
|
||||||
|
if(defaultDirectory != null || !defaultDirectory.isDirectory()) {
|
||||||
|
defaultDirectory.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
FileChooser fc = new FileChooser(defaultDirectory);
|
||||||
|
int result = fc.showOpenDialog(this);
|
||||||
|
|
||||||
|
if(result == FileChooser.APPROVE_OPTION) {
|
||||||
|
f = fc.getSelectedFile();
|
||||||
|
createGrid(f);
|
||||||
|
} else {
|
||||||
|
System.out.println("No file was selected.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given an input filename, open the file and populate the grid.
|
* Given an input filename, open the file and populate the grid.
|
||||||
|
*
|
||||||
|
* This implementation is for use only in the command-line interface.
|
||||||
*
|
*
|
||||||
* @param String
|
* @param String
|
||||||
*/
|
*/
|
||||||
@@ -140,6 +189,7 @@ public class Nav extends JPanel {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("Opening the file " + filename + ".sdku.");
|
||||||
createGrid(f);
|
createGrid(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ public class Settings {
|
|||||||
// settings, effectively toggles. If the file does not exist,
|
// settings, effectively toggles. If the file does not exist,
|
||||||
// populate it with the following values (possibly in .json).
|
// populate it with the following values (possibly in .json).
|
||||||
newFileProperties = 0;
|
newFileProperties = 0;
|
||||||
defaultDirectory = "~/AppData/Local/sudoku/";
|
defaultDirectory = "C:\\Users\\jashton\\AppData\\Local\\Sudoku"; // TODO: This is a Windows default. Need OS detection.
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import javax.swing.JComboBox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A custom JComboBox with the appropriate styling for the Sudoku app.
|
||||||
|
*/
|
||||||
|
public class ComboBox<E> extends JComboBox<E> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new ComboBox with the default styling and without initial items.
|
||||||
|
*/
|
||||||
|
public ComboBox() {
|
||||||
|
super();
|
||||||
|
style();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the ComboBox with the appropriate styling.
|
||||||
|
*/
|
||||||
|
private void style() {
|
||||||
|
// TODO: Style the ComboBox.
|
||||||
|
System.out.println("TODO: Style the ComboBox.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import javax.swing.filechooser.FileFilter;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A custom FileChooser with the appropriate styling for the Sudoku app.
|
||||||
|
* Additionally, this FileChooser only allows .sdku files to be selected.
|
||||||
|
*/
|
||||||
|
public class FileChooser extends JFileChooser {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new FileChooser with the default directory set to the user's
|
||||||
|
* Sudoku directory.
|
||||||
|
*
|
||||||
|
* @param defaultDirectory
|
||||||
|
*/
|
||||||
|
public FileChooser(File defaultDirectory) {
|
||||||
|
super(defaultDirectory);
|
||||||
|
for (File f: defaultDirectory.listFiles()) {
|
||||||
|
ensureFileIsVisible(f);
|
||||||
|
}
|
||||||
|
style();
|
||||||
|
setupSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the FileChooser with the appropriate styling.
|
||||||
|
*/
|
||||||
|
private void style() {
|
||||||
|
// TODO: Style the FileChooser.
|
||||||
|
System.out.println("TODO: Style the FileChooser.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the FileChooser with the appropriate settings.
|
||||||
|
*/
|
||||||
|
private void setupSettings() {
|
||||||
|
setFileHidingEnabled(false);
|
||||||
|
setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||||
|
rescanCurrentDirectory();
|
||||||
|
setFileFilter(new Filter());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A custom FileFilter to only allow .sdku files to be selected.
|
||||||
|
*/
|
||||||
|
private class Filter extends FileFilter {
|
||||||
|
public boolean accept(File f) {
|
||||||
|
String name = f.getName().toLowerCase();
|
||||||
|
if(name.length() > 5) {
|
||||||
|
return name.substring(name.length() - 5).equals(".sdku") || !f.isDirectory();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return "Sudoku Puzzle Files (*.sdku)";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ function Run {
|
|||||||
javac *.java
|
javac *.java
|
||||||
java App $flag
|
java App $flag
|
||||||
rm *.class
|
rm *.class
|
||||||
|
rm gui/*.class
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($args.Length -eq 1) {
|
if ($args.Length -eq 1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user