📄 symboldefinitionspreferencepage.java
字号:
/**
* @see org.eclipse.jface.preference.PreferencePage#performDefaults()
*/
protected void performDefaults() {
// Force a reload of the symbol definitions registry
reloadSymbolDefinitionsRegistry();
// Refresh all of the viewers
initializeDefinitionsCombo();
}
/**
* @see org.eclipse.jface.preference.PreferencePage#performOk()
*/
public boolean performOk() {
boolean succeeded = false;
commitSymbolDefinitions();
try {
SymbolDefinitionSetRegistry.singleton.store();
succeeded = true;
} catch (PersistenceException e) {
handleException("Error storing symbol definitions", e);
} catch (TransformerException e) {
handleException("Error storing symbol definitions", e);
} catch (IOException e) {
handleException("Error storing symbol definitions", e);
}
return succeeded && super.performOk();
}
/**
* Commit the contents of the current symbol definitions back
* to the model object.
*/
public void commitSymbolDefinitions() {
commitSymbolDefinitions(getSelectedSymbolDefinitionSet());
}
/**
* Commit the contents of the current symbol definitions back
* to the model object.
*
* @param set
*/
private void commitSymbolDefinitions(SymbolDefinitionSet set) {
// Save the current definitions
if (set != null) {
String[] defs =
(String[]) currentDefinitions.toArray(new String[currentDefinitions.size()]);
set.setDefinitions(defs);
}
}
/**
* Create the table viewer.
*
* @param parent
*/
private TableViewer createTableViewer(Composite composite) {
int styles =
SWT.SINGLE | SWT.V_SCROLL |
SWT.BORDER | SWT.FULL_SELECTION;
final Table table = new Table(composite, styles);
table.setHeaderVisible(true);
table.setLinesVisible(true);
// Wire up the viewer
TableViewer viewer = new TableViewer(table);
viewer.setContentProvider(new TableContentProvider());
viewer.setLabelProvider(new StringLabelProvider());
viewer.setSorter(new ViewerSorter());
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
updateButtonEnablement();
}
});
// Set up this table column
final TableColumn column = new TableColumn(table, SWT.LEFT);
column.setText("Symbol");
// Attach a listener for resize events
table.addControlListener(new ControlAdapter() {
/**
* Sent when the size (width, height) of a control changes.
* The default behavior is to do nothing.
*
* @param e an event containing information about the resize
*/
public void controlResized(ControlEvent e) {
// Always force our single column to be the full width
// of the table.
column.setWidth(table.getSize().x);
}
});
// Wire up the cell modification handling
final TextCellEditor cellEditor = new TextCellEditor(table);
cellEditor.setValidator(new SymbolNameCellEditorValidator());
cellEditor.addListener(new ICellEditorListener() {
public void applyEditorValue() {}
public void cancelEditor() {}
public void editorValueChanged(boolean oldValidState, boolean newValidState) {
if (!newValidState) {
setErrorMessage(cellEditor.getErrorMessage());
} else {
setErrorMessage(null);
}
}
});
viewer.setCellModifier(new CellModifier());
viewer.setColumnProperties(PROPERTIES);
viewer.setCellEditors(new CellEditor[] {
cellEditor,
});
return viewer;
}
/**
* Return the currently selected symbol definition set or <code>null</code>
* if no set has been selected.
*
* @return
*/
private SymbolDefinitionSet getSelectedSymbolDefinitionSet() {
SymbolDefinitionSet set = null;
IStructuredSelection selection =
(IStructuredSelection) definitionsComboViewer.getSelection();
if (selection.size() == 0) {
set = null;
} else {
set = (SymbolDefinitionSet) selection.getFirstElement();
}
return set;
}
/**
* Return the currently selected symbol or <code>null</code> if
* no symbol is selected.
*
* @return
*/
private String getSelectedSymbol() {
String symbol = null;
IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
if (selection != null) {
symbol = (String) selection.getFirstElement();
}
return symbol;
}
/**
* The add set button has been selected.
*/
private void handleAddSetButton() {
try {
String newSetName = definitionsComboViewer.getCombo().getText();
SymbolDefinitionSet set =
SymbolDefinitionSetRegistry.singleton.addNewDefinitionSet(newSetName);
definitionsComboViewer.refresh();
definitionsComboViewer.setSelection(new StructuredSelection(set));
tableViewer.setInput(set);
} catch (PersistenceException e) {
handleException("Error adding new definition set", e);
}
}
/**
* The add symbol button has been selected.
*/
private void handleAddSymbolButton() {
// Find a new symbol name that doesn't already exist
// in the list
String symbolName = "NewSymbol";
for (int i = 1; i < 100; i++) {
symbolName = "NewSymbol" + i;
if (!currentDefinitions.contains(symbolName)) {
break;
}
}
currentDefinitions.add(symbolName);
tableViewer.refresh();
}
/**
* An exception has occured. Handle it appropriately.
*
* @param t
*/
private void handleException(String message, Throwable t) {
EclipseMECorePlugin.log(IStatus.WARNING, message, t);
MessageDialog.openError(
workbench.getActiveWorkbenchWindow().getShell(),
"Error Occured",
message);
}
/**
* The remove set button has been selected.
*/
private void handleRemoveSetButton() {
SymbolDefinitionSet set = getSelectedSymbolDefinitionSet();
if (set != null) {
SymbolDefinitionSetRegistry registry =
SymbolDefinitionSetRegistry.singleton;
registry.removeDefinitionSet(set);
definitionsComboViewer.refresh();
tableViewer.refresh();
}
}
/**
* The remove symbol button has been selected.
*/
private void handleRemoveSymbolButton() {
Table table = tableViewer.getTable();
int index = table.getSelectionIndex();
if (index != -1) {
currentDefinitions.remove(index);
tableViewer.refresh();
}
}
/**
* Initialize the definitions combo box and selection
*/
private void initializeDefinitionsCombo() {
Object inputObject = new Object(); // Instance doesn't matter for this provider
definitionsComboViewer.setInput(inputObject);
IStructuredContentProvider contentProvider =
(IStructuredContentProvider) definitionsComboViewer.getContentProvider();
Object[] content = contentProvider.getElements(inputObject);
if ((content != null) && (content.length > 0)) {
definitionsComboViewer.setSelection(new StructuredSelection(content[0]), true);
}
}
/**
* Return a boolean indicating whether the specified set name
* is valid.
*
* @param name
* @return
*/
private boolean isValidSetName(String name) {
boolean isValid =
(name != null) &&
(name.trim().length() > 0);
for (int i = 0; isValid && (i < name.length()); i++) {
char c = name.charAt(i);
isValid =
(c == ' ') ||
(Character.isLetterOrDigit(c));
}
return isValid;
}
/**
* Return a boolean indicating whether the specified symbol
* is valid.
*
* @param symbol
* @return
*/
private boolean isValidSymbol(String symbol) {
boolean valid = false;
if (symbol != null) {
Matcher matcher = WHITESPACE_PATTERN.matcher(symbol);
valid = !matcher.matches();
}
return valid;
}
/**
* Reload the symbol definitions registry from disk.
*/
private boolean reloadSymbolDefinitionsRegistry() {
boolean succeeded = true;
try {
SymbolDefinitionSetRegistry.singleton.load();
} catch (PersistenceException e) {
succeeded = false;
handleException("Error reloading symbol definitions", e);
}
return succeeded;
}
/**
* Update the enablement of the add/remove buttons.
*/
private void updateButtonEnablement() {
String typedSetName = definitionsComboViewer.getCombo().getText();
SymbolDefinitionSet typedSet = null;
try {
typedSet = SymbolDefinitionSetRegistry.singleton.getSymbolDefinitionSet(typedSetName);
} catch (PersistenceException e) {
// This should be safe to ignore...
}
SymbolDefinitionSet selectedSet = getSelectedSymbolDefinitionSet();
addSetButton.setEnabled(
isValidSetName(typedSetName) &&
(selectedSet == null) &&
(typedSet == null));
removeSetButton.setEnabled(selectedSet != null);
String selectedSymbol = getSelectedSymbol();
addSymbolButton.setEnabled(selectedSet != null);
removeSymbolButton.setEnabled(selectedSymbol != null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -