org.dynami.utils.UIUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.dynami.utils.UIUtils.java

Source

/*
 * Copyright 2013 Alessandro Atria
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dynami.utils;

import org.dynami.Activator;
import org.dynami.core.util.DUtils;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class UIUtils {

    public static final String SEARCH_HINT = "Search";

    public static Color ALERT = SWTResourceManager.getColor(SWT.COLOR_YELLOW);
    public static Color OK = SWTResourceManager.getColor(SWT.COLOR_WHITE);

    public static boolean checkValue(Text control) {
        return (control.getText() != null && !"".equals(control.getText()));
    }

    public static boolean checkValue(Combo control) {
        return (control.getSelectionIndex() >= 0 && !"".equals(control.getText()));
    }

    public static boolean checkValue(Spinner control) {
        return (control.getSelection() > 0 && !"".equals(control.getText()));
    }

    public static final boolean isValorized(final Text... controls) {
        boolean res = true;
        for (Text txt : controls) {
            if (!checkValue(txt)) {
                res = false;
                txt.setBackground(ALERT);
            } else {
                txt.setBackground(OK);
            }
        }
        return res;
    }

    public static final boolean isValorized(final Spinner... controls) {
        boolean res = true;
        for (Spinner txt : controls) {
            if (!checkValue(txt)) {
                res = false;
                txt.setBackground(ALERT);
            } else {
                txt.setBackground(OK);
            }
        }
        return res;
    }

    public static final boolean isValorized(final Combo... controls) {
        boolean res = true;
        for (Combo c : controls) {
            if (!checkValue(c)) {
                res = false;
                c.setBackground(ALERT);
            } else {
                c.setBackground(OK);
            }
        }
        return res;
    }

    public static final boolean isDoubleText(final Text control) {
        boolean res = true;
        if (!DUtils.isDouble(control.getText())) {
            res = false;
            control.setBackground(ALERT);
        } else {
            control.setBackground(OK);
        }
        return res;
    }

    public static void errorDialog(final Shell shell, final String title, final Exception e) {
        System.out.println("#########################UIUtils.errorDialog()############################");
        e.printStackTrace();
        ErrorDialog.openError(shell, title, e.toString(), new Status(Status.ERROR, Activator.PLUGIN_ID,
                (e.getCause() != null) ? e.getCause().getMessage() : e.getMessage(), e));
    }

    private static final Color RED = SWTResourceManager.getColor(SWT.COLOR_RED);
    private static final Color GREEN = SWTResourceManager.getColor(SWT.COLOR_GREEN);

    public static void colorize(final ViewerCell cell, final Pair p) {
        if (p.value.doubleValue() >= 0.0) {
            cell.setBackground(GREEN);
        } else {
            cell.setBackground(RED);
        }
    }

    public static class Pair implements Comparable<String> {
        private final String name;
        private Number value;

        public Pair(String name, Number value) {
            this.name = name;
            this.value = value;
        }

        public Number getValue() {
            return value;
        }

        public void setValue(Number value) {
            this.value = value;
        }

        public String getName() {
            return name;
        }

        @Override
        public int compareTo(String o) {
            return name.compareTo(o);
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Pair) {
                return name.equals(((Pair) obj).name);
            } else {
                return super.equals(obj);
            }
        }
    }
}