예제 #1
0
class EventEditor(Composite):

    """
    Let user create a new Event.
    
    CSS Configuration
    <ul>
    <li>.gwittit-EventEditor
    </ul>
    """
    
    @java.init
    def __init__(self, *a, **kw):
        self.outer = VerticalPanel()
        self.loader = Image(u"/loader.gif")
        self.eventInfo = None
        self.nameText = TextBox()
        self.categoryListBox = ListBox(False)
        self.subCategoriesListBox = ListBox(False)
        self.hostText = TextBox()
        self.locationText = TextBox()
        self.cityText = TextBox()
        self.createEventButton = Button(u"Create Event")
        self.apiClient = ApiFactory.getInstance()
    
    @__init__.register
    @java.typed()
    def __init__(self, ):
        self.__init__._super()
        self.outer.addStyleName(u"gwittit-EventEditor")
        self.outer.setSpacing(10)
        self.initFields()
        self.outer.add(self.createLabelAndInput(u"Name", self.nameText))
        self.outer.add(self.createLabelAndInput(u"Category", self.categoryListBox))
        self.outer.add(self.createLabelAndInput(u"SubCategory", self.subCategoriesListBox))
        self.outer.add(self.createLabelAndInput(u"Host", self.hostText)) #  outer.add ( createLabelAndInput ( "EventEnds" , endTimePicker ) );
        self.outer.add(self.createLabelAndInput(u"Location", self.locationText))
        self.outer.add(self.createLabelAndInput(u"City", self.cityText))
        self.outer.add(self.createEventButton)
        class _anonymous(ClickHandler):
            
            @java.typed(ClickEvent)
            def onClick(self, event):
                self.saveOrUpdate()
        self.createEventButton.addClickHandler(_anonymous())
        self.initWidget(self.outer)
    
    @java.private
    def saveOrUpdate(self):
        """
        Save event to facebook
        """
        jEvent = Json()
        jEvent.put(u"name", self.nameText.getValue())
        jEvent.put(u"host", self.hostText.getValue())
        jEvent.put(u"location", self.locationText.getValue())
        jEvent.put(u"city", self.cityText.getValue())
        selectedCategory = Integer(self.categoryListBox.getValue(self.categoryListBox.getSelectedIndex())) #  Save Category
        selectedSubCategory = Integer(self.subCategoriesListBox.getValue(self.subCategoriesListBox.getSelectedIndex()))
        jEvent.put(u"category", EventInfo.Category.values()[(selectedCategory - 1)].toString())
        jEvent.put(u"subcategory", EventInfo.SubCategory.values()[(selectedSubCategory - 1)].toString())
        jEvent.put(u"start_time", Date().getTime() + Long(u"9999999999"))
        jEvent.put(u"end_time", Date().getTime() + Long(u"9999999999999"))
        eventInfo = EventInfo.fromJson(java.str(jEvent))
        self.outer.add(self.loader)
        class _anonymous(AsyncCallback):
            
            @java.typed(Throwable)
            def onFailure(self, caught):
                self.outer.remove(self.loader)
                errorResponse = ErrorResponseUI(caught)
                errorResponse.center()
                errorResponse.show()
            
            @java.typed(JavaScriptObject)
            def onSuccess(self, result):
                self.outer.remove(self.loader)
                self.outer.add(HTML(u"Created event with ID " + java.str(result)))
        self.apiClient.eventsCreate(eventInfo, _anonymous()) #  Create the event.
    
    @java.private
    def initFields(self):
        """
        Create widgets, and set default values if any
        """
        self.nameText.setValue(u"Birthday")
        self.hostText.setValue(u"host")
        self.locationText.setValue(u"location")
        self.cityText.setValue(u"Palo Alto, CA")
        for category in EventInfo.Category.values():
            self.categoryListBox.addItem(java.str(category).replace(u"_", u" "), u"" + java.str(category.getId()))
        for subCategory in EventInfo.SubCategory.values():
            self.subCategoriesListBox.addItem(java.str(subCategory).replace(u"_", u" "), u"" + java.str(subCategory.getId()))
    
    @java.private
    @java.typed(String, Widget)
    def createLabelAndInput(self, label, field):
        h = HorizontalPanel()
        l = HTML(java.str(u"<b>" + java.str(label)) + u": </b>")
        l.setWidth(u"150px")
        h.add(l)
        h.add(field)
        return h
예제 #2
0
class Lists(Sink):
    def __init__(self):
        Sink.__init__(self)
        self.sStrings=[["foo0", "bar0", "baz0", "toto0", "tintin0"],
            ["foo1", "bar1", "baz1", "toto1", "tintin1"],
            ["foo2", "bar2", "baz2", "toto2", "tintin2"],
            ["foo3", "bar3", "baz3", "toto3", "tintin3"],
            ["foo4", "bar4", "baz4", "toto4", "tintin4"]]

        self.combo=ListBox()
        self.list=ListBox()
        self.echo=Label()

        self.combo.setVisibleItemCount(1)
        self.combo.addChangeListener(self)
        self.list.setVisibleItemCount(10)
        self.list.setMultipleSelect(True)
        
        for i in range(len(self.sStrings)):
            self.combo.addItem("List %d" % i)
        self.combo.setSelectedIndex(0)
        self.fillList(0)
        
        self.list.addChangeListener(self)
        
        horz = HorizontalPanel()
        horz.setVerticalAlignment(HasAlignment.ALIGN_TOP)
        horz.setSpacing(8)
        horz.add(self.combo)
        horz.add(self.list)
        
        panel = VerticalPanel()
        panel.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
        panel.add(horz)
        panel.add(self.echo)
        self.initWidget(panel)
        
        self.echoSelection()

    def onChange(self, sender):
        print "onChange", sender, self.list, self.combo
        if sender is self.combo:
            print "fill list"
            self.fillList(self.combo.getSelectedIndex())
        elif sender is self.list:
            print "echo "
            self.echoSelection()
        else:
            print "oops"

    def onShow(self):
        pass
    
    def fillList(self, idx):
        self.list.clear()
        strings = self.sStrings[idx]
        for i in range(len(strings)):
            self.list.addItem(strings[i])

        self.echoSelection()

    def echoSelection(self):
        msg = "Selected items: "
        print msg, self.list.getItemCount()
        for i in range(self.list.getItemCount()):
            if self.list.isItemSelected(i):
                msg += "%d" % i + self.list.getItemText(i) + " "
        self.echo.setText(msg)
예제 #3
0
class JSONRPCExample:
    def onModuleLoad(self):
        self.TEXT_WAITING = "Waiting for response..."
        self.TEXT_ERROR = "Server Error"
        self.METHOD_ECHO = "Echo"
        self.METHOD_REVERSE = "Reverse"
        self.METHOD_UPPERCASE = "UPPERCASE"
        self.METHOD_LOWERCASE = "lowercase"
        self.methods = [self.METHOD_ECHO, self.METHOD_REVERSE, self.METHOD_UPPERCASE, self.METHOD_LOWERCASE]

        self.remote_php = EchoServicePHP()
        self.remote_py = EchoServicePython()

        self.status=Label()
        self.text_area = TextArea()
        self.text_area.setText(r"{'Test'} [\"String\"]")
        self.text_area.setCharacterWidth(80)
        self.text_area.setVisibleLines(8)
        
        self.method_list = ListBox()
        #self.method_list.setMultipleSelect(True)
        self.method_list.setVisibleItemCount(1)
        for method in self.methods:
            self.method_list.addItem(method)
        self.method_list.setSelectedIndex(0)

        method_panel = HorizontalPanel()
        method_panel.add(HTML("Remote string method to call: "))
        method_panel.add(self.method_list)
        method_panel.setSpacing(8)

        self.button_php = Button("Send to PHP Service", self)
        self.button_py = Button("Send to Python Service", self)

        buttons = HorizontalPanel()
        buttons.add(self.button_php)
        buttons.add(self.button_py)
        buttons.setSpacing(8)
        
        info = r"<h2>JSON-RPC Example</h2><p>This example demonstrates the calling of server services with <a href=\"http://json-rpc.org/\">JSON-RPC</a>."
        info += "<p>Enter some text below, and press a button to send the text to an Echo service on your server. An echo service simply sends the exact same text back that it receives."
        
        panel = VerticalPanel()
        panel.add(HTML(info))
        panel.add(self.text_area)
        panel.add(method_panel)
        panel.add(buttons)
        panel.add(self.status)
        
        RootPanel().add(panel)

    def onClick(self, sender, event):
        self.status.setText(self.TEXT_WAITING)
        method = self.methods[self.method_list.getSelectedIndex()]
        text = self.text_area.getText()

        print repr(text)

        # demonstrate proxy & callMethod()
        if sender == self.button_php:
            if method == self.METHOD_ECHO:
                id = self.remote_php.echo(text, self)
            elif method == self.METHOD_REVERSE:
                id = self.remote_php.callMethod("reverse", [text], self)
            elif method == self.METHOD_UPPERCASE:
                id = self.remote_php.uppercase(text, self)
            elif method == self.METHOD_LOWERCASE:
                id = self.remote_php.lowercase(text, self)
        else:
            if method == self.METHOD_ECHO:
                id = self.remote_py.echo(text, self)
            elif method == self.METHOD_REVERSE:
                id = self.remote_py.reverse(text, self)
            elif method == self.METHOD_UPPERCASE:
                id = self.remote_py.uppercase(text, self)
            elif method == self.METHOD_LOWERCASE:
                id = self.remote_py.lowercase(text, self)
        if id<0:
            self.status.setText(self.TEXT_ERROR)

    def onRemoteResponse(self, response, request_info):
        self.status.setText(response)

    def onRemoteError(self, code, message, request_info):
        self.status.setText("Server Error or Invalid Response: ERROR " + str(code) + " - " + str(message))