summaryrefslogtreecommitdiff
path: root/src/plugins/db.core/src/com/motorolamobility/studio/android/db/core/ui/DbNode.java
blob: 5faa9c08093fa0186dde3d2c467152675edf0881 (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
/*
 * 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.ui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.datatools.connectivity.ConnectionProfileException;
import org.eclipse.datatools.modelbase.sql.tables.Table;
import org.eclipse.datatools.sqltools.result.ResultsViewAPI;
import org.eclipse.datatools.sqltools.result.core.IResultManagerListener;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;

import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.EclipseUtils;
import com.motorolamobility.studio.android.db.core.DbCoreActivator;
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.model.DbModel;
import com.motorolamobility.studio.android.db.core.model.TableModel;

/**
 * This class represents a database node on the DB Explorer tree view
 */
public class DbNode extends AbstractTreeNode implements IDbNode
{
    /**
     * Properties name space.
     */
    public static final String PROP_NAMESPACE = "com.motorolamobility.studio.android.db.core"; //$NON-NLS-1$

    /**
     * Property value used to check if the database is disconnected.
     */
    public static final String PROP_VALUE_DB_DISCONNECTED =
            "com.motorolamobility.studio.android.db.core.databaseDisconnected"; //$NON-NLS-1$

    /**
     * Property value used to check if the database is connected.
     */
    public static final String PROP_VALUE_DB_CONNECTED =
            "com.motorolamobility.studio.android.db.core.databaseConnected"; //$NON-NLS-1$

    /**
     * Property name used to check database connection status (connected/disconnected).
     */
    public static final String PROP_NAME_DB_CONNECTION =
            "com.motorolamobility.studio.android.db.core.databaseConnection"; //$NON-NLS-1$

    /**
     * Property name used to check database connection status (connected/disconnected).
     */
    public static final String PROP_NAME_DB_NODE_TYPE =
            "com.motorolamobility.studio.android.db.core.IDbNodeType"; //$NON-NLS-1$

    /**
     * Property value used to check if the database is connected.
     */
    public static final String PROP_VALUE_DB_NODE_IS_EXT_STORAGE =
            "com.motorolamobility.studio.android.db.core.isExternalStorage"; //$NON-NLS-1$

    private class ResultManagerAdapter extends AbstractDbResultManagerAdapter
    {
        /* (non-Javadoc)
         * @see com.motorolamobility.studio.android.db.core.ui.AbstractDbResultManagerAdapter#statementExecuted(java.lang.String, java.lang.String)
         */
        @Override
        public void statementExecuted(String profileName, String sqlStatement)
        {
            if (model.getProfileName().equals(profileName))
            {
                //Ignore group execution and read access to table(Select). We'll handle only db changes. 
                if ((!sqlStatement.equals("Group Execution")) //$NON-NLS-1$
                        && (sqlStatement.indexOf("select") != 0) && (!sqlStatement.equals(""))) //$NON-NLS-1$ //$NON-NLS-2$
                {
                    if (sqlStatement.startsWith("drop table") //$NON-NLS-1$
                            || sqlStatement.startsWith("create table")) //$NON-NLS-1$
                    {
                        //A new table has been created let's refresh the dbNode in order to get a update copy
                        refreshAsync();
                    }
                    else if (sqlStatement.startsWith("alter table")) //$NON-NLS-1$
                    {
                        if (!sqlStatement.contains("rename")) //$NON-NLS-1$
                        {
                            //Table has been altered but not renamed, let's refresh the table node, loading the possible changes 
                            String tableName = sqlStatement.replace("alter table ", ""); //$NON-NLS-1$ //$NON-NLS-2$
                            tableName.substring(0, tableName.indexOf(" ")); //$NON-NLS-1$
                            List<ITreeNode> children = getChildren();
                            for (ITreeNode child : children)
                            {
                                if (child.getName().equalsIgnoreCase(tableName))
                                {
                                    child.refreshAsync();
                                    break;
                                }
                            }
                        }
                        else
                        {
                            //Since a name has been renamed a refresh on this db node will force to use the latest information.
                            refreshAsync();
                        }
                    }
                }
            }
        }
    };

    public static final String ICON_PATH = "icons/obj16/dbplate.gif"; //$NON-NLS-1$

    protected DbModel model;

    private IResultManagerListener resultManagerListener;

    protected boolean forceCloseEditors;

    @SuppressWarnings("unused")
    private DbNode()
    {
        //Forcing user to use a proper constructor (with a parent)
    }

    protected DbNode(ITreeNode parent)
    {
        super(parent);
    }

    /**
     * Creates a new DBNode by using a existent db file.
     * @param dbFilePath The SQLite database File
     * @param parent The parent of the new node.
     * @throws MotodevDbException
     */
    public DbNode(IPath dbFilePath, ITreeNode parent) throws MotodevDbException
    {
        this(parent);
        init(dbFilePath);
        model = new DbModel(dbFilePath);
    }

    /**
     * Creates a new DBNode by creating a new SQLite3 database file if requested.
     * @param dbPath The SQLite database File
     * @param parent The parent of the new node.
     * @param create set this flag to true if you want to create a new db file, if the flag is false the behavior is the same as the constructor DbNode(Path dbFilePath, AbstractTreeNode parent)
     * @throws MotodevDbException
     */
    public DbNode(IPath dbPath, ITreeNode parent, boolean create) throws MotodevDbException
    {
        this(parent);
        init(dbPath);
        try
        {
            model = new DbModel(dbPath, create);
        }
        catch (MotodevDbException e)
        {
            throw new MotodevDbException("Could not create DBNode", e); //$NON-NLS-1$
        }
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDBNode#connect()
     */
    public IStatus connect()
    {
        IStatus status = model.connect();
        if (status.isOK())
        {
            if (resultManagerListener == null)
            {
                resultManagerListener = new ResultManagerAdapter();
                ResultsViewAPI.getInstance().getResultManager()
                        .addResultManagerListener(resultManagerListener);
            }
        }

        setNodeStatus(status);
        return status;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDBNode#disconnect()
     */
    public IStatus disconnect()
    {
        IStatus status = closeAssociatedEditors();
        if (status.isOK())
        {
            status = model.disconnect();
            if (status.isOK())
            {
                if (resultManagerListener != null)
                {
                    ResultsViewAPI.getInstance().getResultManager()
                            .removeResultManagerListener(resultManagerListener);
                    resultManagerListener = null;
                }
                clear();
            }
            if (status.getSeverity() != IStatus.CANCEL)
            {
                setNodeStatus(status);
            }
        }

        if (status.getSeverity() != IStatus.CANCEL)
        {
            setNodeStatus(status);
        }

        return status;
    }

    public IStatus createTables(List<TableModel> tables)
    {
        IStatus status = Status.OK_STATUS;
        List<ITreeNode> tableNodes = new ArrayList<ITreeNode>(tables.size());
        for (TableModel table : tables)
        {
            status = model.createTable(table);
            if (status.isOK())
            {
                TableNode tableNode = new TableNode(getTable(table.getName()), model, this);
                tableNodes.add(tableNode);
            }
            else
            {
                break;
            }
        }

        if (status.isOK())
        {
            putChildren(tableNodes);
        }

        return status;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDBNode#createTable(java.lang.String, java.lang.String)
     */
    public IStatus createTable(TableModel table)
    {
        IStatus status = model.createTable(table);
        if (status.isOK())
        {
            TableNode tableNode = new TableNode(getTable(table.getName()), model, this);
            putChild(tableNode);
        }

        return status;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDBNode#deleteTable(java.lang.String)
     */
    public IStatus deleteTable(ITableNode tableNode)
    {
        IStatus status = model.deleteTable(tableNode.getName());
        if (status.isOK())
        {
            removeChild(tableNode);
        }

        return status;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDBNode#getTables()
     */
    public List<Table> getTables()
    {
        return model.getTables();
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.AbstractTreeNode#refresh()
     */
    @Override
    public void refresh()
    {
        if (!model.isConnected())
        {
            connect();
        }
        clear();
        List<Table> tables = getTables();
        List<ITreeNode> tableNodes = new ArrayList<ITreeNode>(tables.size());
        for (Table table : tables)
        {
            TableNode tableNode = new TableNode(table, model, this);
            tableNodes.add(tableNode);
        }
        putChildren(tableNodes);
    }

    protected IStatus closeAssociatedEditors(final boolean quiet, final boolean forceClose)
    {
        Set<IEditorPart> associatedEditors = getAssociatedEditors();
        IStatus status = Status.OK_STATUS;
        if (!associatedEditors.isEmpty())
        {
            final boolean[] success = new boolean[]
            {
                true
            };
            for (final IEditorPart editor : associatedEditors)
            {
                final IWorkbenchPage page = EclipseUtils.getPageForEditor(editor);
                Display.getDefault().syncExec(new Runnable()
                {
                    public void run()
                    {
                        if (!quiet)
                        {
                            page.bringToTop(editor);
                            if (!forceClose)
                            {
                                //Use the default Eclipse behavior
                                if (!page.closeEditor(editor, true))
                                {
                                    success[0] = false;
                                }
                            }
                            else
                            {
                                if (editor.isDirty())
                                {
                                    //Use our dialog, because the operation can't be cancelled.
                                    boolean shallSave =
                                            EclipseUtils.showQuestionDialog(
                                                    DbCoreNLS.DbNode_Close_Editor_Msg_Title,
                                                    NLS.bind(DbCoreNLS.DbNode_Close_Editor_Msg,
                                                            getName()));
                                    if (shallSave)
                                    {
                                        editor.doSave(new NullProgressMonitor());
                                    }
                                }
                                page.closeEditor(editor, false);
                            }
                        }
                        else
                        {
                            page.closeEditor(editor, false);
                        }
                    }
                });

                if (!success[0])
                {
                    break;
                }
            }

            if (!success[0])
            {
                status =
                        new Status(IStatus.CANCEL, DbCoreActivator.PLUGIN_ID,
                                DbCoreNLS.DbNode_Canceled_Save_Operation);
            }
        }
        return status;
    }

    /**
     * @param status
     * @return
     */
    protected IStatus closeAssociatedEditors()
    {
        return closeAssociatedEditors(false, forceCloseEditors);
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDBNode#getTable(java.lang.String)
     */
    public Table getTable(String tableName)
    {
        return model.getTable(tableName);
    }

    private void init(IPath dbFilePath)
    {
        String dbFileName = dbFilePath.lastSegment();
        String id = dbFilePath.toFile().getParent() + "." + dbFileName; //$NON-NLS-1$

        setId(id);
        setName(dbFileName);
        ImageDescriptor icon =
                DbCoreActivator.imageDescriptorFromPlugin(DbCoreActivator.PLUGIN_ID, ICON_PATH);
        setIcon(icon);
        setToolTip(dbFilePath);
    }

    /*
     * Sets the tool tip for the node given its path.
     */
    private void setToolTip(IPath dbPath)
    {
        //for mapped nodes (i.e., children of IDbMapperNode) the tool tip is its path
        if (getParent() instanceof IDbMapperNode)
        {
            setTooltip(NLS.bind(DbCoreNLS.DbNode_Tooltip_Prefix, dbPath.toString()));
        }
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.AbstractTreeNode#isLeaf()
     */
    @Override
    public boolean isLeaf()
    {
        return false;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.IDbNode#deleteDb()
     */
    public IStatus deleteDb()
    {
        closeAssociatedEditors(true, forceCloseEditors);
        disconnect();
        return model.deleteDb();
    }

    /* (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 result = false;

        //check if 'name' is a specific DbNode property 
        if (name.equals(PROP_NAME_DB_CONNECTION)
                || PROP_NAME_DB_CONNECTION.equals(PROP_NAMESPACE + '.' + name))
        {
            if (value.equals(PROP_VALUE_DB_CONNECTED))
            {
                result = isConnected();
            }
            else if (value.equals(PROP_VALUE_DB_DISCONNECTED))
            {
                result = !isConnected();
            }
        }
        else if (name.equals(PROP_NAME_DB_NODE_TYPE))
        {
            if (value.equals(PROP_VALUE_DB_NODE_IS_EXT_STORAGE))
            {
                result = (getParent() instanceof IDbMapperNode);
            }
        }
        else
        {
            //check if 'name' is a generic ITreeNode property
            result = super.testAttribute(target, name, value);
        }

        return result;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.AbstractTreeNode#getIcon()
     */
    @Override
    public ImageDescriptor getIcon()
    {
        return getSpecificIcon("org.eclipse.datatools.connectivity.sqm.core.ui", //$NON-NLS-1$
                "icons/database.gif"); //$NON-NLS-1$
    }

    public boolean isConnected()
    {
        return model != null ? model.isConnected() : false;
    }

    /* (non-Javadoc)
     * @see com.motorolamobility.studio.android.db.core.ui.AbstractTreeNode#clean()
     */
    @Override
    public void cleanUp()
    {
        setForceCloseEditors(true);
        disconnect();
        if (model != null)
        {
            try
            {
                model.cleanModel();
            }
            catch (ConnectionProfileException e)
            {
                StudioLogger.debug(this, "Unable to cleanup db model.");
            }
        }
        super.cleanUp();
    }

    /**
     * Retrieves the open editors that is used to edit the given profile, if any
     * 
     * @param profile
     *            The profile that owns the requested editor
     * @return The open dirty editor for the given profile, or <code>null</code>
     *         if there is no editor in this condition
     */
    public Set<IEditorPart> getAssociatedEditors()
    {
        return model != null ? model.getAssociatedEditors() : new HashSet<IEditorPart>(0);
    }

    /**
     * Checks if the db file exists in filesystem
     * @return
     */
    public boolean existsDbFile()
    {
        return model != null ? model.getDbPath().toFile().exists() : false;
    }

    public IPath getPath()
    {
        return model != null ? model.getDbPath() : null;
    }

    /**
     * @param forceCloseEditors the forceCloseEditors to set
     */
    protected void setForceCloseEditors(boolean forceCloseEditors)
    {
        this.forceCloseEditors = forceCloseEditors;
    }
}