Skip to content
Snippets Groups Projects
Commit 8f6da0a9 authored by helene ortiz's avatar helene ortiz
Browse files

test dpi

parent c82e5807
No related branches found
No related tags found
No related merge requests found
package fr.ill.ics.client.view.test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import fr.ill.ics.client.view.factory.swt.SWTFontFactory;
public class TestDPI {
private Display display;
public static void main(String[] args) {
TestDPI moi = new TestDPI();
moi.run();
}
private void run() {
display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());
final Composite composite = new Composite(shell, SWT.BORDER);
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.NONE);
SWTFontFactory.getInstance().init(display);
label.setFont(SWTFontFactory.getStandardFont());
label.setText("Label");
label.setLayoutData(getSizedGridData(label, 10, false, "Label"));
label.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
Text text = new Text(composite, SWT.NONE);
text.setFont(SWTFontFactory.getStandardFont());
text.setText("Text");
text.setTextLimit(10);
//text.setLayoutData(getSizedGridData(label, 10, false, "Text"));
text.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
System.out.println(getSizedGridData(text, 10, false, "Text").widthHint);
System.out.println(getGridData(text, 10).widthHint);
text.setLayoutData(getGridData(text, 10));
// shell.pack();
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public GridData getSizedGridData(Control control, int maxLength, boolean isNumeric, String who) {
GridData gridData = new GridData();
gridData.grabExcessVerticalSpace = true;
gridData.verticalAlignment = GridData.CENTER;
GC gc = new GC(display);
gc.setFont(control.getFont());
// H.ORTIZ: "h" replaced by "X" (for some strings "h" was not large enough...)
/* Extent for all letters (lower and upper cases):
i j l 3
f r t 4
s z 5
a b c d e g h k n o p q u v x y 6
w 8
m 10
I J 3
E F L P S T Y 6
A B C K N R U V X Z 7
D G H O Q 8
M 9
W 10
*/
Point extent = gc.textExtent("H");
System.out.println("Width for " + who + " = " + extent.x) ;
// H.ORTIZ: Not longer used: generates problem for property with large content (see for example table_step from startmono controller)
// Avoid to have Text too large by limitating width to 400 pixels
//gridData.widthHint = Math.min(maxLength * extent.x, TEXT_MAX_WIDTH);
gridData.widthHint = maxLength * extent.x;
gc.dispose();
return gridData;
}
private GridData getGridData(Control control, int maxLength) {
GC gc = new GC(control);
Point dpi = display.getDPI();
int extentX = gc.textExtent("H").x * maxLength;
int textWidth = extentX * dpi.x / 79;
gc.dispose();
GridData gridData = new GridData();
gridData.grabExcessVerticalSpace = true;
gridData.verticalAlignment = GridData.CENTER;
gridData.widthHint = textWidth;
return gridData;
// Définir la largeur du champ texte
//textField.setSize(textWidth + 20, textField.getSize().y); // +20 pour une marge
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment