001 /*
002 * Created on Oct 13, 2008
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2008-2010 the original author or authors.
014 */
015 package org.fest.swing.query;
016
017 import javax.swing.JTable;
018 import javax.swing.table.TableColumn;
019
020 import org.fest.swing.annotation.RunsInCurrentThread;
021
022 /**
023 * Understands an action that returns the index of a column in a <code>{@link JTable}</code> whose identifier matches
024 * the given one.
025 * @see JTable#getColumn(Object)
026 * @see TableColumn#getModelIndex()
027 *
028 * @author Alex Ruiz
029 */
030 public final class JTableColumnByIdentifierQuery {
031
032 /**
033 * Returns the index of a column in a <code>{@link JTable}</code> whose identifier matches the given one.
034 * <p>
035 * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
036 * responsible for calling this method from the EDT.
037 * </p>
038 * @param table the given <code>JTable</code>.
039 * @param identifier the column identifier.
040 * @return the index of a column with a matching identifier. Otherwise it returns -1.
041 */
042 @RunsInCurrentThread
043 public static int columnIndexByIdentifier(final JTable table, final Object identifier) {
044 try {
045 TableColumn column = table.getColumn(identifier);
046 return table.convertColumnIndexToView(column.getModelIndex());
047 } catch (IllegalArgumentException e) {
048 return -1;
049 }
050 }
051
052 private JTableColumnByIdentifierQuery() {}
053 }