@Override protected void doRun(DIModel selection, CDOTransaction transaction, IProgressMonitor monitor) throws CoreException { List<IStatus> failures = Lists.newArrayListWithExpectedSize(1); for (Object next : selection.getChildren()) { if (next instanceof CDOResource) { // get the resource local to this transaction CDOID oid = ((CDOResource) next).cdoID(); CDOResource toRename = (CDOResource) transaction.getObject(oid); if (toRename != null) { try { toRename.setName(getNewName(toRename.getName())); } catch (Exception e) { failures.add( error("Failed to rename resource " + toRename.getPath(), e)); // $NON-NLS-1$ } } } } if (!failures.isEmpty()) { throw new CoreException(wrap(failures, "Errors occurred in renaming model.")); // $NON-NLS-1$ } }
@Override protected boolean gatherInput(DIModel selection) { boolean result = false; InputDialog dialog = new InputDialog( part.getSite().getShell(), Messages.RenameModelAction_1, Messages.RenameModelAction_2, getModelName(selection.getName()), createInputValidator(selection)); if (dialog.open() == Dialog.OK) { newName = getModelName(dialog.getValue().trim()); result = true; } return result; }
private IInputValidator createInputValidator(DIModel model) { final CDOResource primaryResource = model.getResource(); CDOResourceNode parent = primaryResource.getFolder(); if (parent == null) { parent = (CDOResourceNode) primaryResource.eResource(); } final CDOResourceNode container = parent; return new IInputValidator() { @Override public String isValid(String newText) { String result = null; newText = Strings.nullToEmpty(newText).trim(); if (newText.equals("")) { // $NON-NLS-1$ result = Messages.RenameModelAction_7; } else if ((newText.indexOf('/') >= 0) || (newText.indexOf('\\') >= 0)) { result = Messages.RenameModelAction_8; } else if (getModelName(newText).equals(getModelName(primaryResource.getName()))) { // user didn't change the name result = ""; // $NON-NLS-1$ } else { String base = getBaseName(newText); for (CDOResourceNode next : Iterables.filter(container.eContents(), CDOResourceNode.class)) { if (base.equals(getBaseName(next.getName()))) { result = NLS.bind(Messages.RenameModelAction_10, next.getName()); } } } return result; } }; }
@Override protected CDOView getView(DIModel selection) { return selection.getResource().cdoView(); }