001: package fr.aliacom.form.swt.ui;
002:
003: import org.eclipse.swt.SWT;
004: import org.eclipse.swt.custom.CLabel;
005: import org.eclipse.swt.events.SelectionAdapter;
006: import org.eclipse.swt.events.SelectionEvent;
007: import org.eclipse.swt.graphics.Color;
008: import org.eclipse.swt.graphics.Font;
009: import org.eclipse.swt.graphics.FontData;
010: import org.eclipse.swt.layout.FillLayout;
011: import org.eclipse.swt.layout.GridData;
012: import org.eclipse.swt.layout.GridLayout;
013: import org.eclipse.swt.layout.RowLayout;
014: import org.eclipse.swt.widgets.Button;
015: import org.eclipse.swt.widgets.Composite;
016: import org.eclipse.swt.widgets.Group;
017: import org.eclipse.swt.widgets.Label;
018:
019: import fr.aliacom.commands.Command;
020: import fr.aliacom.common.ui.ICard;
021: import fr.aliacom.common.ui.wizard.IWizard;
022: import fr.aliacom.common.ui.wizard.IWizardModel;
023: import fr.aliacom.form.common.IForm;
024:
025: /**
026: * @author tom
027: *
028: * (C) 2001, 2003 Thomas Cataldo
029: */
030: public final class SWTWizard extends AbstractCardPanel implements
031: IWizard {
032:
033: private Button cancel;
034: private CLabel cardTitle;
035: private int currentCard;
036: private Button finish;
037: private Command finishCommand;
038: private IForm form;
039: private IWizardModel model;
040: private Button next;
041: private Button prev;
042: private Composite toc;
043: private CLabel[] tocItems;
044: private Font bigBoldFont;
045:
046: public SWTWizard(Composite parent, IWizardModel model) {
047: super ();
048: this .model = model;
049: Composite panel = new Composite(parent, SWT.NONE);
050: GridLayout gl = new GridLayout();
051: gl.numColumns = 2;
052: gl.makeColumnsEqualWidth = false;
053: gl.marginHeight = 0;
054: gl.marginWidth = 0;
055: gl.horizontalSpacing = 0;
056: gl.verticalSpacing = 0;
057: panel.setLayout(gl);
058:
059: /* Top row with card title */
060: cardTitle = new CLabel(panel, SWT.NONE);
061: Font prevFont = cardTitle.getFont();
062: FontData[] fData = prevFont.getFontData();
063: for (int i = 0; i < fData.length; i++) {
064: fData[i].setHeight(fData[i].getHeight() + 6);
065: }
066: cardTitle.setFont(bigBoldFont = new Font(
067: cardTitle.getDisplay(), fData));
068: cardTitle.setBackground(new Color(panel.getDisplay(), 255, 255,
069: 255));
070: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
071: gd.horizontalSpan = 2;
072: cardTitle.setLayoutData(gd);
073:
074: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
075: | GridData.VERTICAL_ALIGN_FILL
076: | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
077:
078: if (model.isLinear()) {
079: toc = new Composite(panel, SWT.NO_FOCUS);
080: toc
081: .setLayoutData(new GridData(
082: GridData.VERTICAL_ALIGN_FILL));
083: RowLayout rl = new RowLayout(SWT.VERTICAL);
084: toc.setLayout(rl);
085: currentCard = 0;
086: } else {
087: gd.horizontalSpan = 2;
088: }
089:
090: GridLayout newLayout = new GridLayout();
091: newLayout.numColumns = 1;
092: newLayout.horizontalSpacing = 4;
093: newLayout.verticalSpacing = 4;
094: newLayout.marginHeight = 4;
095: newLayout.marginWidth = 4;
096: Composite comp = new Composite(panel, SWT.NONE);
097: comp.setLayout(newLayout);
098: comp.setLayoutData(gd);
099: Group group = new Group(comp, SWT.SHADOW_ETCHED_IN);
100: group.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
101: | GridData.VERTICAL_ALIGN_FILL
102: | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
103: group.setLayout(new FillLayout());
104:
105: cardPanelWidget = new Composite(group, SWT.NO_FOCUS);
106: cardPanelWidget.setLayout(layout);
107:
108: Label sep = new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL);
109: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
110: gd.horizontalSpan = 2;
111: gd.heightHint = 4;
112: sep.setLayoutData(gd);
113:
114: /* buttons */
115: gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
116: gd.horizontalSpan = 2;
117: Composite buttons = new Composite(panel, SWT.NO_FOCUS);
118: buttons.setLayoutData(gd);
119: buttons.setLayout(new RowLayout(SWT.HORIZONTAL));
120: prev = new Button(buttons, SWT.PUSH);
121: prev.setText("< Prev");
122: next = new Button(buttons, SWT.PUSH);
123: next.setText("Next >");
124: finish = new Button(buttons, SWT.PUSH);
125: finish.setText("Finish");
126: cancel = new Button(buttons, SWT.PUSH);
127: cancel.setText("Cancel");
128: createListeners();
129: }
130:
131: /**
132: * Method createListeners.
133: */
134: private void createListeners() {
135: cancel.addSelectionListener(new SelectionAdapter() {
136: public void widgetSelected(SelectionEvent e) {
137: if (getForm() != null) {
138: getForm().close();
139: }
140: bigBoldFont.dispose();
141: }
142: });
143: next.addSelectionListener(new SelectionAdapter() {
144: public void widgetSelected(SelectionEvent e) {
145: if (!model.isNextEnabled()) {
146: return;
147: }
148: setCard(model.next());
149: if (!model.isLinear()) {
150: return;
151: }
152: tocItems[currentCard++].setForeground(new Color(next
153: .getDisplay(), 0, 0, 0));
154: tocItems[currentCard].setForeground(new Color(next
155: .getDisplay(), 0, 0, 255));
156: cardTitle.setText(((ICard) controls.get(currentCard))
157: .getTitle());
158: }
159: });
160: prev.addSelectionListener(new SelectionAdapter() {
161: public void widgetSelected(SelectionEvent e) {
162: if (!model.isPreviousEnabled()) {
163: return;
164: }
165: setCard(model.previous());
166: if (!model.isLinear()) {
167: return;
168: }
169: tocItems[currentCard--].setForeground(new Color(next
170: .getDisplay(), 0, 0, 0));
171: tocItems[currentCard].setForeground(new Color(next
172: .getDisplay(), 0, 0, 255));
173: cardTitle.setText(((ICard) controls.get(currentCard))
174: .getTitle());
175: }
176: });
177: finish.addSelectionListener(new SelectionAdapter() {
178: public void widgetSelected(SelectionEvent e) {
179: if (finishCommand != null) {
180: finishCommand.run();
181: }
182: }
183: });
184: }
185:
186: /**
187: * Method createToc.
188: * @param parent
189: */
190: private void createToc() {
191: tocItems = new CLabel[controls.size()];
192: for (int i = 0, n = tocItems.length; i < n; i++) {
193: tocItems[i] = new CLabel(toc, SWT.NONE);
194: tocItems[i].setText(i + " - "
195: + ((ICard) controls.get(i)).getTitle());
196: }
197: tocItems[0]
198: .setForeground(new Color(toc.getDisplay(), 0, 0, 255));
199: }
200:
201: public void createUI() {
202: if (toc != null) {
203: createToc();
204: }
205: displayFirst();
206: cardTitle.setText(((ICard) controls.get(0)).getTitle());
207: }
208:
209: /**
210: * @see fr.aliacom.common.ui.wizard.IWizard#getFinishCommand()
211: */
212: public Command getFinishCommand() {
213: return finishCommand;
214: }
215:
216: /**
217: * @see fr.aliacom.common.ui.wizard.IWizard#getForm()
218: */
219: public IForm getForm() {
220: return form;
221: }
222:
223: /**
224: * @see fr.aliacom.common.ui.wizard.IWizard#getModel()
225: */
226: public IWizardModel getModel() {
227: return model;
228: }
229:
230: /**
231: * @see fr.aliacom.common.ui.wizard.IWizard#setFinishCommand(fr.aliacom.commands.Command)
232: */
233: public void setFinishCommand(Command a) {
234: this .finishCommand = a;
235: }
236:
237: /**
238: * @see fr.aliacom.common.ui.wizard.IWizard#setForm(fr.aliacom.form.common.IForm)
239: */
240: public void setForm(IForm form) {
241: this.form = form;
242: }
243:
244: }
|