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 org.eclipse.core.runtime.IStatus;
024import org.eclipse.core.runtime.Status;
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.Label;
032import org.eclipse.swt.widgets.Shell;
033import org.eclipse.swt.widgets.Text;
034import org.eclipse.ui.dialogs.SelectionStatusDialog;
035import org.granite.builder.GraniteActivator;
036import org.granite.builder.util.ProjectUtil;
037
038/**
039 * @author Franck WOLFF
040 */
041public class TemplateUrisDialog extends SelectionStatusDialog {
042
043        private final String initialTemplateUri;
044        private final String initialBaseTemplateUri;
045
046        private Text templateUri = null;
047        private Text baseTemplateUri = null;
048
049        public TemplateUrisDialog(Shell parent, String initialTemplateUri, String initialBaseTemplateUri) {
050                super(parent);
051                this.initialTemplateUri = initialTemplateUri;
052                this.initialBaseTemplateUri = initialBaseTemplateUri;
053        }
054
055        @Override
056        protected Control createDialogArea(Composite parent) {
057                Composite composite = (Composite)super.createDialogArea(parent);
058                GridData data = new GridData(GridData.FILL_BOTH);
059                data.widthHint = convertWidthInCharsToPixels(70);
060                composite.setLayoutData(data);
061
062                Label label = new Label(composite, SWT.NONE);
063                label.setText("Template URI:");
064                
065                templateUri = new Text(composite, SWT.BORDER);
066                templateUri.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
067                if (initialTemplateUri != null)
068                        templateUri.setText(initialTemplateUri);
069                templateUri.addModifyListener(new ModifyListener() {
070                        @Override
071                        public void modifyText(ModifyEvent event) {
072                                if (templateUri.getText().trim().length() == 0)
073                                        updateStatus(ProjectUtil.createErrorStatus("Tempate URI is mandatory"));
074                                else
075                                        updateStatus(new Status(IStatus.OK, GraniteActivator.PLUGIN_ID, ""));
076                        }
077                });
078
079                label = new Label(composite, SWT.NONE);
080                label.setText("Base template URI (optional):");
081                
082                baseTemplateUri = new Text(composite, SWT.BORDER);
083                baseTemplateUri.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
084                if (initialBaseTemplateUri != null)
085                        baseTemplateUri.setText(initialBaseTemplateUri);
086                
087                applyDialogFont(composite);
088                return composite;
089        }
090        
091        @Override
092        protected void okPressed() {
093                if (templateUri.getText().trim().length() == 0)
094                        updateStatus(ProjectUtil.createErrorStatus("Tempate URI is mandatory"));
095                else
096                        super.okPressed();
097        }
098
099        @Override
100        protected void computeResult() {
101                setSelectionResult(new String[]{templateUri.getText().trim(), baseTemplateUri.getText().trim()});
102        }
103        
104        @Override
105        protected void cancelPressed() {
106                setSelectionResult(null);
107                super.cancelPressed();
108        }
109}