summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/src/com/motorola/studio/android/model/BuildingBlockModel.java
blob: fc5c28964444d9a162dcba256c403bd6e94f7e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorola.studio.android.model;

import java.security.InvalidParameterException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;

import com.motorola.studio.android.codeutils.CodeUtilsActivator;
import com.motorola.studio.android.common.IAndroidConstants;
import com.motorola.studio.android.common.exception.AndroidException;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.AndroidUtils;
import com.motorola.studio.android.common.utilities.EclipseUtils;
import com.motorola.studio.android.model.java.JavaClass;

/**
 * Controller for Building Blocks.
 */
public abstract class BuildingBlockModel implements IWizardModel
{
    private String name = "";

    private final Set<String> intentFilterPermissions = new HashSet<String>();

    /**
     * Constructor for Building class
     * @param superClass the Building block superclass
     */
    public BuildingBlockModel(String superClass)
    {
        if ((superClass == null) || (superClass.length() == 0))
        {
            throw new InvalidParameterException();
        }
        this.superClass = superClass;
    }

    private String superClass;

    private IPackageFragmentRoot packageFragmentRoot;

    private IPackageFragment packageFragment;

    private IStatus nameStatus = new Status(IStatus.OK, CodeUtilsActivator.PLUGIN_ID, null);

    private IStatus packageStatus = new Status(IStatus.OK, CodeUtilsActivator.PLUGIN_ID, null);

    private IStatus packageFragmentRootStatus = new Status(IStatus.OK,
            CodeUtilsActivator.PLUGIN_ID, null);

    private String label;

    private IStatus labelStatus = new Status(IStatus.OK, CodeUtilsActivator.PLUGIN_ID, null);

    private int apiVersion;

    /**
     * Return building block name
     * @return
     */
    public String getName()
    {
        return name;
    }

    /**
     * Return superclass
     * @return
     */
    public String getSuperClass()
    {
        return superClass;
    }

    /**
     * Set building block name
     * @param typeName
     */
    public void setName(String typeName)
    {
        this.name = typeName;
    }

    /**
     * Set superclass 
     * @param superClass
     */
    public void setSuperClass(String superClass)
    {
        this.superClass = superClass;
    }

    /**
     * Change source folder
     * @return
     */
    public IPackageFragmentRoot getPackageFragmentRoot()
    {
        return packageFragmentRoot;
    }

    /**
     * Check if using extended Class
     * @return
     */
    public boolean useExtendedClass()
    {
        return false;
    }

    /**
     * Change source folder
     * @param pack
     */
    public void setPackageFragmentRoot(IPackageFragmentRoot pack)
    {
        this.packageFragmentRoot = pack;
    }

    /**
     * Change package
     * @param packageFragment
     */
    public void setPackageFragment(IPackageFragment packageFragment)
    {
        if ((packageFragmentRoot != null) && (packageFragment != null)
                && !packageFragmentRoot.getJavaProject().equals(packageFragment.getJavaProject()))
        {
            throw new InvalidParameterException();
        }
        else
        {
            this.packageFragment = packageFragment;
        }
    }

    /**
     * Return package
     * @return
     */
    public IPackageFragment getPackageFragment()
    {
        return packageFragment;
    }

    /**
     * Configure source folder and package from workbench selection
     * @param selection
     */
    public void configure(IStructuredSelection selection)
    {
        try
        {
            IPackageFragmentRoot srcFolder = extractPackageFragmentRoot(selection);
            setPackageFragmentRoot(srcFolder);
            IJavaProject javaProject = srcFolder == null ? null : srcFolder.getJavaProject();
            setPackageFragment(extractPackageFragment(selection, javaProject));
            if (javaProject != null)
            {
                apiVersion = AndroidUtils.getApiVersionNumberForProject(javaProject.getProject());
            }

        }
        catch (Exception e)
        {
            StudioLogger.error(BuildingBlockModel.class,
                    "Error configuring building block from selection.", e);
        }
    }

    /**
     * Extract Package from selection
     * @param selection
     * @param javaProject
     * @return
     * @throws CoreException
     */
    private IPackageFragment extractPackageFragment(IStructuredSelection selection,
            IJavaProject javaProject) throws CoreException
    {
        IPackageFragment pack = null;
        Object object = selection.getFirstElement();
        if ((object instanceof IPackageFragment)
                && ((IPackageFragment) object).getJavaProject().equals(javaProject))
        {
            pack = (IPackageFragment) object;
        }
        else if ((object instanceof IJavaElement)
                && ((IJavaElement) object).getJavaProject().equals(javaProject))
        {
            pack =
                    (IPackageFragment) ((IJavaElement) object)
                            .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
            if (pack == null)
            {
                pack =
                        EclipseUtils.getDefaultPackageFragment(((IJavaElement) object)
                                .getJavaProject());
            }
        }
        else if (object instanceof IResource)
        {
            pack =
                    EclipseUtils.getDefaultPackageFragment(javaProject == null ? JavaCore
                            .create(((IResource) object).getProject()) : javaProject);
        }
        else
        {
            if (javaProject != null)
            {
                pack = EclipseUtils.getDefaultPackageFragment(javaProject);
            }
            else
            {
                IJavaProject[] prjs =
                        JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
                if (prjs.length > 0)
                {
                    pack = EclipseUtils.getDefaultPackageFragment(prjs[0]);
                }
            }

        }

        if (pack != null)
        {
            if (!pack.getJavaProject().getProject().hasNature(IAndroidConstants.ANDROID_NATURE))
            {
                pack = extractPackageFragment(new TreeSelection(), javaProject);
            }
        }
        return pack;
    }

    /**
     * Extract source folder from selection.
     * @param selection
     * @return
     * @throws CoreException
     */
    private static IPackageFragmentRoot extractPackageFragmentRoot(IStructuredSelection selection)
            throws CoreException
    {
        IPackageFragmentRoot pack = null;
        Object selectionElement = selection.getFirstElement();

        if (selectionElement instanceof IPackageFragmentRoot)
        {
            pack = (IPackageFragmentRoot) selectionElement;
        }
        else if (selectionElement instanceof IJavaElement)
        {
            pack =
                    (IPackageFragmentRoot) ((IJavaElement) selectionElement)
                            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            if (pack == null)
            {
                IJavaProject element = ((IJavaElement) selectionElement).getJavaProject();

                for (IPackageFragmentRoot root : element.getPackageFragmentRoots())
                {
                    if (root.getResource() != null)
                    {
                        boolean isSrc =
                                (root.getElementType() & IPackageFragmentRoot.K_SOURCE) == IPackageFragmentRoot.K_SOURCE;
                        boolean isGen =
                                root.getElementName().equals(IAndroidConstants.GEN_SRC_FOLDER)
                                        && (root.getParent() instanceof IJavaProject);
                        if (isSrc && !isGen)
                        {
                            pack = root;
                            break;
                        }
                    }
                }
            }
        }
        else if (selectionElement instanceof IResource)
        {
            IJavaProject element = JavaCore.create(((IResource) selectionElement).getProject());

            if (element.isOpen())
            {
                for (IPackageFragmentRoot root : element.getPackageFragmentRoots())
                {
                    if (root.getResource() != null)
                    {
                        boolean isSrc =
                                (root.getElementType() & IPackageFragmentRoot.K_SOURCE) == IPackageFragmentRoot.K_SOURCE;
                        boolean isGen =
                                root.getElementName().equals(IAndroidConstants.GEN_SRC_FOLDER)
                                        && (root.getParent() instanceof IJavaProject);
                        if (isSrc && !isGen)
                        {
                            pack = root;
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            IJavaProject[] allProjects =
                    JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
            if ((allProjects != null) && (allProjects.length > 0))
            {
                for (IJavaProject project : allProjects)
                {
                    if (project.getResource().getProject()
                            .hasNature(IAndroidConstants.ANDROID_NATURE))
                    {
                        IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();

                        if ((roots != null) && (roots.length > 0))
                        {
                            boolean found = false;

                            for (IPackageFragmentRoot root : roots)
                            {
                                boolean isSrc =
                                        (root.getElementType() & IPackageFragmentRoot.K_SOURCE) == IPackageFragmentRoot.K_SOURCE;
                                boolean isGen =
                                        root.getElementName().equals(
                                                IAndroidConstants.GEN_SRC_FOLDER)
                                                && (root.getParent() instanceof IJavaProject);
                                if (isSrc && !isGen)
                                {
                                    found = true;
                                    pack = root;
                                    break;
                                }
                            }

                            if (found)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }

        if (pack != null)
        {
            if (!pack.getJavaProject().getProject().hasNature(IAndroidConstants.ANDROID_NATURE))
            {
                pack = extractPackageFragmentRoot(new TreeSelection());
            }
        }
        return pack;
    }

    /** 
     * Return selected project.
     * @return
     */
    public IProject getProject()
    {
        IProject project = null;
        if (packageFragmentRoot != null)
        {
            project = packageFragmentRoot.getJavaProject().getProject();
        }
        else if (packageFragment != null)
        {
            project = packageFragment.getJavaProject().getProject();
        }
        return project;
    }

    /**
     * Change status from name. Use JDT validation.
     * @param nameStatus
     */
    public void setNameStatus(IStatus nameStatus)
    {
        this.nameStatus = nameStatus;
    }

    /*
     * (non-Javadoc)
     * @see com.motorola.studio.android.model.IWizardModel#getStatus()
     */
    public IStatus getStatus()
    {
        return getMostSevere(new IStatus[]
        {
                packageFragmentRootStatus, nameStatus, packageStatus, labelStatus
        });
    }

    /**
     * Find most severe status.
     * @param status
     * @return
     */
    protected static IStatus getMostSevere(IStatus[] status)
    {
        IStatus max = null;
        for (int i = 0; i < status.length; i++)
        {
            IStatus curr = status[i];
            if (curr.matches(IStatus.ERROR))
            {
                return curr;
            }
            if ((max == null) || (curr.getSeverity() > max.getSeverity()))
            {
                max = curr;
            }
        }

        return max;
    }

    /**
     * Change package status. Use JDT Validation
     * @param packageStatus
     */
    public void setPackageStatus(IStatus packageStatus)
    {
        this.packageStatus = packageStatus;
    }

    /**
     * Change source folder status. Use JDT Validation
     * @param packageFragmentRootStatus
     */
    public void setPackageFragmentRootStatus(IStatus packageFragmentRootStatus)
    {
        this.packageFragmentRootStatus = packageFragmentRootStatus;
    }

    /**
     * Change label.
     * @param label
     */
    public void setLabel(String label)
    {
        this.label = label;
    }

    /**
     * Return Label.
     * @return
     */
    public String getLabel()
    {
        return label;
    }

    /**
     * Change Label Status. Don't uses JDT validation.
     * @param labelStatus
     */
    public void setLabelStatus(IStatus labelStatus)
    {
        this.labelStatus = labelStatus;
    }

    /**
     * Creates the Java Class file
     * 
     * @param javaClass The Java Class model
     * @param monitor The progress monitor
     * @throws JavaModelException
     * @throws AndroidException
     */
    protected void createJavaClassFile(JavaClass javaClass, IProgressMonitor monitor)
            throws JavaModelException, AndroidException
    {
        final String JAVA_EXTENSION = ".java";

        IPackageFragment targetPackage =
                getPackageFragmentRoot().getPackageFragment(getPackageFragment().getElementName());

        if (!targetPackage.exists())
        {
            getPackageFragmentRoot().createPackageFragment(targetPackage.getElementName(), true,
                    monitor);
        }

        targetPackage.createCompilationUnit(getName() + JAVA_EXTENSION, //$NON-NLS-1$
                javaClass.getClassContent().get(), true, monitor);
    }

    /**
     * Return all Filter Permissions as an Array.
     * @return
     */
    public String[] getIntentFilterPermissionsAsArray()
    {
        return intentFilterPermissions.toArray(new String[intentFilterPermissions.size()]);
    }

    /**
     * Returns all intent filter permissions.
     * @return
     */
    public Set<String> getIntentFilterPermissions()
    {
        return intentFilterPermissions;
    }

    /**
     * Adds an intent filter permission to this launcher.
     * @param category
     */
    public void addIntentFilterPermissions(String permission)
    {
        intentFilterPermissions.add(permission);
    }

    /**
     * Remove intent filter permission.
     * @param category
     */
    public void removeIntentFilterPermissions(String permission)
    {
        intentFilterPermissions.remove(permission);
    }

    /**
     * @return the apiVersion
     */
    public int getApiVersion()
    {
        return apiVersion;
    }
}