summaryrefslogtreecommitdiff
path: root/src/plugins/db.core/src/com/motorolamobility/studio/android/db/core/command/TableCreateHandler.java
blob: 4d0f997b8ce6107adfab5b1ec8088200920de334 (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
/*
 * 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.command;

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

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.datatools.modelbase.sql.tables.Table;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.motorolamobility.studio.android.db.core.DbCoreActivator;
import com.motorolamobility.studio.android.db.core.i18n.DbCoreNLS;
import com.motorolamobility.studio.android.db.core.model.TableModel;
import com.motorolamobility.studio.android.db.core.ui.ITreeNode;
import com.motorolamobility.studio.android.db.core.ui.action.ITableCreatorNode;
import com.motorolamobility.studio.android.db.core.ui.wizards.CreateTableWizard;

public class TableCreateHandler extends AbstractHandler
{
    private ITableCreatorNode tableCreatorNode = null;

    public TableCreateHandler()
    {
    }

    public TableCreateHandler(ITableCreatorNode tableCreatorNode)
    {
        this.tableCreatorNode = tableCreatorNode;
    }

    /* (non-Javadoc)
     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException
    {
        if (tableCreatorNode == null)
        {
            tableCreatorNode = getSelectedItem();
        }

        //tableCreatorNode may be null if the action come from toolbar
        //and the selected item is not an ITableCreatorNode
        if (tableCreatorNode != null)
        {
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

            boolean tableAdded = false;

            // loop used to validate the new table name. If it already exists 
            // tell the user and open the table wizard again.
            while (!tableAdded)
            {
                //repeat while table not added and dialog not cancelled 
                CreateTableWizard createTableWizard = new CreateTableWizard();
                Set<String> notAllowedNames = getNotAllowedNames(tableCreatorNode.getTables());
                createTableWizard.setNotAllowedNames(notAllowedNames);
                WizardDialog dialog = new WizardDialog(shell, createTableWizard);
                dialog.open();

                if (dialog.getReturnCode() == Dialog.OK)
                {
                    TableModel newTable = createTableWizard.getTable();
                    if (newTable != null)
                    {
                        boolean tableNameAlreadyExists = false;
                        for (Table table : tableCreatorNode.getTables())
                        {
                            if (table.getName().equalsIgnoreCase(newTable.getName()))
                            {
                                tableNameAlreadyExists = true;
                                break;
                            }
                        }
                        if (!tableNameAlreadyExists)
                        {
                            tableCreatorNode.createTable(newTable);
                            tableCreatorNode = null; //clear selected node to force getSelectedItem to be called when calling via toolbar
                            tableAdded = true;
                        }
                        else
                        {
                            //notify error that table already exists
                            MessageDialog
                                    .openError(
                                            PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                                                    .getShell(),
                                            DbCoreNLS.CreateDatabaseWizardPage_Table_Already_Exists_Title,
                                            NLS.bind(
                                                    DbCoreNLS.CreateDatabaseWizardPage_Table_Already_Exists_Msg,
                                                    newTable.getName()));
                        }
                    }
                }
                else
                {
                    //exit the loop if the user cancel dialog
                    break;
                }
            }

        }

        return null;
    }

    private ITableCreatorNode getSelectedItem()
    {
        ITableCreatorNode selectedNode = null;
        ITreeNode selectedItem =
                DbCoreActivator.getMOTODEVDatabaseExplorerView().getSelectedItemOnTree();

        if (selectedItem instanceof ITableCreatorNode)
        {
            selectedNode = (ITableCreatorNode) selectedItem;
        }

        return selectedNode;
    }

    private Set<String> getNotAllowedNames(List<Table> list)
    {

        Set<String> names = new HashSet<String>();

        for (Table table : list)
        {
            names.add(table.getName().toUpperCase());
        }
        return names;
    }
}