initialize();
logFormatUsage();
}
private void initialize() {
final RouteConverter r = RouteConverter.getInstance();
formatAndRoutesModel = new UndoFormatAndRoutesModel(r.getContext().getUndoManager());
positionsSelectionModel = new PositionsSelectionModel() {
public void setSelectedPositions(int[] selectedPositions, boolean replaceSelection) {
if (replaceSelection) {
ListSelectionModel selectionModel = tablePositions.getSelectionModel();
selectionModel.clearSelection();
}
int maximumRangeLength = selectedPositions.length > 19 ? selectedPositions.length / 100 : selectedPositions.length;
new ContinousRange(selectedPositions, new RangeOperation() {
public void performOnIndex(int index) {
}
public void performOnRange(int firstIndex, int lastIndex) {
ListSelectionModel selectionModel = tablePositions.getSelectionModel();
selectionModel.addSelectionInterval(firstIndex, lastIndex);
scrollToPosition(tablePositions, firstIndex);
}
public boolean isInterrupted() {
return false;
}
}).performMonotonicallyIncreasing(maximumRangeLength);
}
};
lengthCalculator = new LengthCalculator();
lengthCalculator.initialize(getPositionsModel(), getCharacteristicsModel());
new FormatToJLabelAdapter(formatAndRoutesModel, labelFormat);
new PositionListsToJLabelAdapter(formatAndRoutesModel, labelPositionLists);
new PositionsCountToJLabelAdapter(getPositionsModel(), labelPositions);
new LengthToJLabelAdapter(getPositionsModel(), lengthCalculator, labelLength, labelDuration);
new ElevationToJLabelAdapter(getPositionsModel(), labelOverallAscend, labelOverallDescend);
registerAction(buttonNewPositionList, "new-positionlist");
registerAction(buttonRenamePositionList, "rename-positionlist");
registerAction(buttonDeletePositionList, "delete-positionlist");
buttonMovePositionToTop.addActionListener(new FrameAction() {
public void run() {
int[] selectedRows = tablePositions.getSelectedRows();
if (selectedRows.length > 0) {
getPositionsModel().top(selectedRows);
selectPositions(selectedRows);
}
}
});
buttonMovePositionUp.addActionListener(new FrameAction() {
public void run() {
int[] selectedRows = tablePositions.getSelectedRows();
if (selectedRows.length > 0) {
getPositionsModel().up(selectedRows, 1);
selectPositions(increment(selectedRows, -1));
}
}
});
buttonNewPosition.addActionListener(r.getContext().getActionManager().get("new-position"));
buttonDeletePosition.addActionListener(r.getContext().getActionManager().get("delete"));
buttonMovePositionDown.addActionListener(new FrameAction() {
public void run() {
int[] selectedRows = tablePositions.getSelectedRows();
if (selectedRows.length > 0) {
getPositionsModel().down(selectedRows, 1);
selectPositions(increment(selectedRows, +1));
}
}
});
buttonMovePositionToBottom.addActionListener(new FrameAction() {
public void run() {
int[] selectedRows = tablePositions.getSelectedRows();
if (selectedRows.length > 0) {
getPositionsModel().bottom(selectedRows);
selectPositions(selectedRows);
}
}
});
formatAndRoutesModel.addListDataListener(new AbstractListDataListener() {
public void process(ListDataEvent e) {
handleRoutesUpdate();
}
});
urlModel.addDocumentListener(new AbstractDocumentListener() {
public void process(DocumentEvent e) {
String url = urlModel.getShortUrl();
String title = (url != null ? url + " - " : "") + RouteConverter.getTitle();
RouteConverter.getInstance().getFrame().setTitle(title);
}
});
r.getUnitSystemModel().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
getPositionsModel().fireTableRowsUpdated(0, MAX_VALUE, ALL_COLUMNS);
}
});
tablePositions.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
if (getPositionsModel().isContinousRange())
return;
handlePositionsUpdate();
}
});
final ActionManager actionManager = r.getContext().getActionManager();
tablePositions.setModel(getPositionsModel());
PositionsTableColumnModel tableColumnModel = new PositionsTableColumnModel();
tablePositions.setColumnModel(tableColumnModel);
tablePositions.registerKeyboardAction(new FrameAction() {
public void run() {
actionManager.run("delete");
}
}, getKeyStroke(VK_DELETE, 0), WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
tablePositions.registerKeyboardAction(new FrameAction() {
public void run() {
selectAndScrollToPosition(tablePositions, 0, 0);
}
}, getKeyStroke(VK_HOME, 0), WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
tablePositions.registerKeyboardAction(new FrameAction() {
public void run() {
selectAndScrollToPosition(tablePositions, 0, tablePositions.getSelectedRow());
}
}, getKeyStroke(VK_HOME, SHIFT_DOWN_MASK), WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
tablePositions.registerKeyboardAction(new FrameAction() {
public void run() {
int lastRow = tablePositions.getRowCount() - 1;
selectAndScrollToPosition(tablePositions, lastRow, lastRow);
}
}, getKeyStroke(VK_END, 0), WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
tablePositions.registerKeyboardAction(new FrameAction() {
public void run() {
selectAndScrollToPosition(tablePositions, tablePositions.getRowCount() - 1, tablePositions.getSelectedRow());
}
}, getKeyStroke(VK_END, SHIFT_DOWN_MASK), WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
tablePositions.setDragEnabled(true);
tablePositions.setDropMode(ON);
TableDragAndDropHandler dropHandler = new TableDragAndDropHandler(new PanelDropHandler());
tablePositions.setTransferHandler(dropHandler);
getPositionsModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if (!isFirstToLastRow(e))
return;
if (getPositionsModel().isContinousRange())
return;
handlePositionsUpdate();
}
});
JMenuBar menuBar = Application.getInstance().getContext().getMenuBar();
tableHeaderMenu = new TableHeaderMenu(tablePositions.getTableHeader(), menuBar, getPositionsModel(),
tableColumnModel, actionManager);
JPopupMenu menu = new TablePopupMenu(tablePositions).createPopupMenu();
JMenu mergeMenu = (JMenu) findMenuComponent(menu, "merge-positionlist");
new MergePositionListMenu(mergeMenu, getPositionsView(), getFormatAndRoutesModel());
ClipboardInteractor clipboardInteractor = new ClipboardInteractor();
clipboardInteractor.watchClipboard();
actionManager.register("undo", new UndoAction());
actionManager.register("redo", new RedoAction());
actionManager.register("copy", new CopyAction(getPositionsView(), getPositionsModel(), clipboardInteractor));
actionManager.register("cut", new CutAction(getPositionsView(), getPositionsModel(), clipboardInteractor));
actionManager.register("delete", new DeleteAction(getPositionsView(), getPositionsModel()));
actionManager.register("new-position", new AddPositionAction(getPositionsView(), getPositionsModel(), getPositionsSelectionModel()));
actionManager.register("new-file", new NewFileAction(this));
actionManager.register("open", new OpenAction(this));
actionManager.register("paste", new PasteAction(getPositionsView(), getPositionsModel(), clipboardInteractor));
actionManager.register("save", new SaveAction(this));
actionManager.register("save-as", new SaveAsAction(this));
actionManager.register("select-all", new SelectAllAction(getPositionsView()));
actionManager.register("new-positionlist", new AddPositionListAction(getFormatAndRoutesModel()));
actionManager.register("rename-positionlist", new RenamePositionListAction(getFormatAndRoutesModel()));
actionManager.register("delete-positionlist", new RemovePositionListAction(getFormatAndRoutesModel()));
actionManager.register("add-coordinates", new AddCoordinatesToPositionsAction());
actionManager.register("add-elevation", new AddElevationToPositionsAction());
actionManager.register("add-postal-address", new AddPostalAddressToPositionsAction());
actionManager.register("add-populated-place", new AddPopulatedPlaceToPositionsAction());
actionManager.register("add-speed", new AddSpeedToPositionsAction());
actionManager.register("add-time", new AddTimeToPositionsAction());
actionManager.register("add-number", new AddNumberToPositionsAction());
actionManager.register("split-positionlist", new SplitPositionListAction(tablePositions, getPositionsModel(), formatAndRoutesModel));
actionManager.register("import-positionlist", new ImportPositionListAction(this));
actionManager.register("export-positionlist", new ExportPositionListAction(this));
registerKeyStroke(tablePositions, "copy");
registerKeyStroke(tablePositions, "cut");
registerKeyStroke(tablePositions, "paste");
formatAndRoutesModel.addModifiedListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
actionManager.enable("save", formatAndRoutesModel.isModified() &&
formatAndRoutesModel.getFormat() != null &&
formatAndRoutesModel.getFormat().isSupportsWriting());
}
});
UndoManager undoManager = Application.getInstance().getContext().getUndoManager();
undoManager.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
handleUndoUpdate();
}
});
handleUndoUpdate();
handleRoutesUpdate();
handlePositionsUpdate();
comboBoxChoosePositionList.setModel(formatAndRoutesModel);
comboBoxChoosePositionList.setRenderer(new RouteListCellRenderer());
comboBoxChoosePositionList.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == SELECTED) {
r.getBatchPositionAugmenter().interrupt();
formatAndRoutesModel.setSelectedItem(e.getItem());
}
}
});
comboBoxChoosePositionListCharacteristics.setModel(getCharacteristicsModel());