summaryrefslogtreecommitdiff
path: root/src/plugins/db.core/src/com/motorolamobility/studio/android/db/core/filesystem/FilesystemRootNode.java
blob: b3617610c0d7f2b8df0f68e84b59964e8f43bf77 (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
/*
 * 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.motorolamobility.studio.android.db.core.filesystem;

import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

import com.motorola.studio.android.common.log.StudioLogger;
import com.motorolamobility.studio.android.db.core.DbCoreActivator;
import com.motorolamobility.studio.android.db.core.event.DatabaseModelEvent.EVENT_TYPE;
import com.motorolamobility.studio.android.db.core.event.DatabaseModelEventManager;
import com.motorolamobility.studio.android.db.core.exception.MotodevDbException;
import com.motorolamobility.studio.android.db.core.i18n.DbCoreNLS;
import com.motorolamobility.studio.android.db.core.ui.AbstractTreeNode;
import com.motorolamobility.studio.android.db.core.ui.DbNode;
import com.motorolamobility.studio.android.db.core.ui.IDbMapperNode;
import com.motorolamobility.studio.android.db.core.ui.IDbNode;
import com.motorolamobility.studio.android.db.core.ui.ISaveStateTreeNode;
import com.motorolamobility.studio.android.db.core.ui.ITreeNode;
import com.motorolamobility.studio.android.db.core.ui.view.SaveStateManager;

/**
 * Root node for the filesystem, that allows database mapping/unmapping.
 *
 */
public class FilesystemRootNode extends AbstractTreeNode implements IDbMapperNode,
        ISaveStateTreeNode
{

    private static final String MEMENTO_KEY = "FileSystemNode"; //$NON-NLS-1$

    private static final String MEMENTO_PATH_PREFIX = "MappedPath_"; //$NON-NLS-1$

    private static final String ICON_PATH = "icons/filesystem.png"; //$NON-NLS-1$

    public FilesystemRootNode()
    {
    }

    public FilesystemRootNode(ITreeNode parent)
    {
        super(parent);
    }

    public FilesystemRootNode(String id, String name, ITreeNode parent)
    {
        super(id, name, parent);
    }

    public FilesystemRootNode(String id, String name, ITreeNode parent, ImageDescriptor icon)
    {
        super(id, name, parent, icon);
    }

    @Override
    public void refresh()
    {
        List<ITreeNode> children = getChildren();
        for (ITreeNode child : children)
        {
            if (child instanceof DbNode)
            {
                DbNode dbNode = (DbNode) child;
                if (!dbNode.existsDbFile())
                {
                    //Set error node! Filesystem path does not exist anymore.
                    IStatus error =
                            new Status(IStatus.ERROR, DbCoreActivator.PLUGIN_ID,
                                    DbCoreNLS.FilesystemRootNode_Mapped_Db_Not_Found);
                    dbNode.setNodeStatus(error);
                }
                else
                {
                    dbNode.setNodeStatus(Status.OK_STATUS);
                }
            }
        }
    }

    @Override
    public boolean isLeaf()
    {
        return getChildren().isEmpty();
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.AbstractTreeNode#getIcon()
     */
    @Override
    public ImageDescriptor getIcon()
    {
        return DbCoreActivator.imageDescriptorFromPlugin(DbCoreActivator.PLUGIN_ID, ICON_PATH);
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionFilter#testAttribute(java.lang.Object, java.lang.String, java.lang.String)
     */
    @Override
    public boolean testAttribute(Object target, String name, String value)
    {
        boolean canUnmap = false;
        if (name.equals(IDbMapperNode.UNMAP_ACTIONFILTER_NAME)
                && value.equals(IDbMapperNode.UNMAP_ACTIONFILTER_VALUE))
        {
            if (!getChildren().isEmpty())
            {
                canUnmap = true;
            }
        }
        return canUnmap;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDbMapperNode#map(org.eclipse.core.runtime.IPath)
     */
    public IStatus map(IPath dbFilePath)
    {
        IStatus status =
                new Status(IStatus.OK, DbCoreActivator.PLUGIN_ID,
                        DbCoreNLS.FilesystemRootNode_Map_Successful);
        DbNode dbNode = null;
        try
        {
            dbNode = new DbNode(dbFilePath, this);
            putChild(dbNode);
            DatabaseModelEventManager.getInstance().fireEvent(dbNode, EVENT_TYPE.SELECT);
            saveState(SaveStateManager.getInstance().getPrefNode());
        }
        catch (MotodevDbException e)
        {
            status =
                    new Status(IStatus.ERROR, DbCoreActivator.PLUGIN_ID, DbCoreNLS.bind(
                            DbCoreNLS.FilesystemRootNode_Error_Mapping_Description, dbFilePath));
        }
        return status;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDbMapperNode#unmap(com.motorolamobility.studio.android.db.core.ui.ITreeNode)
     */
    public IStatus unmap(ITreeNode dbNode)
    {
        IStatus status =
                new Status(IStatus.OK, DbCoreActivator.PLUGIN_ID,
                        DbCoreNLS.FilesystemRootNode_Unmapping_Successful);
        if (dbNode instanceof IDbNode)
        {
            IDbNode node = (IDbNode) dbNode;
            if (node.isConnected())
            {
                //node connected => disconnect
                status = node.disconnect();
            }
            //node disconnected => remove the node from the tree
            removeChild(node);
            saveState(SaveStateManager.getInstance().getPrefNode());
        }
        return status;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDbMapperNode#unmap(java.util.List)
     */
    public IStatus unmap(List<ITreeNode> dbNodeList)
    {
        MultiStatus operationsStatus = null;
        if (dbNodeList != null)
        {
            boolean hasError = false;
            IStatus[] children = new IStatus[dbNodeList.size()];
            int i = 0;
            for (ITreeNode treeNode : dbNodeList)
            {
                IStatus operationStatus = unmap(treeNode);
                if (!operationStatus.isOK())
                {
                    hasError = true;
                }
                children[i] = operationStatus;
                i++;
            }
            int code = hasError ? IStatus.ERROR : IStatus.OK;
            String msg =
                    hasError ? DbCoreNLS.FilesystemRootNode_UnmappingList_Error
                            : DbCoreNLS.FilesystemRootNode_UnmappingList_Successful;
            operationsStatus =
                    new MultiStatus(DbCoreActivator.PLUGIN_ID, code, children, msg, null);
            if (operationsStatus.isOK())
            {
                saveState(SaveStateManager.getInstance().getPrefNode());
            }
        }
        return operationsStatus;
    }

    /*
     * (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.ISaveStateTreeNode#saveState(org.eclipse.core.runtime.preferences.IEclipsePreferences)
     */
    public void saveState(IEclipsePreferences preferences)
    {
        Preferences node = preferences.node(MEMENTO_KEY);

        int i = 1;
        List<ITreeNode> children = getChildren();
        for (ITreeNode child : children)
        {
            DbNode dbNode = (DbNode) child;
            node.put(MEMENTO_PATH_PREFIX + i, dbNode.getPath().toString());
            i++;
        }
        try
        {
            preferences.flush();
        }
        catch (BackingStoreException e)
        {
            StudioLogger.debug(this, "Unable to save preferences, flush failed.");
        }

    }

    /*
     * (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.ISaveStateTreeNode#restoreState(org.eclipse.core.runtime.preferences.IEclipsePreferences)
     */
    public void restoreState(IEclipsePreferences preferences)
    {
        try
        {
            if (preferences.nodeExists(MEMENTO_KEY))
            {
                Preferences node = preferences.node(MEMENTO_KEY);
                String[] attributeKeys = node.keys();
                if (attributeKeys.length > 0)
                {
                    for (String key : attributeKeys)
                    {
                        if (key.startsWith(MEMENTO_PATH_PREFIX))
                        {
                            String mappedPath = node.get(key, null);
                            if (mappedPath != null)
                            {
                                map(new Path(mappedPath));
                            }
                        }
                    }
                }
            }
        }
        catch (BackingStoreException e)
        {
            StudioLogger.debug(this, "Unable to restore preferences.");
        }
    }
}