Example usage for org.eclipse.jface.dialogs Dialog convertWidthInCharsToPixels

List of usage examples for org.eclipse.jface.dialogs Dialog convertWidthInCharsToPixels

Introduction

In this page you can find the example usage for org.eclipse.jface.dialogs Dialog convertWidthInCharsToPixels.

Prototype

public static int convertWidthInCharsToPixels(FontMetrics fontMetrics, int chars) 

Source Link

Document

Returns the number of pixels corresponding to the width of the given number of characters.

Usage

From source file:org.eclipse.jubula.client.ui.utils.LayoutUtil.java

License:Open Source License

/**
 * creates a GridData for textfields with default layout, give a number of expected characters as a hint
 * @param control the m_text control, used to determine the FontMetrics
 * @param numChars number of characters textfield should contain
 * @return a new instance of GridData//from w  w  w . j  av  a 2s .co  m
 */
public static GridData createDefaultTextGridData(Control control, int numChars) {

    GridData result = new GridData();
    result.horizontalAlignment = TEXT_HORIZONTAL_ALIGNMENT;
    result.verticalAlignment = VERTICAL_ALIGNMENT;
    result.horizontalSpan = 1;
    result.verticalSpan = 1;
    result.widthHint = Dialog.convertWidthInCharsToPixels(getFontMetrics(control), numChars);
    return result;
}

From source file:org.eclipse.linuxtools.internal.valgrind.massif.MassifToolPage.java

License:Open Source License

private void createAllocFnControls(Composite top) {

    allocFnList = new List(top, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    FontMetrics fm = MassifPlugin.getFontMetrics(allocFnList);
    allocFnList.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fm, 50),
            Dialog.convertHeightInCharsToPixels(fm, 5)));

    Composite allocButtons = new Composite(top, SWT.NONE);
    GridLayout allocButtonsLayout = new GridLayout();
    allocButtonsLayout.marginWidth = allocButtonsLayout.marginHeight = 0;
    allocButtons.setLayout(allocButtonsLayout);
    allocButtons.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));

    Button newButton = new Button(allocButtons, SWT.PUSH);
    newButton.setText(Messages.getString("MassifToolPage.New")); //$NON-NLS-1$
    newButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    newButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            handleAllocNewButtonPressed();
            updateLaunchConfigurationDialog();
        }/*w w w  .  j  a v  a2 s. c  o m*/
    });

    Button removeButton = new Button(allocButtons, SWT.PUSH);
    removeButton.setText(Messages.getString("MassifToolPage.Remove")); //$NON-NLS-1$
    removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    removeButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            handleAllocRemoveButtonPressed();
            updateLaunchConfigurationDialog();
        }
    });
}

From source file:org.eclipse.linuxtools.internal.valgrind.massif.MassifToolPage.java

License:Open Source License

private void createIgnoreFnControls(Composite top) {

    ignoreFnList = new List(top, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    FontMetrics fm = MassifPlugin.getFontMetrics(ignoreFnList);
    ignoreFnList.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fm, 50),
            Dialog.convertHeightInCharsToPixels(fm, 5)));

    Composite ignoreButtons = new Composite(top, SWT.NONE);
    GridLayout ignoreButtonsLayout = new GridLayout();
    ignoreButtonsLayout.marginWidth = ignoreButtonsLayout.marginHeight = 0;
    ignoreButtons.setLayout(ignoreButtonsLayout);
    ignoreButtons.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));

    Button newButton = new Button(ignoreButtons, SWT.PUSH);
    newButton.setText(Messages.getString("MassifToolPage.New")); //$NON-NLS-1$
    newButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    newButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            handleIgnoreNewButtonPressed();
            updateLaunchConfigurationDialog();
        }//from   w w  w. j av a  2  s  . c  o m
    });

    Button removeButton = new Button(ignoreButtons, SWT.PUSH);
    removeButton.setText(Messages.getString("MassifToolPage.Remove")); //$NON-NLS-1$
    removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    removeButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            handleIgnoreRemoveButtonPressed();
            updateLaunchConfigurationDialog();
        }
    });
}

From source file:org.eclipse.linuxtools.internal.valgrind.memcheck.MemcheckToolPage.java

License:Open Source License

private void createIgnoreRangesControls(Composite top) {

    ignoreRangesList = new List(top, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    FontMetrics fm = MemcheckPlugin.getFontMetrics(ignoreRangesList);
    ignoreRangesList.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fm, 50),
            Dialog.convertHeightInCharsToPixels(fm, 5)));

    Composite ignoreButtons = new Composite(top, SWT.NONE);
    GridLayout ignoreButtonsLayout = new GridLayout();
    ignoreButtonsLayout.marginWidth = ignoreButtonsLayout.marginHeight = 0;
    ignoreButtons.setLayout(ignoreButtonsLayout);
    ignoreButtons.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));

    Button newButton = new Button(ignoreButtons, SWT.PUSH);
    newButton.setText(Messages.getString("MemcheckToolPage.New")); //$NON-NLS-1$
    newButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    newButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            handleIgnoreNewButtonPressed();
            updateLaunchConfigurationDialog();
        }//from www  .j  a v a  2 s.c om
    });

    Button removeButton = new Button(ignoreButtons, SWT.PUSH);
    removeButton.setText(Messages.getString("MemcheckToolPage.Remove")); //$NON-NLS-1$
    removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    removeButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            handleIgnoreRemoveButtonPressed();
            updateLaunchConfigurationDialog();
        }
    });

}

From source file:org.eclipse.pde.api.tools.ui.internal.preferences.ApiErrorsWarningsConfigurationBlock.java

License:Open Source License

public int convertWidthInCharsToPixels(Font font, int chars) {
    GC gc = new GC(font.getDevice());
    gc.setFont(font);/*from ww w  .ja va  2  s  . c o m*/
    FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();
    return Dialog.convertWidthInCharsToPixels(fontMetrics, chars);
}

From source file:org.eclipse.php.internal.ui.preferences.AbstractPHPPreferenceBlock.java

License:Open Source License

/**
 * Add text box with label with input validator
 *//*  w ww .j ava  2  s.c o  m*/
protected Text addLabelledTextField(Composite parent, String label, String key, int textlimit,
        int horizontalIndent, IStringValidator stringValidator) {
    Label labelControl = new Label(parent, SWT.WRAP);
    labelControl.setText(label);
    labelControl.setData(key);
    GridData data = new GridData();
    data.horizontalIndent = horizontalIndent;
    labelControl.setLayoutData(data);
    add(labelControl);

    Text textBox = new Text(parent, SWT.BORDER | SWT.SINGLE);
    textBox.setData(key);
    data = new GridData();
    if (textlimit != 0) {
        textBox.setTextLimit(textlimit);
        data.widthHint = Dialog.convertWidthInCharsToPixels(getFontMetrics(parent), textlimit + 1);
    }
    data.horizontalSpan = 2;
    textBox.setLayoutData(data);

    if (stringValidator != null) {
        textBox.addModifyListener(new TextFieldValidateListener(stringValidator));
    }
    add(textBox);
    return textBox;
}

From source file:org.eclipse.tcf.te.ui.swt.SWTControlUtil.java

License:Open Source License

/**
 * Returns the number of pixels corresponding to the width of the given
 * number of characters.//from  ww w . j  a  v  a  2  s . c o  m
 * <p>
 * This methods uses the static {@link Dialog#convertWidthInCharsToPixels(org.eclipse.swt.graphics.FontMetrics, int)}
 * method for calculation.
 * <p>
 * @param chars The number of characters
 * @return The corresponding width in pixels
 */
public static int convertWidthInCharsToPixels(Control control, int chars) {
    int width = 0;
    if (control != null && !control.isDisposed()) {
        GC gc = new GC(control);
        gc.setFont(JFaceResources.getDialogFont());
        width = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(), chars);
        gc.dispose();
    }

    return width;
}

From source file:org.eclipse.tm.terminal.view.ui.controls.NoteCompositeHelper.java

License:Open Source License

/**
 * Creates a composite with a highlighted Note entry and a message text.
 * This is designed to take up the full width of the page.
 *
 * @see PreferencePage#createNoteComposite, this is a plain copy of that!
 * @param font//from   w  ww.j av  a  2s . co  m
 *            the font to use
 * @param composite
 *            the parent composite
 * @param title
 *            the title of the note
 * @param message
 *            the message for the note
 * @param minCharsPerLine
 *            the minimum number of characters per line. Defaults to '65' if less than '20'.
 *
 * @return the composite for the note
 */
public static Composite createNoteComposite(Font font, Composite composite, String title, String message,
        int minCharsPerLine) {
    final GC gc = new GC(composite);
    gc.setFont(font);

    Composite messageComposite = new NoteComposite(composite, SWT.NONE);

    GridLayout messageLayout = new GridLayout();
    messageLayout.numColumns = 2;
    messageLayout.marginWidth = 0;
    messageLayout.marginHeight = 0;
    messageComposite.setLayout(messageLayout);

    GridData layoutData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    if (composite.getLayout() instanceof GridLayout) {
        layoutData.horizontalSpan = ((GridLayout) composite.getLayout()).numColumns;
    }
    messageComposite.setLayoutData(layoutData);
    messageComposite.setFont(font);

    final Label noteLabel = new Label(messageComposite, SWT.BOLD);
    noteLabel.setText(title);
    noteLabel.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
    noteLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));

    final IPropertyChangeListener fontListener = new IPropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent event) {
            // Note: This is actually wrong but the same as in platforms
            // PreferencePage
            if (JFaceResources.BANNER_FONT.equals(event.getProperty())) {
                noteLabel.setFont(JFaceResources.getFont(JFaceResources.BANNER_FONT));
            }
        }
    };
    JFaceResources.getFontRegistry().addListener(fontListener);
    noteLabel.addDisposeListener(new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent event) {
            JFaceResources.getFontRegistry().removeListener(fontListener);
        }
    });

    Label messageLabel = new Label(messageComposite, SWT.WRAP);
    messageLabel.setText(message);
    messageLabel.setFont(font);

    /**
     * Set the controls style to FILL_HORIZONTAL making it multi-line if
     * needed
     */
    layoutData = new GridData(GridData.FILL_HORIZONTAL);
    layoutData.widthHint = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(),
            minCharsPerLine >= 20 ? minCharsPerLine : 65);
    messageLabel.setLayoutData(layoutData);

    gc.dispose();

    return messageComposite;
}

From source file:org.eclipse.tm.terminal.view.ui.preferences.PreferencePage.java

License:Open Source License

@Override
protected Control createContents(final Composite parent) {
    final GC gc = new GC(parent);
    gc.setFont(JFaceResources.getDialogFont());

    Composite panel = new Composite(parent, SWT.NONE);
    panel.setLayout(new GridLayout());
    GridData layoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    panel.setLayoutData(layoutData);/*www. j  av a  2 s .c o m*/

    Label label = new Label(panel, SWT.HORIZONTAL);
    label.setText(Messages.PreferencePage_label);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    if (!Platform.OS_WIN32.equals(Platform.getOS())) {
        Group group = new Group(panel, SWT.NONE);
        group.setText(Messages.PreferencePage_command_label);
        group.setLayout(new GridLayout(2, false));
        group.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

        command = new Text(group, SWT.SINGLE | SWT.BORDER);
        command.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        command.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                boolean valid = true;
                String message = null;

                String text = command.getText();
                if (text != null && !"".equals(text.trim())) { //$NON-NLS-1$
                    IPath p = new Path(text.trim());
                    valid = p.toFile().isFile() && p.toFile().canRead() && p.toFile().canExecute();
                    if (!valid)
                        message = Messages.PreferencePage_command_invalid;
                }

                setValid(valid);
                setErrorMessage(message);
            }
        });

        commandBrowseButton = new Button(group, SWT.PUSH);
        commandBrowseButton.setText(Messages.PreferencePage_command_button_browse);
        layoutData = new GridData(SWT.FILL, SWT.CENTER, false, false);
        layoutData.widthHint = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(), 14);
        commandBrowseButton.setLayoutData(layoutData);
        commandBrowseButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                FileDialog dialog = new FileDialog(parent.getShell(), SWT.OPEN);

                String text = command.getText();
                if (text != null && !"".equals(text.trim())) { //$NON-NLS-1$
                    IPath p = new Path(text);

                    if (p.toFile().isFile() || !p.toFile().exists()) {
                        dialog.setFilterPath(p.removeLastSegments(1).toOSString());
                        dialog.setFileName(p.lastSegment());
                    } else if (p.toFile().isDirectory()) {
                        dialog.setFilterPath(p.toOSString());
                    }
                }

                String selected = dialog.open();
                if (selected != null) {
                    IPath sp = new Path(selected);
                    command.setText(sp.toOSString());
                }
            }
        });

        String cmd = UIPlugin.getScopedPreferences()
                .getString(IPreferenceKeys.PREF_LOCAL_TERMINAL_DEFAULT_SHELL_UNIX);
        if (cmd != null && !"".equals(cmd)) { //$NON-NLS-1$
            command.setText(new Path(cmd).toOSString());
        }

        Composite argsPanel = new Composite(group, SWT.NONE);
        GridLayout layout = new GridLayout(2, false);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        argsPanel.setLayout(layout);
        layoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
        layoutData.horizontalSpan = 2;
        argsPanel.setLayoutData(layoutData);

        label = new Label(argsPanel, SWT.NONE);
        label.setText(Messages.PreferencePage_command_arguments_label);

        arguments = new Text(argsPanel, SWT.SINGLE | SWT.BORDER);
        arguments.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        String args = UIPlugin.getScopedPreferences()
                .getString(IPreferenceKeys.PREF_LOCAL_TERMINAL_DEFAULT_SHELL_UNIX_ARGS);
        if (args != null && !"".equals(args)) { //$NON-NLS-1$
            arguments.setText(args);
        }

        NoteCompositeHelper.createNoteComposite(group.getFont(), group,
                Messages.PreferencePage_command_note_label, Messages.PreferencePage_command_note_text);
    }

    Group group = new Group(panel, SWT.NONE);
    group.setText(Messages.PreferencePage_workingDir_label);
    group.setLayout(new GridLayout(hasVariablesButton ? 3 : 2, false));
    group.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    workingDir = new Combo(group, SWT.DROP_DOWN);
    Bundle bundle = Platform.getBundle("org.eclipse.core.resources"); //$NON-NLS-1$
    if (bundle != null && bundle.getState() != Bundle.UNINSTALLED && bundle.getState() != Bundle.STOPPING) {
        workingDir.setItems(new String[] { Messages.PreferencePage_workingDir_userhome_label,
                Messages.PreferencePage_workingDir_eclipsehome_label,
                Messages.PreferencePage_workingDir_eclipsews_label });
    } else {
        workingDir.setItems(new String[] { Messages.PreferencePage_workingDir_userhome_label,
                Messages.PreferencePage_workingDir_eclipsehome_label });
    }
    workingDir.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    workingDir.select(0);
    workingDir.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e) {
            boolean valid = true;
            String message = null;

            String text = workingDir.getText();
            if (text != null && !"".equals(text.trim()) //$NON-NLS-1$
                    && !Messages.PreferencePage_workingDir_userhome_label.equals(text)
                    && !Messages.PreferencePage_workingDir_eclipsehome_label.equals(text)
                    && !Messages.PreferencePage_workingDir_eclipsews_label.equals(text)) {
                try {
                    // Resolve possible dynamic variables
                    IStringVariableManager vm = VariablesPlugin.getDefault().getStringVariableManager();
                    String resolved = vm.performStringSubstitution(text.trim());

                    IPath p = new Path(resolved);
                    valid = p.toFile().canRead() && p.toFile().isDirectory();
                    if (!valid)
                        message = Messages.PreferencePage_workingDir_invalid;
                } catch (CoreException ex) {
                    valid = false;
                    message = ex.getLocalizedMessage();
                }
            }

            setValid(valid);
            setErrorMessage(message);
        }
    });

    browseButton = new Button(group, SWT.PUSH);
    browseButton.setText(Messages.PreferencePage_workingDir_button_browse);
    layoutData = new GridData(SWT.FILL, SWT.CENTER, false, false);
    layoutData.widthHint = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(), 14);
    browseButton.setLayoutData(layoutData);
    browseButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IPath uh = null;
            IPath eh = null;
            IPath ew = null;

            // HOME
            String home = System.getProperty("user.home"); //$NON-NLS-1$
            if (home != null && !"".equals(home)) //$NON-NLS-1$
                uh = new Path(home);

            // ECLIPSE_HOME
            String eclipseHomeLocation = System.getProperty("eclipse.home.location"); //$NON-NLS-1$
            if (eclipseHomeLocation != null) {
                try {
                    URI uri = URIUtil.fromString(eclipseHomeLocation);
                    File f = URIUtil.toFile(uri);
                    eh = new Path(f.getAbsolutePath());
                } catch (URISyntaxException ex) {
                    /* ignored on purpose */ }
            }

            // ECLIPSE_WORKSPACE
            Bundle bundle = Platform.getBundle("org.eclipse.core.resources"); //$NON-NLS-1$
            if (bundle != null && bundle.getState() != Bundle.UNINSTALLED
                    && bundle.getState() != Bundle.STOPPING) {
                if (org.eclipse.core.resources.ResourcesPlugin.getWorkspace() != null
                        && org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot() != null
                        && org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot()
                                .getLocation() != null) {
                    ew = org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getLocation();
                }
            }

            DirectoryDialog dialog = new DirectoryDialog(parent.getShell(), SWT.OPEN);

            // Determine the filter path
            String text = workingDir.getText();
            if (Messages.PreferencePage_workingDir_userhome_label.equals(text)) {
                dialog.setFilterPath(uh.toOSString());
            } else if (Messages.PreferencePage_workingDir_eclipsehome_label.equals(text)) {
                dialog.setFilterPath(eh.toOSString());
            } else if (Messages.PreferencePage_workingDir_eclipsews_label.equals(text)) {
                dialog.setFilterPath(ew.toOSString());
            } else if (text != null && !"".equals(text.trim())) { //$NON-NLS-1$
                try {
                    // Resolve possible dynamic variables
                    IStringVariableManager vm = VariablesPlugin.getDefault().getStringVariableManager();
                    String resolved = vm.performStringSubstitution(text.trim());
                    dialog.setFilterPath(resolved);
                } catch (CoreException ex) {
                    if (Platform.inDebugMode()) {
                        UIPlugin.getDefault().getLog().log(ex.getStatus());
                    }
                }
            }

            String selected = dialog.open();
            if (selected != null) {
                IPath sp = new Path(selected);

                if (uh.equals(sp)) {
                    workingDir.select(0);
                } else if (eh.equals(sp)) {
                    workingDir.select(1);
                } else if (ew.equals(sp)) {
                    workingDir.select(2);
                } else {
                    workingDir.setText(sp.toOSString());
                }
            }
        }
    });

    if (hasVariablesButton) {
        variablesButton = new Button(group, SWT.PUSH);
        variablesButton.setText(Messages.PreferencePage_workingDir_button_variables);
        layoutData = new GridData(SWT.FILL, SWT.CENTER, false, false);
        layoutData.widthHint = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(), 14);
        variablesButton.setLayoutData(layoutData);
        variablesButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                org.eclipse.debug.ui.StringVariableSelectionDialog dialog = new org.eclipse.debug.ui.StringVariableSelectionDialog(
                        getShell());
                dialog.open();
                String expression = dialog.getVariableExpression();
                if (expression != null) {
                    if ("${eclipse_home}".equals(expression)) { //$NON-NLS-1$
                        workingDir.select(1);
                    } else if ("${workspace_loc}".equals(expression)) { //$NON-NLS-1$
                        workingDir.select(2);
                    } else {
                        workingDir.setText(expression);
                    }
                }
            }
        });
    }

    String initialCwd = UIPlugin.getScopedPreferences()
            .getString(IPreferenceKeys.PREF_LOCAL_TERMINAL_INITIAL_CWD);
    if (initialCwd == null || IPreferenceKeys.PREF_INITIAL_CWD_USER_HOME.equals(initialCwd)
            || "".equals(initialCwd.trim())) { //$NON-NLS-1$
        workingDir.select(0);
    } else if (IPreferenceKeys.PREF_INITIAL_CWD_ECLIPSE_HOME.equals(initialCwd)
            || "${eclipse_home}".equals(initialCwd)) { //$NON-NLS-1$
        workingDir.select(1);
    } else if (IPreferenceKeys.PREF_INITIAL_CWD_ECLIPSE_WS.equals(initialCwd)
            || "${workspace_loc}".equals(initialCwd)) { //$NON-NLS-1$
        workingDir.select(2);
    } else {
        workingDir.setText(new Path(initialCwd).toOSString());
    }

    NoteCompositeHelper.createNoteComposite(group.getFont(), group,
            Messages.PreferencePage_workingDir_note_label, Messages.PreferencePage_workingDir_note_text);

    group = new Group(panel, SWT.NONE);
    group.setText(Messages.PreferencePage_executables_label);
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    viewer = new TableViewer(group, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);

    Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn column = new TableColumn(table, SWT.LEFT);
    column.setText(Messages.PreferencePage_executables_column_name_label);
    column = new TableColumn(table, SWT.LEFT);
    column.setText(Messages.PreferencePage_executables_column_path_label);

    ColumnViewerToolTipSupport.enableFor(viewer);

    TableLayout tableLayout = new TableLayout();
    tableLayout.addColumnData(new ColumnWeightData(35));
    tableLayout.addColumnData(new ColumnWeightData(65));
    table.setLayout(tableLayout);

    layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
    layoutData.heightHint = Dialog.convertHeightInCharsToPixels(gc.getFontMetrics(), 10);
    table.setLayoutData(layoutData);

    Composite buttonsPanel = new Composite(group, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    buttonsPanel.setLayout(layout);
    buttonsPanel.setLayoutData(new GridData(SWT.LEAD, SWT.BEGINNING, false, false));

    addButton = new Button(buttonsPanel, SWT.PUSH);
    addButton.setText(Messages.PreferencePage_executables_button_add_label);
    layoutData = new GridData(SWT.FILL, SWT.CENTER, false, false);
    layoutData.widthHint = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(), 10);
    addButton.setLayoutData(layoutData);
    addButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            ExternalExecutablesDialog dialog = new ExternalExecutablesDialog(PreferencePage.this.getShell(),
                    false);
            if (dialog.open() == Window.OK) {
                // Get the executable properties and add it to the the list
                Map<String, String> executableData = dialog.getExecutableData();
                if (executableData != null && !executables.contains(executableData)) {
                    executables.add(executableData);
                    viewer.refresh();
                }
            }
        }
    });

    editButton = new Button(buttonsPanel, SWT.PUSH);
    editButton.setText(Messages.PreferencePage_executables_button_edit_label);
    layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
    layoutData.widthHint = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(), 10);
    editButton.setLayoutData(layoutData);
    editButton.addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection s = viewer.getSelection();
            if (s instanceof IStructuredSelection && !s.isEmpty()) {
                Object element = ((IStructuredSelection) s).getFirstElement();
                if (element instanceof Map) {
                    final Map<String, String> m = (Map<String, String>) element;
                    ExternalExecutablesDialog dialog = new ExternalExecutablesDialog(
                            PreferencePage.this.getShell(), true);
                    dialog.setExecutableData(m);
                    if (dialog.open() == Window.OK) {
                        Map<String, String> executableData = dialog.getExecutableData();
                        if (executableData != null) {
                            m.clear();
                            m.putAll(executableData);
                            viewer.refresh();
                        }
                    }
                }
            }
        }
    });

    removeButton = new Button(buttonsPanel, SWT.PUSH);
    removeButton.setText(Messages.PreferencePage_executables_button_remove_label);
    layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
    layoutData.widthHint = Dialog.convertWidthInCharsToPixels(gc.getFontMetrics(), 10);
    removeButton.setLayoutData(layoutData);
    removeButton.addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection s = viewer.getSelection();
            if (s instanceof IStructuredSelection && !s.isEmpty()) {
                Iterator<?> iterator = ((IStructuredSelection) s).iterator();
                while (iterator.hasNext()) {
                    Object element = iterator.next();
                    if (element instanceof Map) {
                        Map<String, Object> m = (Map<String, Object>) element;
                        executables.remove(m);
                    }
                    viewer.refresh();
                }
            }
        }
    });

    viewer.setContentProvider(new IStructuredContentProvider() {
        @Override
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof List && !((List<?>) inputElement).isEmpty()) {
                return ((List<?>) inputElement).toArray();
            }
            return NO_ELEMENTS;
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @Override
        public void dispose() {
        }
    });

    viewer.setLabelProvider(new ITableLabelProvider() {
        @SuppressWarnings("unchecked")
        @Override
        public String getColumnText(Object element, int columnIndex) {
            if (element instanceof Map) {
                Map<String, Object> m = (Map<String, Object>) element;

                switch (columnIndex) {
                case 0:
                    return (String) m.get(IExternalExecutablesProperties.PROP_NAME);
                case 1:
                    return (String) m.get(IExternalExecutablesProperties.PROP_PATH);
                }
            }
            return null;
        }

        @SuppressWarnings("unchecked")
        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            Image i = null;

            if (element instanceof Map) {
                switch (columnIndex) {
                case 0:
                    Map<String, Object> m = (Map<String, Object>) element;
                    String icon = (String) m.get(IExternalExecutablesProperties.PROP_ICON);
                    if (icon != null) {
                        i = images.get(icon);
                        if (i == null) {
                            ImageData id = ExternalExecutablesManager.loadImage(icon);
                            if (id != null) {
                                ImageDescriptor d = ImageDescriptor.createFromImageData(id);
                                if (d != null)
                                    i = d.createImage();
                                if (i != null)
                                    images.put(icon, i);
                            }
                        }
                    }
                    break;
                case 1:
                    break;
                }
            }

            return i;
        }

        @Override
        public void removeListener(ILabelProviderListener listener) {
        }

        @Override
        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        @Override
        public void dispose() {
        }

        @Override
        public void addListener(ILabelProviderListener listener) {
        }
    });

    List<Map<String, String>> l = ExternalExecutablesManager.load();
    if (l != null)
        executables.addAll(l);

    viewer.setInput(executables);

    viewer.addPostSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            updateButtons();
        }
    });

    updateButtons();

    gc.dispose();

    return panel;
}

From source file:org.eclipse.ui.ide.markers.compatibility.internal.DescriptionConfigurationArea.java

License:Open Source License

/**
 * Create the group for the description filter.
 * //from w w w  . jav  a2s .c o  m
 * @param parent
 */
private void createDescriptionGroup(Composite parent) {

    Composite descriptionComposite = new Composite(parent, SWT.NONE);
    descriptionComposite.setLayout(new GridLayout(3, false));
    descriptionComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Label descriptionLabel = new Label(descriptionComposite, SWT.NONE);
    descriptionLabel.setText(MarkerMessages.filtersDialog_descriptionLabel);

    descriptionCombo = new Combo(descriptionComposite, SWT.READ_ONLY);
    descriptionCombo.add(MarkerMessages.filtersDialog_contains);
    descriptionCombo.add(MarkerMessages.filtersDialog_doesNotContain);

    // Prevent Esc and Return from closing the dialog when the combo is
    // active.
    descriptionCombo.addTraverseListener(new TraverseListener() {
        public void keyTraversed(TraverseEvent e) {
            if (e.detail == SWT.TRAVERSE_ESCAPE || e.detail == SWT.TRAVERSE_RETURN) {
                e.doit = false;
            }
        }
    });

    GC gc = new GC(descriptionComposite);
    gc.setFont(JFaceResources.getDialogFont());
    FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();

    descriptionText = new Text(descriptionComposite, SWT.SINGLE | SWT.BORDER);
    GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
    data.widthHint = Dialog.convertWidthInCharsToPixels(fontMetrics, 25);
    descriptionText.setLayoutData(data);
}