package org.docear.plugin.core; import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.List; import org.docear.plugin.core.features.DocearMapModelController; import org.freeplane.core.util.LogUtils; import org.freeplane.core.util.TextUtils; import org.freeplane.features.map.MapModel; import org.freeplane.features.map.mindmapmode.MMapModel; import org.freeplane.features.mapio.MapIO; import org.freeplane.features.mapio.mindmapmode.MMapIO; import org.freeplane.features.mode.Controller; import org.freeplane.features.mode.mindmapmode.MModeController; import org.freeplane.plugin.workspace.WorkspaceUtils; import org.freeplane.view.swing.map.MapView; public class MapItem { private URI uri; private MapModel map; private Boolean mapIsOpen; public MapItem(URI mapUri) { this.uri = mapUri; } public MapItem(MapModel mapModel) { this.map = mapModel; } public MapModel getModel() { Controller.getCurrentController().selectMode(MModeController.MODENAME); if (this.map == null) { URL url; String mapExtensionKey; try { url = WorkspaceUtils.resolveURI(uri).toURI().toURL(); mapExtensionKey = Controller.getCurrentController().getMapViewManager().checkIfFileIsAlreadyOpened(url); } catch (MalformedURLException e) { e.printStackTrace(); return null; } if (mapExtensionKey != null) { map = Controller.getCurrentController().getViewController().getMapViewManager().getMaps().get(mapExtensionKey); if (map != null) { return map; } } map = new MMapModel(); try { File f = WorkspaceUtils.resolveURI(uri); if (f.exists()) { Controller.getCurrentController().selectMode(MModeController.MODENAME); MMapIO mapIO = (MMapIO) MModeController.getMModeController().getExtension(MapIO.class); mapIO.load(url, map); // do not work on non-docear-mindmaps if (DocearMapModelController.getModel(map) == null) { throw new Exception("no DocearMapModel"); } map.setURL(url); map.setSaved(true); } else { map = null; } } catch (Exception e) { LogUtils.warn("org.docear.plugin.core.MapItem.getModel(): "+ e.getMessage()); map = null; } } return map; } public String getIdentifierForDialog() { if (this.uri != null) { return this.uri.getPath(); } if (this.map != null && this.map.getTitle() != null) { return TextUtils.getText("unsaved_map") + " \"" + this.map.getTitle() + "\""; } return TextUtils.getText("unknown_unsaved_map"); } @SuppressWarnings("unchecked") public boolean isMapOpen() { if (this.mapIsOpen == null) { if (this.uri == null) { this.mapIsOpen = true; } else { for (MapView view : (List<MapView>) Controller .getCurrentController().getViewController() .getMapViewManager().getMapViewVector()) { File mapFile = view.getModel().getFile(); if (mapFile != null) { URI mapUri = mapFile.toURI(); if (uri.equals(mapUri)) { this.mapIsOpen = true; } } } if(this.mapIsOpen == null) { this.mapIsOpen = false; } } } return this.mapIsOpen; } }