From 51d22143477e52b926eb050d9ebc9e80edde8250 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 16 Nov 2014 16:02:31 -0700 Subject: [PATCH] cleaning out use of arrays of ArrayList objects --- .../galsasson/mode/tweak/SketchParser.java | 124 +++++++++--------- .../mode/experimental/DebugEditor.java | 101 +++++++------- .../mode/experimental/TextArea.java | 6 +- .../mode/experimental/TextAreaPainter.java | 59 ++++----- 4 files changed, 147 insertions(+), 143 deletions(-) diff --git a/pdex/src/galsasson/mode/tweak/SketchParser.java b/pdex/src/galsasson/mode/tweak/SketchParser.java index 155eac0df..661a79bf8 100644 --- a/pdex/src/galsasson/mode/tweak/SketchParser.java +++ b/pdex/src/galsasson/mode/tweak/SketchParser.java @@ -6,8 +6,8 @@ import java.util.regex.Pattern; public class SketchParser { - public ArrayList[] colorBoxes; - public ArrayList[] allHandles; + public List> colorBoxes; + public List> allHandles; int intVarCount; int floatVarCount; @@ -17,7 +17,7 @@ public class SketchParser { boolean requiresComment; ArrayList colorModes; - ArrayList[] scientificNotations; + List> scientificNotations; public SketchParser(String[] codeTabs, boolean requiresComment) { @@ -33,7 +33,7 @@ public class SketchParser { // handle colors colorModes = findAllColorModes(); - colorBoxes = new ArrayList[codeTabs.length]; + //colorBoxes = new ArrayList[codeTabs.length]; createColorBoxes(); createColorBoxesForLights(); @@ -47,12 +47,14 @@ public class SketchParser { public void addAllNumbers() { - allHandles = new ArrayList[codeTabs.length]; + //allHandles = new ArrayList[codeTabs.length]; // moved inside addAllDecimalNumbers addAllDecimalNumbers(); addAllHexNumbers(); addAllWebColorNumbers(); - for (int i=0; i handle : allHandles) { + //Collections.sort(allHandles[i], new HandleComparator()); + Collections.sort(handle, new HandleComparator()); } } @@ -63,12 +65,17 @@ public class SketchParser { * list of all numbers in the sketch (excluding hexadecimals) */ private void addAllDecimalNumbers() { + allHandles = new ArrayList<>(); + // for every number found: // save its type (int/float), name, value and position in code. Pattern p = Pattern.compile("[\\[\\{<>(),\\t\\s\\+\\-\\/\\*^%!|&=?:~]\\d+\\.?\\d*"); for (int i = 0; i < codeTabs.length; i++) { - allHandles[i] = new ArrayList(); + //allHandles[i] = new ArrayList(); + List handles = new ArrayList(); + allHandles.add(handles); + String c = codeTabs[i]; Matcher m = p.matcher(c); @@ -91,7 +98,7 @@ public class SketchParser { // ignore scientific notation (e.g. 1e-6) boolean found = false; - for (Range r : scientificNotations[i]) { + for (Range r : scientificNotations.get(i)) { if (r.contains(start)) { found=true; break; @@ -135,12 +142,12 @@ public class SketchParser { // consider this as a float String name = varPrefix + "_float[" + floatVarCount +"]"; int decimalDigits = getNumDigitsAfterPoint(value); - allHandles[i].add(new Handle("float", name, floatVarCount, value, i, line, start, end, decimalDigits)); + handles.add(new Handle("float", name, floatVarCount, value, i, line, start, end, decimalDigits)); floatVarCount++; } else { // consider this as an int String name = varPrefix + "_int[" + intVarCount +"]"; - allHandles[i].add(new Handle("int", name, intVarCount, value, i, line, start, end, 0)); + handles.add(new Handle("int", name, intVarCount, value, i, line, start, end, 0)); intVarCount++; } } @@ -201,7 +208,7 @@ public class SketchParser { // don't add this number continue; } - allHandles[i].add(handle); + allHandles.get(i).add(handle); intVarCount++; } } @@ -258,18 +265,16 @@ public class SketchParser { // don't add this number continue; } - allHandles[i].add(handle); + allHandles.get(i).add(handle); intVarCount++; - } - } - } + } + } + } - private ArrayList findAllColorModes() - { + private ArrayList findAllColorModes() { ArrayList modes = new ArrayList(); - for (String tab : codeTabs) - { + for (String tab : codeTabs) { int index = -1; // search for a call to colorMode function while ((index = tab.indexOf("colorMode", index+1)) > -1) { @@ -297,22 +302,24 @@ public class SketchParser { modes.add(ColorMode.fromString(context, modeDesc)); } } - return modes; } - private void createColorBoxes() - { + + private void createColorBoxes() { + colorBoxes = new ArrayList<>(); // search tab for the functions: 'color', 'fill', 'stroke', 'background', 'tint' Pattern p = Pattern.compile("color\\(|color\\s\\(|fill[\\(\\s]|stroke[\\(\\s]|background[\\(\\s]|tint[\\(\\s]"); - for (int i=0; i(); + + for (int i = 0; i < codeTabs.length; i++) { + //colorBoxes[i] = new ArrayList(); + List colorBox = new ArrayList(); + colorBoxes.add(colorBox); + String tab = codeTabs[i]; Matcher m = p.matcher(tab); - while (m.find()) - { + while (m.find()) { ArrayList colorHandles = new ArrayList(); // look for the '(' and ')' positions @@ -329,8 +336,7 @@ public class SketchParser { } // look for handles inside the parenthesis - for (Handle handle : allHandles[i]) - { + for (Handle handle : allHandles.get(i)) { if (handle.startChar > openPar && handle.endChar <= closePar) { // we have a match @@ -370,11 +376,10 @@ public class SketchParser { if (cmode.unrecognizedMode) { // the color mode is unrecognizable add only if is a hex or webcolor if (newCCB.isHex) { - colorBoxes[i].add(newCCB); + colorBox.add(newCCB); } - } - else { - colorBoxes[i].add(newCCB); + } else { + colorBox.add(newCCB); } } } @@ -382,19 +387,17 @@ public class SketchParser { } } - private void createColorBoxesForLights() - { + private void createColorBoxesForLights() { // search code for light color and material color functions. Pattern p = Pattern.compile("ambientLight[\\(\\s]|directionalLight[\\(\\s]"+ "|pointLight[\\(\\s]|spotLight[\\(\\s]|lightSpecular[\\(\\s]"+ "|specular[\\(\\s]|ambient[\\(\\s]|emissive[\\(\\s]"); - for (int i=0; i colorHandles = new ArrayList(); // look for the '(' and ')' positions @@ -422,8 +425,7 @@ public class SketchParser { } } - for (Handle handle : allHandles[i]) - { + for (Handle handle : allHandles.get(i)) { if (handle.startChar > openPar && handle.endChar <= colorParamsEnd) { // we have a match @@ -463,11 +465,10 @@ public class SketchParser { if (cmode.unrecognizedMode) { // the color mode is unrecognizable add only if is a hex or webcolor if (newCCB.isHex) { - colorBoxes[i].add(newCCB); + colorBoxes.get(i).add(newCCB); } - } - else { - colorBoxes[i].add(newCCB); + } else { + colorBoxes.get(i).add(newCCB); } } } @@ -475,8 +476,7 @@ public class SketchParser { } } - private ColorMode getColorModeForContext(String context) - { + private ColorMode getColorModeForContext(String context) { for (ColorMode cm: colorModes) { if (cm.drawContext.equals(context)) { return cm; @@ -517,33 +517,35 @@ public class SketchParser { */ for (int i=0; i toDelete = new ArrayList(); - for (String context : multipleContexts) - { - for (ColorControlBox ccb : colorBoxes[i]) - { + for (String context : multipleContexts) { + for (ColorControlBox ccb : colorBoxes.get(i)) { if (ccb.drawContext.equals(context) && !ccb.isHex) { toDelete.add(ccb); } } } - colorBoxes[i].removeAll(toDelete); + colorBoxes.get(i).removeAll(toDelete); } } - public ArrayList[] getAllScientificNotations() { - ArrayList notations[] = new ArrayList[codeTabs.length]; + public List> getAllScientificNotations() { + //ArrayList notations[] = new ArrayList[codeTabs.length]; + List> notations = new ArrayList<>(); Pattern p = Pattern.compile("[+\\-]?(?:0|[1-9]\\d*)(?:\\.\\d*)?[eE][+\\-]?\\d+"); - for (int i=0; i(); - Matcher m = p.matcher(codeTabs[i]); + //for (int i = 0; i < codeTabs.length; i++) { + for (String code : codeTabs) { + List notation = new ArrayList(); + //notations[i] = new ArrayList(); + //Matcher m = p.matcher(codeTabs[i]); + Matcher m = p.matcher(code); while (m.find()) { - notations[i].add(new Range(m.start(), m.end())); + //notations[i].add(new Range(m.start(), m.end())); + notation.add(new Range(m.start(), m.end())); } + notations.add(notation); } - return notations; } diff --git a/pdex/src/processing/mode/experimental/DebugEditor.java b/pdex/src/processing/mode/experimental/DebugEditor.java index ab2a29c04..cdf073942 100755 --- a/pdex/src/processing/mode/experimental/DebugEditor.java +++ b/pdex/src/processing/mode/experimental/DebugEditor.java @@ -1747,8 +1747,8 @@ public class DebugEditor extends JavaEditor implements ActionListener { ta.startInteractiveMode(); } - public void stopInteractiveMode(ArrayList handles[]) - { + //public void stopInteractiveMode(ArrayList handles[]) { + public void stopInteractiveMode(List> handles) { tweakClient.shutdown(); ta.stopInteractiveMode(); @@ -1819,8 +1819,7 @@ public class DebugEditor extends JavaEditor implements ActionListener { } } - public void updateInterface(ArrayList handles[], ArrayList colorBoxes[]) - { + public void updateInterface(List> handles, List> colorBoxes) { // set OSC port of handles // for (int i=0; i handles[]) - { - boolean[] modifiedTabs = new boolean[handles.length]; + + //private boolean[] getModifiedTabs(ArrayList handles[]) { + private boolean[] getModifiedTabs(List> handles) { + boolean[] modifiedTabs = new boolean[handles.size()]; - for (int i=0; i handles[], boolean withSpaces) + public void initEditorCode(List> handles, boolean withSpaces) { SketchCode[] sketchCode = sketch.getCode(); for (int tab=0; tab handles[]) - { + //public boolean automateSketch(Sketch sketch, ArrayList handles[]) + public boolean automateSketch(Sketch sketch, List> handles) { SketchCode[] code = sketch.getCode(); if (code.length<1) return false; - if (handles.length == 0) + if (handles.size() == 0) return false; int setupStartPos = SketchParser.getSetupStart(baseCode[0]); @@ -1988,7 +1986,7 @@ public class DebugEditor extends JavaEditor implements ActionListener { tweakClient = new UDPTweakClient(port); // update handles with a reference to the client object for (int tab=0; tab list : handles) { + //for (Handle n : handles[i]) { + for (Handle n : list) { header += " " + n.name + " = " + n.strValue + ";\n"; } } @@ -2087,33 +2086,37 @@ public class DebugEditor extends JavaEditor implements ActionListener { return true; } - private String replaceString(String str, int start, int end, String put) - { + private String replaceString(String str, int start, int end, String put) { return str.substring(0, start) + put + str.substring(end, str.length()); } - private int howManyInts(ArrayList handles[]) - { + //private int howManyInts(ArrayList handles[]) + private int howManyInts(List> handles) { int count = 0; - for (int i=0; i list : handles) { + //for (Handle n : handles[i]) { + for (Handle n : list) { + if (n.type == "int" || n.type == "hex" || n.type == "webcolor") { count++; + } } } return count; } - private int howManyFloats(ArrayList handles[]) - { + //private int howManyFloats(ArrayList handles[]) + private int howManyFloats(List> handles) { int count = 0; - for (int i=0; i list : handles) { + //for (Handle n : handles[i]) { + for (Handle n : list) { + if (n.type == "float") { count++; + } } } return count; } - } diff --git a/pdex/src/processing/mode/experimental/TextArea.java b/pdex/src/processing/mode/experimental/TextArea.java index baa8d56bf..6d570d755 100644 --- a/pdex/src/processing/mode/experimental/TextArea.java +++ b/pdex/src/processing/mode/experimental/TextArea.java @@ -31,8 +31,8 @@ import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; -import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.swing.DefaultListModel; @@ -954,8 +954,8 @@ public class TextArea extends JEditTextArea { } } - public void updateInterface(ArrayList handles[], ArrayList colorBoxes[]) - { + //public void updateInterface(ArrayList handles[], ArrayList colorBoxes[]) { + public void updateInterface(List> handles, List> colorBoxes) { customPainter.updateInterface(handles, colorBoxes); } diff --git a/pdex/src/processing/mode/experimental/TextAreaPainter.java b/pdex/src/processing/mode/experimental/TextAreaPainter.java index b6f0b6d43..ae396eac2 100644 --- a/pdex/src/processing/mode/experimental/TextAreaPainter.java +++ b/pdex/src/processing/mode/experimental/TextAreaPainter.java @@ -36,7 +36,7 @@ import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; -import java.util.ArrayList; +import java.util.List; import javax.swing.text.BadLocationException; import javax.swing.text.Segment; @@ -46,6 +46,7 @@ import processing.app.SketchCode; import processing.app.syntax.TextAreaDefaults; import processing.app.syntax.TokenMarker; + /** * Customized line painter. Adds support for background colors, left hand gutter * area with background color and text. @@ -214,6 +215,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter } paintErrorLine(gfx, line, x); } + /** * Paint the gutter background (solid color). @@ -231,6 +233,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter gfx.fillRect(0, y, ta.getGutterWidth(), fm.getHeight()); } + /** * Paint the vertical gutter separator line. * @@ -248,6 +251,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter y + fm.getHeight()); } + /** * Paint the gutter text. * @@ -284,6 +288,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter Utilities.drawTabbedText(new Segment(text.toCharArray(), 0, text.length()), ta.getGutterMargins() + 1, y + 1, gfx, this, 0); } + /** * Paint the background color of a line. @@ -311,6 +316,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter gfx.setColor(col); gfx.fillRect(0, y, getWidth(), height); } + /** * Paints the underline for an error/warning line @@ -428,6 +434,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter // gfx.fillRect(2, y, 3, height); } + /** * Trims out trailing whitespaces (to the right) * @@ -445,6 +452,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter return newString; } + /** * Sets ErrorCheckerService and loads theme for TextAreaPainter(XQMode) * @@ -456,6 +464,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter loadTheme(mode); } + public String getToolTipText(java.awt.event.MouseEvent evt) { if (ta.editor.hasJavaTabs) { // disabled for java tabs setToolTipText(null); @@ -543,8 +552,10 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter protected int horizontalAdjustment = 0; public boolean interactiveMode = false; - public ArrayList handles[]; - public ArrayList colorBoxes[]; +// public ArrayList handles[]; +// public ArrayList colorBoxes[]; + public List> handles; + public List> colorBoxes; public Handle mouseHandle = null; public ColorSelector colorSelector; @@ -574,8 +585,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - for (Handle n : handles[currentTab]) - { + for (Handle n : handles.get(currentTab)) { // update n position and width, and draw it int lineStartChar = ta.getLineStartOffset(n.line); int x = ta.offsetToX(n.line, n.newStartChar - lineStartChar); @@ -587,8 +597,7 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter } // draw color boxes - for (ColorControlBox cBox: colorBoxes[currentTab]) - { + for (ColorControlBox cBox: colorBoxes.get(currentTab)) { int lineStartChar = ta.getLineStartOffset(cBox.getLine()); int x = ta.offsetToX(cBox.getLine(), cBox.getCharIndex() - lineStartChar); int y = ta.lineToY(cBox.getLine()) + fm.getDescent(); @@ -618,8 +627,8 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter } // Update the interface - public void updateInterface(ArrayList handles[], ArrayList colorBoxes[]) - { + //public void updateInterface(ArrayList handles[], ArrayList colorBoxes[]) { + public void updateInterface(List> handles, List> colorBoxes) { this.handles = handles; this.colorBoxes = colorBoxes; @@ -632,18 +641,15 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter * synchronize this method to prevent the execution of 'paint' in the middle. * (don't paint while we make changes to the text of the editor) */ - public synchronized void initInterfacePositions() - { + public synchronized void initInterfacePositions() { SketchCode[] code = ta.editor.getSketch().getCode(); int prevScroll = ta.getVerticalScrollPosition(); String prevText = ta.getText(); - for (int tab=0; tab