下面列出了怎么用org.eclipse.ui.internal.ide.IDEWorkbenchPlugin的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Creates the feature-dependent actions for the menu bar.
*/
private void makeFeatureDependentActions(final IWorkbenchWindow aWindow) {
// final AboutInfo[] infos = null;
final IPreferenceStore prefs = IDEWorkbenchPlugin.getDefault().getPreferenceStore();
// Optimization: avoid obtaining the about infos if the platform state is
// unchanged from last time. See bug 75130 for details.
final String stateKey = "platformState"; //$NON-NLS-1$
final String prevState = prefs.getString(stateKey);
final String currentState = String.valueOf(Platform.getStateStamp());
final boolean sameState = currentState.equals(prevState);
if ( !sameState ) {
prefs.putValue(stateKey, currentState);
}
}
/**
* Return the new name to be given to the target resource.
*
* @return java.lang.String
* @param resource
* the resource to query status on
*/
protected String queryNewResourceName(final IResource resource) {
final IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
final IPath prefix = resource.getFullPath().removeLastSegments(1);
final IInputValidator validator = string -> {
if (resource.getName().equals(string)) {
return IDEWorkbenchMessages.RenameResourceAction_nameMustBeDifferent;
}
final IStatus status = workspace.validateName(string, resource.getType());
if (!status.isOK()) { return status.getMessage(); }
if (workspace.getRoot().exists(prefix.append(string))) {
return IDEWorkbenchMessages.RenameResourceAction_nameExists;
}
return null;
};
final InputDialog dialog =
new InputDialog(WorkbenchHelper.getShell(), IDEWorkbenchMessages.RenameResourceAction_inputDialogTitle,
IDEWorkbenchMessages.RenameResourceAction_inputDialogMessage, resource.getName(), validator);
dialog.setBlockOnOpen(true);
final int result = dialog.open();
if (result == Window.OK) { return dialog.getValue(); }
return null;
}
protected boolean validatePage() {
canFinish=false;
getShell().setText(Messages.CREATEPROJECTWIZARD);
IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
String projectFieldContents = getProjectNameFieldValue();
if (projectFieldContents.equals("")) {
setErrorMessage(null);
setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
return false;
}
IStatus nameStatus = workspace.validateName(projectFieldContents,
IResource.PROJECT);
if (!nameStatus.isOK()) {
setErrorMessage(nameStatus.getMessage());
return false;
}
IProject handle = getProjectHandle();
if (handle.exists()) {
setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
return false;
}
return dialogChanged();
}
/**
* Constructor for ExternalProjectImportWizard.
*
* @param initialPath Default path for wizard to import
* @since 3.5
*/
public ImportProjectWizard(String initialPath)
{
super();
this.initialPath = initialPath;
setWindowTitle(DataTransferMessages.DataTransfer_importTitle);
setNeedsProgressMonitor(true);
IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault()
.getDialogSettings();
IDialogSettings wizardSettings = workbenchSettings
.getSection(IMPORT_PROJECT_SECTION);
if (wizardSettings == null) {
wizardSettings = workbenchSettings
.addNewSection(IMPORT_PROJECT_SECTION);
}
setDialogSettings(wizardSettings);
}
public IStatus validate(Object[] selection) {
int nSelected = selection.length;
String pluginId = IDEWorkbenchPlugin.IDE_WORKBENCH;
if (nSelected == 0 || (nSelected > 1 && multiSelect == false)) {
return new Status(IStatus.ERROR, pluginId, IStatus.ERROR,
IDEResourceInfoUtils.EMPTY_STRING, null);
}
for (int i = 0; i < selection.length; i++) {
Object curr = selection[i];
if (curr instanceof IFileStore) {
IFileStore file = (IFileStore) curr;
if (acceptFolders == false
&& file.fetchInfo().isDirectory()) {
return new Status(IStatus.ERROR, pluginId,
IStatus.ERROR,
IDEResourceInfoUtils.EMPTY_STRING, null);
}
}
}
return Status.OK_STATUS;
}
/**
* Constructor for ExternalProjectImportWizard.
*
* @param initialPath Default path for wizard to import
* @since 3.5
*/
public ImportProjectWizard(String initialPath)
{
super();
this.initialPath = initialPath;
setWindowTitle(DataTransferMessages.DataTransfer_importTitle);
setNeedsProgressMonitor(true);
IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault()
.getDialogSettings();
IDialogSettings wizardSettings = workbenchSettings
.getSection(IMPORT_PROJECT_SECTION);
if (wizardSettings == null) {
wizardSettings = workbenchSettings
.addNewSection(IMPORT_PROJECT_SECTION);
}
setDialogSettings(wizardSettings);
}
public IStatus validate(Object[] selection) {
int nSelected = selection.length;
String pluginId = IDEWorkbenchPlugin.IDE_WORKBENCH;
if (nSelected == 0 || (nSelected > 1 && multiSelect == false)) {
return new Status(IStatus.ERROR, pluginId, IStatus.ERROR,
IDEResourceInfoUtils.EMPTY_STRING, null);
}
for (int i = 0; i < selection.length; i++) {
Object curr = selection[i];
if (curr instanceof IFileStore) {
IFileStore file = (IFileStore) curr;
if (acceptFolders == false
&& file.fetchInfo().isDirectory()) {
return new Status(IStatus.ERROR, pluginId,
IStatus.ERROR,
IDEResourceInfoUtils.EMPTY_STRING, null);
}
}
}
return Status.OK_STATUS;
}
/**
* Build the collection of fileStores that map to fileNames. If any of them
* cannot be found then match then return <code>null</code>.
*
* @param uris
* @return IFileStore[]
*/
private IFileStore[] buildFileStores(URI[] uris) {
IFileStore[] stores = new IFileStore[uris.length];
for (int i = 0; i < uris.length; i++) {
IFileStore store;
try {
store = EFS.getStore(uris[i]);
} catch (CoreException e) {
IDEWorkbenchPlugin.log(e.getMessage(), e);
reportFileInfoNotFound(uris[i].toString());
return null;
}
if (store == null) {
reportFileInfoNotFound(uris[i].toString());
return null;
}
stores[i] = store;
}
return stores;
}
private String getEditorId(String fileName) {
if (fileName.endsWith("xml")) { //$NON-NLS-1$
//if file type is .xml, return the associated XML editor
IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry();
IEditorDescriptor desc = editorReg.getDefaultEditor(fileName, Platform.getContentTypeManager().findContentTypeFor(fileName));
if (desc != null) {
return desc.getId();
}
}
//use eclipse default text editor for all other files
return IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID;
}
/**
* Returns whether this page's controls currently all contain valid values.
*
* @return <code>true</code> if all controls are valid, and <code>false</code> if at least one is invalid
*/
protected boolean validatePage() {
IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
// CHANGED: use getProjectName to allow subclasses to control what concrete project name value is validated
String projectFieldContents = getProjectName();
if (projectFieldContents.equals("")) { //$NON-NLS-1$
setErrorMessage(null);
setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
return false;
}
IStatus nameStatus = workspace.validateName(projectFieldContents,
IResource.PROJECT);
if (!nameStatus.isOK()) {
setErrorMessage(nameStatus.getMessage());
return false;
}
IProject handle = getProjectHandle();
if (handle.exists()) {
setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
return false;
}
// CHANGED: allow getProjectHandle to control the IProject instance used here
locationArea.setExistingProject(handle);
String validLocationMessage = locationArea.checkValidLocation();
if (validLocationMessage != null) { // there is no destination location given
setErrorMessage(validLocationMessage);
return false;
}
setErrorMessage(null);
setMessage(null);
return true;
}
/**
* Prompts the user for whether to switch perspectives.
*
* @param window
* The workbench window in which to switch perspectives; must not
* be <code>null</code>
* @param finalPersp
* The perspective to switch to; must not be <code>null</code>.
*
* @return <code>true</code> if it's OK to switch, <code>false</code>
* otherwise
*/
private static boolean confirmPerspectiveSwitch(IWorkbenchWindow window, IPerspectiveDescriptor finalPersp) {
IPreferenceStore store = IDEWorkbenchPlugin.getDefault().getPreferenceStore();
String pspm = store.getString(IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE);
if (!IDEInternalPreferences.PSPM_PROMPT.equals(pspm)) {
// Return whether or not we should always switch
return IDEInternalPreferences.PSPM_ALWAYS.equals(pspm);
}
String desc = finalPersp.getDescription();
String message;
if (desc == null || desc.length() == 0)
message = NLS.bind(ResourceMessages.NewProject_perspSwitchMessage, finalPersp.getLabel());
else
message = NLS.bind(ResourceMessages.NewProject_perspSwitchMessageWithDesc,
new String[] { finalPersp.getLabel(), desc });
MessageDialogWithToggle dialog = MessageDialogWithToggle.openYesNoQuestion(window.getShell(),
ResourceMessages.NewProject_perspSwitchTitle, message,
null /* use the default message for the toggle */,
false /* toggle is initially unchecked */, store, IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE);
int result = dialog.getReturnCode();
// If we are not going to prompt anymore propogate the choice.
if (dialog.getToggleState()) {
String preferenceValue;
if (result == IDialogConstants.YES_ID) {
// Doesn't matter if it is replace or new window
// as we are going to use the open perspective setting
preferenceValue = IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE;
} else {
preferenceValue = IWorkbenchPreferenceConstants.NO_NEW_PERSPECTIVE;
}
// update PROJECT_OPEN_NEW_PERSPECTIVE to correspond
PrefUtil.getAPIPreferenceStore().setValue(IDE.Preferences.PROJECT_OPEN_NEW_PERSPECTIVE, preferenceValue);
}
return result == IDialogConstants.YES_ID;
}
@Override
public InputStream getContents(Object element) {
try {
return zipFile.getInputStream((ZipArchiveEntry) element);
} catch (IOException e) {
IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
return null;
}
}
@Override
public boolean closeArchive(){
try {
getZipFile().close();
} catch (IOException e) {
IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose
+ getZipFile(), e);
return false;
}
return true;
}
@Override
public InputStream getContents(Object element) {
try {
return tarFile.getInputStream((TarArchiveEntry) element);
} catch (IOException e) {
IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
return null;
}
}
@Override
public boolean closeArchive(){
try {
getTarFile().close();
} catch (IOException e) {
IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose
+ getTarFile().getName(), e);
return false;
}
return true;
}
@Override
protected IDialogSettings getDialogSettings() {
IDialogSettings settings = IDEWorkbenchPlugin.getDefault().getDialogSettings().getSection(
DIALOG_SETTINGS);
if (settings == null) {
settings = IDEWorkbenchPlugin.getDefault().getDialogSettings().addNewSection(
DIALOG_SETTINGS);
}
return settings;
}
/**
* The browse button has been selected. Select the location.
*/
protected void handleLocationDirectoryButtonPressed() {
final DirectoryDialog dialog = new DirectoryDialog(directoryPathField.getShell(), SWT.SHEET);
dialog.setMessage(DataTransferMessages.WizardProjectsImportPage_SelectDialogTitle);
String dirName = directoryPathField.getText().trim();
if (dirName.length() == 0) {
dirName = previouslyBrowsedDirectory;
}
if (dirName.length() == 0) {
dialog.setFilterPath(IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getLocation().toOSString());
} else {
final File path = new File(dirName);
if (path.exists()) {
dialog.setFilterPath(new Path(dirName).toOSString());
}
}
final String selectedDirectory = dialog.open();
if (selectedDirectory != null) {
previouslyBrowsedDirectory = selectedDirectory;
directoryPathField.setText(previouslyBrowsedDirectory);
updateProjectsList(selectedDirectory);
}
}
/**
* The browse button has been selected. Select the location.
*/
protected void handleLocationArchiveButtonPressed() {
final FileDialog dialog = new FileDialog(archivePathField.getShell(), SWT.SHEET);
dialog.setFilterExtensions(FILE_IMPORT_MASK);
dialog.setText(DataTransferMessages.WizardProjectsImportPage_SelectArchiveDialogTitle);
String fileName = archivePathField.getText().trim();
if (fileName.length() == 0) {
fileName = previouslyBrowsedArchive;
}
if (fileName.length() == 0) {
dialog.setFilterPath(IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getLocation().toOSString());
} else {
final File path = new File(fileName).getParentFile();
if (path != null && path.exists()) {
dialog.setFilterPath(path.toString());
}
}
final String selectedArchive = dialog.open();
if (selectedArchive != null) {
previouslyBrowsedArchive = selectedArchive;
archivePathField.setText(previouslyBrowsedArchive);
updateProjectsList(selectedArchive);
}
}
/**
* Returns whether this page's controls currently all contain valid values.
*
* @return <code>true</code> if all controls are valid, and <code>false</code> if at least one is invalid
*/
protected boolean validatePage() {
final IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
final String projectFieldContents = getProjectNameFieldValue();
if (projectFieldContents.equals("")) { //$NON-NLS-1$
setErrorMessage(null);
setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
return false;
}
final IStatus nameStatus = workspace.validateName(projectFieldContents, IResource.PROJECT);
if (!nameStatus.isOK()) {
setErrorMessage(nameStatus.getMessage());
return false;
}
final IProject handle = getProjectHandle();
if (handle.exists()) {
getProjectHandle();
setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
return false;
}
// final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectNameFieldValue());
// locationArea.setExistingProject(project);
// final String validLocationMessage = locationArea.checkValidLocation();
// if (validLocationMessage != null) { // there is no destination location given
// setErrorMessage(validLocationMessage);
// return false;
// }
setErrorMessage(null);
setMessage(null);
return true;
}
public ExportProjectWizard(final String initialPath) {
// this.initialPath = initialPath;
setNeedsProgressMonitor(true);
final IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault().getDialogSettings();
IDialogSettings wizardSettings = workbenchSettings.getSection(EXTERNAL_PROJECT_SECTION);
if (wizardSettings == null) {
wizardSettings = workbenchSettings.addNewSection(EXTERNAL_PROJECT_SECTION);
}
setDialogSettings(wizardSettings);
}
public ImportProjectWizard(final String initialPath) {
this.initialPath = initialPath;
setNeedsProgressMonitor(true);
final IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault().getDialogSettings();
IDialogSettings wizardSettings = workbenchSettings.getSection(EXTERNAL_PROJECT_SECTION);
if (wizardSettings == null) {
wizardSettings = workbenchSettings.addNewSection(EXTERNAL_PROJECT_SECTION);
}
setDialogSettings(wizardSettings);
}
private IDialogSettings getDialogSettings()
{
IDialogSettings ideDialogSettings = IDEWorkbenchPlugin.getDefault().getDialogSettings();
IDialogSettings result = ideDialogSettings.getSection(getClass().getName());
if (result == null)
{
result = ideDialogSettings.addNewSection(getClass().getName());
}
return result;
}
@Override
public Image getImage(Object element)
{
String nature = element.toString();
OverlayIcon oi = null;
ImageData id = EMPTY_IMAGE.getImageData();
try
{
ImageDescriptor d = IDEWorkbenchPlugin.getDefault().getProjectImageRegistry()
.getNatureImage(element.toString());
oi = new CenterIcon(EMPTY_IMAGE, d, new Point(id.width, id.height));
}
catch (Exception e)
{
oi = new CenterIcon(EMPTY_IMAGE, APTANA_NATURE_IMAGE, new Point(id.width, id.height));
}
if (UIEplPlugin.getDefault().getImageRegistry().get(nature) == null)
{
if (oi != null)
{
UIEplPlugin.getDefault().getImageRegistry().put(nature, oi.createImage());
}
}
return UIEplPlugin.getDefault().getImageRegistry().get(nature);
}
/**
* The browse button has been selected. Select the location.
*/
protected void handleLocationDirectoryButtonPressed()
{
DirectoryDialog dialog = new DirectoryDialog(directoryPathField.getShell());
dialog.setMessage(DataTransferMessages.WizardProjectsImportPage_SelectDialogTitle);
String dirName = directoryPathField.getText().trim();
if (dirName.length() == 0)
{
dirName = previouslyBrowsedDirectory;
}
if (dirName.length() == 0)
{
dialog.setFilterPath(IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getLocation().toOSString());
}
else
{
File path = new File(dirName);
if (path.exists())
{
dialog.setFilterPath(new Path(dirName).toOSString());
}
}
String selectedDirectory = dialog.open();
if (selectedDirectory != null)
{
previouslyBrowsedDirectory = selectedDirectory;
directoryPathField.setText(previouslyBrowsedDirectory);
}
setProjectName();
setPageComplete(directoryPathField.getText() != null);
}
/**
* Returns whether this page's controls currently all contain valid
* values.
*
* @return <code>true</code> if all controls are valid, and
* <code>false</code> if at least one is invalid
*/
protected boolean validatePage() {
IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
String projectFieldContents = getProjectNameFieldValue();
if (projectFieldContents.equals("")) { //$NON-NLS-1$
setErrorMessage(null);
setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
return false;
}
IStatus nameStatus = workspace.validateName(projectFieldContents,
IResource.PROJECT);
if (!nameStatus.isOK()) {
setErrorMessage(nameStatus.getMessage());
return false;
}
IProject handle = getProjectHandle();
if (handle.exists()) {
setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
return false;
}
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
getProjectNameFieldValue());
locationArea.setExistingProject(project);
String validLocationMessage = locationArea.checkValidLocation();
if (validLocationMessage != null) { // there is no destination location given
setErrorMessage(validLocationMessage);
return false;
}
setErrorMessage(null);
setMessage(null);
return true;
}
/**
* 声明所需要使用的图片;
*/
private void declareWorkbenchImages() {
final String iconsPath = "$nl$/icons/full/"; //$NON-NLS-1$
final String pathElocaltool = iconsPath + "elcl16/"; // Enabled //$NON-NLS-1$
final String pathObject = iconsPath + "obj16/"; // Model object //$NON-NLS-1$
Bundle ideBundle = Platform.getBundle(IDEWorkbenchPlugin.IDE_WORKBENCH);
declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJ_PROJECT, pathObject + "prj_obj.gif", true); //$NON-NLS-1$
declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED, pathObject + "cprj_obj.gif", true); //$NON-NLS-1$
declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OPEN_MARKER, pathElocaltool + "gotoobj_tsk.gif", true); //$NON-NLS-1$
declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJS_TASK_TSK, pathObject + "taskmrk_tsk.gif", true); //$NON-NLS-1$
declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJS_BKMRK_TSK, pathObject + "bkmrk_tsk.gif", true); //$NON-NLS-1$
}
/**
* 设置在拖动文件到导航视图时的模式为直接复制,见类 {@link CopyFilesAndFoldersOperation} 的方法 CopyFilesAndFoldersOperation
* robert 09-26
*/
private void setDragModle(){
IPreferenceStore store= IDEWorkbenchPlugin.getDefault().getPreferenceStore();
store.setValue(IDEInternalPreferences.IMPORT_FILES_AND_FOLDERS_MODE,
IDEInternalPreferences.IMPORT_FILES_AND_FOLDERS_MODE_MOVE_COPY);
store.setValue(IDEInternalPreferences.IMPORT_FILES_AND_FOLDERS_VIRTUAL_FOLDER_MODE,
IDEInternalPreferences.IMPORT_FILES_AND_FOLDERS_MODE_MOVE_COPY);
}
/**
* Returns the status of the operation. If there were any errors, the result is a status object containing
* individual status objects for each error. If there were no errors, the result is a status object with error code
* <code>OK</code>.
* @return the status
*/
public IStatus getStatus() {
IStatus[] errors = new IStatus[errorTable.size()];
errorTable.toArray(errors);
return new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.OK, errors,
DataTransferMessages.FileSystemExportOperation_problemsExporting, null);
}
/**
* The browse button has been selected. Select the location.
*/
protected void handleLocationArchiveButtonPressed() {
FileDialog dialog = new FileDialog(archivePathField.getShell(), SWT.SHEET);
dialog.setFilterExtensions(FILE_IMPORT_MASK);
dialog
.setText(DataTransferMessages.WizardProjectsImportPage_SelectArchiveDialogTitle);
String fileName = archivePathField.getText().trim();
if (fileName.length() == 0) {
fileName = previouslyBrowsedArchive;
}
if (fileName.length() == 0) {
dialog.setFilterPath(IDEWorkbenchPlugin.getPluginWorkspace()
.getRoot().getLocation().toOSString());
} else {
File path = new File(fileName).getParentFile();
if (path != null && path.exists()) {
dialog.setFilterPath(path.toString());
}
}
String selectedArchive = dialog.open();
if (selectedArchive != null) {
previouslyBrowsedArchive = selectedArchive;
archivePathField.setText(previouslyBrowsedArchive);
updateProjectsList(selectedArchive);
}
}
/**
* Retrieve all the projects in the current workspace.
*
* @return IProject[] array of IProject in the current workspace
*/
private IProject[] getProjectsInWorkspace() {
if (wsProjects == null) {
wsProjects = IDEWorkbenchPlugin.getPluginWorkspace().getRoot()
.getProjects();
}
return wsProjects;
}