Want to advertise here? Contact us!
cghersi
cghersi
Reputation Top 1%
Cristiano Ghersi
Great passion for Software Engineering, mostly coding in Java and C#, I enjoy creating modular and reusable code for everybody!
166 Snippets  (1st place)
Published
13 Channels
Created
51 Channels
Following
7 hours ago
Last Visit
May 5, 2012
Registered
4794 points  (1st place)
Reputation
Top 5% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Guru Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Master Publisher
Guru Publisher
Master Topic Creator
Guru Topic Creator
Junior Topic Hub
Serious Topic Hub
Senior Topic Hub
Master Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Senior Team Manager
Junior Influencer
Serious Influencer
Senior Influencer
Junior Commenter
Serious Commenter
Senior Commenter
Master Commenter
Junior Judge
Serious Judge
Senior Judge
Master Judge
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Master Popular Coder
Guru Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Snip2Coder
Serious Snip2Coder
Senior Snip2Coder
Junior Geek Aggregator
Serious Geek Aggregator
Senior Geek Aggregator
Junior Famous Coder

Recent Snippets See all snippets by cghersi

public by cghersi modified Aug 12, 2014  1  3  2

Embed a browser into SWT Dialog and manage the firing of popups from links in the displayed page

This is how to manage an embedded browser. Note: unfortunately this seems not working on some versions of Mac OS X.
try {
	Browser browser = new Browser(this, SWT.NONE);
	browser.setJavascriptEnabled(true);
	browser.setUrl("www.myurl.com");
	browser.addOpenWindowListener(new OpenWindowListener() {
		public void open(WindowEvent event) {
			log.debug("Opening browser:" + event);
			new Thread( new Runnable() {
	            public void run() {
	            	log.debug("some stuff t do here...");
	            }
			}).start();
			
			final Shell shell = new Shell(event.display);
		    shell.setText("My Browser page");
		    shell.setLayout(new FillLayout());
		    event.browser = new Browser(shell, SWT.NONE);
		    shell.open();
		    
		    event.browser.addCloseWindowListener(new CloseWindowListener() {
				@Override
				public void close(WindowEvent event) {
					log.debug("closing");
					shell.setVisible(false);
				}
			});
		}
	});
} catch (Throwable t) {
	//on some architecture seems that SWT Browser is not working...
	log.warn("Cannot display SWT Browser");
}

public by cghersi modified Aug 5, 2014  1  3  0

How to simply compute the MD5 hash of a given string, being sure to have the same results retrieved by HASH method of MS SQL Server

Returns the MD5 hash of the given string
public static String computeMD5Hash(String input) {
	if (isNullOrEmpty(input))
		return "";
	byte[] bytesOfMessage = null;
	try {
		bytesOfMessage = input.getBytes("UTF-16LE");
	} catch (UnsupportedEncodingException e) {
		log.error("Cannot support UTF-8 encoding due to " + e, e);
		return "";
	}
	MessageDigest md = null;
	try {
		md = MessageDigest.getInstance("MD5");
	} catch (NoSuchAlgorithmException e) {
		log.error("Cannot support MD5 algorithm due to " + e, e);
		return "";
	}
	byte[] thedigest = md.digest(bytesOfMessage);
	return printHexByteArray(thedigest);
}

public by cghersi modified Aug 5, 2014  1  3  0

How to print a byte array in hexadecimal form in Java

Prints the given byte array as hexadecimal string
public static String printHexByteArray(byte[] input) {
    if (input == null)
        return "";
    StringBuilder sbs = new StringBuilder();
    for (int i = 0; i < input.length; i++) {
    	sbs.append(String.format("%02x", input[i] & 0xff));
    }
    return sbs.toString();
}

public by cghersi modified Jul 4, 2014  283  5  2

Print in a nice format the given date in C#

This is how to print the given date in a pretty way. Make sure the input date is in UTC.
static public string SmartDate(this DateTime dateToPrint, bool longDate = false)
{
    DateTime now = DateTime.Now.ToUniversalTime();
    TimeSpan deltaTime = now - dateToPrint;
    if (deltaTime.TotalDays >= 7.0)
      return string.Format("on {0}", (longDate) ? dateToPrint.ToString("MMM d, yyyy") : dateToPrint.ToShortDateString());
    if (deltaTime.TotalDays >= 2.0)
      return string.Format("on {0} at {1}", dateToPrint.DayOfWeek.ToString(), dateToPrint.ToLongTimeString());
    if (deltaTime.TotalDays >= 1.0)
        return string.Format("yesterday at {0}", (longDate) ? dateToPrint.ToLongTimeString() : dateToPrint.ToShortTimeString());
    if (deltaTime.TotalHours >= 2.0)
      return string.Format("{0} hours ago", (int)(deltaTime.TotalHours));
    if (deltaTime.TotalHours >= 1.0)
      return string.Format("{0} hour ago", (int)(deltaTime.TotalHours));
    if (deltaTime.TotalMinutes > 1.0)
      return string.Format("{0} minutes ago", (int)(deltaTime.TotalMinutes));

    return "just now";
}

public by cghersi modified Jul 3, 2014  90  4  1

Where you can find the logs and temporary stuff from IntelliJ on Windows7

This is the location where the plugins can safely write some stuff
C:\Users\<your user>\AppData\Local\Temp\<pluginName>