Search in sources :

Example 1 with Sketch

use of processing.app.Sketch in project processing by processing.

the class FindReplace method find.

// look for the next instance of the find string to be found
// once found, select it (and go to that line)
private boolean find(boolean wrap, boolean backwards) {
    String searchTerm = findField.getText();
    // this will catch "find next" being called when no search yet
    if (searchTerm.length() != 0) {
        String text = editor.getText();
        // Started work on find/replace across tabs. These two variables store
        // the original tab and selection position so that it knew when to stop
        // rotating through.
        Sketch sketch = editor.getSketch();
        int tabIndex = sketch.getCurrentCodeIndex();
        if (ignoreCase) {
            searchTerm = searchTerm.toLowerCase();
            text = text.toLowerCase();
        }
        int nextIndex;
        if (!backwards) {
            //int selectionStart = editor.textarea.getSelectionStart();
            int selectionEnd = editor.getSelectionStop();
            nextIndex = text.indexOf(searchTerm, selectionEnd);
            if (nextIndex == -1 && wrap && !allTabs) {
                // if wrapping, a second chance is ok, start from beginning
                nextIndex = text.indexOf(searchTerm, 0);
            } else if (nextIndex == -1 && allTabs) {
                // For searching in all tabs, wrapping always happens.
                int tempIndex = tabIndex;
                // Look for searchterm in all tabs.
                while (tabIndex <= sketch.getCodeCount() - 1) {
                    if (tabIndex == sketch.getCodeCount() - 1) {
                        // System.out.println("wrapping.");
                        tabIndex = -1;
                    } else if (tabIndex == sketch.getCodeCount() - 1) {
                        break;
                    }
                    try {
                        Document doc = sketch.getCode(tabIndex + 1).getDocument();
                        if (doc != null) {
                            // this thing has the latest changes
                            text = doc.getText(0, doc.getLength());
                        } else {
                            // not this thing.
                            text = sketch.getCode(tabIndex + 1).getProgram();
                        }
                    } catch (BadLocationException e) {
                        e.printStackTrace();
                    }
                    tabIndex++;
                    if (ignoreCase) {
                        text = text.toLowerCase();
                    }
                    nextIndex = text.indexOf(searchTerm, 0);
                    if (nextIndex != -1 || tabIndex == tempIndex) {
                        break;
                    }
                }
                // No tab switching should happen, restore tabIndex
                if (nextIndex == -1) {
                    tabIndex = tempIndex;
                }
            }
        } else {
            //int selectionStart = editor.textarea.getSelectionStart();
            int selectionStart = editor.getSelectionStart() - 1;
            if (selectionStart >= 0) {
                nextIndex = text.lastIndexOf(searchTerm, selectionStart);
            } else {
                nextIndex = -1;
            }
            if (wrap && !allTabs && nextIndex == -1) {
                // if wrapping, a second chance is ok, start from the end
                nextIndex = text.lastIndexOf(searchTerm);
            } else if (nextIndex == -1 && allTabs) {
                int tempIndex = tabIndex;
                // Look for search term in previous tabs.
                while (tabIndex >= 0) {
                    if (tabIndex == 0) {
                        //System.out.println("wrapping.");
                        tabIndex = sketch.getCodeCount();
                    } else if (tabIndex == 0) {
                        break;
                    }
                    try {
                        Document doc = sketch.getCode(tabIndex - 1).getDocument();
                        if (doc != null) {
                            // this thing has the latest changes
                            text = doc.getText(0, doc.getLength());
                        } else {
                            // not this thing.
                            text = sketch.getCode(tabIndex - 1).getProgram();
                        }
                    } catch (BadLocationException e) {
                        e.printStackTrace();
                    }
                    tabIndex--;
                    if (ignoreCase) {
                        text = text.toLowerCase();
                    }
                    nextIndex = text.lastIndexOf(searchTerm);
                    if (nextIndex != -1 || tabIndex == tempIndex) {
                        break;
                    }
                }
                // No tab switching should happen, restore tabIndex
                if (nextIndex == -1) {
                    tabIndex = tempIndex;
                }
            }
        }
        if (nextIndex != -1) {
            if (allTabs) {
                sketch.setCurrentCode(tabIndex);
            }
            editor.setSelection(nextIndex, nextIndex + searchTerm.length());
        } else {
        //Toolkit.getDefaultToolkit().beep();
        }
        if (nextIndex != -1) {
            setFound(true);
            return true;
        }
    }
    setFound(false);
    return false;
}
Also used : Sketch(processing.app.Sketch) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 2 with Sketch

use of processing.app.Sketch in project processing by processing.

the class EditorHeader method placeTabs.

private boolean placeTabs(int left, int right, Graphics2D g) {
    Sketch sketch = editor.getSketch();
    int x = left;
    for (int i = 0; i < sketch.getCodeCount(); i++) {
        SketchCode code = sketch.getCode(i);
        Tab tab = tabs[i];
        //      int pieceCount = 2 + (tab.textWidth / PIECE_WIDTH);
        //      if (tab.textVisible == false) {
        //        pieceCount = 4;
        //      }
        //      int pieceWidth = pieceCount * PIECE_WIDTH;
        int state = (code == sketch.getCurrentCode()) ? SELECTED : UNSELECTED;
        //      if (g != null) {
        //        //g.drawImage(pieces[state][LEFT], x, 0, PIECE_WIDTH, PIECE_HEIGHT, null);
        //        path = new GeneralPath();
        //        path.moveTo(x, bottom);
        //        path.lineTo(x, top + NOTCH);
        //        path.lineTo(x + NOTCH, top);
        //      }
        tab.left = x;
        x += TEXT_MARGIN;
        //      x += PIECE_WIDTH;
        //      int contentLeft = x;
        //      for (int j = 0; j < pieceCount; j++) {
        //        if (g != null) {
        //          g.drawImage(pieces[state][MIDDLE], x, 0, PIECE_WIDTH, PIECE_HEIGHT, null);
        //        }
        //        x += PIECE_WIDTH;
        //      }
        //      if (g != null) {
        int drawWidth = tab.textVisible ? tab.textWidth : NO_TEXT_WIDTH;
        x += drawWidth + TEXT_MARGIN;
        //        path.moveTo(x, top);
        //      }
        tab.right = x;
        if (g != null && tab.right < right) {
            g.setColor(tabColor[state]);
            drawTab(g, tab.left, tab.right, i == 0, false);
            if (tab.textVisible) {
                int textLeft = tab.left + ((tab.right - tab.left) - tab.textWidth) / 2;
                g.setColor(textColor[state]);
                //          int baseline = (int) Math.ceil((sizeH + fontAscent) / 2.0);
                //int baseline = bottom - (TAB_HEIGHT - fontAscent)/2;
                int tabHeight = TAB_BOTTOM - TAB_TOP;
                int baseline = TAB_TOP + (tabHeight + fontAscent) / 2;
                //g.drawString(sketch.code[i].name, textLeft, baseline);
                g.drawString(tab.text, textLeft, baseline);
            //          g.drawLine(tab.left, baseline-fontAscent, tab.right, baseline-fontAscent);
            //          g.drawLine(tab.left, baseline, tab.right, baseline);
            }
            if (code.isModified()) {
                g.setColor(modifiedColor);
                //g.drawLine(tab.left + NOTCH, top, tab.right - NOTCH, top);
                //g.drawLine(tab.left + (i == 0 ? CURVE_RADIUS : 0), TAB_TOP, tab.right-1, TAB_TOP);
                g.drawLine(tab.right, TAB_TOP, tab.right, TAB_BOTTOM);
            }
        }
        //      if (g != null) {
        //        g.drawImage(pieces[state][RIGHT], x, 0, PIECE_WIDTH, PIECE_HEIGHT, null);
        //      }
        //      x += PIECE_WIDTH - 1;  // overlap by 1 pixel
        x += TAB_BETWEEN;
    }
    return x <= right;
}
Also used : SketchCode(processing.app.SketchCode) Sketch(processing.app.Sketch)

Example 3 with Sketch

use of processing.app.Sketch in project processing by processing.

the class EditorHeader method rebuildMenu.

public void rebuildMenu() {
    //System.out.println("rebuilding");
    if (menu != null) {
        menu.removeAll();
    } else {
        menu = new JMenu();
        popup = menu.getPopupMenu();
        add(popup);
        popup.setLightWeightPopupEnabled(true);
    /*
      popup.addPopupMenuListener(new PopupMenuListener() {
          public void popupMenuCanceled(PopupMenuEvent e) {
            // on redraw, the isVisible() will get checked.
            // actually, a repaint may be fired anyway, so this
            // may be redundant.
            repaint();
          }
          public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { }
          public void popupMenuWillBecomeVisible(PopupMenuEvent e) { }
        });
      */
    }
    JMenuItem item;
    final JRootPane rootPane = editor.getRootPane();
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    ActionMap actionMap = rootPane.getActionMap();
    Action action;
    String mapKey;
    KeyStroke keyStroke;
    item = Toolkit.newJMenuItemShift(Language.text("editor.header.new_tab"), KeyEvent.VK_N);
    action = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            editor.getSketch().handleNewCode();
        }
    };
    mapKey = "editor.header.new_tab";
    keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.SHORTCUT_SHIFT_KEY_MASK);
    inputMap.put(keyStroke, mapKey);
    actionMap.put(mapKey, action);
    item.addActionListener(action);
    menu.add(item);
    item = new JMenuItem(Language.text("editor.header.rename"));
    action = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            editor.getSketch().handleRenameCode();
        }
    };
    item.addActionListener(action);
    menu.add(item);
    item = Toolkit.newJMenuItemShift(Language.text("editor.header.delete"), KeyEvent.VK_D);
    action = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Sketch sketch = editor.getSketch();
            if (// ok on OS X
            !Platform.isMacOS() && // mmm! accessor
            editor.base.getEditors().size() == 1 && sketch.getCurrentCodeIndex() == 0) {
                Messages.showWarning(Language.text("editor.header.delete.warning.title"), Language.text("editor.header.delete.warning.text"));
            } else {
                editor.getSketch().handleDeleteCode();
            }
        }
    };
    mapKey = "editor.header.delete";
    keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_D, Toolkit.SHORTCUT_SHIFT_KEY_MASK);
    inputMap.put(keyStroke, mapKey);
    actionMap.put(mapKey, action);
    item.addActionListener(action);
    menu.add(item);
    menu.addSeparator();
    //  KeyEvent.VK_LEFT and VK_RIGHT will make Windows beep
    final String prevTab = Language.text("editor.header.previous_tab");
    if (Platform.isLinux()) {
        item = Toolkit.newJMenuItem(prevTab, KeyEvent.VK_PAGE_UP);
    } else {
        item = Toolkit.newJMenuItemAlt(prevTab, KeyEvent.VK_LEFT);
    }
    action = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            editor.getSketch().handlePrevCode();
        }
    };
    mapKey = "editor.header.previous_tab";
    if (Platform.isLinux()) {
        keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, Toolkit.SHORTCUT_KEY_MASK);
    } else {
        keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, Toolkit.SHORTCUT_ALT_KEY_MASK);
    }
    inputMap.put(keyStroke, mapKey);
    actionMap.put(mapKey, action);
    item.addActionListener(action);
    menu.add(item);
    final String nextTab = Language.text("editor.header.next_tab");
    if (Platform.isLinux()) {
        item = Toolkit.newJMenuItem(nextTab, KeyEvent.VK_PAGE_DOWN);
    } else {
        item = Toolkit.newJMenuItemAlt(nextTab, KeyEvent.VK_RIGHT);
    }
    action = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            editor.getSketch().handleNextCode();
        }
    };
    mapKey = "editor.header.next_tab";
    if (Platform.isLinux()) {
        keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, Toolkit.SHORTCUT_KEY_MASK);
    } else {
        keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, Toolkit.SHORTCUT_ALT_KEY_MASK);
    }
    inputMap.put(keyStroke, mapKey);
    actionMap.put(mapKey, action);
    item.addActionListener(action);
    menu.add(item);
    Sketch sketch = editor.getSketch();
    if (sketch != null) {
        menu.addSeparator();
        ActionListener jumpListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                editor.getSketch().setCurrentCode(e.getActionCommand());
            }
        };
        for (SketchCode code : sketch.getCode()) {
            item = new JMenuItem(code.getPrettyName());
            item.addActionListener(jumpListener);
            menu.add(item);
        }
    }
    Toolkit.setMenuMnemonics(menu);
}
Also used : SketchCode(processing.app.SketchCode) Sketch(processing.app.Sketch)

Example 4 with Sketch

use of processing.app.Sketch in project processing by processing.

the class Editor method handleOpenInternal.

/**
   * Second stage of open, occurs after having checked to see if the
   * modifications (if any) to the previous sketch need to be saved.
   * Because this method is called in Editor's constructor, a subclass
   * shouldn't rely on any of its variables being initialized already.
   */
protected void handleOpenInternal(String path) throws EditorException {
    // check to make sure that this .pde file is
    // in a folder of the same name
    final File file = new File(path);
    final File parentFile = new File(file.getParent());
    final String parentName = parentFile.getName();
    final String defaultName = parentName + "." + mode.getDefaultExtension();
    final File altFile = new File(file.getParent(), defaultName);
    if (defaultName.equals(file.getName())) {
    // no beef with this guy
    } else if (altFile.exists()) {
        // The user selected a source file from the same sketch,
        // but open the file with the default extension instead.
        path = altFile.getAbsolutePath();
    } else if (!mode.canEdit(file)) {
        final String modeName = mode.getTitle().equals("Java") ? "Processing" : (mode.getTitle() + " Mode");
        throw new EditorException(modeName + " can only open its own sketches\n" + "and other files ending in " + mode.getDefaultExtension());
    } else {
        final String properParent = file.getName().substring(0, file.getName().lastIndexOf('.'));
        Object[] options = { Language.text("prompt.ok"), Language.text("prompt.cancel") };
        String prompt = "The file \"" + file.getName() + "\" needs to be inside\n" + "a sketch folder named \"" + properParent + "\".\n" + "Create this folder, move the file, and continue?";
        int result = JOptionPane.showOptionDialog(this, prompt, "Moving", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        if (result == JOptionPane.YES_OPTION) {
            // create properly named folder
            File properFolder = new File(file.getParent(), properParent);
            if (properFolder.exists()) {
                throw new EditorException("A folder named \"" + properParent + "\" " + "already exists. Can't open sketch.");
            }
            if (!properFolder.mkdirs()) {
                throw new EditorException("Could not create the sketch folder.");
            }
            // copy the sketch inside
            File properPdeFile = new File(properFolder, file.getName());
            File origPdeFile = new File(path);
            try {
                Util.copyFile(origPdeFile, properPdeFile);
            } catch (IOException e) {
                throw new EditorException("Could not copy to a proper location.", e);
            }
            // remove the original file, so user doesn't get confused
            origPdeFile.delete();
            // update with the new path
            path = properPdeFile.getAbsolutePath();
        } else {
            //return false;
            throw new EditorException();
        }
    }
    try {
        sketch = new Sketch(path, this);
    } catch (IOException e) {
        throw new EditorException("Could not create the sketch.", e);
    }
    header.rebuild();
    updateTitle();
    // Disable untitled setting from previous document, if any
    //    untitled = false;
    // Store information on who's open and running
    // (in case there's a crash or something that can't be recovered)
    // TODO this probably need not be here because of the Recent menu, right?
    Preferences.save();
}
Also used : Sketch(processing.app.Sketch) Point(java.awt.Point) java.awt.print(java.awt.print)

Example 5 with Sketch

use of processing.app.Sketch in project processing by processing.

the class Debugger method startDebug.

/**
   * Start a debugging session. Builds the sketch and launches a VM to run it.
   * VM starts suspended. Should produce a VMStartEvent.
   */
public synchronized void startDebug() {
    //stopDebug(); // stop any running sessions
    if (isStarted()) {
        // do nothing
        return;
    }
    // we are busy now
    editor.statusBusy();
    // clear console
    editor.clearConsole();
    // clear variable inspector (also resets expanded states)
    editor.variableInspector().reset();
    // load edits into sketch obj, etc...
    editor.prepareRun();
    // after prepareRun, since this removes highlights
    editor.activateDebug();
    try {
        Sketch sketch = editor.getSketch();
        JavaBuild build = new JavaBuild(sketch);
        log("building sketch: " + sketch.getName());
        //LineMapping.addLineNumbers(sketch); // annotate
        //      mainClassName = build.build(false);
        mainClassName = build.build(true);
        //LineMapping.removeLineNumbers(sketch); // annotate
        log("class: " + mainClassName);
        // folder with assembled/preprocessed src
        srcPath = build.getSrcFolder().getPath();
        log("build src: " + srcPath);
        // folder with compiled code (.class files)
        log("build bin: " + build.getBinFolder().getPath());
        if (mainClassName != null) {
            // generate the source line mapping
            //lineMap = LineMapping.generateMapping(srcPath + File.separator + mainClassName + ".java");
            log("launching debuggee runtime");
            runtime = new Runner(build, editor);
            // non-blocking
            VirtualMachine vm = runtime.debug(null);
            if (vm == null) {
                loge("error 37: launch failed", null);
            }
            // start receiving vm events
            VMEventReader eventThread = new VMEventReader(vm.eventQueue(), vmEventListener);
            eventThread.start();
            startTrackingLineChanges();
            editor.statusBusy();
        }
    } catch (Exception e) {
        editor.statusError(e);
    }
}
Also used : Runner(processing.mode.java.runner.Runner) Sketch(processing.app.Sketch)

Aggregations

Sketch (processing.app.Sketch)11 SketchCode (processing.app.SketchCode)7 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 BadLocationException (javax.swing.text.BadLocationException)2 Messages (processing.app.Messages)2 ClassPathFactory (com.google.classpath.ClassPathFactory)1 EventQueue (java.awt.EventQueue)1 Point (java.awt.Point)1 WindowEvent (java.awt.event.WindowEvent)1 WindowFocusListener (java.awt.event.WindowFocusListener)1 GeneralPath (java.awt.geom.GeneralPath)1 java.awt.print (java.awt.print)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1