📄 formlayouttab.java
字号:
String controlString = item.getText (BOTTOM_COL);
int index = new Integer (controlString.substring (controlString.indexOf (',') - 1, controlString.indexOf (','))).intValue ();
code.append ("\t\tdata.bottom = new FormAttachment (" + names [index] + ", " + data.bottom.offset + ", SWT." + alignmentString (data.bottom.alignment) + ");\n");
} else {
code.append ("\t\tdata.bottom = new FormAttachment (" + data.bottom.numerator + ", " + data.bottom.offset + ");\n");
}
}
code.append ("\t\t" + names [i] + ".setLayoutData (data);\n");
}
}
return code;
}
/**
* Returns the layout data field names.
*/
String [] getLayoutDataFieldNames() {
return new String [] {
"",
"Control",
"width",
"height",
"left",
"right",
"top",
"bottom"
};
}
/**
* Gets the text for the tab folder item.
*/
String getTabText () {
return "FormLayout";
}
/**
* Takes information from TableEditors and stores it.
*/
void resetEditors () {
resetEditors (false);
}
void resetEditors (boolean tab) {
TableItem oldItem = comboEditor.getItem ();
if (oldItem != null) {
int row = table.indexOf (oldItem);
try {
new Integer (widthText.getText ()).intValue ();
} catch (NumberFormatException e) {
widthText.setText (oldItem.getText (WIDTH_COL));
}
try {
new Integer (heightText.getText ()).intValue ();
} catch (NumberFormatException e) {
heightText.setText (oldItem.getText (HEIGHT_COL));
}
String [] insert = new String [] {String.valueOf (row), combo.getText (), widthText.getText (), heightText.getText ()};
data.setElementAt (insert, row);
for (int i = 0 ; i < MODIFY_COLS; i++) {
oldItem.setText (i, ((String [])data.elementAt (row)) [i]);
}
if (!tab) disposeEditors ();
}
setLayoutState ();
refreshLayoutComposite ();
setLayoutData ();
layoutComposite.layout (true);
layoutGroup.layout (true);
}
/**
* Sets an attachment to the edge of a widget using the
* information in the table.
*/
FormAttachment setAttachment (String attachment) {
String control, align;
int position, offset;
int comma = attachment.indexOf (',');
char first = attachment.charAt (0);
if (Character.isLetter(first)) {
/* Case where there is a control */
control = attachment.substring (0, comma);
int i = 0;
while (i < control.length () && !Character.isDigit (control.charAt (i))) {
i++;
}
String end = control.substring (i);
int index = new Integer (end).intValue ();
Control attachControl = children [index];
int colon = attachment.indexOf (':');
try {
offset = new Integer (attachment.substring (comma + 1, colon)).intValue ();
} catch (NumberFormatException e) {
offset = 0;
}
align = attachment.substring (colon + 1);
return new FormAttachment (attachControl, offset, alignmentConstant (align));
} else {
/* Case where there is a position */
try {
position = new Integer (attachment.substring (0,comma)).intValue ();
} catch (NumberFormatException e) {
position = 0;
}
try {
offset = new Integer (attachment.substring (comma + 1)).intValue ();
} catch (NumberFormatException e) {
offset = 0;
}
return new FormAttachment (position, offset);
}
}
/**
* Sets the layout data for the children of the layout.
*/
void setLayoutData () {
Control [] children = layoutComposite.getChildren ();
TableItem [] items = table.getItems ();
FormData data;
int width, height;
String left, right, top, bottom;
for (int i = 0; i < children.length; i++) {
width = new Integer (items [i].getText (WIDTH_COL)).intValue ();
height = new Integer (items [i].getText (HEIGHT_COL)).intValue ();
data = new FormData ();
if (width > 0) data.width = width;
if (height > 0) data.height = height;
left = items [i].getText (LEFT_COL);
if (left.length () > 0) {
data.left = setAttachment (left);
if (data.left.control != null) {
String attachment = checkAttachment (left, data.left);
items [i].setText (LEFT_COL, attachment);
}
}
right = items [i].getText (RIGHT_COL);
if (right.length () > 0) {
data.right = setAttachment (right);
if (data.right.control != null) {
String attachment = checkAttachment (right, data.right);
items [i].setText (RIGHT_COL, attachment);
}
}
top = items [i].getText (TOP_COL);
if (top.length () > 0 ) {
data.top = setAttachment (top);
if (data.top.control != null) {
String attachment = checkAttachment (top, data.top);
items [i].setText (TOP_COL, attachment);
}
}
bottom = items [i].getText (BOTTOM_COL);
if (bottom.length () > 0) {
data.bottom = setAttachment (bottom);
if (data.bottom.control != null) {
String attachment = checkAttachment (bottom, data.bottom);
items [i].setText (BOTTOM_COL, attachment);
}
}
children [i].setLayoutData (data);
}
}
/**
* Sets the state of the layout.
*/
void setLayoutState () {
/* Set the margins and spacing */
try {
formLayout.marginHeight = new Integer (marginHeight.getText ()).intValue ();
} catch (NumberFormatException e) {
formLayout.marginHeight = 0;
marginHeight.select (0);
}
try {
formLayout.marginWidth = new Integer (marginWidth.getText ()).intValue ();
} catch (NumberFormatException e) {
formLayout.marginWidth = 0;
marginWidth.select (0);
}
}
/**
* <code>AttachDialog</code> is the class that creates a
* dialog specific for this example. It creates a dialog
* with controls to set the values in a FormAttachment.
*/
public class AttachDialog extends Dialog {
String result = "";
String controlInput, positionInput, alignmentInput, offsetInput;
int col = 0;
public AttachDialog (Shell parent, int style) {
super (parent, style);
}
public AttachDialog (Shell parent) {
this (parent, 0);
}
public void setColumn (int col) {
this.col = col;
}
public String open () {
Shell parent = getParent ();
final Shell shell = new Shell (parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setText (getText ());
GridLayout layout = new GridLayout ();
layout.numColumns = 3;
layout.makeColumnsEqualWidth = true;
shell.setLayout (layout);
/* Find out what was previously set as an attachment */
TableItem newItem = leftEditor.getItem ();
result = newItem.getText (col);
String oldAttach = result;
String oldPos = "0", oldControl = "", oldAlign = "DEFAULT", oldOffset = "0";
boolean isControl = false;
if (oldAttach.length () != 0) {
char first = oldAttach.charAt (0);
if (Character.isLetter(first)) {
/* We have a control */
isControl = true;
oldControl = oldAttach.substring (0, oldAttach.indexOf (','));
oldAlign = oldAttach.substring (oldAttach.indexOf (':') + 1);
oldOffset = oldAttach.substring (oldAttach.indexOf (',') + 1, oldAttach.indexOf (':'));
} else {
/* We have a position */
oldPos = oldAttach.substring (0, oldAttach.indexOf (','));
oldOffset = oldAttach.substring (oldAttach.indexOf (',') + 1);
if (oldOffset.endsWith (")")) { // i.e. (Default)
oldOffset = oldOffset.substring (0, oldOffset.indexOf (' '));
}
}
}
/* Add position field */
final Button posButton = new Button (shell, SWT.RADIO);
posButton.setText (LayoutExample.getResourceString ("Position"));
posButton.setSelection (!isControl);
final Combo position = new Combo (shell, SWT.NONE);
position.setItems (new String [] {"0","25","50","75","100"});
position.setText (oldPos);
position.setEnabled (!isControl);
GridData data = new GridData (GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
position.setLayoutData (data);
/* Add control field */
final Button contButton = new Button (shell, SWT.RADIO);
contButton.setText (LayoutExample.getResourceString ("Control"));
contButton.setSelection (isControl);
final Combo control = new Combo (shell, SWT.READ_ONLY);
TableItem [] items = table.getItems ();
TableItem currentItem = leftEditor.getItem ();
for (int i = 0; i < table.getItemCount (); i++) {
if (items [i].getText (0).length() > 0) {
if (items [i] != currentItem) {
control.add (items [i].getText (COMBO_COL) + i);
}
}
}
if (oldControl.length () != 0) control.setText (oldControl);
else control.select (0);
control.setEnabled (isControl);
data = new GridData (GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
control.setLayoutData (data);
/* Add alignment field */
new Label (shell, SWT.NONE).setText (LayoutExample.getResourceString ("Alignment"));
final Combo alignment = new Combo (shell, SWT.NONE);
String[] alignmentValues;
if (col == LEFT_COL || col == RIGHT_COL) {
alignmentValues = new String [] {"SWT.LEFT", "SWT.RIGHT", "SWT.CENTER", "SWT.DEFAULT"};
} else {
// col == TOP_COL || col == BOTTOM_COL
alignmentValues = new String [] {"SWT.TOP", "SWT.BOTTOM", "SWT.CENTER", "SWT.DEFAULT"};
}
alignment.setItems (alignmentValues);
alignment.setText ("SWT." + oldAlign);
alignment.setEnabled (isControl);
data = new GridData (GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
alignment.setLayoutData (data);
/* Add offset field */
new Label (shell, SWT.NONE).setText (LayoutExample.getResourceString ("Offset"));
final Text offset = new Text (shell, SWT.SINGLE | SWT.BORDER);
offset.setText (oldOffset);
data = new GridData (GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
offset.setLayoutData (data);
/* Add listeners for choosing between position and control */
posButton.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
position.setEnabled (true);
control.setEnabled (false);
alignment.setEnabled(false);
}
});
contButton.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
position.setEnabled (false);
control.setEnabled (true);
alignment.setEnabled(true);
}
});
Button clear = new Button (shell, SWT.PUSH);
clear.setText (LayoutExample.getResourceString ("Clear"));
clear.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_END));
clear.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
result = "";
shell.close ();
}
});
/* OK button sets data into table */
Button ok = new Button (shell, SWT.PUSH);
ok.setText (LayoutExample.getResourceString ("OK"));
ok.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_CENTER));
ok.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
controlInput = control.getText ();
alignmentInput = alignment.getText ().substring (4);
positionInput = position.getText ();
if (positionInput.length () == 0) positionInput = "0";
try {
new Integer (positionInput).intValue ();
} catch (NumberFormatException except) {
positionInput = "0";
}
offsetInput = offset.getText ();
if (offsetInput.length () == 0) offsetInput = "0";
try {
new Integer (offsetInput).intValue ();
} catch (NumberFormatException except) {
offsetInput = "0";
}
if (posButton.getSelection() || controlInput.length () == 0) {
result = positionInput + "," + offsetInput;
} else {
result = controlInput + "," + offsetInput + ":" + alignmentInput;
}
shell.close ();
}
});
Button cancel = new Button (shell, SWT.PUSH);
cancel.setText (LayoutExample.getResourceString ("Cancel"));
cancel.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_BEGINNING));
cancel.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
shell.close ();
}
});
shell.setDefaultButton (ok);
shell.pack ();
/* Center the dialog */
Point center = parent.getLocation ();
center.x = center.x + (parent.getBounds ().width / 2) - (shell.getBounds ().width / 2);
center.y = center.y + (parent.getBounds ().height / 2) - (shell.getBounds ().height / 2);
shell.setLocation (center);
shell.open ();
Display display = shell.getDisplay ();
while (!shell.isDisposed ()) {
if (display.readAndDispatch ()) display.sleep ();
}
return result;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -