下面列出了javax.swing.table.TableModel#getColumnName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* This method will return a string key that uniquely describes a table model and its
* *default* columns (those initially added by the model) so that settings for column state
* can be persisted and retrieved.
*/
private String getPreferenceKey() {
String preferenceKey = table.getPreferenceKey();
if (preferenceKey != null) {
return preferenceKey;
}
TableModel tableModel = table.getModel();
int columnCount = getDefaultColumnCount();
StringBuffer buffer = new StringBuffer();
buffer.append(getTableModelName());
buffer.append(":");
for (int i = 0; i < columnCount; i++) {
String columnName = tableModel.getColumnName(i);
buffer.append(columnName).append(":");
}
return buffer.toString();
}
@Override
public void render(Writer w, int indent) {
TableModel tableModel = textTable.getTableModel();
try (CSVWriter csvWriter = new CSVWriter(w)) {
String[] headers = new String[tableModel.getColumnCount()];
for (int i = 0; i < headers.length; i++) {
headers[i] = tableModel.getColumnName(i);
}
csvWriter.writeNext(headers);
for (int i = 0; i < tableModel.getRowCount(); i++) {
String[] line = new String[tableModel.getColumnCount()];
for (int j = 0; j < tableModel.getColumnCount(); j++) {
Object valueAt = tableModel.getValueAt(i, j);
line[j] = String.valueOf(valueAt);
}
csvWriter.writeNext(line);
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private TableDataRow( final TableModel data, final String valueColumn ) {
if ( data == null ) {
throw new NullPointerException();
}
this.data = data;
this.columnNames = new String[data.getColumnCount()];
this.nameindex = new HashMap<String, Integer>();
this.valueColumn = valueColumn;
for ( int i = 0; i < columnNames.length; i++ ) {
final String name = data.getColumnName( i );
columnNames[i] = name;
nameindex.put( name, IntegerCache.getInteger( i ) );
}
this.currentRow = -1;
}
private void selectMatch(final String sourceLabel) {
waitForSwing();
Window window = waitForWindowByTitleContaining("Test Tool");
assertNotNull(window);
VTMatchTableProvider matchTableProvider =
waitForComponentProvider(VTMatchTableProvider.class);
final GhidraTable matchesTable =
(GhidraTable) getInstanceField("matchesTable", matchTableProvider);
final TableModel model = matchesTable.getModel();
int sourceLabelColumn = -1;
int columnCount = model.getColumnCount();
for (int i = 0; i < columnCount; i++) {
String columnName = model.getColumnName(i);
if (columnName.equals("Source Label")) {
sourceLabelColumn = i;
break;
}
}
Assert.assertNotEquals("Couldn't find Source Label column.", -1, sourceLabelColumn);
final int column = sourceLabelColumn;
runSwing(() -> {
boolean setSelectedRow = false;
int rowCount = model.getRowCount();
for (int i = 0; i < rowCount; i++) {
Object valueObject = model.getValueAt(i, column);
DisplayableLabel currentSourceLabel = (DisplayableLabel) valueObject;
if (currentSourceLabel.getDisplayString().equals(sourceLabel)) {
matchesTable.selectRow(i);
matchesTable.scrollToSelectedRow();
setSelectedRow = true;
break;
}
}
assertEquals("Couldn't select row containing " + sourceLabel + ".", true,
setSelectedRow);
});
waitForSwing();
}
@Override
public void setModel(final TableModel model) {
boolean shouldSort = this.sortable && checkIfSortable(model);
if (shouldSort) {
this.tableSorter = new ExtendedJTableSorterModel(model);
this.tableSorter.setTableHeader(getTableHeader());
super.setModel(this.tableSorter);
} else {
super.setModel(model);
this.tableSorter = null;
}
originalOrder = new String[model.getColumnCount()];
for (int c = 0; c < model.getColumnCount(); c++) {
originalOrder[c] = model.getColumnName(c);
}
// initializing arrays for cell renderer settings
cutOnLineBreaks = new boolean[model.getColumnCount()];
maximalTextLengths = new int[model.getColumnCount()];
Arrays.fill(maximalTextLengths, Integer.MAX_VALUE);
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(final TableModelEvent e) {
int oldLength = cutOnLineBreaks.length;
if (oldLength != model.getColumnCount()) {
cutOnLineBreaks = Arrays.copyOf(cutOnLineBreaks, model.getColumnCount());
maximalTextLengths = Arrays.copyOf(maximalTextLengths, model.getColumnCount());
if (oldLength < cutOnLineBreaks.length) {
Arrays.fill(cutOnLineBreaks, oldLength, cutOnLineBreaks.length, false);
Arrays.fill(maximalTextLengths, oldLength, cutOnLineBreaks.length, Integer.MAX_VALUE);
}
}
}
});
}
/**
* Get underlying column name.
* @param column
* @param tableModel
* @return
*/
private String getColumnName(int column, TableModel tableModel) {
int modelColumnIndex = this.convertColumnIndexToModel(column);
String columnName = null;
if ( tableModel instanceof MyTableModel ) {
columnName = ((MyTableModel)tableModel).getOriginalColumnName(modelColumnIndex);
} else {
columnName = tableModel.getColumnName(modelColumnIndex);
}
return columnName;
}
private static DataRow createReportParameterDataRow( final TableModel burstingData, final int row ) {
final int columnCount = burstingData.getColumnCount();
final String[] columnNames = new String[columnCount];
final Object[] columnValues = new Object[columnCount];
for ( int i = 0; i < columnCount; i++ ) {
columnValues[i] = burstingData.getValueAt( row, i );
columnNames[i] = burstingData.getColumnName( i );
}
return new StaticDataRow( columnNames, columnValues );
}
public DefaultParameterValues( final TableModel parent, final String keyColumn, final String valueColumn ) {
if ( parent == null ) {
throw new NullPointerException();
}
if ( keyColumn == null ) {
throw new NullPointerException();
}
if ( valueColumn == null ) {
throw new NullPointerException();
}
this.keyColumn = keyColumn;
this.valueColumn = valueColumn;
this.parent = parent;
final int colCount = parent.getColumnCount();
keyColumnIdx = -1;
valueColumnIdx = -1;
for ( int i = 0; i < colCount; i++ ) {
final String colName = parent.getColumnName( i );
if ( colName.equals( keyColumn ) ) {
keyColumnIdx = i;
}
if ( colName.equals( valueColumn ) ) {
valueColumnIdx = i;
}
}
if ( keyColumnIdx == -1 ) {
throw new IllegalArgumentException( "Unable to locate the key column in the dataset." );
}
if ( valueColumnIdx == -1 ) {
throw new IllegalArgumentException( "Unable to locate the value column in the dataset." );
}
}
private List<SortConstraint> resolveColumnAliases( final TableModel tableModel, final List<SortConstraint> sort ) {
ArrayList<SortConstraint> result = new ArrayList<SortConstraint>( sort.size() );
for ( final SortConstraint constraint : sort ) {
String field = constraint.getField();
if ( StringUtils.isEmpty( field ) ) {
DebugLog.log( "Sort field is empty" );
continue;
}
if ( field.startsWith( ClassicEngineBoot.INDEX_COLUMN_PREFIX ) ) {
String idx = field.substring( ClassicEngineBoot.INDEX_COLUMN_PREFIX.length() );
try {
int idxParsed = Integer.parseInt( idx );
if ( idxParsed >= 0 && idxParsed < tableModel.getColumnCount() ) {
String columnName = tableModel.getColumnName( idxParsed );
if ( !StringUtils.isEmpty( columnName ) ) {
result.add( new SortConstraint( columnName, constraint.isAscending() ) );
} else {
DebugLog.log( "Resolved column name for column at index " + idxParsed + " is empty." );
}
} else {
logger.debug( "Invalid index on indexed column '" + field + "'" );
}
} catch ( final NumberFormatException iae ) {
logger.debug( "Unable to parse non-decimal index on indexed column '" + field + "'", iae );
}
} else {
result.add( constraint );
}
}
return result;
}
public ReportDataRow( final TableModel reportData ) {
if ( reportData == null ) {
throw new NullPointerException();
}
this.reportData = reportData;
this.cursor = 0;
final int columnCount = reportData.getColumnCount();
this.names = new String[columnCount];
for ( int i = 0; i < columnCount; i++ ) {
this.names[i] = reportData.getColumnName( i );
}
}
protected void initDefaultMetaData( final TableModel model ) {
for ( int i = 0; i < model.getColumnCount(); i++ ) {
final String columnName = model.getColumnName( i );
final Class columnType = model.getColumnClass( i );
final DefaultDataAttributes attributes = new DefaultDataAttributes();
attributes.setMetaAttribute( MetaAttributeNames.Core.NAMESPACE, MetaAttributeNames.Core.NAME,
DefaultConceptQueryMapper.INSTANCE, columnName );
attributes.setMetaAttribute( MetaAttributeNames.Core.NAMESPACE, MetaAttributeNames.Core.TYPE,
DefaultConceptQueryMapper.INSTANCE, columnType );
columnAttributes.add( attributes );
}
tableAttributes = EmptyDataAttributes.INSTANCE;
}
public OfflineTableModel( final TableModel model, final DataAttributeContext dataAttributeContext ) {
columnCount = model.getColumnCount();
columnTypes = new Class[columnCount];
columnNames = new String[columnCount];
columnAttributes = new DefaultDataAttributes[columnCount];
values = new Object[columnCount];
tableAttributes = new DefaultDataAttributes();
for ( int i = 0; i < columnCount; i++ ) {
columnTypes[i] = model.getColumnClass( i );
columnNames[i] = model.getColumnName( i );
columnAttributes[i] = new DefaultDataAttributes();
}
if ( model instanceof MetaTableModel ) {
final MetaTableModel metaTableModel = (MetaTableModel) model;
tableAttributes.merge( metaTableModel.getTableAttributes(), dataAttributeContext );
for ( int i = 0; i < columnCount; i++ ) {
columnAttributes[i].merge( metaTableModel.getColumnAttributes( i ), dataAttributeContext );
}
}
if ( model.getRowCount() > 0 ) {
for ( int i = 0; i < columnCount; i++ ) {
values[i] = model.getValueAt( 0, i );
}
}
}
public TableModelDataRow( final TableModel data ) {
if ( data == null ) {
throw new NullPointerException();
}
this.data = data;
this.columnNames = new String[data.getColumnCount()];
this.nameindex = new HashMap<String, Integer>();
for ( int i = 0; i < columnNames.length; i++ ) {
final String name = data.getColumnName( i );
columnNames[i] = name;
nameindex.put( name, IntegerCache.getInteger( i ) );
}
this.currentRow = -1;
}
public String[] getDataFields() {
final String queryName = getSelectedQuery();
if ( queryName == null ) {
return new String[ 0 ];
}
final DataFactory rawDataFactory = findDataFactoryForQuery( queryName );
if ( rawDataFactory == null ) {
return new String[ 0 ];
}
final DataFactory dataFactory = new CachingDataFactory( rawDataFactory, true );
try {
final ReportDocumentContext activeContext = reportDesignerContext.getActiveContext();
if ( activeContext != null ) {
final MasterReport reportDefinition = activeContext.getContextRoot();
dataFactory.initialize( new DesignTimeDataFactoryContext( reportDefinition ) );
}
final TableModel tableModel =
dataFactory.queryData( queryName, new QueryDataRowWrapper( new ReportParameterValues(), 1, 0 ) );
final int colCount = tableModel.getColumnCount();
final String[] cols = new String[ colCount ];
for ( int i = 0; i < colCount; i++ ) {
cols[ i ] = tableModel.getColumnName( i );
}
return cols;
} catch ( ReportProcessingException aExc ) {
UncaughtExceptionsModel.getInstance().addException( aExc );
return new String[ 0 ];
} finally {
dataFactory.close();
}
}
/**
* Copy data to CSV.
*
* @param csv CSVWriter.
* @param rows Array of rows.
* @param columns List of columns.
* @param includeHeader True to include column headers in the copy.
*/
private void toCSV(final SmartCSVWriter csv, final int[] rows, final List<Integer> columns, final boolean includeHeader) throws IOException {
int auditCounter = 0;
final TableModel dataModel = table.getModel();
int columnModelIdx;
final ArrayList<String> data = new ArrayList<>();
// Add column names.
if (includeHeader) {
final GraphTableModel gtmodel = (GraphTableModel) dataModel;
final GraphElementType elementType = gtmodel.getElementType();
for (final int column : columns) {
columnModelIdx = table.convertColumnIndexToModel(column);
String colName = dataModel.getColumnName(columnModelIdx);
// If this is a transaction table, prefix the column name with the element type so we know which column is which.
if (elementType == GraphElementType.TRANSACTION) {
switch (gtmodel.getAttributeSegment(columnModelIdx).segment) {
case TX:
colName = GraphRecordStoreUtilities.TRANSACTION + colName;
break;
case VX_SRC:
colName = GraphRecordStoreUtilities.SOURCE + colName;
break;
case VX_DST:
colName = GraphRecordStoreUtilities.DESTINATION + colName;
break;
default:
break;
}
}
data.add(colName);
}
csv.writeNext(data.toArray(new String[data.size()]));
}
// Add data.
for (final int row : rows) {
data.clear();
for (final Integer j : columns) {
final int rowModelIndex = table.convertRowIndexToModel(row);
columnModelIdx = table.convertColumnIndexToModel(j);
final Object o = dataModel.getValueAt(rowModelIndex, columnModelIdx);
final String s = o == null ? null : o.toString();
data.add(s);
auditCounter++;
}
csv.writeNext(data.toArray(new String[data.size()]));
}
final int count = auditCounter;
PluginExecution.withPlugin(new SimplePlugin("Copy To Clipboard") {
@Override
protected void execute(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
ConstellationLoggerHelper.copyPropertyBuilder(this, count, ConstellationLoggerHelper.SUCCESS);
}
}).executeLater(null);
}
/**
* Constructs a MonitorPropsDialog class.
* @param title The name of the specified model
* @param table the table to configure
* @param desktop the main desktop.
*/
public TableProperties(String title, JTable table,
MainDesktopPane desktop) {
// Use JInternalFrame constructor
super(title + " Properties", false, true);
// Initialize data members
this.model = table.getColumnModel();
// Prepare content pane
JPanel mainPane = new JPanel();
mainPane.setLayout(new BorderLayout());
mainPane.setBorder(MainDesktopPane.newEmptyBorder());
setContentPane(mainPane);
// Create column pane
JPanel columnPane = new JPanel(new GridLayout(0, 1));
columnPane.setBorder(new MarsPanelBorder());
// Create a checkbox for each column in the model
TableModel dataModel = table.getModel();
for(int i = 0; i < dataModel.getColumnCount(); i++) {
String name = dataModel.getColumnName(i);
JCheckBox column = new JCheckBox(name);
column.setSelected(false);
column.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
columnSelected(event);
}
}
);
columnButtons.add(column);
columnPane.add(column);
}
// Selected if column is visible
Enumeration<?> en = model.getColumns();
while(en.hasMoreElements()) {
int selected = ((TableColumn)en.nextElement()).getModelIndex();
JCheckBox columnButton = columnButtons.get(selected);
columnButton.setSelected(true);
}
mainPane.add(columnPane, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
pack();
desktop.add(this);
// // Add to its own tab pane
// if (desktop.getMainScene() != null)
// desktop.add(this);
// //desktop.getMainScene().getDesktops().get(0).add(this);
// else
// desktop.add(this);
}