View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.resolver;
14  
15  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
16  import com.eviware.soapui.support.StringUtils;
17  import com.eviware.soapui.support.UISupport;
18  import com.eviware.soapui.support.action.SoapUIAction;
19  
20  import java.io.File;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  public class ResolveContext<T extends AbstractWsdlModelItem<?>>
25  {
26     private List<PathToResolve> pathsToResolve = new ArrayList<PathToResolve>();
27     private final T project;
28  
29     public ResolveContext( T project )
30     {
31        this.project = project;
32     }
33  
34     public T getProject()
35     {
36        return project;
37     }
38  
39     public PathToResolve addPathToResolve(
40             AbstractWsdlModelItem<?> owner, String description, String path,
41             SoapUIAction<AbstractWsdlModelItem<?>> defaultAction
42     )
43     {
44        PathToResolve pathToResolve = new PathToResolve( owner, description, path, defaultAction );
45        pathsToResolve.add( pathToResolve );
46        return pathToResolve;
47     }
48  
49     public PathToResolve addPathToResolve(
50             AbstractWsdlModelItem<?> owner, String description, String path,
51             Resolver resolver
52     )
53     {
54        PathToResolve pathToResolve = new PathToResolve( owner, description, path, null );
55        pathToResolve.addResolvers( resolver );
56        pathsToResolve.add( pathToResolve );
57        return pathToResolve;
58     }
59  
60     public class PathToResolve
61     {
62        private final AbstractWsdlModelItem<?> owner;
63        private final String description;
64        private List<Resolver> resolvers = new ArrayList<Resolver>();
65        private final String path;
66        private SoapUIAction<AbstractWsdlModelItem<?>> defaultAction;
67        private Resolver resolver;
68  
69        public PathToResolve( AbstractWsdlModelItem<?> owner, String description, String path, SoapUIAction<AbstractWsdlModelItem<?>> defaultAction )
70        {
71           this.owner = owner;
72           this.description = description;
73           this.path = path;
74           this.defaultAction = defaultAction;
75        }
76  
77        public SoapUIAction<?> getDefaultAction()
78        {
79           return defaultAction;
80        }
81  
82        public void setDefaultAction( SoapUIAction<AbstractWsdlModelItem<?>> defaultAction )
83        {
84           this.defaultAction = defaultAction;
85        }
86  
87        public void addResolvers( Resolver... resolvers )
88        {
89           for( Resolver res : resolvers )
90           {
91              this.resolvers.add( res );
92           }
93        }
94  
95        public AbstractWsdlModelItem<?> getOwner()
96        {
97           return owner;
98        }
99  
100       public String getDescription()
101       {
102          return description;
103       }
104 
105       public Resolver getResolver()
106       {
107          return resolver;
108       }
109 
110       public String getPath()
111       {
112          return path;
113       }
114 
115       public boolean resolve()
116       {
117          if( resolver != null )
118             return resolver.resolve();
119 
120          if( defaultAction != null )
121          {
122             defaultAction.perform( owner, this );
123             return true;
124          }
125 
126          return false;
127       }
128 
129       public void setResolver( Object resolveOrDefaultAction )
130       {
131          this.resolver = (Resolver) resolveOrDefaultAction;
132       }
133 
134       public ArrayList<Resolver> getResolvers()
135       {
136          return (ArrayList<Resolver>) resolvers;
137       }
138    }
139 
140    public interface Resolver
141    {
142       public boolean resolve();
143 
144 //		public boolean apply();
145 
146       public boolean isResolved();
147 
148       public String getResolvedPath();
149 
150       public Object getDescription();
151 
152    }
153 
154    public boolean isEmpty()
155    {
156       return pathsToResolve.isEmpty();
157    }
158 
159    public List<PathToResolve> getPathsToResolve()
160    {
161       return pathsToResolve;
162    }
163 
164    public int getUnresolvedCount()
165    {
166       int resultCnt = 0;
167 
168       for( PathToResolve ptr : pathsToResolve )
169       {
170          if( ptr.getResolver() == null || !ptr.getResolver().isResolved() )
171             resultCnt++;
172       }
173 
174       return resultCnt;
175    }
176 
177    public int apply()
178    {
179       int resultCnt = 0;
180 
181       for( PathToResolve ptr : pathsToResolve )
182       {
183          if( ptr.resolve() )
184             resultCnt++;
185       }
186 
187       return resultCnt;
188    }
189 
190    public abstract static class FileResolver implements Resolver
191    {
192       private String title;
193       private String extension;
194       private String fileType;
195       private String current;
196       private File result;
197       private boolean resolved;
198 
199       public FileResolver( String title, String extension, String fileType, String current )
200       {
201          super();
202 
203          this.title = title;
204          this.extension = extension;
205          this.fileType = fileType;
206          this.current = current;
207       }
208 
209       public boolean isResolved()
210       {
211          return resolved;
212       }
213 
214       public String getResolvedPath()
215       {
216          return result == null ? null : result.getAbsolutePath();
217       }
218 
219       public abstract boolean apply( File newFile );
220 
221       public boolean resolve()
222       {
223          result = UISupport.getFileDialogs().open( this, title, extension, fileType, current );
224          if( result != null )
225             resolved = apply( result );
226 
227          return resolved;
228       }
229 
230       public Object getDescription()
231       {
232          return title;
233       }
234    }
235 
236    public abstract static class DirectoryResolver implements Resolver
237    {
238       private String title;
239       private String current;
240       private File result;
241       private boolean resolved;
242 
243       public DirectoryResolver( String title, String current )
244       {
245          super();
246 
247          this.title = title;
248          this.current = current;
249       }
250 
251       public boolean isResolved()
252       {
253          return resolved;
254       }
255 
256       public String getResolvedPath()
257       {
258          return result == null ? null : result.getAbsolutePath();
259       }
260 
261       public abstract boolean apply( File newFile );
262 
263       public boolean resolve()
264       {
265          result = UISupport.getFileDialogs().openDirectory( this, title,
266                  StringUtils.isNullOrEmpty( current ) ? null : new File( current ) );
267          if( result != null )
268             resolved = apply( result );
269 
270          return resolved;
271       }
272 
273       public Object getDescription()
274       {
275          return title;
276       }
277    }
278 }