/** * Set the behavior of the current editing component to display the specified lines of formatted * text when the client hovers over the text. * * <p>Tooltips do not inherit display characteristics, such as color and styles, from the message * component on which they are applied. * * @param lines The lines of formatted text which will be displayed to the client upon hovering. * @return This builder instance. */ public FancyMessage formattedTooltip(FancyMessage... lines) { if (lines.length < 1) { onHover(null, null); // Clear tooltip return this; } FancyMessage result = new FancyMessage(); result.messageParts .clear(); // Remove the one existing text component that exists by default, which // destabilizes the object for (int i = 0; i < lines.length; i++) { try { for (MessagePart component : lines[i]) { if (component.clickActionData != null && component.clickActionName != null) { throw new IllegalArgumentException("The tooltip text cannot have click data."); } else if (component.hoverActionData != null && component.hoverActionName != null) { throw new IllegalArgumentException("The tooltip text cannot have a tooltip."); } if (component.hasText()) { result.messageParts.add(component.clone()); } } if (i != lines.length - 1) { result.messageParts.add(new MessagePart(rawText("\n"))); } } catch (CloneNotSupportedException e) { e.printStackTrace(); return this; } } return formattedTooltip( result.messageParts.size() == 0 ? null : result); // Throws NPE if size is 0, intended }
/** * Sets the text of the current editing component to a value. * * @param text The new text of the current editing component. * @return This builder instance. * @exception IllegalStateException If the text for the current editing component has already been * set. */ public FancyMessage text(String text) { MessagePart latest = latest(); if (latest.hasText()) { throw new IllegalStateException("text for this message part is already set"); } latest.text = rawText(text); dirty = true; return this; }
public FancyMessage text(TextualComponent text) { MessagePart latest = latest(); if (latest.hasText()) { throw new IllegalStateException("text for this message part is already set"); } latest.text = text; dirty = true; return this; }
public void writeJson(JsonWriter writer) throws IOException { if (messageParts.size() == 1) { latest().writeJson(writer); } else { writer.beginObject().name("text").value("").name("extra").beginArray(); for (final MessagePart part : this) { part.writeJson(writer); } writer.endArray().endObject(); } }
private void writeImports(QName elementName, List<MessagePart> parts) { Set<String> uris = new HashSet<String>(); for (MessagePart p : parts) { String ns = p.getDescriptor().getNamespaceURI(); if (!uris.contains(ns) && !ns.equals("http://www.w3.org/2001/XMLSchema") && !ns.equals(elementName.getNamespaceURI())) { print("<xs:import namespace=''{0}''/>", ns); uris.add(ns); } } }
public String toJSONString() { final JSONStringer json = new JSONStringer(); try { if (messageParts.size() == 1) { latest().writeJson(json); } else { json.object().key("text").value("").key("extra").array(); for (final MessagePart part : messageParts) { part.writeJson(json); } json.endArray().endObject(); } } catch (final JSONException e) { throw new RuntimeException("invalid message"); } return json.toString(); }
private int getRsv(MessagePart uncompressedMessagePart) { int result = uncompressedMessagePart.getRsv(); if (!firstCompressedFrameWritten) { result += RSV_BITMASK; firstCompressedFrameWritten = true; } return result; }
private void build(QName elementName, List<MessagePart> allParts) { print( "<xs:schema xmlns:xs=''http://www.w3.org/2001/XMLSchema''" + " xmlns:jaxb=''http://java.sun.com/xml/ns/jaxb''" + " xmlns:xjc=''http://java.sun.com/xml/ns/jaxb/xjc''" + " jaxb:extensionBindingPrefixes=''xjc''" + " jaxb:version=''1.0''" + " targetNamespace=''{0}''>", elementName.getNamespaceURI()); writeImports(elementName, allParts); if (!asyncRespBeanBinding) { print( "<xs:annotation><xs:appinfo>" + " <jaxb:schemaBindings>" + " <jaxb:package name=''{0}'' />" + " </jaxb:schemaBindings>" + "</xs:appinfo></xs:annotation>", wsdlModeler.getJavaPackage()); asyncRespBeanBinding = true; } print("<xs:element name=''{0}''>", elementName.getLocalPart()); print("<xs:complexType>"); print("<xs:sequence>"); for (MessagePart p : allParts) { // rpclit wsdl:part must reference schema type not element, also it must exclude headers and // mime parts if (p.getDescriptorKind() == SchemaKinds.XSD_ELEMENT) { print( "<xs:element ref=''types:{0}'' xmlns:types=''{1}''/>", p.getDescriptor().getLocalPart(), p.getDescriptor().getNamespaceURI()); } else { print( "<xs:element name=''{0}'' type=''{1}'' xmlns=''{2}'' />", p.getName(), p.getDescriptor().getLocalPart(), p.getDescriptor().getNamespaceURI()); } } print("</xs:sequence>"); print("</xs:complexType>"); print("</xs:element>"); print("</xs:schema>"); // reset the StringWriter, so that next operation element could be written if (buf.toString().length() > 0) { // System.out.println("Response bean Schema for operation========> "+ elementName+"\n\n"+buf); InputSource is = new InputSource(new StringReader(buf.toString())); schemas.add(is); buf.getBuffer().setLength(0); } }
/** * parse a MIME message, calling the builder methods that correspond to the message's header * fields and body parts. */ OutboundMessageIF parse(MIMEMessage msg) { this.msg = msg; builder = MessageBuilder.getInstance(getDestination()); MessagePart hdr = nextHeader(); while (hdr != null) { if (hdr.getName().equalsIgnoreCase("to")) builder.to((String) hdr.getValue()); else if (hdr.getName().equalsIgnoreCase("from")) builder.from((String) hdr.getValue()); // ... hdr = nextHeader(); } // while hdr MessagePart bdy = nextBodyPart(); while (bdy != null) { if (bdy.getName().equals("text/plain")) builder.plainText((String) bdy.getValue()); // ... else if (bdy.getName().equals("image/jpeg")) builder.jpegImage((Image) bdy.getValue()); // ... bdy = nextHeader(); } // while bdy return builder.getOutboundMsg(); } // parse(MIMEMessage)
public void run() { Message msg = null; String msgReceived = null; Image receivedImage = null; mMsgAvail = 0; while (!mEndNow) { synchronized (mMonitor) { // enter monitor if (mMsgAvail <= 0) { try { mMonitor.wait(); } catch (Exception e) { System.out.println(e); } } mMsgAvail--; } try { msg = conn.receive(); if (msg instanceof MultipartMessage) { MultipartMessage mpm = (MultipartMessage) msg; MessagePart[] parts = mpm.getMessageParts(); System.out.println(parts.length); if (parts != null) { for (int i = 0; i < parts.length; i++) { MessagePart mp = parts[i]; byte[] ba = mp.getContent(); receivedImage = Image.createImage(ba, 0, ba.length); mColorSquare.setImage(receivedImage); } } } } catch (IOException e) { System.out.println(e); } } }
public String toJSONString() { if (!dirty && jsonString != null) { return jsonString; } StringWriter string = new StringWriter(); JsonWriter json = new JsonWriter(string); try { if (messageParts.size() == 1) { latest().writeJson(json); } else { json.beginObject().name("text").value("").name("extra").beginArray(); for (final MessagePart part : messageParts) { part.writeJson(json); } json.endArray().endObject(); json.close(); } } catch (Exception e) { throw new RuntimeException("invalid message"); } jsonString = string.toString(); dirty = false; return jsonString; }
private void onHover(final String name, final String data) { final MessagePart latest = latest(); latest.hoverActionName = name; latest.hoverActionData = data; }
private void onClick(final String name, final String data) { final MessagePart latest = latest(); latest.clickActionName = name; latest.clickActionData = data; }
/** * Sets the text of the current editing component to a value. * * @param text The new text of the current editing component. * @return This builder instance. */ public FancyMessage text(String text) { MessagePart latest = latest(); latest.text = rawText(text); dirty = true; return this; }
/** * Deserializes a fancy message from its JSON representation. This JSON representation is of the * format of that returned by {@link #toJSONString()}, and is compatible with vanilla inputs. * * @param json The JSON string which represents a fancy message. * @return A {@code FancyMessage} representing the parameterized JSON message. */ public static FancyMessage deserialize(String json) { JsonObject serialized = _stringParser.parse(json).getAsJsonObject(); JsonArray extra = serialized.getAsJsonArray("extra"); // Get the extra component FancyMessage returnVal = new FancyMessage(); returnVal.messageParts.clear(); for (JsonElement mPrt : extra) { MessagePart component = new MessagePart(); JsonObject messagePart = mPrt.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : messagePart.entrySet()) { // Deserialize text if (TextualComponent.isTextKey(entry.getKey())) { // The map mimics the YAML serialization, which has a "key" field and one or more "value" // fields Map<String, Object> serializedMapForm = new HashMap< String, Object>(); // Must be object due to Bukkit serializer API compliance serializedMapForm.put("key", entry.getKey()); if (entry.getValue().isJsonPrimitive()) { // Assume string serializedMapForm.put("value", entry.getValue().getAsString()); } else { // Composite object, but we assume each element is a string for (Map.Entry<String, JsonElement> compositeNestedElement : entry.getValue().getAsJsonObject().entrySet()) { serializedMapForm.put( "value." + compositeNestedElement.getKey(), compositeNestedElement.getValue().getAsString()); } } component.text = TextualComponent.deserialize(serializedMapForm); } else if (MessagePart.stylesToNames.inverse().containsKey(entry.getKey())) { if (entry.getValue().getAsBoolean()) { component.styles.add(MessagePart.stylesToNames.inverse().get(entry.getKey())); } } else if (entry.getKey().equals("color")) { component.color = ChatColor.valueOf(entry.getValue().getAsString().toUpperCase()); } else if (entry.getKey().equals("clickEvent")) { JsonObject object = entry.getValue().getAsJsonObject(); component.clickActionName = object.get("action").getAsString(); component.clickActionData = object.get("value").getAsString(); } else if (entry.getKey().equals("hoverEvent")) { JsonObject object = entry.getValue().getAsJsonObject(); component.hoverActionName = object.get("action").getAsString(); if (object.get("value").isJsonPrimitive()) { // Assume string component.hoverActionData = new JsonString(object.get("value").getAsString()); } else { // Assume composite type // The only composite type we currently store is another FancyMessage // Therefore, recursion time! component.hoverActionData = deserialize( object .get("value") .toString() /* This should properly serialize the JSON object as a JSON string */); } } } returnVal.messageParts.add(component); } return returnVal; }
private void onHover(final String name, final JsonRepresentedObject data) { final MessagePart latest = latest(); latest.hoverActionName = name; latest.hoverActionData = data; dirty = true; }
/** * Sets the text of the current editing component to a value. * * @param text The new text of the current editing component. * @return This builder instance. */ public FancyMessage text(TextualComponent text) { MessagePart latest = latest(); latest.text = text; dirty = true; return this; }
@Override public List<MessagePart> sendMessagePart(List<MessagePart> uncompressedParts) { List<MessagePart> allCompressedParts = new ArrayList<MessagePart>(); for (MessagePart uncompressedPart : uncompressedParts) { byte opCode = uncompressedPart.getOpCode(); if (Util.isControl(opCode)) { // Control messages can appear in the middle of other messages // and must not be compressed. Pass it straight through allCompressedParts.add(uncompressedPart); } else { List<MessagePart> compressedParts = new ArrayList<MessagePart>(); ByteBuffer uncompressedPayload = uncompressedPart.getPayload(); SendHandler uncompressedIntermediateHandler = uncompressedPart.getIntermediateHandler(); deflater.setInput( uncompressedPayload.array(), uncompressedPayload.arrayOffset() + uncompressedPayload.position(), uncompressedPayload.remaining()); int flush = (uncompressedPart.isFin() ? Deflater.SYNC_FLUSH : Deflater.NO_FLUSH); boolean deflateRequired = true; while (deflateRequired) { ByteBuffer compressedPayload = writeBuffer; int written = deflater.deflate( compressedPayload.array(), compressedPayload.arrayOffset() + compressedPayload.position(), compressedPayload.remaining(), flush); compressedPayload.position(compressedPayload.position() + written); if (!uncompressedPart.isFin() && compressedPayload.hasRemaining() && deflater.needsInput()) { // This message part has been fully processed by the // deflater. Fire the send handler for this message part // and move on to the next message part. break; } // If this point is reached, a new compressed message part // will be created... MessagePart compressedPart; // .. and a new writeBuffer will be required. writeBuffer = ByteBuffer.allocate(Constants.DEFAULT_BUFFER_SIZE); // Flip the compressed payload ready for writing compressedPayload.flip(); boolean fin = uncompressedPart.isFin(); boolean full = compressedPayload.limit() == compressedPayload.capacity(); boolean needsInput = deflater.needsInput(); if (fin && !full && needsInput) { // End of compressed message. Drop EOM bytes and output. compressedPayload.limit(compressedPayload.limit() - EOM_BYTES.length); compressedPart = new MessagePart( true, getRsv(uncompressedPart), opCode, compressedPayload, uncompressedIntermediateHandler, uncompressedIntermediateHandler); deflateRequired = false; startNewMessage(); } else if (full && !needsInput) { // Write buffer full and input message not fully read. // Output and start new compressed part. compressedPart = new MessagePart( false, getRsv(uncompressedPart), opCode, compressedPayload, uncompressedIntermediateHandler, uncompressedIntermediateHandler); } else if (!fin && full && needsInput) { // Write buffer full and input message not fully read. // Output and get more data. compressedPart = new MessagePart( false, getRsv(uncompressedPart), opCode, compressedPayload, uncompressedIntermediateHandler, uncompressedIntermediateHandler); deflateRequired = false; } else if (fin && full && needsInput) { // Write buffer full. Input fully read. Deflater may be // in one of four states: // - output complete (just happened to align with end of // buffer // - in middle of EOM bytes // - about to write EOM bytes // - more data to write int eomBufferWritten = deflater.deflate(EOM_BUFFER, 0, EOM_BUFFER.length, Deflater.SYNC_FLUSH); if (eomBufferWritten < EOM_BUFFER.length) { // EOM has just been completed compressedPayload.limit( compressedPayload.limit() - EOM_BYTES.length + eomBufferWritten); compressedPart = new MessagePart( true, getRsv(uncompressedPart), opCode, compressedPayload, uncompressedIntermediateHandler, uncompressedIntermediateHandler); deflateRequired = false; startNewMessage(); } else { // More data to write // Copy bytes to new write buffer writeBuffer.put(EOM_BUFFER, 0, eomBufferWritten); compressedPart = new MessagePart( false, getRsv(uncompressedPart), opCode, compressedPayload, uncompressedIntermediateHandler, uncompressedIntermediateHandler); } } else { throw new IllegalStateException("Should never happen"); } // Add the newly created compressed part to the set of parts // to pass on to the next transformation. compressedParts.add(compressedPart); } SendHandler uncompressedEndHandler = uncompressedPart.getEndHandler(); int size = compressedParts.size(); if (size > 0) { compressedParts.get(size - 1).setEndHandler(uncompressedEndHandler); } allCompressedParts.addAll(compressedParts); } } if (next == null) { return allCompressedParts; } else { return next.sendMessagePart(allCompressedParts); } }