Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
Optimized Initialization process, including addInternalVocabularyPool
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottCTD committed Oct 2, 2020
1 parent c045fd1 commit 31fcf34
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
Binary file modified build/VocabularyDecoder.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/main/java/xyz/scottc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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;
Expand All @@ -157,24 +146,17 @@ 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) {
e.printStackTrace();
}

//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());
}
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/xyz/scottc/frames/mainFrame/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down Expand Up @@ -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 -> {
Expand Down

0 comments on commit 31fcf34

Please sign in to comment.