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