renameAction = context.new DemarcatingResourceAction(
DevicesMessages.getResourceBundle(),
RESOURCE_PREFIX + "rename.") {
// javadoc inherited
protected void runImpl() {
final ODOMElement selected = getSelectedDevice();
String oldName = getDeviceName(selected);
DeviceRepositoryAccessorManager dram =
context.getDeviceRepositoryAccessorManager();
String newName = getNewName(oldName, dram, false);
if (newName != null) {
UndoRedoManager undoRedoManager =
context.getUndoRedoManager();
try {
// we don't allow device renames to be undoable
undoRedoManager.enable(false);
dram.renameDevice(oldName, newName);
} catch (RepositoryException re) {
EclipseCommonPlugin.handleError(ABPlugin.getDefault(),
re);
throw new UndeclaredThrowableException(re);
} finally {
// re-enable the undo manager
undoRedoManager.enable(true);
}
}
}
/**
* Prompts for a new name for the device.
*
* @param oldName The old name of the device
* @param dram The device repository accessor manager for the device
* repository being edited
* @param hasAdminRights True if the user has admin rights, false
* otherwise
* @return The new name for the device, or null if the request was
* cancelled.
*/
private String getNewName(final String oldName,
final DeviceRepositoryAccessorManager dram,
final boolean hasAdminRights) {
final RE deviceNameMatch =
new RE(DeviceConstants.DEVICE_NAME_REGEXP_STRING);
IInputValidator validator = new IInputValidator() {
public String isValid(String string) {
if (!hasAdminRights && !string.startsWith("_")) {
string = "_" + string;
}
if (string == null || "".equals(string)) {
return RENAME_DEVICE_ERROR_EMPTY;
} else if (string.length() >
DeviceConstants.TEXT_MAX_LENGTH) {
return RENAME_DEVICE_ERROR_LENGTH;
} else if (oldName.equals(string)) {
return RENAME_DEVICE_ERROR_UNCHANGED;
} else if (dram.deviceExists(string)) {
return RENAME_DEVICE_ERROR_EXISTS;
} else if (deviceNameMatch.match(string)) {
if (deviceNameMatch.getParenLength(0) !=
string.length()) {
return RENAME_DEVICE_ERROR_INVALID;
}
} else {
return RENAME_DEVICE_ERROR_INVALID;
}
return null;
}
};
InputDialog dialog = new InputDialog(getShell(),
RENAME_DEVICE_TITLE, RENAME_DEVICE_MESSAGE,
oldName, validator);
dialog.setBlockOnOpen(true);
int result = dialog.open();
// Return the entered name if the dialog was completed
// successfully.
String newName = null;
if (result == Dialog.OK) {
newName = dialog.getValue();
// Add a leading _ if necessary for non-admin users
if (!hasAdminRights && !newName.startsWith("_")) {
newName = "_" + newName;
}
}
return newName;
}
};
// create the move action
moveAction = context.new DemarcatingResourceAction(
DevicesMessages.getResourceBundle(),
RESOURCE_PREFIX + "move.") {
// javadoc inherited
protected void runImpl() {
// get hold of the node that is to be moved
final ODOMElement selected = getSelectedDevice();
// create the dialog that allows the user to select the
// destination for the "move" device action
// we need to filter out the selected node from the
// NodeSelectionDialog as it does not make sense to move the
// selected device to itself or a descendent of itself.
// We can do this by wrapping the hierarchy content provider
// in a FilteringTreeContentProvider that filters out the
// selected device.
ITreeContentProvider filteringContentProvider =
new FilteringTreeContentProvider(
CONTENT_PROVIDER,
new FilteringTreeContentProvider.NodeFilter() {
// To store the filtered nodes
List filtered = new ArrayList();
// javadoc inherited
public Object[] filter(Object[] nodes) {
filtered.clear();
// filter out the selected device if
// it is in the list of nodes
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] != selected) {
filtered.add(nodes[i]);
}
}
return filtered.toArray();
}
});
// create the dialog that allows the user to select the new
// position in the device hierarchy.
NodeSelectionDialog dialog = new NodeSelectionDialog(
displayArea.getShell(),
SELECT_DEVICE_MESSAGE,
context.getDeviceRepositoryAccessorManager().
getDeviceHierarchyDocument().getRootElement(),
filteringContentProvider,
new DeviceHierarchyLabelProvider());
// set the title for the dialog
dialog.setTitle(SELECT_DEVICE_TITLE);
// display the dialog
dialog.open();
// retrieve the selected device
Object[] result = dialog.getResult();
// if the result is null then the user pressed the cancel
// button. However, if it is non null then a selection will
// have been made and the result array should contain 1
// ODOMElement - the device that was selected.
if (result != null) {
// check that the result array contains a single ODOMElement
if (result.length != 1) {
throw new IllegalStateException(
"Expected a single device to be selected" +
" but got " + result.length);
}
// cast to an ODOMelement
ODOMElement parent = (ODOMElement) result[0];
// finally move the selected device to the new location
DeviceRepositoryAccessorManager dram =
context.getDeviceRepositoryAccessorManager();
try {
dram.moveDevice(getDeviceName(selected),
getDeviceName(parent));
} catch (RepositoryException e) {
EclipseCommonPlugin.handleError(ABPlugin.getDefault(),
e);
throw new UndeclaredThrowableException(e);
}
// ensure that device that has been moved is selected
treeViewer.setSelection(new StructuredSelection(selected));
}
}
};
// create the remove action
deleteAction = context.new DemarcatingResourceAction(
DevicesMessages.getResourceBundle(),
RESOURCE_PREFIX + "delete.") {
// javadoc inherited
protected void runImpl() {
// get hold of the selected device
ODOMElement selected = getSelectedDevice();
String message = MessageFormat.format(
DELETE_DEVICE_MESSAGE,
new String[]{getDeviceName(selected)});
if (MessageDialog.openQuestion(getShell(),