import java.awt.Container;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.xerces.util.URI;
/**
*
* @author Cy.Co.Labs
*/
public class Util {
private static JDialog waitDialog;
private boolean b = false;
private final boolean f = false, t = true;
private JFrame currentframe;
DecimalFormat currencyformat = new DecimalFormat("#,###.##");
ArrayList<String> tabledata = new ArrayList<String>();
DefaultTableModel dtm;
Vector v;
////////////////////////////////////////////////
private static Util util;
private Util() {
}
public static Util getUtil() {
System.gc();
if (util == null) {
util = new Util();
}
return util;
}
////////////////////////////////////////////////
public String formatDate(Date day) {
String field, imonth = null;
field = day + "";
field.replace(":", "");
field.replace("00", "");
field = field.substring(4, field.length());
int i = field.indexOf(",");
String year = field.substring(field.length() - 4, field.length());
//String month = field.substring(0, (i - 2)).replace(" ", "");
String date = field.substring(3, 6).trim();
if (date.length() < 2) {
date = "0" + date;
}
if (field.contains("Jan")) {
imonth = "1";
} else if (field.contains("Feb")) {
imonth = "2";
} else if (field.contains("Mar")) {
imonth = "3";
} else if (field.contains("Apr")) {
imonth = "4";
} else if (field.contains("May")) {
imonth = "5";
} else if (field.contains("Jun")) {
imonth = "6";
} else if (field.contains("Jul")) {
imonth = "7";
} else if (field.contains("Aug")) {
imonth = "8";
} else if (field.contains("Sep")) {
imonth = "9";
} else if (field.contains("Oct")) {
imonth = "10";
} else if (field.contains("Nov")) {
imonth = "11";
} else if (field.contains("Dec")) {
imonth = "12";
}
if (imonth.length() < 2) {
imonth = "0" + imonth;
}
String setDay = year + "-" + imonth + "-" + date;
return setDay;
}
public String getCurrentTimeStamp() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
return dateFormat.format(cal.getTime()).toString().substring(0, 19);
}
public String getCurrentTime24() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
//System.out.println(dateFormat.format(cal.getTime()).toString().substring(11, 16));
return dateFormat.format(cal.getTime()).toString().substring(11, 16);
}
public String getDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
return dateFormat.format(cal.getTime()).toString().substring(0, 10);
}
public String getCurrentTime12() {
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Calendar cal = Calendar.getInstance();
return dateFormat.format(cal.getTime()).toString().replace(" ", "_");
}
public void setDateToDateChooser(JDateChooser jDateChooser1) {
Date date = new Date();
jDateChooser1.setDateFormatString("yyyy-MM-dd");
jDateChooser1.setDate(date);
}
public void setDateToDateChooser(JDateChooser datechooser, String yyyy_mm_dd) throws Exception {
datechooser.setDate(new SimpleDateFormat("yyyy-MM-dd").parse(yyyy_mm_dd));
}
public String getMD5forString(String input) {
String md5 = null;
if (null == input) {
return null;
}
try {
//Create MessageDigest object for MD5
MessageDigest digest = MessageDigest.getInstance("MD5");
//Update input string in message digest
digest.update(input.getBytes(), 0, input.length());
//Converts message digest value in base 16 (hex)
md5 = new BigInteger(1, digest.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return md5;
}
public String getMD5forFile(String fileURL) {
File filex;
filex = new File(fileURL);
String md5 = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filex);
// md5Hex converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
// The returned array will be double the length of the passed array, as it takes two characters to represent any given byte.
md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fileInputStream));
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return md5;
}
public String getUserDirectory() {
String dir = System.getProperty("user.dir");
return dir;
}
public JDialog LoadingBar(JFrame frame) {
if (waitDialog == null) {
waitDialog = new JDialog(frame, "Loading...");
waitDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
waitDialog.setBounds(frame.getX(), (frame.getY()) + (frame.getHeight() / 2), frame.getBounds().width, 0);
waitDialog.setAlwaysOnTop(true);
}
return waitDialog;
}
public void copyFile(String source, String destination) {
}
public void setWindowIcon(JFrame frame) {
frame.setIconImage(new ImageIcon(getClass().getResource("/icon/icon.png")).getImage());
}
public Dimension getScreenSize() {
return Toolkit.getDefaultToolkit().getScreenSize();
}
public void setWindowMaximized(JFrame frame) {
GraphicsEnvironment env
= GraphicsEnvironment.getLocalGraphicsEnvironment();
frame.setMaximizedBounds(env.getMaximumWindowBounds());
frame.setExtendedState(frame.getExtendedState() | frame.MAXIMIZED_BOTH);
}
public void setMneoimicToButton(JButton button, Character letter) {
button.setMnemonic(letter);
}
public void setMessageForm(JFrame frame) {
}
public void setCustomJOptionPane(JFrame frame, String message, String title, int type) {
JOptionPane.showMessageDialog(frame, message, title, type);
}
public void setMessageRecordAddedSuccessfully(JFrame frame) {
setCustomJOptionPane(frame, "Record Added Successfully!", "ADD ITEM!", JOptionPane.INFORMATION_MESSAGE);
}
public void setMessageUpdateDoneSuccessfully(JFrame frame) {
JOptionPane.showMessageDialog(frame, "Record Updted Successfully!", "UPDATE ITEM", JOptionPane.INFORMATION_MESSAGE);
}
public void setMessageDeleteDoneSuccessfully(JFrame frame) {
JOptionPane.showMessageDialog(frame, "Record Deleted Successfully!", "DELETE ITEM", JOptionPane.INFORMATION_MESSAGE);
}
public void setMessageException(JFrame frame, String from, Exception e) {
setCustomJOptionPane(frame, "Error From: " + from + "\n" + e.toString(), "SYSTEM ERROR!", JOptionPane.ERROR_MESSAGE);
}
public void setMessageInvalid(JFrame frame, String invalid_what) {
JOptionPane.showMessageDialog(null, "Invalid " + invalid_what + " !", "Error", JOptionPane.ERROR_MESSAGE);
}
public int setMessageConfirmYesNo(JFrame frame, String text) {
return JOptionPane.showConfirmDialog(frame, text, "Confirm", JOptionPane.YES_NO_OPTION);
}
public void setMessageSuccessullResetDB(JFrame frame) {
JOptionPane.showMessageDialog(frame, "Database reset successful!", "Done!", JOptionPane.INFORMATION_MESSAGE);
}
public void setMessageActionDoneSuccessfully(JFrame frame, String message, String title) {
JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
}
public void setMessageActionFail(JFrame frame, String message, String title) {
JOptionPane.showMessageDialog(frame, message, title, JOptionPane.ERROR_MESSAGE);
}
public boolean isName(String text) {
b = f;
if (isNotEmpty(text)) {
if (text.matches("[a-zA-Z ]+")) {
b = t;
}
}
System.out.println("-" + text);
return b;
}
public boolean isLetters(String text) {
b = f;
if (isNotEmpty(text)) {
if (text.matches("[a-zA-Z ]+")) {
b = t;
}
}
System.out.println("-" + text);
return b;
}
public boolean isNumbLett(String text) {
b = f;
if (isNotEmpty(text)) {
if (text.matches("[a-zA-Z0-9 ]+")) {
b = t;
}
}
System.out.println("-" + text);
return b;
}
public boolean isTool(String text) {
b = f;
if (isNotEmpty(text)) {
if (text.matches("[a-zA-Z0-9.,/\\'\\- ]+")) {
b = t;
}
}
System.out.println("-" + text);
return b;
}
public boolean isAddress(String text) {
b = f;
if (isNotEmpty(text)) {
if (text.matches("[a-zA-Z0-9.,/ ]+")) {
b = t;
}
}
System.out.println("-" + text);
return b;
}
public boolean isNumeric(String text) {
b = f;
if (isNotEmpty(text)) {
if (text.matches("[0-9]+")) {
b = t;
}
}
System.out.println("-" + text);
return b;
}
public boolean isNIC(String text) {
b = f;
if (text.length() == 10) {
if (isNumeric((text.substring(0, 9)))) {
if (isLetters((text.substring(9, 10)))) {
b = t;
}
}
}
System.out.println("-" + text);
return b;
}
public boolean isTP(String text) {
b = f;
if (text.length() == 9 || text.length() == 10) {
if (isNumeric(text)) {
b = t;
}
}
System.out.println("-" + text);
return b;
}
private boolean isNotEmpty(String text) {
if (text.length() > 0) {
return true;
} else {
return false;
}
}
public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
public boolean isEmail(String email) {
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
return matcher.find();
}
public void messageErrorInvalidFieldDeatils() {
JOptionPane.showMessageDialog(null, "<html><body>Invalid details!<br/>Please enter correct data!</body><html>", "Error", JOptionPane.ERROR_MESSAGE);
}
public void messageErrorInvalidTableSelection() {
JOptionPane.showMessageDialog(null, "<html><body>Invalid details!<br/>Please select a site to get details!</body><html>", "Error", JOptionPane.ERROR_MESSAGE);
}
public void setStopTableEditing(JTable tbl) {
if (tbl.isEditing()) {
tbl.getCellEditor().stopCellEditing();
}
}
/**
* @return the currentframe
*/
public JFrame getCurrentframe() {
return currentframe;
}
/**
* @param currentframe the currentframe to set
*/
public void setCurrentframe(JFrame currentframe) {
this.currentframe = currentframe;
}
public void setAsMoney(JTextField textfield) {
textfield.setText("" + new BigDecimal(textfield.getText()).setScale(2, BigDecimal.ROUND_HALF_UP));
}
public BigDecimal setAsMoney(String value) {
return new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_UP);
}
public BigDecimal getCountWeightPrice(String weightunits, String unitprice) {
BigDecimal weight2 = (new BigDecimal(weightunits).divide(new BigDecimal(1000))).setScale(3, BigDecimal.ROUND_HALF_UP);
BigDecimal price = new BigDecimal(unitprice).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal total = weight2.multiply(price);
total = total.setScale(2, BigDecimal.ROUND_HALF_UP);
return total;
}
public BigDecimal getCountWeightPriceMinimal(String weightunits, String unitprice) {
BigDecimal total = (((new BigDecimal(weightunits).divide(new BigDecimal(1000))).setScale(3, BigDecimal.ROUND_HALF_UP)).multiply((new BigDecimal(unitprice).setScale(2, BigDecimal.ROUND_HALF_UP)))).setScale(2, BigDecimal.ROUND_HALF_UP);
return total;
}
public void setCursorBusy(Container container) {
container.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
public void setCursorDefault(Container container) {
container.setCursor(Cursor.getDefaultCursor());
}
public String setCommaSeperatedNumbers(BigDecimal number) {
return currencyformat.format(number.doubleValue());
}
public String getUserType() throws Exception {
/*System.out.println("" + new String(Files.readAllBytes(Paths.get(Util.getUtil().getUserDirectory() + "\\DevSerCli.sho")), StandardCharsets.UTF_8));
return new String(Files.readAllBytes(Paths.get(Util.getUtil().getUserDirectory() + "\\DevSerCli.sho")), StandardCharsets.UTF_8);*/
return AppSettings.getAppSettings().getAppmode();
}
public String getTerminalName() throws Exception {
/*System.out.println("" + new String(Files.readAllBytes(Paths.get(Util.getUtil().getUserDirectory() + "\\Pc.sho")), StandardCharsets.UTF_8));
return new String(Files.readAllBytes(Paths.get(Util.getUtil().getUserDirectory() + "\\Pc.sho")), StandardCharsets.UTF_8);*/
return AppSettings.getAppSettings().getTerminalName();
}
public void openFolderFromCurrentLocation(String foldername) throws IOException {
Desktop.getDesktop().open(new File(Util.getUtil().getUserDirectory() + foldername));
}
public void openFolder(String URI) throws IOException {
Desktop.getDesktop().open(new File(URI));
}
/*
to ArrayList
List<String> hotelResult = new ArrayList<String>();
while(result.next()) {
hotelResult.add(result.getString("Enter the columnname here");
}
jComboBox.setModel(new DefaultComboBoxModel(ls.toArray()));
*/
void setResultSetToTable(ResultSet resultset, JTable table, int columns) throws Exception {
/*dtm = ((DefaultTableModel) table.getModel());
dtm.setRowCount(0);
while (resultset.next()) {
for (int i = 0; i > columns; i++) {
tabledata.add(resultset.getString(i-1));
System.out.println("xx "+resultset.getString(i-1));
}
dtm.addRow(tabledata.toArray());
tabledata.clear();
}*/
dtm = ((DefaultTableModel) table.getModel());
dtm.setRowCount(0);
//ResultSets.getResultSets().setRs1(jdbc.getdata("SELECT * FROM sales"));
System.out.println("cc" + dtm.getColumnCount());
while (ResultSets.getResultSets().getRs1().next()) {
v = new Vector();
for (int i = 0; i < dtm.getColumnCount(); i++) {
v.add(ResultSets.getResultSets().getRs1().getString(i + 1));
}
dtm.addRow(v);
}
}
public boolean isInternetAvailable() {
b = false;
try {
URL url = new URL("http://www.helloceylon.com");
URLConnection connection = url.openConnection();
connection.connect();
System.out.println("internet ok");
b = true;
} catch (MalformedURLException e) {
b = false;
Util.getUtil().setMessageInvalid(null, "internet connection\nPlease check your internet connection is available.\nLocal backup will perform now.");
} catch (UnknownHostException e) {
b = false;
Util.getUtil().setMessageInvalid(null, "internet connection\nPlease check your internet connection is available.\nLocal backup will perform now.");
} catch (IOException e) {
b = false;
Util.getUtil().setMessageInvalid(null, "internet connection\nPlease check your internet connection is available.\nLocal backup will perform now.");
}
return b;
}
}
No comments:
Post a Comment