private void onRequestUpload() { if (tempAttachments.size() < 3) { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Open file to attach"); /* if (Utilities.isUnix()) fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));*/ File result = fileChooser.showOpenDialog(stage); if (result != null) { try { URL url = result.toURI().toURL(); try (InputStream inputStream = url.openStream()) { byte[] filesAsBytes = ByteStreams.toByteArray(inputStream); if (filesAsBytes.length <= Connection.getMaxMsgSize()) { tempAttachments.add( new DisputeDirectMessage.Attachment(result.getName(), filesAsBytes)); inputTextArea.setText( inputTextArea.getText() + "\n[Attachment " + result.getName() + "]"); } else { new Popup().error("The max. allowed file size is 100 kB.").show(); } } catch (java.io.IOException e) { e.printStackTrace(); log.error(e.getMessage()); } } catch (MalformedURLException e2) { e2.printStackTrace(); log.error(e2.getMessage()); } } } else { new Popup().error("You cannot send more then 3 attachments in one message.").show(); } }
public static Tuple2<Label, TextArea> addLabelTextArea( GridPane gridPane, int rowIndex, String title, String prompt, double top) { Label label = addLabel(gridPane, rowIndex, title, 0); GridPane.setMargin(label, new Insets(top + 4, 0, 0, 0)); GridPane.setValignment(label, VPos.TOP); TextArea textArea = new TextArea(); textArea.setPromptText(prompt); textArea.setWrapText(true); GridPane.setRowIndex(textArea, rowIndex); GridPane.setColumnIndex(textArea, 1); GridPane.setMargin(textArea, new Insets(top, 0, 0, 0)); gridPane.getChildren().add(textArea); return new Tuple2<>(label, textArea); }
private void onSendMessage(String inputText, Dispute dispute) { DisputeDirectMessage disputeDirectMessage = disputeManager.sendDisputeDirectMessage( dispute, inputText, new ArrayList<>(tempAttachments)); tempAttachments.clear(); scrollToBottom(); inputTextArea.setDisable(true); inputTextArea.clear(); final Timer timer = FxTimer.runLater( Duration.ofMillis(500), () -> { sendMsgInfoLabel.setVisible(true); sendMsgInfoLabel.setManaged(true); sendMsgInfoLabel.setText("Sending Message..."); sendMsgProgressIndicator.setProgress(-1); sendMsgProgressIndicator.setVisible(true); sendMsgProgressIndicator.setManaged(true); }); disputeDirectMessage .arrivedProperty() .addListener( (observable, oldValue, newValue) -> { if (newValue) { hideSendMsgInfo(timer); } }); disputeDirectMessage .storedInMailboxProperty() .addListener( (observable, oldValue, newValue) -> { if (newValue) { sendMsgInfoLabel.setVisible(true); sendMsgInfoLabel.setManaged(true); sendMsgInfoLabel.setText( "Receiver is not online. Message is saved to his mailbox."); hideSendMsgInfo(timer); } }); }
private void hideSendMsgInfo(Timer timer) { timer.stop(); inputTextArea.setDisable(false); FxTimer.runLater( Duration.ofMillis(5000), () -> { sendMsgInfoLabel.setVisible(false); sendMsgInfoLabel.setManaged(false); }); sendMsgProgressIndicator.setProgress(0); sendMsgProgressIndicator.setVisible(false); sendMsgProgressIndicator.setManaged(false); }
private void onSelectDispute(Dispute dispute) { if (dispute == null) { if (root.getChildren().size() > 1) root.getChildren().remove(1); selectedDispute = null; } else if (selectedDispute != dispute) { this.selectedDispute = dispute; boolean isTrader = disputeManager.isTrader(dispute); TableGroupHeadline tableGroupHeadline = new TableGroupHeadline(); tableGroupHeadline.setText("Messages"); tableGroupHeadline.prefWidthProperty().bind(root.widthProperty()); AnchorPane.setTopAnchor(tableGroupHeadline, 10d); AnchorPane.setRightAnchor(tableGroupHeadline, 0d); AnchorPane.setBottomAnchor(tableGroupHeadline, 0d); AnchorPane.setLeftAnchor(tableGroupHeadline, 0d); ObservableList<DisputeDirectMessage> list = dispute.getDisputeDirectMessagesAsObservableList(); SortedList<DisputeDirectMessage> sortedList = new SortedList<>(list); sortedList.setComparator((o1, o2) -> o1.getDate().compareTo(o2.getDate())); list.addListener((ListChangeListener<DisputeDirectMessage>) c -> scrollToBottom()); messageListView = new ListView<>(sortedList); messageListView.setId("message-list-view"); messageListView.prefWidthProperty().bind(root.widthProperty()); messageListView.setMinHeight(150); AnchorPane.setTopAnchor(messageListView, 30d); AnchorPane.setRightAnchor(messageListView, 0d); AnchorPane.setLeftAnchor(messageListView, 0d); messagesAnchorPane = new AnchorPane(); messagesAnchorPane.prefWidthProperty().bind(root.widthProperty()); VBox.setVgrow(messagesAnchorPane, Priority.ALWAYS); inputTextArea = new TextArea(); inputTextArea.setPrefHeight(70); inputTextArea.setWrapText(true); Button sendButton = new Button("Send"); sendButton.setDefaultButton(true); sendButton.setOnAction(e -> onSendMessage(inputTextArea.getText(), dispute)); sendButton.setDisable(true); inputTextArea .textProperty() .addListener( (observable, oldValue, newValue) -> { sendButton.setDisable( newValue.length() == 0 && tempAttachments.size() == 0 && dispute.disputeResultProperty().get() == null); }); Button uploadButton = new Button("Add attachments"); uploadButton.setOnAction(e -> onRequestUpload()); sendMsgInfoLabel = new Label(); sendMsgInfoLabel.setVisible(false); sendMsgInfoLabel.setManaged(false); sendMsgInfoLabel.setPadding(new Insets(5, 0, 0, 0)); sendMsgProgressIndicator = new ProgressIndicator(0); sendMsgProgressIndicator.setPrefHeight(24); sendMsgProgressIndicator.setPrefWidth(24); sendMsgProgressIndicator.setVisible(false); sendMsgProgressIndicator.setManaged(false); dispute .isClosedProperty() .addListener( (observable, oldValue, newValue) -> { messagesInputBox.setVisible(!newValue); messagesInputBox.setManaged(!newValue); AnchorPane.setBottomAnchor(messageListView, newValue ? 0d : 120d); }); if (!dispute.isClosed()) { HBox buttonBox = new HBox(); buttonBox.setSpacing(10); buttonBox .getChildren() .addAll(sendButton, uploadButton, sendMsgProgressIndicator, sendMsgInfoLabel); if (!isTrader) { Button closeDisputeButton = new Button("Close ticket"); closeDisputeButton.setOnAction(e -> onCloseDispute(dispute)); closeDisputeButton.setDefaultButton(true); Pane spacer = new Pane(); HBox.setHgrow(spacer, Priority.ALWAYS); buttonBox.getChildren().addAll(spacer, closeDisputeButton); } messagesInputBox = new VBox(); messagesInputBox.setSpacing(10); messagesInputBox.getChildren().addAll(inputTextArea, buttonBox); VBox.setVgrow(buttonBox, Priority.ALWAYS); AnchorPane.setRightAnchor(messagesInputBox, 0d); AnchorPane.setBottomAnchor(messagesInputBox, 5d); AnchorPane.setLeftAnchor(messagesInputBox, 0d); AnchorPane.setBottomAnchor(messageListView, 120d); messagesAnchorPane .getChildren() .addAll(tableGroupHeadline, messageListView, messagesInputBox); } else { AnchorPane.setBottomAnchor(messageListView, 0d); messagesAnchorPane.getChildren().addAll(tableGroupHeadline, messageListView); } messageListView.setCellFactory( new Callback<ListView<DisputeDirectMessage>, ListCell<DisputeDirectMessage>>() { @Override public ListCell<DisputeDirectMessage> call(ListView<DisputeDirectMessage> list) { return new ListCell<DisputeDirectMessage>() { final Pane bg = new Pane(); final ImageView arrow = new ImageView(); final Label headerLabel = new Label(); final Label messageLabel = new Label(); final HBox attachmentsBox = new HBox(); final AnchorPane messageAnchorPane = new AnchorPane(); final Label statusIcon = new Label(); final double arrowWidth = 15d; final double attachmentsBoxHeight = 20d; final double border = 10d; final double bottomBorder = 25d; final double padding = border + 10d; { bg.setMinHeight(30); messageLabel.setWrapText(true); headerLabel.setTextAlignment(TextAlignment.CENTER); attachmentsBox.setSpacing(5); statusIcon.setStyle("-fx-font-size: 10;"); messageAnchorPane .getChildren() .addAll(bg, arrow, headerLabel, messageLabel, attachmentsBox, statusIcon); } @Override public void updateItem(final DisputeDirectMessage item, boolean empty) { super.updateItem(item, empty); if (item != null && !empty) { /* messageAnchorPane.prefWidthProperty().bind(EasyBind.map(messageListView.widthProperty(), w -> (double) w - padding - GUIUtil.getScrollbarWidth(messageListView)));*/ if (!messageAnchorPane.prefWidthProperty().isBound()) messageAnchorPane .prefWidthProperty() .bind( messageListView .widthProperty() .subtract(padding + GUIUtil.getScrollbarWidth(messageListView))); AnchorPane.setTopAnchor(bg, 15d); AnchorPane.setBottomAnchor(bg, bottomBorder); AnchorPane.setTopAnchor(headerLabel, 0d); AnchorPane.setBottomAnchor(arrow, bottomBorder + 5d); AnchorPane.setTopAnchor(messageLabel, 25d); AnchorPane.setBottomAnchor(attachmentsBox, bottomBorder + 10); boolean senderIsTrader = item.isSenderIsTrader(); boolean isMyMsg = isTrader ? senderIsTrader : !senderIsTrader; arrow.setVisible(!item.isSystemMessage()); arrow.setManaged(!item.isSystemMessage()); statusIcon.setVisible(false); if (item.isSystemMessage()) { headerLabel.setStyle("-fx-text-fill: -bs-green; -fx-font-size: 11;"); bg.setId("message-bubble-green"); messageLabel.setStyle("-fx-text-fill: white;"); } else if (isMyMsg) { headerLabel.setStyle("-fx-text-fill: -fx-accent; -fx-font-size: 11;"); bg.setId("message-bubble-blue"); messageLabel.setStyle("-fx-text-fill: white;"); if (isTrader) arrow.setId("bubble_arrow_blue_left"); else arrow.setId("bubble_arrow_blue_right"); sendMsgProgressIndicator .progressProperty() .addListener( (observable, oldValue, newValue) -> { if ((double) oldValue == -1 && (double) newValue == 0) { if (item.arrivedProperty().get()) showArrivedIcon(); else if (item.storedInMailboxProperty().get()) showMailboxIcon(); } }); if (item.arrivedProperty().get()) showArrivedIcon(); else if (item.storedInMailboxProperty().get()) showMailboxIcon(); // TODO show that icon on error /*else if (sendMsgProgressIndicator.getProgress() == 0) showNotArrivedIcon();*/ } else { headerLabel.setStyle("-fx-text-fill: -bs-light-grey; -fx-font-size: 11;"); bg.setId("message-bubble-grey"); messageLabel.setStyle("-fx-text-fill: black;"); if (isTrader) arrow.setId("bubble_arrow_grey_right"); else arrow.setId("bubble_arrow_grey_left"); } if (item.isSystemMessage()) { AnchorPane.setLeftAnchor(headerLabel, padding); AnchorPane.setRightAnchor(headerLabel, padding); AnchorPane.setLeftAnchor(bg, border); AnchorPane.setRightAnchor(bg, border); AnchorPane.setLeftAnchor(messageLabel, padding); AnchorPane.setRightAnchor(messageLabel, padding); AnchorPane.setLeftAnchor(attachmentsBox, padding); AnchorPane.setRightAnchor(attachmentsBox, padding); } else if (senderIsTrader) { AnchorPane.setLeftAnchor(headerLabel, padding + arrowWidth); AnchorPane.setLeftAnchor(bg, border + arrowWidth); AnchorPane.setRightAnchor(bg, border); AnchorPane.setLeftAnchor(arrow, border); AnchorPane.setLeftAnchor(messageLabel, padding + arrowWidth); AnchorPane.setRightAnchor(messageLabel, padding); AnchorPane.setLeftAnchor(attachmentsBox, padding + arrowWidth); AnchorPane.setRightAnchor(attachmentsBox, padding); AnchorPane.setRightAnchor(statusIcon, padding); } else { AnchorPane.setRightAnchor(headerLabel, padding + arrowWidth); AnchorPane.setLeftAnchor(bg, border); AnchorPane.setRightAnchor(bg, border + arrowWidth); AnchorPane.setRightAnchor(arrow, border); AnchorPane.setLeftAnchor(messageLabel, padding); AnchorPane.setRightAnchor(messageLabel, padding + arrowWidth); AnchorPane.setLeftAnchor(attachmentsBox, padding); AnchorPane.setRightAnchor(attachmentsBox, padding + arrowWidth); AnchorPane.setLeftAnchor(statusIcon, padding); } AnchorPane.setBottomAnchor(statusIcon, 7d); headerLabel.setText(formatter.formatDateTime(item.getDate())); messageLabel.setText(item.getMessage()); if (item.getAttachments().size() > 0) { AnchorPane.setBottomAnchor( messageLabel, bottomBorder + attachmentsBoxHeight + 10); attachmentsBox .getChildren() .add( new Label("Attachments: ") { { setPadding(new Insets(0, 0, 3, 0)); if (isMyMsg) setStyle("-fx-text-fill: white;"); else setStyle("-fx-text-fill: black;"); } }); item.getAttachments() .stream() .forEach( attachment -> { final Label icon = new Label(); setPadding(new Insets(0, 0, 3, 0)); if (isMyMsg) icon.getStyleClass().add("attachment-icon"); else icon.getStyleClass().add("attachment-icon-black"); AwesomeDude.setIcon(icon, AwesomeIcon.FILE_TEXT); icon.setPadding(new Insets(-2, 0, 0, 0)); icon.setTooltip(new Tooltip(attachment.getFileName())); icon.setOnMouseClicked(event -> onOpenAttachment(attachment)); attachmentsBox.getChildren().add(icon); }); } else { attachmentsBox.getChildren().clear(); AnchorPane.setBottomAnchor(messageLabel, bottomBorder + 10); } // TODO There are still some cell rendering issues on updates setGraphic(messageAnchorPane); } else { messageAnchorPane.prefWidthProperty().unbind(); AnchorPane.clearConstraints(bg); AnchorPane.clearConstraints(headerLabel); AnchorPane.clearConstraints(arrow); AnchorPane.clearConstraints(messageLabel); AnchorPane.clearConstraints(statusIcon); AnchorPane.clearConstraints(attachmentsBox); setGraphic(null); } } /* private void showNotArrivedIcon() { statusIcon.setVisible(true); AwesomeDude.setIcon(statusIcon, AwesomeIcon.WARNING_SIGN, "14"); Tooltip.install(statusIcon, new Tooltip("Message did not arrive. Please try to send again.")); statusIcon.setTextFill(Paint.valueOf("#dd0000")); }*/ private void showMailboxIcon() { statusIcon.setVisible(true); AwesomeDude.setIcon(statusIcon, AwesomeIcon.ENVELOPE_ALT, "14"); Tooltip.install(statusIcon, new Tooltip("Message saved in receivers mailbox")); statusIcon.setTextFill(Paint.valueOf("#0f87c3")); } private void showArrivedIcon() { statusIcon.setVisible(true); AwesomeDude.setIcon(statusIcon, AwesomeIcon.OK, "14"); Tooltip.install(statusIcon, new Tooltip("Message arrived at receiver")); statusIcon.setTextFill(Paint.valueOf("#0f87c3")); } }; } }); if (root.getChildren().size() > 1) root.getChildren().remove(1); root.getChildren().add(1, messagesAnchorPane); scrollToBottom(); } }
@Override public void start(Stage primaryStage) { VBox loginPane = new VBox(); loginPane.setAlignment(Pos.CENTER); primaryStage.setTitle("Amoeba"); primaryStage.setScene(new Scene(loginPane, 800, 600)); primaryStage.show(); TextField username = new TextField(); username.setPromptText("Username"); username.setMaxSize(200, 50); Timeline renderTimer = new Timeline(); Timeline inputTimer = new Timeline(); ColorPicker colorPicker = new ColorPicker(); colorPicker.setEditable(true); colorPicker.setValue(Color.BLACK); lagLabel.setTranslateX(25); lagLabel.setTranslateY(10); lagLabel.setTextFill(Color.RED); TextField address = new TextField("localhost"); address.setPromptText("Server IP Address"); address.setMaxSize(200, 50); TextField port = new TextField("8080"); port.setPromptText("Port Number"); port.setMaxSize(200, 50); Button btn = new Button("Play"); btn.setDefaultButton(true); loginPane.getChildren().addAll(username, colorPicker, address, port, btn); primaryStage.setResizable(false); music.setCycleCount(MediaPlayer.INDEFINITE); renderPane.setBackground(new Background(backgroundImage)); renderPane.setPrefSize(800, 600); chatBox.setPrefSize(400, 100); chatBox.setTranslateX(0); chatBox.setTranslateY(468); chatBox.setWrapText(true); chatBox.setEditable(false); chatBox.setStyle("-fx-opacity: 0.5"); chatInput.setPrefSize(400, 10); chatInput.setTranslateX(0); chatInput.setTranslateY(570); chatInput.setStyle("-fx-opacity: 0.8"); highScores.setPrefSize(400, 210); highScores.setEditable(false); currentScores.setPrefSize(400, 250); currentScores.setEditable(false); currentScores.setLayoutY(210); scores.setTranslateX(-390); scores.setTranslateY(0); scores.setStyle("-fx-opacity: 0.8"); scores.setOnMouseEntered( (e) -> { scores.setTranslateX(0); }); scores.setOnMouseExited( (e) -> { if (e.getX() > 350) { scores.setTranslateX(-390); } }); btn.setOnAction( (e) -> { if (!networkController.isConnected()) { networkController.connect(address.getText(), Integer.parseInt(port.getText())); if (username.getText().isEmpty()) { username.setText("Anonymous"); } networkController.sendMessage(new LoginMessage(username.getText(), "")); ArrayList<KeyValuePair> properties = new ArrayList<>(); properties.add(new KeyValuePair("color", colorPicker.getValue().toString())); networkController.sendMessage(new SetBlobPropertiesMessage(properties)); primaryStage.setScene(new Scene(renderPane)); renderTimer.play(); inputTimer.play(); music.play(); } }); inputTimer.setCycleCount(Timeline.INDEFINITE); inputTimer .getKeyFrames() .add( new KeyFrame( Duration.seconds(0.05), (e) -> { if (sendCoordinates) { networkController.sendMessage(new MoveTowardCoordinatesMessage(x, y)); } })); renderTimer.setCycleCount(Timeline.INDEFINITE); renderTimer .getKeyFrames() .add( new KeyFrame( Duration.seconds(0.01), (e) -> { render(); })); renderPane.setOnMouseReleased( (e) -> { this.sendCoordinates = false; }); renderPane.setOnMouseDragged( (e) -> { this.sendCoordinates = true; this.x = e.getX(); this.y = e.getY(); }); renderPane.setOnMouseClicked( (e) -> { if (e.getButton() == MouseButton.SECONDARY) { networkController.sendMessage(new BoostMessage()); } else if (e.getClickCount() > 1 || e.getButton() == MouseButton.MIDDLE) { networkController.sendMessage(new SplitMessage()); } }); renderPane.setOnKeyPressed( (e) -> { if (e.getCode().equals(KeyCode.ENTER) && !chatInput.getText().isEmpty()) { networkController.sendMessage(new ChatMessage("", chatInput.getText())); chatInput.clear(); } }); primaryStage.setOnCloseRequest( (e) -> { networkController.sendMessage(new LogoutMessage()); try { Thread.sleep(100); } catch (InterruptedException ex) { Logger.getLogger(AmoebaClient.class.getName()).log(Level.WARNING, null, ex); } finally { System.exit(0); } }); }
private void render() { ArrayList<NetworkMessage> messages = networkController.getMessages(); for (NetworkMessage message : messages) { String messageType = message.getClass().getName().replace("NetworkMessages.", "").replace("Messages.", ""); switch (messageType) { case "PelletPositionMessage": { PelletPositionMessage m = (PelletPositionMessage) message; Drawable drawable = getCachedDrawable(m.id); if (drawable != null) { drawable.node.relocate(m.x, m.y); drawable.cacheMisses = 0; } else { Random r = new Random(); Node node = drawCircle( m.x, m.y, 5, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1)); drawableCache.add(new Drawable(m.id, node)); } } break; case "BlobStateMessage": { BlobStateMessage m = (BlobStateMessage) message; Color color = Color.web(m.color); Node node; if (color.getBrightness() != 0) { node = drawCircle(m.x, m.y, m.size / 2, color); } else { node = drawCircle(m.x, m.y, m.size / 2, color.brighter().brighter()); } Node nameText = drawText((m.x - m.username.length() * 5), (m.y - m.size / 2), m.username, color); Drawable drawable = getCachedDrawable(m.id); if (drawable != null) { node.setLayoutX((drawable.node.getTranslateX() - m.x) / 10000); node.setLayoutY((drawable.node.getTranslateY() - m.y) / 10000); drawable.node = node; drawable.cacheMisses = 0; Drawable nameTag = getCachedDrawable(m.id + 9000); nameTag.node = nameText; nameTag.cacheMisses = 0; } else { drawableCache.add(new Drawable(m.id, node)); drawableCache.add(new Drawable(m.id + 9000, nameText)); } } break; case "PingMessage": { renderPane.getChildren().clear(); ArrayList<Drawable> drawables = new ArrayList<>(drawableCache); for (Drawable drawable : drawables) { if (drawable.cacheMisses > 1) { drawableCache.remove(drawable); } drawable.cacheMisses++; renderPane.getChildren().add(drawable.node); } renderPane.getChildren().addAll(chatBox, chatInput, lagLabel, scores); lag = System.currentTimeMillis() - lastReceivedTime; lastReceivedTime = System.currentTimeMillis(); lagLabel.setText("Net Lag: " + Long.toString(lag)); } break; case "HighScoreMessage": { HighScoreMessage m = (HighScoreMessage) message; highScores.setText("HIGH SCORES:" + m.text); } break; case "CurrentScoreMessage": { CurrentScoreMessage m = (CurrentScoreMessage) message; currentScores.setText("Current Scores:" + m.text); } break; case "ChatMessage": { ChatMessage m = (ChatMessage) message; chatBox.appendText(m.username + "> " + m.text + "\n"); } break; } } }