Skip to content

Commit

Permalink
Merge pull request #6 from albertopoljak/dev
Browse files Browse the repository at this point in the history
Release update
  • Loading branch information
albertopoljak committed Nov 3, 2017
2 parents 8afab46 + 74c1e45 commit 2c887ff
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 89 deletions.
66 changes: 36 additions & 30 deletions src/main/methods/Log.java → src/main/Logger.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,77 @@
package main.methods;
package main;
import java.awt.Color;
import java.awt.Toolkit;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import main.Main;
import main.methods.Helpers;

public final class Log {
public class Logger extends JTextPane {

private String newLine = System.getProperty("line.separator");;
private String logFileName;
private PrintWriter logOut;

private static String n = System.getProperty("line.separator");
private static String logPath = "log.txt";
private static PrintWriter logOut;

public Logger(){
this.logFileName = "log.txt";
};

private Log(){};
public Logger(String logFileName){
this.logFileName = logFileName;
};


public static void write(String text){
public void append(String text){
appendToTextPanel(text, 'I');
writeToFile(text);
}

public static void write(String text, char color){
public void append(String text, char color){
appendToTextPanel(text, color);
writeToFile(text);
}

public static void writeDebug(String text){
appendToTextPanel(text, 'D');
writeToFile(text);
public void appendDebug(String text){
if( Settings.chckbxDebugMode.isSelected() ){
appendToTextPanel(text, 'D');
writeToFile(text);
}
}

public void changeSavePath(String logFileName){
this.logFileName = logFileName;
}

private static void writeToFile(String text){
private void writeToFile(String text){
try{
openPrintWriter(logPath) ;
openPrintWriter(logFileName) ;
writeStringToFile(text);
closePrintWriter();
}catch (IOException e){
appendToTextPanel("Error creating file for saving!\n"+e, 'E');
}
}

public static void openPrintWriter(String filePath) throws IOException{
private void openPrintWriter(String filePath) throws IOException{
try {
logOut = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)));
} catch (IOException e) {
throw new IOException(e);
}
}

public static void writeStringToFile(String write){
logOut.print( getDate() + write + n );
private void writeStringToFile(String write){
logOut.print( Helpers.getDate() + write + newLine );
}

public static void closePrintWriter(){
private void closePrintWriter(){
if( logOut!=null )
logOut.close();
}
Expand All @@ -74,10 +84,10 @@ public static void closePrintWriter(){
* I or anything else = INFO black
* D = debug
*/
public static void appendToTextPanel(String message, char importance){
StyledDocument doc = Main.txtrLog.getStyledDocument();
public void appendToTextPanel(String message, char importance){
StyledDocument doc = this.getStyledDocument();
//String date = new SimpleDateFormat("[HH:mm:ss] ").format(Calendar.getInstance().getTime());
javax.swing.text.Style style = Main.txtrLog.addStyle("I'm a Style", null);
javax.swing.text.Style style = this.addStyle("I'm a Style", null);

if (importance == 'I' || importance== 'S' )
StyleConstants.setForeground(style, Color.darkGray);
Expand All @@ -93,15 +103,11 @@ else if (importance == 'S' || importance== 'G' ){
}

try {
doc.insertString(doc.getLength(), getDate() + message + n ,style);
doc.insertString(doc.getLength(), Helpers.getDate() + message + newLine ,style);
}catch (BadLocationException e){
Main.txtrLog.setText("ERROR WITH ADDING STRING TO LOG! "+e);
this.setText("ERROR WITH ADDING STRING TO LOG! "+e);
}
}


public static String getDate(){
return new SimpleDateFormat("[HH:mm:ss] ").format(Calendar.getInstance().getTime());
}


}
22 changes: 11 additions & 11 deletions src/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import main.methods.CombinatoricsPermutations;
import main.methods.Helpers;
import main.methods.Log;
import main.methods.ProcessInput;
import java.awt.BorderLayout;
import java.awt.Font;
Expand All @@ -29,7 +27,7 @@ public class Main {
private static JFrame frmDecypherInStyle;
private static Settings windowSettings;
private static JTextArea txtWords;
public static JTextPane txtrLog;
public static Logger txtrLog;
private static JLabel textCurrentMemUsage;
private static PrintWriter out;
private static Timer timer;
Expand Down Expand Up @@ -86,7 +84,7 @@ private static void initialize() {
txtEnter.setOpaque(false);
frmDecypherInStyle.getContentPane().add(txtEnter);

txtrLog = new JTextPane();
txtrLog = new Logger();
txtrLog.setEditable(false);
txtrLog.setBackground(Color.GRAY);
txtrLog.setText("Log:\n");
Expand All @@ -106,7 +104,7 @@ private static void initialize() {
btnGetSavePath.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
Log.write("Save path location:\n"+Settings.savePath , 'I');
txtrLog.append("Save path location:\n"+Settings.savePath , 'I');
}
});
btnGetSavePath.setBounds(624, 390, 140, 23);
Expand Down Expand Up @@ -181,11 +179,12 @@ public void mouseReleased(MouseEvent e) {
tempOutput = ProcessInput.buildWords( ProcessInput.extractInput(txtWords.getText() , Settings.wordSeparator), Settings.wordCombinator , Settings.optionSeparator ) ;
CombinatoricsPermutations.combinations2D( Helpers.convertListToStringArray(tempOutput) );
closePrintWriter();
Log.appendToTextPanel("Test: "+test, 'I');
txtrLog.appendToTextPanel("Finished!" , 'G');
txtrLog.appendToTextPanel("Test: "+test, 'I');
test=0;

}catch(Exception exc){
Log.write("Error: "+exc , 'E');
txtrLog.append("Error: "+exc , 'E');
}
}
});
Expand Down Expand Up @@ -226,7 +225,7 @@ public void mouseReleased(MouseEvent e) {
lblAlgorithmComplexity.setBounds(14, 208, 110, 14);
frmDecypherInStyle.getContentPane().add(lblAlgorithmComplexity);

JLabel txtComplexity = new JLabel("Very High: O(N!*2N+300*N^2)");
JLabel txtComplexity = new JLabel("Not working, to do in following updates");
txtComplexity.setForeground(Color.LIGHT_GRAY);
txtComplexity.setFont(new Font("Monospaced", Font.PLAIN, 14));
txtComplexity.setBounds(124, 204, 640, 25);
Expand All @@ -245,6 +244,7 @@ public void mouseReleased(MouseEvent e) {
frmDecypherInStyle.getContentPane().add(txtEstimatedMemUsage);

JButton btnRefreshVariables = new JButton("Refresh variables");
btnRefreshVariables.setEnabled(false);
btnRefreshVariables.setBackground(Color.DARK_GRAY);
btnRefreshVariables.setForeground(Color.GRAY);
btnRefreshVariables.setFocusPainted(false);
Expand Down Expand Up @@ -307,7 +307,7 @@ public static void openPrintWriter(String filePath) throws IOException{
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)));
} catch (IOException e) {
Log.appendToTextPanel("Error creating file for saving!\n"+e , 'E');
txtrLog.appendToTextPanel("Error creating file for saving!\n"+e , 'E');
throw new IOException(e);
}
}
Expand All @@ -330,10 +330,10 @@ public static void writeStringToPrintWriter(String write[]){
*/
public static void autoMemoryDetection(){
long maxMemory = getMaxMemoryUsage();
Log.write("Max memory: " + maxMemory + "mb");
txtrLog.append("Max memory: " + maxMemory + "mb");
if(maxMemory<2000){
Settings.lowMemory = true;
Log.write("Program is set to run on low memory (less that 2000mb).\n"
txtrLog.append("Program is set to run on low memory (less that 2000mb).\n"
+ "This will affect program execution time but will reduce memory usage.\n"
+ "If the memory reduction is not enough and the program eats all the memory it will crash.\n"
+ "Run in 64bits to increase avaiable memory size (works only if you have more than 2gb RAM)");
Expand Down
Loading

0 comments on commit 2c887ff

Please sign in to comment.