From e869e1bfac7ffa6f24e431b9f23c56d12b554b81 Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Mon, 19 Aug 2013 19:52:10 +0530 Subject: [PATCH] icons in completion list, loading icons only once --- .../mode/experimental/CompletionPanel.java | 49 ++++++++++++++++++- .../mode/experimental/ExperimentalMode.java | 19 ++++++- .../mode/experimental/SketchOutline.java | 19 ++----- .../mode/experimental/TextArea.java | 2 +- 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/pdex/src/processing/mode/experimental/CompletionPanel.java b/pdex/src/processing/mode/experimental/CompletionPanel.java index f054ccfd2..0336c72e9 100644 --- a/pdex/src/processing/mode/experimental/CompletionPanel.java +++ b/pdex/src/processing/mode/experimental/CompletionPanel.java @@ -4,13 +4,18 @@ import static processing.mode.experimental.ExperimentalMode.logE; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.io.File; import javax.swing.BorderFactory; import javax.swing.DefaultListModel; +import javax.swing.Icon; +import javax.swing.ImageIcon; +import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; @@ -31,10 +36,13 @@ public class CompletionPanel { private TextArea textarea; private JScrollPane scrollPane; + + protected DebugEditor editor; public CompletionPanel(JEditTextArea textarea, int position, String subWord, - DefaultListModel items, Point location) { + DefaultListModel items, Point location, DebugEditor dedit) { this.textarea = (TextArea) textarea; + editor = dedit; this.insertionPosition = position; if (subWord.indexOf('.') != -1) this.subWord = subWord.substring(subWord.lastIndexOf('.') + 1); @@ -80,6 +88,7 @@ public class CompletionPanel { } } }); + list.setCellRenderer(new CustomListRenderer()); return list; } @@ -189,4 +198,42 @@ public class CompletionPanel { // }; // }); } + + protected class CustomListRenderer extends + javax.swing.DefaultListCellRenderer { + //protected final ImageIcon classIcon, fieldIcon, methodIcon; + + public Component getListCellRendererComponent(JList list, Object value, + int index, + boolean isSelected, + boolean cellHasFocus) { + JLabel label = (JLabel) super.getListCellRendererComponent(list, value, + index, + isSelected, + cellHasFocus); + if (value instanceof CompletionCandidate) { + CompletionCandidate cc = (CompletionCandidate) value; + switch (cc.getType()) { + case CompletionCandidate.LOCAL_FIELD: + case CompletionCandidate.PREDEF_FIELD: + label.setIcon(editor.dmode.fieldIcon); + break; + case CompletionCandidate.LOCAL_METHOD: + case CompletionCandidate.PREDEF_METHOD: + label.setIcon(editor.dmode.methodIcon); + break; + case CompletionCandidate.LOCAL_CLASS: + case CompletionCandidate.PREDEF_CLASS: + label.setIcon(editor.dmode.classIcon); + break; + + default: + break; + } + + } + return label; + } + } + } \ No newline at end of file diff --git a/pdex/src/processing/mode/experimental/ExperimentalMode.java b/pdex/src/processing/mode/experimental/ExperimentalMode.java index 8619af055..783eeeba9 100755 --- a/pdex/src/processing/mode/experimental/ExperimentalMode.java +++ b/pdex/src/processing/mode/experimental/ExperimentalMode.java @@ -28,6 +28,9 @@ import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; + +import javax.swing.ImageIcon; + import processing.app.Base; import processing.app.Editor; import processing.app.EditorState; @@ -43,7 +46,7 @@ public class ExperimentalMode extends JavaMode { public static final boolean VERBOSE_LOGGING = true; //public static final boolean VERBOSE_LOGGING = false; public static final int LOG_SIZE = 512 * 1024; // max log file size (in bytes) - public static boolean DEBUG = !true; + public static boolean DEBUG = true; public ExperimentalMode(Base base, File folder) { super(base, folder); @@ -96,6 +99,7 @@ public class ExperimentalMode extends JavaMode { // String titleAndVersion = p.getImplementationTitle() + " (v" + p.getImplementationVersion() + ")"; // //log(titleAndVersion); // Logger.getLogger(ExperimentalMode.class.getName()).log(Level.INFO, titleAndVersion); + loadIcons(); } @@ -156,6 +160,19 @@ public class ExperimentalMode extends JavaMode { Logger.getLogger(ExperimentalMode.class.getName()).log(Level.WARNING, "Error loading Color: {0}", attribute); return defaultValue; } + + protected ImageIcon classIcon, fieldIcon, methodIcon; + protected void loadIcons(){ + String iconPath = getContentFile("data") + .getAbsolutePath() + + File.separator + "icons"; + classIcon = new ImageIcon(iconPath + File.separator + "class_obj.png"); + methodIcon = new ImageIcon(iconPath + File.separator + + "methpub_obj.png"); + fieldIcon = new ImageIcon(iconPath + File.separator + + "field_protected_obj.png"); + log("Icons loaded"); + } public ClassLoader getJavaModeClassLoader() { diff --git a/pdex/src/processing/mode/experimental/SketchOutline.java b/pdex/src/processing/mode/experimental/SketchOutline.java index f753d15d0..a78a66f03 100644 --- a/pdex/src/processing/mode/experimental/SketchOutline.java +++ b/pdex/src/processing/mode/experimental/SketchOutline.java @@ -346,19 +346,6 @@ public class SketchOutline { protected class CustomCellRenderer extends DefaultTreeCellRenderer { - protected final ImageIcon classIcon, fieldIcon, methodIcon; - - public CustomCellRenderer() { - String iconPath = editor.getMode().getContentFile("data") - .getAbsolutePath() - + File.separator + "icons"; - classIcon = new ImageIcon(iconPath + File.separator + "class_obj.png"); - methodIcon = new ImageIcon(iconPath + File.separator - + "methpub_obj.png"); - fieldIcon = new ImageIcon(iconPath + File.separator - + "field_protected_obj.png"); - } - public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { @@ -378,11 +365,11 @@ public class SketchOutline { .getUserObject(); int type = awrap.getNode().getParent().getNodeType(); if (type == ASTNode.METHOD_DECLARATION) - return methodIcon; + return editor.dmode.methodIcon; if (type == ASTNode.TYPE_DECLARATION) - return classIcon; + return editor.dmode.classIcon; if (type == ASTNode.VARIABLE_DECLARATION_FRAGMENT) - return fieldIcon; + return editor.dmode.fieldIcon; } return null; } diff --git a/pdex/src/processing/mode/experimental/TextArea.java b/pdex/src/processing/mode/experimental/TextArea.java index 00e1282d5..576b58eaf 100644 --- a/pdex/src/processing/mode/experimental/TextArea.java +++ b/pdex/src/processing/mode/experimental/TextArea.java @@ -711,7 +711,7 @@ public class TextArea extends JEditTextArea { } if (suggestion == null) suggestion = new CompletionPanel(this, position, subWord, defListModel, - location); + location,editor); else suggestion.updateList(defListModel, subWord, position); suggestion.setVisible(true);