Add style to CompletionProposal in Eclipse RCP (Content assist) - java

I'm creating a custom content assist for an editor, this is how I'm creating the proposals:
#Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
String test = "Test";
ContextInformation contextInfo = new ContextInformation("Context display test", "information display test");
CompletionProposal proposal = new CompletionProposal(test,
offset,
0,
test.length(),
Activator.getImage("icons/sample.png"),
test,
contextInfo,
"Additional info");
return new ICompletionProposal[] {proposal};
}
This is the result:
Which is fine, but for example, in the content assist of the Java editor, they are using colors such as blue and gray:
I know there is a class called StyledText that could help but I can't find a good example to use it in combination with CompletionProposal.

The extension interface ICompletionProposalExtension6 supports styled display strings. Its sole method getStyledDisplayString() must return a StyledString that is used for display.
Instead of creating an instance of CompletionProposal you would have to implement your own ICompletionProposal that also implements the above mentioned extension. For example:
class StyledCompletionProposal
implements ICompletionProposal, ICompletionProposalExtension6
{
...
#Override
public StyledString getStyledDisplayString() {
return new StyledString("test").append(" [10%]", Styler.QUALIFIER_STYLER);
}
}
In addition, the content assistant must be configured to enable coloured labels. For editors, this is usually done in SourceViewerConfiguration::getContentAssistant:
ContentAssistant contentAssistant = new ContentAssistant();
contentAssistant.enableColoredLabels(true);

Related

How to make multiple windows in Eclipse RCP e4

We want to achieve an RCP application which may have multiple windows (MWindow)
for distinct data. The Windows must be independent (unlike the Eclipse IDE new
window menu entry), but it must be possible to copy & paste, drag & drop things from
one window into another one. Imagine an application like Word where you can
have multiple documents open. We tried various approaches, but it is quiet
difficult to find out the right e4 way:
1. Creating a new E4Application for each window
Our first approach was to create and run a complete new E4Application for each
new window. But this sounds not to be the right e4 way. Also it is buggy: Key
bindings does not work correct and also the LifecycleManager is called for each new
application and therefor for each new window, which should not be.
E4Application application = new E4Application();
BundleContext context = InternalPlatform.getDefault().getBundleContext();
ServiceReference<?> appContextService = context.getServiceReference(IApplicationContext.class);
IApplicationContext iac = (IApplicationContext) context.getService(appContextService);
IWorkbench workbench = application.createE4Workbench(iac, display);
final E4Workbench implementation = (E4Workbench) workbench;
implementation.createAndRunUI(workbench.getApplication());
This seems not the right approach to do it.
2. The Eclipse IDE approach
In the Eclipse IDE you can go to the menu and click Window -> New Window which
will open a complete new top level window. But it is synchronized: Open the
same text file in both windows and editing it in the first one will alter it in
the other one too. Albeit we tried that approach by simply copy and pasting it
from org.eclipse.ui.actions.OpenInNewWindowAction#run():
// Does not work because we do not have the RCP3 workbench in RCP4.
final IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
final IWorkbenchPage activePage = workbenchWindow.getActivePage();
final String perspectiveId;
if (activePage != null && activePage.getPerspective() != null) {
perspectiveId = activePage.getPerspective().getId();
} else {
perspectiveId = workbenchWindow.getWorkbench().getPerspectiveRegistry().getDefaultPerspective();
}
workbenchWindow.getWorkbench().openWorkbenchWindow(perspectiveId, null);
It looks like that the Eclipse IDE uses the RCP3 compatibility layer. We didn't
found a way to obtain the IWorkbench object. Neither by
PlatformUI#getWorkbench(), nor via the application context, nor the bundle
context.
3. Clone the main window
We stumbled upon Opening multiple instances of an MTrimmedWindow complete with perspectives etc
n-mtrimmedwindow-complete-with-perspectives-etc and did a lot of trial and
error and came up with this muddy code:
class ElementCloningBasedCreator {
EModelService models = ...; // injected
MApplication app = ...; // injected
public void openNewWindow() {
MWindow originWindow = (MWindow) models.find("the.main.window.id", app);
MWindow newWindow = (MWindow) models.cloneElement(originWindow, null);
MPerspectiveStack newPerspectiveStack =
(MPerspectiveStack) models.find(the.main.perspective.stack.id, newWindow);
newPerspectiveStack.setParent((MElementContainer) newWindow);
addTo(app, newWindow);
// Clone the shared elements. If we don't do that the rendering somewhere
// deep in the rabbit hole throws assertion erros because the recurisve
// finding of an element fails because the search root is null.
for (final MUIElement originSharedElement : originWindow.getSharedElements()) {
final MUIElement clonedSharedElement = models.cloneElement(originSharedElement, null);
clonedSharedElement.setParent((MElementContainer) newWindow);
newWindow.getSharedElements().add(clonedSharedElement);
}
cloneSnippets(app, originWindow, newPerspectiveStack, newWindow);
newWindow.setContext(createContextForNewWindow(originWindow, newWindow));
newWindow.setToBeRendered(true);
newWindow.setVisible(true);
newWindow.setOnTop(true);
models.bringToTop(newWindow);
}
#SuppressWarnings({ "rawtypes", "unchecked" })
private void addTo(MElementContainer target, MUIElement child) {
child.setParent(target);
target.getChildren().add(child);
}
/**
* Clone each snippet that is a perspective and add the cloned perspective
* into the main PerspectiveStack.
*/
private void cloneSnippets(MApplication app, MWindow originWindow,
MPerspectiveStack newPerspectiveStack, MWindow newWindow) {
boolean isFirstSnippet = true;
for (MUIElement snippet : app.getSnippets()) {
if (ignoreSnippet(snippet)) {
continue;
}
String snipetId = snippet.getElementId();
MPerspective clonedPerspective =
(MPerspective) models.cloneSnippet(app, snipetId, originWindow);
findPlaceholdersAndCloneReferencedParts(clonedPerspective, newWindow);
addTo(newPerspectiveStack, clonedPerspective);
if (isFirstSnippet) {
newPerspectiveStack.setSelectedElement(clonedPerspective);
isFirstSnippet = false;
}
}
}
private boolean ignoreSnippet(MUIElement snippet) {
return !(snippet instanceof MPerspective);
}
private void findPlaceholdersAndCloneReferencedParts(MPerspective clonedPerspective, MWindow newWindow) {
List<MPlaceholder> placeholders =
models.findElements(clonedPerspective, null, MPlaceholder.class, null);
for (MPlaceholder placeholder : placeholders) {
MUIElement reference = placeholder.getRef();
if (reference != null) {
placeholder.setRef(models.cloneElement(placeholder.getRef(), null));
placeholder.getRef().setParent((MElementContainer) newWindow);
}
}
}
}
This code does not really work and we really need some hints/advices how to do
it right, because of the lack of official documentation. The questions open are:
Do we need to clone the shared objects and if not how do we prevent the
errors during rendering)?
We only saw code where the cloned elements are
added to the parent via getChildren().add(), but we found out that the
children din't get the parent automatically and it is null though. Is it the
right pattern to add the parent to the child too?
We have the deep feeling
that we are doing it not right. It looks way too complicated what we do here. Is
there a simpler/better approach?
You can use the EModelService cloneSnippet method to do this.
Design your MTrimmedWindow (or whatever type of window you want) in the Snippets section of the Application.e4xmi. Be sure that the To Be Rendered and Visible flags are checked. You may need to set the width and height bounds (and you may want to set the x and y position as well).
Your command handler to create the new window would simply be:
#Execute
public void execute(EModelService modelService, MApplication app)
{
MTrimmedWindow newWin = (MTrimmedWindow)modelService.cloneSnippet(app, "id of the snippet", null);
app.getChildren().add(newWin);
}

How to validate fields in vaadin made form

I am making a Java project with vaadin. Right now I have a user registration form looking like that:
public class RegistrationComponent extends CustomComponent implements View {
public static final String VIEW_NAME = "Registration";
public RegistrationComponent(){
Panel panel = new Panel("Registration Form");
panel.setSizeUndefined();
FormLayout content = new FormLayout();
CheckBox checkBox1, checkBox2, checkBox3;
checkBox1 = new CheckBox("Check Box 1");
checkBox2 = new CheckBox("Check Box 2");
checkBox3 = new CheckBox("Check Box 3");
checkBox1.setRequired(true);
checkBox2.setRequired(true);
TextField mailTextField = new TextField("Email Address");
TextField passwordTextField = new TextField("Password");
TextField confirmPasswordTextField = new TextField("Confirm Password");
final Button submitButton = new Button("Submit");
content.addComponent(mailTextField);
content.addComponent(passwordTextField);
content.addComponent(confirmPasswordTextField);
content.addComponent(checkBox1);
content.addComponent(checkBox2);
content.addComponent(checkBox3);
content.addComponent(submitButton);
content.setSizeUndefined(); // Shrink to fit
content.setMargin(true);
panel.setContent(content);
setCompositionRoot(panel);
//listeners:
submitButton.addClickListener(new Button.ClickListener() {
public void buttonClick(Button.ClickEvent event) {
//
}
});
}
#Override
public void enter(ViewChangeListener.ViewChangeEvent event){
//
}
}
Of course, the form doesn't do anything other than being displayed.
What I wanna do, is make Vaadin display error messages next to fields if some requirements are not met. The requirements themselves are not that important (lets say I want email field to contain at least 8 characters). What I wanna know, is: is there any simple built-in way to do that? I was here:
https://vaadin.com/api/com/vaadin/data/Validator.html
but I dont understand how to use a validator, or even if that is what I want to use. I've been looking all over google for usage examples, but so far with no success. Thanks for help!
Vaadin 7
The following applies to Vaadin 7. The validate() method has been removed in Vaadin 8.
All Field types in Vaadin implement the Validatable interface which has the addValidator method that accepts an implementation of Validator as parameter.
So to add a validator that checks the length of the value of a TextField, you would do this:
TextField textField = new TextField();
textField.addValidator(
new StringLengthValidator(
"Must be between 2 and 10 characters in length", 2, 10, false));
Vaadin fields have built-in functionality for displaying the validation errors to the user. By default, the field will be highlighted in red and an exclamation mark will appear next to the field, hovering over this will show a more detailed message to the user.
Automatic Validation
By default, the field will now validate on the next server request which contains a changed value for the field to the server. If the field is set to 'immediate', this will happen when the field looses focus. If the field is not immediate, validation will happen when some other UI action triggers a request back to the server.
Explicit Validation
Sometimes, you may want to exercise more control over when validation happens and when validation errors are displayed to the user. Automatic validation can be disabled by setting validationVisible to false.
textField.setValidationVisible(false);
When you are ready to validate the field (e.g. in a button click listener) you can explicitly call the validate (you can also use commit() if it is a buffered field) method on the TextField instance to trigger validation. validate will throw an InvalidValueException if the value is invalid. If you want to use the builtin display of validation errors included in the TextField component you will also have to set validationVisible back to true.
try {
textField.validate();
} catch (Validator.InvalidValueException ex) {
textField.setValidationVisible(true);
Notification.show("Invalid value!");
}
Note that once validationVisbible is set back to true, validation will happen implicitly so you must remember to set it back to false on the next request if you want to maintain explicit control over validation.
Validation Messages
Individual validation messages can be extracted from the instance of Validator.InvalidValueException which is thrown when validate() or commit() is called.
try {
textField.validate();
} catch (Validator.InvalidValueException ex) {
for (Validator.InvalidValueException cause: ex.getCauses()) {
System.err.println(cause.getMessage());
}
}
Validators
Validators implement the Validator interface and there are several useful validators shipped with Vaadin. Check out the API docs for more information on these: https://vaadin.com/api/7.4.5/com/vaadin/data/Validator.html
Custom validators are easy to implement, here is an example taken from the Book of Vaadin:
class MyValidator implements Validator {
#Override
public void validate(Object value)
throws InvalidValueException {
if (!(value instanceof String &&
((String)value).equals("hello")))
throw new InvalidValueException("You're impolite");
}
}
final TextField field = new TextField("Say hello");
field.addValidator(new MyValidator());
field.setImmediate(true);
layout.addComponent(field);
Problem solved,
Apparently I wasn't looking deep enough before. Here it comes:
field.addValidator(new StringLengthValidator("The name must be 1-10 letters (was {0})",1, 10, true));
all details here:
https://vaadin.com/book/-/page/components.fields.html
Vaadin 8
Using Vaadin 8 com.vaadin.data.Binder easily you can validate your fields. See Binding Data to Forms in the manual.
Create a TextField and a binder to validate the text field.
public class MyPage extends VerticalLayout{
TextField investorCode = new TextField();
Binder<MyBean> beanBinder = new Binder<MyBean>();
//Info : MyBean class contains getter and setter to store values of textField.
public MyPage (){
investorCode.addValueChangeListener(e->valueChange(e));
addComponent(investorCode);
bindToBean();
}
private void bindToBean() {
beanBinder.forField(investorCode)
.asRequired("Field cannot be empty")
.withValidator(investorCode -> investorCode.length() > 0,"Code shold be atleast 1 character long").bind(MyBean::getInvestorCode,MyBean::setInvestorCode);
}
//rest of the code .....
private void valueChange(ValueChangeEvent<String> e) {
beanBinder.validate();
}
}
Call validate() from binder will invoke the validation action.
beanBinder.validate();
to validate the filed. You can call this from anywhere in the page. I used to call this on value change or on a button click.

How to dispose of an SWT Font set on a Draw2D Label?

In a GEF editor, I have the following EditPart:
public class MyLabelEditPart extends AbstractGraphicalEditPart {
#Override
protected IFigure createFigure() {
return new Label();
}
#Override
protected void refreshVisuals() {
MyModel model = (MyModel) getModel();
Label figure = (Label) getFigure();
EditPart parent = getParent();
Font font = new Font(Display.getCurrent(), "sansserif", 11, SWT.BOLD);
figure.setFont(font);
figure.setForegroundColor(ColorConstants.darkGray);
figure.setText(model.getValueString());
parent.refresh();
}
All works fine with most models, but - you will have spotted the error already - I never dispose of the font. So, with a large-ish model of 10k+ tokens, this throws an org.eclipse.swt.SWTError: No more handles. At least I think (hope) this is what causes the error.
Now I cannot figure out how to dispose the font, as the figure for the EditPart is a Draw2D Label, not an SWT Widget. How can I make sure the dreaded error can be circumvented?
Apart from Baz' solution to make the font a static field, a good solution is to use a JFace FontRegistry, as detailed in this strangeoptics blog post.
If you want to keep and reuse all your resources ( images, fonts, colors..etc) at one point rather than using registries, follow this windows builder class
http://code.google.com/p/goclipse/source/browse/trunk/goclipse-n/src/org/eclipse/wb/swt/SWTResourceManager.java?r=445

java JComboBox doesn't update (via model)

I've created a small application that reads data about university subjects from an XML file.
I can add new subjects, etc.
My problem however is that I have another frame with a JComboBox in it which is filled only via one method, which is called whenever I make a change (either reading from an XML file or adding a subject which is then added to my JTable and the XML file).
When I initially start my application it will automatically open a default XML file and read the contents, and add every subject to the combo box, just as I want. However, subsequent calls (like adding a subject or opening a new file) seem to have no effect, whatsoever.
The code in question:
public void fillComboBoxSubject(ArrayList<Subject> subjectList)
{
DefaultComboBoxModel<String> cbm = new DefaultComboBoxModel<String>();
for ( Subject subject : subjectList )
{
cbm.addElement( subject.getName() ); //getName() returns a String
System.out.println(subject.getName());
}
comboBoxSubject.setModel(cbm);
}
The println will display me every single subject if I open a new file, the combo box is not updated, though.
Regards,
LML
edit: SSCCE
Contains all appearances of the combo box:
public class FEnterMark extends JFrame
{
private JComboBox<String> comboBoxSubject;
public FEnterMark()
{
comboBoxSubject = new JComboBox<String>();
comboBoxSubject.setBounds(83, 8, 140, 20);
contentPane.add(comboBoxSubject);
}
public void fillComboBoxSubject(ArrayList<Subject> subjectList)
{
DefaultComboBoxModel<String> cbm = new DefaultComboBoxModel<String>();
for ( Subject subject : subjectList )
{
cbm.addElement( subject.getName() );
System.out.println(subject.getName());
}
comboBoxSubject.setModel(cbm);
}
}

how to write lwuit radio button code

I have my form Welcome on this form i have two radio buttons-Verification and enrollment and a OK button .when user select one of radio buttons and press OK then a form will show but i am not able to do that. Please help.
this is my Statemachine class code:
package userclasses;
import generated.StateMachineBase;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.Form;
import com.sun.lwuit.util.Resources;
public class StateMachine extends StateMachineBase implements ActionListener {
Resources resources;
RadioButton Verification = new RadioButton("Verification");
RadioButton Enrollment = new RadioButton("Enrollment");
StateMachineBase cl = new StateMachineBase() { };
com.sun.lwuit.ButtonGroup bg=new ButtonGroup();
Form fo, f;
public StateMachine(String resFile) {
super(resFile);
// do not modify, write code in initVars and initialize class members there,
// the constructor might be invoked too late due to race conditions that might occur
}
/**
* this method should be used to initialize variables instead of
* the constructor/class scope to avoid race conditions
*/
StateMachine()
{
try {
resources = Resources.open("/NEW AADHAR.res");
}
catch(java.io.IOException err) {
err.printStackTrace();
}
cl.setHomeForm("Welcome");
fo = (Form)cl.startApp(resources,null,true);
f = cl.findWelcome(fo);
//fo.addCommandListener(this);
Verification = cl.findRadioButton1(f);
Enrollment = cl.findRadioButton(f);
f.addComponent(Verification);
f.addComponent(Enrollment);
//fo.addComponent(bg,null);
bg.add(Enrollment);
bg.add(Verification);
Verification.addActionListener(this);
Enrollment.addActionListener(this);
}
protected void initVars() { }
protected void onWelcome_OKAction(Component c, ActionEvent event) { }
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
protected boolean onWelcomeEXIT() {
// If the resource file changes the names of components this call will break notifying you that you should fix the code
boolean val = super.onWelcomeEXIT();
return val;
}
protected void onWelcome_ButtonAction(Component c, ActionEvent event) {
// If the resource file changes the names of components this call will break notifying you that you should fix the code
super.onWelcome_RadioButton1Action(c, event);
super.onWelcome_RadioButtonAction(c, event);
if(Verification.hasFocus()) {
showForm("Login",null);
}
else if(Enrollment.hasFocus()) {
showForm("Authentication",null);
}
else {
Dialog.show("INFORMATION","Please select option","OK","CANCEL");
}
}
|
When you generate a netbeans project from the GUI builder the src folder will now contain the res file you need to work with. Whenever you modify the GUI code that StateMachineBase will be regenerated so you can just rename the components in the GUI builder (you can do this by clicking on the tree node and pressing F2 or by selecting the name attribute in the properties table).
The properties table allows you to assign an event for every component that supports it (e.g. radio button action events) which will generate the appropriate callback method in the StateMachine class (write your code only in the StateMachine class).
Radio buttons can be associated with one group by giving them the same group name.
The easiest way to do it is to use Resource Editor. Simply run it from LWUIT/util directory.
To create project using this tool follow each step from this video: http://www.youtube.com/watch?v=HOfb8qiySd8. Be sure to watch it to the end.
It will create 4 Netbeans projects (ProjectName, ProjectName_Desktop, ProjectName_MIDP, ProjectName_RIM). Fix depedencies (most important for ProjectName and _MIDP one) and you can start coding.
File StateMachineBase.java will be located in 'generated' package, which means that it will be regenerated every time you change something in Resource Editor.
Implement everything in StateMachine class ('userclasses' package), but don't create new methods there, use Resource Editor to create them for you: Resource Editor -> GUI Builder (tab on left side) -> Select component -> Events (tab on the right).
Now, if you want to do something for example, you want to change TextField value, you will write something like this:
protected boolean onUstawieniaKontoZapisz() {
// If the resource file changes the names of components this call will break notifying you that you should fix the code //this comment was generated
boolean val = super.onUstawieniaKontoZapisz(); //generated
Form current = Display.getInstance().getCurrent();
TextField login = findUstawieniaKontoLoginTextField(current); //TextField name in Editor is: 'UstawieniaKontoLoginTextField' - a bit long I know, but it's unique
TextField password = findUstawieniaKontoHasloTextField(current); //same here, 'UstawieniaKontoHasloTextField' is second's TextField name
Configuration.setEmail(login.getText()); //Configuration class is used to store preferences
Configuration.setPassword(password.getText());
return val; //generated
}
You can find all 'find*' methods inside StateMachineBase class. There is one for each Component you have added using Resource Editor (GUI Builder tab).
For grouping radio buttons into groups use Resource Editor too, select each radio button and on Properties tab find 'Group' property. Set it to the same word on every radio button you want to have in the same group.

Categories

Resources