下面列出了javax.swing.ListSelectionModel#addSelectionInterval ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Set the lead and anchor without affecting selection.
*/
public static void setLeadAnchorWithoutSelection(ListSelectionModel model,
int lead, int anchor) {
if (anchor == -1) {
anchor = lead;
}
if (lead == -1) {
model.setAnchorSelectionIndex(-1);
model.setLeadSelectionIndex(-1);
} else {
if (model.isSelectedIndex(lead)) {
model.addSelectionInterval(lead, lead);
} else {
model.removeSelectionInterval(lead, lead);
}
model.setAnchorSelectionIndex(anchor);
}
}
/**
* Définit la liste d'objets sélectionnés.
*
* @param newSelectedList
* List
* @see #getSelectedList
*/
public void setSelectedList(final List<T> newSelectedList) {
clearSelection();
if (newSelectedList == null || newSelectedList.isEmpty()) {
return;
}
final ListSelectionModel listSelectionModel = getSelectionModel();
final List<T> list = getList();
for (final T object : newSelectedList) {
int rowIndex = list.indexOf(object);
rowIndex = convertRowIndexToView(rowIndex);
if (rowIndex > -1) {
listSelectionModel.addSelectionInterval(rowIndex, rowIndex);
}
}
// scrolle pour afficher la première ligne sélectionnée
final int firstIndex = getSelectionModel().getMinSelectionIndex();
final Rectangle cellRect = getCellRect(firstIndex, 0, true);
scrollRectToVisible(cellRect);
}
public void initialize() throws ReportDataFactoryException {
adjustingToExternalInput = true;
try {
final KeyedComboBoxModel keyedComboBoxModel =
DefaultParameterComponentFactory.createModel( listParameter, parameterContext );
list.setModel( keyedComboBoxModel );
final ListSelectionModel selectionModel = list.getSelectionModel();
final Object value = updateContext.getParameterValue( listParameter.getName() );
final HashSet keylist = getNormalizedSet( value );
selectionModel.setValueIsAdjusting( true );
list.clearSelection();
final int size = keyedComboBoxModel.getSize();
for ( int i = 0; i < size; i++ ) {
final Object key = keyedComboBoxModel.getKeyAt( i );
if ( isSafeMatch( key, keylist ) ) {
selectionModel.addSelectionInterval( i, i );
}
}
selectionModel.setValueIsAdjusting( false );
} finally {
adjustingToExternalInput = false;
}
}
public void invertSelection() {
if(tableSelectionModel != null) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
for(int c=0; c<colCount; c++) {
ListSelectionModel columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
for(int r=0; r<rowCount; r++) {
if(columnSelectionModel.isSelectedIndex(r)) {
columnSelectionModel.removeSelectionInterval(r, r);
}
else {
columnSelectionModel.addSelectionInterval(r, r);
}
}
}
}
}
this.repaint();
}
public void setTo(ListSelectionModel sm) {
sm.clearSelection();
sm.setSelectionMode(selectionMode);
for (int[] itv : intervals) {
sm.addSelectionInterval(itv[0], itv[1]);
}
sm.setAnchorSelectionIndex(anchor);
sm.setLeadSelectionIndex(lead);
}
public final void setSelectedNodes (File[] selectedFiles) {
Set<File> files = new HashSet<File>(Arrays.asList(selectedFiles));
ListSelectionModel selection = table.getSelectionModel();
selection.setValueIsAdjusting(true);
selection.clearSelection();
for (int i = 0; i < table.getRowCount(); ++i) {
T node = tableModel.getNode(table.convertRowIndexToModel(i));
if (files.contains(node.getFile())) {
selection.addSelectionInterval(i, i);
}
}
selection.setValueIsAdjusting(false);
}
private void selectRows(int[] selectedRows, int delta) {
ListSelectionModel listSelectionModel = mappingsTable.getSelectionModel();
listSelectionModel.clearSelection();
for (int selectedRow : selectedRows) {
listSelectionModel.addSelectionInterval(selectedRow + delta, selectedRow + delta);
}
}
public void setSelectedRows(int[] rows, final boolean scrollToSel) {
final ListSelectionModel model = getSelectionModel();
int i = -1;
for (int j : rows) {
i = j;
model.addSelectionInterval(i, i);
}
if (scrollToSel)
scrollRowToVisible(i);
}
/**
* Select destination rows.
*
* @param selectedIndexes the selected indexes
*/
protected void selectDestination(int[] selectedIndexes) {
ListSelectionModel model = destinationTable.getSelectionModel();
model.clearSelection();
for (int index : selectedIndexes) {
model.addSelectionInterval(index, index);
}
}
public static void reSelect(final ListSelectionModel lsm)
{
final int maxSelectionIndex = lsm.getMaxSelectionIndex();
for (int i = 0; i <= maxSelectionIndex; i++)
if (lsm.isSelectedIndex( i ))
{
lsm.removeSelectionInterval( i, i );
lsm.addSelectionInterval( i, i );
}
}
public void setSelectedEntriesByAliases(String... aliases) {
JTable jtKeyStore = getActiveKeyStoreTable();
jtKeyStore.requestFocusInWindow();
ListSelectionModel selectionModel = jtKeyStore.getSelectionModel();
selectionModel.clearSelection();
Set<String> aliasesToSelect = new HashSet<>(Arrays.asList(aliases));
for (int i = 0; i < jtKeyStore.getRowCount(); i++) {
if (aliasesToSelect.contains(jtKeyStore.getValueAt(i, 3))) {
selectionModel.addSelectionInterval(i, i);
}
}
}
@Override
public void selectAll() {
if(tableSelectionModel != null) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
for(int c=0; c<colCount; c++) {
ListSelectionModel columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
columnSelectionModel.addSelectionInterval(0, rowCount);
}
}
}
this.repaint();
}
public void selectColumn(int column) {
int rowCount = this.getRowCount();
int colCount = this.getColumnCount();
if(tableSelectionModel != null && column < colCount) {
ListSelectionModel columnSelectionModel = tableSelectionModel.getListSelectionModelAt(column);
if(columnSelectionModel != null) {
columnSelectionModel.addSelectionInterval(0, rowCount-1);
}
}
this.repaint();
}
public void selectColumns() {
if(tableSelectionModel != null) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
for(int c=0; c<colCount; c++) {
ListSelectionModel columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
if(!columnSelectionModel.isSelectionEmpty()) {
columnSelectionModel.addSelectionInterval(0, rowCount-1);
}
}
}
}
this.repaint();
}
public void selectRow(int row) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
if(tableSelectionModel != null && row < rowCount) {
ListSelectionModel columnSelectionModel = null;
for(int c=0; c<colCount; c++) {
columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
columnSelectionModel.addSelectionInterval(row, row);
}
}
}
this.repaint();
}
public void selectRows() {
if(tableSelectionModel != null) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
ListSelectionModel columnSelectionModel = null;
for(int r=0; r<rowCount; r++) {
boolean selectRow = false;
for(int c=0; c<colCount; c++) {
columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
if(columnSelectionModel.isSelectedIndex(r)) {
selectRow = true;
break;
}
}
}
if(selectRow) {
for(int c=0; c<colCount; c++) {
columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
columnSelectionModel.addSelectionInterval(r, r);
}
}
}
}
}
this.repaint();
}
public void selectCell(int c, int r) {
//System.out.println("Selecting c,r == "+c+","+r);
if(tableSelectionModel != null) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
if(c >= 0 && c < colCount && r >= 0 && r < rowCount) {
ListSelectionModel columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
//System.out.println("Selecting c,r == "+c+","+r);
columnSelectionModel.addSelectionInterval(r, r);
}
}
}
this.repaint();
}
public void selectArea(int c1, int r1, int c2, int r2) {
if(tableSelectionModel != null) {
int colCount = this.getColumnCount();
int rowCount = this.getRowCount();
c1 = Math.min(c1, colCount);
c2 = Math.min(c2, colCount);
r1 = Math.min(r1, rowCount);
r2 = Math.min(r2, rowCount);
int cs = c1;
int ce = c2;
if(c2 < c1) {
cs = c2;
ce = c1;
}
int rs = r1;
int re = r2;
if(r2 < r1) {
rs = r2;
re = r1;
}
for(int c=cs; c<=ce; c++) {
ListSelectionModel columnSelectionModel = tableSelectionModel.getListSelectionModelAt(c);
if(columnSelectionModel != null) {
columnSelectionModel.addSelectionInterval(rs, re);
}
}
}
this.repaint();
}
private void addFolders( File files[] ) {
int[] si = rootsList.getSelectedRows();
int lastIndex = si == null || si.length == 0 ? -1 : si[si.length - 1];
ListSelectionModel selectionModel = this.rootsList.getSelectionModel();
selectionModel.clearSelection();
Set<File> rootsFromOtherProjects = new HashSet<File>();
Set<File> rootsFromRelatedSourceRoots = new HashSet<File>();
String type = RootsAccessor.getInstance().getType(sourceRoots);
boolean isModule = JavaProjectConstants.SOURCES_TYPE_MODULES.equals(type);
out: for( int i = 0; i < files.length; i++ ) {
File normalizedFile = FileUtil.normalizeFile(files[i]);
Project p;
if (ownedFolders.contains(normalizedFile)) {
Vector dataVector = rootsModel.getDataVector();
for (int j=0; j<dataVector.size();j++) {
//Sequential search in this minor case is faster than update of positions during each modification
File f = (File )((Vector)dataVector.elementAt(j)).elementAt(0);
if (f.equals(normalizedFile)) {
selectionModel.addSelectionInterval(j,j);
}
}
}
else if (this.relatedEditMediator != null && this.relatedEditMediator.ownedFolders.contains(normalizedFile)) {
rootsFromRelatedSourceRoots.add (normalizedFile);
continue;
}
if ((p=FileOwnerQuery.getOwner(Utilities.toURI(normalizedFile)))!=null && !p.getProjectDirectory().equals(project.getProjectDirectory())) {
final Sources sources = p.getLookup().lookup(Sources.class);
if (sources == null) {
rootsFromOtherProjects.add (normalizedFile);
continue;
}
final SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
final SourceGroup[] javaGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
final SourceGroup[] groups = new SourceGroup [sourceGroups.length + javaGroups.length];
System.arraycopy(sourceGroups,0,groups,0,sourceGroups.length);
System.arraycopy(javaGroups,0,groups,sourceGroups.length,javaGroups.length);
final FileObject projectDirectory = p.getProjectDirectory();
final FileObject fileObject = FileUtil.toFileObject(normalizedFile);
if (projectDirectory == null || fileObject == null) {
rootsFromOtherProjects.add (normalizedFile);
continue;
}
for (int j=0; j<groups.length; j++) {
final FileObject sgRoot = groups[j].getRootFolder();
if (fileObject.equals(sgRoot)) {
rootsFromOtherProjects.add (normalizedFile);
continue out;
}
if (!projectDirectory.equals(sgRoot) && FileUtil.isParentOf(sgRoot, fileObject)) {
rootsFromOtherProjects.add (normalizedFile);
continue out;
}
}
}
int current = lastIndex + 1 + i;
rootsModel.insertRow( current, new Object[] {normalizedFile, isModule
? ((ModuleRoots)sourceRoots).createInitialPath()
: sourceRoots.createInitialDisplayName(normalizedFile)});
selectionModel.addSelectionInterval(current,current);
this.ownedFolders.add (normalizedFile);
}
if (rootsFromOtherProjects.size() > 0 || rootsFromRelatedSourceRoots.size() > 0) {
rootsFromOtherProjects.addAll(rootsFromRelatedSourceRoots);
showIllegalRootsDialog (rootsFromOtherProjects);
}
// fireActionPerformed();
}
/**
* Forwards the request to the ListSelectionModel
* at the specified column.
*/
public void addSelection(int row, int column) {
ListSelectionModel lsm = getListSelectionModelAt(column);
lsm.addSelectionInterval(row, row);
}