aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/draw9patch/Draw9PatchEditor.java
blob: 48ef7c366ce7fe3d158c9c99b0d520b6315838a1 (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
/*
 * Copyright (C) 2013 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.android.ide.eclipse.adt.internal.editors.draw9patch;

import static com.android.SdkConstants.DOT_9PNG;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.draw9patch.graphics.NinePatchedImage;
import com.android.ide.eclipse.adt.internal.editors.draw9patch.ui.ImageViewer;
import com.android.ide.eclipse.adt.internal.editors.draw9patch.ui.MainFrame;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.SaveAsDialog;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.FileEditorInput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Draw9Patch editor part.
 */
public class Draw9PatchEditor extends EditorPart implements ImageViewer.UpdateListener {

    private IProject mProject = null;

    private FileEditorInput mFileEditorInput = null;

    private String mFileName = null;

    private NinePatchedImage mNinePatchedImage = null;

    private MainFrame mMainFrame = null;

    @Override
    public void init(IEditorSite site, IEditorInput input) throws PartInitException {
        setSite(site);
        setInput(input);
        setPartName(input.getName());

        // The contract of init() mentions we need to fail if we can't
        // understand the input.
        if (input instanceof FileEditorInput) {
            // We try to open a file that is part of the current workspace
            mFileEditorInput = (FileEditorInput) input;
            mFileName = mFileEditorInput.getName();
            mProject = mFileEditorInput.getFile().getProject();
        } else {
            throw new PartInitException("Input is not of type FileEditorInput " + //$NON-NLS-1$
                    "nor FileStoreEditorInput: " + //$NON-NLS-1$
                    input == null ? "null" : input.toString()); //$NON-NLS-1$
        }

    }

    @Override
    public boolean isSaveAsAllowed() {
        return true;
    }

    @Override
    public void doSaveAs() {
        IPath relativePath = null;
        if ((relativePath = showSaveAsDialog()) != null) {
            mFileEditorInput = new FileEditorInput(ResourcesPlugin.getWorkspace().getRoot()
                    .getFile(relativePath));
            mFileName = mFileEditorInput.getName();
            setInput(mFileEditorInput);

            doSave(new NullProgressMonitor());
        }
    }

    @Override
    public void doSave(final IProgressMonitor monitor) {
        boolean hasNinePatchExtension = mFileName.endsWith(DOT_9PNG);
        boolean doConvert = false;

        if (!hasNinePatchExtension) {
            String patchedName = NinePatchedImage.getNinePatchedFileName(mFileName);
            doConvert = MessageDialog
                    .openQuestion(AdtPlugin.getDisplay().getActiveShell(),
                            "Warning",
                            String.format(
                                    "The file \"%s\" doesn't seem to be a 9-patch file. \n"
                                            + "Do you want to convert and save as \"%s\" ?",
                                    mFileName, patchedName));

            if (doConvert) {
                IFile destFile = mProject.getFile(NinePatchedImage.getNinePatchedFileName(
                        mFileEditorInput.getFile().getProjectRelativePath().toOSString()));
                if (!destFile.exists()) {
                    mFileEditorInput = new FileEditorInput(destFile);
                    mFileName = mFileEditorInput.getName();
                } else {
                    IPath relativePath = null;
                    if ((relativePath = showSaveAsDialog()) != null) {
                        mFileEditorInput = new FileEditorInput(ResourcesPlugin.getWorkspace()
                                .getRoot().getFile(relativePath));
                        mFileName = mFileEditorInput.getName();
                    } else {
                        doConvert = false;
                    }
                }
            }
        }

        if (hasNinePatchExtension || doConvert) {
            ImageLoader loader = new ImageLoader();
            loader.data = new ImageData[] {
                mNinePatchedImage.getRawImageData()
            };

            IFile file = mFileEditorInput.getFile();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            loader.save(outputStream, SWT.IMAGE_PNG);
            byte[] byteArray = outputStream.toByteArray();

            try {
                if (file.exists()) {
                    file.setContents(new ByteArrayInputStream(byteArray), true, false, monitor);
                } else {
                    file.create(new ByteArrayInputStream(byteArray), true, monitor);
                }

                mNinePatchedImage.clearDirtyFlag();

                AdtPlugin.getDisplay().asyncExec(new Runnable() {
                    @Override
                    public void run() {
                        setPartName(mFileName);
                        firePropertyChange(PROP_DIRTY);
                    }
                });
            } catch (CoreException e) {
                AdtPlugin.log(e, null);
            }
        }
    }

    @Override
    public void createPartControl(Composite parent) {
        mMainFrame = new MainFrame(parent, SWT.NULL);

        ImageViewer imageViewer = mMainFrame.getImageEditorPanel().getImageViewer();
        imageViewer.addUpdateListener(this);

        mNinePatchedImage = imageViewer.loadFile(mFileEditorInput.getPath().toOSString());
        if (mNinePatchedImage.hasNinePatchExtension()) {
            if (!mNinePatchedImage.ensure9Patch() && showConvertMessageBox(mFileName)) {
                // Reload image
                mNinePatchedImage = imageViewer.loadFile(mFileEditorInput.getPath().toOSString());
                mNinePatchedImage.convertToNinePatch();
            }
        } else {
            mNinePatchedImage.convertToNinePatch();
        }

        imageViewer.startDisplay();

        parent.layout();
    }

    @Override
    public void setFocus() {
        mMainFrame.forceFocus();
    }

    @Override
    public boolean isDirty() {
        return mNinePatchedImage.isDirty();
    }

    @Override
    public void update(NinePatchedImage image) {
        if (image.isDirty()) {
            firePropertyChange(PROP_DIRTY);
        }
    }

    private IPath showSaveAsDialog() {
        SaveAsDialog dialog = new SaveAsDialog(AdtPlugin.getDisplay().getActiveShell());

        IFile dest = mProject.getFile(NinePatchedImage.getNinePatchedFileName(
                mFileEditorInput.getFile().getProjectRelativePath().toOSString()));
        dialog.setOriginalFile(dest);

        dialog.create();

        if (dialog.open() == Window.CANCEL) {
            return null;
        }

        return dialog.getResult();
    }

    private static boolean showConvertMessageBox(String fileName) {
        return MessageDialog.openQuestion(
                AdtPlugin.getDisplay().getActiveShell(),
                "Warning",
                String.format("The file \"%s\" doesn't seem to be a 9-patch file. \n"
                        + "Do you want to convert?", fileName));
    }
}