package org.livesub.utils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.scene.layout.BorderPane;
import javafx.stage.Popup;
import org.apache.log4j.Logger;
import org.livesub.player.PlayerState;
import org.mozilla.universalchardet.UniversalDetector;
/**
*
* @author vasilis
*/
public class Utils {
public static final String VERSION = "0.2.1a";
public static final Logger LOG = Logger.getLogger(Utils.class);
private static Dimension screenSize;
private static double defaultWidth;
private static double defaultHeight;
public static final byte SRT_DELAY = 1;
public static final String LINE_BREAK = "\n";
public static final PlayerState playerState = new PlayerState();
public static String encoding = "UTF-8";
public static double fps = 23.976;
public static final String PAUSED_TEXT = "PAUSED!"+ LINE_BREAK;
public static final String LOADED_TEXT = "Subtitles are Loaded!"+ LINE_BREAK
+ "Press (Play button)"+ LINE_BREAK
+ "to start playing...";
public static final String START_TEXT = "Press one of: (Space)\\(Right Click)\\(Load button)"
+ "to load subtitles...";
public static final String HOW_TO_USE = "Controls:"+ LINE_BREAK
+ "Load >>> (Space) || (Right Click)"+ LINE_BREAK
+ "Play >>> (Space) || (Right Click)"+ LINE_BREAK
+ "Pause >>> (Space) || (Right Click)"+ LINE_BREAK
+ "Control Panel >>> (Double Left Click)"+ LINE_BREAK
+ "Settings Panel >>> (S)"+ LINE_BREAK
+ "Exit >>> (Esc) || (Double Right Click)";
public static Color backgroundColor = new Color(.5f, .5f, .5f, .01f);
public static Color fontColor = Color.WHITE;
public static Font font = new Font("Arial",0,45);
/**
* Resize the main panel to fit the width of the screen
* @param panel the main outer which holds everything
*/
public static void setOptimalSize(BorderPane panel) {
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setOptimalSize(panel, screenSize.width, screenSize.height);
}
/**
* Resize the main panel to fit the given dimensions
* @param panel the main outer which holds everything
* @param width
* @param height
*/
public static void setOptimalSize(BorderPane panel, double width, double height) {
if(panel.getPrefWidth() != width - 55) {
defaultWidth = width - 55;
defaultHeight = height * 200 / 1050;
panel.setPrefWidth(defaultWidth);
LOG.trace(String.format("Window resized: %.2fx%.2f",
defaultWidth, defaultHeight));
}
}
/**
* Place the main window (popup) to an optimal position
* @param popup the window to be positioned
*/
public static void adjustLocation(Popup popup) {
double width = popup.getWidth();
double height = popup.getHeight();
double x = (screenSize.width - width) / 2;
double y = (screenSize.height - height);
popup.setX(x);
popup.setY(y);
LOG.trace(String.format("Screen resolution: %dx%d",
screenSize.width, screenSize.height));
}
/**
* Convert the String time stamp [00:00:00,0000] to long
* @param period
* @return -1 for error
*/
public static long dateParseRegExp(String period) {
Pattern pattern = Pattern.compile("([0-9]+):([0-9]+):([0-9]+),([0-9]+)");
Matcher matcher = pattern.matcher(period);
if (matcher.matches()) {
return Long.parseLong(matcher.group(1)) * 3600000L
+ Long.parseLong(matcher.group(2)) * 60000
+ Long.parseLong(matcher.group(3)) * 1000
+ Long.parseLong(matcher.group(4));
}
LOG.error(String.format("Invalid time stamp format %s", period));
return -1;
}
/**
* Find the encoding of the subtitle's file
* @param filepath file to be analyzed
* @return the name of encoding
* @throws IOException
*/
public static String detectEncoding(String filepath) throws IOException {
byte[] buf = new byte[4096];
FileInputStream fis = new FileInputStream(filepath);
// Construct an instance of UniversalDetector
UniversalDetector detector = new UniversalDetector(null);
// Feed some data to the detector
int nread;
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
// Notify the detector of the end of data
detector.dataEnd();
// Get the detected encoding name
String detectedEncoding = detector.getDetectedCharset();
if (detectedEncoding != null) {
LOG.info(String.format("The encoding of the file is %s", detectedEncoding));
} else {
LOG.warn(String.format("No encoding detected! Use defaul %s", Utils.encoding));
detectedEncoding = Utils.encoding;
}
// Reset the detecor
detector.reset();
return detectedEncoding;
}
public static void printVersion() {
LOG.info(String.format("Version %s", VERSION));
}
}