001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder.ui;
022
023 import java.util.regex.Pattern;
024
025 import org.eclipse.swt.SWT;
026 import org.eclipse.swt.events.ModifyEvent;
027 import org.eclipse.swt.events.ModifyListener;
028 import org.eclipse.swt.layout.GridData;
029 import org.eclipse.swt.widgets.Composite;
030 import org.eclipse.swt.widgets.Control;
031 import org.eclipse.swt.widgets.Shell;
032 import org.eclipse.swt.widgets.Text;
033 import org.eclipse.ui.dialogs.SelectionStatusDialog;
034 import org.granite.builder.util.SWTUtil;
035
036 /**
037 * @author Franck WOLFF
038 */
039 public class PromptDialog extends SelectionStatusDialog {
040
041 private final String initialValue;
042 private final Pattern valuePattern;
043
044 private Text input = null;
045
046 public PromptDialog(Shell parent, String initialValue, Pattern valuePattern) {
047 super(parent);
048 this.initialValue = initialValue;
049 this.valuePattern = valuePattern;
050 }
051
052 @Override
053 protected Control createDialogArea(Composite parent) {
054 Composite composite = (Composite)super.createDialogArea(parent);
055 createMessageArea(composite);
056 input = new Text(composite, SWT.BORDER);
057 input.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
058 if (initialValue != null)
059 input.setText(initialValue);
060 input.addModifyListener(new ModifyListener() {
061 public void modifyText(ModifyEvent event) {
062 String value = input.getText();
063 if (valuePattern != null) {
064 if (valuePattern.matcher(value).matches()) {
065 input.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.WHITE));
066 getOkButton().setEnabled(true);
067 }
068 else {
069 input.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.LIGHT_RED));
070 getOkButton().setEnabled(false);
071 }
072 }
073 }
074 });
075
076 applyDialogFont(composite);
077 return composite;
078 }
079
080 @Override
081 protected void computeResult() {
082 if (input != null && input.getText() != null)
083 setSelectionResult(new String[]{input.getText().trim()});
084 }
085
086 @Override
087 protected void cancelPressed() {
088 setSelectionResult(null);
089 super.cancelPressed();
090 }
091 }