Example usage for javafx.scene.control TablePosition getTableColumn

List of usage examples for javafx.scene.control TablePosition getTableColumn

Introduction

In this page you can find the example usage for javafx.scene.control TablePosition getTableColumn.

Prototype

@Override
public final TableColumn<S, T> getTableColumn() 

Source Link

Usage

From source file:com.helegris.szorengeteg.ui.forms.TopicFormView.java

/**
 * Handles click events on the table. When clicking on the "image" or
 * "audio" column of a word, it opens a new stage for editing them.
 *
 * @param event//  ww w. jav a2 s .c  o m
 */
private void tableClick(MouseEvent event) {
    if (!rows.isEmpty() && !tableView.getSelectionModel().getSelectedCells().isEmpty()) {
        TablePosition position = tableView.getSelectionModel().getSelectedCells().get(0);
        if (colImage.equals(position.getTableColumn())) {
            int index = position.getRow();
            RowForCard row = (RowForCard) sortedRows.get(index);
            Image currentImage = row.getImageView().getImage();
            ImagePopup imagePopup = new ImagePopup(currentImage);
            Stage stage = new Stage();
            stage.setScene(new SceneStyler().createScene(imagePopup, SceneStyler.Style.TOPIC_LIST));
            stage.setTitle(Messages.msg("form.set_image_of_word", row.getTxtWord().getText()));
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.initOwner(tableView.getScene().getWindow());
            stage.setResizable(false);
            tableView.getSelectionModel().clearSelection();

            stage.showAndWait();
            if (imagePopup.isOk()) {
                Image rowImage = imagePopup.getFinalImage();
                File cardImageFile = imagePopup.getFile();
                if (rowImage != null && cardImageFile != null) {
                    row.setImageFile(cardImageFile);
                    row.setImage(rowImage);
                } else if (rowImage == null) {
                    row.setImage(DefaultImage.getInstance());
                }
            }
        } else if (colAudio.equals(position.getTableColumn())) {
            int index = position.getRow();
            RowForCard row = (RowForCard) sortedRows.get(index);
            AudioPopup audioPopup = new AudioPopup(row.getAudioIcon().getAudio());
            Stage stage = new Stage();
            stage.setScene(new SceneStyler().createScene(audioPopup, SceneStyler.Style.TOPIC_LIST));
            stage.setTitle(Messages.msg("form.set_audio_of_word", row.getTxtWord().getText()));
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.initOwner(tableView.getScene().getWindow());
            stage.setResizable(false);
            tableView.getSelectionModel().clearSelection();

            stage.showAndWait();
            if (audioPopup.isOk()) {
                File audioFile = audioPopup.getFile();
                row.setAudioFile(audioFile);
                row.setAudio(audioPopup.getFinalAudio());
            }
        }
    }
}

From source file:jp.ac.tohoku.ecei.sb.metabolome.lims.gui.MainWindowController.java

@FXML
void onTableClicked(MouseEvent event) {
    if (event.getClickCount() != 2)
        return;//from   w  w  w.ja v a2  s.c  o  m
    TableView tableView = ((TableView) event.getSource());
    TableView.TableViewFocusModel focusModel = (TableView.TableViewFocusModel) tableView.focusModelProperty()
            .getValue();
    TablePosition position = focusModel.getFocusedCell();
    if (position.getTableColumn() == null)
        return;
    ObservableValue value = position.getTableColumn().getCellObservableValue(position.getRow());
    String valueStr = value.getValue().toString();
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setHeaderText(String.format("Value at ID:%d  %s",
            ((LIMSData) tableView.getItems().get(position.getRow())).getId(),
            position.getTableColumn().getText()));
    if (valueStr.length() > 300)
        alert.setContentText(valueStr.substring(0, 300) + "...");
    else
        alert.setContentText(valueStr);

    TextArea textArea = new TextArea(valueStr);
    textArea.setEditable(false);
    alert.getDialogPane().setExpandableContent(textArea);

    event.consume();
    alert.show();
}

From source file:com.github.drbookings.ui.controller.MainController.java

private ListChangeListener<TablePosition> getCellSelectionListener() {
    return change -> {
        final List<RoomBean> cells = new ArrayList<>();
        final List<DateBean> dates = new ArrayList<>();
        for (final TablePosition tp : change.getList()) {
            final int r = tp.getRow();
            final int c = tp.getColumn();
            final Object cell = tp.getTableColumn().getCellData(r);
            if (cell instanceof DateBean) {
                final RoomBean room = ((DateBean) cell).getRoom("" + c);
                if (room != null) {
                    cells.add(room);//from w  ww .ja va2  s  .c o  m
                }
                dates.add((DateBean) cell);
            }
        }
        RoomBeanSelectionManager.getInstance().setSelection(cells);
        DateBeanSelectionManager.getInstance().setSelection(dates);
    };
}

From source file:com.github.drbookings.ui.controller.MainController.java

private void deleteSelected() {
    final ObservableList<TablePosition> selectedItems = tableView.getSelectionModel().getSelectedCells();
    if (logger.isDebugEnabled()) {
        logger.debug("Delete " + selectedItems);
    }/*  w  ww .j av  a 2s. c  o  m*/
    for (final TablePosition<DateBean, ?> tp : selectedItems) {
        final int c = tp.getColumn();
        final int r = tp.getRow();
        final TableColumn<DateBean, ?> tableColumn = tp.getTableColumn();
        final Object cellData = tableColumn.getCellData(r);
        if (logger.isDebugEnabled()) {
            logger.debug("Delete in column " + c + ", " + cellData);
        }
        if (cellData instanceof DateBean) {
            final DateBean db = (DateBean) cellData;
            if (db.getRooms().isEmpty()) {

            } else {
                final RoomBean rb = db.getRoom("" + c);
                if (rb.isEmpty()) {
                    continue;
                }
                final BookingBean booking = rb.getFilteredBookingEntries().get(0).getElement();
                if (logger.isDebugEnabled()) {
                    logger.debug("Deleting " + booking);
                }
                manager.removeBooking(booking);
            }
        }
    }
}

From source file:com.panemu.tiwulfx.table.TableControl.java

public TableControl(Class<R> recordClass) {
    this.recordClass = recordClass;
    initControls();/*from  www.j av  a 2 s  .  co  m*/

    tblView.getSortOrder().addListener(new ListChangeListener<TableColumn<R, ?>>() {
        @Override
        public void onChanged(ListChangeListener.Change<? extends TableColumn<R, ?>> change) {
            reload();
        }
    });

    btnAdd.disableProperty().bind(mode.isEqualTo(Mode.EDIT));
    btnEdit.disableProperty().bind(mode.isNotEqualTo(Mode.READ));
    btnSave.disableProperty().bind(mode.isEqualTo(Mode.READ));
    btnDelete.disableProperty().bind(new BooleanBinding() {
        {
            super.bind(mode, tblView.getSelectionModel().selectedItemProperty(), lstChangedRow);
        }

        @Override
        protected boolean computeValue() {
            if ((mode.get() == Mode.INSERT && lstChangedRow.size() < 2)
                    || tblView.getSelectionModel().selectedItemProperty().get() == null
                    || mode.get() == Mode.EDIT) {
                return true;
            }
            return false;
        }
    });
    tblView.editableProperty().bind(mode.isNotEqualTo(Mode.READ));
    tblView.getSelectionModel().cellSelectionEnabledProperty().bind(tblView.editableProperty());
    mode.addListener(new ChangeListener<Mode>() {
        @Override
        public void changed(ObservableValue<? extends Mode> ov, Mode t, Mode t1) {
            if (t1 == Mode.READ) {
                directEdit = false;
            }
        }
    });

    tblView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
            lblRowIndex
                    .setText(TiwulFXUtil.getLiteral("row.param", (page * maxResult.get() + t1.intValue() + 1)));
        }
    });

    tblView.getFocusModel().focusedCellProperty().addListener(new ChangeListener<TablePosition>() {
        @Override
        public void changed(ObservableValue<? extends TablePosition> observable, TablePosition oldValue,
                TablePosition newValue) {
            if (tblView.isEditable() && directEdit && agileEditing.get()) {
                tblView.edit(newValue.getRow(), newValue.getTableColumn());
            }
        }
    });

    tblView.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.ESCAPE) {
                directEdit = false;
            } else if (event.getCode() == KeyCode.ENTER && mode.get() == Mode.READ) {
                getController().doubleClick(getSelectionModel().getSelectedItem());
            }
        }
    });

    /**
     * Define policy for TAB key press
     */
    tblView.addEventFilter(KeyEvent.KEY_PRESSED, tableKeyListener);
    /**
     * In INSERT mode, only inserted row that is focusable
     */
    tblView.getFocusModel().focusedCellProperty().addListener(tableFocusListener);

    tblView.setOnMouseReleased(tableRightClickListener);

    if (Platform.isFxApplicationThread()) {
        cm = new ContextMenu();
        cm.setAutoHide(true);
        setToolTips();
        /**
         * create custom row factory that can intercept double click on grid row
         */
        tblView.setRowFactory(new Callback<TableView<R>, TableRow<R>>() {
            @Override
            public TableRow<R> call(TableView<R> param) {
                return new TableRowControl(TableControl.this);
            }
        });
    }

    columns.addListener(new ListChangeListener<TableColumn<R, ?>>() {
        @Override
        public void onChanged(ListChangeListener.Change<? extends TableColumn<R, ?>> change) {
            while (change.next()) {
                if (change.wasAdded()) {
                    for (TableColumn<R, ?> column : change.getAddedSubList()) {
                        initColumn(column);
                    }
                }
                lastColumnIndex = getLeafColumns().size() - 1;
            }
        }
    });
    attachWindowVisibilityListener();
}

From source file:com.panemu.tiwulfx.table.TableControl.java

/**
 * Paste text on clipboard. Doesn't work on READ mode.
 *//*from   www.  j  a v  a  2s .co m*/
public void paste() {
    if (mode.get() == Mode.READ) {
        return;
    }
    final Clipboard clipboard = Clipboard.getSystemClipboard();
    if (clipboard.hasString()) {
        final String text = clipboard.getString();
        if (text != null) {
            List<TablePosition> cells = tblView.getSelectionModel().getSelectedCells();
            if (cells.isEmpty()) {
                return;
            }
            TablePosition cell = cells.get(0);
            List<TableColumn<R, ?>> lstColumn = getLeafColumns();
            TableColumn startColumn = null;
            for (TableColumn clm : lstColumn) {
                if (clm instanceof BaseColumn && clm == cell.getTableColumn()) {
                    startColumn = (BaseColumn) clm;
                    break;
                }
            }
            if (startColumn == null) {
                return;
            }
            int rowIndex = cell.getRow();
            String[] arrString = text.split("\n");
            boolean stopPasting = false;
            for (String line : arrString) {
                if (stopPasting) {
                    break;
                }
                R item = null;
                if (rowIndex < tblView.getItems().size()) {
                    item = tblView.getItems().get(rowIndex);
                } else if (mode.get() == Mode.EDIT) {
                    /**
                     * Will ensure the content display to TEXT_ONLY because
                     * there is no way to update cell editors value (in
                     * agile editing mode)
                     */
                    tblView.getSelectionModel().clearSelection();
                    return;//stop pasting as it already touched last row
                }

                if (!lstChangedRow.contains(item)) {
                    if (mode.get() == Mode.INSERT) {
                        //means that selected row is not new row. Let's create new row
                        createNewRow(rowIndex);
                        item = tblView.getItems().get(rowIndex);
                    } else {
                        lstChangedRow.add(item);
                    }
                }

                showRow(rowIndex);
                /**
                 * Handle multicolumn paste
                 */
                String[] stringCellValues = line.split("\t");
                TableColumn toFillColumn = startColumn;
                tblView.getSelectionModel().select(rowIndex, toFillColumn);
                for (String stringCellValue : stringCellValues) {
                    if (toFillColumn == null) {
                        break;
                    }
                    if (toFillColumn instanceof BaseColumn && toFillColumn.isEditable()
                            && toFillColumn.isVisible()) {
                        try {
                            Object oldValue = toFillColumn.getCellData(item);
                            Object newValue = ((BaseColumn) toFillColumn).convertFromString(stringCellValue);
                            PropertyUtils.setSimpleProperty(item, ((BaseColumn) toFillColumn).getPropertyName(),
                                    newValue);
                            if (mode.get() == Mode.EDIT) {
                                ((BaseColumn) toFillColumn).addRecordChange(item, oldValue, newValue);
                            }
                        } catch (Exception ex) {
                            MessageDialog.Answer answer = MessageDialogBuilder.error(ex)
                                    .message("msg.paste.error", stringCellValue, toFillColumn.getText())
                                    .buttonType(MessageDialog.ButtonType.YES_NO)
                                    .yesOkButtonText("continue.pasting").noButtonText("stop")
                                    .show(getScene().getWindow());
                            if (answer == MessageDialog.Answer.NO) {
                                stopPasting = true;
                                break;
                            }
                        }
                    }
                    tblView.getSelectionModel().selectRightCell();
                    TablePosition nextCell = tblView.getSelectionModel().getSelectedCells().get(0);
                    if (nextCell.getTableColumn() instanceof BaseColumn
                            && nextCell.getTableColumn() != toFillColumn) {
                        toFillColumn = (BaseColumn) nextCell.getTableColumn();
                    } else {
                        toFillColumn = null;
                    }
                }
                rowIndex++;
            }

            refresh();

            /**
             * Will ensure the content display to TEXT_ONLY because there is
             * no way to update cell editors value (in agile editing mode)
             */
            tblView.getSelectionModel().clearSelection();
        }
    }
}

From source file:com.panemu.tiwulfx.table.TableControl.java

/**
 * Delete selected row. This method is called when pressing delete button.
 * It will delete selected record(s)/*  w ww .j  a  va 2s .c  o  m*/
 *
 */
public void delete() {
    /**
     * Delete row that is not yet persisted in database.
     */
    if (mode.get() == Mode.INSERT) {
        TablePosition selectedCell = tblView.getSelectionModel().getSelectedCells().get(0);
        int selectedRow = selectedCell.getRow();
        lstChangedRow.removeAll(tblView.getSelectionModel().getSelectedItems());
        tblView.getSelectionModel().clearSelection();// it is needed if agile editing is enabled to trigger content display change later
        tblView.getItems().remove(selectedRow);
        tblView.layout();//relayout first before set selection. Without this, cell contend display won't be set propertly
        tblView.requestFocus();
        if (selectedRow == tblView.getItems().size()) {
            selectedRow--;
        }
        if (lstChangedRow.contains(tblView.getItems().get(selectedRow))) {
            tblView.getSelectionModel().select(selectedRow, selectedCell.getTableColumn());
        } else {
            tblView.getSelectionModel().select(selectedRow - 1, selectedCell.getTableColumn());
        }
        return;
    }

    /**
     * Delete persistence record.
     */
    try {
        if (!controller.canDelete(this)) {
            return;
        }
        int selectedRow = tblView.getSelectionModel().getSelectedIndex();
        List<R> lstToDelete = new ArrayList<>(tblView.getSelectionModel().getSelectedItems());

        service.runDeleteInBackground(lstToDelete, selectedRow);

    } catch (Exception ex) {
        handleException(ex);
    }
}