Debian Codegraph @

build/shared/tools/Mangler/src/Mangler.java

raw symbols SCIP
1 /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3 /*
4 Part of the Processing project - http://processing.org
5
6 Copyright (c) 2008 Ben Fry and Casey Reas
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 package com.transformers.supermangletron;
24
25
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28
29 import javax.swing.JOptionPane;
30
31 import processing.app.Editor;
32 import processing.app.tools.Tool;
33
34
35
36 /**
37 * Example Tools menu entry.
38 */
39 public class Mangler implements Tool {
40 Editor editor;
41
42
43 public void init(Editor editor) {
44 this.editor = editor;
45 }
46
47
48 public String getMenuTitle() {
49 return "Mangle Selection";
50 }
51
52
53 public void run() {
54 String sketchName = editor.getSketch().getName();
55
56 Object[] options = { "Yes, please", "No, thanks" };
57 int result = JOptionPane.showOptionDialog(editor,
58 "Is " + sketchName +
59 " ready for destruction?",
60 "Super Mangle Tron",
61 JOptionPane.YES_NO_OPTION,
62 JOptionPane.QUESTION_MESSAGE,
63 null,
64 options,
65 options[1]);
66 if (result == JOptionPane.YES_OPTION) {
67 mangleSelection();
68 }
69 }
70
71
72 protected void mangleSelection() {
73 //Check if there is selected text
74 if (editor.getCurrentTab().getSelectedText() != null) {
75 String selection = editor.getCurrentTab().getSelectedText();
76 char[] stuff = selection.toCharArray();
77 // Randomly swap a bunch of characters in the text
78 for (int i = 0; i < stuff.length / 10; i++) {
79 int a = (int) (Math.random() * stuff.length);
80 int b = (int) (Math.random() * stuff.length);
81 if (stuff[a] == '\n' || stuff[b] == '\n') {
82 continue; // skip newline characters
83 }
84 stuff[a] = selection.charAt(b);
85 stuff[b] = selection.charAt(a);
86 }
87 editor.getCurrentTab().setSelectedText(new String(stuff));
88 editor.statusNotice("Now that feels better, doesn't it?");
89
90 } else {
91 //When there is no selected text
92 editor.statusError("No selection, no dice.");
93 }
94 }
95 }