001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
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.events.SelectionAdapter;
029    import org.eclipse.swt.events.SelectionEvent;
030    import org.eclipse.swt.layout.FillLayout;
031    import org.eclipse.swt.layout.GridData;
032    import org.eclipse.swt.layout.GridLayout;
033    import org.eclipse.swt.widgets.Button;
034    import org.eclipse.swt.widgets.Composite;
035    import org.eclipse.swt.widgets.Control;
036    import org.eclipse.swt.widgets.Label;
037    import org.eclipse.swt.widgets.List;
038    import org.eclipse.swt.widgets.Shell;
039    import org.eclipse.swt.widgets.Text;
040    import org.eclipse.ui.dialogs.SelectionStatusDialog;
041    import org.granite.builder.util.SWTUtil;
042    import org.granite.builder.util.StringUtil;
043    
044    /**
045     * @author Franck WOLFF
046     */
047    public class IncludeExcludeOutputDialog extends SelectionStatusDialog {
048    
049            private static final Pattern CLUSION_PATTERN = Pattern.compile("[a-zA-Z_0-9\\*\\.\\$\\?/]+");
050            private static final Pattern OUTPUT_PATTERN = Pattern.compile("[a-zA-Z_0-9\\-\\.\\/]*");
051            
052            private final String[] initialValues;
053            
054            private List includes = null;
055            private List excludes = null;
056            private Text output = null;
057            private Text baseOutput = null; 
058            
059            public IncludeExcludeOutputDialog(Shell parent, String[] initialValues) {
060                    super(parent);
061                    
062                    if (initialValues == null)
063                            initialValues = new String[3];
064                    else if (initialValues.length != 3)
065                            throw new IllegalArgumentException("Bad initialValues length: " + initialValues.length);
066                    for (int i = 0; i < initialValues.length; i++) {
067                            if (initialValues[i] == null)
068                                    initialValues[i] = "";
069                    }
070                    
071                    this.initialValues = initialValues;
072                    
073                    setSelectionResult(null);
074                    setStatusLineAboveButtons(true);
075    
076                    int shellStyle = getShellStyle();
077                    setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
078            }
079            
080            @Override
081            protected void cancelPressed() {
082                    setSelectionResult(initialValues);
083                    super.cancelPressed();
084            }
085            
086            @Override
087            protected void computeResult() {
088                    String[] result = new String[3];
089                    
090                    StringBuilder sb = new StringBuilder();
091                    for (String value : includes.getItems()) {
092                            if (sb.length() > 0)
093                                    sb.append(';');
094                            sb.append(value);
095                    }
096                    result[0] = sb.toString();
097                    
098                    sb.setLength(0);
099                    for (String value : excludes.getItems()) {
100                            if (sb.length() > 0)
101                                    sb.append(';');
102                            sb.append(value);
103                    }
104                    result[1] = sb.toString();
105                    
106                    sb.setLength(0);
107                    sb.append(output.getText()).append(';').append(baseOutput.getText());
108                    result[2] = sb.toString();
109                    
110                    setSelectionResult(result);
111            }
112            
113            @Override
114            protected Control createDialogArea(Composite parent) {
115            final Composite composite = (Composite)super.createDialogArea(parent);
116                    GridData data = new GridData(GridData.FILL_BOTH);
117                    data.widthHint = convertWidthInCharsToPixels(70);
118                    data.heightHint = convertHeightInCharsToPixels(26);
119                    composite.setLayoutData(data);
120                    composite.setLayout(new GridLayout(2, false));
121                    
122                    Label label = new Label(composite, SWT.NONE);
123                    label.setText("Inclusion Patterns:");
124                    label.setLayoutData(SWTUtil.newGridData(SWT.NONE, 2));
125            
126                    includes = new List(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
127                    includes.setLayoutData(new GridData(GridData.FILL_BOTH));
128                    for (String s : StringUtil.split(initialValues[0], ';')) {
129                            if (s.length() != 0)
130                                    includes.add(s);
131                    }
132                    
133                    Composite includesButtons = new Composite(composite, SWT.NONE);
134                    includesButtons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
135                    includesButtons.setLayout(new FillLayout(SWT.VERTICAL));
136                    
137                    SWTUtil.newButton(includesButtons, "Add...", true, new SelectionAdapter() {
138                            @Override
139                            public void widgetSelected(SelectionEvent e) {
140                                    String pattern = Dialogs.prompt(
141                                            getShell(),
142                                            "Add Inclusion Pattern",
143                                            "Allowed wildcards are '?', '*' and '**'",
144                                            null,
145                                            CLUSION_PATTERN
146                                    );
147                                    if (pattern != null && pattern.trim().length() > 0)
148                                            includes.add(pattern.trim());
149                            }
150                    });
151                    final Button includesEditButton = SWTUtil.newButton(includesButtons, "Edit...", false, new SelectionAdapter() {
152                            @Override
153                            public void widgetSelected(SelectionEvent e) {
154                                    int selectedIndex = includes.getSelectionIndex();
155                                    String pattern = Dialogs.prompt(
156                                            getShell(),
157                                            "Edit Inclusion Pattern",
158                                            "Allowed wildcards are '?', '*' and '**'",
159                                            includes.getItem(selectedIndex),
160                                            CLUSION_PATTERN
161                                    );
162                                    includes.setItem(selectedIndex, pattern.trim());
163                            }
164                    });
165                    final Button includesRemoveButton = SWTUtil.newButton(includesButtons, "Remove", false, new SelectionAdapter() {
166                            @Override
167                            public void widgetSelected(SelectionEvent e) {
168                                    int selectedIndex = includes.getSelectionIndex();
169                                    includes.remove(selectedIndex);
170                                    
171                                    if (includes.getItemCount() <= 0) {
172                                            ((Button)e.getSource()).setEnabled(false);
173                                            includesEditButton.setEnabled(false);
174                                    }
175                                    else if (selectedIndex < includes.getItemCount())
176                                            includes.setSelection(selectedIndex);
177                                    else
178                                            includes.setSelection(selectedIndex - 1);
179                            }
180                    });
181                    
182                    includes.addSelectionListener(new SelectionAdapter() {
183                            @Override
184                            public void widgetSelected(SelectionEvent event) {
185                                    includesRemoveButton.setEnabled(true);
186                                    includesEditButton.setEnabled(true);
187                            }
188                    });
189                    
190                    label = new Label(composite, SWT.NONE);
191                    label.setText("Exclusion Patterns:");
192                    label.setLayoutData(SWTUtil.newGridData(SWT.NONE, 2));
193            
194                    excludes = new List(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
195                    excludes.setLayoutData(new GridData(GridData.FILL_BOTH));
196                    for (String s : StringUtil.split(initialValues[1], ';')) {
197                            if (s.length() != 0)
198                                    excludes.add(s);
199                    }
200                    
201                    Composite excludesButtons = new Composite(composite, SWT.NONE);
202                    excludesButtons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
203                    excludesButtons.setLayout(new FillLayout(SWT.VERTICAL));
204                    
205                    SWTUtil.newButton(excludesButtons, "Add...", true, new SelectionAdapter() {
206                            @Override
207                            public void widgetSelected(SelectionEvent e) {
208                                    String pattern = Dialogs.prompt(
209                                            getShell(),
210                                            "Add Exclusion Pattern",
211                                            "Allowed wildcards are '?', '*' and '**'",
212                                            null,
213                                            CLUSION_PATTERN
214                                    );
215                                    if (pattern != null && pattern.trim().length() > 0)
216                                            excludes.add(pattern.trim());
217                            }
218                    });
219                    final Button excludesEditButton = SWTUtil.newButton(excludesButtons, "Edit...", false, new SelectionAdapter() {
220                            @Override
221                            public void widgetSelected(SelectionEvent e) {
222                                    int selectedIndex = excludes.getSelectionIndex();
223                                    String pattern = Dialogs.prompt(
224                                            getShell(),
225                                            "Edit Exclusion Pattern",
226                                            "Allowed wildcards are '?', '*' and '**'",
227                                            excludes.getItem(selectedIndex),
228                                            CLUSION_PATTERN
229                                    );
230                                    excludes.setItem(selectedIndex, pattern.trim());
231                            }
232                    });
233                    final Button excludesRemoveButton = SWTUtil.newButton(excludesButtons, "Remove", false, new SelectionAdapter() {
234                            @Override
235                            public void widgetSelected(SelectionEvent e) {
236                                    int selectedIndex = excludes.getSelectionIndex();
237                                    excludes.remove(selectedIndex);
238                                    
239                                    if (excludes.getItemCount() <= 0) {
240                                            ((Button)e.getSource()).setEnabled(false);
241                                            excludesEditButton.setEnabled(false);
242                                    }
243                                    else if (selectedIndex < excludes.getItemCount())
244                                            excludes.setSelection(selectedIndex);
245                                    else
246                                            excludes.setSelection(selectedIndex - 1);
247                            }
248                    });
249                    
250                    excludes.addSelectionListener(new SelectionAdapter() {
251                            @Override
252                            public void widgetSelected(SelectionEvent event) {
253                                    excludesRemoveButton.setEnabled(true);
254                                    excludesEditButton.setEnabled(true);
255                            }
256                    });
257    
258                    String[] outputs = StringUtil.split(initialValues[2], ';');
259                    
260                    label = new Label(composite, SWT.NONE);
261                    label.setText("Output Directory (relative to project dir):");
262                    label.setLayoutData(SWTUtil.newGridData(SWT.NONE, 2));
263            
264            output = new Text(composite, SWT.BORDER);
265            output.setLayoutData(SWTUtil.newGridData(GridData.FILL_HORIZONTAL, 2));
266            if (outputs.length > 0)
267                    output.setText(outputs[0]);
268            output.addModifyListener(new ModifyListener() {
269                            public void modifyText(ModifyEvent event) {
270                                    if (OUTPUT_PATTERN.matcher(output.getText()).matches()) {
271                                            output.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.WHITE));
272                                            getOkButton().setEnabled(true);
273                                    }
274                                    else {
275                                            output.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.LIGHT_RED));
276                                            getOkButton().setEnabled(false);
277                                    }
278                            }
279                    });
280    
281            label = new Label(composite, SWT.NONE);
282            label.setText("Base Output Directory (optional, default to output above):");
283            label.setLayoutData(SWTUtil.newGridData(SWT.NONE, 2));
284            
285            baseOutput = new Text(composite, SWT.BORDER);
286            baseOutput.setLayoutData(SWTUtil.newGridData(GridData.FILL_HORIZONTAL, 2));
287            if (outputs.length > 1)
288                    baseOutput.setText(outputs[1]);
289            baseOutput.addModifyListener(new ModifyListener() {
290                            public void modifyText(ModifyEvent event) {
291                                    if (OUTPUT_PATTERN.matcher(baseOutput.getText()).matches()) {
292                                            baseOutput.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.WHITE));
293                                            getOkButton().setEnabled(true);
294                                    }
295                                    else {
296                                            baseOutput.setBackground(SWTUtil.getColor(getShell().getDisplay(), SWTUtil.LIGHT_RED));
297                                            getOkButton().setEnabled(false);
298                                    }
299                            }
300                    });
301    
302            applyDialogFont(composite);
303    
304            return composite;
305            }
306    }