001: package com.bostechcorp.cbesb.ui.variant.wizards;
002:
003: import java.util.regex.Matcher;
004: import java.util.regex.Pattern;
005:
006: import org.eclipse.core.resources.IContainer;
007: import org.eclipse.core.resources.IResource;
008: import org.eclipse.jface.viewers.ISelection;
009: import org.eclipse.jface.viewers.IStructuredSelection;
010: import org.eclipse.jface.wizard.WizardPage;
011: import org.eclipse.swt.SWT;
012: import org.eclipse.swt.events.ModifyEvent;
013: import org.eclipse.swt.events.ModifyListener;
014: import org.eclipse.swt.layout.GridData;
015: import org.eclipse.swt.layout.GridLayout;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Label;
018: import org.eclipse.swt.widgets.MessageBox;
019: import org.eclipse.swt.widgets.Text;
020: import org.eclipse.ui.IWorkbench;
021: import org.eclipse.ui.PlatformUI;
022:
023: import com.bostechcorp.cbesb.common.i18n.Message;
024: import com.bostechcorp.cbesb.common.i18n.Messages;
025: import com.bostechcorp.cbesb.ui.baseformat.editor.editors.CommonEditor;
026:
027: public class VariantNewWizardMainPage extends WizardPage {
028:
029: private Text nameText;
030: private ISelection selection;
031: private IWorkbench workbench;
032: protected String filename = "";
033:
034: /**
035: * Constructor for VariantNewWizardMainPage.
036: *
037: * @param workbench
038: * @param selection
039: */
040: public VariantNewWizardMainPage(IWorkbench workbench,
041: ISelection selection, String title, String description) {
042: super ("MainPage");
043: this .selection = selection;
044: this .workbench = workbench;
045: setTitle(title);// "Choose
046: setDescription(description);// "Choose
047:
048: }
049:
050: public void createControl(Composite parent) {
051:
052: final Composite composite = new Composite(parent, SWT.NONE);
053: final GridLayout gridLayout = new GridLayout();
054: gridLayout.numColumns = 2;
055: gridLayout.marginTop = 10;
056: gridLayout.verticalSpacing = 10;
057: composite.setLayout(gridLayout);
058:
059: initialize();
060: setControl(composite);
061:
062: final Label nameLabel = new Label(composite, SWT.NONE);
063: nameLabel.setText("Name");
064:
065: nameText = new Text(composite, SWT.BORDER);
066: nameText.addModifyListener(new ModifyListener() {
067: public void modifyText(ModifyEvent e) {
068: isNameValid();
069: }
070: });
071: filename = nameText.getText();
072: nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
073: false));
074: }
075:
076: private void initialize() {
077: if (selection != null && selection.isEmpty() == false
078: && selection instanceof IStructuredSelection) {
079: IStructuredSelection ssel = (IStructuredSelection) selection;
080: if (ssel.size() > 1)
081: return;
082: Object obj = ssel.getFirstElement();
083: if (obj instanceof IResource) {
084: IContainer container;
085: if (obj instanceof IContainer)
086: container = (IContainer) obj;
087: else
088: container = ((IResource) obj).getParent();
089: }
090: }
091: }
092:
093: private void isNameValid() {
094:
095: filename = nameText.getText();
096: if (filename.equals("")) {
097: setPageComplete(false);
098: setErrorMessage("Name is required.");
099: return;
100: } else {
101: if (!validateName(filename)) {
102: setPageComplete(false);
103: return;
104: }
105: }
106: setPageComplete(true);
107: setErrorMessage(null);
108:
109: }
110:
111: public static void messageBox(String text, String message) {
112: MessageBox msg = new MessageBox(PlatformUI.getWorkbench()
113: .getActiveWorkbenchWindow().getShell(),
114: SWT.ICON_WARNING | SWT.OK);
115: msg.setText(text);
116: msg.setMessage(message);
117: msg.open();
118: }
119:
120: public boolean validateName(String name) {
121: Pattern pattern = Pattern.compile("([_]|([a-z]|[A-Z])).*");
122: Matcher matcher = pattern.matcher(name);
123: if (matcher.matches()) {
124: Pattern pattern2 = Pattern.compile("([a-zA-Z0-9]|[_])+");
125: Matcher matcher2 = pattern2.matcher(name);
126: if (matcher2.matches()) {
127: Pattern pattern1 = Pattern.compile("[xX][mM][lL].*");
128: Matcher matcher1 = pattern1.matcher(name);
129: if (matcher1.matches()) {
130: this
131: .messageBox(
132: new Message(
133: Messages.MDL_ERROR_WARNING)
134: .getMessage(),
135: new Message(
136: Messages.MDL_ERROR_XML_BEGIN_WARNING)
137: .getMessage());
138: return false;
139: } else
140: return true;
141: } else {
142: this .messageBox(new Message(Messages.MDL_ERROR_WARNING)
143: .getMessage(), new Message(
144: Messages.MDL_NAME_PRO_WARNING).getMessage());
145: return false;
146: }
147: } else {
148:
149: this .messageBox(new Message(Messages.MDL_ERROR_WARNING)
150: .getMessage(), new Message(
151: Messages.MDL_ERROR_BEGIN_WARNING).getMessage());
152: return false;
153: }
154: }
155:
156: public String getFilename() {
157: return filename;
158: }
159:
160: public void setFilename(String filename) {
161: this.filename = filename;
162: }
163:
164: }
|