Search in sources :

Example 1 with Track

use of net.parostroj.timetable.model.Track in project grafikon by jub77.

the class TrackCellEditor method getTableCellEditorComponent.

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    ignoreAction = true;
    editor.removeAllItems();
    TimeInterval interval = ((TrainTableModel) table.getModel()).getTrain().getTimeIntervalList().get(row);
    List<? extends Track> tracks = interval.getOwner().getTracks();
    for (Track track : tracks) {
        editor.addItem(track);
    }
    editor.setSelectedItem(interval.getTrack());
    ignoreAction = false;
    return editor;
}
Also used : TimeInterval(net.parostroj.timetable.model.TimeInterval) Track(net.parostroj.timetable.model.Track)

Example 2 with Track

use of net.parostroj.timetable.model.Track in project grafikon by jub77.

the class TrainTableModel method setValueAt.

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    int time = 0;
    editBlock = true;
    TimeInterval interval = train.getTimeIntervalList().get(rowIndex);
    TrainTableColumn column = TrainTableColumn.getColumn(columnIndex);
    switch(column) {
        case START:
            time = converter.convertTextToInt((String) aValue);
            if (time != -1) {
                int oldTime = TimeUtil.normalizeTime(interval.getStart());
                int newTime = TimeUtil.normalizeTime(time);
                int newStartTime = TimeUtil.normalizeTime(train.getStartTime() + (newTime - oldTime));
                train.move(newStartTime);
                this.fireTableRowsUpdated(0, lastRow);
            }
            break;
        case END:
            // departure
            time = converter.convertTextToInt((String) aValue);
            if (time != -1) {
                if (rowIndex == 0) {
                    train.move(time);
                    this.fireTableDataChanged();
                } else {
                    int start = TimeUtil.normalizeTime(interval.getStart());
                    time = TimeUtil.normalizeTime(time);
                    if (time < start) {
                        time += TimeInterval.DAY;
                    }
                    int newStop = time - start;
                    if (newStop >= 0) {
                        train.changeStopTime(interval, newStop);
                        this.fireTableRowsUpdated(rowIndex - 1, lastRow);
                    }
                }
            }
            break;
        case STOP:
            // stop time
            try {
                time = converter.convertMinutesTextToInt((String) aValue);
            } catch (ParseException e) {
                // wrong conversion doesn't change anything
                time = -1;
            }
            if (time >= 0) {
                boolean originalIsFreight = interval.isFreight();
                train.changeStopTime(interval, time);
                int firstRow = rowIndex - 1;
                if (interval.isFreight() != originalIsFreight) {
                    firstRow = 0;
                }
                this.fireTableRowsUpdated(firstRow, lastRow);
            }
            break;
        case SPEED_LIMIT:
            // velocity limit
            Integer velocity = (Integer) aValue;
            if (velocity == null || velocity > 0) {
                train.changeSpeedAndAddedTime(interval, velocity, interval.getAddedTime());
                this.fireTableRowsUpdated(rowIndex - 2 >= 0 ? rowIndex - 2 : 0, lastRow);
            }
            break;
        case ADDED_TIME:
            // added time
            if (aValue != null) {
                int addedTime;
                try {
                    addedTime = ObjectsUtil.isEmpty((String) aValue) ? 0 : converter.convertMinutesTextToInt((String) aValue);
                } catch (ParseException e) {
                    // wrong conversion doesn't change anything
                    addedTime = -1;
                }
                if (addedTime >= 0) {
                    train.changeSpeedAndAddedTime(interval, interval.getSpeedLimit(), addedTime);
                }
            } else {
                train.changeSpeedAndAddedTime(interval, interval.getSpeedLimit(), 0);
            }
            this.fireTableRowsUpdated(rowIndex, lastRow);
            break;
        case PLATFORM:
            // platform
            Track track = (Track) aValue;
            if (interval.isNodeOwner()) {
                NodeTrack newTrack = (NodeTrack) track;
                if (newTrack != null) {
                    train.changeNodeTrack(interval, newTrack);
                    this.fireTableRowsUpdated(rowIndex, rowIndex);
                }
            } else if (interval.isLineOwner()) {
                LineTrack newTrack = (LineTrack) track;
                if (newTrack != null) {
                    train.changeLineTrack(interval, newTrack);
                    this.fireTableRowsUpdated(rowIndex, rowIndex);
                }
            }
            break;
        case COMMENT:
            // comment
            String commentStr = ObjectsUtil.checkAndTrim((String) aValue);
            LocalizedString lComment = null;
            if (commentStr != null) {
                lComment = LocalizedString.newBuilder(interval.getComment()).setDefaultString(commentStr).build();
            }
            interval.getAttributes().setRemove(TimeInterval.ATTR_COMMENT, lComment);
            break;
        case OCCUPIED_ENTRY:
            // entry of the occupied track
            interval.getAttributes().setBool(TimeInterval.ATTR_OCCUPIED, (Boolean) aValue);
            break;
        case SHUNT:
            // entry shunting on the far side
            interval.getAttributes().setBool(TimeInterval.ATTR_SHUNT, (Boolean) aValue);
            break;
        case COMMENT_SHOWN:
            // entry shunting on the far side
            interval.getAttributes().setBool(TimeInterval.ATTR_COMMENT_SHOWN, (Boolean) aValue);
            break;
        case SET_SPEED:
            // train speed
            Integer trainSpeed = (Integer) aValue;
            if (trainSpeed != null && trainSpeed <= 0) {
                trainSpeed = null;
            }
            interval.getAttributes().setRemove(TimeInterval.ATTR_SET_SPEED, trainSpeed);
            break;
        case IGNORE_LENGTH:
            // ignore length of the station in computation
            interval.getAttributes().setBool(TimeInterval.ATTR_IGNORE_LENGTH, (Boolean) aValue);
            this.fireTableRowsUpdated(rowIndex, rowIndex);
            break;
        case MANAGED_FREIGHT:
            interval.getAttributes().setBool(TimeInterval.ATTR_NOT_MANAGED_FREIGHT, !((Boolean) aValue));
            this.fireTableRowsUpdated(0, lastRow);
            break;
        case REGION_CENTER_TRANSFER:
            interval.setAttributeAsBool(TimeInterval.ATTR_NO_REGION_CENTER_TRANSFER, !((Boolean) aValue));
            this.fireTableRowsUpdated(0, lastRow);
            break;
        default:
            break;
    }
    editBlock = false;
}
Also used : NodeTrack(net.parostroj.timetable.model.NodeTrack) TimeInterval(net.parostroj.timetable.model.TimeInterval) LocalizedString(net.parostroj.timetable.model.LocalizedString) ParseException(java.text.ParseException) LocalizedString(net.parostroj.timetable.model.LocalizedString) LineTrack(net.parostroj.timetable.model.LineTrack) NodeTrack(net.parostroj.timetable.model.NodeTrack) Track(net.parostroj.timetable.model.Track) LineTrack(net.parostroj.timetable.model.LineTrack)

Aggregations

TimeInterval (net.parostroj.timetable.model.TimeInterval)2 Track (net.parostroj.timetable.model.Track)2 ParseException (java.text.ParseException)1 LineTrack (net.parostroj.timetable.model.LineTrack)1 LocalizedString (net.parostroj.timetable.model.LocalizedString)1 NodeTrack (net.parostroj.timetable.model.NodeTrack)1