[Title] Added SWTUtil
authorchanghyun1.lee <changhyun1.lee@samsung.com>
Fri, 22 Feb 2013 12:24:59 +0000 (21:24 +0900)
committerchanghyun1.lee <changhyun1.lee@samsung.com>
Fri, 22 Feb 2013 12:33:23 +0000 (21:33 +0900)
[Desc.]
[Issue]

Change-Id: Ib28e113454ee10ee277fa3e09486c90fab597639

org.tizen.common/src/org/tizen/common/util/SWTUtil.java

index 4e2f9bb..89cf846 100755 (executable)
@@ -55,6 +55,8 @@ import org.eclipse.swt.events.PaintEvent;
 import org.eclipse.swt.events.PaintListener;
 import org.eclipse.swt.events.VerifyEvent;
 import org.eclipse.swt.events.VerifyListener;
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.FontData;
 import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.graphics.Point;
@@ -62,12 +64,14 @@ import org.eclipse.swt.graphics.Resource;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Group;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Spinner;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.Tree;
@@ -85,16 +89,16 @@ import org.tizen.common.ui.TableToolTipListener;
 
 @SuppressWarnings("restriction")
 public class SWTUtil {
-       
-       private SWTUtil() {}
 
-       // 2012-04-25
-       @Deprecated
+    private SWTUtil() {}
+
+    // 2012-04-25
+    @Deprecated
     public static final String HTML_EXTENSIONS[] = { "html", "htm", "shtml", "shtm", "xhtml"};
-       
-    protected static final Collection<?> HTML_EXTENSIONS2 =
-       Collections.unmodifiableCollection( new HashSet<Object>( Arrays.asList( HTML_EXTENSIONS ) ) );
     
+    protected static final Collection<?> HTML_EXTENSIONS2 =
+        Collections.unmodifiableCollection( new HashSet<Object>( Arrays.asList( HTML_EXTENSIONS ) ) );
+
     /**
      * return {@link Display} in context
      * 
@@ -107,15 +111,15 @@ public class SWTUtil {
      */
     public static Display getDisplay()
     {
-       Display dp = Display.getCurrent();
-       if ( null != dp )
-       {
-               return dp;
-       }
-       
-       return Display.getDefault();
+        Display dp = Display.getCurrent();
+        if ( null != dp )
+        {
+            return dp;
+        }
+
+        return Display.getDefault();
     }
-    
+
     /**
      * <p>
      * Return active {@link Shell} in context
@@ -126,15 +130,15 @@ public class SWTUtil {
      */
     public static Shell getActiveShell()
     {
-       final Display dp = getDisplay();
-       if ( null == dp )
-       {
-               return null;
-       }
-       
-       return dp.getActiveShell();
+        final Display dp = getDisplay();
+        if ( null == dp )
+        {
+            return null;
+        }
+
+        return dp.getActiveShell();
     }
-    
+
     /**
      * execute {@link Cabinet#run()} and reeturn the result
      *  
@@ -153,7 +157,7 @@ public class SWTUtil {
         
         return runnable.getData();
     }
-    
+
     /**
      * execute {@link Runnable} in SWT thread synchronously
      * 
@@ -161,9 +165,9 @@ public class SWTUtil {
      */
     public static void syncExec( final Runnable runnable )
     {
-       syncExec( getDisplay(), runnable );
+        syncExec( getDisplay(), runnable );
     }
-    
+
     /**
      * execute {@link Runnable} in SWT thread synchronously
      *
@@ -172,9 +176,9 @@ public class SWTUtil {
      */
     public static void syncExec( final Display dp, final Runnable runnable )
     {
-       dp.syncExec( runnable );
+        dp.syncExec( runnable );
     }
-    
+
     /**
      * execute {@link Runnable} in SWT thread asynchronously
      * 
@@ -182,9 +186,9 @@ public class SWTUtil {
      */
     public static void asyncExec( final Runnable runnable )
     {
-       asyncExec( getDisplay(), runnable );
+        asyncExec( getDisplay(), runnable );
     }
-    
+
     /**
      * execute {@link Runnable} in SWT thread asynchronously
      * 
@@ -193,9 +197,9 @@ public class SWTUtil {
      */
     public static void asyncExec( final Display dp, final Runnable runnable )
     {
-       dp.asyncExec( runnable );
+        dp.asyncExec( runnable );
     }
-    
+
     /**
      * dispose {@link Widget}s
      * 
@@ -203,20 +207,20 @@ public class SWTUtil {
      */
     public static void tryDispose( final Widget... disposables )
     {
-       if ( null == disposables )
-       {
-               return ;
-       }
-       
-       for ( final Widget disposable : disposables )
-       {
-               if ( null == disposable || disposable.isDisposed() )
-               {
-                       continue;
-               }
-               
-               disposable.dispose();
-       }
+        if ( null == disposables )
+        {
+            return ;
+        }
+
+        for ( final Widget disposable : disposables )
+        {
+            if ( null == disposable || disposable.isDisposed() )
+            {
+                continue;
+            }
+
+            disposable.dispose();
+        }
     }
 
     /**
@@ -296,7 +300,7 @@ public class SWTUtil {
 
         return sc;
     }
-    
+
     /**
      * Get current active page.
      * 
@@ -310,7 +314,7 @@ public class SWTUtil {
         
         if ( null == window )
         {
-               return null;
+            return null;
         }
         return window.getActivePage();
     }
@@ -330,7 +334,7 @@ public class SWTUtil {
 
         return page.getActiveEditor();
     }
-    
+
     /**
      * Get editor of file extension.
      *
@@ -436,7 +440,7 @@ public class SWTUtil {
 
         return projectPath;
     }
-    
+
     public static void addTableToolTipListener(Table table, TableToolTipListener listener) {
         listener.setTable(table);
         table.addListener(SWT.MouseMove, listener);
@@ -444,7 +448,7 @@ public class SWTUtil {
         table.addListener(SWT.Dispose, listener);
         table.addListener(SWT.MouseHover, listener);
     }
-    
+
     /**
      * Expands tree and its sub treeitems.
      *
@@ -464,11 +468,11 @@ public class SWTUtil {
         Stack<TreeItem> treeStack = new Stack<TreeItem>();
         treeStack.add(treeItem);
         TreeItem selectedItem = null;
-        
+
         do {
             selectedItem = treeStack.pop();
             selectedItem.setExpanded(true);
-            
+
             if(selectedItem.getItems() != null && selectedItem.getItemCount() != 0) {
                 for (TreeItem childItem : selectedItem.getItems()) {
                     treeStack.add(childItem);
@@ -505,7 +509,7 @@ public class SWTUtil {
         }
         c.setLayoutData(gd);
     }
-    
+
     /**
      * Make error dialog which prints stack trace of the throwable 
      *
@@ -533,7 +537,7 @@ public class SWTUtil {
         
         ErrorDialog.openError(shell, "Error Dialog", msg, ms);
     }
-    
+
     public static Composite createCompositeEx(Composite parent, int numColumns, int layoutMode) {
         Composite composite = new Composite(parent, SWT.NULL);
         composite.setFont(parent.getFont());
@@ -561,7 +565,7 @@ public class SWTUtil {
         gd.horizontalSpan = columnSpan;
         label.setLayoutData(gd);
     }
-    
+
     /**
      * Returns a width hint for a button control.
      * @param button the button
@@ -573,7 +577,7 @@ public class SWTUtil {
         int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
         return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
     }
-    
+
     /**
      * make background image of child control transient.
      * Actually, it just copy parent image and paint it to the child background.
@@ -587,9 +591,9 @@ public class SWTUtil {
             final Object lock = new Object();
             final AtomicBoolean finished = new AtomicBoolean(false);
             parent.addPaintListener(new PaintListener() {
-                
+
                 class PairControl {
-                    
+
                     PairControl(Composite parent, Control child) {
                         this.parent = parent;
                         this.child = child;
@@ -597,7 +601,7 @@ public class SWTUtil {
                     Composite parent;
                     Control child;
                 }
-                
+
                 @Override
                 public void paintControl(PaintEvent e) {
                     if(finished.get()) {
@@ -607,20 +611,20 @@ public class SWTUtil {
                         finished.set(true);
                         parent.removePaintListener(this);
                         if(parent.getBackgroundImage() != null) {
-                            
+
                             Stack<PairControl> childStack = new Stack<PairControl>();
-                            
+
                             for(Control childControl: parent.getChildren()) {
                                 childStack.add(new PairControl(parent, childControl));
                             }
-                            
+
                             while(!childStack.isEmpty()) {
                                 PairControl pair = childStack.pop();
                                 Control childControl = pair.child;
                                 Composite parentComposite = pair.parent;
                                 Image parentImage = parentComposite.getBackgroundImage();
                                 Point childLoc = childControl.getLocation();
-                                
+
                                 if(childControl instanceof Composite) {
                                     Composite childComposite = ((Composite)childControl);
                                     for(Control grandChildControl: childComposite.getChildren()) {
@@ -641,4 +645,138 @@ public class SWTUtil {
         }
     }
 
+    /**
+     * Constructs a new instance of Combo class given its parent, label, tooltip and combo items.
+     * @param parent a composite control which will be the parent of the new instance (cannot be null)
+     * @param label the new text
+     * @param tooltip the new text 
+     * @param contents the list of strings for the combo box
+     * 
+     * @return Combo
+     * 
+     * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
+     */
+    public static Combo createCombo(Composite parent, String label, String tooltip, String[] contents) {
+        return createCombo(parent, label, tooltip, contents, 0);
+    }
+
+    /**
+     * Constructs a new instance of Combo class given its parent, label, tooltip, combo items and selectedIndex.
+     * @param parent a composite control which will be the parent of the new instance (cannot be null)
+     * @param label the new text
+     * @param tooltip the new text 
+     * @param contents the list of strings for the combo box
+     * @param selectedIndex the index of the item to select
+     * 
+     * @return Combo
+     * 
+     * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
+     */
+    public static Combo createCombo( Composite parent, String label, String tooltip, String[] contents, int selectedIndex) {
+        Label l = new Label(parent, SWT.NONE);
+        l.setText(label);
+        l.setToolTipText(tooltip);
+        l.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
+
+        Combo combo  = new Combo(parent, SWT.READ_ONLY);
+        //Flowing piece of code prevents cutting combo box in Windows Vista/7
+        //It decreases font size by one, because with the current size combo
+        //box is to large and cut.
+        FontData[] fontData = combo.getFont().getFontData();
+        for (int i = 0; i < fontData.length; i++) {
+            fontData[i].setHeight(fontData[i].getHeight() - 1);
+        }
+
+        Font font = new Font(combo.getDisplay(), fontData);
+        combo.setFont(font);
+
+        for (int i = 0; i  < contents.length ; i++) {
+            combo.add(contents[i]);
+        }
+        combo.select(selectedIndex);
+        return combo;
+    }
+
+    /**
+     * Constructs a new instance of Combo class given its parent, label, tooltip, enum values and selectedIndex.
+     * @param parent a composite control which will be the parent of the new instance (cannot be null)
+     * @param label the new text
+     * @param tooltip the new text 
+     * @param contents the values of enum
+     * @param selectedIndex the index of the item to select
+     * 
+     * @return Combo
+     * 
+     * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
+     */
+    public static <E extends Enum<E>> Combo createEnumCombo( Composite parent, String label, String tooltip, E[] values, int selectedIndex) {
+        String contents[] = StringUtil.enumNameToStringArray(values);
+        for (int i = 0; i < contents.length; i++) {
+            contents[i] = contents[i].toLowerCase();
+        }
+        return createCombo(parent, label, tooltip, contents, selectedIndex);
+    }
+
+    /**
+     * Constructs a new instance of Spinner class given its parent, label and tooltip.
+     * @param parent a composite control which will be the parent of the new instance (cannot be null)
+     * @param label the new text
+     * @param tooltip the new text
+     * 
+     * @return Spinner
+     * 
+     * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
+     */
+    public static Spinner createSpinner(Composite parent, String label, String tooltip) {
+        Label l = new Label(parent, SWT.NONE);
+        l.setText(label);
+        l.setToolTipText(tooltip);
+        l.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
+
+        Spinner s = new Spinner(parent, SWT.BORDER);
+        return s;
+    }
+
+    /**
+     * Constructs a new instance of Spinner class given its parent, label and tooltip.
+     * @param parent a composite control which will be the parent of the new instance (cannot be null)
+     * @param label the new text
+     * @param tooltip the new text
+     * @param minimum the new minimum, which must be less than or equal to the current maximum
+     * @param maximum the new maximum, which must be greater than or equal to the current minimum
+     * 
+     * @return Spinner
+     * 
+     * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
+     */
+    public static Spinner createSpinner(Composite parent, String label, String tooltip, int minimum, int maximum) {
+        Spinner s = createSpinner(parent, label, tooltip);
+        s.setMinimum(minimum);
+        s.setMaximum(maximum);
+        return s;
+    }
+
+    /**
+     * Constructs a new instance of Text class given its parent, label, tooltip, horazontalSpan and verticalSpan.
+     * @param parent a composite control which will be the parent of the new instance (cannot be null)
+     * @param label the new text
+     * @param tooltip the new text
+     * @param horazontalSpan the number of column cells that the control will take up
+     * @param verticalSpan the number of row cells that the control will take up
+     * 
+     * @return Text
+     * 
+     * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
+     */
+    public static Text createText(Composite parent, String label, String tooltip, int horizontalSpan, int verticalSpan) {
+        Label l = new Label(parent, SWT.NONE);
+        l.setText(label);
+        l.setToolTipText(tooltip);
+        l.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
+
+        Text t = new Text(parent, SWT.BORDER);
+        t.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, horizontalSpan, verticalSpan));
+        return t;
+    }
+
 }