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.jboss.tools.windup.ui.internal.rules.delegate.JavaEmbeddedEditor.java

License:Open Source License

public Control createControls(Composite parent) {
    fViewer = new JDISourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.LEFT_TO_RIGHT);
    fViewer.setEditable(false);//from   w w  w. j  av a2 s  .co  m
    ControlDecoration decoration = new ControlDecoration(fViewer.getControl(), SWT.TOP | SWT.LEFT);
    decoration.setShowOnlyOnFocus(true);
    FieldDecoration dec = FieldDecorationRegistry.getDefault()
            .getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
    decoration.setImage(dec.getImage());
    decoration.setDescriptionText(dec.getDescription());
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    // set height/width hints based on font
    GC gc = new GC(fViewer.getTextWidget());
    gc.setFont(fViewer.getTextWidget().getFont());
    FontMetrics fontMetrics = gc.getFontMetrics();
    gd.heightHint = Dialog.convertHeightInCharsToPixels(fontMetrics, 17);
    gd.widthHint = Dialog.convertWidthInCharsToPixels(fontMetrics, 40);
    gc.dispose();
    fViewer.getControl().setLayoutData(gd);
    fContentAssistHandler = new AbstractHandler() {
        @Override
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
            return null;
        }
    };
    fUndoHandler = new AbstractHandler() {
        @Override
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ITextOperationTarget.UNDO);
            return null;
        }
    };
    fRedoHandler = new AbstractHandler() {
        @Override
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ITextOperationTarget.REDO);
            return null;
        }
    };
    fHandlerService = PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
    fViewer.getControl().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            activateHandlers();
        }

        @Override
        public void focusLost(FocusEvent e) {
            deactivateHandlers();
        }
    });
    parent.addDisposeListener(new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent e) {
            dispose();
        }
    });
    fViewer.getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {
        @Override
        public void verifyKey(VerifyEvent event) {
            fViewer.getTextWidget().getText();
        }
    });
    return parent;
}

From source file:org.key_project.sed.key.ui.launch.JavaSnippetSourceViewer.java

License:Open Source License

/**
 * Constructor.//w w w  .  j  a  v  a  2 s .c  om
 * @param parent The parent {@link Composite}.
 * @param style The style to use.
 */
public JavaSnippetSourceViewer(Composite parent, int style) {
    fViewer = new JDISourceViewer(parent, null, style);
    decorateViewer();

    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    // set height/width hints based on font
    GC gc = new GC(fViewer.getTextWidget());
    gc.setFont(fViewer.getTextWidget().getFont());
    FontMetrics fontMetrics = gc.getFontMetrics();
    //gd.heightHint = Dialog.convertHeightInCharsToPixels(fontMetrics, 10);
    gd.widthHint = Dialog.convertWidthInCharsToPixels(fontMetrics, 40);
    gc.dispose();
    getControl().setLayoutData(gd);

    fContentAssistHandler = new AbstractHandler() {
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
            return null;
        }
    };
    fUndoHandler = new AbstractHandler() {
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ITextOperationTarget.UNDO);
            return null;
        }
    };
    fRedoHandler = new AbstractHandler() {
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ITextOperationTarget.REDO);
            return null;
        }
    };

    fHandlerService = (IHandlerService) PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
    getControl().addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            activateHandlers();
        }

        public void focusLost(FocusEvent e) {
            deactivateHandlers();
        }
    });

    // Set new document
    IDocument document = new Document();
    document.addDocumentListener(fDocumentListener);
    JDIDebugUIPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document,
            IJavaPartitions.JAVA_PARTITIONING);
    fViewer.setInput(document);
}

From source file:org.org.eclipse.core.utils.platform.properties.AbstractTabItemDefinition.java

License:Open Source License

protected static Text createLabelAndTextField(Composite parent, String label, String toolTip,
        int textFieldWidth) {
    Composite composite = createDefaultComposite(parent);
    Label labelWidget = new Label(composite, SWT.NONE);
    labelWidget.setText(label);//w ww. j ava2  s  .c o  m
    labelWidget.setToolTipText(toolTip);
    Text textWidget = new Text(composite, SWT.SINGLE | SWT.BORDER);
    GridData gd = new GridData();
    gd.widthHint = Dialog.convertWidthInCharsToPixels(getFontMetrics(), textFieldWidth);
    textWidget.setLayoutData(gd);
    return textWidget;
}

From source file:org.sonar.ide.eclipse.ui.internal.views.issues.AssigneeConfigurationArea.java

License:Open Source License

/**
 * Create the group for the description filter.
 *
 * @param parent//from   www.  j ava  2s.co m
 */
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("Login:");

    descriptionCombo = new Combo(descriptionComposite, SWT.READ_ONLY);
    descriptionCombo.add("contains");
    descriptionCombo.add("doesn't contains");

    // 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);
}

From source file:org.springsource.ide.eclipse.commons.frameworks.ui.internal.wizard.GenericWizardCommandParametersPage.java

License:Open Source License

/**
 * Given a list of parameters, find the parameter name that is the longest,
 * as this will be used to compute the pixel equivalent width for the label
 * column./*from w w  w .  j a  v a2s .  c  o  m*/
 * 
 * @param parent
 * @param parameters
 * @return longest parameter name that is found in pixels.
 */
protected Point getLongestLabelSize() {

    if (longestLabelWidth != null) {
        return longestLabelWidth;
    }
    int length = SWT.DEFAULT;
    int charLength = 0;
    List<ICommandParameter> parameters = getCommand().getParameters();
    if (parameters != null) {
        for (ICommandParameter parameter : parameters) {
            // only compute label length of Base and Java editors
            ParameterKind kind = parameter.getParameterDescriptor().getParameterKind();
            if (kind != ParameterKind.BOOLEAN) {
                String name = parameter.getParameterDescriptor().getName();
                if (name != null) {
                    int nameLength = name.length();
                    if (nameLength > charLength) {
                        charLength = nameLength;
                    }
                }
            }
        }
    }
    if (charLength > 0) {
        Control control = getShell();
        GC gc = new GC(control);
        Font requiredLabelFont = getRequiredParameterFont();
        gc.setFont(requiredLabelFont != null ? requiredLabelFont : control.getFont());
        FontMetrics fontMetrics = gc.getFontMetrics();
        // Increment the length by a few pixels to cover colon that may be
        // appended
        length = Dialog.convertWidthInCharsToPixels(fontMetrics, charLength);
        gc.dispose();
    }
    longestLabelWidth = new Point(length, -1);
    longestLabelWidth.x += getLabelNameSeparatorOffset();
    return longestLabelWidth;
}

From source file:org.summer.sdt.debug.ui.breakpoints.JavaBreakpointConditionEditor.java

License:Open Source License

/**
 * Creates the condition editor widgets and returns the top level
 * control./*from   ww  w .ja  v a 2  s  .  com*/
 * 
 * @param parent composite to embed the editor controls in
 * @return top level control
 */
@Override
public Control createControl(Composite parent) {
    Composite controls = SWTFactory.createComposite(parent, parent.getFont(), 2, 1, GridData.FILL_HORIZONTAL, 0,
            0);
    fConditional = SWTFactory.createCheckButton(controls,
            processMnemonics(PropertyPageMessages.JavaBreakpointConditionEditor_0), null, false, 1);
    fConditional.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    fConditional.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            boolean checked = fConditional.getSelection();
            setEnabled(checked, true);
            setDirty(PROP_CONDITION_ENABLED);
        }
    });
    Composite radios = SWTFactory.createComposite(controls, controls.getFont(), 2, 1, GridData.FILL_HORIZONTAL,
            0, 0);
    fWhenTrue = SWTFactory.createRadioButton(radios,
            processMnemonics(PropertyPageMessages.JavaBreakpointConditionEditor_1));
    fWhenTrue.setLayoutData(new GridData());
    fWhenChange = SWTFactory.createRadioButton(radios,
            processMnemonics(PropertyPageMessages.JavaBreakpointConditionEditor_2));
    fWhenChange.setLayoutData(new GridData());
    fWhenTrue.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            setDirty(PROP_CONDITION_SUSPEND_POLICY);
        }
    });
    fWhenChange.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            setDirty(PROP_CONDITION_SUSPEND_POLICY);
        }
    });

    if (fConditionHistoryDialogSettings != null) {
        fLocalConditionHistory = new HashMap<IJavaLineBreakpoint, Stack<String>>();
        fConditionHistory = SWTFactory.createCombo(parent, SWT.DROP_DOWN | SWT.READ_ONLY, 1, null);
        initializeConditionHistoryDropDown();
        fConditionHistory.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int historyIndex = fConditionHistory.getSelectionIndex() - 1;
                if (historyIndex >= 0 && historyIndex != fSeparatorIndex) {
                    fViewer.getDocument().set(getConditionHistory()[historyIndex]);
                }
            }
        });
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.widthHint = 10;
        fConditionHistory.setLayoutData(data);
        fLocalConditionHistory = new HashMap<IJavaLineBreakpoint, Stack<String>>(10);
    }

    fViewer = new JDISourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.LEFT_TO_RIGHT);
    fViewer.setEditable(false);
    ControlDecoration decoration = new ControlDecoration(fViewer.getControl(), SWT.TOP | SWT.LEFT);
    decoration.setShowOnlyOnFocus(true);
    FieldDecoration dec = FieldDecorationRegistry.getDefault()
            .getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
    decoration.setImage(dec.getImage());
    decoration.setDescriptionText(dec.getDescription());
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    // set height/width hints based on font
    GC gc = new GC(fViewer.getTextWidget());
    gc.setFont(fViewer.getTextWidget().getFont());
    FontMetrics fontMetrics = gc.getFontMetrics();
    gd.heightHint = Dialog.convertHeightInCharsToPixels(fontMetrics, 17);
    gd.widthHint = Dialog.convertWidthInCharsToPixels(fontMetrics, 40);
    gc.dispose();
    fViewer.getControl().setLayoutData(gd);
    fContentAssistHandler = new AbstractHandler() {
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
            return null;
        }
    };
    fUndoHandler = new AbstractHandler() {
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ITextOperationTarget.UNDO);
            return null;
        }
    };
    fRedoHandler = new AbstractHandler() {
        public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
            fViewer.doOperation(ITextOperationTarget.REDO);
            return null;
        }
    };
    fHandlerService = (IHandlerService) PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
    fViewer.getControl().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            activateHandlers();
        }

        @Override
        public void focusLost(FocusEvent e) {
            deactivateHandlers();
        }
    });
    parent.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            dispose();
        }
    });
    return parent;
}

From source file:uk.ac.bath.cs.asp.ide.lparse.internal.preferences.LparsePreferencePage.java

License:Open Source License

protected void createFieldEditors() {
    final Composite parent = getFieldEditorParent();

    final FileFieldEditor locationEditor = new FileFieldEditor(
            LparsePreferenceConstants.LPARSE_PROGRAM_LOCATION, "Program Location", parent);
    // locationEditor.setEmptyStringAllowed(false);

    // Limit the width of the text field to 20 characters
    final Text fileText = locationEditor.getTextControl(parent);

    final GC gc = new GC(fileText);
    gc.setFont(fileText.getFont());// ww  w  .  j  a va  2  s  . c  o m
    final FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();

    final int width = Dialog.convertWidthInCharsToPixels(fontMetrics, 20);

    final GridData gridData = (GridData) fileText.getLayoutData();
    gridData.widthHint = width;
    gridData.grabExcessHorizontalSpace = true;

    // Add the field to the page
    addField(locationEditor);
}

From source file:uk.ac.bath.cs.asp.ide.smodels.internal.preferences.SmodelsPreferencePage.java

License:Open Source License

protected void createFieldEditors() {
    final Composite parent = getFieldEditorParent();

    final FileFieldEditor locationEditor = new FileFieldEditor(
            SmodelsPreferenceConstants.SMODELS_PROGRAM_LOCATION, "Program Location", parent);
    // locationEditor.setEmptyStringAllowed(false);

    // Limit the width of the text field to 20 characters
    final Text fileText = locationEditor.getTextControl(parent);

    final GC gc = new GC(fileText);
    gc.setFont(fileText.getFont());//from www. j  av a  2  s .  c o  m
    final FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();

    final int width = Dialog.convertWidthInCharsToPixels(fontMetrics, 20);

    final GridData gridData = (GridData) fileText.getLayoutData();
    gridData.widthHint = width;
    gridData.grabExcessHorizontalSpace = true;

    // Add the field to the page
    addField(locationEditor);
}

From source file:ummisco.gama.ui.viewers.gis.geotools.styling.simple.AbstractSimpleConfigurator.java

/**
 * Construct a subpart labeled with the provided tag.
 * <p>/*from  w  w  w.  j  a va  2  s.  c  o  m*/
 * Creates a composite with a grid layout of the specifed columns, and a
 * label with text from label.
 * </p>
 * 
 * @param parent
 * @param label
 * @return Composite with one label
 */
public static Composite subpart(final Composite parent, final String label) {
    final Composite subpart = new Composite(parent, SWT.NONE);
    final RowLayout across = new RowLayout();
    across.type = SWT.HORIZONTAL;
    across.wrap = true;
    across.pack = true;
    across.fill = true;
    across.marginBottom = 1;
    across.marginRight = 2;

    subpart.setLayout(across);

    final Label labell = new Label(subpart, SWT.LEFT);
    labell.setText(label);

    final RowData data = new RowData();
    data.width = 40;
    // check to see if width is not enough space
    final GC gc = new GC(parent.getParent());
    gc.setFont(parent.getParent().getFont());
    final FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();
    final int labelWidth = Dialog.convertWidthInCharsToPixels(fontMetrics, labell.getText().length() + 1);
    if (labelWidth > data.width) {
        data.width = labelWidth;
    }
    // TODO: adjust the methods that call this one to keep a consistent
    // width (otherwise they're misaligned)
    data.height = 10;
    labell.setLayoutData(data);

    return subpart;
}