How to add DropListener to drop text in a draw2d Label - eclipse-gef

I am Trying to add a dropListener so I can Drop and text into a draw2d Label ,in GEf Editor , Can anyone help how Can I do that. An example will be great.

To respond to drop events on a GEF edit part viewer you have to install on the viewer itself an implementation of org.eclipse.jface.util.TransferDropTargetListener that understands transfers of type org.eclipse.swt.dnd.TextTransfer and that creates some kind of org.eclipse.gef.Request that can be handled by an org.eclipse.gef.EditPolicy installed on the target org.eclipse.gef.EditPart.
You have to understand that both the Request and the EditPolicy allow you to customize the drop behavior on a EditPart basis. As a consequence, I can show you an example that is actually fully functional, but feel free to customize it to your real needs.
First create the TransferDropTargetListener:
public class TextTransferDropTargetListener extends AbstractTransferDropTargetListener {
public TextTransferDropTargetListener(EditPartViewer viewer) {
super(viewer, TextTransfer.getInstance());
}
#Override
protected void handleDragOver() {
getCurrentEvent().feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND;
super.handleDragOver();
}
#Override
protected Request createTargetRequest() {
return new ChangeBoundsRequest(REQ_ADD);
}
#Override
protected void updateTargetRequest() {
ChangeBoundsRequest request = (ChangeBoundsRequest) getTargetRequest();
request.setEditParts(Collections.EMPTY_LIST);
request.setLocation(getDropLocation());
}
#Override
protected void handleDrop() {
super.handleDrop();
if (getCurrentEvent().detail != DND.DROP_NONE) {
getViewer().setSelection(StructuredSelection.EMPTY);
getViewer().getControl().setFocus();
}
}
#Override
protected Command getCommand() {
String text = (String) getCurrentEvent().data;
List<IEntityPart> editParts = new ArrayList<IEntityPart>();
//
// using the 'text' variable you have to create
// a new EditPart that would eventually replace the old one.
//
editParts.add(createNewLabelPart());
ChangeBoundsRequest request = (ChangeBoundsRequest) getTargetRequest();
request.setEditParts(editParts);
return super.getCommand();
}
}
then install the listener in the graphical viewer constructor using the following statement:
addDropTargetListener(new TextTransferDropTargetListener(this));
finally ensure that an EditPolicy that understands requests of type REQ_ADD (maybe you already added one that extends LayoutEditPolicy or ContainerEditPolicy) is installed on the target EditPart, which is usually done in the AbstractEditPart.createEditPolicies().
To better understand the chain of responsibilities, I suggest you to have a look at the super implementation of the TransferDropTargetListener.getCommand() method.

Related

Automatic horizontal scroll on javafx TableView

If I scroll on my table to the right it will automatically scroll back to the left. I have no own implementation for scrolling, all is done by the TableView. Has anyone seen this before? If I test it on windows I have no issue.
compile "org.javafxports:jfxdvk:8.60.13"
javafxportsVersion = '8.60.13'
This issue is more related to javafx. If the "IS_TOUCH_SUPPORTED" flag is set the scroll bar is set to visible false after use (VirtualFlow). A change listener onVisible does then a updateHbar(). And because of hbar.isVisible() the scroll var value is set to 0.
I did not want to change "IS_TOUCH_SUPPORTED". That's why I create a own skin which only creates a custom virtual flow. The custom virtual flow is also very simple I only set the tempVisibility to true via reflection. And I also overwrite the startSBReleasedAnimation() method and do nothing inside.
public class CustomVirtualFlow<T extends IndexedCell<?>> extends VirtualFlow<T> {
public CustomVirtualFlow() {
super();
try {
final Field field = VirtualFlow.class.getDeclaredField("tempVisibility");
field.setAccessible(true);
field.setBoolean(this, true);
} catch (final Exception e) {
}
}
/*
* (non-Javadoc)
*
* #see com.sun.javafx.scene.control.skin.VirtualFlow#startSBReleasedAnimation()
*/
#Override
protected void startSBReleasedAnimation() {
// do not call the super.startSBReleasedAnimation()
}

Conditional AJAX confirm with a new value in Wicket

I need to solve a simple problem, but yet I have not been able to found out any solution yet.
I have a simple DropDownChoice with AJAX onChange() JS event. I need to add a confirm box before the onUpdate() action is done - this is not difficult, BUT I need to display the confirm box only if the new selected value of the DropDownChoice is X (one certain value), and do not display the confirm box in any other case. Is it doable?
Short example snippet:
DropDownChoice<Integer> choice = new DropDownChoice<Integer>("id", new Model<Integer>(0));
choice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
// do some stuff
}
#Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.getAjaxCallListeners().add(new AjaxCallListener() {
#Override
public CharSequence getPrecondition(Component component) {
return "return confirm('Really?')"; // I NEED THIS DISPLAYED CONDITIONALLY
}
}
}
}
I don't know how to access the "choice" model object (converted input...) with the proposed value to add it to a condition in updateAjaxAttributes() method.
Thank you.
I think you should go for a JavaScript-based solution. The code of AJAX call listener is executed in a scope where you can use variable attrs. This variable contains the parameters used to perform AJAX call, including the id of the component. In this way you could check for selected value.
See more at http://wicket.apache.org/guide/guide/ajax.html#ajax_5

Change themes in Vaadin 7 via code

I am doing a project in Vaadin 7. In that I need to change the theme of a page.
In Vaadin 6, there is a function called 'setTheme()'. so that I can change the theme using that function wherever I want in my code.
But, In Vaadin 7, I couldn't find any like that.
I know there will be a way to do it.
And also how to apply changes on the UI when I change a theme?
Will it be changed automatically? (or)
ICEPush gonna help me?
In Vaadin 7 the method 'setTheme()' has been replaced with the new Annotation #Theme. The "on the fly theme change" is not possible in Vaadin 7.
There is a disucssion in this Vaadin Forum Thread about the on fly theme change in Vaadin 7. You should have a look on it.
setTheme functionality has been introduced in Vaadin 7.3.0: https://vaadin.com/wiki/-/wiki/Main/Changing+theme+on+the+fly
you can try this for Vaadin 7:
Create your own UIProvider
Register your UIProvider in root UI
Switch theme in UIProvider and trigger page reload
DynamicThemeUIProvider.java
public class DynamicThemeUIProvider extends UIProvider {
private String currentTheme = "reindeer";
#Override
public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
return DemoUI.class;
}
public void setTheme(String theme) {
currentTheme = theme;
}
public String getTheme(UICreateEvent event) {
return currentTheme;
}
}
DemoUI.java
public class DemoUI extends UI {
private DynamicThemeUIProvider provider;
#Override
protected void init(VaadinRequest request) {
provider = new DynamicThemeUIProvider();
getSession().addUIProvider(provider);
}
public DynamicThemeUIProvider getDynamicThemeUIProvider() {
return provider;
}
}
Then on a component which switches the theme:
#Override
public void valueChange(ValueChangeEvent event) {
DemoUI ui = (DemoUI) getUI();
DynamicThemeUIProvider uiProvider = ui.getDynamicThemeUIProvider();
if (uiProvider == null) {
return;
}
uiProvider.setTheme("reindeer");
try {
String value = (String) event.getProperty().getValue();
uiProvider.setTheme(value.toLowerCase());
} catch (Exception e) {
e.printStackTrace();
}
ui.getPage().getJavaScript().execute("document.location.reload(true)"); // page refresh
}
Since I used custom themes, I have made it pretty simple. I used a toggle button and executed the required piece of code every time.
JavaScript.getCurrent().execute("document.body.className = document.body.className.replace(\"theme1\",\"theme2\"); ");
JavaScript.getCurrent().execute("document.body.className = document.body.className.replace(\"theme2\",\"theme1\"); ");
My css file will be like this.
.theme1 .v-button {
/* some css attribute */
}
.theme2 .v-button {
/* some css attribute */
}
Believe me; the theme switch is very very fast since the browser itself do the trick to switch the theme rather than asking the Vaadin server to do the switch.
Regarding themes for charts:
simply have a switch somewhere inside a listener of either a ComboBox or an OptionGroup (for radio buttons) to make a the following ChartOptions static method call, e.g.:
ChartOptions.get().setTheme(new VaadinTheme())
then
ChartOptions.get().setTheme(new SkiesTheme())
etc.
there's also GridTheme(); GrayTheme() and HighChartsDefaultTheme(); you can even extend the base theme to create your own theme (look that up in the Book of Vaadin).
Since Vaadin 7.3 you can use UI#setTheme()
In Vaadin 7 and higher Versions we have an Annotation called #Theme(yourThemeName)
based on the Theme name which you give here it will redirect to that specific .scss Style.This annotation is called before the Init method is called.

lwuit change UI language

I use codenameone to develop my mobile application. In this application I implement some classes and codes manually for instance create all forms by hard coding not using codenameone designer for some reason.
By the way I wanted to navigate in forms like what codenameone use, so I use one variable from type of Form called it prevForm and when I want to open a form I set it to current form and then I show new form.
Ok, that is main scenario. In this application I wanna implement internationalization too, so I create my own hashtable (Farsi and English) for this application.
This is my problem:
How can I set or change language and apply it to forms that I opened?
Is my method for navigate between forms are good?
Here is my code:
public class BaseForm extends Form implements ActionListener {
public BaseForm(){
this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
}
Command exit, ok, back;
Form prevForm;
protected void initForm(){
}
protected void showForm(){
}
protected void showForm(final Form prevForm){
//String name = this.getName();
//if("Reminder".equals(name) || "3Transaction".equals(name))
{
this.prevForm = prevForm;
Form f = this;
back = new Command("Back");
//ok = new Command("Ok");
//delete = new Command("Delete");;
Button button = new Button("Button");
f.addCommand(back);
//f.addCommand(ok);
//f.addCommand(delete);
//f.addComponent(button);
f.addCommandListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getCommand().equals(back)) {
//Do Exit command code
System.out.println("Back pressed");
prevForm.showBack();
} else if (ae.getCommand().equals(ok)) {
//Do Start command code
System.out.println("Ok pressed");
}
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Do button code
System.out.println("Action performed");
}
});
}
showForm();
}}
for open nested form I use this code:
LanguageUI lang = new LanguageUI();
lang.showForm(this);
change language [form]:
protected boolean onBtnSave() {
if(isRbFarsiSelected()){
UIManager.getInstance().setResourceBundle(new CommonSettings().getFarsi());
}
else {
UIManager.getInstance().setResourceBundle(new CommonSettings().getEnglish());
}
return false;
}
I also hard code my UI on lwuit, and i have a variable parentForm on every class so i can easily show previous form. For language change i know there is Localization in the resource editor that you can make use of. Below is how you can access it. I guess the trick is how to set the content of the L10N in the res file in code? On the other hand you can create your own helper classes that mirror the methods below.
Resources theme = Resources.open("/theme.res");
theme.getL10N(id, locale);
theme.getL10NResourceNames();
theme.isL10N(name);
theme.listL10NLocales(id)

GWT - Handling events from underlying widgets in EntryPoint

I have searched the web for the correct answer, but I've been failing to achieve this :
In EntryPoint class, I need to manage widgets according to events that occur in nested widgets. I've cleaned of the code to focus only on what is important here.
I have built a few UiBinder widgets, for example, a Login pane where the user can enter his credentials. In my EntryPoint class, I add the widgets in the correct position.
// This is from EntryPoint class
public void onModuleLoad() {
LoginPane lp = new LoginPane();
RootPanel.get("headerRightPane").add(lp);
lp.setFocus();
// Other widgets added in same manner after this point...
}
I would like a successful login to remove the LoginPane and replace it by another widget (AccountPane) that would show the account information for the user that is logged in. I have an onClick event, in LoginPane, that sends a request to a fully functional Servlet that checks the credentials. At this exact point, if the Servlet determines that the login is indeed successful, I would like to fire a "successfulLogin" event (from LoginPane) that could notify the EntryPoint class that the LoginPane can now be replaced by the AccountPane.
// This is from LoginPane class
#UiHandler("loginButton")
void onClick(ClickEvent e) {
checkCredentials(usernameField.getText(), passwordField.getText());
}
public void checkCredentials(String username, String password) {
String usernameToServer = username;
String passwordToServer = password;
credentialsService.credentialsServer(usernameToServer, passwordToServer,
new AsyncCallback<CredentialsPaneContent>() {
public void onFailure(Throwable caught) {
answerLabel.setText(Utilities.SERVER_ERROR);
}
public void onSuccess(CredentialsPaneContent result) {
if ( result == null ) {
answerLabel.setText("Login Failed.");
} else {
// Fire event here (to be caught by EntryPoint class)
answerLabel.setText("Login Successful.");
}
}
});
}
So, the question : How should I proceed to create, fire and listen to the event from my nested widget?
Use an EventBus. Additionally, consider adopting the Model-View-Presenter pattern to keep your application maintainable as it grows:
Large scale application development and MVP, Part I
Large scale application development and MVP, Part II
GWT MVP Development with Activities and Places
Lets have an interface which is implemented by EntryPoint class,
now have a referrence of interface type which actually an object of interface.
Using this interface referrence invoke the listner(interface) mothod, which serves your purpose.

Resources