未验证 提交 75d5603b 编写于 作者: D Deathmarine 提交者: GitHub

Merge pull request #149 from refactormachine/feature/refactored-file-dialog

Refactored the file dialog preferences
package us.deathmarine.luyten;
import javax.swing.*;
import java.io.File;
class DirPreferences {
private LuytenPreferences luytenPrefs;
public DirPreferences(LuytenPreferences luytenPrefs) {
this.luytenPrefs = luytenPrefs;
}
void retrieveOpenDialogDir(JFileChooser fc) {
try {
String currentDirStr = luytenPrefs.getFileOpenCurrentDirectory();
if (currentDirStr != null && currentDirStr.trim().length() > 0) {
File currentDir = new File(currentDirStr);
if (currentDir.exists() && currentDir.isDirectory()) {
fc.setCurrentDirectory(currentDir);
}
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
void saveOpenDialogDir(JFileChooser fc) {
try {
File currentDir = fc.getCurrentDirectory();
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
luytenPrefs.setFileOpenCurrentDirectory(currentDir.getAbsolutePath());
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
void retrieveSaveDialogDir(JFileChooser fc) {
try {
String currentDirStr = luytenPrefs.getFileSaveCurrentDirectory();
if (currentDirStr != null && currentDirStr.trim().length() > 0) {
File currentDir = new File(currentDirStr);
if (currentDir.exists() && currentDir.isDirectory()) {
fc.setCurrentDirectory(currentDir);
}
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
void saveSaveDialogDir(JFileChooser fc) {
try {
File currentDir = fc.getCurrentDirectory();
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
luytenPrefs.setFileSaveCurrentDirectory(currentDir.getAbsolutePath());
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
}
\ No newline at end of file
...@@ -9,9 +9,9 @@ import javax.swing.filechooser.FileFilter; ...@@ -9,9 +9,9 @@ import javax.swing.filechooser.FileFilter;
* FileChoosers for Open and Save * FileChoosers for Open and Save
*/ */
public class FileDialog { public class FileDialog {
private ConfigSaver configSaver; private final DirPreferences dirPreferences;
private LuytenPreferences luytenPrefs; private ConfigSaver configSaver;
private Component parent; private Component parent;
private JFileChooser fcOpen; private JFileChooser fcOpen;
private JFileChooser fcSave; private JFileChooser fcSave;
private JFileChooser fcSaveAll; private JFileChooser fcSaveAll;
...@@ -19,9 +19,10 @@ public class FileDialog { ...@@ -19,9 +19,10 @@ public class FileDialog {
public FileDialog(Component parent) { public FileDialog(Component parent) {
this.parent = parent; this.parent = parent;
configSaver = ConfigSaver.getLoadedInstance(); configSaver = ConfigSaver.getLoadedInstance();
luytenPrefs = configSaver.getLuytenPreferences(); LuytenPreferences luytenPrefs = configSaver.getLuytenPreferences();
dirPreferences = new DirPreferences(luytenPrefs);
new Thread() { new Thread() {
public void run() { public void run() {
try { try {
initOpenDialog(); initOpenDialog();
...@@ -40,9 +41,9 @@ public class FileDialog { ...@@ -40,9 +41,9 @@ public class FileDialog {
File selectedFile = null; File selectedFile = null;
initOpenDialog(); initOpenDialog();
retrieveOpenDialogDir(fcOpen); dirPreferences.retrieveOpenDialogDir(fcOpen);
int returnVal = fcOpen.showOpenDialog(parent); int returnVal = fcOpen.showOpenDialog(parent);
saveOpenDialogDir(fcOpen); dirPreferences.saveOpenDialogDir(fcOpen);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFile = fcOpen.getSelectedFile(); selectedFile = fcOpen.getSelectedFile();
...@@ -54,10 +55,10 @@ public class FileDialog { ...@@ -54,10 +55,10 @@ public class FileDialog {
File selectedFile = null; File selectedFile = null;
initSaveDialog(); initSaveDialog();
retrieveSaveDialogDir(fcSave); dirPreferences.retrieveSaveDialogDir(fcSave);
fcSave.setSelectedFile(new File(recommendedFileName)); fcSave.setSelectedFile(new File(recommendedFileName));
int returnVal = fcSave.showSaveDialog(parent); int returnVal = fcSave.showSaveDialog(parent);
saveSaveDialogDir(fcSave); dirPreferences.saveSaveDialogDir(fcSave);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFile = fcSave.getSelectedFile(); selectedFile = fcSave.getSelectedFile();
...@@ -69,10 +70,10 @@ public class FileDialog { ...@@ -69,10 +70,10 @@ public class FileDialog {
File selectedFile = null; File selectedFile = null;
initSaveAllDialog(); initSaveAllDialog();
retrieveSaveDialogDir(fcSaveAll); dirPreferences.retrieveSaveDialogDir(fcSaveAll);
fcSaveAll.setSelectedFile(new File(recommendedFileName)); fcSaveAll.setSelectedFile(new File(recommendedFileName));
int returnVal = fcSaveAll.showSaveDialog(parent); int returnVal = fcSaveAll.showSaveDialog(parent);
saveSaveDialogDir(fcSaveAll); dirPreferences.saveSaveDialogDir(fcSaveAll);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFile = fcSaveAll.getSelectedFile(); selectedFile = fcSaveAll.getSelectedFile();
...@@ -83,21 +84,21 @@ public class FileDialog { ...@@ -83,21 +84,21 @@ public class FileDialog {
public synchronized void initOpenDialog() { public synchronized void initOpenDialog() {
if (fcOpen == null) { if (fcOpen == null) {
fcOpen = createFileChooser("*.jar", "*.zip", "*.class"); fcOpen = createFileChooser("*.jar", "*.zip", "*.class");
retrieveOpenDialogDir(fcOpen); dirPreferences.retrieveOpenDialogDir(fcOpen);
} }
} }
public synchronized void initSaveDialog() { public synchronized void initSaveDialog() {
if (fcSave == null) { if (fcSave == null) {
fcSave = createFileChooser("*.txt", "*.java"); fcSave = createFileChooser("*.txt", "*.java");
retrieveSaveDialogDir(fcSave); dirPreferences.retrieveSaveDialogDir(fcSave);
} }
} }
public synchronized void initSaveAllDialog() { public synchronized void initSaveAllDialog() {
if (fcSaveAll == null) { if (fcSaveAll == null) {
fcSaveAll = createFileChooser("*.jar", "*.zip"); fcSaveAll = createFileChooser("*.jar", "*.zip");
retrieveSaveDialogDir(fcSaveAll); dirPreferences.retrieveSaveDialogDir(fcSaveAll);
} }
} }
...@@ -131,53 +132,4 @@ public class FileDialog { ...@@ -131,53 +132,4 @@ public class FileDialog {
} }
} }
private void retrieveOpenDialogDir(JFileChooser fc) {
try {
String currentDirStr = luytenPrefs.getFileOpenCurrentDirectory();
if (currentDirStr != null && currentDirStr.trim().length() > 0) {
File currentDir = new File(currentDirStr);
if (currentDir.exists() && currentDir.isDirectory()) {
fc.setCurrentDirectory(currentDir);
}
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
private void saveOpenDialogDir(JFileChooser fc) {
try {
File currentDir = fc.getCurrentDirectory();
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
luytenPrefs.setFileOpenCurrentDirectory(currentDir.getAbsolutePath());
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
private void retrieveSaveDialogDir(JFileChooser fc) {
try {
String currentDirStr = luytenPrefs.getFileSaveCurrentDirectory();
if (currentDirStr != null && currentDirStr.trim().length() > 0) {
File currentDir = new File(currentDirStr);
if (currentDir.exists() && currentDir.isDirectory()) {
fc.setCurrentDirectory(currentDir);
}
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
private void saveSaveDialogDir(JFileChooser fc) {
try {
File currentDir = fc.getCurrentDirectory();
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
luytenPrefs.setFileSaveCurrentDirectory(currentDir.getAbsolutePath());
}
} catch (Exception e) {
Luyten.showExceptionDialog("Exception!", e);
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册