Anonymous avatar Anonymous committed c663697

Tag visualization code was externalized in the TagView class.

Comments (0)

Files changed (6)

org.mandriva.annotator/org.mandriva.annotator.gwtapp/src/main/java/org/mandriva/annotator/gwtapp/client/EntitiesPanel.java

 package org.mandriva.annotator.gwtapp.client;
 
 import org.mandriva.annotator.gwtapp.client.model.Entity;
+import org.mandriva.annotator.gwtapp.client.widgets.TagView;
 
 import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.core.client.JsArray;
-import com.google.gwt.dom.client.Style;
 import com.google.gwt.dom.client.Style.Unit;
-import com.google.gwt.event.dom.client.ClickEvent;
-import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.logical.shared.CloseEvent;
+import com.google.gwt.event.logical.shared.CloseHandler;
+import com.google.gwt.event.logical.shared.HasSelectionHandlers;
+import com.google.gwt.event.logical.shared.SelectionEvent;
+import com.google.gwt.event.logical.shared.SelectionHandler;
+import com.google.gwt.event.shared.HandlerRegistration;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.HTML;
-import com.google.gwt.user.client.ui.InlineHTML;
+import com.google.gwt.user.client.ui.Widget;
 
 /**
  * Panel containing a list of entities.
  * 
  * @author kotelnikov
  */
-public class EntitiesPanel extends Composite {
+public class EntitiesPanel extends Composite
+    implements
+    HasSelectionHandlers<Entity> {
 
-    public interface IEntityListener {
-        void onEnitiy(Entity entity);
+    public static class EntityView extends TagView {
+        private Entity fEntity;
+
+        public EntityView(Entity entity) {
+            fEntity = entity;
+            setLabel(entity.getLabel(), false);
+        }
+
+        protected Entity getEntity() {
+            return fEntity;
+        }
     }
 
-    private IEntityListener fClickListener;
+    private CloseHandler<TagView> fCloseHandler;
 
     private HTML fEmptyListMessage = new HTML("Empty list.");
 
-    private boolean fEnableRemoveEntities;
-
-    private JsArray<Entity> fEntities = JsArray.createArray().cast();
-
     private FlowPanel fPanel = new FlowPanel();
 
+    private SelectionHandler<TagView> fTagSelectionHandler = new SelectionHandler<TagView>() {
+        public void onSelection(SelectionEvent<TagView> event) {
+            EntityView entityView = (EntityView) event.getSelectedItem();
+            Entity entity = entityView.getEntity();
+            SelectionEvent.fire(EntitiesPanel.this, entity);
+        };
+    };
+
     public EntitiesPanel() {
-        this(null, false);
+        this(false);
     }
 
     public EntitiesPanel(boolean enableRemoveEntities) {
-        this(null, enableRemoveEntities);
+        initWidget(fPanel);
+        getElement().getStyle().setPadding(0.5, Unit.EM);
+        if (enableRemoveEntities) {
+            fCloseHandler = new CloseHandler<TagView>() {
+                public void onClose(CloseEvent<TagView> event) {
+                    TagView entityView = event.getTarget();
+                    entityView.removeFromParent();
+                }
+            };
+        }
     }
 
-    public EntitiesPanel(IEntityListener clickListener) {
-        this(clickListener, false);
+    public void addEntity(Entity entity) {
+        if (!hasEntity(entity)) {
+            EntityView view = newEntityView(entity);
+            fPanel.add(view);
+        }
     }
 
-    public EntitiesPanel(
-        IEntityListener clickListener,
-        boolean enableRemoveEntities) {
-        initWidget(fPanel);
-        getElement().getStyle().setPadding(0.5, Unit.EM);
-        fClickListener = clickListener;
-        fEnableRemoveEntities = enableRemoveEntities;
-    }
-
-    public void addEntity(final Entity entity) {
-        InlineHTML view = addView(entity.getLabel(), new ClickHandler() {
-            public void onClick(ClickEvent event) {
-                if (fClickListener != null) {
-                    fClickListener.onEnitiy(entity);
-                }
-            }
-        });
-        if (fEnableRemoveEntities) {
-            Style style = view.getElement().getStyle();
-            style.setMarginRight(1, Unit.PX);
-            view = addView("x", new ClickHandler() {
-                public void onClick(ClickEvent event) {
-                    removeEntity(entity);
-                }
-            });
-            style = view.getElement().getStyle();
-            style.setMarginLeft(0, Unit.EM);
-        }
-        fEntities.push(entity);
-    }
-
-    private InlineHTML addView(String title, ClickHandler clickHandler) {
-        InlineHTML view = new InlineHTML(
-            "<a href='javascript:void(0);' style='text-decoration: none;'>"
-                + title
-                + "</a>");
-        fPanel.add(view);
-        view.addClickHandler(clickHandler);
-        Style style = view.getElement().getStyle();
-        style.setBackgroundColor("#EEEEEE");
-        style.setMargin(0.3, Unit.EM);
-        style.setMarginLeft(0, Unit.EM);
-        style.setMarginRight(0.5, Unit.EM);
-        style.setPadding(0.2, Unit.EM);
-        return view;
-
+    public HandlerRegistration addSelectionHandler(
+        SelectionHandler<Entity> handler) {
+        return addHandler(handler, SelectionEvent.getType());
     }
 
     public void clear() {
         fPanel.clear();
-        fEntities = JavaScriptObject.createArray().cast();
     }
 
     public JsArray<Entity> getEntities() {
-        return fEntities;
+        JsArray<Entity> list = JavaScriptObject.createArray().cast();
+        for (Widget w : fPanel) {
+            if (w instanceof EntityView) {
+                EntityView view = (EntityView) w;
+                Entity entity = view.getEntity();
+                list.push(entity);
+            }
+        }
+        return list;
     }
 
     public boolean hasEntity(Entity entity) {
         boolean result = false;
-        int len = fEntities.length();
-        for (int i = len - 1; !result && i >= 0; i--) {
-            Entity e = fEntities.get(i);
-            result = entity.isEqual(e);
+        for (Widget w : fPanel) {
+            if (w instanceof EntityView) {
+                EntityView view = (EntityView) w;
+                Entity e = view.getEntity();
+                if (e.isEqual(entity)) {
+                    result = true;
+                    break;
+                }
+            }
         }
         return result;
     }
 
-    public void removeEntity(Entity entity) {
-        JsArray<Entity> list = JavaScriptObject.createArray().cast();
-        int len = fEntities.length();
-        for (int i = len - 1; i >= 0; i--) {
-            Entity e = fEntities.get(i);
-            if (!entity.isEqual(e)) {
-                list.push(e);
-            } else {
-                if (fEnableRemoveEntities) {
-                    fPanel.remove(i * 2 + 1);
-                    fPanel.remove(i * 2);
-                } else {
-                    fPanel.remove(i);
-                }
-            }
+    private EntityView newEntityView(Entity entity) {
+        EntityView view = new EntityView(entity);
+        view.addSelectionHandler(fTagSelectionHandler);
+        if (fCloseHandler != null) {
+            view.showCloseButton(true);
+            view.addCloseHandler(fCloseHandler);
         }
-        fEntities = list;
+        return view;
+
     }
 
     public void setEmptyListMessage(String message) {

org.mandriva.annotator/org.mandriva.annotator.gwtapp/src/main/java/org/mandriva/annotator/gwtapp/client/WebAnnotatorDialogBox.java

 
 import org.mandriva.annotator.bookmarklet.client.AbstractBookmarklet.IBookmarkletApplication;
 import org.mandriva.annotator.bookmarklet.client.XdmSocket;
-import org.mandriva.annotator.gwtapp.client.EntitiesPanel.IEntityListener;
 import org.mandriva.annotator.gwtapp.client.model.Annotation;
 import org.mandriva.annotator.gwtapp.client.model.AnnotationLoadRequest;
 import org.mandriva.annotator.gwtapp.client.model.DataObject;
 import com.google.gwt.dom.client.Style.Unit;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.logical.shared.SelectionEvent;
+import com.google.gwt.event.logical.shared.SelectionHandler;
 import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.rpc.AsyncCallback;
 import com.google.gwt.user.client.ui.Button;
 
     private SelectionTracker fSelectionTracker = new SelectionTracker();
 
-    private EntitiesPanel fSuggestedEntities = new EntitiesPanel(
-        new IEntityListener() {
-            public void onEnitiy(Entity entity) {
-                addRelatedEntity(entity);
-            }
-        });
+    private EntitiesPanel fSuggestedEntities = new EntitiesPanel(false);
 
     private HTML fSuggestedEntitiesTitle;
 
 
     public WebAnnotatorDialogBox(XdmSocket fetcher) {
         fFetcher = fetcher;
+        fSuggestedEntities.addSelectionHandler(new SelectionHandler<Entity>() {
+            public void onSelection(SelectionEvent<Entity> event) {
+                Entity entity = event.getSelectedItem();
+                addRelatedEntity(entity);
+            }
+        });
 
         fDialogBox.setTitle("Web Page Annotator");
 
     }
 
     private void addRelatedEntity(Entity entity) {
-        if (!fRelatedEntities.hasEntity(entity)) {
-            fRelatedEntities.addEntity(entity);
-        }
+        fRelatedEntities.addEntity(entity);
     }
 
     private FlowPanel addRowContainer() {

org.mandriva.annotator/org.mandriva.annotator.gwtapp/src/main/java/org/mandriva/annotator/gwtapp/client/widgets/InlineHTMLPanel.java

+/**
+ * 
+ */
+package org.mandriva.annotator.gwtapp.client.widgets;
+
+import com.google.gwt.safehtml.shared.SafeHtml;
+import com.google.gwt.user.client.ui.HTMLPanel;
+
+/**
+ * @author kotelnikov
+ */
+public class InlineHTMLPanel extends HTMLPanel {
+
+    public InlineHTMLPanel(SafeHtml safeHtml) {
+        this(safeHtml.asString());
+    }
+
+    public InlineHTMLPanel(String html) {
+        super("span", html);
+    }
+
+}

org.mandriva.annotator/org.mandriva.annotator.gwtapp/src/main/java/org/mandriva/annotator/gwtapp/client/widgets/TagView.java

+/**
+ * 
+ */
+package org.mandriva.annotator.gwtapp.client.widgets;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.logical.shared.CloseEvent;
+import com.google.gwt.event.logical.shared.CloseHandler;
+import com.google.gwt.event.logical.shared.HasCloseHandlers;
+import com.google.gwt.event.logical.shared.HasSelectionHandlers;
+import com.google.gwt.event.logical.shared.SelectionEvent;
+import com.google.gwt.event.logical.shared.SelectionHandler;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.resources.client.ClientBundle;
+import com.google.gwt.resources.client.ImageResource;
+import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.uibinder.client.UiField;
+import com.google.gwt.uibinder.client.UiHandler;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.Image;
+import com.google.gwt.user.client.ui.InlineHTML;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * @author kotelnikov
+ */
+public class TagView extends Composite
+    implements
+    HasSelectionHandlers<TagView>,
+    HasCloseHandlers<TagView> {
+
+    interface EntityViewUiBinder extends UiBinder<Widget, TagView> {
+    }
+
+    public interface Resources extends ClientBundle {
+        @Source("closeIcon.png")
+        ImageResource closeIcon();
+    }
+
+    private static EntityViewUiBinder uiBinder = GWT
+        .create(EntityViewUiBinder.class);
+
+    @UiField
+    Image fBtn;
+
+    @UiField
+    InlineHTML fLabel;
+
+    private boolean fShowCloseButton;
+
+    public TagView() {
+        this(false);
+    }
+
+    /**
+     * @param showCloseButton
+     */
+    public TagView(boolean showCloseButton) {
+        initWidget(uiBinder.createAndBindUi(this));
+        showCloseButton(showCloseButton);
+    }
+
+    public HandlerRegistration addCloseHandler(CloseHandler<TagView> handler) {
+        return addHandler(handler, CloseEvent.getType());
+    }
+
+    public HandlerRegistration addSelectionHandler(
+        SelectionHandler<TagView> handler) {
+        return addHandler(handler, SelectionEvent.getType());
+    }
+
+    public String getLabel() {
+        return fLabel.getText();
+    }
+
+    public String getLabelHTML() {
+        return fLabel.getHTML();
+    }
+
+    @UiHandler("fBtn")
+    void handleBtnClick(ClickEvent e) {
+        if (fShowCloseButton) {
+            CloseEvent.fire(this, this);
+        }
+    }
+
+    @UiHandler("fLabel")
+    void handleLabelClick(ClickEvent e) {
+        SelectionEvent.fire(this, this);
+    }
+
+    public void setLabel(String label, boolean html) {
+        if (html) {
+            fLabel.setHTML(label);
+        } else {
+            fLabel.setText(label);
+        }
+    }
+
+    public void showCloseButton(boolean show) {
+        fShowCloseButton = show;
+        fBtn.setVisible(fShowCloseButton);
+    }
+
+}

org.mandriva.annotator/org.mandriva.annotator.gwtapp/src/main/java/org/mandriva/annotator/gwtapp/client/widgets/TagView.ui.xml

+<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
+<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
+    xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:w="urn:import:org.mandriva.annotator.gwtapp.client.widgets">
+    <ui:with field='res'
+        type='org.mandriva.annotator.gwtapp.client.widgets.TagView.Resources' />
+    <ui:style>
+        .tag {
+            margin: 3px;
+            margin-left: 0;
+            background-color: #eeeeee;
+            border-color: silver;
+            -webkit-border-radius: 3px;
+            -moz-border-radius: 3px;
+            border-radius: 3px;
+            cursor: pointer;
+        }
+        
+        .label {
+            white-space: nowrap;
+        }
+        
+        .reducer {
+            padding: 2px;
+        }
+        
+        .btn {
+            
+        }
+    </ui:style>
+    <w:InlineHTMLPanel styleName="{style.tag}">
+        <span class="{style.reducer}">
+            <g:InlineHTML styleName="{style.label}"
+                ui:field="fLabel" />
+            <g:Image styleName="{style.btn}" ui:field="fBtn"
+                resource='{res.closeIcon}' />
+        </span>
+    </w:InlineHTMLPanel>
+</ui:UiBinder>
Add a comment to this file

org.mandriva.annotator/org.mandriva.annotator.gwtapp/src/main/java/org/mandriva/annotator/gwtapp/client/widgets/closeIcon.png

Added
New image
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.