Java Code Examples for org.eclipse.jface.dialogs.InputDialog#setBlockOnOpen()

The following examples show how to use org.eclipse.jface.dialogs.InputDialog#setBlockOnOpen() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenameResourceAction.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: RenameTermHandler.java    From slr-toolkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {			
	ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
	if (selection == null || !(selection instanceof IStructuredSelection)) {
		return null;
	}
	IStructuredSelection currentSelection = (IStructuredSelection) selection;
	if (currentSelection.size() == 1) {
		Term term = (Term) currentSelection.getFirstElement();	
		InputDialog dialog = new InputDialog(
				null,
				"Rename Term", 
				"Rename Term: " + term.getName() + " to:",
				term.getName(),
				null);
		dialog.setBlockOnOpen(true);
		if (dialog.open() == InputDialog.OK) {
			TermRenamer.rename(term, dialog.getValue());
		}
	}		
	return null;
}
 
Example 3
Source File: DialogHelpers.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public static Integer openAskInt(String title, String message, int initial) {
    Shell shell = EditorUtils.getShell();
    String initialValue = "" + initial;
    IInputValidator validator = new IInputValidator() {

        @Override
        public String isValid(String newText) {
            if (newText.length() == 0) {
                return "At least 1 char must be provided.";
            }
            try {
                Integer.parseInt(newText);
            } catch (Exception e) {
                return "A number is required.";
            }
            return null;
        }
    };
    InputDialog dialog = new InputDialog(shell, title, message, initialValue, validator);
    dialog.setBlockOnOpen(true);
    if (dialog.open() == Window.OK) {
        return Integer.parseInt(dialog.getValue());
    }
    return null;
}
 
Example 4
Source File: RenameResourceAndCloseEditorAction.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
	IInputValidator validator = new IInputValidator() {
		public String isValid(String string) {
			if (resource.getName().equals(string)) {
				return IDEWorkbenchMessages.RenameResourceAction_nameMustBeDifferent;
			}
			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;
		}
	};

	InputDialog dialog = new InputDialog(shellProvider.getShell(),
			IDEWorkbenchMessages.RenameResourceAction_inputDialogTitle,
			IDEWorkbenchMessages.RenameResourceAction_inputDialogMessage,
			resource.getName(), validator);
	dialog.setBlockOnOpen(true);
	int result = dialog.open();
	if (result == Window.OK)
		return dialog.getValue();
	return null;
}
 
Example 5
Source File: RenameResourceAndCloseEditorAction.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
	IInputValidator validator = new IInputValidator() {
		public String isValid(String string) {
			if (resource.getName().equals(string)) {
				return IDEWorkbenchMessages.RenameResourceAction_nameMustBeDifferent;
			}
			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;
		}
	};

	InputDialog dialog = new InputDialog(shellProvider.getShell(),
			IDEWorkbenchMessages.RenameResourceAction_inputDialogTitle,
			IDEWorkbenchMessages.RenameResourceAction_inputDialogMessage,
			resource.getName(), validator);
	dialog.setBlockOnOpen(true);
	int result = dialog.open();
	if (result == Window.OK)
		return dialog.getValue();
	return null;
}
 
Example 6
Source File: DialogHelpers.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public static String openInputRequest(String title, String message, Shell shell, IInputValidator validator) {
    if (shell == null) {
        shell = EditorUtils.getShell();
    }
    String initialValue = "";
    InputDialog dialog = new InputDialog(shell, title, message, initialValue, validator);
    dialog.setBlockOnOpen(true);
    if (dialog.open() == Window.OK) {
        return dialog.getValue();
    }
    return null;
}
 
Example 7
Source File: PyRenameResourceAction.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return the new name to be given to the target resource.
 *
 * @return java.lang.String
 * @param resource the resource to query status on
 *
 * Fix from platform: was not checking return from dialog.open
 */
@Override
protected String queryNewResourceName(final IResource resource) {
    final IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
    final IPath prefix = resource.getFullPath().removeLastSegments(1);
    IInputValidator validator = new IInputValidator() {
        @Override
        public String isValid(String string) {
            if (resource.getName().equals(string)) {
                return IDEWorkbenchMessages.RenameResourceAction_nameMustBeDifferent;
            }
            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;
        }
    };

    InputDialog dialog = new InputDialog(shell, IDEWorkbenchMessages.RenameResourceAction_inputDialogTitle,
            IDEWorkbenchMessages.RenameResourceAction_inputDialogMessage, resource.getName(), validator);
    dialog.setBlockOnOpen(true);
    if (dialog.open() == Window.OK) {
        return dialog.getValue();
    } else {
        return null;
    }
}
 
Example 8
Source File: RenameSpecHandler.java    From tlaplus with MIT License 4 votes vote down vote up
public Object execute(ExecutionEvent event) throws ExecutionException {
	/*
	 * No parameter try to get it from active navigator if any
	 */
	final IWorkbenchPage activePage = UIHelper.getActivePage();
	if (activePage != null) {
		final ISelection selection = activePage.getSelection(ToolboxExplorer.VIEW_ID);
		if (selection != null && selection instanceof IStructuredSelection) {
			// handler is set up to only allow single selection in
			// plugin.xml -> no need to handle multi selection
			final Spec spec = (Spec) ((IStructuredSelection) selection).getFirstElement();

			// name proposal
			String specName = spec.getName() + "_Copy";

			// dialog that prompts the user for the new spec name (no need
			// to join UI thread, this is the UI thread)
			final InputDialog dialog = new InputDialog(UIHelper.getShell(), "New specification name",
					"Please input the new name of the specification", specName, new SpecNameValidator());
			dialog.setBlockOnOpen(true);
			if (dialog.open() == Window.OK) {
				final WorkspaceSpecManager specManager = Activator.getSpecManager();
				
				// close open spec before it gets renamed
				reopenEditorAfterRename = false;
				if (specManager.isSpecLoaded(spec)) {
					UIHelper.runCommand(CloseSpecHandler.COMMAND_ID, new HashMap<String, String>());
					reopenEditorAfterRename = true;
				}
				
				// use confirmed rename -> rename
				final Job j = new ToolboxJob("Renaming spec...") {
					protected IStatus run(final IProgressMonitor monitor) {
						// do the actual rename
						specManager.renameSpec(spec, dialog.getValue(), monitor);
						
						// reopen editor again (has to happen in UI thread)
						UIHelper.runUIAsync(new Runnable() {
							/* (non-Javadoc)
							 * @see java.lang.Runnable#run()
							 */
							public void run() {
								if (reopenEditorAfterRename) {
									Map<String, String> parameters = new HashMap<String, String>();
									parameters.put(OpenSpecHandler.PARAM_SPEC, dialog.getValue());
									UIHelper.runCommand(OpenSpecHandler.COMMAND_ID, parameters);
								}
							}
						});
						return Status.OK_STATUS;
					}
				};
				j.schedule();
			}
		}
	}
	return null;
}
 
Example 9
Source File: RenameModelHandlerDelegate.java    From tlaplus with MIT License 4 votes vote down vote up
public Object execute(ExecutionEvent event) throws ExecutionException
 {
     final ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
     if (selection != null && selection instanceof IStructuredSelection)
     {
         // model file
         final Model model = (Model) ((IStructuredSelection) selection).getFirstElement();

         // a) fail if model is in use
if (model.isRunning()) {
	MessageDialog.openError(UIHelper.getShellProvider().getShell(), "Could not rename models",
			"Could not rename the model " + model.getName()
					+ ", because it is being model checked.");
	return null;
}
if (model.isSnapshot()) {
	MessageDialog.openError(UIHelper.getShellProvider().getShell(), "Could not rename model",
			"Could not rename the model " + model.getName()
					+ ", because it is a snapshot.");
	return null;
}

         // b) open dialog prompting for new model name
         final IInputValidator modelNameInputValidator = new ModelNameValidator(model.getSpec());
         final InputDialog dialog = new InputDialog(UIHelper.getShell(), "Rename model...",
                 "Please input the new name of the model", model.getName(), modelNameInputValidator);
         dialog.setBlockOnOpen(true);
         if(dialog.open() == Window.OK) {
         	// c1) close model editor if open
             IEditorPart editor = model.getAdapter(ModelEditor.class);
             if(editor != null) {
             	reopenModelEditorAfterRename = true;
             	UIHelper.getActivePage().closeEditor(editor, true);
             }
             // c2) close snapshot model editors 
             final Collection<Model> snapshots = model.getSnapshots();
	for (Model snapshot : snapshots) {
		editor = snapshot.getAdapter(ModelEditor.class);
		if (editor != null) {
			UIHelper.getActivePage().closeEditor(editor, true);
		}
	}
             
	final WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
		@Override
		protected void execute(IProgressMonitor monitor)
				throws CoreException, InvocationTargetException, InterruptedException {
			// d) rename
			final String newModelName = dialog.getValue();
			model.rename(newModelName, monitor);

			// e) reopen (in UI thread)
			if (reopenModelEditorAfterRename) {
				UIHelper.runUIAsync(new Runnable() {
					/*
					 * (non-Javadoc)
					 * 
					 * @see java.lang.Runnable#run()
					 */
					public void run() {
						Map<String, String> parameters = new HashMap<String, String>();
						parameters.put(OpenModelHandler.PARAM_MODEL_NAME, newModelName);
						UIHelper.runCommand(OpenModelHandler.COMMAND_ID, parameters);
					}
				});
			}
		}
	};
	final IRunnableContext ctxt = new ProgressMonitorDialog(UIHelper.getShell());
	try {
		ctxt.run(true, false, operation);
	} catch (InvocationTargetException | InterruptedException e) {
		throw new ExecutionException(e.getMessage(), e);
	}
          }
     }
     return null;
 }
 
Example 10
Source File: CloneModelHandlerDelegate.java    From tlaplus with MIT License 4 votes vote down vote up
/**
 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
 */
public Object execute(ExecutionEvent event) throws ExecutionException {
	final TLCSpec spec = ToolboxHandle.getCurrentSpec().getAdapter(TLCSpec.class);
	
	Model model = null;
	/*
	 * First try to get the model from the parameters. It is an optional
	 * parameter, so it may not have been set.
	 */
	final String paramModelName = (String) event.getParameter(PARAM_MODEL_NAME);
	final String paramForeignModelName = (String) event.getParameter(PARAM_FOREIGN_FULLY_QUALIFIED_MODEL_NAME);
	final boolean isForeignClone = (paramForeignModelName != null);
	if (paramModelName != null) {
		// The name is given which means the user clicked the main menu
		// instead of the spec explorer. Under the constraint that only ever
		// a single spec can be open, lookup the current spec to eventually
		// get the corresponding model.
		model = spec.getModel(paramModelName);
	} else if (isForeignClone) {
		model = TLCModelFactory.getByName(paramForeignModelName);
	} else {
		/*
		 * No parameter try to get it from active navigator if any
		 */
		final ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
		if (selection != null && selection instanceof IStructuredSelection) {
			// model
			model = (Model) ((IStructuredSelection) selection).getFirstElement();
		}
	}

	if (model != null) {
		final InputDialog dialog = new InputDialog(UIHelper.getShellProvider().getShell(), "Clone model...",
				"Please input the new name of the model", spec.getModelNameSuggestion(model), new ModelNameValidator(spec));
		dialog.setBlockOnOpen(true);
		if (dialog.open() == Window.OK) {
			final String chosenName = dialog.getValue();

			if (isForeignClone) {
				if (model.copyIntoForeignSpec(spec, chosenName) == null) {
					throw new ExecutionException("Failed to copy with name " + chosenName
													+ " from model " + model.getName() + " in spec " + model.getSpec().getName());
				}
			} else {
				if (model.copy(chosenName) == null) {
					throw new ExecutionException("Failed to copy with name " + chosenName + " from model " + model.getName());
				}
			}

			// Open the previously created model
			final Map<String, String> parameters = new HashMap<String, String>();
			parameters.put(OpenModelHandler.PARAM_MODEL_NAME, chosenName);
			UIHelper.runCommand(OpenModelHandler.COMMAND_ID, parameters);
		}
	}
	return null;
}
 
Example 11
Source File: NewFolderAction.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the new name to be given to the target resource.
 * 
 * @param container
 *            the container to query status on
 * @return the new name to be given to the target resource.
 */
private String queryNewResourceName( final File container )
{
	final IWorkspace workspace = ResourcesPlugin.getWorkspace( );

	IInputValidator validator = new IInputValidator( ) {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
		 */
		public String isValid( String string )
		{
			if ( string == null || string.length( ) <= 0 )
			{
				return Messages.getString( "NewFolderAction.emptyName" ); //$NON-NLS-1$
			}

			File newPath = new File( container, string );

			if ( newPath.exists( ) )
			{
				return Messages.getString( "NewFolderAction.nameExists" ); //$NON-NLS-1$
			}

			IStatus status = workspace.validateName( newPath.getName( ),
					IResource.FOLDER );

			if ( !status.isOK( ) )
			{
				return status.getMessage( );
			}
			return null;
		}
	};

	InputDialog dialog = new InputDialog( getShell( ),
			Messages.getString( "NewFolderAction.inputDialogTitle" ), //$NON-NLS-1$
			Messages.getString( "NewFolderAction.inputDialogMessage" ), //$NON-NLS-1$
			"", //$NON-NLS-1$
			validator );

	dialog.setBlockOnOpen( true );
	int result = dialog.open( );
	if ( result == Window.OK )
	{
		return dialog.getValue( );
	}
	return null;
}