/* * Deragon Informatique inc. Copyright 2003. * @author Hans Deragon * Created on 2003-03-24 * * This code is released under the LGPL license. For text of the license, * see http://www.gnu.org/licenses/lgpl.html */ package biz.deragon.swt.examples; import org.eclipse.jface.text.Document; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import biz.deragon.swt.layout.FormLayoutColumn; import biz.deragon.swt.models.*; import biz.deragon.swt.widgets.*; /** * @author Hans Deragon */ public class All extends ApplicationWindow { static final int ORIGINAL = 1; static final int COPY = 2; private StringModel mStringModel = null; private ComboModel mComboModel = null; private ButtonModel mButtonModel1 = null; private ButtonModel mButtonModel2 = null; private ButtonModel mButtonModel3 = null; private ButtonModel mRadioButtonModel1 = null; private ButtonModel mRadioButtonModel2 = null; private ButtonModel mRadioButtonModel3 = null; private ButtonModel mRBRL1 = null; private ButtonModel mRBRL2 = null; private ButtonModel mRBRL3 = null; private ButtonModel mRBFL1 = null; private ButtonModel mRBFL2 = null; private ButtonModel mRBFL3 = null; private RadioGroupModel mRgModel = null; private ButtonModel mRBFL2_1 = null; private ButtonModel mRBFL2_2 = null; private ButtonModel mRBFL2_3 = null; private Document mDoc = null; public static void main(String[] args) { All all = new All(); all.setBlockOnOpen(true); all.open(); } /** * @param parentShell */ public All() { super(null); } protected Control createContents(Composite parent) { Composite mainComposite = new Composite(parent, SWT.NULL); mainComposite.setLayout(new RowLayout(SWT.VERTICAL)); buildButtonBarComposite(mainComposite); buildWidgetsComposite(mainComposite); return parent; } private Composite buildButtonBarComposite(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new FillLayout()); Button button = new Button(composite, SWT.PUSH); button.setText("Text fields"); button.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent event) { handleEvent(); } public void widgetDefaultSelected(SelectionEvent event) { handleEvent(); } private void handleEvent() { TextFields window = new TextFields(null); window.open(); } }); return composite; } private Composite buildWidgetsComposite(Composite parent) { mStringModel = new StringModel(); mComboModel = new ComboModel(); mComboModel.add("Element #1"); mComboModel.add("Element #2"); mComboModel.add("Element #3"); mComboModel.setIndex(0); mComboModel.setAsOriginal(); mButtonModel1 = new ButtonModel(); mButtonModel2 = new ButtonModel(); mButtonModel3 = new ButtonModel(); mRadioButtonModel1 = new ButtonModel(); mRadioButtonModel2 = new ButtonModel(); mRadioButtonModel3 = new ButtonModel(); mRBRL1 = new ButtonModel(); mRBRL2 = new ButtonModel(); mRBRL3 = new ButtonModel(); mRBFL1 = new ButtonModel(); mRBFL2 = new ButtonModel(); mRBFL3 = new ButtonModel(); mRgModel = new RadioGroupModel(); mRBFL2_1 = new ButtonModel(); mRBFL2_2 = new ButtonModel(); mRBFL2_3 = new ButtonModel(); mRgModel.add(mRBFL2_1, 0); mRgModel.add(mRBFL2_2, 1); mRgModel.add(mRBFL2_3, 2); mDoc = new Document(); Composite composite = new Composite(parent, SWT.NULL); FillLayout layout = new FillLayout(); composite.setLayout(layout); makeColumn(composite, ORIGINAL); makeColumn(composite, COPY); return composite; } private void makeColumn(Composite parent, int type) { WidgetWithModel widget; WidgetWithModel.setDefaultShowChange(true); Composite cmp = new Composite(parent, SWT.BORDER); FormLayout layout = new FormLayout(); layout.marginHeight = 5; layout.marginWidth = 5; cmp.setLayout(layout); FormLayoutColumn flCol = new FormLayoutColumn(cmp, 0, 0, 5, 5); String titleLabel; if(type == ORIGINAL) titleLabel = "Original fields"; else titleLabel = "Mirror fields"; Label header = new Label(cmp, SWT.NULL); header.setText(titleLabel); Font font = new Font(parent.getDisplay(), "Arial", 14, SWT.BOLD); header.setFont(font); flCol.setHeader(header); widget = new WText(cmp, SWT.BORDER, mStringModel); flCol.add("A text field:", widget); //TextViewer tv = new TextViewer(cmp, SWT.BORDER); //tv.setDocument(mDoc); //Text text = (Text) tv.getControl(); //text.setTextLimit(10); //flCol.add("TextViewer:", tv.getControl()); WCombo wCombo = new WCombo(cmp, SWT.NULL, mComboModel); flCol.add("A combo:", wCombo); if(false) { flCol.layout(); return; } WButton wNormalButton = new WButton(cmp, SWT.NULL, mButtonModel1); wNormalButton.setText("Press here"); flCol.add("A normal button:", wNormalButton); WButton wCheckButton = new WButton(cmp, SWT.CHECK | SWT.LEFT, mButtonModel2); wCheckButton.setText("Check:"); flCol.add("A check button:", wCheckButton); WButton wToggleButton = new WButton(cmp, SWT.TOGGLE, mButtonModel3); wToggleButton.setText("Press here"); flCol.add("A toggle button:", wToggleButton); RowLayout wRadioGroupLayout = new RowLayout(SWT.VERTICAL); WRadioGroup wRadioGroupWithRowLayout = new WRadioGroup(cmp, SWT.NULL, wRadioGroupLayout); WButton tmpButton; wRadioGroupWithRowLayout.setShowChange(true); wRadioGroupWithRowLayout.addRadio(tmpButton = new WButton(wRadioGroupWithRowLayout, SWT.RADIO, mRBRL1), 0); tmpButton.setText("Radio #1"); wRadioGroupWithRowLayout.addRadio(tmpButton = new WButton(wRadioGroupWithRowLayout, SWT.RADIO, mRBRL2), 1); tmpButton.setText("Radio #2"); wRadioGroupWithRowLayout.addRadio(tmpButton = new WButton(wRadioGroupWithRowLayout, SWT.RADIO, mRBRL3), 2); tmpButton.setText("Radio #3"); flCol.add("Vertical radios:", wRadioGroupWithRowLayout); // In this example, the radio buttons are created with internal models, thus the mirror // version will not reflect any changes done on the original. WRadioGroupFillLayout wRadioGroupFillLayout = new WRadioGroupFillLayout(cmp, SWT.NULL); wRadioGroupFillLayout.setShowChange(true); wRadioGroupFillLayout.add("Radio #1", 0); wRadioGroupFillLayout.add("Radio #2", 1); wRadioGroupFillLayout.add("Radio #3", 2); flCol.add("Adding without models:", wRadioGroupFillLayout); // Here, we pass common models, thus any changes on the original radio buttons will be // automatically reflected on the mirror version. WRadioGroupFillLayout wRadioGroupFillLayout2 = new WRadioGroupFillLayout(cmp, SWT.NULL); wRadioGroupFillLayout2.add("Radio #1", mRBFL1, 0); wRadioGroupFillLayout2.add("Radio #2", mRBFL2, 1); wRadioGroupFillLayout2.add("Radio #3", mRBFL3, 2); flCol.add("Adding with models:", wRadioGroupFillLayout2); // Here, we use a RadioGroupModel to control the widgets. String labels3[] = {"Radio #1", "Radio #2", "Radio #3" }; WRadioGroupFillLayout wRadioGroupFillLayout3 = new WRadioGroupFillLayout(cmp, SWT.NULL, mRgModel, labels3, SWT.HORIZONTAL); flCol.add("RadioGroupModel used:", wRadioGroupFillLayout3); // Following is an example where SWT MVC Wrapper widgets do not behave like normal // SWT widgets. Unfortunatly, the following standard SWT code for manipulating radio // button will not work. Notice that they WButton, even if they are part of a same // Composite, are not mutually exclusive. Composite radioCmp = new Composite(cmp, SWT.NULL); radioCmp.setLayout(new FillLayout()); flCol.add("Wrong way with WRadioButton:", radioCmp); WButton wRadio1 = new WButton(radioCmp, SWT.RADIO, mRadioButtonModel1); wRadio1.setAlignment(SWT.LEFT); wRadio1.setText("Radio #1"); WButton wRadio2 = new WButton(radioCmp, SWT.RADIO, mRadioButtonModel2); wRadio2.setAlignment(SWT.RIGHT); wRadio2.setText("Radio #2"); WButton wRadio3 = new WButton(radioCmp, SWT.RADIO, mRadioButtonModel3); wRadio3.setText("Radio #3"); flCol.layout(); } // protected Point getInitialSize() // { // return getShell().computeSize(1024, 500, true); // } }