Skip to content

Commit

Permalink
Implement basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Jul 10, 2017
1 parent 6c1ee7b commit 3b84a2f
Show file tree
Hide file tree
Showing 10 changed files with 560 additions and 1 deletion.
10 changes: 9 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
<idea-version since-build="145.0"/>

<actions>
<!-- Add your actions here -->
<action id="randomness.InsertNumber" class="com.fwdekker.randomness.insertion.InsertRandomNumber"
text="Insert Random Number" description="Inserts a random number at the caret."/>
<action id="randomness.NumberSettings" class="com.fwdekker.randomness.settings.NumberSettingsAction"
text="Insert Random Number Settings" description="Settings for Insert Random Number."/>

<action id="randomness.InsertString" class="com.fwdekker.randomness.insertion.InsertRandomString"
text="Insert Random String" description="Inserts a random string at the caret."/>
<action id="randomness.StringSettings" class="com.fwdekker.randomness.settings.StringSettingsAction"
text="Insert Random String Settings" description="Settings for Insert Random String."/>
</actions>
</idea-plugin>
69 changes: 69 additions & 0 deletions src/com/fwdekker/randomness/insertion/InsertRandomNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.fwdekker.randomness.insertion;

import java.util.Random;


/**
* Generates a random number based on adjustable parameters.
*/
public class InsertRandomNumber extends InsertRandomSomething {
private static final Random RANDOM = new Random();

/**
* The minimum value to be generated, inclusive.
*/
private static int minValue = 0;
/**
* The maximum value to be generated, inclusive.
*/
private static int maxValue = 1000;


/**
* Returns a random number between the minimum and maximum value, inclusive.
*
* @return a random number between the minimum and maximum value, inclusive
*/
@Override
String generateString() {
final int range = maxValue - minValue;
return Integer.toString(minValue + RANDOM.nextInt(range + 1));
}


/**
* Returns the minimum value to be generated, inclusive.
*
* @return the minimum value to be generated, inclusive
*/
public static int getMinValue() {
return minValue;
}

/**
* Sets the minimum value to be generated.
*
* @param minValue the minimum value to be generated, inclusive
*/
public static void setMinValue(final int minValue) {
InsertRandomNumber.minValue = minValue;
}

/**
* Returns the maximum value to be generated, inclusive.
*
* @return the maximum value to be generated, inclusive
*/
public static int getMaxValue() {
return maxValue;
}

/**
* Sets the maximum value to be generated.
*
* @param maxValue the maximum value to be generated, inclusive
*/
public static void setMaxValue(final int maxValue) {
InsertRandomNumber.maxValue = maxValue;
}
}
43 changes: 43 additions & 0 deletions src/com/fwdekker/randomness/insertion/InsertRandomSomething.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.fwdekker.randomness.insertion;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;


/**
* Inserts a randomly generated string at the position of the event's editor's caret.
*/
public abstract class InsertRandomSomething extends AnAction {
/**
* Inserts the string generated by {@link #generateString()} at the caret in the editor.
*
* @param event
*/
@Override
public void actionPerformed(final AnActionEvent event) {
final Project project = event.getData(CommonDataKeys.PROJECT);
final Editor editor = event.getData(CommonDataKeys.EDITOR);
final Document document = editor.getDocument();
final SelectionModel selectionModel = editor.getSelectionModel();

final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();

final Runnable runnable = () -> document.replaceString(start, end, generateString());
WriteCommandAction.runWriteCommandAction(project, runnable);
}


/**
* Generates a random string.
*
* @return a random string
*/
abstract String generateString();
}
78 changes: 78 additions & 0 deletions src/com/fwdekker/randomness/insertion/InsertRandomString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.fwdekker.randomness.insertion;

import java.util.Random;


/**
* Generates random alphanumerical strings based on adjustable parameters.
*/
public final class InsertRandomString extends InsertRandomSomething {
private static final Random RANDOM = new Random();
/**
* The characters that may be used in generated strings.
*/
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

/**
* The minimum length of a generated string, inclusive.
*/
private static int minLength = 10;
/**
* The maximum length of a generated string, inclusive.
*/
private static int maxLength = 10;


/**
* Returns a random string of alphanumerical characters.
*
* @return a random string of alphanumerical characters
*/
@Override
String generateString() {
final int length = minLength + RANDOM.nextInt(maxLength - minLength + 1);

final char[] text = new char[length];
for (int i = 0; i < length; i++) {
text[i] = ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length()));
}
return "\"" + new String(text) + "\"";
}


/**
* Returns the minimum length of a generated string, inclusive.
*
* @return the minimum length of a generated string, inclusive
*/
public static int getMinLength() {
return minLength;
}

/**
* Sets the minimum length of a generated string, inclusive.
*
* @param minLength the minimum length of a generated string, inclusive
*/
public static void setMinLength(final int minLength) {
InsertRandomString.minLength = minLength;
}

/**
* Returns the maximum length of a generated string, inclusive.
*
* @return the maximum length of a generated string, inclusive
*/
public static int getMaxLength() {
return maxLength;
}

/**
* Sets the maximum length of a generated string, inclusive.
*
* @param maxLength the maximum length of a generated string, inclusive
*/
public static void setMaxLength(final int maxLength) {
InsertRandomString.maxLength = maxLength;
}
}
21 changes: 21 additions & 0 deletions src/com/fwdekker/randomness/settings/NumberSettingsAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fwdekker.randomness.settings;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;


/**
* Controller for random number generation settings.
*/
public class NumberSettingsAction extends AnAction {
/**
* Shows a {@link NumberSettingsDialog}.
*/
@Override
public void actionPerformed(final AnActionEvent e) {
final NumberSettingsDialog dialog = new NumberSettingsDialog();
dialog.setTitle("Insert Random Number Settings");
dialog.pack();
dialog.setVisible(true);
}
}
98 changes: 98 additions & 0 deletions src/com/fwdekker/randomness/settings/NumberSettingsDialog.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.fwdekker.randomness.settings.NumberSettingsDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="OK"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="d1dad" class="javax.swing.JTextField" binding="minValue">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="f1509" class="javax.swing.JTextField" binding="maxValue">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="62ab2" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Minimum value"/>
</properties>
</component>
<component id="56ad0" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Maximum value"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
63 changes: 63 additions & 0 deletions src/com/fwdekker/randomness/settings/NumberSettingsDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.fwdekker.randomness.settings;

import com.fwdekker.randomness.insertion.InsertRandomNumber;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/**
* Dialog for settings of random number generation.
*/
public class NumberSettingsDialog extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JTextField minValue;
private JTextField maxValue;


/**
* Constructs a new {@code NumberSettingsDialog}.
*/
public NumberSettingsDialog() {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);

buttonOK.addActionListener(e -> onOK());
buttonCancel.addActionListener(e -> onCancel());

// Call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});

// Call onCancel() on ESCAPE
contentPane.registerKeyboardAction(
e -> onCancel(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

minValue.setText(Integer.toString(InsertRandomNumber.getMinValue()));
maxValue.setText(Integer.toString(InsertRandomNumber.getMaxValue()));
}


private void onOK() {
final int newMinValue = Integer.parseInt(minValue.getText());
final int newMaxValue = Integer.parseInt(maxValue.getText());
InsertRandomNumber.setMinValue(newMinValue);
InsertRandomNumber.setMaxValue(newMaxValue);

dispose();
}

private void onCancel() {
dispose();
}
}
Loading

0 comments on commit 3b84a2f

Please sign in to comment.