aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/ColumnDialog.java
blob: be987d49810fa4e92ffebc620dba1c9255c977d7 (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
/*
 * Copyright (C) 2011 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.lint;

import com.android.ide.eclipse.adt.AdtPlugin;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.dialogs.SelectionStatusDialog;

/**
 * Dialog for editing visible columns in the {@link LintList}
 */
class ColumnDialog extends SelectionStatusDialog implements Listener, IStructuredContentProvider {
    private LintColumn[] mColumns;
    private LintColumn[] mSelectedColumns;
    private CheckboxTableViewer mViewer;

        public ColumnDialog(Shell parent, LintColumn[] fields, LintColumn[] selected) {
            super(parent);
            mColumns = fields;
            mSelectedColumns = selected;
            setTitle("Select Visible Columns");
            setHelpAvailable(false);
        }

        @Override
        protected Control createDialogArea(Composite parent) {
            Composite container = new Composite(parent, SWT.NONE);
            container.setLayout(new GridLayout(1, false));
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
            // Wide enough to accommodate the error label
            gridData.widthHint = 500;
            container.setLayoutData(gridData);

            Label lblSelectVisibleColumns = new Label(container, SWT.NONE);
            lblSelectVisibleColumns.setText("Select visible columns:");

            mViewer = CheckboxTableViewer.newCheckList(container,
                    SWT.BORDER | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
            Table table = mViewer.getTable();
            table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
            mViewer.setContentProvider(this);

            mViewer.setInput(mColumns);
            mViewer.setCheckedElements(mSelectedColumns);

            validate();

            return container;
        }

        @Override
        protected void computeResult() {
            Object[] checked = mViewer.getCheckedElements();
            mSelectedColumns = new LintColumn[checked.length];
            for (int i = 0, n = checked.length; i < n; i++) {
                mSelectedColumns[i] = (LintColumn) checked[i];
            }
        }

        public LintColumn[] getSelectedColumns() {
            return mSelectedColumns;
        }

        @Override
        public void handleEvent(Event event) {
            validate();
        }

        private void validate() {
            IStatus status;
            computeResult();

            if (mViewer.getCheckedElements().length <= 1) {
                status = new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
                        "Must selected at least one column");
            } else {
                status = new Status(IStatus.OK, AdtPlugin.PLUGIN_ID, null);
            }
            updateStatus(status);
        }

        // ---- Implements IStructuredContentProvider ----

        @Override
        public void dispose() {
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @Override
        public Object[] getElements(Object inputElement) {
            return mColumns;
        }
    }