Search in sources :

Example 6 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class SyncSystemModulesManager method updateStructures.

/**
 * Here we'll update the tree structure to be shown to the user with the changes (root).
 * The managerToNameToInfo structure has the information on the interpreter manager and related
 * interpreter infos for which the changes should be checked.
 */
public void updateStructures(IProgressMonitor monitor, final DataAndImageTreeNode root, ManagerInfoToUpdate managerToNameToInfo, CreateInterpreterInfoCallback callback) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    IImageCache imageCache = SharedCorePlugin.getImageCache();
    if (imageCache == null) {
        imageCache = new IImageCache() {

            @Override
            public IImageHandle getStringDecorated(String key, String stringToAddToDecoration) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation, String secondDecoration, int secondDecorationLocation) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration) {
                return null;
            }

            @Override
            public IImageDescriptor getDescriptor(String projectIcon) {
                return null;
            }

            @Override
            public IImageHandle get(String key) {
                return null;
            }
        };
    }
    for (Tuple<IInterpreterManager, IInterpreterInfo> infos : managerToNameToInfo.getManagerAndInfos()) {
        IInterpreterManager manager = infos.o1;
        IInterpreterInfo internalInfo = infos.o2;
        String executable = internalInfo.getExecutableOrJar();
        IInterpreterInfo newInterpreterInfo = callback.createInterpreterInfo(manager, executable, monitor);
        if (newInterpreterInfo == null) {
            continue;
        }
        DefaultPathsForInterpreterInfo defaultPaths = new DefaultPathsForInterpreterInfo();
        OrderedSet<String> newEntries = new OrderedSet<String>(newInterpreterInfo.getPythonPath());
        newEntries.removeAll(internalInfo.getPythonPath());
        // Iterate over the new entries to suggest what should be added (we already have only what's not there).
        for (Iterator<String> it = newEntries.iterator(); it.hasNext(); ) {
            String entryInPythonpath = it.next();
            if (!defaultPaths.selectByDefault(entryInPythonpath) || !defaultPaths.exists(entryInPythonpath)) {
                // Don't suggest the addition of entries in the workspace or entries which do not exist.
                it.remove();
            }
        }
        // Iterate over existing entries to suggest what should be removed.
        OrderedSet<String> removedEntries = new OrderedSet<String>();
        List<String> pythonPath = internalInfo.getPythonPath();
        for (String string : pythonPath) {
            if (!new File(string).exists()) {
                // Only suggest a removal if it was removed from the filesystem.
                removedEntries.add(string);
            }
        }
        if (newEntries.size() > 0 || removedEntries.size() > 0) {
            DataAndImageTreeNode<IInterpreterInfo> interpreterNode = new DataAndImageTreeNode<IInterpreterInfo>(root, internalInfo, imageCache.get(UIConstants.PY_INTERPRETER_ICON));
            for (String s : newEntries) {
                new DataAndImageTreeNode(interpreterNode, new PythonpathChange(s, true), imageCache.get(UIConstants.LIB_SYSTEM));
            }
            for (String s : removedEntries) {
                new DataAndImageTreeNode(interpreterNode, new PythonpathChange(s, false), imageCache.get(UIConstants.REMOVE_LIB_SYSTEM));
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) IImageCache(org.python.pydev.shared_core.image.IImageCache) IImageDescriptor(org.python.pydev.shared_core.image.IImageDescriptor) DefaultPathsForInterpreterInfo(org.python.pydev.ast.interpreter_managers.DefaultPathsForInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) File(java.io.File)

Example 7 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class PyStringCodeCompletion method fillWithEpydocFields.

/**
 * @param ret OUT: this is where the completions are stored
 */
private void fillWithEpydocFields(CompletionRequest request, List<ICompletionProposalHandle> ret) {
    try {
        Region region = new Region(request.documentOffset - request.qlen, request.qlen);
        IImageHandle image = PyCodeCompletionImages.getImageForType(IToken.TYPE_EPYDOC);
        TemplateContext context = createContext(region, request.doc);
        char c = request.doc.getChar(request.documentOffset - request.qualifier.length() - 1);
        boolean createFields = c == '@' || c == ':';
        if (createFields) {
            String lineContentsToCursor = PySelection.getLineContentsToCursor(request.doc, request.documentOffset - request.qualifier.length() - 1);
            if (lineContentsToCursor.trim().length() != 0) {
                // Only create if @param or :param is the first thing in the line.
                createFields = false;
            }
        }
        if (createFields) {
            // ok, looking for epydoc filters
            for (int i = 0; i < EPYDOC_FIELDS.length; i++) {
                String f = EPYDOC_FIELDS[i];
                if (f.startsWith(request.qualifier)) {
                    Template t = new Template(f, EPYDOC_FIELDS[i + 2], "", EPYDOC_FIELDS[i + 1], false);
                    ret.add(CompletionProposalFactory.get().createPyTemplateProposalForTests(t, context, region, image, 5));
                }
                i += 2;
            }
        }
    } catch (BadLocationException e) {
    // just ignore it
    }
}
Also used : IImageHandle(org.python.pydev.shared_core.image.IImageHandle) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) BadLocationException(org.eclipse.jface.text.BadLocationException) Template(org.eclipse.jface.text.templates.Template)

Example 8 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class ImageCache method getImageDecorated.

/**
 * @param key the key of the image that should be decorated (relative path to the plugin directory)
 * @param decoration the key of the image that should be decorated (relative path to the plugin directory)
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation, String secondDecoration, int secondDecorationLocation) {
    Display display = Display.getCurrent();
    if (display == null) {
        Log.log("This method should only be called in a UI thread.");
    }
    Object cacheKey = new Tuple4(key, decoration, decorationLocation, "imageDecoration");
    if (secondDecoration != null) {
        // Also add the second decoration to the cache key.
        cacheKey = new Tuple3(cacheKey, secondDecoration, secondDecorationLocation);
    }
    Image image = getFromImageHash(cacheKey);
    if (image == null) {
        // Note that changing the image data gotten here won't affect the original image.
        ImageData baseImageData = (ImageData) get(key).getImageData();
        image = decorateImage(decoration, decorationLocation, display, baseImageData);
        if (secondDecoration != null) {
            image = decorateImage(secondDecoration, secondDecorationLocation, display, image.getImageData());
        }
        image = putOnImageHash(cacheKey, image);
    }
    final Image computed = image;
    return new IImageHandle() {

        @Override
        public Object getImageData() {
            return computed.getImageData();
        }

        @Override
        public Object getImage() {
            return computed;
        }
    };
}
Also used : Tuple4(org.python.pydev.shared_core.structure.Tuple4) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) ImageData(org.eclipse.swt.graphics.ImageData) Tuple3(org.python.pydev.shared_core.structure.Tuple3) Image(org.eclipse.swt.graphics.Image) Display(org.eclipse.swt.widgets.Display)

Example 9 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class ImageCache method getStringDecorated.

/**
 * @param key the key of the image that should be decorated (relative path to the plugin directory)
 * @param stringToAddToDecoration the string that should be drawn over the image
 */
@Override
public IImageHandle getStringDecorated(String key, String stringToAddToDecoration) {
    Display display = Display.getCurrent();
    if (display == null) {
        Log.log("This method should only be called in a UI thread.");
    }
    Tuple3<String, String, String> cacheKey = new Tuple3<String, String, String>(key, stringToAddToDecoration, "stringDecoration");
    Image image = getFromImageHash(cacheKey);
    if (image == null) {
        image = new Image(display, asImage(get(key)), SWT.IMAGE_COPY);
        GC gc = new GC(image);
        // Color color = new Color(display, 0, 0, 0);
        // Color color2 = new Color(display, 255, 255, 255);
        // gc.setForeground(color2);
        // gc.setBackground(color2);
        // gc.setFillRule(SWT.FILL_WINDING);
        // gc.fillRoundRectangle(2, 1, base-1, base, 2, 2);
        // gc.setForeground(color);
        // gc.drawRoundRectangle(6, 0, base, base+1, 2, 2);
        // color2.dispose();
        // color.dispose();
        Color colorBackground = new Color(display, 255, 255, 255);
        Color colorForeground = new Color(display, 0, 83, 41);
        // get TextFont from preferences
        FontData fontData = FontUtils.getFontData(IFontUsage.IMAGECACHE, true);
        fontData.setStyle(SWT.BOLD);
        Font font = new Font(display, fontData);
        try {
            gc.setForeground(colorForeground);
            gc.setBackground(colorBackground);
            gc.setTextAntialias(SWT.ON);
            gc.setFont(font);
            gc.drawText(stringToAddToDecoration, 5, 0, true);
        } catch (Exception e) {
            Log.log(e);
        } finally {
            colorBackground.dispose();
            colorForeground.dispose();
            font.dispose();
            gc.dispose();
        }
        image = putOnImageHash(cacheKey, image);
    }
    Image computed = image;
    return new IImageHandle() {

        @Override
        public Object getImageData() {
            return computed.getImageData();
        }

        @Override
        public Object getImage() {
            return computed;
        }
    };
}
Also used : IImageHandle(org.python.pydev.shared_core.image.IImageHandle) Tuple3(org.python.pydev.shared_core.structure.Tuple3) Color(org.eclipse.swt.graphics.Color) FontData(org.eclipse.swt.graphics.FontData) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC) Font(org.eclipse.swt.graphics.Font) MalformedURLException(java.net.MalformedURLException) Display(org.eclipse.swt.widgets.Display)

Example 10 with IImageHandle

use of org.python.pydev.shared_core.image.IImageHandle in project Pydev by fabioz.

the class PythonBaseModelProvider method createErrorWorkingSetWithoutChildren.

private TreeNode<LabelAndImage> createErrorWorkingSetWithoutChildren(IWorkingSet parentElement) {
    IImageHandle img = SharedUiPlugin.getImageCache().get(UIConstants.WARNING);
    TreeNode<LabelAndImage> root = new TreeNode<LabelAndImage>(parentElement, new LabelAndImage("Warning: working set: " + parentElement.getName() + " does not have any contents.", img));
    new TreeNode<>(root, new LabelAndImage("Access the menu (Ctrl+F10) to edit the working set.", null));
    new TreeNode<>(root, new LabelAndImage("Or select the working set in the tree and use Alt+Enter.", null));
    return root;
}
Also used : IImageHandle(org.python.pydev.shared_core.image.IImageHandle) TreeNode(org.python.pydev.shared_core.structure.TreeNode)

Aggregations

IImageHandle (org.python.pydev.shared_core.image.IImageHandle)10 Image (org.eclipse.swt.graphics.Image)4 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 Display (org.eclipse.swt.widgets.Display)2 IImageCache (org.python.pydev.shared_core.image.IImageCache)2 IImageDescriptor (org.python.pydev.shared_core.image.IImageDescriptor)2 Tuple3 (org.python.pydev.shared_core.structure.Tuple3)2 File (java.io.File)1 IOException (java.io.IOException)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 CompletionProposal (org.eclipse.jdt.core.CompletionProposal)1 CompletionProposalLabelProvider (org.eclipse.jdt.ui.text.java.CompletionProposalLabelProvider)1 CompositeImageDescriptor (org.eclipse.jface.resource.CompositeImageDescriptor)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1