Search in sources :

Example 21 with MModeController

use of org.freeplane.features.mode.mindmapmode.MModeController in project freeplane by freeplane.

the class DoAutomaticSave method run.

@Override
public void run() {
    /* Map is dirty enough? */
    if (model.getNumberOfChangesSinceLastSave() == changeState) {
        return;
    }
    changeState = model.getNumberOfChangesSinceLastSave();
    if (changeState == 0) {
        /* map was recently saved. */
        return;
    }
    try {
        cancel();
        Controller.getCurrentController().getViewController().invokeAndWait(new Runnable() {

            public void run() {
                /* Now, it is dirty, we save it. */
                try {
                    final ModeController currentModeController = Controller.getCurrentModeController();
                    if (!(currentModeController instanceof MModeController))
                        return;
                    MModeController modeController = ((MModeController) currentModeController);
                    final File pathToStore;
                    final URL url = model.getURL();
                    final File file = new File(// 
                    url != null ? // 
                    url.getFile() : model.getTitle() + UrlManager.FREEPLANE_FILE_EXTENSION);
                    if (url == null) {
                        pathToStore = new File(ResourceController.getResourceController().getFreeplaneUserDirectory(), BACKUP_DIR);
                    } else if (singleBackupDirectory != null) {
                        pathToStore = singleBackupDirectory;
                    } else {
                        pathToStore = new File(file.getParent(), BACKUP_DIR);
                    }
                    pathToStore.mkdirs();
                    final File tempFile = MFileManager.renameBackupFiles(pathToStore, file, numberOfFiles, AUTOSAVE_EXTENSION);
                    if (tempFile == null) {
                        return;
                    }
                    if (filesShouldBeDeletedAfterShutdown) {
                        tempFile.deleteOnExit();
                    }
                    ((MFileManager) UrlManager.getController()).saveInternal((MMapModel) model, tempFile, true);
                    modeController.getController().getViewController().out(TextUtils.format("automatically_save_message", tempFile));
                } catch (final Exception e) {
                    LogUtils.severe("Error in automatic MapModel.save(): ", e);
                }
            }
        });
    } catch (final Exception e) {
        LogUtils.severe(e);
    }
}
Also used : MMapModel(org.freeplane.features.map.mindmapmode.MMapModel) MModeController(org.freeplane.features.mode.mindmapmode.MModeController) ModeController(org.freeplane.features.mode.ModeController) File(java.io.File) URL(java.net.URL) MModeController(org.freeplane.features.mode.mindmapmode.MModeController)

Example 22 with MModeController

use of org.freeplane.features.mode.mindmapmode.MModeController in project freeplane by freeplane.

the class ExportBranchAction method actionPerformed.

public void actionPerformed(final ActionEvent e) {
    final NodeModel existingNode = Controller.getCurrentModeController().getMapController().getSelectedNode();
    final Controller controller = Controller.getCurrentController();
    final MapModel parentMap = controller.getMap();
    if (parentMap == null || existingNode == null || existingNode.isRoot()) {
        controller.getViewController().err("Could not export branch.");
        return;
    }
    if (parentMap.getFile() == null) {
        controller.getViewController().out("You must save the current map first!");
        ((MModeController) Controller.getCurrentModeController()).save();
    }
    JFileChooser chooser;
    final File file = parentMap.getFile();
    if (file == null) {
        return;
    }
    chooser = new JFileChooser(file.getParentFile());
    chooser.setSelectedFile(new File(createFileName(TextController.getController().getShortPlainText(existingNode))));
    if (((MFileManager) UrlManager.getController()).getFileFilter() != null) {
        chooser.addChoosableFileFilter(((MFileManager) UrlManager.getController()).getFileFilter());
    }
    final int returnVal = chooser.showSaveDialog(controller.getViewController().getCurrentRootComponent());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File chosenFile = chooser.getSelectedFile();
        final String ext = FileUtils.getExtension(chosenFile.getName());
        if (!ext.equals(org.freeplane.features.url.UrlManager.FREEPLANE_FILE_EXTENSION_WITHOUT_DOT)) {
            chosenFile = new File(chosenFile.getParent(), chosenFile.getName() + org.freeplane.features.url.UrlManager.FREEPLANE_FILE_EXTENSION);
        }
        try {
            Compat.fileToUrl(chosenFile);
        } catch (final MalformedURLException ex) {
            UITools.errorMessage(TextUtils.getText("invalid_url"));
            return;
        }
        if (chosenFile.exists()) {
            final int overwriteMap = JOptionPane.showConfirmDialog(controller.getMapViewManager().getMapViewComponent(), TextUtils.getText("map_already_exists"), "Freeplane", JOptionPane.YES_NO_OPTION);
            if (overwriteMap != JOptionPane.YES_OPTION) {
                return;
            }
        }
        /*
			 * Now make a copy from the node, remove the node from the map and
			 * create a new Map with the node as root, store the new Map, add
			 * the copy of the node to the parent, and set a link from the copy
			 * to the new Map.
			 */
        final NodeModel parent = existingNode.getParentNode();
        final File oldFile = parentMap.getFile();
        final URI newUri = LinkController.toLinkTypeDependantURI(oldFile, chosenFile);
        final URI oldUri = LinkController.toLinkTypeDependantURI(chosenFile, file);
        ((MLinkController) LinkController.getController()).setLink(existingNode, oldUri, LinkController.LINK_ABSOLUTE);
        final int nodePosition = parent.getIndex(existingNode);
        final ModeController modeController = Controller.getCurrentModeController();
        modeController.undoableResolveParentExtensions(LogicalStyleKeys.NODE_STYLE, existingNode);
        final MMapController mMapController = (MMapController) modeController.getMapController();
        mMapController.deleteNode(existingNode);
        {
            final IActor actor = new IActor() {

                private final boolean wasFolded = existingNode.isFolded();

                public void undo() {
                    PersistentNodeHook.removeMapExtensions(existingNode);
                    existingNode.setMap(parentMap);
                    existingNode.setFolded(wasFolded);
                }

                public String getDescription() {
                    return "ExportBranchAction";
                }

                public void act() {
                    existingNode.setFolded(false);
                }
            };
            Controller.getCurrentModeController().execute(actor, parentMap);
        }
        mMapController.newModel(existingNode);
        final MapModel newMap = existingNode.getMap();
        IExtension[] oldExtensions = newMap.getRootNode().getSharedExtensions().values().toArray(new IExtension[] {});
        for (final IExtension extension : oldExtensions) {
            final Class<? extends IExtension> clazz = extension.getClass();
            if (MapExtensions.isMapExtension(clazz)) {
                existingNode.removeExtension(clazz);
            }
        }
        final Collection<IExtension> newExtensions = parentMap.getRootNode().getSharedExtensions().values();
        for (final IExtension extension : newExtensions) {
            final Class<? extends IExtension> clazz = extension.getClass();
            if (MapExtensions.isMapExtension(clazz)) {
                existingNode.addExtension(extension);
            }
        }
        ((MFileManager) UrlManager.getController()).save(newMap, chosenFile);
        final NodeModel newNode = mMapController.addNewNode(parent, nodePosition, existingNode.isLeft());
        ((MTextController) TextController.getController()).setNodeText(newNode, existingNode.getText());
        modeController.undoableCopyExtensions(LogicalStyleKeys.NODE_STYLE, existingNode, newNode);
        newMap.getFile();
        ((MLinkController) LinkController.getController()).setLink(newNode, newUri, LinkController.LINK_ABSOLUTE);
        newMap.destroy();
        existingNode.setParent(null);
        mMapController.select(newNode);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) MMapController(org.freeplane.features.map.mindmapmode.MMapController) MapModel(org.freeplane.features.map.MapModel) ModeController(org.freeplane.features.mode.ModeController) MModeController(org.freeplane.features.mode.mindmapmode.MModeController) MTextController(org.freeplane.features.text.mindmapmode.MTextController) MMapController(org.freeplane.features.map.mindmapmode.MMapController) LinkController(org.freeplane.features.link.LinkController) Controller(org.freeplane.features.mode.Controller) TextController(org.freeplane.features.text.TextController) ModeController(org.freeplane.features.mode.ModeController) MLinkController(org.freeplane.features.link.mindmapmode.MLinkController) MModeController(org.freeplane.features.mode.mindmapmode.MModeController) URI(java.net.URI) NodeModel(org.freeplane.features.map.NodeModel) MLinkController(org.freeplane.features.link.mindmapmode.MLinkController) JFileChooser(javax.swing.JFileChooser) IExtension(org.freeplane.core.extension.IExtension) IActor(org.freeplane.core.undo.IActor) MTextController(org.freeplane.features.text.mindmapmode.MTextController) File(java.io.File) MModeController(org.freeplane.features.mode.mindmapmode.MModeController)

Example 23 with MModeController

use of org.freeplane.features.mode.mindmapmode.MModeController in project freeplane by freeplane.

the class SaveAction method actionPerformed.

public void actionPerformed(final ActionEvent e) {
    if (Controller.getCurrentController().getMap().isReadOnly()) {
        JOptionPane.showMessageDialog(Controller.getCurrentController().getMapViewManager().getMapViewComponent(), TextUtils.getText("SaveAction_readonlyMsg"), TextUtils.getText("SaveAction_readonlyTitle"), JOptionPane.WARNING_MESSAGE);
        return;
    }
    final boolean success = ((MModeController) Controller.getCurrentModeController()).save();
    final Controller controller = Controller.getCurrentController();
    if (success) {
        controller.getViewController().out(TextUtils.getText("saved"));
    } else {
        controller.getViewController().out(TextUtils.getText("saving_canceled"));
    }
    controller.getMapViewManager().setMapTitles();
}
Also used : Controller(org.freeplane.features.mode.Controller) MModeController(org.freeplane.features.mode.mindmapmode.MModeController) MModeController(org.freeplane.features.mode.mindmapmode.MModeController)

Aggregations

MModeController (org.freeplane.features.mode.mindmapmode.MModeController)23 Controller (org.freeplane.features.mode.Controller)8 ModeController (org.freeplane.features.mode.ModeController)8 MMapController (org.freeplane.features.map.mindmapmode.MMapController)6 LinkController (org.freeplane.features.link.LinkController)5 MLinkController (org.freeplane.features.link.mindmapmode.MLinkController)5 ResourceController (org.freeplane.core.resources.ResourceController)4 NodeModel (org.freeplane.features.map.NodeModel)4 Component (java.awt.Component)3 URL (java.net.URL)3 RootPaneContainer (javax.swing.RootPaneContainer)3 ClipboardController (org.freeplane.features.clipboard.ClipboardController)3 IconController (org.freeplane.features.icon.IconController)3 MIconController (org.freeplane.features.icon.mindmapmode.MIconController)3 MapController (org.freeplane.features.map.MapController)3 MapModel (org.freeplane.features.map.MapModel)3 NodeStyleController (org.freeplane.features.nodestyle.NodeStyleController)3 MNodeStyleController (org.freeplane.features.nodestyle.mindmapmode.MNodeStyleController)3 LogicalStyleController (org.freeplane.features.styles.LogicalStyleController)3 TextController (org.freeplane.features.text.TextController)3