Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 65fd8c6434c645d6e5ed13441f960bf0a45b23fa (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*******************************************************************************
 * Copyright (c) 2013 Red Hat Inc. 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:
 *     Neil Guzman - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.rpm.createrepo.wizard;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.linuxtools.internal.rpm.createrepo.ICreaterepoConstants;
import org.eclipse.linuxtools.internal.rpm.createrepo.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * This page allows the user to initialize the .repo file
 * for the repository with the mandatory options: id, name, and
 * base url.
 */
public class CreaterepoNewWizardPageTwo extends WizardPage {

    private Text repositoryIDTxt;
    private Text repositoryNameTxt;
    private Text repositoryBaseURLTxt;

    /**
     * Constructor for CreaterepoWizardPage. Will set the page name, title, and
     * description.
     *
     * @param pageName The wizard page's name.
     */
    public CreaterepoNewWizardPageTwo(String pageName) {
        super(pageName);
        setTitle(Messages.CreaterepoNewWizardPageTwo_wizardPageTitle);
        setDescription(Messages.CreaterepoNewWizardPageTwo_wizardPageDescription);
        setPageComplete(false);
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    @Override
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        GridData layoutData = new GridData();
        layoutData.grabExcessHorizontalSpace = true;
        layoutData.grabExcessVerticalSpace = true;
        layoutData.horizontalAlignment = GridData.FILL;
        layoutData.verticalAlignment = GridData.FILL;
        container.setLayout(layout);
        container.setLayoutData(layoutData);

        // composite to hold the required information
        Composite information = new Composite(container, SWT.NONE);
        layout = new GridLayout(2, false);
        layoutData = new GridData();
        layoutData.grabExcessHorizontalSpace = true;
        layoutData.horizontalAlignment = GridData.FILL;
        layoutData.verticalAlignment = GridData.CENTER;
        information.setLayoutData(layoutData);
        information.setLayout(layout);

        // listen on modifying the Text widgets
        ModifyListener modifyListner = new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                setPageComplete(isValid());
            }
        };
        repositoryIDTxt = createTextFieldWithLabel(information, Messages.CreaterepoNewWizardPageTwo_labelID,
                Messages.CreaterepoNewWizardPageTwo_tooltipID);
        repositoryIDTxt.addModifyListener(modifyListner);
        repositoryNameTxt = createTextFieldWithLabel(information, Messages.CreaterepoNewWizardPageTwo_labelName,
                Messages.CreaterepoNewWizardPageTwo_tooltipName);
        repositoryNameTxt.addModifyListener(modifyListner);
        repositoryBaseURLTxt = createTextFieldWithLabel(information, Messages.CreaterepoNewWizardPageTwo_labelURL,
                Messages.CreaterepoNewWizardPageTwo_tooltipURL);
        repositoryBaseURLTxt.addModifyListener(modifyListner);
        setControl(container);
    }

    /**
     * Create a text field with a label.
     *
     * @param parent The parent of the text field and label.
     * @param labelName The name on the label.
     * @return The newly created text field.
     */
    protected Text createTextFieldWithLabel(Composite parent, String labelName, String tooltip) {
        GridData layoutData = new GridData();
        // create the label
        Label respositoryBaseURLLbl = new Label(parent, SWT.NONE);
        respositoryBaseURLLbl.setText(labelName);
        layoutData = new GridData();
        layoutData.horizontalAlignment = GridData.BEGINNING;
        layoutData.verticalAlignment = GridData.CENTER;
        respositoryBaseURLLbl.setToolTipText(tooltip);
        // create the text field
        Text textField = new Text(parent, SWT.BORDER | SWT.SINGLE);
        layoutData = new GridData();
        layoutData.grabExcessHorizontalSpace = true;
        layoutData.horizontalAlignment = GridData.FILL;
        layoutData.verticalAlignment = GridData.CENTER;
        textField.setLayoutData(layoutData);
        textField.setToolTipText(tooltip);
        return textField;
    }

    /**
     * Check to see if all the text fields are valid. Otherwise, set error
     * messages appropriately.
     *
     * @return True if all the text fields are valid, false otherwise.
     */
    protected boolean isValid() {
        if (!validateID()) {
            setErrorMessage(Messages.CreaterepoNewWizardPageTwo_errorID);
        }
        if (repositoryNameTxt.getText().trim().isEmpty()) {
            setErrorMessage(Messages.CreaterepoNewWizardPageTwo_errorName);
        }
        if (!validateURL()) {
            setErrorMessage(Messages.CreaterepoNewWizardPageTwo_errorURL);
        }
        if (validateID() && validateURL() && !repositoryNameTxt.getText().trim().isEmpty()) {
            setErrorMessage(null);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check if the ID is valid. It is valid if it is a single string and
     * not empty.
     *
     * @return True if the ID is valid, false otherwise.
     */
    private boolean validateID() {
        // check if repository ID is a single string
        String tmpRepoID = repositoryIDTxt.getText().trim();
        Pattern singleStringPattern = Pattern.compile("\\b(\\S+)\\b", //$NON-NLS-1$
                Pattern.CASE_INSENSITIVE);
        Matcher singleStringMatcher = singleStringPattern.matcher(tmpRepoID);
        if (singleStringMatcher.matches()) {
            return true;
        }
        return false;
    }

    /**
     * Check if the URL is valid. It is valid if it contains a valid protocol and link.
     *
     * @return True if the URL is valid, false otherwise.
     */
    private boolean validateURL() {
        // check if baseURL is a valid URL
        String tmpRepoURL = repositoryBaseURLTxt.getText().trim();
        // TODO: possibly validate if pointing to something that exists (not really necessary, but nice)
        try {
            new URL(tmpRepoURL);
            return true;
        } catch (MalformedURLException e) {
            return false;
        }
    }

    public String getRepositoryID() {
        if (repositoryIDTxt == null) {
            return ICreaterepoConstants.EMPTY_STRING;
        }
        return repositoryIDTxt.getText().trim();
    }

    public String getRepositoryName() {
        if (repositoryNameTxt == null) {
            return ICreaterepoConstants.EMPTY_STRING;
        }
        return repositoryNameTxt.getText().trim();
    }

    public String getRepositoryURL() {
        if (repositoryBaseURLTxt == null) {
            return ICreaterepoConstants.EMPTY_STRING;
        }
        return repositoryBaseURLTxt.getText().trim();
    }

}

Back to the top