1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
package tim.prune;
import java.io.File;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.HashSet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import tim.prune.cmd.Command;
import tim.prune.config.Config;
import tim.prune.data.LatLonRectangle;
import tim.prune.data.RecentFile;
import tim.prune.data.Track;
import tim.prune.data.TrackInfo;
import tim.prune.function.media.BlockMultipleMediaDialog;
import tim.prune.function.media.LinkedMediaLoader;
import tim.prune.function.settings.SaveConfig;
import tim.prune.gui.IconManager;
import tim.prune.gui.SidebarController;
import tim.prune.gui.Viewport;
import tim.prune.gui.colour.ColourerCaretaker;
import tim.prune.gui.colour.PointColourer;
import tim.prune.load.FileLoader;
import tim.prune.load.ItemToLoad;
import tim.prune.load.JpegLoader;
import tim.prune.load.ItemToLoad.BlockStatus;
import tim.prune.save.ExifSaver;
import tim.prune.save.FileSaver;
import tim.prune.tips.TipManager;
import tim.prune.undo.RedoManager;
import tim.prune.undo.UndoManager;
import tim.prune.undo.UndoStack;
/**
* Main controller for the application
*/
public class App
{
// Instance variables
private final JFrame _frame;
private final Config _config;
private final IconManager _iconManager;
private final Track _track;
private final TrackInfo _trackInfo;
private int _lastSavePosition = 0;
private SidebarController _sidebarController = null;
private FileLoader _fileLoader = null;
private LinkedMediaLoader _linkedMediaLoader = null;
private JpegLoader _jpegLoader = null;
private FileSaver _fileSaver = null;
private final UndoStack _undoStack;
private final UndoStack _redoStack;
private final ColourerCaretaker _colCaretaker;
private Viewport _viewport = null;
private final ArrayList<ItemToLoad> _itemsToLoad = new ArrayList<>();
private AppMode _appMode = AppMode.NORMAL;
/** Enum for the app mode - may expand later */
public enum AppMode {NORMAL, DRAWRECT_INSIDE, DRAWRECT_OUTSIDE}
/**
* Constructor
* @param inFrame frame object for application
* @param inConfig config object
*/
public App(JFrame inFrame, Config inConfig)
{
_frame = inFrame;
_config = inConfig == null ? new Config() : inConfig;
_iconManager = new IconManager(_config.getConfigBoolean(Config.KEY_ICONS_DOUBLE_SIZE));
_undoStack = new UndoStack();
_redoStack = new UndoStack();
_track = new Track();
_trackInfo = new TrackInfo(_track);
FunctionLibrary.initialise(this);
_colCaretaker = new ColourerCaretaker(this);
UpdateMessageBroker.addSubscriber(_colCaretaker);
_colCaretaker.setColourer(_config.getPointColourer());
}
/**
* @return the current TrackInfo
*/
public TrackInfo getTrackInfo() {
return _trackInfo;
}
/**
* @return the dialog frame
*/
public JFrame getFrame() {
return _frame;
}
/**
* Check if the application has unsaved data
* @return true if data is unsaved, false otherwise
*/
public boolean hasDataUnsaved()
{
return (_undoStack.size() > _lastSavePosition
&& (_track.getNumPoints() > 0 || _trackInfo.getPhotoList().hasModifiedMedia()));
}
/**
* @return the undo stack
*/
public UndoStack getUndoStack() {
return _undoStack;
}
/**
* @return the redo stack
*/
public UndoStack getRedoStack() {
return _redoStack;
}
/**
* Update the system's point colourer using the one in the Config
*/
public void updatePointColourer()
{
if (_colCaretaker != null) {
_colCaretaker.setColourer(_config.getPointColourer());
}
}
/**
* @return colourer object, or null
*/
public PointColourer getPointColourer() {
return _colCaretaker == null ? null : _colCaretaker.getColourer();
}
/**
* Show the specified tip if appropriate
* @param inTipNumber tip number from TipManager
*/
public void showTip(int inTipNumber)
{
String key = TipManager.fireTipTrigger(inTipNumber);
if (key != null && !key.isEmpty())
{
JOptionPane.showMessageDialog(_frame, I18nManager.getText(key),
I18nManager.getText("tip.title"), JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* Load the specified data files one by one
* @param inDataFiles arraylist containing File objects to load
*/
public void loadDataFiles(ArrayList<File> inDataFiles)
{
if (inDataFiles == null || inDataFiles.isEmpty()) {
return;
}
for (File file : inDataFiles) {
_itemsToLoad.add(ItemToLoad.dataFile(file));
}
loadNextItem(false); // don't auto-append, prompt when necessary
}
/**
* Load the specified linked media one by one
* @param inItems media items to load
*/
public void loadLinkedMedia(ArrayList<ItemToLoad> inItems)
{
if (inItems != null) {
_itemsToLoad.addAll(0, inItems); // prepended to start of list, not appended to end
}
}
public boolean execute(Command inCommand)
{
if (inCommand.execute(_trackInfo))
{
_undoStack.add(inCommand);
_redoStack.clear();
UpdateMessageBroker.informSubscribers(inCommand.getConfirmText());
UpdateMessageBroker.informSubscribers(inCommand.getUpdateFlags());
return true;
}
return false;
}
/**
* Open a file containing track or waypoint data
*/
public void openFile()
{
if (_fileLoader == null) {
_fileLoader = new FileLoader(this);
}
_fileLoader.openFile();
}
/**
* Add a photo or a directory of photos
*/
public void addPhotos()
{
if (_jpegLoader == null) {
_jpegLoader = new JpegLoader(this, _frame);
}
_jpegLoader.openDialog(new LatLonRectangle(_track.getLatRange(), _track.getLonRange()));
}
/**
* Save the file in the selected format
*/
public void saveFile()
{
if (_track == null) {
showErrorMessage("error.save.dialogtitle", "error.save.nodata");
}
else
{
if (_fileSaver == null) {
_fileSaver = new FileSaver(this, _frame);
}
char delim = ',';
if (_fileLoader != null) {delim = _fileLoader.getLastUsedDelimiter();}
_fileSaver.showDialog(delim);
}
}
/**
* Exit the application if confirmed
*/
public void exit()
{
// grab focus
_frame.toFront();
_frame.requestFocus();
// check if ok to exit
Object[] buttonTexts = {I18nManager.getText("button.exit"), I18nManager.getText("button.cancel")};
// Has the user got unsaved data?
if (hasDataUnsaved()
&& JOptionPane.showOptionDialog(_frame, I18nManager.getText("dialog.exit.unsaveddata.text"),
I18nManager.getText("dialog.exit.confirm.title"), JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
!= JOptionPane.YES_OPTION)
{
// No, the user chose to cancel
return;
}
// Has the user got unsaved settings?
if (_config.hasUnsavedChanges()
&& JOptionPane.showOptionDialog(_frame, I18nManager.getText("dialog.exit.unsavedsettings.text"),
I18nManager.getText("dialog.exit.confirm.title"), JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
!= JOptionPane.YES_OPTION)
{
// No, the user chose to cancel
return;
}
// Checks passed, let's save settings and exit
if (_config.getConfigBoolean(Config.KEY_AUTOSAVE_SETTINGS)) {
new SaveConfig(this).silentSave();
}
System.exit(0);
}
/**
* Select nothing
*/
public void selectNone()
{
// deselect point, range and photo
_trackInfo.getSelection().clearAll();
_trackInfo.clearAllMarkers();
}
/**
* Inform that a file has either been loaded or saved, and the recent file list should be modified
* @param inFile file loaded or saved
* @param inIsRegularLoad true for regular load, false for GPSBabel import
*/
public void addRecentFile(File inFile, boolean inIsRegularLoad)
{
_config.getRecentFileList().addFile(new RecentFile(inFile, inIsRegularLoad));
UpdateMessageBroker.informSubscribers(DataSubscriber.FILE_LOADED);
if (inIsRegularLoad && getTrackInfo().getTrack().hasSingleSourceFile()) {
informDataSaved(); // allow exit without saving, because we've only just loaded
}
}
/**
* Inform the app that a file load process is complete, either successfully or cancelled
*/
public void informDataLoadComplete()
{
_trackInfo.clearFileInfo();
UpdateMessageBroker.informSubscribers(DataSubscriber.FILE_LOADED);
// Load next item if there's a queue
loadNextItem(true); // with auto-append
}
/**
* Load the next item in the waiting list, if any
* @param inAutoAppend true to automatically append, false otherwise
*/
private void loadNextItem(boolean inAutoAppend)
{
new Thread(() -> {
checkForMultipleMediaDomains();
while (!_itemsToLoad.isEmpty())
{
ItemToLoad item = _itemsToLoad.remove(0);
if (item.isDataFile())
{
if (_fileLoader == null) {
_fileLoader = new FileLoader(this);
}
_fileLoader.openFile(item.getDataFile(), inAutoAppend);
return;
}
if (item.isUrl())
{
getLinkedMediaLoader().loadFromUrl(item.getUrl(), item.getPoint(), item.getBlockStatus());
return;
}
else if (item.isArchivedFile()) {
getLinkedMediaLoader().loadFromArchive(item.getArchiveFile(), item.getItemPath(), item.getPoint());
return;
}
}
}).start();
}
private LinkedMediaLoader getLinkedMediaLoader()
{
if (_linkedMediaLoader == null) {
_linkedMediaLoader = new LinkedMediaLoader(this);
}
return _linkedMediaLoader;
}
private void checkForMultipleMediaDomains()
{
HashSet<String> domains = new HashSet<>();
for (ItemToLoad item : _itemsToLoad)
{
if (!item.isUrl() || item.getBlockStatus() != BlockStatus.NOT_ASKED) {
continue;
}
String domain = item.getUrl().getHost();
if (!getLinkedMediaLoader().isDomainKnown(domain)) {
domains.add(domain);
}
}
if (domains.size() < 3) {
return;
}
new BlockMultipleMediaDialog(_frame, domains, _itemsToLoad).setVisible(true);
}
/**
* Save the coordinates of photos in their exif data
*/
public void saveExif()
{
ExifSaver saver = new ExifSaver(_frame, _config.getConfigString(Config.KEY_EXIFTOOL_PATH));
saver.saveExifInformation(_trackInfo.getPhotoList());
}
/**
* Inform the app that the data has been saved
*/
public void informDataSaved() {
_lastSavePosition = _undoStack.size();
}
/**
* Begin undo process
*/
public void beginUndo()
{
if (_undoStack.isEmpty())
{
// Nothing to undo
JOptionPane.showMessageDialog(_frame, I18nManager.getText("dialog.undo.none.text"),
I18nManager.getText("dialog.undo.none.title"), JOptionPane.INFORMATION_MESSAGE);
}
else {
new UndoManager(this, _frame).show(getUndoStack());
}
}
/**
* Clear the undo stack (losing all undo information)
*/
public void clearUndo()
{
// Exit if nothing to undo
if (_undoStack == null || _undoStack.isEmpty()) {
return;
}
// Has track got unsaved data?
boolean unsaved = hasDataUnsaved();
// Confirm operation with dialog
int answer = JOptionPane.showConfirmDialog(_frame,
I18nManager.getText("dialog.clearundo.text"),
I18nManager.getText("dialog.clearundo.title"),
JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
{
_undoStack.clear();
_lastSavePosition = unsaved ? -1 : 0;
UpdateMessageBroker.informSubscribers();
}
}
/**
* Undo the specified number of actions
* @param inNumUndos number of actions to undo
*/
public void undoActions(int inNumUndos)
{
try
{
for (int i=0; i<inNumUndos; i++)
{
Command command = _undoStack.popCommand();
_redoStack.add(command);
command.getInverse().execute(_trackInfo);
}
String message = (inNumUndos == 1 ? I18nManager.getText("confirm.undo.single")
: I18nManager.getTextWithNumber("confirm.undo.multi", inNumUndos));
UpdateMessageBroker.informSubscribers(message);
}
catch (EmptyStackException empty) {}
UpdateMessageBroker.informSubscribers();
}
/**
* Begin redo process
*/
public void beginRedo()
{
if (!_redoStack.isEmpty()) {
new RedoManager(this, _frame).show(_redoStack);
}
}
/**
* Redo the specified number of actions
* @param inNumActions number of actions to undo
*/
public void redoActions(int inNumActions)
{
try
{
for (int i=0; i<inNumActions; i++)
{
Command command = _redoStack.popCommand();
_undoStack.add(command);
command.execute(_trackInfo);
}
String message = (inNumActions == 1 ? I18nManager.getText("confirm.redo.single")
: I18nManager.getTextWithNumber("confirm.redo.multi", inNumActions));
UpdateMessageBroker.informSubscribers(message);
}
catch (EmptyStackException empty) {}
UpdateMessageBroker.informSubscribers();
}
/**
* @return the current data status, used for later comparison
*/
public DataStatus getCurrentDataStatus() {
return new DataStatus(_undoStack.size(), _undoStack.getNumUndos());
}
/**
* Display a standard error message
* @param inTitleKey key to lookup for window title
* @param inMessageKey key to lookup for error message
*/
public void showErrorMessage(String inTitleKey, String inMessageKey)
{
JOptionPane.showMessageDialog(_frame, I18nManager.getText(inMessageKey),
I18nManager.getText(inTitleKey), JOptionPane.ERROR_MESSAGE);
}
/**
* Display a standard error message
* @param inTitleKey key to lookup for window title
* @param inMessage error message
*/
public void showErrorMessageNoLookup(String inTitleKey, String inMessage)
{
JOptionPane.showMessageDialog(_frame, inMessage,
I18nManager.getText(inTitleKey), JOptionPane.ERROR_MESSAGE);
}
/**
* @param inViewport viewport object
*/
public void setViewport(Viewport inViewport) {
_viewport = inViewport;
}
/**
* @return current viewport object
*/
public Viewport getViewport() {
return _viewport;
}
/**
* Set the controller for the full screen mode
* @param inController controller object
*/
public void setSidebarController(SidebarController inController) {
_sidebarController = inController;
}
/**
* Toggle sidebars on and off
*/
public void toggleSidebars()
{
if (_sidebarController != null) {
_sidebarController.toggle();
}
}
/** @return current app mode */
public AppMode getCurrentMode() {
return _appMode;
}
/** @param inMode the current app mode */
public void setCurrentMode(AppMode inMode) {
_appMode = inMode;
}
/** @return config object */
public Config getConfig() {
return _config;
}
/** @return icon manager */
public IconManager getIconManager() {
return _iconManager;
}
}
|