aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CreateNewConfigJob.java
blob: 44cd0810fe72f6ef62ec721285ee30abf7a5cfa4 (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.android.ide.eclipse.adt.internal.editors.layout.gle2;

import com.android.annotations.NonNull;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.AdtUtils;
import com.android.ide.eclipse.adt.internal.editors.layout.configuration.ConfigurationChooser;
import com.android.ide.eclipse.adt.internal.resources.manager.ResourceManager;
import com.android.resources.ResourceFolderType;
import com.google.common.base.Charsets;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IWorkspaceRoot;
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.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PartInitException;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/** Job which creates a new layout file for a given configuration */
class CreateNewConfigJob extends Job {
    private final GraphicalEditorPart mEditor;
    private final IFile mFromFile;
    private final FolderConfiguration mConfig;

    CreateNewConfigJob(
            @NonNull GraphicalEditorPart editor,
            @NonNull IFile fromFile,
            @NonNull FolderConfiguration config) {
        super("Create Alternate Layout");
        mEditor = editor;
        mFromFile = fromFile;
        mConfig = config;
    }

    @Override
    protected IStatus run(IProgressMonitor monitor) {
        // get the folder name
        String folderName = mConfig.getFolderName(ResourceFolderType.LAYOUT);
        try {
            // look to see if it exists.
            // get the res folder
            IFolder res = (IFolder) mFromFile.getParent().getParent();

            IFolder newParentFolder = res.getFolder(folderName);
            AdtUtils.ensureExists(newParentFolder);
            final IFile file = newParentFolder.getFile(mFromFile.getName());
            if (file.exists()) {
                String message = String.format("File 'res/%1$s/%2$s' already exists!",
                        folderName, mFromFile.getName());
                return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, message);
            }

            // Read current document contents instead of from file: mFromFile.getContents()
            String text = mEditor.getEditorDelegate().getEditor().getStructuredDocument().get();
            ByteArrayInputStream input = new ByteArrayInputStream(text.getBytes(Charsets.UTF_8));
            file.create(input, false, monitor);
            input.close();

            // Ensure that the project resources updates itself to notice the new configuration.
            // In theory, this shouldn't be necessary, but we need to make sure the
            // resource manager knows about this immediately such that the call below
            // to find the best configuration takes the new folder into account.
            ResourceManager resourceManager = ResourceManager.getInstance();
            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            IFolder folder = root.getFolder(newParentFolder.getFullPath());
            resourceManager.getResourceFolder(folder);

            // Switch to the new file
            Display display = mEditor.getConfigurationChooser().getDisplay();
            display.asyncExec(new Runnable() {
                @Override
                public void run() {
                    // The given old layout has been forked into a new layout
                    // for a given configuration. This means that the old layout
                    // is no longer a match for the configuration, which is
                    // probably what it is still showing. We have to modify
                    // its configuration to no longer be an impossible
                    // configuration.
                    ConfigurationChooser chooser = mEditor.getConfigurationChooser();
                    chooser.onAlternateLayoutCreated();

                    // Finally open the new layout
                    try {
                        AdtPlugin.openFile(file, null, false);
                    } catch (PartInitException e) {
                        AdtPlugin.log(e, null);
                    }
                }
            });
        } catch (IOException e2) {
            String message = String.format(
                    "Failed to create File 'res/%1$s/%2$s' : %3$s",
                    folderName, mFromFile.getName(), e2.getMessage());
            AdtPlugin.displayError("Layout Creation", message);

            return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
                    message, e2);
        } catch (CoreException e2) {
            String message = String.format(
                    "Failed to create File 'res/%1$s/%2$s' : %3$s",
                    folderName, mFromFile.getName(), e2.getMessage());
            AdtPlugin.displayError("Layout Creation", message);

            return e2.getStatus();
        }

        return Status.OK_STATUS;
    }
}