/*
* Copyright (C) 2014 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.android.tools.idea.sdk.wizard;
import com.android.annotations.NonNull;
import com.intellij.ui.TableUtil;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.ColumnInfo;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
/**
* Select table in SMW selection step. <br/>
* The table is constructed with a {@link ListSelectionModel#SINGLE_SELECTION} mode.
* Source: PluginTable. <br/>
*/
public class SmwSelectionTable extends JBTable {
public SmwSelectionTable() {
super();
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setShowGrid(false);
setStriped(true);
setTableHeader(null);
}
public SmwSelectionTable(@NonNull SmwSelectionTableModel model) {
super(model);
setModel(model);
}
@Override
public void setModel(@NotNull TableModel model) {
super.setModel(model);
if (!(model instanceof SmwSelectionTableModel)) {
return;
}
SmwSelectionTableModel tableModel = (SmwSelectionTableModel) model;
getColumnModel().setColumnMargin(0);
for (int i = 0; i < tableModel.getColumnCount(); i++) {
TableColumn column = getColumnModel().getColumn(i);
final ColumnInfo columnInfo = tableModel.getColumnInfo(i);
//noinspection unchecked
column.setCellEditor(columnInfo.getEditor(null));
if (columnInfo.getColumnClass() == Boolean.class) {
TableUtil.setupCheckboxColumn(column);
}
}
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setShowGrid(false);
setStriped(true);
setTableHeader(null);
if (tableModel.getColumnCount() > 1) {
setColumnWidth(1, new JCheckBox().getPreferredSize().width + 4);
}
}
public void setColumnWidth(final int columnIndex, final int width) {
TableColumn column = getColumnModel().getColumn(columnIndex);
column.setMinWidth(width);
column.setMaxWidth(width);
}
@Override
public TableCellRenderer getCellRenderer(final int row, final int column) {
TableModel model = getModel();
if (model instanceof SmwSelectionTableModel) {
SmwSelectionTableModel smwModel = (SmwSelectionTableModel)getModel();
final ColumnInfo columnInfo = smwModel.getColumnInfo(column);
//noinspection ConstantConditions,unchecked
return columnInfo.getRenderer(smwModel.getObjectAt(row));
} else {
return super.getCellRenderer(row, column);
}
}
}