Search in sources :

Example 1 with BitmapImagePreview

use of org.freeplane.core.ui.components.BitmapImagePreview in project freeplane by freeplane.

the class MTextController method setImageByFileChooser.

public void setImageByFileChooser() {
    boolean picturesAmongSelecteds = false;
    final ModeController modeController = Controller.getCurrentModeController();
    for (final NodeModel node : modeController.getMapController().getSelectedNodes()) {
        final URI link = NodeLinks.getLink(node);
        if (link != null) {
            final String linkString = link.toString();
            final String lowerCase = linkString.toLowerCase();
            if (lowerCase.endsWith(".png") || lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") || lowerCase.endsWith(".gif")) {
                picturesAmongSelecteds = true;
                final String encodedLinkString = HtmlUtils.unicodeToHTMLUnicodeEntity(linkString);
                final String strText = "<html><img src=\"" + encodedLinkString + "\">";
                ((MLinkController) LinkController.getController()).setLink(node, (URI) null, LinkController.LINK_ABSOLUTE);
                setNodeText(node, strText);
            }
        }
    }
    if (picturesAmongSelecteds) {
        return;
    }
    final Controller controller = modeController.getController();
    final ViewController viewController = controller.getViewController();
    final NodeModel selectedNode = modeController.getMapController().getSelectedNode();
    final MapModel map = selectedNode.getMap();
    final File file = map.getFile();
    if (file == null && LinkController.getLinkType() == LinkController.LINK_RELATIVE_TO_MINDMAP) {
        JOptionPane.showMessageDialog(viewController.getCurrentRootComponent(), TextUtils.getText("not_saved_for_image_error"), "Freeplane", JOptionPane.WARNING_MESSAGE);
        return;
    }
    final ExampleFileFilter filter = new ExampleFileFilter();
    filter.addExtension("jpg");
    filter.addExtension("jpeg");
    filter.addExtension("png");
    filter.addExtension("gif");
    filter.setDescription(TextUtils.getText("bitmaps"));
    final UrlManager urlManager = modeController.getExtension(UrlManager.class);
    final JFileChooser chooser = urlManager.getFileChooser(null, false);
    chooser.setFileFilter(filter);
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setAccessory(new BitmapImagePreview(chooser));
    final int returnVal = chooser.showOpenDialog(viewController.getCurrentRootComponent());
    if (returnVal != JFileChooser.APPROVE_OPTION) {
        return;
    }
    final File input = chooser.getSelectedFile();
    URI uri = input.toURI();
    if (uri == null) {
        return;
    }
    // bad hack: try to interpret file as http link
    if (!input.exists()) {
        uri = LinkController.toRelativeURI(map.getFile(), input, LinkController.LINK_RELATIVE_TO_MINDMAP);
        if (uri == null || !"http".equals(uri.getScheme())) {
            UITools.errorMessage(TextUtils.format("file_not_found", input.toString()));
            return;
        }
    } else if (LinkController.getLinkType() != LinkController.LINK_ABSOLUTE) {
        uri = LinkController.toLinkTypeDependantURI(map.getFile(), input);
    }
    String uriString = uri.toString();
    if (uriString.startsWith("http:/")) {
        uriString = "http://" + uriString.substring("http:/".length());
    }
    final String strText = "<html><img src=\"" + uriString + "\">";
    setNodeText(selectedNode, strText);
}
Also used : UrlManager(org.freeplane.features.url.UrlManager) ModeController(org.freeplane.features.mode.ModeController) MModeController(org.freeplane.features.mode.mindmapmode.MModeController) MapModel(org.freeplane.features.map.MapModel) MMapController(org.freeplane.features.map.mindmapmode.MMapController) MapController(org.freeplane.features.map.MapController) MIconController(org.freeplane.features.icon.mindmapmode.MIconController) IconController(org.freeplane.features.icon.IconController) Controller(org.freeplane.features.mode.Controller) TextController(org.freeplane.features.text.TextController) MLinkController(org.freeplane.features.link.mindmapmode.MLinkController) NodeStyleController(org.freeplane.features.nodestyle.NodeStyleController) FormatController(org.freeplane.features.format.FormatController) LogicalStyleController(org.freeplane.features.styles.LogicalStyleController) ResourceController(org.freeplane.core.resources.ResourceController) ViewController(org.freeplane.features.ui.ViewController) LinkController(org.freeplane.features.link.LinkController) ModeController(org.freeplane.features.mode.ModeController) MNodeStyleController(org.freeplane.features.nodestyle.mindmapmode.MNodeStyleController) MModeController(org.freeplane.features.mode.mindmapmode.MModeController) ScannerController(org.freeplane.features.format.ScannerController) URI(java.net.URI) NodeModel(org.freeplane.features.map.NodeModel) MLinkController(org.freeplane.features.link.mindmapmode.MLinkController) JFileChooser(javax.swing.JFileChooser) ViewController(org.freeplane.features.ui.ViewController) BitmapImagePreview(org.freeplane.core.ui.components.BitmapImagePreview) ExampleFileFilter(org.freeplane.core.ui.ExampleFileFilter) File(java.io.File)

Aggregations

File (java.io.File)1 URI (java.net.URI)1 JFileChooser (javax.swing.JFileChooser)1 ResourceController (org.freeplane.core.resources.ResourceController)1 ExampleFileFilter (org.freeplane.core.ui.ExampleFileFilter)1 BitmapImagePreview (org.freeplane.core.ui.components.BitmapImagePreview)1 FormatController (org.freeplane.features.format.FormatController)1 ScannerController (org.freeplane.features.format.ScannerController)1 IconController (org.freeplane.features.icon.IconController)1 MIconController (org.freeplane.features.icon.mindmapmode.MIconController)1 LinkController (org.freeplane.features.link.LinkController)1 MLinkController (org.freeplane.features.link.mindmapmode.MLinkController)1 MapController (org.freeplane.features.map.MapController)1 MapModel (org.freeplane.features.map.MapModel)1 NodeModel (org.freeplane.features.map.NodeModel)1 MMapController (org.freeplane.features.map.mindmapmode.MMapController)1 Controller (org.freeplane.features.mode.Controller)1 ModeController (org.freeplane.features.mode.ModeController)1 MModeController (org.freeplane.features.mode.mindmapmode.MModeController)1 NodeStyleController (org.freeplane.features.nodestyle.NodeStyleController)1