1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.wizards.templates;
17 
18 import static com.android.ide.eclipse.adt.AdtUtils.extractClassName;
19 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectPage.ACTIVITY_NAME_SUFFIX;
20 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectPage.LAYOUT_NAME_PREFIX;
21 
22 import com.android.ide.eclipse.adt.AdtUtils;
23 
24 import freemarker.template.SimpleScalar;
25 import freemarker.template.TemplateMethodModel;
26 import freemarker.template.TemplateModel;
27 import freemarker.template.TemplateModelException;
28 
29 import java.util.List;
30 
31 /**
32  * Method invoked by FreeMarker to convert a layout name into an appropriate
33  * Activity class.
34  */
35 public class FmLayoutToActivityMethod implements TemplateMethodModel {
36     @Override
exec(List args)37     public TemplateModel exec(List args) throws TemplateModelException {
38         if (args.size() != 1) {
39             throw new TemplateModelException("Wrong arguments");
40         }
41 
42         String name = args.get(0).toString();
43 
44         // Strip off the beginning portion of the layout name. The user might be typing
45         // the activity name such that only a portion has been entered so far (e.g.
46         // "MainActivi") and we want to chop off that portion too such that we don't
47         // offer a layout name partially containing the activity suffix (e.g. "main_activi").
48         if (name.startsWith(LAYOUT_NAME_PREFIX)) {
49             name = name.substring(LAYOUT_NAME_PREFIX.length());
50         }
51 
52         name = AdtUtils.underlinesToCamelCase(name);
53         String className = extractClassName(name);
54         if (className == null) {
55             className = "My";
56         }
57         String activityName = className + ACTIVITY_NAME_SUFFIX;
58 
59         return new SimpleScalar(activityName);
60     }
61 }