Bug 276165 - New Snippet Contribution: Re-sizable Text Field Example
Summary: New Snippet Contribution: Re-sizable Text Field Example
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.0   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-05-13 14:42 EDT by Jim Hayes CLA
Modified: 2021-11-12 11:45 EST (History)
1 user (show)

See Also:


Attachments
screenshot of example (40.39 KB, image/jpeg)
2009-05-13 14:42 EDT, Jim Hayes CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jim Hayes CLA 2009-05-13 14:42:32 EDT
Created attachment 135654 [details]
screenshot of example

Eclipse build id or SWT version: SWT 3.4.2

Platform(s) tested on: Windows XP

Snippet title: A Re-sizable Text Field Example.

Snippet code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ResizableTextExample extends Composite {

    // maximum number of lines in the re-sizable text field.
    private int maxLineCount = 15;
    // minimum number of lines in the re-sizable text field.
    private int minLineCount = 3;
    // the width of the re-sizable text field.
    private int width = 500;
    // the height of a row of text.
    private int fontHeight;

    public ResizableTextExample(Composite parent, int style) {
        super(parent, style);

        setLayout(new GridLayout(2, false));

        // ///////////////////////////////////////////////////////////////
        // Add some stuff before the re-sizable text.
        // ///////////////////////////////////////////////////////////////

        Label label1 = new Label(this, SWT.NONE);
        label1.setText("A non-resizable text field:");

        Text text1 = new Text(this, SWT.MULTI | SWT.BORDER 
                | SWT.H_SCROLL | SWT.V_SCROLL);
        text1.setText("non-resizable text non-resizable text"
                        + "\nnon-resizable text non-resizable"
                        + "\nnon-resizable text non-resizable");
        text1.setLayoutData(new GridData(width, 100));

        // ///////////////////////////////////////////////////////////////
        // Add the re-sizable text
        // ///////////////////////////////////////////////////////////////

        Label label2 = new Label(this, SWT.NONE);
        label2.setText("The resizable text field:");

        final Text resizableText = new Text(this, SWT.BORDER
                | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        resizableText.setText("This text field will become larger as you add lines of text,"
                + "\nand smaller as you remove lines of text."
                + "\nThis text field will become larger as you add lines of text,"
                + "\nand smaller as you remove lines of text.");

        // Figure out how tall the font is. We use this value to determine how
        // large to make the text field.
        GC gc = new GC(resizableText);
        FontMetrics fm = gc.getFontMetrics();
        fontHeight = fm.getHeight();

        updateTextSize(resizableText);

        // Add a modify listener to the text field so that we can 
        // resize it whenever the contents change.
        resizableText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                if (updateTextSize(resizableText)) {
                    // re-layout the composite because the size of 
                    // the field has changed.
                    layout();
                }
            }
        });

        // ///////////////////////////////////////////////////////////////
        // Add some stuff after the re-sizable text.
        // ///////////////////////////////////////////////////////////////

        Label label3 = new Label(this, SWT.NONE);
        label3.setText("A non-resizable text field:");

        Text text3 = new Text(this, SWT.MULTI | SWT.BORDER 
                | SWT.H_SCROLL | SWT.V_SCROLL);
        text3.setText("non-resizable text non-resizable text"
                        + "\nnon-resizable text non-resizable text"
                        + "\nnon-resizable text non-resizable text");
        text3.setLayoutData(new GridData(width, 100));
    }

    private boolean updateTextSize(Text resizableText) {
        int lineCount = resizableText.getLineCount();
        
        // Add a line so that there will always be a blank line after the last line.
        lineCount++;

        // Limit the height of the text field to a minimum and maximum.
        int height;
        if (lineCount > maxLineCount) {
            height = maxLineCount * fontHeight;
        }
        else if (lineCount < minLineCount) {
            height = minLineCount * fontHeight;
        }
        else {
            height = lineCount * fontHeight;
        }
        
        // See if the new size is different from the current size.
        // The size of the text only changes when lines are added or removed, 
        // so we usually do not need to resize.
        GridData gridData = (GridData) resizableText.getLayoutData();        
        if (gridData != null && gridData.widthHint == width 
                && gridData.heightHint == height) {
            return false;
        }

        resizableText.setLayoutData(new GridData(width, height));
        return true;
    }

    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        // Add the UI to the shell.
        new ResizableTextExample(shell, SWT.NONE);

        shell.setSize(800, 600);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}
Comment 1 Eclipse Webmaster CLA 2019-09-06 16:08:38 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.