1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.lint;
17 
18 import com.android.ide.eclipse.adt.AdtPlugin;
19 
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.viewers.CheckboxTableViewer;
23 import org.eclipse.jface.viewers.IStructuredContentProvider;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Event;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Listener;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.ui.dialogs.SelectionStatusDialog;
36 
37 /**
38  * Dialog for editing visible columns in the {@link LintList}
39  */
40 class ColumnDialog extends SelectionStatusDialog implements Listener, IStructuredContentProvider {
41     private LintColumn[] mColumns;
42     private LintColumn[] mSelectedColumns;
43     private CheckboxTableViewer mViewer;
44 
ColumnDialog(Shell parent, LintColumn[] fields, LintColumn[] selected)45         public ColumnDialog(Shell parent, LintColumn[] fields, LintColumn[] selected) {
46             super(parent);
47             mColumns = fields;
48             mSelectedColumns = selected;
49             setTitle("Select Visible Columns");
50             setHelpAvailable(false);
51         }
52 
53         @Override
createDialogArea(Composite parent)54         protected Control createDialogArea(Composite parent) {
55             Composite container = new Composite(parent, SWT.NONE);
56             container.setLayout(new GridLayout(1, false));
57             GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
58             // Wide enough to accommodate the error label
59             gridData.widthHint = 500;
60             container.setLayoutData(gridData);
61 
62             Label lblSelectVisibleColumns = new Label(container, SWT.NONE);
63             lblSelectVisibleColumns.setText("Select visible columns:");
64 
65             mViewer = CheckboxTableViewer.newCheckList(container,
66                     SWT.BORDER | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
67             Table table = mViewer.getTable();
68             table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
69             mViewer.setContentProvider(this);
70 
71             mViewer.setInput(mColumns);
72             mViewer.setCheckedElements(mSelectedColumns);
73 
74             validate();
75 
76             return container;
77         }
78 
79         @Override
computeResult()80         protected void computeResult() {
81             Object[] checked = mViewer.getCheckedElements();
82             mSelectedColumns = new LintColumn[checked.length];
83             for (int i = 0, n = checked.length; i < n; i++) {
84                 mSelectedColumns[i] = (LintColumn) checked[i];
85             }
86         }
87 
getSelectedColumns()88         public LintColumn[] getSelectedColumns() {
89             return mSelectedColumns;
90         }
91 
92         @Override
handleEvent(Event event)93         public void handleEvent(Event event) {
94             validate();
95         }
96 
validate()97         private void validate() {
98             IStatus status;
99             computeResult();
100 
101             if (mViewer.getCheckedElements().length <= 1) {
102                 status = new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
103                         "Must selected at least one column");
104             } else {
105                 status = new Status(IStatus.OK, AdtPlugin.PLUGIN_ID, null);
106             }
107             updateStatus(status);
108         }
109 
110         // ---- Implements IStructuredContentProvider ----
111 
112         @Override
dispose()113         public void dispose() {
114         }
115 
116         @Override
inputChanged(Viewer viewer, Object oldInput, Object newInput)117         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
118         }
119 
120         @Override
getElements(Object inputElement)121         public Object[] getElements(Object inputElement) {
122             return mColumns;
123         }
124     }