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;
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;
@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
*
*/
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
*/
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
*
return runnable.getData();
}
-
+
/**
* execute {@link Runnable} in SWT thread synchronously
*
*/
public static void syncExec( final Runnable runnable )
{
- syncExec( getDisplay(), runnable );
+ syncExec( getDisplay(), runnable );
}
-
+
/**
* execute {@link Runnable} in SWT thread synchronously
*
*/
public static void syncExec( final Display dp, final Runnable runnable )
{
- dp.syncExec( runnable );
+ dp.syncExec( runnable );
}
-
+
/**
* execute {@link Runnable} in SWT thread asynchronously
*
*/
public static void asyncExec( final Runnable runnable )
{
- asyncExec( getDisplay(), runnable );
+ asyncExec( getDisplay(), runnable );
}
-
+
/**
* execute {@link Runnable} in SWT thread asynchronously
*
*/
public static void asyncExec( final Display dp, final Runnable runnable )
{
- dp.asyncExec( runnable );
+ dp.asyncExec( runnable );
}
-
+
/**
* dispose {@link Widget}s
*
*/
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();
+ }
}
/**
return sc;
}
-
+
/**
* Get current active page.
*
if ( null == window )
{
- return null;
+ return null;
}
return window.getActivePage();
}
return page.getActiveEditor();
}
-
+
/**
* Get editor of file extension.
*
return projectPath;
}
-
+
public static void addTableToolTipListener(Table table, TableToolTipListener listener) {
listener.setTable(table);
table.addListener(SWT.MouseMove, listener);
table.addListener(SWT.Dispose, listener);
table.addListener(SWT.MouseHover, listener);
}
-
+
/**
* Expands tree and its sub treeitems.
*
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);
}
c.setLayoutData(gd);
}
-
+
/**
* Make error dialog which prints stack trace of the throwable
*
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());
gd.horizontalSpan = columnSpan;
label.setLayoutData(gd);
}
-
+
/**
* Returns a width hint for a button control.
* @param button the button
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.
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;
Composite parent;
Control child;
}
-
+
@Override
public void paintControl(PaintEvent e) {
if(finished.get()) {
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()) {
}
}
+ /**
+ * 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;
+ }
+
}