Skip to main content
summaryrefslogtreecommitdiffstats
blob: 488c6ea155437aba5547180484e6703e387566ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * Copyright (c) 2001, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
// Based on version 1.6 of original xsdeditor
package org.eclipse.wst.xsd.ui.internal.wizards;

import java.util.regex.Pattern;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.TextProcessor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorCSHelpIds;
import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorPlugin;
import org.eclipse.wst.xsd.ui.internal.util.ViewUtility;


public class RegexTestingPage extends WizardPage
{
  /* Validator from xerces package. */
//  private RegularExpression validator;
  
  /* Displays the status of the match. */
  private Label matchLabel;


  /* The regex. */
  private Text value;
  
  /* The string the user is trying to match against the regex. */
  private StyledText testString;

  public RegexTestingPage()
  {
    super(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_TESTING_PAGE_TITLE"));

    setTitle(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_TESTING_PAGE_TITLE"));
    setDescription(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_TESTING_PAGE_DESCRIPTION"));
  }


  public void createControl(Composite parent)
  {
    Composite composite = ViewUtility.createComposite(parent, 1);
    PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, XSDEditorCSHelpIds.REGEX_TEST_PAGE);

    matchLabel = new Label(composite, SWT.WRAP);
    matchLabel.setText(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_TESTING_PAGE_DESCRIPTION"));
    FontData[] fontData = matchLabel.getFont().getFontData();
    GridData dataF = new GridData();
    dataF.widthHint = 400;
    dataF.heightHint = 6 * fontData[0].getHeight();
    matchLabel.setLayoutData(dataF);
    
    Composite controls = new Composite(composite, SWT.NONE);
    GridLayout controlsLayout = new GridLayout();
    controlsLayout.numColumns = 2;
    controls.setLayout(controlsLayout);
    controls.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    new Label(controls, SWT.LEFT).setText(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_REGEX_LABEL"));
    value = new Text(controls, SWT.BORDER | SWT.READ_ONLY);
    value.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));


    new Label(controls, SWT.LEFT).setText(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_SAMPLE_TEXT"));
    testString = new StyledText(controls, SWT.SINGLE | SWT.BORDER);
    PlatformUI.getWorkbench().getHelpSystem().setHelp(testString, XSDEditorCSHelpIds.REGEX_SAMPLE_TEXT);
    testString.addListener(SWT.Modify, new TestStringListener());
    testString.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    
    controls.pack();
    
    Label separator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
    GC gc = new GC(separator);
    Point pointSize = gc.stringExtent(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_TESTING_PAGE_DESCRIPTION"));
    GridData gd = new GridData();
    gd.widthHint = pointSize.x / 2 + gc.getAdvanceWidth('M')*11;
    gd.horizontalAlignment= GridData.FILL;
    separator.setLayoutData(gd);
    
    composite.pack();

    setControl(composite);
  }


  private String getPatternValue()
  {
    return ( (RegexCompositionPage)getPreviousPage() ).getPattern().getLexicalValue();
  }

  private String getFlags()
  {
    return ( (RegexCompositionPage)getPreviousPage() ).getFlags();
  }

  public void setVisible(boolean visible)
  {
    super.setVisible(visible);

    getFlags();
    value.setText(TextProcessor.process(getPatternValue()));
    updateMatchStatus();
    testString.setFocus();
  }

  class TestStringListener implements Listener
  {
    public void handleEvent(Event e)
    {
      updateMatchStatus();
    }
  }

  private void updateMatchStatus()
  {
    if (Pattern.matches(getPatternValue(), testString.getText()))
    {
//      matchLabel.setText(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_MATCHES"));
      setMessage(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_MATCHES"), 1);
    }
    else
    {
      setMessage(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_DOES_NOT_MATCH"), 2);
//      matchLabel.setText(XSDEditorPlugin.getXSDString("_UI_REGEX_WIZARD_DOES_NOT_MATCH"));
    }
  }
}

Back to the top