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.ArrayList;
024    import java.util.regex.Pattern;
025    
026    import org.eclipse.core.resources.IContainer;
027    import org.eclipse.core.resources.IFolder;
028    import org.eclipse.core.resources.IProject;
029    import org.eclipse.core.resources.IResource;
030    import org.eclipse.core.resources.IWorkspaceRoot;
031    import org.eclipse.core.resources.ResourcesPlugin;
032    import org.eclipse.core.runtime.IPath;
033    import org.eclipse.jdt.core.IJavaProject;
034    import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
035    import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
036    import org.eclipse.jface.window.Window;
037    import org.eclipse.swt.widgets.Shell;
038    import org.eclipse.swt.widgets.TreeItem;
039    import org.eclipse.ui.model.WorkbenchContentProvider;
040    import org.eclipse.ui.model.WorkbenchLabelProvider;
041    import org.granite.builder.properties.Gas3Source;
042    import org.granite.builder.util.SWTUtil;
043    
044    /**
045     * @author Franck WOLFF
046     */
047    @SuppressWarnings("all")
048    public class Dialogs {
049    
050            public static String[] addPackageTranslator(Shell shell, String title) {
051                    PackageTranslatorDialog dialog = new PackageTranslatorDialog(shell, null, null);
052            
053                    dialog.setTitle(title);
054            dialog.setHelpAvailable(false);
055            dialog.create();
056            
057            if (dialog.open() == Window.OK) {
058                    Object[] result = dialog.getResult();
059                    if (result != null && result.length == 2)
060                            return (String[])result;
061            }
062            
063            return null;
064            }
065    
066            public static String[] editPackageTranslator(Shell shell, String title, String initialJavaPath, String initialAs3Path) {
067                    PackageTranslatorDialog dialog = new PackageTranslatorDialog(shell, initialJavaPath, initialAs3Path);
068            
069                    dialog.setTitle(title);
070            dialog.setHelpAvailable(false);
071            dialog.create();
072            
073            if (dialog.open() == Window.OK) {
074                    Object[] result = dialog.getResult();
075                    if (result != null && result.length == 2)
076                            return (String[])result;
077            }
078            
079            return null;
080            }
081    
082            public static String[] editTemplateUris(Shell shell, String title, String initialTemplateUri, String initialBaseTemplateUri) {
083                    TemplateUrisDialog dialog = new TemplateUrisDialog(shell, initialTemplateUri, initialBaseTemplateUri);
084            
085                    dialog.setTitle(title);
086            dialog.setHelpAvailable(false);
087            dialog.create();
088            
089            if (dialog.open() == Window.OK) {
090                    Object[] result = dialog.getResult();
091                    if (result != null && result.length == 2)
092                            return (String[])result;
093            }
094            
095            return null;
096            }
097    
098            public static String prompt(Shell shell, String title, String message, String initialValue, Pattern valuePattern) {
099                    PromptDialog dialog = new PromptDialog(shell, initialValue, valuePattern);
100            
101                    dialog.setTitle(title);
102            dialog.setMessage(message);
103            dialog.setHelpAvailable(false);
104            dialog.create();
105            dialog.getOkButton().setEnabled(initialValue != null && initialValue.trim().length() > 0);
106            
107            if (dialog.open() == Window.OK) {
108                    Object[] result = dialog.getResult();
109                    if (result != null && result.length > 0)
110                            return (String)result[0];
111            }
112            
113            return null;
114            }
115            
116            public static Gas3Source editSourceFolderAttributes(IJavaProject project, Shell shell, Gas3Source source) {
117                    String[] initialValues = new String[] {source.getIncludes(), source.getExcludes(), source.getOutput()};
118                    
119                    IncludeExcludeOutputDialog dialog = new IncludeExcludeOutputDialog(shell, initialValues);
120                    dialog.setTitle("Source Folder Configuration");
121                    dialog.setHelpAvailable(false);
122                    
123                    if (dialog.open() == Window.OK) {
124                            String[] result = (String[])dialog.getResult();
125                            if (result != null) {
126                                    source.setIncludes(result[0]);
127                                    source.setExcludes(result[1]);
128                                    source.setOutput(result[2]);
129                            }
130                    }
131                    return source;
132            }
133            
134            public static IPath[] chooseSourceFolderEntries(IJavaProject project, Shell shell, IPath initialSelection, IPath[] usedEntries) {
135                    if (usedEntries == null)
136                            throw new IllegalArgumentException();
137                    String title= "Source Folder Selection"; 
138                    String message= "Choose source folder to be add to generation process"; 
139                    return internalChooseFolderEntries(project, shell, initialSelection, usedEntries, title, message);
140            }
141            
142                    
143            private static IPath[] internalChooseFolderEntries(IJavaProject project, Shell shell, IPath initialSelection, IPath[] usedEntries, String title, String message) {      
144                    Class[] acceptedClasses= new Class[] { IProject.class, IFolder.class };
145                    ArrayList usedContainers= new ArrayList(usedEntries.length);
146                    IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
147                    for (int i= 0; i < usedEntries.length; i++) {
148                            IResource resource= root.findMember(usedEntries[i]);
149                            if (resource instanceof IContainer) {
150                                    usedContainers.add(resource);
151                            }
152                    }
153                    
154                    IResource focus= initialSelection != null ? root.findMember(initialSelection) : null;
155                    Object[] used= usedContainers.toArray();
156                    
157                    MultipleFolderSelectionDialog dialog= new MultipleFolderSelectionDialog(
158                            shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider(), false);
159                    dialog.setExisting(used);
160                    dialog.setTitle(title); 
161                    dialog.setMessage(message); 
162                    dialog.setHelpAvailable(false);
163                    dialog.addFilter(new JavaFoldersViewerFilter(project));
164                    dialog.setInput(root);
165                    dialog.setInitialFocus(focus);
166                    
167                    if (dialog.open() == Window.OK) {
168                            Object[] elements= dialog.getResult();
169                            IPath[] res= new IPath[elements.length];
170                            for (int i= 0; i < res.length; i++) {
171                                    IResource elem= (IResource) elements[i];
172                                    res[i]= elem.getFullPath();
173                            }
174                            return res;
175                    }
176                    return null;            
177            }
178            
179            public static IPath[] chooseProjectEntries(IJavaProject project, Shell shell, IPath initialSelection, IPath[] usedEntries) {
180                    if (usedEntries == null)
181                            throw new IllegalArgumentException();
182                    String title= "Other Granite Project Selection"; 
183                    String message= "Choose granite project to be used in the generation process"; 
184                    return internalProjectEntries(project, shell, initialSelection, usedEntries, title, message);
185            }
186            
187                    
188            private static IPath[] internalProjectEntries(IJavaProject project, Shell shell, IPath initialSelection, IPath[] usedEntries, String title, String message) {   
189                    Class[] acceptedClasses= new Class[] { IProject.class };
190                    ArrayList usedContainers= new ArrayList(usedEntries.length);
191                    IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
192                    for (int i= 0; i < usedEntries.length; i++) {
193                            IResource resource= root.findMember(usedEntries[i]);
194                            if (resource instanceof IContainer)
195                                    usedContainers.add(resource);
196                    }
197                    
198                    IResource focus= initialSelection != null ? root.findMember(initialSelection) : null;
199                    Object[] used= usedContainers.toArray();
200                    
201                    MultipleFolderSelectionDialog dialog= new MultipleFolderSelectionDialog(
202                            shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider(), false);
203                    dialog.setExisting(used);
204                    dialog.setTitle(title); 
205                    dialog.setMessage(message); 
206                    dialog.setHelpAvailable(false);
207                    dialog.addFilter(new GraniteProjectsViewerFilter(project));
208                    dialog.setInput(root);
209                    dialog.setInitialFocus(focus);
210                    
211                    if (dialog.open() == Window.OK) {
212                            Object[] elements= dialog.getResult();
213                            IPath[] res= new IPath[elements.length];
214                            for (int i= 0; i < res.length; i++) {
215                                    IResource elem= (IResource) elements[i];
216                                    res[i]= elem.getFullPath();
217                            }
218                            return res;
219                    }
220                    return null;            
221            }
222    }