Monday, 17 June 2013

How to disable fullscreen button in Mac OS and Windows in SWT / Java App?


I am working on SWT app. It works fine on windows, but when I run the same code on mac. I get a full screen button on right corner of my shell.
enter image description here
On clicking that full screen button the app stop responding and nothing happens. I want to disable the click on that fullscreen button.

This will work for windows:
display = Display.getDefault();
shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);        
setDialogShell(shell);
getDialogShell().setLayout( new FormLayout());
getDialogShell().setFullScreen(false);
see link but it won't disable that full screen button in mac.
This will work for Mac:
display = Display.getDefault();
shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
//Shell shell = window.getShell();
NSWindow nswindow = shell.view.window();
nswindow.setCollectionBehavior(0);  
nswindow.setShowsResizeIndicator(false);
setDialogShell(shell);
getDialogShell().setLayout( new FormLayout());
getDialogShell().setFullScreen(false);


This worked for both windows and Mac OS:
The above code is not running in windows, as no recognised "NSWindow" class. For using common code for both window and mac use the below code.
public static boolean isWindows() {

      return (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0);

     }
/// Check if OS is Window, Then change the code as follow
display = Display.getDefault();
        shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
         //Shell shell = window.getShell();


        if(!isWindows()){
            Field field = Control.class.getDeclaredField("view");
            Object /*NSView*/ view = field.get(shell);
            if (view != null)
            {
                Class<?> c = Class.forName("org.eclipse.swt.internal.cocoa.NSView");
                Object /*NSWindow*/ window = c.getDeclaredMethod("window").invoke(view);

                c = Class.forName("org.eclipse.swt.internal.cocoa.NSWindow");
                Method setCollectionBehavior = c.getDeclaredMethod(
                        "setCollectionBehavior", /*JVM.is64bit() ?*/ long.class /*: int.class*/);
                setCollectionBehavior.invoke(window,0);
            }
            //          NSWindow nswindow = shell.view.window();
            //          nswindow.setCollectionBehavior(0) ; 
            //          nswindow.setShowsResizeIndicator(false);
        }
        setDialogShell(shell);
        getDialogShell().setLayout( new FormLayout());
        getDialogShell().setFullScreen(false);

        getDialogShell().layout();
        getDialogShell().pack();       

How to show up an image with swt in java?


The simplest way to create an SWT Image is to load it from a recognized graphic file format. This includes GIF, BMP (Windows format bitmap), JPG, and PNG. The TIFF format is also supported in more recent Eclipse releases.
Images can be loaded from a known location in the file system using the constructor
Image(Display display, String fileLocation):
Eg:
Image image = new Image(display,
   "C:/eclipse/eclipse/plugins/org.eclipse.platform_2.0.2/eclipse_lg.gif");
Instead of hard-coding the location of the image, it's more common to load the Image from a folder location relative to a given class. This is done by creating an InputStream pointing to the file with the method
Class.getResourceAsStream(String name),
and using the result as the argument to the constructor
 Image(Display display, InputStream inputStream).
Eg: If in Eclipse package explorer the class com.foo.ShellWithButtonShowingEclipseLogo and the eclipse_lg.gif in the same folder.
To load the graphic from its location relative to the class.
Image image = new Image(display, 
    ShellWithButtonShowingEclipseLogo.class.getResourceAsStream(
      "eclipse_lg.gif"));
Then you can add it on your label as:
Label myLabel = new Label( shell, SWT.NONE );
myLabel.setImage( image );

Saturday, 15 June 2013

Resize UIImage to generate thumbnail in apple iOS app - Xcode.


+ (UIImage *)generatePhotoThumbnail:(UIImage *)image withSide:(CGFloat)ratio
{
// Create a thumbnail version of the image for the event object.
CGSize size = image.size;
CGSize croppedSize;

CGFloat offsetX = 0.0;
CGFloat offsetY = 0.0;
    
// check the size of the image, we want to make it
// a square with sides the size of the smallest dimension.
    // So clip the extra portion from x or y coordinate
if (size.width > size.height) {
offsetX = (size.height - size.width) / 2;
croppedSize = CGSizeMake(size.height, size.height);
} else {
offsetY = (size.width - size.height) / 2;
croppedSize = CGSizeMake(size.width, size.width);
}
    
// Crop the image before resize
CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
// Done cropping
    
// Resize the image
CGRect rect = CGRectMake(0.0, 0.0, ratio, ratio);
    
UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Done Resizing
    
return thumbnail;
}

NSUserDefaults storing persistent data in iOS App - Xcode.


We can use Sqlite, p-List, and NSFile for storing User Data.
But they are little complex, if we just want store small data we can use NSUserDefaults.
Note: The values are removed on app deletion and the values are not affected when an application is updated.



Eg://Set the values


[[NSUserDefaults standardUserDefaults] setBool:NO      forKey:@"myBoolKey"];

[[NSUserDefaults standardUserDefaults] setObject:@"myString" forKey:@"myStringKey"];

[[NSUserDefaults standardUserDefaults] setInteger:1      forKey:@"myIntegerKey"]; 


//Fetch the values

[[NSUserDefaults standardUserDefaults] boolForKey:@"myBoolKey"];

 [[NSUserDefaults standardUserDefaults]   valueForKey:@"myStringKey"];

[[NSUserDefaults standardUserDefaults] integerForKey:@"myIntegerKey"];



To remove/reset all the values:

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
To remove one the values:
 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"myKey"];






Friday, 14 June 2013

Check internet connectivity iOS.


+ (BOOL)connectedToInternet
{
//return NO; // force for offline testing
Reachability *hostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
return !(netStatus == NotReachable);
}