From 8968c45d844737a24e458f6242ad65a1746134e0 Mon Sep 17 00:00:00 2001 From: Mikhail Khodjaiants Date: Mon, 20 Oct 2003 22:40:05 +0000 Subject: [PATCH] Added the "Search subfolders" option for directory search locations. --- debug/org.eclipse.cdt.debug.core/ChangeLog | 6 + .../IDirectorySourceLocation.java | 3 + .../sourcelookup/SourceLocationFactory.java | 9 +- .../CDirectorySourceLocation.java | 175 +++++++++++++++--- debug/org.eclipse.cdt.debug.ui/ChangeLog | 5 + .../AddDirectorySourceLocationBlock.java | 19 +- .../ui/sourcelookup/SourceLookupBlock.java | 52 ++++-- 7 files changed, 221 insertions(+), 48 deletions(-) diff --git a/debug/org.eclipse.cdt.debug.core/ChangeLog b/debug/org.eclipse.cdt.debug.core/ChangeLog index 148d3e60fce..a1074340a1b 100644 --- a/debug/org.eclipse.cdt.debug.core/ChangeLog +++ b/debug/org.eclipse.cdt.debug.core/ChangeLog @@ -1,3 +1,9 @@ +2003-10-20 Mikhail Khodjaiants + Core support of the "Search subfolders" option for directory source locations. + * IDirectorySourceLocation.java + * SourceLocationFactory.java + * CDirectorySourceLocation.java + 2003-10-20 Mikhail Khodjaiants Do not interrupt the initialization of all additional source locations if the initialization of one fails. diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/IDirectorySourceLocation.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/IDirectorySourceLocation.java index 5b12c6c2304..1b4ff0c0e54 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/IDirectorySourceLocation.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/IDirectorySourceLocation.java @@ -16,5 +16,8 @@ import org.eclipse.core.runtime.IPath; public interface IDirectorySourceLocation extends ICSourceLocation { IPath getDirectory(); + IPath getAssociation(); + + boolean searchSubfolders(); } diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/SourceLocationFactory.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/SourceLocationFactory.java index 84784e55aea..c09a581900e 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/SourceLocationFactory.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/SourceLocationFactory.java @@ -28,13 +28,8 @@ public class SourceLocationFactory return new CProjectSourceLocation( project, generated ); } - public static IDirectorySourceLocation createDirectorySourceLocation( IPath directory ) + public static IDirectorySourceLocation createDirectorySourceLocation( IPath directory, IPath association, boolean searchSubfolders ) { - return new CDirectorySourceLocation( directory ); - } - - public static IDirectorySourceLocation createDirectorySourceLocation( IPath directory, IPath association ) - { - return new CDirectorySourceLocation( directory, association ); + return new CDirectorySourceLocation( directory, association, searchSubfolders ); } } diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CDirectorySourceLocation.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CDirectorySourceLocation.java index 16fa6c2bafc..d539140c551 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CDirectorySourceLocation.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/sourcelookup/CDirectorySourceLocation.java @@ -6,10 +6,14 @@ package org.eclipse.cdt.debug.internal.core.sourcelookup; import java.io.File; +import java.io.FileFilter; import java.io.IOException; import java.io.StringReader; import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedList; +import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -46,6 +50,7 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation private static final String ELEMENT_NAME = "cDirectorySourceLocation"; private static final String ATTR_DIRECTORY = "directory"; private static final String ATTR_ASSOCIATION = "association"; + private static final String ATTR_SEARCH_SUBFOLDERS = "searchSubfolders"; /** * The root directory of this source location @@ -59,6 +64,10 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation private boolean fSearchForDuplicateFiles = false; + private boolean fSearchSubfolders = false; + + private File[] fFolders = null; + /** * Constructor for CDirectorySourceLocation. */ @@ -69,18 +78,11 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation /** * Constructor for CDirectorySourceLocation. */ - public CDirectorySourceLocation( IPath directory ) - { - setDirectory( directory ); - } - - /** - * Constructor for CDirectorySourceLocation. - */ - public CDirectorySourceLocation( IPath directory, IPath association ) + public CDirectorySourceLocation( IPath directory, IPath association, boolean searchSubfolders ) { setDirectory( directory ); setAssociation( association ); + setSearchSubfolders( searchSubfolders ); } /* (non-Javadoc) @@ -159,12 +161,44 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation } private Object findFileByAbsolutePath( String name ) + { + File file = new File( name ); + if ( !file.isAbsolute() ) + return null; + File[] folders = getFolders(); + if ( folders != null ) + { + LinkedList list = new LinkedList(); + for ( int i = 0; i < folders.length; ++i ) + { + Object result = findFileByAbsolutePath( folders[i], name ); + if ( result instanceof List ) + { + if ( searchForDuplicateFiles() ) + list.addAll( (List)result ); + else + return list.getFirst(); + } + else if ( result != null ) + { + if ( searchForDuplicateFiles() ) + list.add( result ); + else + return result; + } + } + return list; + } + return null; + } + + private Object findFileByAbsolutePath( File folder, String name ) { File file = new File( name ); if ( !file.isAbsolute() ) return null; IPath filePath = new Path( name ); - IPath path = getDirectory(); + IPath path = new Path( folder.getAbsolutePath() ); IPath association = getAssociation(); if ( isPrefix( path, filePath ) ) { @@ -201,27 +235,53 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation private Object findFileByRelativePath( String fileName ) { - IPath path = getDirectory(); - if ( path != null ) + File[] folders = getFolders(); + if ( folders != null ) { - path = path.append( fileName ); - File file = path.toFile(); - if ( file.exists() ) + LinkedList list = new LinkedList(); + for ( int i = 0; i < folders.length; ++i ) { - path = new Path( file.getAbsolutePath() ); // can't use getCanonicalPath because of links - IFile[] wsFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation( path ); - LinkedList list = new LinkedList(); - for ( int j = 0; j < wsFiles.length; ++j ) - if ( wsFiles[j].exists() ) - if ( !searchForDuplicateFiles() ) - return wsFiles[j]; - else - list.add( wsFiles[j] ); - if ( list.size() > 0 ) - return ( list.size() == 1 ) ? list.getFirst() : list; - else - return createExternalFileStorage( path ); + Object result = findFileByRelativePath( folders[i], fileName ); + if ( result instanceof List ) + { + if ( searchForDuplicateFiles() ) + list.addAll( (List)result ); + else + return list.getFirst(); + } + else if ( result != null ) + { + if ( searchForDuplicateFiles() ) + list.add( result ); + else + return result; + } } + return list; + } + return null; + } + + private Object findFileByRelativePath( File folder, String fileName ) + { + IPath path = new Path( folder.getAbsolutePath() ); + path = path.append( fileName ); + File file = path.toFile(); + if ( file.exists() ) + { + path = new Path( file.getAbsolutePath() ); + IFile[] wsFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation( path ); + LinkedList list = new LinkedList(); + for ( int j = 0; j < wsFiles.length; ++j ) + if ( wsFiles[j].exists() ) + if ( !searchForDuplicateFiles() ) + return wsFiles[j]; + else + list.add( wsFiles[j] ); + if ( list.size() > 0 ) + return ( list.size() == 1 ) ? list.getFirst() : list; + else + return createExternalFileStorage( path ); } return null; } @@ -242,6 +302,7 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation node.setAttribute( ATTR_DIRECTORY, getDirectory().toOSString() ); if ( getAssociation() != null ) node.setAttribute( ATTR_ASSOCIATION, getAssociation().toOSString() ); + node.setAttribute( ATTR_SEARCH_SUBFOLDERS, new Boolean( searchSubfolders() ).toString() ); try { return CDebugUtils.serializeDocument( doc, " " ); @@ -276,7 +337,7 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation else { IPath path = new Path( dir ); - if ( path.isValidPath( dir ) && path.toFile().isDirectory() ) + if ( path.isValidPath( dir ) && path.toFile().isDirectory() && path.toFile().exists() ) { setDirectory( path ); } @@ -302,6 +363,7 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation setAssociation( null ); } } + setSearchSubfolders( Boolean.valueOf( root.getAttribute( ATTR_SEARCH_SUBFOLDERS ) ).booleanValue() ); return; } catch( ParserConfigurationException e ) @@ -385,4 +447,59 @@ public class CDirectorySourceLocation implements IDirectorySourceLocation { return fSearchForDuplicateFiles; } + + /* (non-Javadoc) + * @see org.eclipse.cdt.debug.core.sourcelookup.IDirectorySourceLocation#searchSubfolders() + */ + public boolean searchSubfolders() + { + return fSearchSubfolders; + } + + public void setSearchSubfolders( boolean search ) + { + fSearchSubfolders = search; + } + + protected File[] getFolders() + { + if ( fFolders == null ) + initializeFolders(); + return fFolders; + } + + protected void resetFolders() + { + fFolders = null; + } + + private void initializeFolders() + { + if ( getDirectory() != null ) + { + ArrayList list = new ArrayList(); + File root = getDirectory().toFile(); + list.add( root ); + if ( searchSubfolders() ) + list.addAll( getFileFolders( root ) ); + fFolders = (File[])list.toArray( new File[list.size()] ); + } + } + + private List getFileFolders( File file ) + { + ArrayList list = new ArrayList(); + File[] folders = file.listFiles( + new FileFilter() + { + public boolean accept( File pathname ) + { + return pathname.isDirectory(); + } + } ); + list.addAll( Arrays.asList( folders ) ); + for ( int i = 0; i < folders.length; ++i ) + list.addAll( getFileFolders( folders[i] ) ); + return list; + } } diff --git a/debug/org.eclipse.cdt.debug.ui/ChangeLog b/debug/org.eclipse.cdt.debug.ui/ChangeLog index e30243733ff..4007d2c2edf 100644 --- a/debug/org.eclipse.cdt.debug.ui/ChangeLog +++ b/debug/org.eclipse.cdt.debug.ui/ChangeLog @@ -1,3 +1,8 @@ +2003-10-20 Mikhail Khodjaiants + Implementation of the "Search subfolders" option for directory source locations. + * AddDirectorySourceLocationBlock.java + * SourceLookupBlock.java + 2003-10-17 Mikhail Khodjaiants UI support of the 'Search for duplicate source files' option. * icons/full/obj16/prj_file_obj.gif: new diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/wizards/AddDirectorySourceLocationBlock.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/wizards/AddDirectorySourceLocationBlock.java index ceb31e91fa6..36a3332e842 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/wizards/AddDirectorySourceLocationBlock.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/wizards/AddDirectorySourceLocationBlock.java @@ -37,6 +37,7 @@ public class AddDirectorySourceLocationBlock private Text fLocationText = null; private Text fAssociationText = null; private Button fAssocitedCheckButton = null; + private Button fSearchSubfoldersButton = null; private Shell fShell = null; private IPath fInitialAssosciationPath = null; @@ -59,6 +60,7 @@ public class AddDirectorySourceLocationBlock createLocationControls( fControl ); createAssociationControls( fControl ); + createSearchSubfoldersButton( fControl ); setInitialAssociationPath(); } @@ -146,6 +148,16 @@ public class AddDirectorySourceLocationBlock fAssociationText.setText( "" ); } + protected void createSearchSubfoldersButton( Composite parent ) + { + Composite composite = new Composite( parent, SWT.NONE ); + composite.setLayout( new GridLayout() ); + GridData data = new GridData( GridData.FILL_BOTH ); + composite.setLayoutData( data ); + fSearchSubfoldersButton = new Button( composite, SWT.CHECK ); + fSearchSubfoldersButton.setText( "Search sub&folders" ); + } + protected Button createButton( Composite parent, String label ) { Button button = new Button( parent, SWT.PUSH ); @@ -172,12 +184,17 @@ public class AddDirectorySourceLocationBlock return ""; } + public boolean searchSubfolders() + { + return ( fSearchSubfoldersButton != null ) ? fSearchSubfoldersButton.getSelection() : false; + } + public IDirectorySourceLocation getSourceLocation() { if ( isLocationPathValid() ) { Path association = ( isAssociationPathValid() ) ? new Path( getAssociationPath() ) : null; - return SourceLocationFactory.createDirectorySourceLocation( new Path( getLocationPath() ), association ); + return SourceLocationFactory.createDirectorySourceLocation( new Path( getLocationPath() ), association, searchSubfolders() ); } return null; } diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/sourcelookup/SourceLookupBlock.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/sourcelookup/SourceLookupBlock.java index c4d9eaa161f..3a5cca33130 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/sourcelookup/SourceLookupBlock.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/sourcelookup/SourceLookupBlock.java @@ -36,6 +36,7 @@ import org.eclipse.jface.viewers.CellEditor; import org.eclipse.jface.viewers.CheckStateChangedEvent; import org.eclipse.jface.viewers.CheckboxTableViewer; import org.eclipse.jface.viewers.ColumnWeightData; +import org.eclipse.jface.viewers.ComboBoxCellEditor; import org.eclipse.jface.viewers.ICellModifier; import org.eclipse.jface.viewers.ICheckStateListener; import org.eclipse.jface.viewers.ILabelProvider; @@ -94,13 +95,16 @@ public class SourceLookupBlock table.setHeaderVisible( true ); new TableColumn( table, SWT.NULL ); - tableLayout.addColumnData( new ColumnWeightData( 1, true ) ); + tableLayout.addColumnData( new ColumnWeightData( 2, true ) ); + new TableColumn( table, SWT.NULL ); + tableLayout.addColumnData( new ColumnWeightData( 2, true ) ); new TableColumn( table, SWT.NULL ); tableLayout.addColumnData( new ColumnWeightData( 1, true ) ); TableColumn[] columns = table.getColumns(); columns[0].setText( "Location" ); columns[1].setText( "Association" ); + columns[2].setText( "Search subfolders" ); return viewer; } @@ -147,13 +151,23 @@ public class SourceLookupBlock return ((IDirectorySourceLocation)element).getAssociation().toOSString(); } } + else if ( columnIndex == 2 ) + { + if ( element instanceof IDirectorySourceLocation ) + return ( ((IDirectorySourceLocation)element).searchSubfolders() ) ? YES_VALUE : NO_VALUE; + } return ""; } } + // String constants + protected static final String YES_VALUE = "yes"; + protected static final String NO_VALUE = "no"; + // Column properties private static final String CP_LOCATION = "location"; private static final String CP_ASSOCIATION = "association"; + private static final String CP_SEARCH_SUBFOLDERS = "searchSubfolders"; private Composite fControl = null; private Shell fShell = null; @@ -280,9 +294,10 @@ public class SourceLookupBlock TableViewer viewer = fAddedSourceListField.getTableViewer(); Table table = viewer.getTable(); - CellEditor cellEditor = new TextCellEditor( table ); - viewer.setCellEditors( new CellEditor[]{ null, cellEditor } ); - viewer.setColumnProperties( new String[]{ CP_LOCATION, CP_ASSOCIATION } ); + CellEditor textCellEditor = new TextCellEditor( table ); + CellEditor comboCellEditor = new ComboBoxCellEditor( table, new String[]{ YES_VALUE, NO_VALUE } ); + viewer.setCellEditors( new CellEditor[]{ null, textCellEditor, comboCellEditor } ); + viewer.setColumnProperties( new String[]{ CP_LOCATION, CP_ASSOCIATION, CP_SEARCH_SUBFOLDERS } ); viewer.setCellModifier( createCellModifier() ); // new Separator().doFillIntoGrid( fControl, 3, converter.convertHeightInCharsToPixels( 1 ) ); @@ -296,7 +311,7 @@ public class SourceLookupBlock { public boolean canModify( Object element, String property ) { - return ( element instanceof CDirectorySourceLocation && property.equals( CP_ASSOCIATION ) ); + return ( element instanceof CDirectorySourceLocation && ( property.equals( CP_ASSOCIATION ) || property.equals( CP_SEARCH_SUBFOLDERS ) ) ); } public Object getValue( Object element, String property ) @@ -306,20 +321,35 @@ public class SourceLookupBlock return ( ((CDirectorySourceLocation)element).getAssociation() != null ) ? ((CDirectorySourceLocation)element).getAssociation().toOSString() : ""; } + if ( element instanceof CDirectorySourceLocation && property.equals( CP_SEARCH_SUBFOLDERS ) ) + { + return ( ((CDirectorySourceLocation)element).searchSubfolders() ) ? new Integer( 0 ) : new Integer( 1 ); + } return null; } public void modify( Object element, String property, Object value ) { Object entry = getSelection(); - if ( entry instanceof CDirectorySourceLocation && - property.equals( CP_ASSOCIATION ) && - value instanceof String ) + if ( entry instanceof CDirectorySourceLocation ) { - Path association = new Path( (String)value ); - if ( association.isValidPath( (String)value ) ) + boolean changed = false; + if ( property.equals( CP_ASSOCIATION ) && value instanceof String ) + { + Path association = new Path( (String)value ); + if ( association.isValidPath( (String)value ) ) + { + ((CDirectorySourceLocation)entry).setAssociation( association ); + changed = true; + } + } + if ( property.equals( CP_SEARCH_SUBFOLDERS ) && value instanceof Integer ) + { + ((CDirectorySourceLocation)entry).setSearchSubfolders( ((Integer)value).intValue() == 0 ); + changed = true; + } + if ( changed ) { - ((CDirectorySourceLocation)entry).setAssociation( association ); getAddedSourceListField().refresh(); updateLaunchConfigurationDialog(); }