📄 devicemanagementpreferencepage.java
字号:
* Create the contents for a device registry exception.
*
* @param parent
* @param e
* @return
*/
private Control createErrorContents(Composite parent, Exception e) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, true));
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
String text =
"An error (" + e.getClass().getName() + ": " +
e.getMessage() + ") occurred reading the device registry.\n" +
"Please consult the error log for more information.";
Text errorText =
new Text(composite, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
errorText.setLayoutData(new GridData(GridData.FILL_BOTH));
errorText.setText(text);
return composite;
}
/**
* Create the devices table viewer.
*
* @param parent
*/
private CheckboxTableViewer createTableViewer(Composite composite) {
int styles =
SWT.MULTI | SWT.V_SCROLL |
SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK;
Table table = new Table(composite, styles);
table.setHeaderVisible(true);
table.setLinesVisible(true);
// Wire up the viewer
CheckboxTableViewer viewer = new CheckboxTableViewer(table);
viewer.setContentProvider(new TableContentProvider());
viewer.setLabelProvider(new DeviceTableLabelProvider());
viewer.addCheckStateListener(new ICheckStateListener() {
public void checkStateChanged(CheckStateChangedEvent event) {
handleCheckStateChange(event);
}
});
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection =
(IStructuredSelection) event.getSelection();
IDevice device = (IDevice) selection.getFirstElement();
editDevice(device);
}
});
IDialogSettings viewerSettings =
EclipseMEUIPlugin.getDialogSettings("deviceManagementViewerSettings");
TableViewerConfiguration viewerConfiguration =
new TableViewerConfiguration(viewerSettings, DEFAULT_TABLE_WIDTH, COLUMN_INFO, 1);
viewerConfiguration.configure(viewer);
viewer.setInput(new Object());
return viewer;
}
/**
* Edit the specified device.
*
* @param device
*/
private void editDevice(IDevice device) {
if (editDelegate != null) {
editDelegate.run(editAction);
viewer.refresh(getSelectedDevice(), true);
}
}
/**
* Return the action delegate for the specified device or <code>null</code>
* if no delegate can be found.
*
* @param device
* @return
*/
private IActionDelegate findActionDelegate(IDevice device) {
IActionDelegate delegate = null;
DeviceEditorConfigElement element = DeviceEditorRegistry.findEditorElement(device);
if (element != null) {
try {
delegate = element.getActionDelegate();
} catch (CoreException e) {
EclipseMECorePlugin.log(IStatus.WARNING, "Error retrieving editor delegate", e);
}
}
return delegate;
}
/**
* Find a new name that is unique using the specified name
* as a base.
*
* @param name
* @return
*/
private String findUniqueName(IDevice device) {
String uniqueName = null;
String groupName = device.getGroupName();
String baseName = device.getName();
for (int i = 1; i <= 100; i++) {
StringBuffer deviceNameBuffer = new StringBuffer(baseName);
Matcher matcher = UNIQUE_NAME_PATTERN.matcher(deviceNameBuffer);
if (matcher.find()) {
int matchStart = matcher.start(1);
int matchEnd = matcher.end(1);
deviceNameBuffer.replace(matchStart, matchEnd, Integer.toString(i));
} else {
deviceNameBuffer.append(" (").append(i).append(")");
}
uniqueName = deviceNameBuffer.toString();
try {
IDevice foundDevice =
DeviceRegistry.singleton.getDevice(groupName, uniqueName);
if (foundDevice == null) {
break;
}
} catch (PersistenceException e) {
EclipseMECorePlugin.log(IStatus.ERROR, "Error finding device", e);
}
}
return uniqueName;
}
/**
* Return the currently selected device.
*
* @return
*/
private IDevice getSelectedDevice() {
return (IDevice) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
}
/**
* The check state has changed, handle appropriately.
*
* @param event
*/
private void handleCheckStateChange(CheckStateChangedEvent event) {
if (!updatingCheckState && event.getChecked()) {
// Switching to checked... Handle appropriately
IDevice newDefault = (IDevice) event.getElement();
DeviceRegistry.singleton.setDefaultDevice(newDefault);
updateCheckboxState();
}
}
/**
* Handle the delete button being selected.
*/
private void handleDeleteButton() {
IStructuredSelection selection =
(IStructuredSelection) viewer.getSelection();
int count = selection.size();
String title = "Confirm Delete";
String message = "Are you sure you want to remove " + count + " device(s)?";
if (MessageDialog.openConfirm(getShell(), title, message)) {
Iterator items = selection.iterator();
while (items.hasNext()) {
IDevice device = (IDevice) items.next();
try {
DeviceRegistry.singleton.removeDevice(device);
} catch (PersistenceException e) {
handleException("Error removing device", e);
}
}
viewer.refresh();
}
}
/**
* Handle the duplicate button being selected.
*/
private void handleDuplicateButton() {
IDevice device = getSelectedDevice();
try {
IDevice clonedDevice = (IDevice) PersistableUtilities.clonePersistable(device);
String uniqueName = findUniqueName(clonedDevice);
clonedDevice.setName(uniqueName);
DeviceRegistry.singleton.addDevice(clonedDevice);
viewer.refresh();
viewer.setSelection(new StructuredSelection(clonedDevice));
} catch (Exception e) {
handleException("Error duplicating device", e);
}
}
/**
* Handle the edit button being selected.
*/
private void handleEditButton() {
IDevice device = getSelectedDevice();
editDevice(device);
}
/**
* Handle the specified exception by displaying to the user and logging.
*
* @param message
* @param throwable
*/
private void handleException(String message, Throwable throwable) {
EclipseMECorePlugin.log(IStatus.WARNING, "Device registry exception", throwable);
EclipseMEUIPlugin.displayError(
getShell(),
IStatus.WARNING,
-999,
"Device Registry Error",
message,
throwable);
}
/**
* Prompt the user to import some devices using
* the import device wizard.
*/
private void importDevices() {
DeviceImportWizard wizard = new DeviceImportWizard();
WizardDialog dialog = new WizardDialog(
workbench.getActiveWorkbenchWindow().getShell(),
wizard);
if (dialog.open() == WizardDialog.OK) {
viewer.refresh();
IDevice defaultDevice = DeviceRegistry.singleton.getDefaultDevice();
if (defaultDevice == null) {
selectStartingDefaultDevice();
updateCheckboxState();
}
}
}
/**
* Select a starting default device from the current
* available input.
*/
private void selectStartingDefaultDevice() {
TableContentProvider contentProvider =
(TableContentProvider) viewer.getContentProvider();
Object[] elements = contentProvider.getElements(viewer.getInput());
if ((elements != null) && (elements.length > 0)) {
DeviceRegistry.singleton.setDefaultDevice((IDevice) elements[0]);
}
}
/**
* Update the enablement of the buttons in the preference page.
*
* @param selection
*/
private void updateButtonEnablement(ISelection selection) {
IDevice device = getSelectedDevice();
editDelegate = (device == null) ? null : findActionDelegate(device);
if (editDelegate != null) {
editDelegate.selectionChanged(editAction, selection);
}
int selectedCount = ((IStructuredSelection) viewer.getSelection()).size();
duplicateButton.setEnabled(selectedCount == 1);
editButton.setEnabled((editDelegate != null) && editAction.isEnabled());
deleteButton.setEnabled(selectedCount > 0);
}
/**
* Update the state of the checkboxes based on the
* default value in the registry.
*/
private void updateCheckboxState() {
updatingCheckState = true;
viewer.setAllChecked(false);
IDevice defaultDevice = DeviceRegistry.singleton.getDefaultDevice();
if (defaultDevice != null) {
viewer.setChecked(defaultDevice, true);
}
updatingCheckState = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -