I have solved this issue now and for those interested, the code is posted below... I help you all find this helpful:
public final class ExportLayerToShapefileAction extends SelectionAction {
public ExportLayerToShapefileAction() {
super("gsExportToShapefile");
}
@Override
protected void doActionPerformed(ActionEvent e) {
IFeatureSelection featureSelection = getFeatureSelection();
if (!(featureSelection instanceof IFeatureLayer)) {
return;
}
FeatureLayer featureLayer;
try {
featureLayer = Casting.cast(featureSelection, FeatureLayer.class);
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd_hh-mm-ss");
String dateString = dateFormat.format(currentDate);
IFeatureLayer selectionLayer = featureLayer.createSelectionLayer(featureLayer.getName() + " " + dateString,
true,
"",
"");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select a directory to export to.");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int userSelection = fileChooser.showSaveDialog(WinMgr.getInstance().getSelectedNavigator());
if (!(userSelection == JFileChooser.APPROVE_OPTION)) {
return;
}
FileUtil.createFileIfNotExists(new File(fileChooser.getSelectedFile()
.getAbsolutePath() + "\\Layer_Export_" + dateString));
exportLayerToShapefile(fileChooser.getSelectedFile().getAbsolutePath(),
"Layer_Export_" + dateString,
selectionLayer,
null,
null);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static boolean exportLayerToShapefile(
String shapePath,
String shapeName,
ILayer source,
IEnumFieldError[] fieldErrors,
IEnumInvalidObject invalidObjects) {
try {
IFeatureLayer sfeatlayer = (IFeatureLayer) source;
IFeatureClass sfeatClass = sfeatlayer.getFeatureClass();
IDataset sdataset = Casting.cast(sfeatClass, IDataset.class);
IDatasetName sdatasetName = (IDatasetName) sdataset.getFullName();
ISelectionSet sSelectionSet = (
(ITable) source).select(new QueryFilter(),
esriSelectionType.esriSelectionTypeHybrid,
esriSelectionOption.esriSelectionOptionNormal,
sdataset.getWorkspace());
IWorkspaceFactory factory = new ShapefileWorkspaceFactory();
IWorkspace targetWorkspace = factory.openFromFile(shapePath, 0);
IDataset targetDataset = (IDataset) targetWorkspace;
IName targetWorkspaceName = targetDataset.getFullName();
IWorkspaceName tWorkspaceName = (IWorkspaceName) targetWorkspaceName;
IFeatureClassName tFeatClassname = new FeatureClassName();
IDatasetName tDatasetName = (IDatasetName) tFeatClassname;
tDatasetName.setName(shapeName);
tDatasetName.setWorkspaceNameByRef(tWorkspaceName);
IFieldChecker fieldChecker = new FieldChecker();
IFields sFields = sfeatClass.getFields();
fieldChecker.setInputWorkspace(sdataset.getWorkspace());
fieldChecker.setValidateWorkspaceByRef(targetWorkspace);
fieldChecker.validate(sFields, fieldErrors, null);
if (fieldErrors != null) {
System.out.println("Errors encountered during field validation");
}
String shapefieldName = sfeatClass.getShapeFieldName();
int shapeFieldIndex = sfeatClass.findField(shapefieldName);
IField shapefield = sFields.getField(shapeFieldIndex);
IGeometryDef geomDef = shapefield.getGeometryDef();
IClone geomDefClone = (IClone) geomDef;
IClone targetGeomDefClone = geomDefClone.esri_clone();
IGeometryDef tGeomDef = (IGeometryDef) targetGeomDefClone;
IFeatureDataConverter2 featDataConverter = (IFeatureDataConverter2) new FeatureDataConverter();
invalidObjects = featDataConverter.convertFeatureClass(
sdatasetName,
null,
sSelectionSet,
null,
tFeatClassname,
tGeomDef,
null,
"",
1000, 0);
return true;
} catch (IOException | Casting.CastException e) {
e.printStackTrace();
}
return false;
}
}