Sunday, 29 January 2012

File and Folder Selectors in JAVA

There are basically three methods or mechanisms in java which can be used to select files or folders. These are as follow:

1) JFileChooser  :

JFileChooser provides a simple mechanism for the user to choose a file or folder. Here is the hierarchy of JFileChoose class.

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JFileChooser
 
Example :   
 
final JFrame frame = new JFrame();





JFileChooser chooser = new JFileChooser();
 
//If you want any folder or files to be selected by default use this  
//JFileChooser chooser = new JFileChooser("C:\\Users\\sshankar\\Desktop\\ftp");

//To select only folders use this 
//chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 


//Code to select files of a particular extensions
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);

JTextField jTextField1 = new javax.swing.JTextField();

int choice = chooser.showOpenDialog(frame);
 
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
     System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
} 

 
Note : To use ExampleFileFilter class you need to need to include 
ExampleFileFilter.java file.You can download it from here

2) FolderChooser  :  

FolderChooser provides a simple mechanism for the user to choose a folder.

In addition to supporting the basic folder choosing function, it also supports create new folder, delete an existing folder. Another useful feature is recent list. It allows you to set a list of recent selected folders so that user can choose them directly instead of navigating to it in the file system tree.

Following is the hierarchy of FolderChooser class.

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JFileChooser
                  extended by com.jidesoft.swing.FolderChooser

Example : 


final JFrame frame = new JFrame();

FolderChooser chooser = new FolderChooser("C:\\Users\\sshankar\\Desktop\\shiv");
 
int returnVal = chooser.showOpenDialog(frame);
if(returnVal == FolderChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +      
                                             chooser.getSelectedFile().getName());
}

3) JDirectoryChooser :

JDirectoryChooser also provides the mechanism for the user to choose a folder. 
A fix to JFileChooser that correctly allows only directories to be selected. Does the trick by accepting "." as selection of the currently displayed directory. 

Following is the hierarchy of JDirectoryChooser class.

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JFileChooser
                  extended by se.datadosen.component.JDirectoryChooser


Example :

final JFrame frame = new JFrame();

JDirectoryChooser chooser = new JDirectoryChooser("C:\\Users\\sshankar\\Desktop\\shiv");

int choice = chooser.showOpenDialog(frame);

if(returnVal == FolderChooser.APPROVE_OPTION) {
           System.out.println("You chose to open this file: " + 

                                        chooser.getSelectedFile().getName());
}



Key Differences :

1) FolderChooser is slower than both JFileChooser and JDirectoryChooser.

2) JDirectoryChooser provides the additional facility to delete the folder.

3) Appearance of JFileChooser dialog box is different from both JDirectoryChooser and FolderChooser while JDirectoryChooser and FolderChooser dialog boxes almost looks same.

Saturday, 21 January 2012

Relative vs Absolute positioning in CSS

An important concept to understand first is that every single element on a web page is a block. Literally a rectangle of pixels. This is easy to understand when when you set the element to display: block; or if that element is by default display: block; This means you can set a width and a height and that element will respect that. But elements that are display: inline, like a span by default, are also rectangles, they just flow onto the page different, lining up horizontally as they can.

Now that you are picturing every single page element as a block of pixels, we can talk about how positioning is used to get the blocks of pixels exactly where you want them to go. We're going to leave off any discussion of the box model, but that factors into this as well...

There is four type of positioning in CSS

Static  

This is the default for every single page element. Different elements don't have different default values for positioning, they all start out as static. Static doesn't mean much, it just means that the element will flow into the page as it normally would. The only reason you would ever set an element to position: static is to forcefully-remove some positioning that got applied to an element outside of your control. This is fairly rare, as positioning doesn't cascade.

Relative

This type of positioning is probably the most confusing. What it really means is "relative to itself".

Following are the points to keep in mind while using relative positioning :

1) If you set position: relative; on an element but no other positioning attributes (top, left, bottom or right), it will no effect on it's positioning at all, it will be exactly as it would be if you left it as position: static; But if you DO give it some other positioning attribute, say, top: 10px;, it will shift it's position 10 pixels DOWN from where it would NORMALLY be. I'm sure you can imagine, the ability to shift an element around based on it's regular position is pretty useful. I find myself using this to line up form elements many times that have a tendency to not want to line up how I want them to.

There are two other things that happen when you set position: relative; on an element that you should be aware of.

2) One is that it introduces the ability to use z-index on that element, which doesn't really work with statically positioned elements. Even if you don't set a z-index value, this element will now appear on top of any other statically positioned element. You can't fight it by setting a higher z-index value on a statically positioned element.

3) The other thing that happens is it limits the scope of absolutely positioned child elements. Any element that is a child of the relatively positioned element can be absolutely positioned within that block.

Third one is the most important point to note, means if you are having one parent element and the other is child one and parent element is having positioned relative while the child one is positioned absolute, then the child element would behave absolutely to the relative positions of the parent element. It may be bit confusing but for more clarifications you can following the links given below :

http://learncss.tutsplus.com/lesson/relative-and-absolute-positioning/

http://www.barelyfitz.com/screencast/html-training/css/positioning/

Absolute

This is a very powerful type of positioning that allows you to literally place any page element exactly where you want it. You use the positioning attributes top, left bottom and right to set the location. Remember that these values will be relative to the next parent element with relative (or absolute) positioning. If there is no such parent, it will default all the way back up to the <html> element itself meaning it will be placed relatively to the page itself. 

The trade-off, and most important thing to remember, about absolute positioning is that these elements are removed from the flow of elements on the page. An element with this type of positioning is not affected by other elements and it doesn't affect other elements. This is a serious thing to consider every time you use absolute positioning. It's overuse or improper use can limit the flexibility of your site.

Fixed

This type of positioning is fairly rare but certainly has its uses. A fixed position element is positioned relative to the viewport, or the browser window itself. The viewport doesn't change when the window is scrolled, so a fixed positioned element will stay right where it is when the page is scrolled.

Saturday, 14 January 2012

How Regular Expression works in PHP?

Regular Expression, commonly known as RegEx is considered to be one of the most complex concepts. However, this is not really true. Unless you have worked with regular expressions before, when you look at a regular expression containing a sequence of special characters like /, $, ^, \, ?, *, etc., in combination with alphanumeric characters, you might think it a mess. RegEx is a kind of language and if you have learnt its symbols and understood their meaning, you would find it as the most useful tool in hand to solve many complex problems related to text searches and validations.

Basic Syntax of Regular Expressions

First of all, let's take a look at two special symbols: '^' and '$'. What they do is indicate the start and the end of a string, respectively, like this:
  • "^The": matches any string that starts with "The";
  • "of despair$": matches a string that ends in the substring "of despair";
  • "^abc$": a string that starts and ends with "abc" -- that could only be "abc" itself!
  • "notice": a string that has the text "notice" in it.
You can see that if you don't use either of the two characters we mentioned, as in the last example, you're saying that the pattern may occur anywhere inside the string -- you're not "hooking" it to any of the edges.

Multiple Characters

There are also the symbols '*', '+', and '?', which denote the number of times a character or a sequence of characters may occur. What they mean is: "zero or more", "one or more", and "zero or one." Here are some examples:

  • "ab*": matches a string that has an a followed by zero or more b's ("a", "ab", "abbb", etc.);
  • "ab+": same, but there's at least one b ("ab", "abbb", etc.);
  • "ab?": there might be a b or not;
  • "a?b+$": a possible a followed by one or more b's ending a string.
You can also use bounds, which come inside braces and indicate ranges in the number of occurences:
  • "ab{2}": matches a string that has an a followed by exactly two b's ("abb");
  • "ab{2,}": there are at least two b's ("abb", "abbbb", etc.);
  • "ab{3,5}": from three to five b's ("abbb", "abbbb", or "abbbbb").
Note that you must always specify the first number of a range (i.e, "{0,2}", not "{,2}"). Also, as you might have noticed, the symbols '*', '+', and '?' have the same effect as using the bounds "{0,}", "{1,}", and "{0,1}", respectively.
Now, to quantify a sequence of characters, put them inside parentheses:
  • "a(bc)*": matches a string that has an a followed by zero or more copies of the sequence "bc";
  • "a(bc){1,5}": one through five copies of "bc."

OR operator

The '|' symbol works as an OR operator: "tips|tutorials": matches a string that has either "tips" or "tutorials" in it.
"(b|cd)ef": a string that has either "bef" or "cdef".
"(a|b)*c": a string that has a sequence of alternating a's and b's ending in a c.


Wild character

A period ('.') is a wild character - it can stand for any single character:
"t.*p": matches a string that has a t followed by any number of characters followed by a p ("tip", "tp", "tdfsadfsadsfp", etc).
"^.{5}$": a string with exactly 5 characters ("bingo", "blind", "rainy", "asdfe", etc).

Bracket expressions

Bracket expressions lets you match a whole range of characters to a single position of a string:
"[tu]": matches a string that has either a 't' or a 'u' (that's the same as "t|u");
"[a-d]": a string that has lowercase letters 'a' through 'd' (that's equal to "a|b|c|d" and even "[abcd]");
"^[a-zA-Z]": a string that starts with a letter;
"[0-9]%": a string that has a single digit before a percent sign;
",[a-zA-Z0-9]$": a string that ends in a comma followed by an alphanumeric character.
Note that inside brackets, all the regex special characters are just ordinary characters - they don't do any of their usual regular expression functions.


Excluding characters

You can also exclude characters by using a '^' as the first symbol in a bracket expression:
"%[^a-zA-Z]%" matches a string with a character that is not a letter between two percent signs).
Note - the difference between this application and using ^ at the start of a regular expression which specifies the first character of a string.

Escaping regular expression characters

What do you do if you want to check for one of the regular expression special characters "^.[$()|*+?{\" in your text string? You have to escape these characters with a backslash ('\').

"(https?:\/\/)" matches http:// or https://

Retrieving text using preg_match

If you want to check if the string matches the regular expression or not, use preg_match function
preg_match($pattern, $string);
It returns a value of 1 if there is a match to your regular expression, a value of 0 if no match. For example,  

function validate_URL($url){
    $pattern = "/^(https?:\/\/)?([a-z0-9-])+(.[a-z0-9-]+)*$/";
    return preg_match($pattern, $url);
}
$result = validate_URL('http://www.google.com');
print $result;
output : 1
Some more valid urls you can test using this function like:
https://www.google.com
www.google.com

http://dirtyhandsphp.blogspot.com
dirtyhandsphp.blogspot.com

 

Odoo 10: Close wizard and open standard form

Hi, Today we are going to learn how to open standard form after saving data in wizard. Let's say I have created an wizard to fill bas...