diff --git a/build/VocabularyDecoder.jar b/build/VocabularyDecoder.jar index fef29c3..57b2bce 100644 Binary files a/build/VocabularyDecoder.jar and b/build/VocabularyDecoder.jar differ diff --git a/src/main/java/xyz/scottc/Main.java b/src/main/java/xyz/scottc/Main.java index b401324..c941c7c 100644 --- a/src/main/java/xyz/scottc/Main.java +++ b/src/main/java/xyz/scottc/Main.java @@ -13,12 +13,12 @@ public static void main(String[] args) { } private static void initFrame() { -/* try { + try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) { e.printStackTrace(); - }*/ + } MainFrame frame = new MainFrame("Vocabulary Decoder"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(1000, 1000)); diff --git a/src/main/java/xyz/scottc/frames/commonModeFrame/component/LeftPanel.java b/src/main/java/xyz/scottc/frames/commonModeFrame/component/LeftPanel.java index 0fcf2a0..0dec7f1 100644 --- a/src/main/java/xyz/scottc/frames/commonModeFrame/component/LeftPanel.java +++ b/src/main/java/xyz/scottc/frames/commonModeFrame/component/LeftPanel.java @@ -11,9 +11,12 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; +import java.util.Objects; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -47,8 +50,10 @@ public LeftPanel(CommonModeFrame parent, FunctionPanel functionPanel, TopPanel t this.setLayout(new CommonModeTopPanelLayout()); this.add(this.importButton); + this.importButton.setToolTipText("Import the customized VD File which can be acquired by using \"Tools\" menu in the Main windows."); this.importButton.addActionListener(e -> this.importFile()); this.add(this.exportButton); + this.exportButton.setToolTipText("Export your answer report."); this.add(this.internalFileListLabel); this.add(this.internalFileListScrollPane); @@ -76,6 +81,7 @@ public void mouseClicked(MouseEvent e) { //backButton this.add(this.backButton); + this.backButton.setToolTipText("Go back to the Main windows."); this.backButton.addActionListener(e -> this.goBackToMainFrame()); } @@ -128,23 +134,6 @@ private void addExternalVocabularyPool() { } private void addInternalVocabularyPool() { - //create the InternalLibrary directory - File directory = FileUtils.getDirectoryFile(this); - File internalLibrary = new File(directory.getAbsolutePath() + "/InternalLibrary"); - if (internalLibrary.exists()) { - for (File file : internalLibrary.listFiles()) { - boolean success = file.delete(); - if (!success) { - System.out.println("File deleting in InternalLibrary Fail!"); - } - } - } else { - boolean success = internalLibrary.mkdir(); - if (!success) { - System.out.println("Creating InternalLibrary Fails!"); - } - } - //copy the internal json file to the InternalLibrary directory String jarPath = FileUtils.getJarFilePath(this); JarFile jarFile; @@ -157,16 +146,9 @@ private void addInternalVocabularyPool() { String innerPath = jarEntry.getName(); if (innerPath.startsWith(internalPath) && !innerPath.equals(internalPath)) { InputStream inputStream = this.getClass().getResourceAsStream("/" + innerPath); - String target = internalLibrary.getAbsolutePath() + innerPath.substring(28); - OutputStream outputStream = new FileOutputStream(target); - int length; - byte[] buffer = new byte[1024]; - while ((length = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, length); - } - outputStream.flush(); + String target = MainFrame.internalLibrary.getAbsolutePath() + innerPath.substring(28); + Files.copy(inputStream, Paths.get(target)); inputStream.close(); - outputStream.close(); } } } catch (IOException e) { @@ -174,7 +156,7 @@ private void addInternalVocabularyPool() { } //add all the file in InternalLibrary directory to list - for (File file : internalLibrary.listFiles()) { + for (File file : Objects.requireNonNull(MainFrame.internalLibrary.listFiles())) { this.internalVocabularyPool.add(file); this.internalFileListModel.addElement(file.getName()); } diff --git a/src/main/java/xyz/scottc/frames/mainFrame/MainFrame.java b/src/main/java/xyz/scottc/frames/mainFrame/MainFrame.java index d6bd148..da0f4d5 100644 --- a/src/main/java/xyz/scottc/frames/mainFrame/MainFrame.java +++ b/src/main/java/xyz/scottc/frames/mainFrame/MainFrame.java @@ -9,6 +9,7 @@ import java.awt.*; import java.awt.event.ActionListener; import java.io.File; +import java.util.Objects; public class MainFrame extends JFrame { @@ -32,6 +33,8 @@ public class MainFrame extends JFrame { public boolean isExternalLibraryExist = false; public static File externalLibrary; + public boolean isInternalLibraryExist = false; + public static File internalLibrary; public MainFrame(String title) throws HeadlessException { super(title); @@ -73,18 +76,44 @@ private void menuBarHandler() { this.inputToJson.addActionListener(this.inputToJsonListener); } + //Initialize the directories required for internal and external vocabularies list. private void initialize() { File directory = FileUtils.getDirectoryFile(this); - File[] files = directory.listFiles(); - externalLibrary = new File(directory.getAbsolutePath() + "/ExternalLibrary"); - for (File file : files) { - if (file.isDirectory() && "ExternalLibrary".equals(file.getName())) { - this.isExternalLibraryExist = true; + if (directory != null) { + File[] files = directory.listFiles(); + if (files != null) { + externalLibrary = new File(directory.getAbsolutePath() + "/ExternalLibrary"); + internalLibrary = new File(directory.getAbsolutePath() + "/InternalLibrary"); + for (File file : files) { + if (file.isDirectory() && "ExternalLibrary".equals(file.getName())) { + for (File subFile : Objects.requireNonNull(file.listFiles())) { + boolean success = subFile.delete(); + if (success) System.out.println("File deletion failed"); + } + this.isExternalLibraryExist = true; + } + if (file.isDirectory() && "InternalLibrary".equals(file.getName())) { + for (File subFile : Objects.requireNonNull(file.listFiles())) { + boolean success = subFile.delete(); + if (success) System.out.println("File deletion failed"); + } + this.isInternalLibraryExist = true; + } + } + if (!this.isExternalLibraryExist) { + boolean success = externalLibrary.mkdir(); + if (!success) { + System.out.println("Creating ExternalLibrary Fails!"); + } + } + if (!this.isInternalLibraryExist) { + boolean success = internalLibrary.mkdir(); + if (!success) { + System.out.println("Creating InternalLibrary Fails!"); + } + } } } - if (!this.isExternalLibraryExist) { - boolean success = externalLibrary.mkdir(); - } } private final ActionListener txtToJsonListener = e -> {