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 021package org.granite.builder.ui; 022 023import java.util.regex.Pattern; 024 025import org.eclipse.swt.SWT; 026import org.eclipse.swt.events.ModifyEvent; 027import org.eclipse.swt.events.ModifyListener; 028import org.eclipse.swt.layout.GridData; 029import org.eclipse.swt.widgets.Composite; 030import org.eclipse.swt.widgets.Control; 031import org.eclipse.swt.widgets.Shell; 032import org.eclipse.swt.widgets.Text; 033import org.eclipse.ui.dialogs.SelectionStatusDialog; 034import org.granite.builder.util.SWTUtil; 035 036/** 037 * @author Franck WOLFF 038 */ 039public 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 @Override 062 public void modifyText(ModifyEvent event) { 063 String value = input.getText(); 064 if (valuePattern != null) { 065 if (valuePattern.matcher(value).matches()) { 066 input.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.WHITE)); 067 getOkButton().setEnabled(true); 068 } 069 else { 070 input.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.LIGHT_RED)); 071 getOkButton().setEnabled(false); 072 } 073 } 074 } 075 }); 076 077 applyDialogFont(composite); 078 return composite; 079 } 080 081 @Override 082 protected void computeResult() { 083 if (input != null && input.getText() != null) 084 setSelectionResult(new String[]{input.getText().trim()}); 085 } 086 087 @Override 088 protected void cancelPressed() { 089 setSelectionResult(null); 090 super.cancelPressed(); 091 } 092}