50% OFF!!!

Booking.com

Tuesday, June 23, 2009

Html (& javascript) Slider example


Here is an example for html slider - Only using HTML & Javascript.
Nice animated slider (good looking animation)

Code:
<html>
<head>
<style>
.spltr {width: 4px; background-color: firebrick;}
</style>
<script language="javascript" type="text/javascript">
function scrollSpeedAdd(obj,step)
{
scrollStop();
scrollAdd(obj, scrollAdd.step + step);
}
function scrollAdd(obj,step)
{
scrollAdd.elem = document.getElementById('MainSlideDiv');
if(scrollAdd.elem)
{
scrollAdd.step = step;
scrollAdd.I = window.setInterval(startScroll,10);
}
}
function startScroll()
{
scrollAdd.elem.scrollLeft = scrollAdd.elem.scrollLeft + scrollAdd.step;
}
function scrollStop()
{
window.clearInterval(scrollAdd.I);
}
</script>
</head>
<body
<b>Set your mouse over the arrows...</b><br /><br />
<table width="700" border="0" cellspacing="0" cellpadding="0" bgcolor="khaki">
<tr style="text-align: center;">
<td width="30" onmouseout="scrollStop();" onmouseover="scrollAdd(this,-2)" onclick="scrollSpeedAdd(this,-1)"> << </td>
<td width="640" align="center">
<div style="overflow:hidden; width:630; height:55;" id="MainSlideDiv" dir="rtl">
<table width="100%" style="text-align:center" border="0" cellspacing="5" cellpadding="5">
<tr>
<td>Test 1111 </td>
<td class="spltr"> </td>
<td>Test 2222 </td>
<td class="spltr"> </td>
<td>Test 3333 </td>
<td class="spltr"> </td>
<td>Test 4444 </td>
<td class="spltr"> </td>
<td>Test 5555 </td>
<td class="spltr"> </td>
<td>Test 6666 </td>
<td class="spltr"> </td>
<td>Test 7777 </td>
<td class="spltr"> </td>
<td>Test 8888 </td>
<td class="spltr"> </td>
<td>Test 9999 </td>
<td class="spltr"> </td>
<td>Test AAAA </td>
<td class="spltr"> </td>
<td>Test BBBB </td>
<td class="spltr"> </td>
<td>Test CCCC </td>
<td class="spltr"> </td>
<td>Test DDDD </td>
<td class="spltr"> </td>
<td>Test EEEE </td>
<td class="spltr"> </td>
<td>Test FFFF </td>
<td class="spltr"> </td>
</tr>
</table>
</div>
</td>
<td width="30" onmouseout="scrollStop()" onmouseover="scrollAdd(this,2)" onclick="scrollSpeedAdd(this,+1)"> >> </td>
</tr>
</table>
</body>
</html>

Thursday, June 18, 2009

Working with sql xml | Sql server


Here is a code for manipulating XML object under SQL Server.
This code allow to recieve attribute and note contents.
Best for xml manipulations.

--Under this configuration:
DECLARE @xml XML
SET @xml = '<root><a><b id="456"></b><c>world...</c></a></root>'


--Get attribute's content from xml field:
SELECT @xml.value('(//root/a/b/@id)[1]','int') AS 'Attribute Content'


--Get node's content from xml field:
SELECT @xml.value('(//root/a/c)[1]','nvarchar(MAX)') AS 'Node Content'




Bits and bitwise operation in C#


Here is an example for bits string format and bitwise opertaion (not).
It helps us to print the number as bit representation.

Example (using console application):
using System;
class MainClass
{
static void Main()
{
int[] values = { 0, 0x111, 0xfffff, 0x8888, 0x22000022};
foreach (int v in values)
{
Console.WriteLine("~0x{0:x8} = 0x{1:x8}", v, ~v);
}
}
}


Output:
~0x00000000 = 0xffffffff
~0x00000111 = 0xfffffeee
~0x000fffff = 0xfff00000
~0x00008888 = 0xffff7777
~0x22000022 = 0xddffffdd


Helpful for "bits users" which deal with common bit operations...


Source: http://msdn.microsoft.com/en-us/library/d2bd4x66(VS.80).aspx

Tuesday, June 16, 2009

J2ME | Analog Clock control - custom item



Here is an example for anlogic clock control (extends CustomItem) which may be added to Form class and also in Canvas class.
for Form class just add it as and item,
and for Canvas class and its paint method, provide graphics to AnalogClock's paint method.

Here is Code for the analog clock:


package CustomItems;

import javax.microedition.lcdui.*;
import java.util.Calendar;

public class AnalogClock extends CustomItem implements Runnable
{

private double diam = 0.38;
private double LineLengthSeconds = 0.90;
private double LineLengthMinutes = 0.75;
private double LineLengthHour = 0.50;
private double LineLengthTicks = 0.08;
private double TextPositionRelativeR = 1.22;
//
/***
* Represents control's width
*/
private int Width = 100;
/***
* Represents control's height
*/
private int Height = 100;
/***
* Represents clock's radius
*/
private int _raduis;
/***
* Represents clock's center point - X
*/
private int _circleCenterX;
/***
* Represents clock's center point - Y
*/
private int _circleCenterY;
/***
* Represents clock's current date&time
*/
private Calendar _now;
/***
* Represents the thread which implement the clock's ticks
*/
private Thread _thread = null;
/***
* Represents a flag which describe if the thread is running or not
*/
private boolean _threadIsRunning = false;
//
/***
* Represents the background color of the clock
*/
public int BackColor = 0xffffff;
/***
* Represents the color of the clock (text & lines)
*/
public int ClockColor = 0x000000;
/***
* A flag which represent whether or not display the date inside the clock
*/
public boolean ShowDate = false;
/***
* Represents the font of the text drawn
*/
public Font Font;

/** Creates a new instance of AnalogClock */
public AnalogClock(String p_label, int p_size)
{
super(p_label);
if (p_size <= 0)
{
sizeChanged(Width, Height);
}
else
{
sizeChanged(p_size, p_size);
}
Font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
}

protected int getMinContentWidth()
{
return 10;
}

protected int getMinContentHeight()
{
return 10;
}

protected int getPrefContentWidth(int p_height)
{
return Width;
}

protected int getPrefContentHeight(int p_width)
{
return Height;
}

protected void sizeChanged(int p_w, int p_h)
{
Height = p_h;
Width = p_w;
int size = Math.min(Width, Height);
_raduis = (int) (diam * (double) size);
_circleCenterX = size / 2;
_circleCenterY = size / 2;
_now = Calendar.getInstance();
}

private int pointX(double minute, double radius, int _circleCenterX)
{
double angle = minute * Math.PI / 30.0;
return (int) ((double) _circleCenterX + radius * Math.sin(angle));
}

private int pointY(double minute, double radius, int oy)
{
double angle = minute * Math.PI / 30.0;
return (int) ((double) oy - radius * Math.cos(angle));
}

public void updateTime()
{
_now = Calendar.getInstance();
repaint();
}

public void updateTime(Calendar p_currtime)
{
_now = p_currtime;
repaint();
}

protected void paint(Graphics g, int w, int h)
{
// clear background
g.setColor(BackColor);
g.fillRect(0, 0, Width - 1, Height - 1);
g.setColor(ClockColor);

// draw circle
g.drawArc(_circleCenterX - _raduis, _circleCenterY - _raduis, _raduis * 2, _raduis * 2, 0, 360);

// set text's font
g.setFont(Font);
int textH = Font.getHeight();

// draw date (if allowed)
if (ShowDate == true)
{
String strDate = getDateString(_now, "-");
int strDateWidth = Font.stringWidth(strDate);
g.drawRect(_circleCenterX - strDateWidth / 2, _circleCenterY, strDateWidth, textH);
g.drawString(strDate, _circleCenterX, _circleCenterY, Graphics.TOP | Graphics.HCENTER);
}

// draw ticks & digits
int textW;
for (int hour = 1; hour <= 12; hour++)
{
double angle = hour * 60.0 / 12.0;
g.drawLine(
pointX(angle, _raduis * (1 - LineLengthTicks), _circleCenterX),
pointY(angle, _raduis * (1 - LineLengthTicks), _circleCenterY),
pointX(angle, _raduis, _circleCenterX),
pointY(angle, _raduis, _circleCenterY));

// texts
textW = Font.stringWidth("" + hour);
g.drawString("" + hour,
(int) pointX(angle, _raduis * TextPositionRelativeR, _circleCenterX) - textW / 2,
(int) pointY(angle, _raduis * TextPositionRelativeR, _circleCenterY) - textH / 2,
0);
}

double hour = _now.get(Calendar.HOUR) * 60.0 / 12.0;
double minute = _now.get(Calendar.MINUTE);
double second = _now.get(Calendar.SECOND);

// draw hour line
g.drawLine(_circleCenterX, _circleCenterY,
pointX(hour + (double) minute / 12.0, _raduis * LineLengthHour, _circleCenterX),
pointY(hour + (double) minute / 12.0, _raduis * LineLengthHour, _circleCenterY));

// draw minutes line
g.drawLine(_circleCenterX, _circleCenterY,
pointX(minute + second / 60.0, _raduis * LineLengthMinutes, _circleCenterX),
pointY(minute + second / 60.0, _raduis * LineLengthMinutes, _circleCenterY));

// draw seconds line
g.drawLine(_circleCenterX, _circleCenterY,
pointX((double) second, _raduis * LineLengthSeconds, _circleCenterX),
pointY((double) second, _raduis * LineLengthSeconds, _circleCenterY));
}

public synchronized void startTicking()
{
if (_thread == null)
{
_thread = new Thread(this);

_threadIsRunning = true;
_thread.start();
}
}

public synchronized void stopTicking()
{
if (_thread != null)
{
_threadIsRunning = false;
try
{
_thread.join();
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}

public void run()
{
while (_threadIsRunning == true)
{
this.updateTime();

try
{
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}

private String getDateString(Calendar p_calendar, String p_delimiter)
{
int day = p_calendar.get(Calendar.DAY_OF_MONTH);
int month = p_calendar.get(Calendar.MONTH) + 1;
int year = p_calendar.get(Calendar.YEAR);

String strDay = (day < 10) ? "0" + day : String.valueOf(day);
String strMonth = (month < 10) ? "0" + month : String.valueOf(month);
String strYear = String.valueOf(year);

StringBuffer sb = new StringBuffer();
sb.append(strDay);
sb.append(p_delimiter);
sb.append(strMonth);
sb.append(p_delimiter);
sb.append(strYear);

return sb.toString();
}
}


Usage sample:

package Test;

import CustomItems.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class StartMidlet extends MIDlet implements CommandListener
{
public void startApp()
{
Form f = new Form("My Clock Test");
int w = f.getWidth();
int h = f.getHeight();

AnalogClock cl = new AnalogClock(null, (int) (w * 0.8));
cl.setLayout(Item.LAYOUT_2 | Item.LAYOUT_CENTER);
cl.startTicking();
f.append(cl);

Command cmd = new Command("Back", Command.BACK, 0);
f.addCommand(cmd);

cmd = new Command("Next", Command.OK, 1);
f.addCommand(cmd);

f.setCommandListener(this);

Display.getDisplay(this).setCurrent(f);
}

public void pauseApp()
{ }

public void destroyApp(boolean unconditional)
{ }

public void commandAction(Command arg0, Displayable arg1)
{ }
}




The contorl have 2 methods for managing clock timer.
startTicking - for start ticking the timer (this calls repaint...)
stopTicking- for stop the timer



Use it smartly...

Thursday, June 11, 2009

Sql Server | Check if exist: DataBase, Table or Stored-Procedure

Here is a code for checking if an object exist in SQL SERVER.
This may be helpful if you have 2 enviroments and on each one of them
the object (db, table, stored-procedure) are diffrent,
so you can check whether this is the specific db and perform actions...

Check is Database exists:

if db_id('dbname') is not null



Check is Table exists:

if object_id('object_name', 'U') is not null -- for table



Check is Store-Procedure exists:

if object_id('object_name', 'P') is not null -- for SP



hope this is helps...

Monday, June 8, 2009

J2ME | String Replace method



I found a good string replace function/method.
J2ME do not contain string replace (only char replace),
so this method is very helpful!!!



public static String replace(String _text, String _searchStr, String _replacementStr) {
// String buffer to store str
StringBuffer sb = new StringBuffer();

// Search for search
int searchStringPos = _text.indexOf(_searchStr);
int startPos = 0;
int searchStringLength = _searchStr.length();

// Iterate to add string
while (searchStringPos != -1) {
sb.append(_text.substring(startPos, searchStringPos)).append(_replacementStr);
startPos = searchStringPos + searchStringLength;
searchStringPos = _text.indexOf(_searchStr, startPos);
}

// Create string
sb.append(_text.substring(startPos,_text.length()));

return sb.toString();
}





found at http://forums.sun.com/thread.jspa?threadID=734604&tstart=5431

Thursday, June 4, 2009

Sql Server | Compare 2 tables columns


Here is a sql statement for comapring two tables (may be in diffrent DBs) columns.
This SQL statement compare only if column exist in both or not and return the difference.
You may improve the sql statement for comapring more... :)
I hope this sql script will be helpful...



DECLARE @Db1 NVARCHAR(MAX)
DECLARE @Table1 NVARCHAR(MAX)
DECLARE @Db2 NVARCHAR(MAX)
DECLARE @Table2 NVARCHAR(MAX)
DECLARE @Sql NVARCHAR(MAX)

SET @Db1 = 'MMIS_SNAP'
SET @Table1 = 'CodeTablesData'
SET @Db2 = 'MMIS_SNAP'
SET @Table2 = 'CodeTables'
SET @Sql = ' ' +
' SELECT ''in ' + @Db1 + '.' + @Table1 + ' --- not in ' + @Db2 + '.' + @Table2 + ''' AS TITLE, a.TABLE_CATALOG, a.column_name ' +
' FROM ' + @Db1 + '.INFORMATION_SCHEMA.COLUMNS a ' +
' WHERE a.column_name NOT IN (SELECT column_name ' +
' FROM ' + @Db2 + '.INFORMATION_SCHEMA.COLUMNS b ' +
' WHERE b.table_name IN (''' + @Table2 + ''')) ' +
' AND a.table_name IN (''' + @Table1 + ''') ' +

' UNION ALL ' +

' SELECT ''in ' + @Db2 + '.' + @Table2 + ' --- not in ' + @Db1 + '.' + @Table1 + ''' AS TITLE, a.TABLE_CATALOG, a.column_name ' +
' FROM ' + @Db2 + '.INFORMATION_SCHEMA.COLUMNS a ' +
' WHERE a.column_name NOT IN (SELECT column_name ' +
' FROM ' + @Db1 + '.INFORMATION_SCHEMA.COLUMNS b ' +
' WHERE b.table_name IN (''' + @Table1 + ''')) ' +
' AND a.table_name IN (''' + @Table2 + ''') ' +
''

EXEC (@Sql)




You may create store-procedure from this script, and the automaticly run it
on all your tables.
This may be automatic table columns comparison...


post your additional information as comments, thanks


ANOTHER SAMPLE, faster compare with 'Allow null' & 'Data type':



SET @Db1 = 'TestDB'
SET @Table1 = 'Users'
SET @Db2 = 'TestDB'
SET @Table2 = 'Users2'

SET @Sql = ' ' +
' SELECT ''in ' + @Db1 + '.' + @Table1 + ' --- not in ' + @Db2 + '.' + @Table2 + ''' AS TITLE, ' +
' a.TABLE_CATALOG, ' +
' a.COLUMN_NAME, ' +
' a.IS_NULLABLE, ' +
' a.DATA_TYPE ' +
' FROM ' + @Db1 + '.INFORMATION_SCHEMA.COLUMNS a ' +
' WHERE NOT EXISTS ( ' +
' SELECT b.COLUMN_NAME ' +
' FROM ' + @Db2 + '.INFORMATION_SCHEMA.COLUMNS b ' +
' WHERE b.table_name IN (''' + @Table2 + ''') ' +
' AND b.COLUMN_NAME = a.COLUMN_NAME ' +
' AND b.IS_NULLABLE = a.IS_NULLABLE ' +
' AND b.DATA_TYPE = a.DATA_TYPE ' +
' ) ' +
' AND a.table_name IN (''' + @Table1 + ''') ' +
' ' +
' UNION ALL ' +
' ' +
' SELECT ''in ' + @Db2 + '.' + @Table2 + ' --- not in ' + @Db1 + '.' + @Table1 + ''' AS TITLE, ' +
' a.TABLE_CATALOG, ' +
' a.COLUMN_NAME, ' +
' a.IS_NULLABLE, ' +
' a.DATA_TYPE ' +
' FROM ' + @Db2 + '.INFORMATION_SCHEMA.COLUMNS a ' +
' WHERE NOT EXISTS ( ' +
' SELECT b.COLUMN_NAME ' +
' FROM ' + @Db1 + '.INFORMATION_SCHEMA.COLUMNS b ' +
' WHERE b.table_name IN (''' + @Table1 + ''') ' +
' AND b.COLUMN_NAME = a.COLUMN_NAME ' +
' AND b.IS_NULLABLE = a.IS_NULLABLE ' +
' AND b.DATA_TYPE = a.DATA_TYPE ' +
' ) ' +
' AND a.table_name IN (''' + @Table2 + ''') ' +
' '

PRINT @Sql
EXEC (@Sql)