Identification of code in eclipse helios - java

How is possible to identify if a selected code is a method, a function, a variable...???
public class Modifiers implements IObjectActionDelegate{
private Shell shell;
public void run(IAction action) {
SelectedText selectedText;
IEditorPart editor = getActiveEditor();
if (editor instanceof AbstractTextEditor) {
selectedText = getSelectedText(editor);
//HOW TO IDENTIFY THE SELECTED CODE
}
}
public void selectionChanged(IAction action, ISelection selection) {
}
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}
private IEditorPart getActiveEditor() {
return Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getActiveEditor();
}
private SelectedText getSelectedText(IEditorPart editor) {
SelectedText selectedText;
try {
final ISelection selection = editor.getEditorSite().getSelectionProvider().getSelection();
final ITextSelection textSelection = (ITextSelection) selection;
selectedText = new SelectedText(textSelection.getText(), textSelection.getOffset(), textSelection.getLength());
} catch (Exception e) {
selectedText = new SelectedText("", 0, 0);
}
return selectedText;
}
}
As you can see I have the selected code in selectedText. Now I want to know is how can I identify if the code in that variable is a method, a variable or whatever it contains.
When doing Refactors with eclipse it shows the code information you have selected. The idea is to do something like that.
Thanks for your help.

If i am correct,you need to simply move your mouse pointer on the code about which you want to get details, it will display a pop up window containing the details with specific symbol such as for static variable it will display 's' in that symbol or icon.
See carefully each symbol containing the different 2 letter and color has its own meaning.
i.e. green for public, red for private and gray for local etc.

Related

TableViewer does not update the default value of a cell

I have a UI bug in a legacy code in our Java project. We display a table, with three columns (HumanReadable, name and value) in a window. In that window, users can click on each cell and update the values. Before that, user clicks the "add" button to add a new row (three new cells). Each cell has a default value, until the user decides to update the value. Now, when the users decides to update the value of the cell, he clicks on the cell and types in the value. The bug is that, once done editing, it keeps the default value in the UI. In the backend, the value has changed (if you click the cell again, it will go into editing mode and show you the value).
I uploaded a short GIF that shows the issue and can be found here.
In that GIF you can see that I updated the default value of the first column to be test. Then I click some other place (to exit the edit mode) and it showed the default value instead of test in the first column.
The method that creates the table:
private void createTable(final Composite parent) {
final Table varTable = new Table(parent, SWT.MULTI);
varTable.setHeaderVisible(true);
varTable.setLinesVisible(true);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(varTable);
varTableViewer = new TableViewer(varTable);
final DataBindingContext bindingContext = new DataBindingContext();
final TableViewerColumn col1 = GuiUtils.createTableColumn(varTableViewer, "Human Readable");
col1.setEditingSupport(new StringEditingSupport(varTableViewer, bindingContext, dataProperty));
col1.getColumn().setWidth(120);
final TableViewerColumn col2 = GuiUtils.createTableColumn(varTableViewer, "Name");
col2.getColumn().setWidth(120);
col2.setEditingSupport(new StringEditingSupport(varTableViewer, bindingContext, nameProperty));
final TableViewerColumn col3 = GuiUtils.createTableColumn(varTableViewer, "Value");
col3.setEditingSupport(new StringEditingSupport(varTableViewer, bindingContext, valueProperty));
KeyBoardNavigationSupport.createSupport(varTableViewer);
input = new WritableList(globalVars, FlowVar.class);
ViewerSupport.bind(varTableViewer, input, BeanProperties.values(new String[] { dataProperty, nameProperty, valueProperty }));
}
The StringEditingSupport class:
public class StringEditingSupport extends ObservableValueEditingSupport {
private class CellEditorPrintValidatorErrors extends TextCellEditor {
public CellEditorPrintValidatorErrors(Composite control) {
super(control);
}
#Override
protected void focusLost(){
if(this.getErrorMessage() != null) {
MessageDialog.openError(this.getControl().getShell(), "Invalid input", this.getErrorMessage());
}
}
}
private final CellEditor cellEditor;
String propertyName;
public StringEditingSupport(final ColumnViewer viewer, final DataBindingContext dbc, final String propertyName) {
super(viewer, dbc);
cellEditor = new TextCellEditor((Composite) viewer.getControl());
this.propertyName = propertyName;
}
public StringEditingSupport(final ColumnViewer viewer, final DataBindingContext dbc, final String propertyName, final ICellEditorValidator validator) {
super(viewer, dbc);
cellEditor = new CellEditorPrintValidatorErrors((Composite) viewer.getControl());
cellEditor.setValidator(validator);
this.propertyName = propertyName;
}
#Override
protected IObservableValue doCreateCellEditorObservable(final CellEditor cellEditor) {
return SWTObservables.observeText(cellEditor.getControl(), SWT.Modify);
}
#Override
protected IObservableValue doCreateElementObservable(final Object element, final ViewerCell cell) {
return BeansObservables.observeValue(element, propertyName);
}
#Override
protected CellEditor getCellEditor(final Object element) {
return cellEditor;
}
public String getErrorMessage(){
return cellEditor.getErrorMessage();
}
}
I believe it has something to do with the StringEditingSupport class. This class allows to edit the value in each cell of table. But I couldn't figure out a way to "update" the value shown in the GUI. As I understand input (of type WritableList) contains all the information. Here is the add button listener method:
private class AddButtonSelectionListener extends SelectionAdapter {
#Override
public void widgetSelected(final SelectionEvent e) {
String name = nameProperty;
String meaning = dataProperty;
final List<String> names = new ArrayList<String>();
final List<String> meanings = new ArrayList<String>();
for (final Object var : input) {
names.add(((FlowVar) var).getName());
meanings.add(((FlowVar) var).getData());
}
int index = 0;
while (names.contains(name)) {
name = nameProperty + ++index;
}
index = 0;
while (meanings.contains(meaning)) {
meaning = dataProperty + ++index;
}
input.add(new FlowVar(name, valueProperty, meaning));
}
}
So, as I understand, I need to somehow bind the input to the UI (the content of each cell). I did try many attempts like trying to set a listener to the whole table (varTableViewer.addSelectionChangedListener) but none of them worked. Is it possible to suggest a way to solve this kind of issue?
If anything is missing, please let me know and I'll add it.

Passing SWT Control reference to Enum

My Setup
I have some code in an Eclipse RCP application that looks like this (it is in the #PostConstruct method of a Part):
scroll = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
taskingInputsGroup = new Composite(scroll, SWT.NONE);
textSendTime = new Text(taskingInputsGroup, SWT.BORDER);
textSubject = new Text(taskingInputsGroup, SWT.BORDER);
textTaskStartTime = new Text(taskingInputsGroup, SWT.BORDER);
I'm trying to set a private Control field for an Enum's constants to each of these Text objects:
textSendTime = new Text(taskingInputsGroup, SWT.BORDER);
MsgField.SEND_TIME.setControl(textSendTime);
In the Enum, I just have a simple getter/setter for the Control field.
I have a method that is called when a user presses a Button. This method loops through the Enum constants and sets the text of some TreeItems to whatever is in the Control objects:
MsgField[] msgFields= MsgField.values();
for (int i = 0; i < msgFields.length; i++) {
Control control = msgFields[i].getControl();
if (control != null) {
if (control instanceof Text) {
root.getItem(i).getItem(0).setText(((Text)control).getText());
}
}
}
My Question
I am getting empty text from ((Text)control).getText() even if there is text in the Text field. Why could this be? I know I'm overlooking something simple (it's been a long day). I've read a bunch of SO posts on Java passing by value, but I can't seem to apply the answers to this issue. This works fine if I call getText() directly on the object in my view class:
root.getItem(1).getItem(0).setText(textSendTime.getText());
EDIT - Some MsgField enum code:
public enum MsgField {
private boolean isRequiredField;
private String treeName;
public abstract void setValue(Msg msg, String value);
public abstract Object getValue(Msg msg);
private MsgField(boolean req, String name) {
isRequiredField = req;
treeName = name;
}
SEND_TIME("Send Time", true) {
#Override
public void setValue(Msg msg, String value) {
msg.setSendTime(value);
}
#Override
public Object getValue(Msg msg) {
return msg.getSendTime();
}
},
// ....
// other fields
// ....
START_TIME("Start Time", false) {
#Override
public void setValue(Msg msg, String value) {
msg.setStartTime(value);
}
#Override
public Object getValue(Msg msg) {
return msg.getStartTime();
}
};
}
So the idea here is to represent the fields for the Msg class and have these get/set methods for each one so that I can easily iterate over them and get/set the fields for my Msg object. This worked fine (although maybe you can suggest a better alternative), but the trouble came with the addition of that Control field as I mentioned.

How to set Eclipse Help Scope

I have an eclipse plugin where you select text from the editor, press a shortcut(in my case it's alt+F1) and the Eclipse help search opens and automatically searches for that selected text.
Here is the code:
public class Button1 extends AbstractHandler {
public Button1() {}
public String getCurrentSelection()
{
IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (part instanceof ITextEditor)
{
final ITextEditor editor = (ITextEditor) part;
ISelection sel = editor.getSelectionProvider().getSelection();
if (sel instanceof TextSelection)
{
ITextSelection textSel = (ITextSelection) sel;
return textSel.getText();
}
}
return null;
}
public void searchInHelp(String str) {
IWorkbenchHelpSystem help = PlatformUI.getWorkbench().getHelpSystem();
help.search(str);
}
public Object execute(ExecutionEvent event) throws ExecutionException {
String str = getCurrentSelection();
searchInHelp(str);
return null;
}
}
Unfortunately when the search is performed it's performed for all the Eclipse Help while I only want to apply it to a certain scope like, let's say, Workbench User Guide, so that it only searches there.
As far as I've seen in IWorkbenchHelpSystem there is no handling of Help Search Scope, and, from what I've been told, everything that deals with the scope is in an internal package so it is not intended for use by other plugins.
I did found something like this:
String qualifier = ResourcesPlugin.getDescriptor().getUniqueIdentifier();
String key = ResourcesPlugin.PREF_AUTO_BUILDING;
boolean value = true;
IScopeContext context = new ProjectScope(MyProject);
IEclipsePreferences node = context.getNode(qualifier);
if (node != null)
node.putBoolean(key, value);
But I have no idea if this ProjectScope has anything to do with the Eclipse Help Scope or how to correctly integrate it with my code (i.e just add it in a method and call it before doing the search?).
So can anyone help me out on how to set the help scope when performing the search?

Disable modify\delete JTextArea content (but not insert)

I have JTextArea component and I need to disable modify\delete current content in component by users. Users may only add\insert some text at the end, but setText method must work as usual.
tnx
I need to disable modify\delete current content in component by users.
textArea.setEditable( false );
Users may only add\insert some text at the end, but setText method must work as usual.
You should have an "Add Text" button that will take text from a separate text field and then append the text to the Document using the append(...) method of the JTextArea.
Could you post an example of what you already have?
To clarify, if you want users to be unable to certain things, you may need to re-insert the original text manually. I'm unsure of the editor used by a JTextArea, but you could try overriding that.
Horrific code I'm coming up with on the spot incoming, you can probably do this much easier:
private static String mand = "mandatory.";
private static JTextArea test = new JTextArea(mand);
public static String getMand() {
return mand;
}
public static JTextArea getTest() {
return test;
}
public static void setMand(String mand2) {
mand = mand2;
}
public static void setTest(JTextArea test2) {
test = test2;
}
getTest().addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent arg0) {
// do nothing
}
#Override
public void keyReleased(KeyEvent arg0) {
// do nothing
}
#Override
public void keyTyped(KeyEvent arg0) {
if(getTest().getText().startsWith(getMand())) {
System.out.println("good, text still present");
setMand(test.getText());
} else {
getTest().setText(getMand());
}
}
});
WARNING :: if the user makes any mistakes in adding information to the JTextArea, the code will not allow the user to fix these mistakes.
Tested successfully under JDK (/JRE) 7.

Selecting only leafs in JTree

So I want to be able to only select leafs in JTree. There are some solutions online, but they don't work on multiple selection...
What I would like is to find the part of the code that fires when user clicks on a node and modify that part to suit my needs.
I have found a listener within DefaultTreeCellEditor, but that code seem to apply to when only one node is selected at a time...
The bottom line is, where can I find the code that, when nodes gets clicked, decides if it will select it or not, and will it or not deselect all the other selected nodes?
Fixed it!
public class LeafOnlyTreeSelectionModel extends DefaultTreeSelectionModel
{
private static final long serialVersionUID = 1L;
private TreePath[] augmentPaths(TreePath[] pPaths)
{
ArrayList<TreePath> paths = new ArrayList<TreePath>();
for (int i = 0; i < pPaths.length; i++)
{
if (((DefaultMutableTreeNode) pPaths[i].getLastPathComponent()).isLeaf())
{
paths.add(pPaths[i]);
}
}
return paths.toArray(pPaths);
}
#Override
public void setSelectionPaths(TreePath[] pPaths)
{
super.setSelectionPaths(augmentPaths(pPaths));
}
#Override
public void addSelectionPaths(TreePath[] pPaths)
{
super.addSelectionPaths(augmentPaths(pPaths));
}
}

Categories

Resources