001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.wizards;
025:
026: import java.io.File;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.Set;
030:
031: import org.eclipse.core.resources.IProject;
032: import org.eclipse.core.runtime.IPath;
033: import org.eclipse.core.runtime.Path;
034: import org.eclipse.jface.wizard.IWizardPage;
035: import org.eclipse.swt.SWT;
036: import org.eclipse.swt.events.ModifyEvent;
037: import org.eclipse.swt.events.ModifyListener;
038: import org.eclipse.swt.events.SelectionAdapter;
039: import org.eclipse.swt.events.SelectionEvent;
040: import org.eclipse.swt.layout.GridData;
041: import org.eclipse.swt.layout.GridLayout;
042: import org.eclipse.swt.widgets.Button;
043: import org.eclipse.swt.widgets.Combo;
044: import org.eclipse.swt.widgets.Composite;
045: import org.eclipse.swt.widgets.FileDialog;
046: import org.eclipse.swt.widgets.Label;
047: import org.eclipse.swt.widgets.Text;
048: import org.eclipse.ui.PlatformUI;
049: import org.eclipse.ui.part.FileEditorInput;
050:
051: import com.bostechcorp.cbesb.common.i18n.I18N;
052: import com.bostechcorp.cbesb.common.i18n.Message;
053: import com.bostechcorp.cbesb.common.i18n.Messages;
054: import com.bostechcorp.cbesb.common.util.custcomponent.IBooleanProperty;
055: import com.bostechcorp.cbesb.common.util.custcomponent.ICustComponent;
056: import com.bostechcorp.cbesb.common.util.custcomponent.IEndpointProperty;
057: import com.bostechcorp.cbesb.common.util.custcomponent.IEnumProperty;
058: import com.bostechcorp.cbesb.common.util.custcomponent.IFileProperty;
059: import com.bostechcorp.cbesb.common.util.custcomponent.IProperty;
060: import com.bostechcorp.cbesb.common.util.custcomponent.ITextProperty;
061: import com.bostechcorp.cbesb.common.util.custcomponent.ICustComponent.DefaultMEP;
062: import com.bostechcorp.cbesb.common.util.custcomponent.ICustComponent.Role;
063: import com.bostechcorp.cbesb.common.util.custcomponent.IFileProperty.FolderBrowseStyle;
064: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.Custom;
065: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.DefaultMepType;
066: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.PropertyObject;
067: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.RoleType;
068: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.Scheduler;
069: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.componentflowFactory;
070: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.part.ComponentFlowEditorDiagramEditorPlugin;
071: import com.bostechcorp.cbesb.ui.util.browser.BaseFileBrowser;
072:
073: public class CustomDynaPage extends PropertyWizardPage {
074:
075: private HashMap<Object, PropertyObject> map = new HashMap<Object, PropertyObject>();
076:
077: private HashMap<String, Object> map_require = new HashMap<String, Object>();
078:
079: protected IWizardPage nextPage;
080:
081: public Label defaultmepLabel;
082:
083: public Combo comboDefaultMep;
084:
085: protected Custom custom;
086:
087: protected ICustComponent customDef;
088:
089: protected int len;
090:
091: protected RoleType type;
092:
093: protected IProperty[] properties;
094:
095: private IProject project;
096:
097: String projectName;
098:
099: protected boolean consumerFirst = false;
100:
101: protected com.bostechcorp.cbesb.common.util.custcomponent.IWizardPage wizardPage;
102:
103: private SchedulerGroup tiggerMethodGroup;
104:
105: private HashMap<String, Object> detailMap = new HashMap<String, Object>(); // for
106:
107: // scheduler
108:
109: private Scheduler scheduler = componentflowFactory.eINSTANCE
110: .createScheduler();
111:
112: protected CustomDynaPage(
113: Custom custom,
114: ICustComponent customDef,
115: com.bostechcorp.cbesb.common.util.custcomponent.IWizardPage wizardPage,
116: RoleType type, boolean consumerFirst) {
117: super (wizardPage.getName());
118: this .custom = custom;
119: this .customDef = customDef;
120: setTitle(wizardPage.getName());
121: setDescription(wizardPage.getDescription());
122: this .wizardPage = wizardPage;
123: properties = wizardPage.getProperties();
124: len = properties.length;
125: this .type = type;
126:
127: FileEditorInput editorInput = (FileEditorInput) PlatformUI
128: .getWorkbench().getActiveWorkbenchWindow()
129: .getActivePage().getActiveEditor().getEditorInput();
130: project = editorInput.getFile().getProject();
131: String project_name = project.toString();
132: projectName = project_name.substring(
133: project_name.indexOf("/") + 1, project_name.length());
134: this .consumerFirst = consumerFirst;
135: }
136:
137: public void createControl(Composite parent) {
138: final Composite topLevel = new Composite(parent, SWT.NONE);
139: final GridLayout gridLayout = new GridLayout();
140: gridLayout.horizontalSpacing = 10;
141: gridLayout.numColumns = 3;
142: topLevel.setLayout(gridLayout);
143: topLevel.setLayoutData(new GridData(
144: GridData.VERTICAL_ALIGN_FILL
145: | GridData.HORIZONTAL_ALIGN_FILL));
146: topLevel.setFont(parent.getFont());
147: this .setControl(topLevel);
148:
149: if (consumerFirst) {
150: defaultmepLabel = new Label(topLevel, SWT.NONE);
151: defaultmepLabel
152: .setText(I18N.getString(Messages.WIZARD_MEP));
153: comboDefaultMep = new Combo(topLevel, SWT.READ_ONLY);
154: comboDefaultMep.setLayoutData(new GridData(SWT.FILL,
155: SWT.CENTER, true, false, 2, 1));
156: if (customDef.getDefaultMep(Role.CONSUMER).equals(
157: DefaultMEP.IN_ONLY)) {
158: comboDefaultMep.setItems(new String[] { "in-only" });
159: comboDefaultMep.setText(custom.getCustomConsumer()
160: .getDefaultMep().getLiteral());
161: } else if (customDef.getDefaultMep(Role.CONSUMER).equals(
162: DefaultMEP.IN_OUT)) {
163: comboDefaultMep.setItems(new String[] { "in-only",
164: "in-out", "reliablein" });
165: comboDefaultMep.setText(custom.getCustomConsumer()
166: .getDefaultMep().getLiteral());
167: }
168:
169: else {
170: comboDefaultMep.setItems(new String[] { "in-only",
171: "reliablein" });
172: comboDefaultMep.setText(custom.getCustomConsumer()
173: .getDefaultMep().getLiteral());
174: }
175: }
176:
177: for (int i = 0; i < len; i++) {
178: final Label labelName = new Label(topLevel, SWT.NONE);
179: PropertyObject pro = null;
180: if (type.equals(RoleType.get(RoleType.CONSUMER))) {
181: pro = custom.getValueConsumer(properties[i].getName());
182: } else {
183: pro = custom.getValueProvider(properties[i].getName());
184: }
185:
186: labelName.setText(properties[i].getName());
187: final boolean require = properties[i].isRequired();
188: if (require)
189: labelName.setText("*" + properties[i].getName());
190: else
191: labelName.setText(properties[i].getName());
192: boolean readonly = properties[i].isReadonly();
193:
194: PropertyObject obj = componentflowFactory.eINSTANCE
195: .createPropertyObject();
196: obj.setName(properties[i].getName());
197: obj.setValue(pro.getValue());
198:
199: if (properties[i] instanceof ITextProperty) {
200: int style = SWT.BORDER
201: | (((ITextProperty) properties[i]).isPassword() ? SWT.PASSWORD
202: : 0);
203:
204: final Text textName = new Text(topLevel, style);
205:
206: if (readonly)
207: textName.setEditable(false);
208: textName.setLayoutData(new GridData(SWT.FILL,
209: SWT.CENTER, true, false, 2, 1));
210: if (pro.getValue() != null)
211: textName.setText(pro.getValue());
212:
213: map.put(textName, obj);
214: textName.addModifyListener(new ModifyListener() {
215: public void modifyText(ModifyEvent e) {
216: map.get(textName).setValue(textName.getText());
217: updatePageComplete();
218: }
219: });
220: if (require)
221: map_require.put(obj.getName(), textName);
222:
223: }
224:
225: else if (properties[i] instanceof IBooleanProperty) {
226: final Button button = new Button(topLevel, SWT.CHECK);
227: map.put(button, obj);
228: if (readonly)
229: button.setEnabled(false);
230: button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
231: true, false, 2, 1));
232: if (pro.getValue().equalsIgnoreCase("true")) {
233: button.setSelection(true);
234: } else
235: button.setSelection(false);
236:
237: button.addSelectionListener(new SelectionAdapter() {
238: public void widgetSelected(SelectionEvent e) {
239:
240: if (button.getSelection())
241: map.get(button).setValue("true");
242: else
243:
244: map.get(button).setValue("false");
245:
246: }
247: });
248:
249: } else if (properties[i] instanceof IEnumProperty) {
250: final Combo combo;
251: boolean editable = ((IEnumProperty) properties[i])
252: .isEditable();
253:
254: if (editable) {
255: combo = new Combo(topLevel, SWT.BORDER);
256: } else {
257: combo = new Combo(topLevel, SWT.READ_ONLY);
258: }
259:
260: map.put(combo, obj);
261: combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
262: true, false, 2, 1));
263:
264: String[] values = ((IEnumProperty) properties[i])
265: .getValues();
266: for (int m = 0; m < values.length; m++) {
267: combo.add(values[m]);
268: }
269:
270: if (pro.getValue() != null)
271: combo.setText(pro.getValue());
272:
273: if (readonly)
274: combo.setEnabled(false);
275:
276: combo.addModifyListener(new ModifyListener() {
277: public void modifyText(ModifyEvent e) {
278: map.get(combo).setValue(combo.getText());
279: updatePageComplete();
280: }
281: });
282:
283: if (require)
284: map_require.put(obj.getName(), combo);
285:
286: } else if (properties[i] instanceof IEndpointProperty) {
287: final Combo endPointCombo;
288: boolean editable = ((IEndpointProperty) properties[i])
289: .isEditable();
290:
291: if (editable) {
292: endPointCombo = new Combo(topLevel, SWT.BORDER);
293: } else {
294: endPointCombo = new Combo(topLevel, SWT.READ_ONLY);
295: }
296:
297: map.put(endPointCombo, obj);
298: endPointCombo.setLayoutData(new GridData(SWT.FILL,
299: SWT.CENTER, true, false, 2, 1));
300: endPointCombo.add("<None>");
301: ArrayList<String> list = WizardUtil
302: .getAllComponentNameList();
303: if (list != null) {
304: for (int j = 0; j < list.size(); j++) {
305: if (list.get(j) != null) {
306: endPointCombo.add(list.get(j));
307: }
308: }
309: }
310:
311: if (pro.getValue() != null)
312: endPointCombo.setText(pro.getValue());
313:
314: if (readonly)
315: endPointCombo.setEnabled(false);
316:
317: endPointCombo.addModifyListener(new ModifyListener() {
318: public void modifyText(ModifyEvent e) {
319: map.get(endPointCombo).setValue(
320: endPointCombo.getText());
321: updatePageComplete();
322: }
323: });
324: if (require)
325: map_require.put(obj.getName(), endPointCombo);
326:
327: } else {
328:
329: final boolean allowFile = ((IFileProperty) properties[i])
330: .allowNewFile();
331: final String baseFolderName = ((IFileProperty) properties[i])
332: .getBaseFolderName();
333:
334: final FolderBrowseStyle style = ((IFileProperty) properties[i])
335: .getFolderBrowseStyle();
336:
337: final Text textName = new Text(topLevel, SWT.BORDER);
338: map.put(textName, obj);
339: if (readonly)
340: textName.setEditable(false);
341: if (pro.getValue() != null)
342: textName.setText(pro.getValue());
343: textName.setLayoutData(new GridData(SWT.FILL,
344: SWT.CENTER, true, false, 1, 1));
345: textName.addModifyListener(new ModifyListener() {
346: public void modifyText(ModifyEvent e) {
347: map.get(textName).setValue(textName.getText());
348: updatePageComplete();
349: // String text = textName.getText();
350: /*
351: * if(require && !text.trim().equals(""))
352: * fileExist(text);
353: */
354: }
355: });
356: final Button button_browse = new Button(topLevel,
357: SWT.NONE);
358: button_browse.setText(I18N
359: .getString(Messages.MDL_COMMON_BROWSE));
360:
361: button_browse
362: .addSelectionListener(new SelectionAdapter() {
363: public void widgetSelected(
364: final SelectionEvent e) {
365: if (style
366: .equals(FolderBrowseStyle.FILE_SYSTEM)) {
367: IPath folder = new Path(
368: baseFolderName);
369: IPath path = browse(folder,
370: allowFile);
371: textName.setText(path.toString());
372: } else {
373: BaseFileBrowser browser = new BaseFileBrowser(
374: ComponentFlowEditorDiagramEditorPlugin
375: .getInstance(),
376: button_browse.getShell(),
377: textName,
378: projectName,
379: ".*",
380: baseFolderName,
381: I18N
382: .getString(Messages.CMW_ADVANCED_MESSAGE_ERROR_SELECT_FILE));
383: browser.open();
384: }
385:
386: }
387: });
388: if (require)
389: map_require.put(obj.getName(), textName);
390: }
391:
392: }
393: // disply also the scheduler composite
394: boolean isScheduleable = ((CustomPropertyWizard) getWizard())
395: .getCustom().getCustomComponent()
396: .isConsumerScheduleable();
397: if (isScheduleable && consumerFirst) {
398: // final Composite composite = new Composite(topLevel, SWT.NONE);
399: // composite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA));
400: // composite.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_GREEN));
401: // composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
402: // true, 3, 1));
403: // composite.setLayout(new GridLayout());
404:
405: tiggerMethodGroup = new SchedulerGroup(topLevel, SWT.NONE,
406: I18N.getString(Messages.SCAN_INT), "5000",
407: detailMap, this .getWizard().isUpdateOperation());
408: tiggerMethodGroup.setLayoutData(new GridData(SWT.FILL,
409: SWT.FILL, true, true, 3, 1));
410:
411: if (this .getWizard().isUpdateOperation()) {
412: modelsToControl();
413: }
414: }
415:
416: updatePageComplete();
417: }
418:
419: private void fileExist(String text) {
420: File file = new File(text);
421: if (!file.exists()) {
422: this .setPageComplete(false);
423: this .setErrorMessage(I18N.getString(Messages.IDE_FILE_NOT));
424:
425: }
426: }
427:
428: private void updatePageComplete() {
429: Object[] obj = map_require.values().toArray();
430: Set set_key = map_require.keySet();
431: Object[] key = set_key.toArray();
432:
433: for (int i = 0; i < obj.length; i++) {
434: if (emptyText(obj[i])) {
435: this .setPageComplete(false);
436: this .setErrorMessage(new Message(
437: Messages.REQUIRE_FIELD, (String) key[i])
438: .getMessage());
439:
440: break;
441: } else {
442:
443: this .setPageComplete(true);
444: this .setErrorMessage(null);
445: }
446:
447: }
448: }
449:
450: private IPath browse(IPath path, boolean allowNew) {
451: FileDialog dialog = new FileDialog(getShell(),
452: allowNew ? SWT.SAVE : SWT.OPEN);
453: dialog.setFilterPath(path.toOSString());
454: dialog.setFilterExtensions(new String[] { "*.*", });
455: String result = dialog.open();
456: if (result == null)
457: return null;
458: if (allowNew) {
459: File file = new File(result);
460: if (!file.exists()) {
461: try {
462: boolean success = file.createNewFile();
463: } catch (Exception e) {
464: e.printStackTrace();
465: }
466: }
467: }
468: return new Path(result);
469: }
470:
471: private boolean emptyText(Object obj) {
472: if (obj instanceof Text) {
473: if (((Text) obj).getText().trim().equals("")) {
474: return true;
475: } else
476: return false;
477:
478: } else if (obj instanceof Combo) {
479: if (((Combo) obj).getText().trim().equals("")) {
480: return true;
481: } else
482: return false;
483: } else
484: return false;
485: }
486:
487: public IWizardPage getNextPage() {
488: return nextPage;
489: }
490:
491: public void setNextPage(IWizardPage nextPage) {
492: this .nextPage = nextPage;
493: }
494:
495: @Override
496: public void controlToModels() {
497: CustomPropertyWizard CustomWizard = (CustomPropertyWizard) this
498: .getWizard();
499: Custom models = CustomWizard.getModels();
500: if (models == null)
501: return;
502: Object[] properties = map.values().toArray();
503: try {
504: if (type.equals(RoleType.get(RoleType.CONSUMER))) {
505: for (int i = 0; i < properties.length; i++) {
506: String name = ((PropertyObject) properties[i])
507: .getName();
508:
509: models.getValueConsumer(name)
510: .setValue(
511: ((PropertyObject) properties[i])
512: .getValue());
513: }
514: } else {
515: for (int i = 0; i < properties.length; i++) {
516:
517: String name = ((PropertyObject) properties[i])
518: .getName();
519:
520: models.getValueProvider(name)
521: .setValue(
522: ((PropertyObject) properties[i])
523: .getValue());
524: }
525: }
526: // setting standard properties
527: if ((consumerFirst)
528: && ((CustomPropertyPageMain) CustomWizard
529: .getPage("CommonProperty")).isConsumer()) {
530: // checking MEP
531: if (comboDefaultMep.getText().equals("in-only")) {
532: models.getCustomConsumer().setDefaultMep(
533: DefaultMepType.get(DefaultMepType.INONLY));
534: } else if (comboDefaultMep.getText().equals("in-out")) {
535: models.getCustomConsumer().setDefaultMep(
536: DefaultMepType.get(DefaultMepType.INOUT));
537: } else
538: models.getCustomConsumer().setDefaultMep(
539: DefaultMepType
540: .get(DefaultMepType.RELIABLEIN));
541: // Checking Scheduler
542: if (tiggerMethodGroup != null)
543: tiggerMethodGroup.controlToModel();
544: //
545:
546: models.getCustomConsumer().setUseScheduler(
547: new Boolean((String) detailMap
548: .get("useschedule")));
549: if (new Boolean((String) detailMap.get("useschedule"))) {
550: SchedulerHelper.copy((Scheduler) detailMap
551: .get("scheduler"), models
552: .getCustomConsumer().getScheduler());
553: } else {
554: models.getCustomConsumer().setTriggerInteval(
555: (String) detailMap.get("scaninterval"));
556: }
557: }
558:
559: } catch (Exception e) {
560: e.printStackTrace();
561: }
562:
563: }
564:
565: @Override
566: public void modelsToControl() {
567:
568: if (this .custom.getCustomConsumer().isUseScheduler()) {
569: Scheduler modelScheduler = this .custom.getCustomConsumer()
570: .getScheduler();
571: SchedulerHelper.copy(modelScheduler, scheduler);
572: detailMap.put("scheduler", scheduler);
573: detailMap.put("useschedule", new Boolean(this .custom
574: .getCustomConsumer().isUseScheduler()).toString());
575: comboDefaultMep.select(comboDefaultMep.indexOf(this .custom
576: .getCustomConsumer().getDefaultMep().getLiteral()));
577: } else
578: detailMap.put("scaninterval", this.custom
579: .getCustomConsumer().getTriggerInteval());
580:
581: if (tiggerMethodGroup != null)
582: tiggerMethodGroup.modelToControl();
583:
584: }
585:
586: }
|