📄 commonutils.java
字号:
package com.cownew.studio.modelDev.common;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import com.cownew.studio.Activator;
public class CommonUtils
{
public static String LINESEPARATOR = System.getProperties().getProperty("line.separator");
public static void closeStream(InputStream inStream)
{
if(inStream==null)
{
return;
}
try
{
inStream.close();
} catch (IOException e)
{
Activator.logException(e);
}
}
public static void handleException(Exception e)
{
MessageDialog.openError(Activator.getShell(), "error", e.toString());
Activator.logException(e);
}
public static void handleExceptionAndAbort(Exception e)
{
handleException(e);
throw new RuntimeException(e);
}
public static String emptyIfNull(String value)
{
if(value!=null&&value.trim().length()>0)
{
return value;
}
return "";
}
public static boolean isEmptyString(String value)
{
if(value==null||value.trim().length()<=0)
{
return true;
}
return false;
}
public static Set<IFile> getAllFiles(IStructuredSelection structSelect,String fileExtension)
throws CoreException
{
Set<IFile> retSet = new HashSet<IFile>();
List<IResource> selectList = structSelect.toList();
for(int i=0,n=selectList.size();i<n;i++)
{
Set<IFile> allFiles = getAllFiles(selectList.get(i),fileExtension);
retSet.addAll(allFiles);
}
return retSet;
}
public static Set<IFile> getAllFiles(IResource res,String fileExtension) throws CoreException
{
Set<IFile> retSet = new HashSet<IFile>();
if(res instanceof IFile)
{
IFile file = (IFile) res;
if(!isEmptyString(fileExtension))
{
if(file.getFileExtension().equalsIgnoreCase(fileExtension))
{
retSet.add(file);
}
}
else
{
retSet.add(file);
}
}
else if(res instanceof IContainer)
{
IContainer cont = (IContainer) res;
IResource[] chidRess = cont.members();
for(int i=0,n=chidRess.length;i<n;i++)
{
retSet.addAll(getAllFiles(chidRess[i],fileExtension));
}
}
return retSet;
}
public static IFolder packageFragementToFolder(IPackageFragment fragment)
{
IPath path = fragment.getPath();
return ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
}
public static void saveToFile(IFile file,String value,IProgressMonitor monitor)
throws CoreException
{
InputStream stream = null;
try
{
stream = new ByteArrayInputStream(value.getBytes());
if (file.exists())
{
//如果文件已经存在则直接设置文件的内容
file.setContents(stream, true, true, monitor);
} else
{
//如果不存在则首先创建然后再设置文件的内容
file.create(stream, true, monitor);
}
//设置文件的编码
file.setCharset("UTF-8",monitor);
}
finally
{
closeStream(stream);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -