diff options
author | Carolyn MacLeod <carolyn> | 2007-12-31 21:54:10 +0000 |
---|---|---|
committer | Carolyn MacLeod <carolyn> | 2007-12-31 21:54:10 +0000 |
commit | ca8346412189df48223ba59c33b21708729a1c0e (patch) | |
tree | ac562b5d846c774949be77c029e6941dbcf51846 | |
parent | 2cc7d30698448c6df09a70c69839e5a3830f9bc0 (diff) | |
download | eclipse.platform.swt-ca8346412189df48223ba59c33b21708729a1c0e.tar.gz eclipse.platform.swt-ca8346412189df48223ba59c33b21708729a1c0e.tar.xz eclipse.platform.swt-ca8346412189df48223ba59c33b21708729a1c0e.zip |
updates from Z, plus additional updates
8 files changed, 1867 insertions, 1489 deletions
diff --git a/examples/org.eclipse.swt.examples/src/examples_layout.properties b/examples/org.eclipse.swt.examples/src/examples_layout.properties index 47a9c80a27..6132739c5d 100644 --- a/examples/org.eclipse.swt.examples/src/examples_layout.properties +++ b/examples/org.eclipse.swt.examples/src/examples_layout.properties @@ -17,7 +17,7 @@ Children = Children Type = Type Layout = Layout Preferred_Size = Preferred Size -Code = C&ode +Generate_Code = &Generate Code Generated_Code = Generated Code Parameters = Parameters Properties = Properties diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FillLayoutTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FillLayoutTab.java index a84b065d72..8a90b7b42c 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FillLayoutTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FillLayoutTab.java @@ -10,65 +10,112 @@ *******************************************************************************/ package org.eclipse.swt.examples.layoutexample; - import org.eclipse.swt.*; import org.eclipse.swt.custom.*; import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; class FillLayoutTab extends Tab { /* Controls for setting layout parameters */ Button horizontal, vertical; + Spinner marginWidth, marginHeight, spacing; /* The example layout instance */ FillLayout fillLayout; /* TableEditors and related controls*/ - TableEditor comboEditor; + TableEditor comboEditor, nameEditor; CCombo combo; + int prevSelected = 0; + Text nameText; + final int NAME_COL = 0; + final int TOTAL_COLS = 2; /** * Creates the Tab within a given instance of LayoutExample. */ - FillLayoutTab(LayoutExample instance) { - super(instance); + FillLayoutTab() { } /** * Creates the widgets in the "child" group. */ - void createChildWidgets () { + void createChildWidgets() { /* Add common controls */ - super.createChildWidgets (); + super.createChildWidgets(); /* Add TableEditors */ - comboEditor = new TableEditor (table); - table.addSelectionListener (new SelectionAdapter() { - public void widgetSelected(SelectionEvent e) { - resetEditors (); - index = table.getSelectionIndex (); - if (index == -1) return; - TableItem oldItem = comboEditor.getItem (); - newItem = table.getItem (index); - if (newItem == oldItem || newItem != lastSelected) { + comboEditor = new TableEditor(table); + nameEditor = new TableEditor(table); + table.addMouseListener(new MouseAdapter() { + public void mouseDown(MouseEvent e) { + resetEditors(); + index = table.getSelectionIndex(); + if(index == -1) return; + TableItem oldItem = comboEditor.getItem(); + newItem = table.getItem(index); + if(newItem == oldItem || newItem != lastSelected) { lastSelected = newItem; return; } - table.showSelection (); + table.showSelection(); + combo = new CCombo(table, SWT.READ_ONLY); + createComboEditor(combo, comboEditor); - combo = new CCombo (table, SWT.READ_ONLY); - createComboEditor (combo, comboEditor); + nameText = new Text(table, SWT.SINGLE); + nameText.setText(((String[])data.elementAt(index))[NAME_COL]); + createTextEditor(nameText, nameEditor, NAME_COL); } - }); - + }); - /* Add listener to add an element to the table */ - add.addSelectionListener(new SelectionAdapter () { - public void widgetSelected(SelectionEvent e) { - TableItem item = new TableItem (table, 0); - item.setText (0, String.valueOf (table.indexOf (item))); - item.setText (1, "Button"); - data.addElement ("Button"); - resetEditors (); + // Add listener to add an element to the table + add.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + if(event.detail == SWT.ARROW) { + ToolItem item = (ToolItem)event.widget; + ToolBar bar = item.getParent(); + Display display = bar.getDisplay(); + Shell shell = bar.getShell(); + final Menu menu = new Menu(shell, SWT.POP_UP); + for (int i = 0; i < OPTIONS.length; i++) { + final MenuItem newItem = new MenuItem(menu, SWT.RADIO); + newItem.setText(OPTIONS[i]); + newItem.addSelectionListener(new SelectionAdapter (){ + public void widgetSelected (SelectionEvent event) { + MenuItem menuItem = (MenuItem)event.widget; + if (menuItem.getSelection()) { + Menu menu = menuItem.getParent(); + prevSelected = menu.indexOf(menuItem); + TableItem item = new TableItem (table, SWT.NONE); + String name = menuItem.getText().toLowerCase() + String.valueOf(table.getItemCount() - 1); + String[] insert = new String[] {name, menuItem.getText()}; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } + } + }); + newItem.setSelection(i == prevSelected); + } + Point pt = display.map(bar, null, event.x, event.y); + menu.setLocation(pt.x, pt.y); + menu.setVisible(true); + + while(menu != null && !menu.isDisposed() && menu.isVisible()) { + if(!display.readAndDispatch()) { + display.sleep(); + } + } + menu.dispose(); + } else { + String selection = OPTIONS[prevSelected]; + TableItem item = new TableItem(table, 0); + String name = selection.toLowerCase() + String.valueOf(table.indexOf(item)); + String[] insert = new String[] {name, selection }; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } } }); } @@ -76,60 +123,91 @@ class FillLayoutTab extends Tab { /** * Creates the control widgets. */ - void createControlWidgets () { + void createControlWidgets() { /* Controls the type of FillLayout */ - Group typeGroup = new Group (controlGroup, SWT.NONE); - typeGroup.setText (LayoutExample.getResourceString ("Type")); - typeGroup.setLayout (new GridLayout ()); - typeGroup.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); - horizontal = new Button (typeGroup, SWT.RADIO); - horizontal.setText ("SWT.HORIZONTAL"); - horizontal.setLayoutData(new GridData (GridData.FILL_HORIZONTAL)); + Group typeGroup = new Group(controlGroup, SWT.NONE); + typeGroup.setText(LayoutExample.getResourceString("Type")); + typeGroup.setLayout(new GridLayout()); + typeGroup.setLayoutData(new GridData (SWT.FILL, SWT.FILL, true, false)); + horizontal = new Button(typeGroup, SWT.RADIO); + horizontal.setText("SWT.HORIZONTAL"); + horizontal.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); horizontal.setSelection(true); - horizontal.addSelectionListener (selectionListener); - vertical = new Button (typeGroup, SWT.RADIO); - vertical.setText ("SWT.VERTICAL"); - vertical.setLayoutData(new GridData (GridData.FILL_HORIZONTAL)); - vertical.addSelectionListener (selectionListener); + horizontal.addSelectionListener(selectionListener); + vertical = new Button(typeGroup, SWT.RADIO); + vertical.setText("SWT.VERTICAL"); + vertical.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + vertical.addSelectionListener(selectionListener); + + /* Controls the margins and spacing of the FormLayout */ + Group marginGroup = new Group(controlGroup, SWT.NONE); + marginGroup.setText (LayoutExample.getResourceString("Margins")); + marginGroup.setLayout(new GridLayout(2, false)); + marginGroup.setLayoutData (new GridData(SWT.FILL, SWT.CENTER, true, false)); + new Label(marginGroup, SWT.NONE).setText("Margin Width"); + marginWidth = new Spinner(marginGroup, SWT.BORDER); + marginWidth.setSelection(0); + marginWidth.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Margin Height"); + marginHeight = new Spinner(marginGroup, SWT.BORDER); + marginHeight.setSelection(0); + marginHeight.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); + marginHeight.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText ("Spacing"); + spacing = new Spinner(marginGroup, SWT.BORDER); + spacing.setSelection(0); + spacing.addSelectionListener(selectionListener); /* Add common controls */ - super.createControlWidgets (); + super.createControlWidgets(); /* Position the sash */ - sash.setWeights (new int [] {4,1}); + sash.setWeights(new int[] {4,1}); +// sash.setWeights(new int[] {6,4}); } + /** * Creates the example layout. */ - void createLayout () { - fillLayout = new FillLayout (); - layoutComposite.setLayout (fillLayout); + void createLayout() { + fillLayout = new FillLayout(); + layoutComposite.setLayout(fillLayout); } /** * Disposes the editors without placing their contents * into the table. */ - void disposeEditors () { - comboEditor.setEditor (null, null, -1); - combo.dispose (); + void disposeEditors() { + comboEditor.setEditor(null, null, -1); + combo.dispose(); + nameText.dispose(); } /** * Generates code for the example layout. */ - StringBuffer generateLayoutCode () { - StringBuffer code = new StringBuffer (); - code.append ("\t\tFillLayout fillLayout = new FillLayout ();\n"); + StringBuffer generateLayoutCode() { + StringBuffer code = new StringBuffer(); + code.append("\t\tFillLayout fillLayout = new FillLayout ();\n"); if (fillLayout.type == SWT.VERTICAL) { - code.append ("\t\tfillLayout.type = SWT.VERTICAL;\n"); + code.append("\t\tfillLayout.type = SWT.VERTICAL;\n"); } - code.append ("\t\tshell.setLayout (fillLayout);\n"); - for (int i = 0; i < children.length; i++) { - Control control = children [i]; - code.append (getChildCode (control, i)); + if (fillLayout.marginWidth != 0) { + code.append("\t\tfillLayout.marginWidth = " + fillLayout.marginWidth + ";\n"); + } + if (fillLayout.marginHeight != 0) { + code.append("\t\tfillLayout.marginHeight = " + fillLayout.marginHeight + ";\n"); + } + if (fillLayout.spacing != 0) { + code.append("\t\tfillLayout.spacing = " + fillLayout.spacing + ";\n"); + } + code.append("\t\tshell.setLayout (fillLayout);\n"); + for(int i = 0; i < children.length; i++) { + Control control = children[i]; + code.append(getChildCode(control, i)); } return code; } @@ -138,42 +216,55 @@ class FillLayoutTab extends Tab { * Returns the layout data field names. */ String [] getLayoutDataFieldNames() { - return new String [] {"","Control"}; + return new String[] { "Control Name", "Control Type" }; } /** * Gets the text for the tab folder item. */ - String getTabText () { + String getTabText() { return "FillLayout"; } /** * Takes information from TableEditors and stores it. */ - void resetEditors () { - TableItem oldItem = comboEditor.getItem (); - comboEditor.setEditor (null, null, -1); + void resetEditors() { + TableItem oldItem = comboEditor.getItem(); + comboEditor.setEditor(null, null, -1); if (oldItem != null) { - int row = table.indexOf (oldItem); - data.insertElementAt (combo.getText (), row); - oldItem.setText (1, data.elementAt (row).toString ()); - combo.dispose (); + int row = table.indexOf(oldItem); + try { + new String(nameText.getText()); + } catch(NumberFormatException e) { + nameText.setText(oldItem.getText (NAME_COL)); + } + String[] insert = new String[] {nameText.getText(), combo.getText ()}; + data.setElementAt(insert, row); + for (int i = 0 ; i < TOTAL_COLS; i++) { + oldItem.setText(i, ((String[])data.elementAt(row))[i]); + } + disposeEditors(); } - setLayoutState (); - refreshLayoutComposite (); - layoutComposite.layout (true); - layoutGroup.layout (true); + setLayoutState(); + refreshLayoutComposite(); + layoutComposite.layout(true); + layoutGroup.layout(true); } /** * Sets the state of the layout. */ - void setLayoutState () { - if (vertical.getSelection()) { + void setLayoutState() { + if(vertical.getSelection()) { fillLayout.type = SWT.VERTICAL; } else { fillLayout.type = SWT.HORIZONTAL; } + + /* Set the margins and spacing */ + fillLayout.marginWidth = marginWidth.getSelection(); + fillLayout.marginHeight = marginHeight.getSelection(); + fillLayout.spacing = spacing.getSelection(); } } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FormLayoutTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FormLayoutTab.java index 1adb77b85a..e51e382e45 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FormLayoutTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/FormLayoutTab.java @@ -10,7 +10,6 @@ *******************************************************************************/ package org.eclipse.swt.examples.layoutexample; - import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; @@ -20,17 +19,19 @@ import org.eclipse.swt.custom.*; class FormLayoutTab extends Tab { /* Controls for setting layout parameters */ - Combo marginHeight, marginWidth; + Spinner marginWidth, marginHeight, spacing; /* The example layout instance */ FormLayout formLayout; /* TableEditors and related controls*/ - TableEditor comboEditor, widthEditor, heightEditor; + TableEditor nameEditor, comboEditor, widthEditor, heightEditor; TableEditor leftEditor, rightEditor, topEditor, bottomEditor; CCombo combo; - Text widthText, heightText; + Text nameText, widthText, heightText; Button leftAttach, rightAttach, topAttach, bottomAttach; + int prevSelected = 0; /* Constants */ + final int NAME_COL = 0; final int COMBO_COL = 1; final int WIDTH_COL = 2; final int HEIGHT_COL = 3; @@ -45,15 +46,14 @@ class FormLayoutTab extends Tab { /** * Creates the Tab within a given instance of LayoutExample. */ - FormLayoutTab(LayoutExample instance) { - super(instance); + FormLayoutTab() { } /** * Returns the constant for the alignment for an * attachment given a string. */ - int alignmentConstant (String align) { + int alignmentConstant(String align) { if (align.equals("LEFT")) return SWT.LEFT; if (align.equals("RIGHT")) return SWT.RIGHT; if (align.equals("TOP")) return SWT.TOP; @@ -66,8 +66,8 @@ class FormLayoutTab extends Tab { * Returns a string representing the alignment for an * attachment given a constant. */ - String alignmentString (int align) { - switch (align) { + String alignmentString(int align) { + switch(align) { case SWT.LEFT: return "LEFT"; case SWT.RIGHT: return "RIGHT"; case SWT.TOP: return "TOP"; @@ -81,151 +81,157 @@ class FormLayoutTab extends Tab { * Update the attachment field in case the type of control * has changed. */ - String checkAttachment (String oldAttach, FormAttachment newAttach) { - String controlClass = newAttach.control.getClass().toString (); - String controlType = controlClass.substring (controlClass.lastIndexOf ('.') + 1); + String checkAttachment(String oldAttach, FormAttachment newAttach) { + String controlClass = newAttach.control.getClass().toString(); + String controlType = controlClass.substring(controlClass.lastIndexOf('.') + 1); int i = 0; - while (i < oldAttach.length () && !Character.isDigit(oldAttach.charAt (i))) { + while(i < oldAttach.length() && !Character.isDigit(oldAttach.charAt(i))) { i++; } - String index = oldAttach.substring (i, oldAttach.indexOf (',')); - return controlType + index + "," + newAttach.offset + ":" + alignmentString (newAttach.alignment); + String index = oldAttach.substring(i, oldAttach.indexOf(',')); + return controlType + index + "," + newAttach.offset + ":" + alignmentString(newAttach.alignment); } /** * Creates the widgets in the "child" group. */ - void createChildWidgets () { + void createChildWidgets() { /* Add common controls */ - super.createChildWidgets (); + super.createChildWidgets(); /* Resize the columns */ - table.getColumn (LEFT_COL).setWidth (100); - table.getColumn (RIGHT_COL).setWidth (100); - table.getColumn (TOP_COL).setWidth (100); - table.getColumn (BOTTOM_COL).setWidth (100); + table.getColumn(LEFT_COL).setWidth(100); + table.getColumn(RIGHT_COL).setWidth(100); + table.getColumn(TOP_COL).setWidth(100); + table.getColumn(BOTTOM_COL).setWidth(100); - /* Add TableEditors */ - comboEditor = new TableEditor (table); - widthEditor = new TableEditor (table); - heightEditor = new TableEditor (table); - leftEditor = new TableEditor (table); - rightEditor = new TableEditor (table); - topEditor = new TableEditor (table); - bottomEditor = new TableEditor (table); - table.addMouseListener (new MouseAdapter () { + /* Add TableEditors */ + nameEditor = new TableEditor(table); + comboEditor = new TableEditor(table); + widthEditor = new TableEditor(table); + heightEditor = new TableEditor(table); + leftEditor = new TableEditor(table); + rightEditor = new TableEditor(table); + topEditor = new TableEditor(table); + bottomEditor = new TableEditor(table); + table.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { resetEditors(); - index = table.getSelectionIndex (); - Point pt = new Point (e.x, e.y); - newItem = table.getItem (pt); - if (newItem == null) return; - TableItem oldItem = comboEditor.getItem (); - if (newItem == oldItem || newItem != lastSelected) { + index = table.getSelectionIndex(); + Point pt = new Point(e.x, e.y); + newItem = table.getItem(pt); + if(newItem == null) return; + TableItem oldItem = comboEditor.getItem(); + if(newItem == oldItem || newItem != lastSelected) { lastSelected = newItem; return; } - table.showSelection (); + table.showSelection(); + + combo = new CCombo(table, SWT.READ_ONLY); + createComboEditor(combo, comboEditor); - combo = new CCombo (table, SWT.READ_ONLY); - createComboEditor (combo, comboEditor); + nameText = new Text(table, SWT.SINGLE); + System.out.println(((String[])data.elementAt(index))[NAME_COL]); + nameText.setText(((String[])data.elementAt(index))[NAME_COL]); + createTextEditor(nameText, nameEditor, NAME_COL); - widthText = new Text (table, SWT.SINGLE); - widthText.setText (((String [])data.elementAt (index)) [WIDTH_COL]); - createTextEditor (widthText, widthEditor, WIDTH_COL); + widthText = new Text(table, SWT.SINGLE); + widthText.setText(((String[])data.elementAt (index))[WIDTH_COL]); + createTextEditor(widthText, widthEditor, WIDTH_COL); - heightText = new Text (table, SWT.SINGLE); - heightText.setText (((String [])data.elementAt (index)) [HEIGHT_COL]); - createTextEditor (heightText, heightEditor, HEIGHT_COL); + heightText = new Text(table, SWT.SINGLE); + heightText.setText(((String[])data.elementAt(index))[HEIGHT_COL]); + createTextEditor(heightText, heightEditor, HEIGHT_COL); - leftAttach = new Button (table, SWT.PUSH); - leftAttach.setText (LayoutExample.getResourceString ("Attach_Edit")); + leftAttach = new Button(table, SWT.PUSH); + leftAttach.setText(LayoutExample.getResourceString("Attach_Edit")); leftEditor.horizontalAlignment = SWT.LEFT; leftEditor.grabHorizontal = true; - leftEditor.minimumWidth = leftAttach.computeSize (SWT.DEFAULT, SWT.DEFAULT).x; - leftEditor.setEditor (leftAttach, newItem, LEFT_COL); - leftAttach.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - Shell shell = tabFolderPage.getShell (); - AttachDialog dialog = new AttachDialog (shell); - dialog.setText (LayoutExample.getResourceString ("Left_Attachment")); - dialog.setColumn (LEFT_COL); - String attach = dialog.open (); - newItem.setText (LEFT_COL, attach); - resetEditors (); + leftEditor.minimumWidth = leftAttach.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; + leftEditor.setEditor(leftAttach, newItem, LEFT_COL); + leftAttach.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + Shell shell = tabFolderPage.getShell(); + AttachDialog dialog = new AttachDialog(shell); + dialog.setText(LayoutExample.getResourceString ("Left_Attachment")); + dialog.setColumn(LEFT_COL); + String attach = dialog.open(); + newItem.setText(LEFT_COL, attach); + resetEditors(); } }); - rightAttach = new Button (table, SWT.PUSH); - rightAttach.setText (LayoutExample.getResourceString ("Attach_Edit")); + rightAttach = new Button(table, SWT.PUSH); + rightAttach.setText(LayoutExample.getResourceString("Attach_Edit")); rightEditor.horizontalAlignment = SWT.LEFT; rightEditor.grabHorizontal = true; - rightEditor.minimumWidth = rightAttach.computeSize (SWT.DEFAULT, SWT.DEFAULT).x; - rightEditor.setEditor (rightAttach, newItem, RIGHT_COL); - rightAttach.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - Shell shell = tabFolderPage.getShell (); - AttachDialog dialog = new AttachDialog (shell); - dialog.setText (LayoutExample.getResourceString ("Right_Attachment")); - dialog.setColumn (RIGHT_COL); + rightEditor.minimumWidth = rightAttach.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; + rightEditor.setEditor(rightAttach, newItem, RIGHT_COL); + rightAttach.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + Shell shell = tabFolderPage.getShell(); + AttachDialog dialog = new AttachDialog(shell); + dialog.setText(LayoutExample.getResourceString("Right_Attachment")); + dialog.setColumn(RIGHT_COL); String attach = dialog.open (); - newItem.setText (RIGHT_COL, attach); - if (newItem.getText (LEFT_COL).endsWith (")")) newItem.setText (LEFT_COL, ""); - resetEditors (); + newItem.setText(RIGHT_COL, attach); + if (newItem.getText(LEFT_COL).endsWith(")")) newItem.setText(LEFT_COL, ""); + resetEditors(); } }); - topAttach = new Button (table, SWT.PUSH); - topAttach.setText (LayoutExample.getResourceString ("Attach_Edit")); + topAttach = new Button(table, SWT.PUSH); + topAttach.setText (LayoutExample.getResourceString("Attach_Edit")); topEditor.horizontalAlignment = SWT.LEFT; topEditor.grabHorizontal = true; - topEditor.minimumWidth = topAttach.computeSize (SWT.DEFAULT, SWT.DEFAULT).x; - topEditor.setEditor (topAttach, newItem, TOP_COL); - topAttach.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - Shell shell = tabFolderPage.getShell (); - AttachDialog dialog = new AttachDialog (shell); - dialog.setText (LayoutExample.getResourceString ("Top_Attachment")); - dialog.setColumn (TOP_COL); - String attach = dialog.open (); - newItem.setText (TOP_COL, attach); - resetEditors (); + topEditor.minimumWidth = topAttach.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; + topEditor.setEditor(topAttach, newItem, TOP_COL); + topAttach.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + Shell shell = tabFolderPage.getShell(); + AttachDialog dialog = new AttachDialog(shell); + dialog.setText(LayoutExample.getResourceString("Top_Attachment")); + dialog.setColumn(TOP_COL); + String attach = dialog.open(); + newItem.setText(TOP_COL, attach); + resetEditors(); } }); - bottomAttach = new Button (table, SWT.PUSH); - bottomAttach.setText (LayoutExample.getResourceString ("Attach_Edit")); + bottomAttach = new Button(table, SWT.PUSH); + bottomAttach.setText(LayoutExample.getResourceString("Attach_Edit")); bottomEditor.horizontalAlignment = SWT.LEFT; bottomEditor.grabHorizontal = true; - bottomEditor.minimumWidth = bottomAttach.computeSize (SWT.DEFAULT, SWT.DEFAULT).x; + bottomEditor.minimumWidth = bottomAttach.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; bottomEditor.setEditor (bottomAttach, newItem, BOTTOM_COL); - bottomAttach.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - Shell shell = tabFolderPage.getShell (); - AttachDialog dialog = new AttachDialog (shell); - dialog.setText (LayoutExample.getResourceString ("Bottom_Attachment")); - dialog.setColumn (BOTTOM_COL); - String attach = dialog.open (); - newItem.setText (BOTTOM_COL, attach); - if (newItem.getText (TOP_COL).endsWith (")")) newItem.setText (TOP_COL, ""); - resetEditors (); + bottomAttach.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + Shell shell = tabFolderPage.getShell(); + AttachDialog dialog = new AttachDialog(shell); + dialog.setText(LayoutExample.getResourceString("Bottom_Attachment")); + dialog.setColumn(BOTTOM_COL); + String attach = dialog.open(); + newItem.setText(BOTTOM_COL, attach); + if (newItem.getText(TOP_COL).endsWith (")")) newItem.setText(TOP_COL, ""); + resetEditors(); } }); - for (int i=0; i<table.getColumnCount (); i++) { - Rectangle rect = newItem.getBounds (i); - if (rect.contains (pt)) { - switch (i) { + for (int i=0; i<table.getColumnCount(); i++) { + Rectangle rect = newItem.getBounds(i); + if(rect.contains(pt)) { + switch(i) { case 0: - resetEditors (); + resetEditors(); break; case COMBO_COL : - combo.setFocus (); + combo.setFocus(); break; case WIDTH_COL : - widthText.setFocus (); + widthText.setFocus(); break; case HEIGHT_COL : - heightText.setFocus (); + heightText.setFocus(); break; default : break; @@ -234,160 +240,195 @@ class FormLayoutTab extends Tab { } } }); - - /* Add listener to add an element to the table */ - add.addSelectionListener(new SelectionAdapter () { - public void widgetSelected(SelectionEvent e) { - TableItem item = new TableItem (table, 0); - String [] insert = new String [] { - String.valueOf (table.indexOf (item)), "Button", "-1", "-1", - "0,0 (" + LayoutExample.getResourceString ("Default") + ")", "", - "0,0 (" + LayoutExample.getResourceString ("Default") + ")", ""}; - item.setText (insert); - data.addElement (insert); - resetEditors (); + //Add listener to add an element to the table + add.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + if(event.detail == SWT.ARROW) { + ToolItem item = (ToolItem)event.widget; + ToolBar bar = item.getParent(); + Display display = bar.getDisplay(); + Shell shell = bar.getShell(); + Menu menu = new Menu (shell, SWT.POP_UP); + for(int i = 0; i < OPTIONS.length; i++) { + final MenuItem newItem = new MenuItem(menu, SWT.RADIO); + newItem.setText(OPTIONS[i]); + newItem.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent event) { + MenuItem menuItem = (MenuItem)event.widget; + if(menuItem.getSelection()) { + Menu menu = menuItem.getParent(); + prevSelected = menu.indexOf(menuItem); + TableItem item = new TableItem(table, SWT.NONE); + String name = menuItem.getText().toLowerCase() + String.valueOf(table.indexOf (item)); + String[] insert = new String [] {name, menuItem.getText(),"-1", "-1", + "0,0 (" + LayoutExample.getResourceString ("Default") + ")", "", + "0,0 (" + LayoutExample.getResourceString ("Default") + ")", ""}; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } + } + }); + newItem.setSelection(i == prevSelected); + } + Point pt = display.map(bar, null, event.x, event.y); + menu.setLocation(pt.x, pt.y); + menu.setVisible(true); + + while(menu != null && !menu.isDisposed() && menu.isVisible()) { + if(!display.readAndDispatch()) { + display.sleep(); + } + } + menu.dispose(); + }else { + String selection = OPTIONS[prevSelected]; + TableItem item = new TableItem(table, 0); + String name = selection.toLowerCase() + String.valueOf(table.indexOf(item)); + String [] insert = new String [] {name, selection,"-1", "-1", + "0,0 (" + LayoutExample.getResourceString("Default") + ")", "", + "0,0 (" + LayoutExample.getResourceString("Default") + ")", ""}; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } } - }); + }); } /** * Creates the control widgets. */ - void createControlWidgets () { + void createControlWidgets() { /* Controls the margins and spacing of the FormLayout */ - String [] marginValues = new String [] {"0","3","5","10"}; - Group marginGroup = new Group (controlGroup, SWT.NONE); - marginGroup.setText (LayoutExample.getResourceString ("Margins")); - GridLayout layout = new GridLayout (); - layout.numColumns = 2; - marginGroup.setLayout (layout); - marginGroup.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); - new Label (marginGroup, SWT.NONE).setText ("marginHeight"); - marginHeight = new Combo (marginGroup, SWT.NONE); - marginHeight.setItems (marginValues); - marginHeight.select (0); - marginHeight.addSelectionListener (selectionListener); - marginHeight.addTraverseListener (traverseListener); - GridData data = new GridData (GridData.FILL_HORIZONTAL); - data.widthHint = 60; - marginHeight.setLayoutData (data); - new Label (marginGroup, SWT.NONE).setText ("marginWidth"); - marginWidth = new Combo (marginGroup, SWT.NONE); - marginWidth.setItems (marginValues); - marginWidth.select (0); - marginWidth.addSelectionListener (selectionListener); - marginWidth.addTraverseListener (traverseListener); - data = new GridData (GridData.FILL_HORIZONTAL); - data.widthHint = 60; - marginWidth.setLayoutData (data); + Group marginGroup = new Group(controlGroup, SWT.NONE); + marginGroup.setText (LayoutExample.getResourceString("Margins")); + marginGroup.setLayout(new GridLayout(2, false)); + marginGroup.setLayoutData (new GridData(SWT.FILL, SWT.CENTER, true, false)); + new Label(marginGroup, SWT.NONE).setText("Margin Width"); + marginWidth = new Spinner(marginGroup, SWT.BORDER); + marginWidth.setSelection(0); + marginWidth.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Margin Height"); + marginHeight = new Spinner(marginGroup, SWT.BORDER); + marginHeight.setSelection(0); + marginHeight.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText ("Spacing"); + spacing = new Spinner(marginGroup, SWT.BORDER); + spacing.setSelection(0); + spacing.addSelectionListener(selectionListener); /* Add common controls */ - super.createControlWidgets (); + super.createControlWidgets(); /* Position the sash */ - sash.setWeights (new int [] {6,4}); + sash.setWeights(new int[] {6,4}); } /** * Creates the example layout. */ - void createLayout () { - formLayout = new FormLayout (); - layoutComposite.setLayout (formLayout); + void createLayout() { + formLayout = new FormLayout(); + layoutComposite.setLayout(formLayout); } /** * Disposes the editors without placing their contents * into the table. */ - void disposeEditors () { - comboEditor.setEditor (null, null, -1); - combo.dispose (); - widthText.dispose (); - heightText.dispose (); - leftAttach.dispose (); - rightAttach.dispose (); - topAttach.dispose (); - bottomAttach.dispose (); + void disposeEditors() { + comboEditor.setEditor(null, null, -1); + combo.dispose(); + nameText.dispose(); + widthText.dispose(); + heightText.dispose(); + leftAttach.dispose(); + rightAttach.dispose(); + topAttach.dispose(); + bottomAttach.dispose(); } /** * Generates code for the example layout. */ - StringBuffer generateLayoutCode () { - StringBuffer code = new StringBuffer (); - code.append ("\t\tFormLayout formLayout = new FormLayout ();\n"); + StringBuffer generateLayoutCode() { + StringBuffer code = new StringBuffer(); + code.append("\t\tFormLayout formLayout = new FormLayout ();\n"); + if (formLayout.marginWidth != 0) { + code.append("\t\tformLayout.marginWidth = " + formLayout.marginWidth + ";\n"); + } if (formLayout.marginHeight != 0) { - code.append ("\t\tformLayout.marginHeight = " + formLayout.marginHeight + ";\n"); + code.append("\t\tformLayout.marginHeight = " + formLayout.marginHeight + ";\n"); } - if (formLayout.marginWidth != 0) { - code.append ("\t\tformLayout.marginWidth = " + formLayout.marginWidth + ";\n"); + if (formLayout.spacing != 0) { + code.append("\t\tformLayout.spacing = " + formLayout.spacing + ";\n"); } - code.append ("\t\tshell.setLayout (formLayout);\n"); + code.append("\t\tshell.setLayout (formLayout);\n"); boolean first = true; for (int i = 0; i < children.length; i++) { - Control control = children [i]; - code.append (getChildCode (control, i)); - FormData data = (FormData) control.getLayoutData (); - if (data != null) { - code.append ("\t\t"); - if (first) { - code.append ("FormData "); + Control control = children[i]; + code.append(getChildCode(control, i)); + FormData data = (FormData)control.getLayoutData(); + if(data != null) { + code.append("\t\t"); + if(first) { + code.append("FormData "); first = false; } - code.append ("data = new FormData ();\n"); - if (data.width != SWT.DEFAULT) { - code.append ("\t\tdata.width = " + data.width + ";\n"); + code.append("data = new FormData ();\n"); + if(data.width != SWT.DEFAULT) { + code.append("\t\tdata.width = " + data.width + ";\n"); } - if (data.height != SWT.DEFAULT) { - code.append ("\t\tdata.height = " + data.height + ";\n"); + if(data.height != SWT.DEFAULT) { + code.append("\t\tdata.height = " + data.height + ";\n"); } - if (data.left != null) { - if (data.left.control != null) { - TableItem item = table.getItem (i); - String controlString = item.getText (LEFT_COL); - int index = new Integer (controlString.substring (controlString.indexOf (',') - 1, controlString.indexOf (','))).intValue (); - code.append ("\t\tdata.left = new FormAttachment (" + names [index] + ", " + data.left.offset + ", SWT." + alignmentString (data.left.alignment) + ");\n"); + if(data.left != null) { + if(data.left.control != null) { + TableItem item = table.getItem(i); + String controlString = item.getText(LEFT_COL); + int index = new Integer(controlString.substring(controlString.indexOf(',') - 1, controlString.indexOf(','))).intValue(); + code.append("\t\tdata.left = new FormAttachment (" + names[index] + ", " + data.left.offset + ", SWT." + alignmentString(data.left.alignment) + ");\n"); } else { - if (data.right != null || (data.left.numerator != 0 ||data.left.offset != 0)) { - code.append ("\t\tdata.left = new FormAttachment (" + data.left.numerator + ", " + data.left.offset + ");\n"); + if(data.right != null || (data.left.numerator != 0 ||data.left.offset != 0)) { + code.append("\t\tdata.left = new FormAttachment (" + data.left.numerator + ", " + data.left.offset + ");\n"); } } } - if (data.right != null) { - if (data.right.control != null) { - TableItem item = table.getItem (i); - String controlString = item.getText (RIGHT_COL); - int index = new Integer (controlString.substring (controlString.indexOf (',') - 1, controlString.indexOf (','))).intValue (); - code.append ("\t\tdata.right = new FormAttachment (" + names [index] + ", " + data.right.offset + ", SWT." + alignmentString (data.right.alignment) + ");\n"); + if(data.right != null) { + if(data.right.control != null) { + TableItem item = table.getItem(i); + String controlString = item.getText(RIGHT_COL); + int index = new Integer(controlString.substring(controlString.indexOf(',') - 1, controlString.indexOf (','))).intValue(); + code.append("\t\tdata.right = new FormAttachment (" + names[index] + ", " + data.right.offset + ", SWT." + alignmentString(data.right.alignment) + ");\n"); } else { - code.append ("\t\tdata.right = new FormAttachment (" + data.right.numerator + ", " + data.right.offset + ");\n"); + code.append("\t\tdata.right = new FormAttachment (" + data.right.numerator + ", " + data.right.offset + ");\n"); } } - if (data.top != null) { - if (data.top.control != null) { - TableItem item = table.getItem (i); - String controlString = item.getText (TOP_COL); - int index = new Integer (controlString.substring (controlString.indexOf (',') - 1, controlString.indexOf (','))).intValue (); - code.append ("\t\tdata.top = new FormAttachment (" + names [index] + ", " + data.top.offset + ", SWT." + alignmentString (data.top.alignment) + ");\n"); + if(data.top != null) { + if(data.top.control != null) { + TableItem item = table.getItem(i); + String controlString = item.getText(TOP_COL); + int index = new Integer(controlString.substring(controlString.indexOf (',') - 1, controlString.indexOf(','))).intValue(); + code.append("\t\tdata.top = new FormAttachment (" + names [index] + ", " + data.top.offset + ", SWT." + alignmentString(data.top.alignment) + ");\n"); } else { - if (data.bottom != null || (data.top.numerator != 0 ||data.top.offset != 0)) { - code.append ("\t\tdata.top = new FormAttachment (" + data.top.numerator + ", " + data.top.offset + ");\n"); + if(data.bottom != null || (data.top.numerator != 0 ||data.top.offset != 0)) { + code.append("\t\tdata.top = new FormAttachment (" + data.top.numerator + ", " + data.top.offset + ");\n"); } } } if (data.bottom != null) { if (data.bottom.control != null) { - TableItem item = table.getItem (i); - 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"); + TableItem item = table.getItem(i); + 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\tdata.bottom = new FormAttachment (" + data.bottom.numerator + ", " + data.bottom.offset + ");\n"); } } - code.append ("\t\t" + names [i] + ".setLayoutData (data);\n"); + code.append("\t\t" + names[i] + ".setLayoutData (data);\n"); } } return code; @@ -397,173 +438,170 @@ class FormLayoutTab extends Tab { * Returns the layout data field names. */ String [] getLayoutDataFieldNames() { - return new String [] { - "", - "Control", + return new String[] { + "Control Name", + "Control Type", "width", "height", - "left", - "right", - "top", - "bottom" + "marginLeft", + "marginRight", + "marginTop", + "marginBottom" }; } /** * Gets the text for the tab folder item. */ - String getTabText () { + String getTabText() { return "FormLayout"; } /** * Takes information from TableEditors and stores it. */ - void resetEditors () { - resetEditors (false); + void resetEditors() { + resetEditors(false); } - void resetEditors (boolean tab) { - TableItem oldItem = comboEditor.getItem (); - if (oldItem != null) { - int row = table.indexOf (oldItem); + 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)); + new String (nameText.getText()); + } catch(NumberFormatException e) { + nameText.setText(oldItem.getText(NAME_COL)); } try { - new Integer (heightText.getText ()).intValue (); - } catch (NumberFormatException e) { - heightText.setText (oldItem.getText (HEIGHT_COL)); + new Integer(widthText.getText()).intValue(); + } catch(NumberFormatException e) { + widthText.setText(oldItem.getText(WIDTH_COL)); } - String [] insert = new String [] {String.valueOf (row), combo.getText (), widthText.getText (), heightText.getText ()}; + try { + new Integer(heightText.getText()).intValue(); + } catch(NumberFormatException e) { + heightText.setText (oldItem.getText(HEIGHT_COL)); + } + String[] insert = new String[] {nameText.getText(), 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]); + oldItem.setText(i, ((String[])data.elementAt(row))[i]); } - if (!tab) disposeEditors (); + if (!tab) disposeEditors(); } - setLayoutState (); - refreshLayoutComposite (); - setLayoutData (); - layoutComposite.layout (true); - layoutGroup.layout (true); + + 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) { + FormAttachment setAttachment(String attachment) { String control, align; int position, offset; - int comma = attachment.indexOf (','); - char first = attachment.charAt (0); - if (Character.isLetter(first)) { + int comma = attachment.indexOf(','); + char first = attachment.charAt(0); + if(Character.isLetter(first)) { /* Case where there is a control */ - control = attachment.substring (0, comma); + control = attachment.substring(0, comma); int i = 0; - while (i < control.length () && !Character.isDigit (control.charAt (i))) { + 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 (':'); + 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 = 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)); - } - /* 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; + 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); } - 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 (); + 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 (); + 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; + 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); + 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) { + 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); + 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); + 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); + 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); + children[i].setLayoutData(data); } } /** * Sets the state of the layout. */ - void setLayoutState () { + 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); - } - } - + formLayout.marginWidth = marginWidth.getSelection(); + formLayout.marginHeight = marginHeight.getSelection(); + formLayout.spacing = spacing.getSelection(); + } /** * <code>AttachDialog</code> is the class that creates a @@ -575,184 +613,172 @@ class FormLayoutTab extends Tab { String controlInput, positionInput, alignmentInput, offsetInput; int col = 0; - public AttachDialog (Shell parent, int style) { - super (parent, style); + public AttachDialog(Shell parent, int style) { + super(parent, style); } - public AttachDialog (Shell parent) { - this (parent, 0); + public AttachDialog(Shell parent) { + this(parent, 0); } - public void setColumn (int col) { + 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); + public String open() { + Shell parent = getParent(); + final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); + shell.setText(getText()); + shell.setLayout(new GridLayout(3, true)); /* Find out what was previously set as an attachment */ - TableItem newItem = leftEditor.getItem (); - result = newItem.getText (col); + 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) { + if(oldAttach.length() != 0) { char first = oldAttach.charAt (0); - if (Character.isLetter(first)) { + 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 (':')); + 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 (' ')); + 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); + 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); + position.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); /* 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); + 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); + if(oldControl.length() != 0) control.setText(oldControl); + else control.select(0); + control.setEnabled(isControl); + control.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); /* Add alignment field */ - new Label (shell, SWT.NONE).setText (LayoutExample.getResourceString ("Alignment")); - final Combo alignment = new Combo (shell, SWT.NONE); + 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) { + 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"}; + 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); + alignment.setItems(alignmentValues); + alignment.setText("SWT." + oldAlign); + alignment.setEnabled(isControl); + alignment.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); /* 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); + new Label(shell, SWT.NONE).setText(LayoutExample.getResourceString("Offset")); + final Text offset = new Text(shell, SWT.SINGLE | SWT.BORDER); + offset.setText(oldOffset); + offset.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); /* Add listeners for choosing between position and control */ - posButton.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - position.setEnabled (true); - control.setEnabled (false); + 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); + 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) { + Button clear = new Button(shell, SWT.PUSH); + clear.setText(LayoutExample.getResourceString("Clear")); + clear.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false)); + clear.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { result = ""; - shell.close (); + 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"; + Button ok = new Button(shell, SWT.PUSH); + ok.setText(LayoutExample.getResourceString("OK")); + ok.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); + 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 (); + new Integer (positionInput).intValue(); } catch (NumberFormatException except) { positionInput = "0"; } - offsetInput = offset.getText (); - if (offsetInput.length () == 0) offsetInput = "0"; + offsetInput = offset.getText(); + if (offsetInput.length() == 0) offsetInput = "0"; try { - new Integer (offsetInput).intValue (); - } catch (NumberFormatException except) { + new Integer(offsetInput).intValue(); + } catch(NumberFormatException except) { offsetInput = "0"; } - if (posButton.getSelection() || controlInput.length () == 0) { + if(posButton.getSelection() || controlInput.length() == 0) { result = positionInput + "," + offsetInput; } else { result = controlInput + "," + offsetInput + ":" + alignmentInput; } - shell.close (); + 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 (); + Button cancel = new Button(shell, SWT.PUSH); + cancel.setText(LayoutExample.getResourceString("Cancel")); + cancel.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + shell.close(); } }); - shell.setDefaultButton (ok); - shell.pack (); + 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 (); + 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; diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/GridLayoutTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/GridLayoutTab.java index bc26f0ae40..74bc04157c 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/GridLayoutTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/GridLayoutTab.java @@ -20,19 +20,20 @@ import org.eclipse.swt.graphics.*; class GridLayoutTab extends Tab { /* Controls for setting layout parameters */ - Text numColumns; + Spinner numColumns; Button makeColumnsEqualWidth; - Combo marginHeight, marginWidth, horizontalSpacing, verticalSpacing; + Spinner marginWidth, marginHeight, horizontalSpacing, verticalSpacing; /* The example layout instance */ GridLayout gridLayout; /* TableEditors and related controls*/ - TableEditor comboEditor, widthEditor, heightEditor; + TableEditor nameEditor, comboEditor, widthEditor, heightEditor; TableEditor vAlignEditor, hAlignEditor, hIndentEditor; TableEditor hSpanEditor, vSpanEditor, hGrabEditor, vGrabEditor; CCombo combo, vAlign, hAlign, hGrab, vGrab; - Text widthText, heightText, hIndent, hSpan, vSpan; - + Text nameText, widthText, heightText, hIndent, hSpan, vSpan; + int prevSelected = 0; /* Constants */ + final int NAME_COL = 0; final int COMBO_COL = 1; final int WIDTH_COL = 2; final int HEIGHT_COL = 3; @@ -49,148 +50,155 @@ class GridLayoutTab extends Tab { /** * Creates the Tab within a given instance of LayoutExample. */ - GridLayoutTab(LayoutExample instance) { - super(instance); + GridLayoutTab() { } /** * Creates the widgets in the "child" group. */ - void createChildWidgets () { + void createChildWidgets() { /* Create the TraverseListener */ - final TraverseListener traverseListener = new TraverseListener () { - public void keyTraversed (TraverseEvent e) { - if (e.detail == SWT.TRAVERSE_RETURN || e.detail == SWT.TRAVERSE_TAB_NEXT) - resetEditors (); - if (e.detail == SWT.TRAVERSE_ESCAPE) - disposeEditors (); + final TraverseListener traverseListener = new TraverseListener() { + public void keyTraversed(TraverseEvent e) { + if(e.detail == SWT.TRAVERSE_RETURN || e.detail == SWT.TRAVERSE_TAB_NEXT) + resetEditors(); + if(e.detail == SWT.TRAVERSE_ESCAPE) + disposeEditors(); } }; /* Add common controls */ - super.createChildWidgets (); + super.createChildWidgets(); /* Add TableEditors */ - comboEditor = new TableEditor (table); - widthEditor = new TableEditor (table); - heightEditor = new TableEditor (table); - vAlignEditor = new TableEditor (table); - hAlignEditor = new TableEditor (table); - hIndentEditor = new TableEditor (table); - hSpanEditor = new TableEditor (table); - vSpanEditor = new TableEditor (table); - hGrabEditor = new TableEditor (table); - vGrabEditor = new TableEditor (table); - table.addMouseListener (new MouseAdapter () { + nameEditor = new TableEditor(table); + comboEditor = new TableEditor(table); + widthEditor = new TableEditor(table); + heightEditor = new TableEditor(table); + vAlignEditor = new TableEditor(table); + hAlignEditor = new TableEditor(table); + hIndentEditor = new TableEditor(table); + hSpanEditor = new TableEditor(table); + vSpanEditor = new TableEditor(table); + hGrabEditor = new TableEditor(table); + vGrabEditor = new TableEditor(table); + table.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { resetEditors(); - index = table.getSelectionIndex (); - Point pt = new Point (e.x, e.y); - newItem = table.getItem (pt); - if (newItem == null) return; - TableItem oldItem = comboEditor.getItem (); - if (newItem == oldItem || newItem != lastSelected) { + index = table.getSelectionIndex(); + Point pt = new Point(e.x, e.y); + newItem = table.getItem(pt); + if(newItem == null) return; + TableItem oldItem = comboEditor.getItem(); + if(newItem == oldItem || newItem != lastSelected) { lastSelected = newItem; return; } - table.showSelection (); + table.showSelection(); - combo = new CCombo (table, SWT.READ_ONLY); - createComboEditor (combo, comboEditor); + nameText = new Text(table, SWT.SINGLE); + nameText.setText(((String [])data.elementAt(index))[NAME_COL]); + createTextEditor(nameText, nameEditor, NAME_COL); - widthText = new Text (table, SWT.SINGLE); - widthText.setText (((String [])data.elementAt (index)) [WIDTH_COL]); - createTextEditor (widthText, widthEditor, WIDTH_COL); + combo = new CCombo(table, SWT.READ_ONLY); + createComboEditor(combo, comboEditor); + + widthText = new Text(table, SWT.SINGLE); + widthText.setText(((String [])data.elementAt(index))[WIDTH_COL]); + createTextEditor(widthText, widthEditor, WIDTH_COL); - heightText = new Text (table, SWT.SINGLE); - heightText.setText (((String [])data.elementAt (index)) [HEIGHT_COL]); - createTextEditor (heightText, heightEditor, HEIGHT_COL); - String [] alignValues = new String [] {"BEGINNING","CENTER","END","FILL"}; - hAlign = new CCombo (table, SWT.NONE); - hAlign.setItems (alignValues); - hAlign.setText (newItem.getText (HALIGN_COL)); + heightText = new Text(table, SWT.SINGLE); + heightText.setText(((String[])data.elementAt(index))[HEIGHT_COL]); + createTextEditor(heightText, heightEditor, HEIGHT_COL); + String[] alignValues = new String[] {"BEGINNING","CENTER","END","FILL"}; + hAlign = new CCombo(table, SWT.NONE); + hAlign.setItems(alignValues); + hAlign.setText(newItem.getText(HALIGN_COL)); hAlignEditor.horizontalAlignment = SWT.LEFT; hAlignEditor.grabHorizontal = true; hAlignEditor.minimumWidth = 50; - hAlignEditor.setEditor (hAlign, newItem, HALIGN_COL); - hAlign.addTraverseListener (traverseListener); + hAlignEditor.setEditor(hAlign, newItem, HALIGN_COL); + hAlign.addTraverseListener(traverseListener); - vAlign = new CCombo (table, SWT.NONE); - vAlign.setItems (alignValues); - vAlign.setText (newItem.getText (VALIGN_COL)); + vAlign = new CCombo(table, SWT.NONE); + vAlign.setItems(alignValues); + vAlign.setText(newItem.getText(VALIGN_COL)); vAlignEditor.horizontalAlignment = SWT.LEFT; vAlignEditor.grabHorizontal = true; vAlignEditor.minimumWidth = 50; - vAlignEditor.setEditor (vAlign, newItem, VALIGN_COL); - vAlign.addTraverseListener (traverseListener); + vAlignEditor.setEditor(vAlign, newItem, VALIGN_COL); + vAlign.addTraverseListener(traverseListener); - hIndent = new Text (table, SWT.SINGLE); - hIndent.setText (((String [])data.elementAt (index)) [HINDENT_COL]); - createTextEditor (hIndent, hIndentEditor, HINDENT_COL); + hIndent = new Text(table, SWT.SINGLE); + hIndent.setText(((String[])data.elementAt(index))[HINDENT_COL]); + createTextEditor(hIndent, hIndentEditor, HINDENT_COL); - hSpan = new Text (table, SWT.SINGLE); + hSpan = new Text(table, SWT.SINGLE); hSpan.setText (((String [])data.elementAt (index)) [HSPAN_COL]); createTextEditor (hSpan, hSpanEditor, HSPAN_COL); vSpan = new Text (table, SWT.SINGLE); - vSpan.setText (((String [])data.elementAt (index)) [VSPAN_COL]); - createTextEditor (vSpan, vSpanEditor, VSPAN_COL); + vSpan.setText(((String[])data.elementAt(index))[VSPAN_COL]); + createTextEditor(vSpan, vSpanEditor, VSPAN_COL); - String [] boolValues = new String [] {"false","true"}; - hGrab = new CCombo (table, SWT.NONE); - hGrab.setItems (boolValues); - hGrab.setText (newItem.getText (HGRAB_COL)); + String[] boolValues = new String[] {"false","true"}; + hGrab = new CCombo(table, SWT.NONE); + hGrab.setItems(boolValues); + hGrab.setText(newItem.getText (HGRAB_COL)); hGrabEditor.horizontalAlignment = SWT.LEFT; hGrabEditor.grabHorizontal = true; hGrabEditor.minimumWidth = 50; - hGrabEditor.setEditor (hGrab, newItem, HGRAB_COL); - hGrab.addTraverseListener (traverseListener); + hGrabEditor.setEditor(hGrab, newItem, HGRAB_COL); + hGrab.addTraverseListener(traverseListener); - vGrab = new CCombo (table, SWT.NONE); - vGrab.setItems (boolValues); - vGrab.setText (newItem.getText (VGRAB_COL)); + vGrab = new CCombo(table, SWT.NONE); + vGrab.setItems(boolValues); + vGrab.setText(newItem.getText (VGRAB_COL)); vGrabEditor.horizontalAlignment = SWT.LEFT; vGrabEditor.grabHorizontal = true; vGrabEditor.minimumWidth = 50; - vGrabEditor.setEditor (vGrab, newItem, VGRAB_COL); - vGrab.addTraverseListener (traverseListener); + vGrabEditor.setEditor(vGrab, newItem, VGRAB_COL); + vGrab.addTraverseListener(traverseListener); - for (int i=0; i<table.getColumnCount (); i++) { - Rectangle rect = newItem.getBounds (i); - if (rect.contains (pt)) { + for(int i=0; i<table.getColumnCount(); i++) { + Rectangle rect = newItem.getBounds(i); + if(rect.contains(pt)) { switch (i) { + case NAME_COL: + nameText.setFocus(); + break; case COMBO_COL : - combo.setFocus (); + combo.setFocus(); break; case WIDTH_COL : - widthText.setFocus (); + widthText.setFocus(); break; case HEIGHT_COL : - heightText.setFocus (); + heightText.setFocus(); break; case HALIGN_COL : - hAlign.setFocus (); + hAlign.setFocus(); break; case VALIGN_COL : - vAlign.setFocus (); + vAlign.setFocus(); break; case HINDENT_COL : - hIndent.setFocus (); + hIndent.setFocus(); break; case HSPAN_COL : - hSpan.setFocus (); + hSpan.setFocus(); break; case VSPAN_COL : - vSpan.setFocus (); + vSpan.setFocus(); break; case HGRAB_COL : - hGrab.setFocus (); + hGrab.setFocus(); break; case VGRAB_COL : - vGrab.setFocus (); + vGrab.setFocus(); break; default : - resetEditors (); + resetEditors(); break; } } @@ -198,17 +206,56 @@ class GridLayoutTab extends Tab { } }); - /* Add listener to add an element to the table */ - add.addSelectionListener(new SelectionAdapter () { - public void widgetSelected(SelectionEvent e) { - TableItem item = new TableItem (table, 0); - String [] insert = new String [] { - String.valueOf (table.indexOf (item)), "Button", - "-1","-1","BEGINNING","CENTER", - "0","1","1","false","false"}; - item.setText (insert); - data.addElement (insert); - resetEditors (); + add.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + if(event.detail == SWT.ARROW) { + ToolItem item = (ToolItem)event.widget; + ToolBar bar = item.getParent(); + Display display = bar.getDisplay(); + Shell shell = bar.getShell(); + Menu menu = new Menu(shell, SWT.POP_UP); + for(int i = 0; i < OPTIONS.length; i++) { + final MenuItem newItem = new MenuItem(menu, SWT.RADIO); + newItem.setText(OPTIONS[i]); + newItem.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent event) { + MenuItem menuItem = (MenuItem)event.widget; + if(menuItem.getSelection()) { + Menu menu = menuItem.getParent(); + prevSelected = menu.indexOf(menuItem); + TableItem item = new TableItem(table, SWT.NONE); + String name = menuItem.getText().toLowerCase() + String.valueOf(table.indexOf(item)); + String[] insert = new String[] {name, menuItem.getText(), + "-1","-1","BEGINNING","CENTER", + "0","1","1","false","false"}; + item.setText(insert); + data.addElement(insert); + resetEditors (); + } + } + }); + newItem.setSelection(i == prevSelected); + } + Point pt = display.map(bar, null, event.x, event.y); + menu.setLocation(pt.x, pt.y); + menu.setVisible(true); + while (menu != null && !menu.isDisposed() && menu.isVisible()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + menu.dispose(); + } else { + String selection = OPTIONS[prevSelected]; + TableItem item = new TableItem(table, 0); + String name = selection.toLowerCase() + String.valueOf(table.indexOf(item)); + String[] insert = new String[] {name, selection, + "-1","-1","BEGINNING","CENTER", + "0","1","1","false","false"}; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } } }); } @@ -216,194 +263,158 @@ class GridLayoutTab extends Tab { /** * Creates the control widgets. */ - void createControlWidgets () { - /* Rearrange the layout of the control group */ - size.setLayoutData (new GridData ()); - - /* Controls the margins and spacing of the GridLayout */ - String [] marginValues = new String [] {"0","3","5","10"}; - Group marginGroup = new Group (controlGroup, SWT.NONE); - marginGroup.setText (LayoutExample.getResourceString ("Margins_Spacing")); - GridData data = new GridData (GridData.FILL_HORIZONTAL); - data.verticalSpan = 2; - marginGroup.setLayoutData (data); - GridLayout layout = new GridLayout (); - layout.numColumns = 2; - marginGroup.setLayout (layout); - new Label (marginGroup, SWT.NONE).setText ("marginHeight"); - marginHeight = new Combo (marginGroup, SWT.NONE); - marginHeight.setItems (marginValues); - marginHeight.select (2); - data = new GridData (GridData.FILL_HORIZONTAL); - data.widthHint = 60; - marginHeight.setLayoutData (data); - marginHeight.addSelectionListener (selectionListener); - marginHeight.addTraverseListener (traverseListener); - new Label (marginGroup, SWT.NONE).setText ("marginWidth"); - marginWidth = new Combo (marginGroup, SWT.NONE); - marginWidth.setItems (marginValues); - marginWidth.select (2); - data = new GridData (GridData.FILL_HORIZONTAL); - data.widthHint = 60; - marginWidth.setLayoutData (data); - marginWidth.addSelectionListener (selectionListener); - marginWidth.addTraverseListener (traverseListener); - new Label (marginGroup, SWT.NONE).setText ("horizontalSpacing"); - horizontalSpacing = new Combo (marginGroup, SWT.NONE); - horizontalSpacing.setItems (marginValues); - horizontalSpacing.select (2); - data = new GridData (GridData.FILL_HORIZONTAL); - data.widthHint = 60; - horizontalSpacing.setLayoutData (data); - horizontalSpacing.addSelectionListener (selectionListener); - horizontalSpacing.addTraverseListener (traverseListener); - new Label (marginGroup, SWT.NONE).setText ("verticalSpacing"); - verticalSpacing = new Combo (marginGroup, SWT.NONE); - verticalSpacing.setItems (marginValues); - verticalSpacing.select (2); - data = new GridData (GridData.FILL_HORIZONTAL); - data.widthHint = 60; - verticalSpacing.setLayoutData (data); - verticalSpacing.addSelectionListener (selectionListener); - verticalSpacing.addTraverseListener (traverseListener); - + void createControlWidgets() { /* Controls the columns in the GridLayout */ - Group columnGroup = new Group (controlGroup, SWT.NONE); - columnGroup.setText (LayoutExample.getResourceString ("Columns")); - layout = new GridLayout (); - layout.numColumns = 2; - columnGroup.setLayout (layout); - data = new GridData (GridData.VERTICAL_ALIGN_FILL); - columnGroup.setLayoutData (data); - numColumns = new Text (columnGroup, SWT.BORDER); - numColumns.setText ("1"); - numColumns.addSelectionListener (selectionListener); - numColumns.addTraverseListener (traverseListener); - data = new GridData (GridData.FILL_HORIZONTAL); - data.widthHint = 15; - numColumns.setLayoutData (data); - new Label (columnGroup, SWT.NONE).setText ("numColumns"); - makeColumnsEqualWidth = new Button (columnGroup, SWT.CHECK); - makeColumnsEqualWidth.setText ("makeColumnsEqualWidth"); - makeColumnsEqualWidth.addSelectionListener (selectionListener); - data = new GridData (GridData.FILL_HORIZONTAL); - data.horizontalSpan = 2; - data.horizontalIndent = 14; - makeColumnsEqualWidth.setLayoutData (data); + Group columnGroup = new Group(controlGroup, SWT.NONE); + columnGroup.setText(LayoutExample.getResourceString ("Columns")); + columnGroup.setLayout(new GridLayout(2, false)); + columnGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); + new Label(columnGroup, SWT.NONE).setText("Number of Columns"); + numColumns = new Spinner(columnGroup, SWT.BORDER); + numColumns.setMinimum(1); + numColumns.addSelectionListener(selectionListener); + makeColumnsEqualWidth = new Button(columnGroup, SWT.CHECK); + makeColumnsEqualWidth.setText("Make Columns Equal Width"); + makeColumnsEqualWidth.addSelectionListener(selectionListener); + makeColumnsEqualWidth.setEnabled(false); + makeColumnsEqualWidth.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1)); + /* Controls the margins and spacing of the GridLayout */ + Group marginGroup = new Group(controlGroup, SWT.NONE); + marginGroup.setText (LayoutExample.getResourceString("Margins_Spacing")); + marginGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); + marginGroup.setLayout(new GridLayout(2, false)); + new Label (marginGroup, SWT.NONE).setText("Margin Width"); + marginWidth = new Spinner (marginGroup, SWT.BORDER); + marginWidth.setSelection(5); + marginWidth.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Margin Height"); + marginHeight = new Spinner(marginGroup, SWT.BORDER); + marginHeight.setSelection(5); + marginHeight.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Horizontal Spacing"); + horizontalSpacing = new Spinner(marginGroup, SWT.BORDER); + horizontalSpacing.setSelection(5); + horizontalSpacing.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Vertical Spacing"); + verticalSpacing = new Spinner(marginGroup, SWT.BORDER); + verticalSpacing.setSelection(5); + verticalSpacing.addSelectionListener(selectionListener); + /* Add common controls */ - super.createControlWidgets (); + super.createControlWidgets(); controlGroup.pack(); } /** * Creates the example layout. */ - void createLayout () { - gridLayout = new GridLayout (); - layoutComposite.setLayout (gridLayout); + void createLayout() { + gridLayout = new GridLayout(); + layoutComposite.setLayout(gridLayout); } /** * Disposes the editors without placing their contents * into the table. */ - void disposeEditors () { - comboEditor.setEditor (null, null, -1); - combo.dispose (); - widthText.dispose (); - heightText.dispose (); - hAlign.dispose (); - vAlign.dispose (); - hIndent.dispose (); - hSpan.dispose (); - vSpan.dispose (); - hGrab.dispose (); - vGrab.dispose (); + void disposeEditors() { + comboEditor.setEditor(null, null, -1); + combo.dispose(); + nameText.dispose(); + widthText.dispose(); + heightText.dispose(); + hAlign.dispose(); + vAlign.dispose(); + hIndent.dispose(); + hSpan.dispose(); + vSpan.dispose(); + hGrab.dispose(); + vGrab.dispose(); } /** * Generates code for the example layout. */ - StringBuffer generateLayoutCode () { - StringBuffer code = new StringBuffer (); - code.append ("\t\tGridLayout gridLayout = new GridLayout ();\n"); - if (gridLayout.numColumns != 1) { - code.append ("\t\tgridLayout.numColumns = " + gridLayout.numColumns + ";\n"); + StringBuffer generateLayoutCode() { + StringBuffer code = new StringBuffer(); + code.append("\t\tGridLayout gridLayout = new GridLayout ();\n"); + if(gridLayout.numColumns != 1) { + code.append("\t\tgridLayout.numColumns = " + gridLayout.numColumns + ";\n"); } - if (gridLayout.makeColumnsEqualWidth) { - code.append ("\t\tgridLayout.makeColumnsEqualWidth = true;\n"); + if(gridLayout.makeColumnsEqualWidth) { + code.append("\t\tgridLayout.makeColumnsEqualWidth = true;\n"); } - if (gridLayout.marginHeight != 5) { - code.append ("\t\tgridLayout.marginHeight = " + gridLayout.marginHeight + ";\n"); + if(gridLayout.marginWidth != 5) { + code.append("\t\tgridLayout.marginWidth = " + gridLayout.marginWidth + ";\n"); } - if (gridLayout.marginWidth != 5) { - code.append ("\t\tgridLayout.marginWidth = " + gridLayout.marginWidth + ";\n"); + if(gridLayout.marginHeight != 5) { + code.append("\t\tgridLayout.marginHeight = " + gridLayout.marginHeight + ";\n"); } - if (gridLayout.horizontalSpacing != 5) { - code.append ("\t\tgridLayout.horizontalSpacing = " + gridLayout.horizontalSpacing + ";\n"); + if(gridLayout.horizontalSpacing != 5) { + code.append("\t\tgridLayout.horizontalSpacing = " + gridLayout.horizontalSpacing + ";\n"); } - if (gridLayout.verticalSpacing != 5) { - code.append ("\t\tgridLayout.verticalSpacing = " + gridLayout.verticalSpacing + ";\n"); + if(gridLayout.verticalSpacing != 5) { + code.append("\t\tgridLayout.verticalSpacing = " + gridLayout.verticalSpacing + ";\n"); } - code.append ("\t\tshell.setLayout (gridLayout);\n"); + code.append("\t\tshell.setLayout (gridLayout);\n"); boolean first = true; - for (int i = 0; i < children.length; i++) { - Control control = children [i]; - code.append (getChildCode (control, i)); - GridData data = (GridData) control.getLayoutData (); - if (data != null) { - code.append ("\t\t"); - if (first) { - code.append ("GridData "); + for(int i = 0; i < children.length; i++) { + Control control = children[i]; + code.append(getChildCode(control, i)); + GridData data = (GridData)control.getLayoutData(); + if(data != null) { + code.append("\t\t"); + if(first) { + code.append("GridData "); first = false; } code.append ("data = new GridData ();\n"); if (data.widthHint != SWT.DEFAULT) { - code.append ("\t\tdata.widthHint = " + data.widthHint + ";\n"); + code.append("\t\tdata.widthHint = " + data.widthHint + ";\n"); } if (data.heightHint != SWT.DEFAULT) { - code.append ("\t\tdata.heightHint = " + data.heightHint + ";\n"); + code.append("\t\tdata.heightHint = " + data.heightHint + ";\n"); } - if (data.horizontalAlignment != GridData.BEGINNING) { + if(data.horizontalAlignment != SWT.BEGINNING) { String alignment; int hAlignment = data.horizontalAlignment; - if (hAlignment == GridData.CENTER) alignment = "GridData.CENTER"; - else if (hAlignment == GridData.END) alignment = "GridData.END"; - else alignment = "GridData.FILL"; - code.append ("\t\tdata.horizontalAlignment = " + alignment + ";\n"); + if(hAlignment == SWT.CENTER) alignment = "SWT.CENTER"; + else if(hAlignment == SWT.END) alignment = "SWT.END"; + else alignment = "SWT.FILL"; + code.append("\t\tdata.horizontalAlignment = " + alignment + ";\n"); } - if (data.verticalAlignment != GridData.CENTER) { + if(data.verticalAlignment != SWT.CENTER) { String alignment; int vAlignment = data.verticalAlignment; - if (vAlignment == GridData.BEGINNING) alignment = "GridData.BEGINNING"; - else if (vAlignment == GridData.END) alignment = "GridData.END"; - else alignment = "GridData.FILL"; - code.append ("\t\tdata.verticalAlignment = " + alignment + ";\n"); + if(vAlignment == SWT.BEGINNING) alignment = "SWT.BEGINNING"; + else if(vAlignment == SWT.END) alignment = "SWT.END"; + else alignment = "SWT.FILL"; + code.append("\t\tdata.verticalAlignment = " + alignment + ";\n"); } - if (data.horizontalIndent != 0) { + if(data.horizontalIndent != 0) { code.append ("\t\tdata.horizontalIndent = " + data.horizontalIndent + ";\n"); } - if (data.horizontalSpan != 1) { + if(data.horizontalSpan != 1) { code.append ("\t\tdata.horizontalSpan = " + data.horizontalSpan + ";\n"); } - if (data.verticalSpan != 1) { + if(data.verticalSpan != 1) { code.append ("\t\tdata.verticalSpan = " + data.verticalSpan + ";\n"); } - if (data.grabExcessHorizontalSpace) { + if(data.grabExcessHorizontalSpace) { code.append ("\t\tdata.grabExcessHorizontalSpace = true;\n"); } - if (data.grabExcessVerticalSpace) { + if(data.grabExcessVerticalSpace) { code.append ("\t\tdata.grabExcessVerticalSpace = true;\n"); } - if (code.substring (code.length () - 33).equals ("GridData data = new GridData ();\n")) { - code.delete (code.length () - 33, code.length ()); + if(code.substring(code.length() - 33).equals("GridData data = new GridData ();\n")) { + code.delete(code.length() - 33, code.length()); first = true; - } else if (code.substring (code.length () - 24).equals ("data = new GridData ();\n")) { - code.delete (code.length () - 24, code.length ()); - } else { - code.append ("\t\t" + names [i] + ".setLayoutData (data);\n"); + } else if(code.substring(code.length () - 24).equals("data = new GridData ();\n")) { + code.delete(code.length() - 24, code.length()); + } else{ + code.append("\t\t" + names[i] + ".setLayoutData (data);\n"); } } } @@ -413,10 +424,10 @@ class GridLayoutTab extends Tab { /** * Returns the layout data field names. */ - String [] getLayoutDataFieldNames() { - return new String [] { - "", - "Control", + String[] getLayoutDataFieldNames() { + return new String[] { + "Control Name", + "Control Type", "width", "height", "horizontalAlignment", @@ -432,159 +443,142 @@ class GridLayoutTab extends Tab { /** * Gets the text for the tab folder item. */ - String getTabText () { + String getTabText() { return "GridLayout"; } /** * Takes information from TableEditors and stores it. */ - void resetEditors () { - resetEditors (false); + void resetEditors() { + resetEditors(false); } - void resetEditors (boolean tab) { - TableItem oldItem = comboEditor.getItem (); - if (oldItem != null) { - int row = table.indexOf (oldItem); + void resetEditors(boolean tab) { + TableItem oldItem = comboEditor.getItem(); + if(oldItem != null) { + int row = table.indexOf(oldItem); + /** Make sure user enters a valid data*/ + try { + new String(nameText.getText ()); + } catch(NumberFormatException e) { + nameText.setText(oldItem.getText (NAME_COL)); + } try { - new Integer (widthText.getText ()).intValue (); - } catch (NumberFormatException e) { - widthText.setText (oldItem.getText (WIDTH_COL)); + 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)); + new Integer(heightText.getText()).intValue(); + } catch(NumberFormatException e) { + heightText.setText(oldItem.getText(HEIGHT_COL)); } try { - new Integer (hIndent.getText ()).intValue (); - } catch (NumberFormatException e) { - hIndent.setText (oldItem.getText (HINDENT_COL)); + new Integer(hIndent.getText()).intValue(); + } catch(NumberFormatException e) { + hIndent.setText(oldItem.getText(HINDENT_COL)); } try { - new Integer (hSpan.getText ()).intValue (); - } catch (NumberFormatException e) { - hSpan.setText (oldItem.getText (HSPAN_COL)); + new Integer(hSpan.getText()).intValue(); + } catch(NumberFormatException e) { + hSpan.setText(oldItem.getText(HSPAN_COL)); } try { - new Integer (vSpan.getText ()).intValue (); - } catch (NumberFormatException e) { - vSpan.setText (oldItem.getText (VSPAN_COL)); + new Integer(vSpan.getText()).intValue(); + } catch(NumberFormatException e) { + vSpan.setText(oldItem.getText(VSPAN_COL)); } - String [] insert = new String [] { - String.valueOf (row), combo.getText (), widthText.getText (), heightText.getText (), - hAlign.getText (), vAlign.getText (), hIndent.getText (), - hSpan.getText (), vSpan.getText (), hGrab.getText (), vGrab.getText () + String[] insert = new String[] { + nameText.getText(), combo.getText(), widthText.getText(), heightText.getText(), + hAlign.getText(), vAlign.getText(), hIndent.getText(), + hSpan.getText(), vSpan.getText(), hGrab.getText(), vGrab.getText() }; - data.setElementAt (insert, row); - for (int i = 0; i < TOTAL_COLS; i++) { - oldItem.setText (i, ((String [])data.elementAt (row)) [i]); + data.setElementAt(insert, row); + for(int i = 0; i < TOTAL_COLS; i++) { + oldItem.setText(i, ((String[])data.elementAt(row))[i]); } - if (!tab) disposeEditors (); + if(!tab) disposeEditors(); } - setLayoutState (); - refreshLayoutComposite (); - setLayoutData (); - layoutComposite.layout (true); - layoutGroup.layout (true); + setLayoutState(); + refreshLayoutComposite(); + setLayoutData(); + layoutComposite.layout(true); + layoutGroup.layout(true); } /** * Sets the layout data for the children of the layout. */ - void setLayoutData () { - Control [] children = layoutComposite.getChildren (); - TableItem [] items = table.getItems (); + void setLayoutData() { + Control[] children = layoutComposite.getChildren(); + TableItem[] items = table.getItems(); GridData data; int hIndent, hSpan, vSpan; String vAlign, hAlign, vGrab, hGrab; - for (int i = 0; i < children.length; i++) { - data = new GridData (); + for(int i = 0; i < children.length; i++) { + data = new GridData(); /* Set widthHint and heightHint */ - data.widthHint = new Integer (items [i].getText (WIDTH_COL)).intValue (); - data.heightHint = new Integer (items [i].getText (HEIGHT_COL)).intValue (); + data.widthHint = new Integer(items [i].getText(WIDTH_COL)).intValue(); + data.heightHint = new Integer(items [i].getText(HEIGHT_COL)).intValue(); /* Set vertical alignment and horizontal alignment */ - hAlign = items [i].getText (HALIGN_COL); - if (hAlign.equals ("CENTER")) { - data.horizontalAlignment = GridData.CENTER; - } else if (hAlign.equals ("END")) { - data.horizontalAlignment = GridData.END; - } else if (hAlign.equals ("FILL")) { - data.horizontalAlignment = GridData.FILL; + hAlign = items[i].getText(HALIGN_COL); + if (hAlign.equals("CENTER")) { + data.horizontalAlignment = SWT.CENTER; + } else if(hAlign.equals("END")) { + data.horizontalAlignment = SWT.END; + } else if(hAlign.equals("FILL")) { + data.horizontalAlignment = SWT.FILL; } else { - data.horizontalAlignment = GridData.BEGINNING; + data.horizontalAlignment = SWT.BEGINNING; } - vAlign = items [i].getText (VALIGN_COL); - if (vAlign.equals ("BEGINNING")) { - data.verticalAlignment = GridData.BEGINNING; - } else if (vAlign.equals ("END")) { - data.verticalAlignment = GridData.END; - } else if (vAlign.equals ("FILL")) { - data.verticalAlignment = GridData.FILL; + vAlign = items [i].getText(VALIGN_COL); + if (vAlign.equals("BEGINNING")) { + data.verticalAlignment = SWT.BEGINNING; + } else if (vAlign.equals("END")) { + data.verticalAlignment = SWT.END; + } else if (vAlign.equals("FILL")) { + data.verticalAlignment = SWT.FILL; } else { - data.verticalAlignment = GridData.CENTER; + data.verticalAlignment = SWT.CENTER; } /* Set indents and spans */ - hIndent = new Integer (items [i].getText (HINDENT_COL)).intValue (); + hIndent = new Integer(items[i].getText(HINDENT_COL)).intValue(); data.horizontalIndent = hIndent; - hSpan = new Integer (items [i].getText (HSPAN_COL)).intValue (); + hSpan = new Integer(items [i].getText(HSPAN_COL)).intValue(); data.horizontalSpan = hSpan; - vSpan = new Integer (items [i].getText (VSPAN_COL)).intValue (); + vSpan = new Integer(items[i].getText(VSPAN_COL)).intValue(); data.verticalSpan = vSpan; /* Set grabbers */ - hGrab = items [i].getText (HGRAB_COL); - if (hGrab.equals ("true")) { + hGrab = items[i].getText(HGRAB_COL); + if(hGrab.equals("true")) { data.grabExcessHorizontalSpace = true; } else { data.grabExcessHorizontalSpace = false; } - vGrab = items [i].getText (VGRAB_COL); - if (vGrab.equals ("true")) { + vGrab = items [i].getText(VGRAB_COL); + if (vGrab.equals("true")) { data.grabExcessVerticalSpace = true; } else { data.grabExcessVerticalSpace = false; } - children [i].setLayoutData (data); + children[i].setLayoutData (data); } } /** * Sets the state of the layout. */ - void setLayoutState () { + void setLayoutState() { /* Set the columns for the layout */ - try { - gridLayout.numColumns = new Integer (numColumns.getText ()).intValue (); - } catch (NumberFormatException e) { - gridLayout.numColumns = 1; - } - gridLayout.makeColumnsEqualWidth = makeColumnsEqualWidth.getSelection (); + gridLayout.numColumns = numColumns.getSelection(); + gridLayout.makeColumnsEqualWidth = makeColumnsEqualWidth.getSelection(); + makeColumnsEqualWidth.setEnabled(numColumns.getSelection() > 1); /* Set the margins and spacing */ - try { - gridLayout.marginHeight = new Integer (marginHeight.getText ()).intValue (); - } catch (NumberFormatException e) { - gridLayout.marginHeight = 5; - marginHeight.select (2); - } - try { - gridLayout.marginWidth = new Integer (marginWidth.getText ()).intValue (); - } catch (NumberFormatException e) { - gridLayout.marginWidth = 5; - marginWidth.select (2); - } - try { - gridLayout.horizontalSpacing = new Integer (horizontalSpacing.getText ()).intValue (); - } catch (NumberFormatException e) { - gridLayout.horizontalSpacing = 5; - horizontalSpacing.select (2); - } - try { - gridLayout.verticalSpacing = new Integer (verticalSpacing.getText ()).intValue (); - } catch (NumberFormatException e) { - gridLayout.verticalSpacing = 5; - verticalSpacing.select (2); - } + gridLayout.marginWidth = marginWidth.getSelection(); + gridLayout.marginHeight = marginHeight.getSelection(); + gridLayout.horizontalSpacing = horizontalSpacing.getSelection(); + gridLayout.verticalSpacing = verticalSpacing.getSelection(); } } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/LayoutExample.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/LayoutExample.java index 2f5a48990f..4115c0c31e 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/LayoutExample.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/LayoutExample.java @@ -10,7 +10,6 @@ *******************************************************************************/ package org.eclipse.swt.examples.layoutexample; - import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; @@ -30,36 +29,22 @@ public class LayoutExample { * @param parent the container of the example */ public LayoutExample(Composite parent) { - tabFolder = new TabFolder (parent, SWT.NONE); - Tab [] tabs = new Tab [] { - new FillLayoutTab (this), - new RowLayoutTab (this), - new GridLayoutTab (this), - new FormLayoutTab (this), + tabFolder = new TabFolder(parent, SWT.NULL); + Tab[] tabs = new Tab[] { + new FillLayoutTab(), + new RowLayoutTab(), + new GridLayoutTab(), + new FormLayoutTab(), + new StackLayoutTab() }; - for (int i=0; i<tabs.length; i++) { - TabItem item = new TabItem (tabFolder, SWT.NONE); - item.setText (tabs [i].getTabText ()); - item.setControl (tabs [i].createTabFolderPage (tabFolder)); + for(int i=0; i<tabs.length; i++) { + TabItem item = new TabItem(tabFolder, SWT.NULL); + item.setText(tabs [i].getTabText()); + item.setControl(tabs[i].createTabFolderPage(tabFolder)); } } /** - * Grabs input focus. - */ - public void setFocus() { - tabFolder.setFocus(); - } - - /** - * Disposes of all resources associated with a particular - * instance of the LayoutExample. - */ - public void dispose() { - tabFolder = null; - } - - /** * Invokes as a standalone program. */ public static void main(String[] args) { @@ -68,17 +53,17 @@ public class LayoutExample { shell.setLayout(new FillLayout()); new LayoutExample(shell); shell.setText(getResourceString("window.title")); - shell.addShellListener (new ShellAdapter () { + shell.addShellListener(new ShellAdapter() { public void shellClosed(ShellEvent e) { - Shell [] shells = display.getShells(); - for (int i = 0; i < shells.length; i++) { - if (shells [i] != shell) shells [i].close (); + Shell[] shells = display.getShells(); + for(int i = 0; i < shells.length; i++) { + if(shells[i] != shell) shells[i].close(); } } }); shell.open(); - while (! shell.isDisposed()) { - if (! display.readAndDispatch()) display.sleep(); + while(! shell.isDisposed()) { + if(! display.readAndDispatch()) display.sleep(); } } @@ -90,9 +75,9 @@ public class LayoutExample { static String getResourceString(String key) { try { return resourceBundle.getString(key); - } catch (MissingResourceException e) { + } catch(MissingResourceException e) { return key; - } catch (NullPointerException e) { + } catch(NullPointerException e) { return "!" + key + "!"; } } @@ -105,9 +90,9 @@ public class LayoutExample { static String getResourceString(String key, Object[] args) { try { return MessageFormat.format(getResourceString(key), args); - } catch (MissingResourceException e) { + } catch(MissingResourceException e) { return key; - } catch (NullPointerException e) { + } catch(NullPointerException e) { return "!" + key + "!"; } } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/RowLayoutTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/RowLayoutTab.java index 2d37352593..962159438f 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/RowLayoutTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/RowLayoutTab.java @@ -21,80 +21,87 @@ import org.eclipse.swt.custom.*; class RowLayoutTab extends Tab { /* Controls for setting layout parameters */ Button horizontal, vertical; - Button wrap, pack, justify; - Combo marginRight, marginLeft, marginTop, marginBottom, spacing; + Button wrap, pack, fill, justify; + Spinner marginWidth, marginHeight, marginRight, marginTop, marginLeft, marginBottom, spacing; /* The example layout instance */ RowLayout rowLayout; /* TableEditors and related controls*/ - TableEditor comboEditor, widthEditor, heightEditor; + TableEditor comboEditor, widthEditor, heightEditor, nameEditor; CCombo combo; - Text widthText, heightText; + Text nameText, widthText, heightText; + int prevSelected; /* Constants */ + final int NAME_COL = 0; final int COMBO_COL = 1; final int WIDTH_COL = 2; - final int HEIGHT_COL = 3; - + final int HEIGHT_COL = 3; final int TOTAL_COLS = 4; + /** * Creates the Tab within a given instance of LayoutExample. */ - RowLayoutTab(LayoutExample instance) { - super(instance); + RowLayoutTab() { } /** * Creates the widgets in the "child" group. */ - void createChildWidgets () { + void createChildWidgets() { /* Add common controls */ - super.createChildWidgets (); + super.createChildWidgets(); - /* Add TableEditors */ - comboEditor = new TableEditor (table); - widthEditor = new TableEditor (table); - heightEditor = new TableEditor (table); - table.addMouseListener (new MouseAdapter () { + /* Add TableEditors */ + nameEditor = new TableEditor(table); + comboEditor = new TableEditor(table); + widthEditor = new TableEditor(table); + heightEditor = new TableEditor(table); + table.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { resetEditors(); - index = table.getSelectionIndex (); - Point pt = new Point (e.x, e.y); - newItem = table.getItem (pt); - if (newItem == null) return; - TableItem oldItem = comboEditor.getItem (); - if (newItem == oldItem || newItem != lastSelected) { + index = table.getSelectionIndex(); + Point pt = new Point(e.x, e.y); + newItem = table.getItem(pt); + if(newItem == null) return; + TableItem oldItem = comboEditor.getItem(); + if(newItem == oldItem || newItem != lastSelected) { lastSelected = newItem; return; } - table.showSelection (); - + table.showSelection (); combo = new CCombo (table, SWT.READ_ONLY); createComboEditor (combo, comboEditor); - widthText = new Text (table, SWT.SINGLE); - widthText.setText (((String [])data.elementAt (index)) [WIDTH_COL]); - createTextEditor (widthText, widthEditor, WIDTH_COL); + nameText = new Text(table, SWT.SINGLE); + nameText.setText(((String[])data.elementAt(index))[NAME_COL]); + createTextEditor(nameText, nameEditor, NAME_COL); + + widthText = new Text(table, SWT.SINGLE); + widthText.setText(((String[])data.elementAt(index))[WIDTH_COL]); + createTextEditor(widthText, widthEditor, WIDTH_COL); - heightText = new Text (table, SWT.SINGLE); - heightText.setText (((String [])data.elementAt (index)) [HEIGHT_COL]); - createTextEditor (heightText, heightEditor, HEIGHT_COL); + heightText = new Text(table, SWT.SINGLE); + heightText.setText(((String[])data.elementAt(index))[HEIGHT_COL]); + createTextEditor(heightText, heightEditor, HEIGHT_COL); - for (int i=0; i<table.getColumnCount (); i++) { - Rectangle rect = newItem.getBounds (i); - if (rect.contains (pt)) { - switch (i) { + for(int i=0; i<table.getColumnCount(); i++) { + Rectangle rect = newItem.getBounds(i); + if(rect.contains(pt)) { + switch(i) { + case NAME_COL : + nameText.setFocus(); case COMBO_COL : - combo.setFocus (); + combo.setFocus(); break; case WIDTH_COL : - widthText.setFocus (); + widthText.setFocus(); break; case HEIGHT_COL : - heightText.setFocus (); + heightText.setFocus(); break; default : - resetEditors (); + resetEditors(); break; } } @@ -102,16 +109,54 @@ class RowLayoutTab extends Tab { } }); - /* Add listener to add an element to the table */ - add.addSelectionListener(new SelectionAdapter () { - public void widgetSelected(SelectionEvent e) { - TableItem item = new TableItem (table, 0); - String [] insert = new String [] { - String.valueOf (table.indexOf (item)), - "Button", "-1", "-1"}; - item.setText (insert); - data.addElement (insert); - resetEditors (); + //Add listener to add an element to the table + add.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + if(event.detail == SWT.ARROW) { + ToolItem item = (ToolItem)event.widget; + ToolBar bar = item.getParent(); + Display display = bar.getDisplay(); + Shell shell = bar.getShell(); + Menu menu = new Menu (shell, SWT.POP_UP); + for(int i = 0; i < OPTIONS.length; i++) { + final MenuItem newItem = new MenuItem(menu, SWT.RADIO); + newItem.setText(OPTIONS[i]); + newItem.addSelectionListener (new SelectionAdapter(){ + public void widgetSelected(SelectionEvent event) { + MenuItem menuItem = (MenuItem)event.widget; + if (menuItem.getSelection()) { + Menu menu = menuItem.getParent(); + prevSelected = menu.indexOf(menuItem); + TableItem item = new TableItem(table, SWT.NONE); + String name = menuItem.getText().toLowerCase() + String.valueOf(table.indexOf (item)); + String[] insert = new String[] {name, menuItem.getText(), "-1", "-1"}; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } + } + }); + newItem.setSelection(i == prevSelected); + } + Point pt = display.map(bar, null, event.x, event.y); + menu.setLocation(pt.x, pt.y); + menu.setVisible(true); + + while(menu != null && !menu.isDisposed() && menu.isVisible()) { + if(!display.readAndDispatch()) { + display.sleep(); + } + } + menu.dispose(); + }else { + String selection = OPTIONS[prevSelected]; + TableItem item = new TableItem(table, 0); + String name = selection.toLowerCase() + String.valueOf (table.indexOf(item)); + String[] insert = new String[] {name, selection, "-1", "-1"}; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } } }); } @@ -119,170 +164,172 @@ class RowLayoutTab extends Tab { /** * Creates the control widgets. */ - void createControlWidgets () { + void createControlWidgets() { /* Controls the type of RowLayout */ - Group typeGroup = new Group (controlGroup, SWT.NONE); - typeGroup.setText (LayoutExample.getResourceString ("Type")); - typeGroup.setLayout (new GridLayout ()); - GridData data = new GridData (GridData.FILL_HORIZONTAL); - typeGroup.setLayoutData (data); - horizontal = new Button (typeGroup, SWT.RADIO); - horizontal.setText ("SWT.HORIZONTAL"); - horizontal.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); + Group typeGroup = new Group(controlGroup, SWT.NONE); + typeGroup.setText(LayoutExample.getResourceString("Type")); + typeGroup.setLayout(new GridLayout()); + typeGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); + horizontal = new Button(typeGroup, SWT.RADIO); + horizontal.setText("SWT.HORIZONTAL"); + horizontal.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); horizontal.setSelection(true); - horizontal.addSelectionListener (selectionListener); - vertical = new Button (typeGroup, SWT.RADIO); - vertical.setText ("SWT.VERTICAL"); - vertical.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); - vertical.addSelectionListener (selectionListener); + horizontal.addSelectionListener(selectionListener); + vertical = new Button(typeGroup, SWT.RADIO); + vertical.setText("SWT.VERTICAL"); + vertical.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + vertical.addSelectionListener(selectionListener); /* Controls the margins and spacing of the RowLayout */ - String [] marginValues = new String [] {"0","3","5","10"}; - Group marginGroup = new Group (controlGroup, SWT.NONE); - marginGroup.setText (LayoutExample.getResourceString ("Margins_Spacing")); - data = new GridData (GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); - data.verticalSpan = 2; - marginGroup.setLayoutData (data); - GridLayout layout = new GridLayout (); - layout.numColumns = 2; - marginGroup.setLayout (layout); - new Label (marginGroup, SWT.NONE).setText ("marginRight"); - marginRight = new Combo (marginGroup, SWT.NONE); - marginRight.setItems (marginValues); - marginRight.select (1); - marginRight.setLayoutData (new GridData(GridData.FILL_HORIZONTAL)); - marginRight.addSelectionListener (selectionListener); - marginRight.addTraverseListener (traverseListener); - new Label (marginGroup, SWT.NONE).setText ("marginLeft"); - marginLeft = new Combo (marginGroup, SWT.NONE); - marginLeft.setItems (marginValues); - marginLeft.select (1); - marginLeft.setLayoutData (new GridData(GridData.FILL_HORIZONTAL)); - marginLeft.addSelectionListener (selectionListener); - marginLeft.addTraverseListener(traverseListener); - new Label (marginGroup, SWT.NONE).setText ("marginTop"); - marginTop = new Combo (marginGroup, SWT.NONE); - marginTop.setItems (marginValues); - marginTop.select (1); - marginTop.setLayoutData (new GridData(GridData.FILL_HORIZONTAL)); - marginTop.addSelectionListener (selectionListener); - marginTop.addTraverseListener(traverseListener); - new Label (marginGroup, SWT.NONE).setText ("marginBottom"); - marginBottom = new Combo (marginGroup, SWT.NONE); - marginBottom.setItems (marginValues); - marginBottom.select (1); - marginBottom.setLayoutData (new GridData(GridData.FILL_HORIZONTAL)); - marginBottom.addSelectionListener (selectionListener); - marginBottom.addTraverseListener(traverseListener); - new Label (marginGroup, SWT.NONE).setText ("spacing"); - spacing = new Combo (marginGroup, SWT.NONE); - spacing.setItems (marginValues); - spacing.select (1); - spacing.setLayoutData (new GridData(GridData.FILL_HORIZONTAL)); - spacing.addSelectionListener (selectionListener); - spacing.addTraverseListener(traverseListener); + Group marginGroup = new Group(controlGroup, SWT.NONE); + marginGroup.setText(LayoutExample.getResourceString("Margins_Spacing")); + marginGroup.setLayout(new GridLayout(2, false)); + marginGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 2)); + new Label(marginGroup, SWT.NONE).setText("Margin Width"); + marginWidth = new Spinner(marginGroup, SWT.BORDER); + marginWidth.setSelection(0); + marginWidth.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Margin Height"); + marginHeight = new Spinner(marginGroup, SWT.BORDER); + marginHeight.setSelection(0); + marginHeight.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); + marginHeight.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Right Margin"); + marginRight = new Spinner(marginGroup, SWT.BORDER); + marginRight.setSelection(3); + marginRight.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Left Margin"); + marginLeft = new Spinner(marginGroup, SWT.BORDER); + marginLeft.setSelection(3); + marginLeft.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Top Margin"); + marginTop = new Spinner(marginGroup, SWT.BORDER); + marginTop.setSelection(3); + marginTop.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText("Bottom Margin"); + marginBottom = new Spinner(marginGroup, SWT.BORDER); + marginBottom.setSelection(3); + marginBottom.addSelectionListener(selectionListener); + new Label(marginGroup, SWT.NONE).setText ("Spacing"); + spacing = new Spinner(marginGroup, SWT.BORDER); + spacing.setSelection(3); + spacing.addSelectionListener(selectionListener); /* Controls other parameters of the RowLayout */ - Group specGroup = new Group (controlGroup, SWT.NONE); - specGroup.setText (LayoutExample.getResourceString ("Properties")); - specGroup.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); - specGroup.setLayout (new GridLayout ()); - wrap = new Button (specGroup, SWT.CHECK); - wrap.setText ("wrap"); - wrap.setSelection (true); - wrap.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); - wrap.addSelectionListener (selectionListener); - pack = new Button (specGroup, SWT.CHECK); - pack.setText ("pack"); - pack.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); - pack.setSelection (true); - pack.addSelectionListener (selectionListener); - justify = new Button (specGroup, SWT.CHECK); - justify.setText ("justify"); - justify.setLayoutData (new GridData (GridData.FILL_HORIZONTAL)); - justify.addSelectionListener (selectionListener); + Group specGroup = new Group(controlGroup, SWT.NONE); + specGroup.setText(LayoutExample.getResourceString("Properties")); + specGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); + specGroup.setLayout(new GridLayout()); + wrap = new Button(specGroup, SWT.CHECK); + wrap.setText("Wrap"); + wrap.setSelection(true); + wrap.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + wrap.addSelectionListener(selectionListener); + pack = new Button(specGroup, SWT.CHECK); + pack.setText("Pack"); + pack.setLayoutData(new GridData (SWT.FILL, SWT.CENTER, true, false)); + pack.setSelection(true); + pack.addSelectionListener(selectionListener); + fill = new Button(specGroup, SWT.CHECK); + fill.setText("Fill"); + fill.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + fill.addSelectionListener(selectionListener); + justify = new Button(specGroup, SWT.CHECK); + justify.setText("Justify"); + justify.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + justify.addSelectionListener(selectionListener); /* Add common controls */ - super.createControlWidgets (); + super.createControlWidgets(); /* Position the sash */ - sash.setWeights (new int [] {6,5}); + sash.setWeights(new int[] {6,5}); } /** * Creates the example layout. */ - void createLayout () { - rowLayout = new RowLayout (); - layoutComposite.setLayout (rowLayout); + void createLayout() { + rowLayout = new RowLayout(); + layoutComposite.setLayout(rowLayout); } /** * Disposes the editors without placing their contents * into the table. */ - void disposeEditors () { - comboEditor.setEditor (null, null, -1); - combo.dispose (); - widthText.dispose (); - heightText.dispose (); + void disposeEditors() { + comboEditor.setEditor(null, null, -1); + combo.dispose(); + widthText.dispose(); + heightText.dispose(); + nameText.dispose(); } /** * Generates code for the example layout. */ - StringBuffer generateLayoutCode () { - StringBuffer code = new StringBuffer (); - code.append ("\t\tRowLayout rowLayout = new RowLayout ();\n"); + StringBuffer generateLayoutCode() { + StringBuffer code = new StringBuffer(); + code.append("\t\tRowLayout rowLayout = new RowLayout ();\n"); if (rowLayout.type == SWT.VERTICAL) { - code.append ("\t\trowLayout.type = SWT.VERTICAL;\n"); + code.append("\t\trowLayout.type = SWT.VERTICAL;\n"); } - if (rowLayout.wrap == false) { - code.append ("\t\trowLayout.wrap = false;\n"); + if(rowLayout.wrap == false) { + code.append("\t\trowLayout.wrap = false;\n"); } - if (rowLayout.pack == false) { - code.append ("\t\trowLayout.pack = false;\n"); + if(rowLayout.pack == false) { + code.append("\t\trowLayout.pack = false;\n"); + } + if(rowLayout.fill == true) { + code.append("\t\trowLayout.fill = false;\n"); + } + if(rowLayout.justify == true) { + code.append("\t\trowLayout.justify = true;\n"); } - if (rowLayout.justify == true) { - code.append ("\t\trowLayout.justify = true;\n"); + if (rowLayout.marginWidth != 0) { + code.append("\t\trowLayout.marginWidth = " + rowLayout.marginWidth + ";\n"); } - if (rowLayout.marginLeft != 3) { - code.append ("\t\trowLayout.marginLeft = " + rowLayout.marginLeft + ";\n"); + if (rowLayout.marginHeight != 0) { + code.append("\t\trowLayout.marginHeight = " + rowLayout.marginHeight + ";\n"); } - if (rowLayout.marginRight != 3) { - code.append ("\t\trowLayout.marginRight = " + rowLayout.marginRight + ";\n"); + if(rowLayout.marginLeft != 3) { + code.append("\t\trowLayout.marginLeft = " + rowLayout.marginLeft + ";\n"); } - if (rowLayout.marginTop != 3) { - code.append ("\t\trowLayout.marginTop = " + rowLayout.marginTop + ";\n"); + if(rowLayout.marginRight != 3) { + code.append("\t\trowLayout.marginRight = " + rowLayout.marginRight + ";\n"); } - if (rowLayout.marginBottom != 3) { - code.append ("\t\trowLayout.marginBottom = " + rowLayout.marginBottom + ";\n"); + if(rowLayout.marginTop != 3) { + code.append("\t\trowLayout.marginTop = " + rowLayout.marginTop + ";\n"); } - if (rowLayout.spacing != 3) { - code.append ("\t\trowLayout.spacing = " + rowLayout.spacing + ";\n"); + if(rowLayout.marginBottom != 3) { + code.append("\t\trowLayout.marginBottom = " + rowLayout.marginBottom + ";\n"); } - code.append ("\t\tshell.setLayout (rowLayout);\n"); + if(rowLayout.spacing != 3) { + code.append("\t\trowLayout.spacing = " + rowLayout.spacing + ";\n"); + } + code.append("\t\tshell.setLayout (rowLayout);\n"); boolean first = true; - for (int i = 0; i < children.length; i++) { + for(int i = 0; i < children.length; i++) { Control control = children [i]; - code.append (getChildCode (control,i)); - RowData data = (RowData) control.getLayoutData (); - if (data != null) { - if (data.width != -1 || data.height != -1) { - code.append ("\t\t"); - if (first) { - code.append ("RowData "); + code.append(getChildCode(control,i)); + RowData rowData = (RowData)control.getLayoutData(); + if(rowData != null) { + if(rowData.width != -1 || rowData.height != -1) { + code.append("\t\t"); + if(first) { + code.append("RowData "); first = false; } - if (data.width == -1) { - code.append ("data = new RowData (SWT.DEFAULT, " + data.height + ");\n"); - } else if (data.height == -1) { - code.append ("data = new RowData (" + data.width + ", SWT.DEFAULT);\n"); + if(rowData.width == -1) { + code.append("rowData = new RowData (SWT.DEFAULT, " + rowData.height + ");\n"); + } else if(rowData.height == -1) { + code.append("rowData = new RowData (" + rowData.width + ", SWT.DEFAULT);\n"); } else { - code.append ("data = new RowData (" + data.width + ", " + data.height + ");\n"); + code.append("rowData = new RowData (" + rowData.width + ", " + rowData.height + ");\n"); } - code.append ("\t\t" + names [i] + ".setLayoutData (data);\n"); + code.append("\t\t" + names [i] + ".setLayoutData (rowData);\n"); } } } @@ -292,10 +339,10 @@ class RowLayoutTab extends Tab { /** * Returns the layout data field names. */ - String [] getLayoutDataFieldNames() { - return new String [] { - "", - "Control", + String[] getLayoutDataFieldNames() { + return new String[] { + "Control Name", + "Control Type", "width", "height" }; @@ -304,60 +351,60 @@ class RowLayoutTab extends Tab { /** * Gets the text for the tab folder item. */ - String getTabText () { + String getTabText() { return "RowLayout"; } - /** - * Takes information from TableEditors and stores it. - */ - void resetEditors () { - resetEditors (false); - } - - void resetEditors (boolean tab) { + void resetEditors(boolean tab) { TableItem oldItem = comboEditor.getItem (); - if (oldItem != null) { - int row = table.indexOf (oldItem); + + if(oldItem != null) { + int row = table.indexOf(oldItem); /* Make sure user has entered valid data */ + try { + new String(nameText.getText()); + } catch(NumberFormatException e) { + nameText.setText(oldItem.getText(NAME_COL)); + } try { - new Integer (widthText.getText ()).intValue (); + new Integer(widthText.getText()).intValue(); } catch (NumberFormatException e) { - widthText.setText (oldItem.getText (WIDTH_COL)); + widthText.setText(oldItem.getText(WIDTH_COL)); } try { - new Integer (heightText.getText ()).intValue (); - } catch (NumberFormatException e) { - heightText.setText (oldItem.getText (HEIGHT_COL)); + 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 ()}; + + String[] insert = new String[] { + nameText.getText(), combo.getText(), widthText.getText(), heightText.getText()}; data.setElementAt (insert, row); - for (int i = 0 ; i < TOTAL_COLS; i++) { - oldItem.setText (i, ((String [])data.elementAt (row)) [i]); + for(int i = 0 ; i < TOTAL_COLS; i++) { + oldItem.setText(i, ((String [])data.elementAt(row))[i]); } - if (!tab) disposeEditors (); + if (!tab) disposeEditors(); } - setLayoutState (); - refreshLayoutComposite (); - setLayoutData (); - layoutComposite.layout (true); - layoutGroup.layout (true); + setLayoutState(); + refreshLayoutComposite(); + setLayoutData(); + layoutComposite.layout(true); + layoutGroup.layout(true); } /** * Sets the layout data for the children of the layout. */ - void setLayoutData () { - Control [] children = layoutComposite.getChildren (); - TableItem [] items = table.getItems (); + void setLayoutData() { + Control[] children = layoutComposite.getChildren(); + TableItem[] items = table.getItems(); RowData data; int width, height; - 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 RowData (width, height); - children [i].setLayoutData (data); + 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 RowData(width, height); + children[i].setLayoutData(data); } } @@ -365,49 +412,23 @@ class RowLayoutTab extends Tab { /** * Sets the state of the layout. */ - void setLayoutState () { + void setLayoutState() { /* Set the type of layout */ - if (vertical.getSelection ()) { - rowLayout.type = SWT.VERTICAL; - } else { - rowLayout.type = SWT.HORIZONTAL; - } + rowLayout.type = vertical.getSelection() ? SWT.VERTICAL : SWT.HORIZONTAL; /* Set the margins and spacing */ - try { - rowLayout.marginRight = new Integer (marginRight.getText ()).intValue (); - } catch (NumberFormatException e) { - rowLayout.marginRight = 3; - marginRight.select (1); - } - try { - rowLayout.marginLeft = new Integer (marginLeft.getText ()).intValue (); - } catch (NumberFormatException e) { - rowLayout.marginLeft = 3; - marginLeft.select (1); - } - try { - rowLayout.marginTop = new Integer (marginTop.getText ()).intValue (); - } catch (NumberFormatException e) { - rowLayout.marginTop = 3; - marginTop.select (1); - } - try { - rowLayout.marginBottom = new Integer (marginBottom.getText ()).intValue (); - } catch (NumberFormatException e) { - rowLayout.marginBottom = 3; - marginBottom.select (1); - } - try { - rowLayout.spacing = new Integer (spacing.getText ()).intValue (); - } catch (NumberFormatException e) { - rowLayout.spacing = 3; - spacing.select (1); - } + rowLayout.marginWidth = marginWidth.getSelection(); + rowLayout.marginHeight = marginHeight.getSelection(); + rowLayout.marginRight = marginRight.getSelection(); + rowLayout.marginLeft = marginLeft.getSelection(); + rowLayout.marginTop = marginTop.getSelection(); + rowLayout.marginBottom = marginBottom.getSelection(); + rowLayout.spacing = spacing.getSelection(); /* Set the other layout properties */ - rowLayout.wrap = wrap.getSelection (); - rowLayout.pack = pack.getSelection (); - rowLayout.justify = justify.getSelection (); + rowLayout.wrap = wrap.getSelection(); + rowLayout.pack = pack.getSelection(); + rowLayout.fill = fill.getSelection(); + rowLayout.justify = justify.getSelection(); } } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/StackLayoutTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/StackLayoutTab.java new file mode 100644 index 0000000000..2f20160951 --- /dev/null +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/StackLayoutTab.java @@ -0,0 +1,266 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.examples.layoutexample; + +import org.eclipse.swt.*; +import org.eclipse.swt.custom.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +class StackLayoutTab extends Tab { + /* Controls for setting layout parameters */ + Button backButton, advanceButton; + StackLayout stackLayout; + int currentLayer = 0; + /* TableEditors and related controls*/ + TableEditor comboEditor, nameEditor; + CCombo combo; + int prevSelected = 0; + Text nameText; + final int NAME_COL = 0; + + /** + * Creates the Tab within a given instance of LayoutExample. + */ + StackLayoutTab() { + } + + /** + * Creates the widgets in the "child" group. + */ + void createChildWidgets() { + /* Add common controls */ + super.createChildWidgets(); + + /* Add TableEditors */ + comboEditor = new TableEditor(table); + nameEditor = new TableEditor(table); + table.addMouseListener (new MouseAdapter() { + public void mouseDown(MouseEvent e) { + resetEditors(); + index = table.getSelectionIndex(); + if(index == -1) return; + //set top layer of stack to the selected item + currentLayer = index; + stackLayout.topControl = children[currentLayer]; + backButton.setEnabled(currentLayer > 0); + advanceButton.setEnabled(currentLayer < children.length - 1); + layoutComposite.layout(); + + TableItem oldItem = comboEditor.getItem(); + newItem = table.getItem(index); + if(newItem == oldItem || newItem != lastSelected) { + lastSelected = newItem; + return; + } + table.showSelection(); + combo = new CCombo(table, SWT.READ_ONLY); + createComboEditor(combo, comboEditor); + + nameText = new Text(table, SWT.SINGLE); + nameText.setText(((String[])data.elementAt(index))[NAME_COL]); + createTextEditor(nameText, nameEditor, NAME_COL); + } + }); + + // Add listener to add an element to the table + add.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + if (event.detail == SWT.ARROW) { + ToolItem item = (ToolItem)event.widget; + ToolBar bar = item.getParent(); + Display display = bar.getDisplay(); + Shell shell = bar.getShell(); + final Menu menu = new Menu(shell, SWT.POP_UP); + for(int i = 0; i < OPTIONS.length; i++) { + final MenuItem newItem = new MenuItem(menu, SWT.RADIO); + newItem.setText(OPTIONS[i]); + newItem.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent event) { + MenuItem menuItem = (MenuItem)event.widget; + if (menuItem.getSelection()) { + Menu menu = menuItem.getParent(); + prevSelected = menu.indexOf(menuItem); + TableItem item = new TableItem(table, SWT.NONE); + String name = menuItem.getText().toLowerCase() + String.valueOf(table.getItemCount() - 1); + String[] insert = new String[] {name, menuItem.getText()}; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } + } + }); + newItem.setSelection(i == prevSelected); + } + Point pt = display.map(bar, null, event.x, event.y); + menu.setLocation(pt.x, pt.y); + menu.setVisible(true); + + while(menu != null && !menu.isDisposed() && menu.isVisible()) { + if(!display.readAndDispatch()) { + display.sleep(); + } + } + menu.dispose(); + } else { + String selection = OPTIONS[prevSelected]; + TableItem item = new TableItem(table, 0); + String name = selection.toLowerCase() + String.valueOf(table.indexOf(item)); + String[] insert = new String[] { name, selection }; + item.setText(insert); + data.addElement(insert); + resetEditors(); + } + currentLayer = children.length -1; + stackLayout.topControl = children[currentLayer]; + layoutComposite.layout(); + backButton.setEnabled(children.length > 1); + advanceButton.setEnabled(false); + } + }); + } + + /** + * Creates the control widgets. + */ + void createControlWidgets() { + /* Add common controls */ + super.createControlWidgets(); + + /* Position the sash */ + sash.setWeights(new int[] {4,1}); + } + + /** + * Creates the example layout. + */ + void createLayout() { + stackLayout = new StackLayout(); + layoutComposite.setLayout(stackLayout); + } + + void createLayoutComposite() { + layoutComposite = new Composite(layoutGroup, SWT.BORDER); + layoutComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); + createLayout(); + } + + /** + * Creates the layout group. This is the group on the + * left half of each example tab. + */ + void createLayoutGroup() { + layoutGroup = new Group(sash, SWT.NONE); + layoutGroup.setText(LayoutExample.getResourceString("Layout")); + layoutGroup.setLayout(new GridLayout(2, true)); + createLayoutComposite(); + + backButton = new Button(layoutGroup, SWT.PUSH); + backButton.setText("<<"); + backButton.setEnabled(false); + backButton.setLayoutData(new GridData (SWT.END, SWT.CENTER, false, false)); + backButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + currentLayer--; + stackLayout.topControl = children [currentLayer]; + layoutComposite.layout(); + backButton.setEnabled(currentLayer > 0); + advanceButton.setEnabled(currentLayer < children.length); + } + }); + + advanceButton = new Button(layoutGroup, SWT.PUSH); + advanceButton.setText(">>"); + advanceButton.setEnabled(false); + advanceButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + currentLayer++; + stackLayout.topControl = children [currentLayer]; + layoutComposite.layout(); + backButton.setEnabled(currentLayer > 0); + advanceButton.setEnabled(currentLayer < children.length - 1); + } + }); + } + + /** + * Disposes the editors without placing their contents + * into the table. + */ + void disposeEditors() { + comboEditor.setEditor(null, null, -1); + combo.dispose(); + nameText.dispose(); + } + + /** + * Generates code for the example layout. + */ + StringBuffer generateLayoutCode() { + StringBuffer code = new StringBuffer(); + code.append("\t\tStackLayout stackLayout = new StackLayout ();\n"); + code.append("\t\tshell.setLayout (stackLayout);\n"); + for(int i = 0; i < children.length; i++) { + Control control = children[i]; + code.append (getChildCode(control, i)); + } + return code; + } + + boolean needsCustom() { + return true; + } + + /** + * Returns the layout data field names. + */ + String[] getLayoutDataFieldNames() { + return new String[] {"Control Name", "Control Type"}; + } + + /** + * Gets the text for the tab folder item. + */ + String getTabText() { + return "StackLayout"; + } + + /** + * Takes information from TableEditors and stores it. + */ + void resetEditors() { + TableItem oldItem = comboEditor.getItem(); + comboEditor.setEditor(null, null, -1); + if(oldItem != null) { + int row = table.indexOf(oldItem); + try { + new String(nameText.getText()); + } catch(NumberFormatException e) { + nameText.setText(oldItem.getText(NAME_COL)); + } + String[] insert = new String[] {nameText.getText(), combo.getText()}; + data.setElementAt(insert, row); + for(int i = 0 ; i < table.getColumnCount(); i++) { + oldItem.setText(i, ((String[])data.elementAt(row))[i]); + } + disposeEditors(); + } + refreshLayoutComposite(); + if(children.length > 0){ + stackLayout.topControl = children[currentLayer]; + layoutComposite.layout(true); + } + layoutGroup.layout(true); + } +} + diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/Tab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/Tab.java index 05f3c074b0..e20fdbfe5f 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/Tab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/layoutexample/Tab.java @@ -9,10 +9,8 @@ * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.examples.layoutexample; - import java.util.Vector; - import org.eclipse.swt.*; import org.eclipse.swt.custom.*; import org.eclipse.swt.events.*; @@ -47,147 +45,190 @@ abstract class Tab { /* Common controls for modifying the example layout */ String [] names; Control [] children; - Button size, add, delete, clear, code; + ToolItem add, delete, clear, code; /* Common values for working with TableEditors */ Table table; int index; + boolean comboReset = false; + final String[] OPTIONS = {"Button", "Canvas", "Combo", "Composite", "CoolBar", + "Group", "Label", "List", "ProgressBar", "Scale", "Slider", "StyledText", + "Table", "Text", "ToolBar", "Tree"}; TableItem newItem, lastSelected; Vector data = new Vector (); - /* Controlling instance */ - final LayoutExample instance; /* Listeners */ - SelectionListener selectionListener = new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - resetEditors (); + SelectionListener selectionListener = new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + resetEditors(); } }; - - TraverseListener traverseListener = new TraverseListener () { - public void keyTraversed (TraverseEvent e) { - if (e.detail == SWT.TRAVERSE_RETURN) { - e.doit = false; - resetEditors (); - } - } - }; - + /** * Creates the Tab within a given instance of LayoutExample. */ - Tab(LayoutExample instance) { - this.instance = instance; + Tab() { } /** - * Creates the "child" group. This is the group that allows + * Creates the "children" group. This is the group that allows * you to add children to the layout. It exists within the * controlGroup. */ - void createChildGroup () { - childGroup = new Group (controlGroup, SWT.NONE); - childGroup.setText (LayoutExample.getResourceString("Children")); - GridLayout layout = new GridLayout (); - layout.numColumns = 3; - childGroup.setLayout (layout); - GridData data = new GridData (GridData.FILL_BOTH); - data.horizontalSpan = 2; - childGroup.setLayoutData (data); - createChildWidgets (); - } - - /** - * Creates the controls for modifying the "children" - * table, and the table itself. - * Subclasses override this method to augment the - * standard table. - */ - void createChildWidgets () { - /* Controls for adding and removing children */ - add = new Button (childGroup, SWT.PUSH); - add.setText (LayoutExample.getResourceString ("Add")); - add.setLayoutData(new GridData (GridData.FILL_HORIZONTAL)); - delete = new Button (childGroup, SWT.PUSH); - delete.setText (LayoutExample.getResourceString ("Delete")); - delete.setLayoutData(new GridData (GridData.FILL_HORIZONTAL)); - delete.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - resetEditors (); - int [] selected = table.getSelectionIndices (); + void createChildGroup() { + childGroup = new Group(controlGroup, SWT.NONE); + childGroup.setText(LayoutExample.getResourceString("Children")); + childGroup.setLayout(new GridLayout()); + childGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); + + ToolBar toolBar = new ToolBar(childGroup, SWT.FLAT); + toolBar.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); + add = new ToolItem(toolBar, SWT.DROP_DOWN); + add.setText(LayoutExample.getResourceString("Add")); + new ToolItem(toolBar,SWT.SEPARATOR); + + delete = new ToolItem(toolBar, SWT.PUSH); + delete.setText(LayoutExample.getResourceString("Delete")); + delete.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + resetEditors(); + int [] selected = table.getSelectionIndices(); table.remove (selected); /* Refresh the control indices of the table */ - for (int i = 0; i < table.getItemCount(); i++) { - table.getItem (i).setText (0, String.valueOf (i)); + for(int i = 0; i < table.getItemCount(); i++) { + TableItem item = table.getItem(i); + item.setText (0, item.getText(0)); } - refreshLayoutComposite (); - layoutComposite.layout (true); - layoutGroup.layout (true); + refreshLayoutComposite(); + layoutComposite.layout(true); + layoutGroup.layout(true); } }); - clear = new Button (childGroup, SWT.PUSH); - clear.setText (LayoutExample.getResourceString ("Clear")); - clear.setLayoutData(new GridData (GridData.FILL_HORIZONTAL)); - clear.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - resetEditors (); - children = layoutComposite.getChildren (); - for (int i = 0; i < children.length; i++) { - children [i].dispose (); + + new ToolItem(toolBar,SWT.SEPARATOR); + clear = new ToolItem(toolBar, SWT.PUSH); + clear.setText(LayoutExample.getResourceString("Clear")); + clear.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + resetEditors(); + children = layoutComposite.getChildren(); + for(int i = 0; i < children.length; i++) { + children[i].dispose(); } - table.removeAll (); - data.clear (); - children = new Control [0]; - layoutGroup.layout (true); + table.removeAll(); + data.clear(); + children = new Control[0]; + layoutGroup.layout(true); } }); + toolBar.pack(); + + new ToolItem(toolBar,SWT.SEPARATOR); + code = new ToolItem(toolBar, SWT.PUSH); + code.setText(LayoutExample.getResourceString ("Generate_Code")); + code.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + final Shell shell = new Shell(); + shell.setText(LayoutExample.getResourceString("Generated_Code")); + shell.setLayout(new FillLayout()); + final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); + String layoutCode = generateCode().toString (); + if(layoutCode.length() == 0) return; + text.setText(layoutCode); + + Menu bar = new Menu(shell, SWT.BAR); + shell.setMenuBar(bar); + MenuItem editItem = new MenuItem(bar, SWT.CASCADE); + editItem.setText(LayoutExample.getResourceString("Edit")); + Menu menu = new Menu(bar); + MenuItem select = new MenuItem(menu, SWT.PUSH); + select.setText(LayoutExample.getResourceString("Select_All")); + select.setAccelerator(SWT.MOD1 + 'A'); + select.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + text.selectAll(); + } + }); + MenuItem copy = new MenuItem(menu, SWT.PUSH); + copy.setText(LayoutExample.getResourceString("Copy")); + copy.setAccelerator(SWT.MOD1 + 'C'); + copy.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + text.copy(); + } + }); + MenuItem exit = new MenuItem(menu, SWT.PUSH); + exit.setText(LayoutExample.getResourceString("Exit")); + exit.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + shell.close(); + } + }); + editItem.setMenu(menu); + + shell.pack(); + shell.setSize(400, 500); + shell.open(); + Display display = shell.getDisplay(); + while(!shell.isDisposed()) + if(!display.readAndDispatch()) display.sleep(); + } + }); + + createChildWidgets(); + } + + /** + * Creates the controls for modifying the "children" + * table, and the table itself. + * Subclasses override this method to augment the + * standard table. + */ + void createChildWidgets() { /* Create the "children" table */ - table = new Table (childGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION); - table.setLinesVisible (true); - table.setHeaderVisible (true); - GridData gridData = new GridData (GridData.FILL_BOTH); - gridData.horizontalSpan = 3; + table = new Table(childGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION); + table.setLinesVisible(true); + table.setHeaderVisible(true); + FontData def[] = Display.getCurrent().getSystemFont().getFontData(); + table.setFont(new Font(Display.getCurrent(), def[0].getName(), 10, SWT.NONE)); + GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1); gridData.heightHint = 150; - table.setLayoutData (gridData); - table.addTraverseListener (traverseListener); + table.setLayoutData(gridData); /* Add columns to the table */ - String [] columnHeaders = getLayoutDataFieldNames (); + String[] columnHeaders = getLayoutDataFieldNames(); for (int i = 0; i < columnHeaders.length; i++) { TableColumn column = new TableColumn(table, SWT.NONE); - column.setText (columnHeaders [i]); - if (i == 0) column.setWidth (20); - else if (i == 1) column.setWidth (80); - else column.pack (); + column.setText(columnHeaders [i]); + if(i == 0) column.setWidth(100); + else if(i == 1) column.setWidth(90); + else column.pack(); } } - + /** * Creates the TableEditor with a CCombo in the first column * of the table. This CCombo lists all the controls that * the user can select to place on their layout. */ - void createComboEditor (CCombo combo, TableEditor comboEditor) { - combo.setItems (new String [] { - "Button", "Canvas", "Combo", "Composite", - "CoolBar", "Group", "Label", "List", - "ProgressBar", "Scale", "Slider", "StyledText", - "Table", "Text", "ToolBar", "Tree"}); - combo.setText (newItem.getText (1)); + void createComboEditor(CCombo combo, TableEditor comboEditor) { + combo.setItems(OPTIONS); + combo.setText(newItem.getText (1)); /* Set up editor */ comboEditor.horizontalAlignment = SWT.LEFT; comboEditor.grabHorizontal = true; comboEditor.minimumWidth = 50; - comboEditor.setEditor (combo, newItem, 1); + comboEditor.setEditor(combo, newItem, 1); /* Add listener */ combo.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { - if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_RETURN) { - resetEditors (); + if(e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_RETURN) { + comboReset = true; + resetEditors(); } - if (e.detail == SWT.TRAVERSE_ESCAPE) { - disposeEditors (); + if(e.detail == SWT.TRAVERSE_ESCAPE) { + disposeEditors(); } } }); @@ -199,120 +240,69 @@ abstract class Tab { * for adding new children to the layoutComposite, and * for modifying the children's layout data. */ - void createControlGroup () { - controlGroup = new Group (sash, SWT.NONE); - controlGroup.setText (LayoutExample.getResourceString("Parameters")); - GridLayout layout = new GridLayout (); - layout.numColumns = 2; - controlGroup.setLayout (layout); - size = new Button (controlGroup, SWT.CHECK); - size.setText (LayoutExample.getResourceString ("Preferred_Size")); - size.setSelection (false); - size.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - resetEditors (); - if (size.getSelection ()) { - layoutComposite.setLayoutData (new GridData ()); - layoutGroup.layout (true); + void createControlGroup() { + controlGroup = new Group(sash, SWT.NONE); + controlGroup.setText(LayoutExample.getResourceString("Parameters")); + GridLayout layout = new GridLayout(2, true); + layout.horizontalSpacing = 10; + controlGroup.setLayout(layout); + final Button preferredButton = new Button(controlGroup, SWT.CHECK); + preferredButton.setText(LayoutExample.getResourceString("Preferred_Size")); + preferredButton.setSelection (false); + preferredButton.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + resetEditors(); + GridData data = (GridData)layoutComposite.getLayoutData(); + if (preferredButton.getSelection()) { + data.heightHint = data.widthHint = SWT.DEFAULT; + data.verticalAlignment = data.horizontalAlignment = 0; + data.grabExcessVerticalSpace = data.grabExcessHorizontalSpace = false; } else { - layoutComposite.setLayoutData (new GridData (GridData.FILL_BOTH)); - layoutGroup.layout (true); + data.verticalAlignment = data.horizontalAlignment = SWT.FILL; + data.grabExcessVerticalSpace = data.grabExcessHorizontalSpace = true; } + layoutComposite.setLayoutData(data); + layoutGroup.layout(true); } }); - GridData data = new GridData (GridData.FILL_HORIZONTAL); - data.horizontalSpan = 2; - size.setLayoutData (data); - createControlWidgets (); + preferredButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); + createControlWidgets(); } - + /** * Creates the "control" widget children. * Subclasses override this method to augment * the standard controls created. */ - void createControlWidgets () { - createChildGroup (); - code = new Button (controlGroup, SWT.PUSH); - code.setText (LayoutExample.getResourceString ("Code")); - GridData gridData = new GridData (GridData.HORIZONTAL_ALIGN_CENTER | GridData.GRAB_HORIZONTAL); - gridData.horizontalSpan = 2; - code.setLayoutData (gridData); - code.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - final Shell shell = new Shell (); - shell.setText (LayoutExample.getResourceString ("Generated_Code")); - shell.setLayout (new FillLayout ()); - final StyledText text = new StyledText (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); - String layoutCode = generateCode ().toString (); - if (layoutCode.length () == 0) return; - text.setText (layoutCode); - - Menu bar = new Menu (shell, SWT.BAR); - shell.setMenuBar (bar); - MenuItem editItem = new MenuItem (bar, SWT.CASCADE); - editItem.setText (LayoutExample.getResourceString ("Edit")); - Menu menu = new Menu (bar); - MenuItem select = new MenuItem (menu, SWT.PUSH); - select.setText (LayoutExample.getResourceString ("Select_All")); - select.setAccelerator (SWT.MOD1 + 'A'); - select.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - text.selectAll (); - } - }); - MenuItem copy = new MenuItem (menu, SWT.PUSH); - copy.setText (LayoutExample.getResourceString ("Copy")); - copy.setAccelerator (SWT.MOD1 + 'C'); - copy.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - text.copy (); - } - }); - MenuItem exit = new MenuItem (menu, SWT.PUSH); - exit.setText (LayoutExample.getResourceString ("Exit")); - exit.addSelectionListener (new SelectionAdapter () { - public void widgetSelected (SelectionEvent e) { - shell.close (); - } - }); - editItem.setMenu (menu); - - shell.pack (); - shell.setSize (400, 500); - shell.open (); - Display display = shell.getDisplay (); - while (!shell.isDisposed ()) - if (!display.readAndDispatch ()) display.sleep (); - } - }); + void createControlWidgets() { + createChildGroup(); } /** * Creates the example layout. * Subclasses override this method. */ - void createLayout () { + void createLayout() { } /** * Creates the composite that contains the example layout. */ - void createLayoutComposite () { - layoutComposite = new Composite (layoutGroup, SWT.BORDER); - layoutComposite.setLayoutData (new GridData (GridData.FILL_BOTH)); - createLayout (); + void createLayoutComposite() { + layoutComposite = new Composite(layoutGroup, SWT.BORDER); + layoutComposite.setLayoutData(new GridData (SWT.FILL, SWT.FILL, true, true)); + createLayout(); } /** * Creates the layout group. This is the group on the * left half of each example tab. */ - void createLayoutGroup () { - layoutGroup = new Group (sash, SWT.NONE); - layoutGroup.setText (LayoutExample.getResourceString("Layout")); - layoutGroup.setLayout (new GridLayout ()); - createLayoutComposite (); + void createLayoutGroup() { + layoutGroup = new Group(sash, SWT.NONE); + layoutGroup.setText(LayoutExample.getResourceString("Layout")); + layoutGroup.setLayout(new GridLayout()); + createLayoutComposite(); } /** @@ -321,16 +311,16 @@ abstract class Tab { * @param tabFolder org.eclipse.swt.widgets.TabFolder * @return the new page for the tab folder */ - Composite createTabFolderPage (TabFolder tabFolder) { + Composite createTabFolderPage(TabFolder tabFolder) { /* Create a two column page with a SashForm*/ - tabFolderPage = new Composite (tabFolder, SWT.NONE); - tabFolderPage.setLayout (new FillLayout ()); - sash = new SashForm (tabFolderPage, SWT.HORIZONTAL); - + tabFolderPage = new Composite(tabFolder, SWT.NULL); + tabFolderPage.setLayoutData (new GridData(SWT.FILL, SWT.FILL, true, true)); + tabFolderPage.setLayout(new FillLayout()); + sash = new SashForm(tabFolderPage, SWT.HORIZONTAL); /* Create the "layout" and "control" columns */ - createLayoutGroup (); - createControlGroup (); - + createLayoutGroup(); + createControlGroup(); + sash.setWeights(new int[] {50, 50}); return tabFolderPage; } @@ -338,23 +328,12 @@ abstract class Tab { * Creates the TableEditor with a Text in the given column * of the table. */ - void createTextEditor (Text text, TableEditor textEditor, int column) { - text.setFont (table.getFont ()); - text.selectAll (); + void createTextEditor(Text text, TableEditor textEditor, int column) { + text.setFont(table.getFont()); + text.selectAll(); textEditor.horizontalAlignment = SWT.LEFT; textEditor.grabHorizontal = true; - textEditor.setEditor (text, newItem, column); - - text.addTraverseListener(new TraverseListener() { - public void keyTraversed(TraverseEvent e) { - if (e.detail == SWT.TRAVERSE_TAB_NEXT) { - resetEditors (true); - } - if (e.detail == SWT.TRAVERSE_ESCAPE) { - disposeEditors (); - } - } - }); + textEditor.setEditor(text, newItem, column); } /** @@ -362,132 +341,147 @@ abstract class Tab { * into the table. * Subclasses override this method. */ - void disposeEditors () { + void disposeEditors() { } /** * Generates the code needed to produce the example layout. */ - StringBuffer generateCode () { + StringBuffer generateCode() { /* Make sure all information being entered is stored in the table */ - resetEditors (); + resetEditors(); /* Get names for controls in the layout */ - names = new String [children.length]; - for (int i = 0; i < children.length; i++) { - Control control = children [i]; - String controlClass = control.getClass ().toString (); - String controlType = controlClass.substring (controlClass.lastIndexOf ('.') + 1); - names [i] = controlType.toLowerCase () + i; + names = new String[children.length]; + for(int i =0; i< children.length; i++) { + TableItem myItem = table.getItem(i); + String name = myItem.getText(0); + if(name.matches("\\d")){ + Control control = children[i]; + String controlClass = control.getClass().toString(); + String controlType = controlClass.substring (controlClass.lastIndexOf ('.') + 1); + names[i] = controlType.toLowerCase() + i; + } else { + names[i] = myItem.getText(0); + } } /* Create StringBuffer containing the code */ StringBuffer code = new StringBuffer (); - code.append ("import org.eclipse.swt.*;\n"); - code.append ("import org.eclipse.swt.custom.*;\n"); - code.append ("import org.eclipse.swt.graphics.*;\n"); - code.append ("import org.eclipse.swt.layout.*;\n"); - code.append ("import org.eclipse.swt.widgets.*;\n\n"); - code.append ("public class MyLayout {\n"); - code.append ("\tpublic static void main (String [] args) {\n"); - code.append ("\t\tDisplay display = new Display ();\n"); - code.append ("\t\tShell shell = new Shell (display);\n"); + code.append("import org.eclipse.swt.*;\n"); + code.append("import org.eclipse.swt.layout.*;\n"); + code.append("import org.eclipse.swt.widgets.*;\n"); + if (needsCustom()) code.append("import org.eclipse.swt.custom.*;\n"); + if (needsGraphics()) code.append("import org.eclipse.swt.graphics.*;\n"); + code.append("\n"); + code.append("public class MyLayout {\n"); + code.append("\tpublic static void main (String [] args) {\n"); + code.append("\t\tDisplay display = new Display ();\n"); + code.append("\t\tShell shell = new Shell (display);\n"); /* Get layout specific code */ - code.append (generateLayoutCode ()); + code.append(generateLayoutCode()); - code.append ("\n\t\tshell.pack ();\n\t\tshell.open ();\n\n"); - code.append ("\t\twhile (!shell.isDisposed ()) {\n"); - code.append ("\t\t\tif (!display.readAndDispatch ())\n"); - code.append ("\t\t\t\tdisplay.sleep ();\n\t\t}\n\t\tdisplay.dispose ();\n\t}\n}"); + code.append("\n\t\tshell.pack ();\n\t\tshell.open ();\n\n"); + code.append("\t\twhile (!shell.isDisposed ()) {\n"); + code.append("\t\t\tif (!display.readAndDispatch ())\n"); + code.append("\t\t\t\tdisplay.sleep ();\n\t\t}\n\t\tdisplay.dispose ();\n\t}\n}"); return code; } + boolean needsGraphics() { + return false; + } + + boolean needsCustom() { + return false; + } + /** * Generates layout specific code for the example layout. * Subclasses override this method. */ - StringBuffer generateLayoutCode () { - return new StringBuffer (); + StringBuffer generateLayoutCode() { + return new StringBuffer(); } /** * Returns the StringBuffer for the code which will * create a child control. */ - StringBuffer getChildCode (Control control, int i) { - StringBuffer code = new StringBuffer (); + StringBuffer getChildCode(Control control, int i) { + StringBuffer code = new StringBuffer(); /* Find the type of control */ - String controlClass = control.getClass().toString (); - String controlType = controlClass.substring (controlClass.lastIndexOf ('.') + 1); + String controlClass = control.getClass().toString(); + String controlType = controlClass.substring(controlClass.lastIndexOf('.') + 1); /* Find the style of the control */ String styleString; - if (controlType.equals ("Button")) { + if (controlType.equals("Button")) { styleString = "SWT.PUSH"; - } else if (controlType.equals ("Text")) { + } else if(controlType.equals("Text")) { styleString = "SWT.BORDER"; - } else if (controlType.equals ("StyledText")) { + } else if(controlType.equals("StyledText")) { styleString = "SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL"; - } else if (controlType.equals ("Canvas") || controlType.equals ("Composite") || - controlType.equals ("Table") || controlType.equals ("StyledText") || - controlType.equals ("ToolBar") || controlType.equals ("Tree") || - controlType.equals ("List")) { + } else if(controlType.equals("Canvas") || controlType.equals("Composite") || + controlType.equals("Table") || controlType.equals("StyledText") || + controlType.equals("ToolBar") || controlType.equals("Tree") || + controlType.equals("List")) { styleString = "SWT.BORDER"; } else styleString = "SWT.NONE"; /* Write out the control being declared */ - code.append ("\n\t\t" + controlType + " " + names [i] + + code.append("\n\t\t" + controlType + " " + names[i] + " = new " + controlType + " (shell, " + styleString + ");\n"); /* Add items to those controls that need items */ - if (controlType.equals ("Combo") || controlType.equals ("List")) { - code.append ("\t\t" + names [i] + ".setItems (new String [] {\"Item 1\", \"Item 2\", \"Item 2\"});\n"); - } else if (controlType.equals ("Table")) { - code.append ("\t\t" + names [i] + ".setLinesVisible (true);\n"); - for (int j = 1; j < 3; j++) { - code.append ("\t\tTableItem tableItem" + j + " = new TableItem (" + names [i] + ", SWT.NONE);\n"); - code.append ("\t\ttableItem" + j + ".setText (\"Item" + j + "\");\n"); + if(controlType.equals("Combo") || controlType.equals("List")) { + code.append ("\t\t" + names[i] + ".setItems (new String [] {\"Item 1\", \"Item 2\", \"Item 2\"});\n"); + } else if(controlType.equals("Table")) { + code.append ("\t\t" + names[i] + ".setLinesVisible (true);\n"); + for(int j = 1; j < 3; j++) { + code.append("\t\tTableItem tableItem" + j + " = new TableItem (" + names [i] + ", SWT.NONE);\n"); + code.append("\t\ttableItem" + j + ".setText (\"Item" + j + "\");\n"); } - } else if (controlType.equals ("Tree")) { + } else if(controlType.equals("Tree")) { for (int j = 1; j < 3; j++) { - code.append ("\t\tTreeItem treeItem" + j + " = new TreeItem (" + names [i] + ", SWT.NONE);\n"); - code.append ("\t\ttreeItem" + j + ".setText (\"Item" + j + "\");\n"); + code.append("\t\tTreeItem treeItem" + j + " = new TreeItem (" + names [i] + ", SWT.NONE);\n"); + code.append("\t\ttreeItem" + j + ".setText (\"Item" + j + "\");\n"); } - } else if (controlType.equals ("ToolBar")) { - for (int j = 1; j < 3; j++) { - code.append ("\t\tToolItem toolItem" + j + " = new ToolItem (" + names [i] + ", SWT.NONE);\n"); - code.append ("\t\ttoolItem" + j + ".setText (\"Item" + j + "\");\n"); + } else if(controlType.equals("ToolBar")) { + for(int j = 1; j < 3; j++) { + code.append("\t\tToolItem toolItem" + j + " = new ToolItem (" + names [i] + ", SWT.NONE);\n"); + code.append("\t\ttoolItem" + j + ".setText (\"Item" + j + "\");\n"); } - } else if (controlType.equals ("CoolBar")) { - code.append ("\t\tToolBar coolToolBar = new ToolBar (" + names [i] + ", SWT.BORDER);\n"); - code.append ("\t\tToolItem coolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); - code.append ("\t\tcoolToolItem.setText (\"Item 1\");\n"); - code.append ("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); - code.append ("\t\tcoolToolItem.setText (\"Item 2\");\n"); - code.append ("\t\tCoolItem coolItem1 = new CoolItem (" + names [i] + ", SWT.NONE);\n"); - code.append ("\t\tcoolItem1.setControl (coolToolBar);\n"); - code.append ("\t\tPoint size = coolToolBar.computeSize (SWT.DEFAULT, SWT.DEFAULT);\n"); - code.append ("\t\tcoolItem1.setSize (coolItem1.computeSize (size.x, size.y));\n"); - code.append ("\t\tcoolToolBar = new ToolBar (" + names [i] + ", SWT.BORDER);\n"); - code.append ("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); - code.append ("\t\tcoolToolItem.setText (\"Item 3\");\n"); - code.append ("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); - code.append ("\t\tcoolToolItem.setText (\"Item 4\");\n"); - code.append ("\t\tCoolItem coolItem2 = new CoolItem (" + names [i] + ", SWT.NONE);\n"); - code.append ("\t\tcoolItem2.setControl (coolToolBar);\n"); - code.append ("\t\tsize = coolToolBar.computeSize (SWT.DEFAULT, SWT.DEFAULT);\n"); - code.append ("\t\tcoolItem2.setSize (coolItem2.computeSize (size.x, size.y));\n"); - code.append ("\t\t" + names [i] + ".setSize (" + names [i] + ".computeSize (SWT.DEFAULT, SWT.DEFAULT));\n"); - } else if (controlType.equals ("ProgressBar")) { - code.append ("\t\t" + names [i] + ".setSelection (50);\n"); + } else if(controlType.equals("CoolBar")) { + code.append("\t\tToolBar coolToolBar = new ToolBar (" + names [i] + ", SWT.BORDER);\n"); + code.append("\t\tToolItem coolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); + code.append("\t\tcoolToolItem.setText (\"Item 1\");\n"); + code.append("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); + code.append("\t\tcoolToolItem.setText (\"Item 2\");\n"); + code.append("\t\tCoolItem coolItem1 = new CoolItem (" + names [i] + ", SWT.NONE);\n"); + code.append("\t\tcoolItem1.setControl (coolToolBar);\n"); + code.append("\t\tPoint size = coolToolBar.computeSize (SWT.DEFAULT, SWT.DEFAULT);\n"); + code.append("\t\tcoolItem1.setSize (coolItem1.computeSize (size.x, size.y));\n"); + code.append("\t\tcoolToolBar = new ToolBar (" + names [i] + ", SWT.BORDER);\n"); + code.append("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); + code.append("\t\tcoolToolItem.setText (\"Item 3\");\n"); + code.append("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); + code.append("\t\tcoolToolItem.setText (\"Item 4\");\n"); + code.append("\t\tCoolItem coolItem2 = new CoolItem (" + names [i] + ", SWT.NONE);\n"); + code.append("\t\tcoolItem2.setControl (coolToolBar);\n"); + code.append("\t\tsize = coolToolBar.computeSize (SWT.DEFAULT, SWT.DEFAULT);\n"); + code.append("\t\tcoolItem2.setSize (coolItem2.computeSize (size.x, size.y));\n"); + code.append("\t\t" + names [i] + ".setSize (" + names [i] + ".computeSize (SWT.DEFAULT, SWT.DEFAULT));\n"); + } else if (controlType.equals("ProgressBar")) { + code.append("\t\t" + names [i] + ".setSelection (50);\n"); } /* Set text for those controls that support it */ - if (controlType.equals ("Button") || - controlType.equals ("Combo") || - controlType.equals ("Group") || - controlType.equals ("Label") || - controlType.equals ("StyledText") || - controlType.equals ("Text")) { - code.append ("\t\t" + names [i] + ".setText (\"" + names [i] + "\");\n"); + if (controlType.equals("Button") || + controlType.equals("Combo") || + controlType.equals("Group") || + controlType.equals("Label") || + controlType.equals("StyledText") || + controlType.equals("Text")) { + code.append("\t\t" + names [i] + ".setText (\"" + names [i] + "\");\n"); } return code; } @@ -496,15 +490,15 @@ abstract class Tab { * Returns the layout data field names. * Subclasses override this method. */ - String [] getLayoutDataFieldNames () { - return new String [] {}; + String[] getLayoutDataFieldNames() { + return new String[] {}; } /** * Gets the text for the tab folder item. * Subclasses override this method. */ - String getTabText () { + String getTabText() { return ""; } @@ -512,109 +506,110 @@ abstract class Tab { * Refreshes the composite and draws all controls * in the layout example. */ - void refreshLayoutComposite () { + void refreshLayoutComposite() { /* Remove children that are already laid out */ - children = layoutComposite.getChildren (); - for (int i = 0; i < children.length; i++) { - children [i].dispose (); + children = layoutComposite.getChildren(); + for(int i = 0; i < children.length; i++) { + children[i].dispose(); } /* Add all children listed in the table */ - TableItem [] items = table.getItems (); - children = new Control [items.length]; - String [] itemValues = new String [] { - LayoutExample.getResourceString ("Item",new String [] {"1"}), - LayoutExample.getResourceString ("Item",new String [] {"2"}), - LayoutExample.getResourceString ("Item",new String [] {"3"})}; - for (int i = 0; i < items.length; i++) { - String control = items [i].getText (1); - if (control.equals ("Button")) { - Button button = new Button (layoutComposite, SWT.PUSH); - button.setText (LayoutExample.getResourceString ("Button_Index", new String [] {new Integer (i).toString ()})); - children [i] = button; - } else if (control.equals ("Canvas")) { - Canvas canvas = new Canvas (layoutComposite, SWT.BORDER); - children [i] = canvas; - } else if (control.equals ("Combo")) { - Combo combo = new Combo (layoutComposite, SWT.NONE); - combo.setItems (itemValues); - combo.setText (LayoutExample.getResourceString ("Combo_Index", new String [] {new Integer (i).toString ()})); - children [i] = combo; - } else if (control.equals ("Composite")) { - Composite composite = new Composite (layoutComposite, SWT.BORDER); - children [i] = composite; - } else if (control.equals ("CoolBar")) { - CoolBar coolBar = new CoolBar (layoutComposite, SWT.NONE); - ToolBar toolBar = new ToolBar (coolBar, SWT.BORDER); - ToolItem item = new ToolItem (toolBar, 0); - item.setText (LayoutExample.getResourceString ("Item",new String [] {"1"})); - item = new ToolItem (toolBar, 0); - item.setText (LayoutExample.getResourceString ("Item",new String [] {"2"})); - CoolItem coolItem1 = new CoolItem (coolBar, 0); - coolItem1.setControl (toolBar); - toolBar = new ToolBar (coolBar, SWT.BORDER); - item = new ToolItem (toolBar, 0); - item.setText (LayoutExample.getResourceString ("Item",new String [] {"3"})); + TableItem[] items = table.getItems(); + children = new Control[items.length]; + String[] itemValues = new String[] { + LayoutExample.getResourceString("Item", new String[] {"1"}), + LayoutExample.getResourceString("Item", new String[] {"2"}), + LayoutExample.getResourceString("Item", new String[] {"3"})}; + for(int i = 0; i < items.length; i++) { + String control = items[i].getText(1); + String controlName = items[i].getText(0); + if (control.equals("Button")) { + Button button = new Button(layoutComposite, SWT.PUSH); + button.setText(controlName); + children[i] = button; + } else if (control.equals("Canvas")) { + Canvas canvas = new Canvas(layoutComposite, SWT.BORDER); + children[i] = canvas; + } else if(control.equals("Combo")) { + Combo combo = new Combo(layoutComposite, SWT.NONE); + combo.setItems(itemValues); + combo.setText(controlName); + children[i] = combo; + } else if(control.equals("Composite")) { + Composite composite = new Composite(layoutComposite, SWT.BORDER); + children[i] = composite; + } else if(control.equals("CoolBar")) { + CoolBar coolBar = new CoolBar(layoutComposite, SWT.NONE); + ToolBar toolBar = new ToolBar(coolBar, SWT.BORDER); + ToolItem item = new ToolItem(toolBar, 0); + item.setText(LayoutExample.getResourceString("Item",new String[] {"1"})); + item = new ToolItem(toolBar, 0); + item.setText(LayoutExample.getResourceString ("Item",new String[] {"2"})); + CoolItem coolItem1 = new CoolItem(coolBar, 0); + coolItem1.setControl(toolBar); + toolBar = new ToolBar(coolBar, SWT.BORDER); + item = new ToolItem(toolBar, 0); + item.setText(LayoutExample.getResourceString("Item",new String[] {"3"})); item = new ToolItem (toolBar, 0); - item.setText (LayoutExample.getResourceString ("Item",new String [] {"4"})); - CoolItem coolItem2 = new CoolItem (coolBar, 0); - coolItem2.setControl (toolBar); + item.setText(LayoutExample.getResourceString ("Item",new String [] {"4"})); + CoolItem coolItem2 = new CoolItem(coolBar, 0); + coolItem2.setControl(toolBar); Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT); - coolItem1.setSize(coolItem1.computeSize (size.x, size.y)); - coolItem2.setSize(coolItem2.computeSize (size.x, size.y)); + coolItem1.setSize(coolItem1.computeSize(size.x, size.y)); + coolItem2.setSize(coolItem2.computeSize(size.x, size.y)); coolBar.setSize(coolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT)); - children [i] = coolBar; - } else if (control.equals ("Group")) { + children[i] = coolBar; + } else if(control.equals("Group")) { Group group = new Group (layoutComposite, SWT.NONE); - group.setText (LayoutExample.getResourceString ("Group_Index", new String [] {new Integer (i).toString ()})); - children [i] = group; - } else if (control.equals ("Label")) { - Label label = new Label (layoutComposite, SWT.NONE); - label.setText (LayoutExample.getResourceString ("Label_Index", new String [] {new Integer (i).toString ()})); - children [i] = label; - } else if (control.equals ("List")) { - List list = new List (layoutComposite, SWT.BORDER); - list.setItems (itemValues); - children [i] = list; - } else if (control.equals ("ProgressBar")) { - ProgressBar progress = new ProgressBar (layoutComposite, SWT.NONE); - progress.setSelection (50); - children [i] = progress; - } else if (control.equals ("Scale")) { - Scale scale = new Scale (layoutComposite, SWT.NONE); - children [i] = scale; - } else if (control.equals ("Slider")) { - Slider slider = new Slider (layoutComposite, SWT.NONE); - children [i] = slider; - } else if (control.equals ("StyledText")) { - StyledText styledText = new StyledText (layoutComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); - styledText.setText (LayoutExample.getResourceString ("StyledText_Index", new String [] {new Integer (i).toString ()})); - children [i] = styledText; - } else if (control.equals ("Table")) { - Table table = new Table (layoutComposite, SWT.BORDER); - table.setLinesVisible (true); - TableItem item1 = new TableItem (table, 0); - item1.setText (LayoutExample.getResourceString ("Item",new String [] {"1"})); + group.setText(controlName); + children[i] = group; + } else if(control.equals("Label")) { + Label label = new Label(layoutComposite, SWT.NONE); + label.setText(controlName); + children[i] = label; + } else if(control.equals("List")) { + List list = new List(layoutComposite, SWT.BORDER); + list.setItems(itemValues); + children[i] = list; + } else if(control.equals("ProgressBar")) { + ProgressBar progress = new ProgressBar(layoutComposite, SWT.NONE); + progress.setSelection(50); + children[i] = progress; + } else if(control.equals("Scale")) { + Scale scale = new Scale(layoutComposite, SWT.NONE); + children[i] = scale; + } else if(control.equals("Slider")) { + Slider slider = new Slider(layoutComposite, SWT.NONE); + children[i] = slider; + } else if(control.equals("StyledText")) { + StyledText styledText = new StyledText(layoutComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + styledText.setText(controlName); + children[i] = styledText; + } else if(control.equals("Table")) { + Table table = new Table(layoutComposite, SWT.BORDER); + table.setLinesVisible(true); + TableItem item1 = new TableItem(table, 0); + item1.setText(LayoutExample.getResourceString("Item",new String[] {"1"})); TableItem item2 = new TableItem (table, 0); - item2.setText (LayoutExample.getResourceString ("Item",new String [] {"2"})); - children [i] = table; - } else if (control.equals ("Text")) { - Text text = new Text (layoutComposite, SWT.BORDER); - text.setText (LayoutExample.getResourceString ("Text_Index", new String [] {new Integer (i).toString ()})); - children [i] = text; - } else if (control.equals ("ToolBar")) { - ToolBar toolBar = new ToolBar (layoutComposite, SWT.BORDER); - ToolItem item1 = new ToolItem (toolBar, 0); - item1.setText (LayoutExample.getResourceString ("Item",new String [] {"1"})); - ToolItem item2 = new ToolItem (toolBar, 0); - item2.setText (LayoutExample.getResourceString ("Item",new String [] {"2"})); - children [i] = toolBar; + item2.setText(LayoutExample.getResourceString("Item",new String[] {"2"})); + children[i] = table; + } else if(control.equals("Text")) { + Text text = new Text(layoutComposite, SWT.BORDER); + text.setText(controlName); + children[i] = text; + } else if(control.equals("ToolBar")) { + ToolBar toolBar = new ToolBar(layoutComposite, SWT.BORDER); + ToolItem item1 = new ToolItem(toolBar, 0); + item1.setText(LayoutExample.getResourceString("Item",new String[] {"1"})); + ToolItem item2 = new ToolItem(toolBar, 0); + item2.setText(LayoutExample.getResourceString("Item",new String[] {"2"})); + children[i] = toolBar; } else { - Tree tree = new Tree (layoutComposite, SWT.BORDER); - TreeItem item1 = new TreeItem (tree, 0); - item1.setText (LayoutExample.getResourceString ("Item",new String [] {"1"})); - TreeItem item2 = new TreeItem (tree, 0); - item2.setText (LayoutExample.getResourceString ("Item",new String [] {"2"})); - children [i] = tree; + Tree tree = new Tree(layoutComposite, SWT.BORDER); + TreeItem item1 = new TreeItem(tree, 0); + item1.setText (LayoutExample.getResourceString("Item",new String[] {"1"})); + TreeItem item2 = new TreeItem(tree, 0); + item2.setText(LayoutExample.getResourceString ("Item",new String[] {"2"})); + children[i] = tree; } } } @@ -623,24 +618,24 @@ abstract class Tab { * Takes information from TableEditors and stores it. * Subclasses override this method. */ - void resetEditors () { - resetEditors (false); + void resetEditors() { + resetEditors(false); } - void resetEditors (boolean tab) { + void resetEditors(boolean tab) { } /** * Sets the layout data for the children of the layout. * Subclasses override this method. */ - void setLayoutData () { + void setLayoutData() { } /** * Sets the state of the layout. * Subclasses override this method. */ - void setLayoutState () { + void setLayoutState() { } } |