1 /******************************************************************************* 2 * Copyright (c) 2000, 2017 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.swt.examples.controlexample; 15 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.ControlListener; 19 import org.eclipse.swt.layout.GridData; 20 import org.eclipse.swt.layout.GridLayout; 21 import org.eclipse.swt.widgets.Button; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Group; 24 import org.eclipse.swt.widgets.TabFolder; 25 import org.eclipse.swt.widgets.Text; 26 import org.eclipse.swt.widgets.Widget; 27 28 class TextTab extends ScrollableTab { 29 /* Example widgets and groups that contain them */ 30 Text text; 31 Group textGroup; 32 33 /* Style widgets added to the "Style" group */ 34 Button wrapButton, readOnlyButton, passwordButton, searchButton, iconCancelButton, iconSearchButton; 35 Button leftButton, centerButton, rightButton; 36 37 /** 38 * Creates the Tab within a given instance of ControlExample. 39 */ TextTab(ControlExample instance)40 TextTab(ControlExample instance) { 41 super(instance); 42 } 43 44 /** 45 * Creates the "Example" group. 46 */ 47 @Override createExampleGroup()48 void createExampleGroup () { 49 super.createExampleGroup (); 50 51 /* Create a group for the text widget */ 52 textGroup = new Group (exampleGroup, SWT.NONE); 53 textGroup.setLayout (new GridLayout ()); 54 textGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true)); 55 textGroup.setText ("Text"); 56 } 57 58 /** 59 * Creates the "Example" widgets. 60 */ 61 @Override createExampleWidgets()62 void createExampleWidgets () { 63 64 /* Compute the widget style */ 65 int style = getDefaultStyle(); 66 if (singleButton.getSelection ()) style |= SWT.SINGLE; 67 if (multiButton.getSelection ()) style |= SWT.MULTI; 68 if (horizontalButton.getSelection ()) style |= SWT.H_SCROLL; 69 if (verticalButton.getSelection ()) style |= SWT.V_SCROLL; 70 if (wrapButton.getSelection ()) style |= SWT.WRAP; 71 if (readOnlyButton.getSelection ()) style |= SWT.READ_ONLY; 72 if (passwordButton.getSelection ()) style |= SWT.PASSWORD; 73 if (searchButton.getSelection ()) style |= SWT.SEARCH; 74 if (iconCancelButton.getSelection ()) style |= SWT.ICON_CANCEL; 75 if (iconSearchButton.getSelection ()) style |= SWT.ICON_SEARCH; 76 if (borderButton.getSelection ()) style |= SWT.BORDER; 77 if (leftButton.getSelection ()) style |= SWT.LEFT; 78 if (centerButton.getSelection ()) style |= SWT.CENTER; 79 if (rightButton.getSelection ()) style |= SWT.RIGHT; 80 81 /* Create the example widgets */ 82 text = new Text (textGroup, style); 83 text.setText (ControlExample.getResourceString("Example_string") + Text.DELIMITER + ControlExample.getResourceString("One_Two_Three")); 84 } 85 86 /** 87 * Creates the "Style" group. 88 */ 89 @Override createStyleGroup()90 void createStyleGroup() { 91 super.createStyleGroup(); 92 93 /* Create the extra widgets */ 94 wrapButton = new Button (styleGroup, SWT.CHECK); 95 wrapButton.setText ("SWT.WRAP"); 96 readOnlyButton = new Button (styleGroup, SWT.CHECK); 97 readOnlyButton.setText ("SWT.READ_ONLY"); 98 passwordButton = new Button (styleGroup, SWT.CHECK); 99 passwordButton.setText ("SWT.PASSWORD"); 100 searchButton = new Button (styleGroup, SWT.CHECK); 101 searchButton.setText ("SWT.SEARCH"); 102 iconCancelButton = new Button (styleGroup, SWT.CHECK); 103 iconCancelButton.setText ("SWT.ICON_CANCEL"); 104 iconSearchButton = new Button (styleGroup, SWT.CHECK); 105 iconSearchButton.setText ("SWT.ICON_SEARCH"); 106 107 Composite alignmentGroup = new Composite (styleGroup, SWT.NONE); 108 GridLayout layout = new GridLayout (); 109 layout.marginWidth = layout.marginHeight = 0; 110 alignmentGroup.setLayout (layout); 111 alignmentGroup.setLayoutData (new GridData (GridData.FILL_BOTH)); 112 leftButton = new Button (alignmentGroup, SWT.RADIO); 113 leftButton.setText ("SWT.LEFT"); 114 centerButton = new Button (alignmentGroup, SWT.RADIO); 115 centerButton.setText ("SWT.CENTER"); 116 rightButton = new Button (alignmentGroup, SWT.RADIO); 117 rightButton.setText ("SWT.RIGHT"); 118 } 119 120 /** 121 * Creates the tab folder page. 122 * 123 * @param tabFolder org.eclipse.swt.widgets.TabFolder 124 * @return the new page for the tab folder 125 */ 126 @Override createTabFolderPage(TabFolder tabFolder)127 Composite createTabFolderPage (TabFolder tabFolder) { 128 super.createTabFolderPage (tabFolder); 129 130 /* 131 * Add a resize listener to the tabFolderPage so that 132 * if the user types into the example widget to change 133 * its preferred size, and then resizes the shell, we 134 * recalculate the preferred size correctly. 135 */ 136 tabFolderPage.addControlListener(ControlListener.controlResizedAdapter(e -> setExampleWidgetSize ())); 137 138 return tabFolderPage; 139 } 140 141 /** 142 * Gets the "Example" widget children. 143 */ 144 @Override getExampleWidgets()145 Widget [] getExampleWidgets () { 146 return new Widget [] {text}; 147 } 148 149 /** 150 * Returns a list of set/get API method names (without the set/get prefix) 151 * that can be used to set/get values in the example control(s). 152 */ 153 @Override getMethodNames()154 String[] getMethodNames() { 155 return new String[] {"DoubleClickEnabled", "EchoChar", "Editable", "Message", "Orientation", "Selection", "Tabs", "Text", "TextChars", "TextLimit", "ToolTipText", "TopIndex"}; 156 } 157 158 /** 159 * Gets the text for the tab folder item. 160 */ 161 @Override getTabText()162 String getTabText () { 163 return "Text"; 164 } 165 166 /** 167 * Sets the state of the "Example" widgets. 168 */ 169 @Override setExampleWidgetState()170 void setExampleWidgetState () { 171 super.setExampleWidgetState (); 172 wrapButton.setSelection ((text.getStyle () & SWT.WRAP) != 0); 173 readOnlyButton.setSelection ((text.getStyle () & SWT.READ_ONLY) != 0); 174 passwordButton.setSelection ((text.getStyle () & SWT.PASSWORD) != 0); 175 searchButton.setSelection ((text.getStyle () & SWT.SEARCH) != 0); 176 leftButton.setSelection ((text.getStyle () & SWT.LEFT) != 0); 177 centerButton.setSelection ((text.getStyle () & SWT.CENTER) != 0); 178 rightButton.setSelection ((text.getStyle () & SWT.RIGHT) != 0); 179 180 /* Special case: ICON_CANCEL and H_SCROLL have the same value, 181 * and ICON_SEARCH and V_SCROLL have the same value, 182 * so to avoid confusion, only set CANCEL if SEARCH is set. */ 183 if ((text.getStyle () & SWT.SEARCH) != 0) { 184 iconCancelButton.setSelection ((text.getStyle () & SWT.ICON_CANCEL) != 0); 185 iconSearchButton.setSelection ((text.getStyle () & SWT.ICON_SEARCH) != 0); 186 horizontalButton.setSelection (false); 187 verticalButton.setSelection (false); 188 } else { 189 iconCancelButton.setSelection (false); 190 iconSearchButton.setSelection (false); 191 horizontalButton.setSelection ((text.getStyle () & SWT.H_SCROLL) != 0); 192 verticalButton.setSelection ((text.getStyle () & SWT.V_SCROLL) != 0); 193 } 194 195 passwordButton.setEnabled ((text.getStyle () & SWT.SINGLE) != 0); 196 searchButton.setEnabled ((text.getStyle () & SWT.SINGLE) != 0); 197 iconCancelButton.setEnabled ((text.getStyle () & SWT.SEARCH) != 0); 198 iconSearchButton.setEnabled ((text.getStyle () & SWT.SEARCH) != 0); 199 wrapButton.setEnabled ((text.getStyle () & SWT.MULTI) != 0); 200 horizontalButton.setEnabled ((text.getStyle () & SWT.MULTI) != 0); 201 verticalButton.setEnabled ((text.getStyle () & SWT.MULTI) != 0); 202 } 203 } 204