11package  com.fwdekker.randomness 
22
3- import  com.fwdekker.randomness.array.ArrayScheme 
4- import  com.fwdekker.randomness.array.ArraySettingsAction 
53import  com.intellij.codeInsight.hint.HintManager 
6- import  com.intellij.icons.AllIcons 
7- import  com.intellij.ide.actions.QuickSwitchSchemeAction 
84import  com.intellij.openapi.actionSystem.ActionGroup 
95import  com.intellij.openapi.actionSystem.AnAction 
106import  com.intellij.openapi.actionSystem.AnActionEvent 
117import  com.intellij.openapi.actionSystem.CommonDataKeys 
128import  com.intellij.openapi.actionSystem.DataContext 
13- import  com.intellij.openapi.actionSystem.DefaultActionGroup 
149import  com.intellij.openapi.command.WriteCommandAction 
10+ import  com.intellij.openapi.options.Configurable 
1511import  com.intellij.openapi.options.ShowSettingsUtil 
16- import  com.intellij.openapi.project.DumbAwareAction 
17- import  com.intellij.openapi.project.Project 
1812import  icons.RandomnessIcons 
1913import  java.awt.event.ActionEvent 
20- import  java.util.concurrent.ExecutionException 
21- import  java.util.concurrent.Executors 
22- import  java.util.concurrent.TimeUnit 
23- import  java.util.concurrent.TimeoutException 
2414import  javax.swing.Icon 
25- import  kotlin.random.Random 
2615
2716
2817/* *
@@ -45,36 +34,11 @@ abstract class DataGroupAction(private val icon: Icon = RandomnessIcons.Data.Bas
4534     */  
4635    abstract  val  insertAction:  DataInsertAction 
4736
48-     /* *
49-      * The action used to insert arrays of data. 
50-      */  
51-     abstract  val  insertArrayAction:  DataInsertArrayAction 
52- 
53-     /* *
54-      * The action used to insert repeated single data. 
55-      */  
56-     abstract  val  insertRepeatAction:  DataInsertRepeatAction 
57- 
58-     /* *
59-      * The action used to insert repeated arrays of data. 
60-      */  
61-     abstract  val  insertRepeatArrayAction:  DataInsertRepeatArrayAction 
62- 
6337    /* *
6438     * The action used to edit the generator settings for this data type. 
6539     */  
6640    abstract  val  settingsAction:  DataSettingsAction 
6741
68-     /* *
69-      * The action used to quickly switch between schemes of this data type. 
70-      */  
71-     abstract  val  quickSwitchSchemeAction:  DataQuickSwitchSchemeAction <* >
72- 
73-     /* *
74-      * The action used to quickly switch between array schemes. 
75-      */  
76-     abstract  val  quickSwitchArraySchemeAction:  DataQuickSwitchSchemeAction <* >
77- 
7842
7943    /* *
8044     * Returns the insert action, array insert action, and settings action. 
@@ -83,7 +47,7 @@ abstract class DataGroupAction(private val icon: Icon = RandomnessIcons.Data.Bas
8347     * @return the insert action, array insert action, and settings action 
8448     */  
8549    override  fun  getChildren (event :  AnActionEvent ? ) = 
86-         arrayOf(insertArrayAction, insertRepeatAction, insertRepeatArrayAction,  settingsAction, quickSwitchSchemeAction )
50+         arrayOf(settingsAction)
8751
8852    /* *
8953     * Returns `true`. 
@@ -99,21 +63,9 @@ abstract class DataGroupAction(private val icon: Icon = RandomnessIcons.Data.Bas
9963     * @param event carries information on the invocation place 
10064     */  
10165    override  fun  actionPerformed (event :  AnActionEvent ) {
102-         val  altPressed =  event.modifiers and  ActionEvent .ALT_MASK  !=  0 
103-         val  ctrlPressed =  event.modifiers and  ActionEvent .CTRL_MASK  !=  0 
104-         val  shiftPressed =  event.modifiers and  ActionEvent .SHIFT_MASK  !=  0 
105- 
10666        //  alt behavior is handled by implementation of `actionPerformed`
107-         when  {
108-             altPressed &&  ctrlPressed &&  shiftPressed ->  quickSwitchArraySchemeAction.actionPerformed(event)
109-             altPressed &&  ctrlPressed ->  quickSwitchSchemeAction.actionPerformed(event)
110-             altPressed &&  shiftPressed ->  insertRepeatArrayAction.actionPerformed(event)
111-             ctrlPressed &&  shiftPressed ->  ArraySettingsAction ().actionPerformed(event)
112-             altPressed ->  insertRepeatAction.actionPerformed(event)
113-             ctrlPressed ->  settingsAction.actionPerformed(event)
114-             shiftPressed ->  insertArrayAction.actionPerformed(event)
115-             else  ->  insertAction.actionPerformed(event)
116-         }
67+         if  (event.modifiers and  ActionEvent .CTRL_MASK  !=  0 ) settingsAction.actionPerformed(event)
68+         else  insertAction.actionPerformed(event)
11769    }
11870
11971    /* *
@@ -149,9 +101,9 @@ abstract class DataInsertAction(private val icon: Icon) : AnAction() {
149101    abstract  val  name:  String 
150102
151103    /* *
152-      * The random generator  used to generate random values . 
104+      * The scheme that is  used to generate data . 
153105     */  
154-     var  random :   Random   =   Random . Default 
106+     abstract   val  scheme :   Scheme 
155107
156108
157109    /* *
@@ -169,7 +121,7 @@ abstract class DataInsertAction(private val icon: Icon) : AnAction() {
169121    }
170122
171123    /* *
172-      * Inserts the data generated by [generateStrings]  at the caret(s) in the editor; one datum for each caret. 
124+      * Inserts the data generated by the scheme  at the caret(s) in the editor; one datum for each caret. 
173125     * 
174126     * @param event carries information on the invocation place 
175127     */  
@@ -182,7 +134,7 @@ abstract class DataInsertAction(private val icon: Icon) : AnAction() {
182134
183135        val  data = 
184136            try  {
185-                 generateStringsTimely (editor.caretModel.caretCount)
137+                 generateTimely { scheme.generateStrings (editor.caretModel.caretCount) } 
186138            } catch  (e:  DataGenerationException ) {
187139                HintManager .getInstance().showErrorHint(
188140                    editor,
@@ -207,67 +159,6 @@ abstract class DataInsertAction(private val icon: Icon) : AnAction() {
207159        }
208160    }
209161
210-     /* *
211-      * Generates a random datum. 
212-      * 
213-      * @return a random datum 
214-      * @throws DataGenerationException if data could not be generated 
215-      */  
216-     @Throws(DataGenerationException ::class )
217-     fun  generateString () =  generateStrings(1 ).first()
218- 
219-     /* *
220-      * Generates a random datum, or throws an exception if it takes longer than [GENERATOR_TIMEOUT] milliseconds. 
221-      * 
222-      * @return a random datum 
223-      * @throws DataGenerationException if data could not be generated in time 
224-      */  
225-     @Throws(DataGenerationException ::class )
226-     fun  generateStringTimely () =  generateTimely { generateString() }
227- 
228-     /* *
229-      * Generates random data. 
230-      * 
231-      * @param count the number of data to generate 
232-      * @return random data 
233-      * @throws DataGenerationException if data could not be generated 
234-      */  
235-     @Throws(DataGenerationException ::class )
236-     abstract  fun  generateStrings (count :  Int  = 1): List <String >
237- 
238-     /* *
239-      * Generates random data, or throws an exception if it takes longer than [GENERATOR_TIMEOUT] milliseconds. 
240-      * 
241-      * @param count the number of data to generate 
242-      * @return random data 
243-      * @throws DataGenerationException if data could not be generated in time 
244-      */  
245-     @Throws(DataGenerationException ::class )
246-     fun  generateStringsTimely (count :  Int  = 1) =  generateTimely { generateStrings(count) }
247- 
248-     /* *
249-      * Runs the given function and returns its return value, or throws an exception if it takes longer than 
250-      * [GENERATOR_TIMEOUT] milliseconds. 
251-      * 
252-      * @param T the return type of [generator] 
253-      * @param generator the function to call 
254-      * @return the return value of [generator] 
255-      * @throws DataGenerationException if data could not be generated in time 
256-      */  
257-     @Throws(DataGenerationException ::class )
258-     private  fun  <T > generateTimely (generator :  () ->  T ): T  {
259-         val  executor =  Executors .newSingleThreadExecutor()
260-         try  {
261-             return  executor.submit<T > { generator() }.get(GENERATOR_TIMEOUT , TimeUnit .MILLISECONDS )
262-         } catch  (e:  TimeoutException ) {
263-             throw  DataGenerationException (" Timed out while generating data." 
264-         } catch  (e:  ExecutionException ) {
265-             throw  DataGenerationException (e.cause?.message ? :  e.message, e)
266-         } finally  {
267-             executor.shutdown()
268-         }
269-     }
270- 
271162
272163    /* *
273164     * Holds constants. 
@@ -281,86 +172,6 @@ abstract class DataInsertAction(private val icon: Icon) : AnAction() {
281172}
282173
283174
284- /* *
285-  * Inserts randomly generated arrays of strings at the event's editor's carets. 
286-  * 
287-  * @param arrayScheme the scheme to use for generating arrays 
288-  * @param dataInsertAction the action to generate data with 
289-  * @param icon the icon to display with the action 
290-  */  
291- abstract  class  DataInsertArrayAction (
292-     private  val  arrayScheme :  ArrayScheme ,
293-     private  val  dataInsertAction :  DataInsertAction ,
294-     icon :  Icon  = RandomnessIcons .Data .Array 
295- ) : DataInsertAction(icon) {
296-     /* *
297-      * Generates array-like strings of random data. 
298-      * 
299-      * @param count the number of array-like strings to generate 
300-      * @return array-like strings of random data 
301-      * @throws DataGenerationException if data could not be generated 
302-      */  
303-     @Throws(DataGenerationException ::class )
304-     override  fun  generateStrings (count :  Int ): List <String > {
305-         if  (arrayScheme.count <=  0 )
306-             throw  DataGenerationException (" Array cannot have fewer than 1 element." 
307- 
308-         dataInsertAction.random =  random
309-         return  dataInsertAction.generateStrings(count *  arrayScheme.count)
310-             .chunked(arrayScheme.count)
311-             .map { arrayScheme.arrayify(it) }
312-     }
313- }
314- 
315- 
316- /* *
317-  * Inserts the same randomly generated string at the event's editor's carets. 
318-  * 
319-  * @param dataInsertAction the action to generate data with 
320-  * @param icon the icon to display with the action 
321-  */  
322- abstract  class  DataInsertRepeatAction (
323-     private  val  dataInsertAction :  DataInsertAction ,
324-     icon :  Icon  = RandomnessIcons .Data .Array 
325- ) : DataInsertAction(icon) {
326-     /* *
327-      * Generates a random datum and repeats it [count] times. 
328-      * 
329-      * @param count the number of times to repeat the data 
330-      * @return a random datum, repeated [count] times 
331-      * @throws DataGenerationException if data could not be generated 
332-      */  
333-     @Throws(DataGenerationException ::class )
334-     override  fun  generateStrings (count :  Int ): List <String > {
335-         dataInsertAction.random =  random
336-         return  dataInsertAction.generateString().let  { string ->  List (count) { string } }
337-     }
338- }
339- 
340- 
341- /* *
342-  * Inserts the same randomly generated array of strings at the event's editor's carets. 
343-  * 
344-  * @param dataInsertArrayAction the action to generate data with 
345-  * @param icon the icon to display with the action 
346-  */  
347- abstract  class  DataInsertRepeatArrayAction (
348-     private  val  dataInsertArrayAction :  DataInsertArrayAction ,
349-     icon :  Icon  = RandomnessIcons .Data .Array 
350- ) : DataInsertAction(icon) {
351-     /* *
352-      * Generates a random array-like string of random data and repeats it [count] times. 
353-      * 
354-      * @param count the number of times to repeat the data 
355-      * @return a random array-like string of random data and repeats it [count] times 
356-      * @throws DataGenerationException if data could not be generated 
357-      */  
358-     @Throws(DataGenerationException ::class )
359-     override  fun  generateStrings (count :  Int ) = 
360-         dataInsertArrayAction.generateString().let  { string ->  List (count) { string } }
361- }
362- 
363- 
364175/* *
365176 * Opens the settings window for changing settings. 
366177 * 
@@ -375,7 +186,7 @@ abstract class DataSettingsAction(private val icon: Icon = RandomnessIcons.Data.
375186    /* *
376187     * The class of the configurable maintaining the settings. 
377188     */  
378-     protected  abstract  val  configurableClass:  Class <out  SettingsConfigurable < * ,  * > >
189+     protected  abstract  val  configurableClass:  Class <out  Configurable >
379190
380191
381192    /* *
@@ -398,55 +209,3 @@ abstract class DataSettingsAction(private val icon: Icon = RandomnessIcons.Data.
398209    override  fun  actionPerformed (event :  AnActionEvent ) = 
399210        ShowSettingsUtil .getInstance().showSettingsDialog(event.project, configurableClass)
400211}
401- 
402- 
403- /* *
404-  * Opens a popup to allow the user to quickly switch to the selected scheme. 
405-  * 
406-  * @param T the type of scheme that can be switched between 
407-  * @param settings the settings containing the schemes that can be switched between 
408-  * @param icon the icon to present with this action 
409-  */  
410- abstract  class  DataQuickSwitchSchemeAction <T  :  Scheme <T >>(
411-     private  val  settings :  Settings <* , T >,
412-     private  val  icon :  Icon  = RandomnessIcons .Data .Settings 
413- ) : QuickSwitchSchemeAction(true ) {
414-     /* *
415-      * The name of the action. 
416-      */  
417-     abstract  val  name:  String 
418- 
419- 
420-     /* *
421-      * Sets the title and icon of this action. 
422-      * 
423-      * @param event carries information on the invocation place 
424-      */  
425-     override  fun  update (event :  AnActionEvent ) {
426-         super .update(event)
427- 
428-         event.presentation.text =  name
429-         event.presentation.icon =  icon
430-     }
431- 
432-     /* *
433-      * Adds actions for all schemes in `settings` to the given group. 
434-      * 
435-      * @param project ignored 
436-      * @param group the group to add actions to 
437-      * @param dataContext ignored 
438-      */  
439-     override  fun  fillActions (project :  Project ? , group :  DefaultActionGroup , dataContext :  DataContext ) {
440-         val  current =  settings.currentScheme
441- 
442-         settings.schemes.forEach { scheme -> 
443-             val  icon =  if  (scheme == =  current) AllIcons .Actions .Forward  else  ourNotCurrentAction
444- 
445-             group.add(object  :  DumbAwareAction (scheme.myName, " " 
446-                 override  fun  actionPerformed (event :  AnActionEvent ) {
447-                     settings.currentSchemeName =  scheme.name
448-                 }
449-             })
450-         }
451-     }
452- }
0 commit comments