Lara Nyman

My experiences of technology

Category: Technical

IntelliJ : Live Templates

One of the most underutilised tools by a developer is their Intergrated Development Environment (IDE).  The two main common IDE’s in use for professional Java development are Eclipse and IntelliJ; the latter being my preferred choice.  There are many different features and functionalities that an IDE provides to help the developer produce higher quality code in an efficient manner. In my opinion, one of the features not used enough is Live Templating (IntelliJ) or Templates (Eclipse).  I thought I would write a short blog to document and highlight a few templates I have set up in my IntelliJ that are invaluable to my development experience.

With the release of IntelliJ 2018.2, JetBrains have improved their documentation around Live Templates and how to create them.  There are three types of live templates:

  1. Simple Templates
  2. Parameterised Templates
  3. Surround Templates

I shall provide examples of the first two as these are the most commonly used and a visual guide on how to set up your own.

Simple Templates

This is used when there are no dynamic variables to change – the code is static.  As an example, IntelliJ has pre-setup:

Abbreviation Code
sout
System.out.println();

Additional templates to the above that I have setup are:

Abbreviation Code Live Template
main
public static void main(String[] args){

}
public static void main(String[] args){
    $END$ 
}
LiveTemplateMain

Parameterised Templates

This is used when there are variables that require the user input.  The IDE has been programmed to try and calculate these automatically.  As an example, IntelliJ has setup:

Abbreviation Code
fori
for (int i = 0; i < ; i++) { }

 

Additional templates to the above that I have setup are:

Abbreviation Code Live Template
logger
public static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(.class)
public static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class)
test
@org.junit.Test
public void ()
{
}
@org.junit.Test
public void $NAME$()
{
   $END$
}

LiveTemplateLogger TemplateVariable LiveTemplateTest

To save having to set up the above custom live templates, which can be a tedious task, I have attached here a copy of my live template settings to be imported into your local version of IntelliJ.  I might add to this list over time so it is worth checking periodically for updates.

Importing Settings

As wordpress does not allow .jar attachments, upon download, please change the file extension to .jar (from .zip) before proceeding with the below steps directly taken from JetBrains documentation:

  1. On the File menu, click Import Settings.
  2. Specify the path to the JAR file with the exported live template configuration.
  3. In the Import Settings dialog box, select the Live templates check box and click OK.
  4. After restarting IntelliJ IDEA, you will see the imported live templates on the Live Templates page of the Settings / Preferences Dialog.

Attachments

‘Tis The Season To Be Jolly

As we near the end of the year, I thought I would delve into my ‘box of things’ to create something apt for the festive season;  I came up with Christmas Lights.

What I used

  1. Raspberry Pi (with Raspbian ‘Wheezy’ operating system (OS) on an SD card)
  2. Micro USB cable power supply
  3. Arduino
  4. Breadboard
  5. 8 x 5mm LED’s
  6. 8 x 330Ω resistors
  7. 10 Jumper wires
  8. Either an Arduberry or a Cable A to Cable B USB (to connect the Raspberry Pi to Arduino)
  9. Edimax wifi adaptor ( If wanting to use Raspberry Pi without physical connection to the router )
  10. Ethernet cable ( If network settings have not already been configured on the Raspberry Pi or you do not have a wifi adaptor )
  11. Laptop/PC

Aim

I wanted to create an application that combined the use of my Raspberry Pi with my Arduino; the use of the Raspberry Pi would act as my mini computer whilst the Arduino would be utilised for the output.  I also wanted to use my Raspberry Pi headless i.e. with no keyboard, mouse or monitor.  Therefore I used virtual network computing (VNC) from my laptop to connect to my Raspberry Pi.

Assumptions

I am making the assumption that the OS has already been loaded correctly onto the SD card and inserted into the Raspberry Pi and that the initial setup of the Raspberry Pi has been completed.  As I have a MacBook, these instructions are tailored to a computer with a UNIX environment.

What I did

1.   Configure Raspberry Pi Network Settings

As I connect my Raspberry Pi to the same network each time I use it, I decided to configure the network connection within the wifi protected access (WPA) client and supplicant on my Raspberry Pi; this enables me to get going quickly each time I turn my device on.

To do this, I initially needed to connect the Raspberry Pi directly to the router using the ethernet cable and be able to ssh into the device to be able to configure the correct settings.  However, in order to ssh, I needed to find out what IP address the Raspberry Pi was connecting to.  By running the following command through terminal on my MacBook, I was able to see the different connections:

arp -a

One of the connections showed:

raspberrypi (192.168.1.97) at 74:da:38:54:8c:ae on en0 ifscope [ethernet]

where 192.168.1.97 is the IP address.  The default username on the Raspberry Pi is ‘pi’, so I was able to ssh by running:

ssh pi@192.168.1.97

When connecting to the host for the first time, you might see the following:

The authenticity of host '192.168.1.97 (192.168.1.97)' can't be established. 
RSA key fingerprint is e3:68:b6:5b:27:37:85:57:f6:90:34:81:ae:3b:66:23. 
Are you sure you want to continue connecting (yes/no)?

You should type ‘yes’ in response.  It will then proceed to add the IP address to the list of known trusted hosts.

The default password on a raspberry pi is ‘raspberry’, so when prompted, this should be typed in.

Now that I was connected to my Raspberry Pi, I was able to proceed with editing the WPA settings.

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Inside of the conf, make sure that there is the following making sure to replace {WifiName} and {WifiPassword} with your wifi connection settings:

trl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="{WifiName}"
psk="{WifiPassword}"
}

You should now restart your Raspberry Pi safely using:

sudo reboot

2.   SSH into the Raspberry Pi

On restarting, you should disconnect your device from the ethernet cable and plug in the wifi dongle if you wish.  On startup, you should now be able to SSH into the device through your wireless wifi connection.  A reminder of the commands are:

arp -a
ssh pi@192.168.1.97

3.   Housekeeping and Installation of Software

I used the advanced package tool (APT) in order to install software onto the Raspberry Pi.  For a bit of housekeeping, the following should be run:

sudo apt-get update 
sudo apt-get upgrade

N.B There will be much output from these commands in the terminal.  I was then able to install a VNC server and the Arduino IDE onto my Raspberry Pi:

sudo apt-get install tightvncserver
sudo apt-get install arduino

4.   Start VNC server

tightvncserver

The first time that this is run, a password will be required to access the VNC desktop.

5.   Start VNC session

vncserver :0 -geometry 1920x1080 -depth 24

6.   Check out the VNC desktop

You need to go to a web browser and type the following as the URL replacing {ipaddress} with the IP address you identified earlier when SSH’ing into the Raspberry Pi :

vnc://{ipaddress}

In this example, I would direct my browser to vnc://192.168.1.97 .  A pop up may appear; you need to specify your IP address as your VNC server and your password is what you have set in step 4.

Now that the setup of the Raspberry Pi has been completed, the configuration of the Arduino can be carried out.

7.   Plug Everything In

Christmas Lights Circuit

Diagram from SparkFun

Christmas Lights Setup

8.  Write Code

You should navigate to the Raspberry Pi desktop and open the ArduinoIDE.  In a new sketch, type the following:

int ledPins[] = {2,3,4,5,6,7,8,9};

void setup()
{
  for(int i = 0; i <= 7; i++)
  {
    pinMode(ledPins[i],OUTPUT);
  }
}

void loop()
{
  oneAfterAnotherLoop();  
}

void oneAfterAnotherLoop()
{
  int delayTime = 100; 
  for(int i = 0; i <= 7; i++)
  {
    digitalWrite(ledPins[i], HIGH);
    delay(delayTime);                
  }                                  
  for(int j = 7; j >= 0; j--)
  {
    digitalWrite(ledPins[j], LOW);
    delay(delayTime);
  }               
}

9.   Upload to Arduino

The Arduino needs to be connected to the Raspberry Pi.  This was done using a cable A to cable B USB.  To upload the code to the Arduino, you need to make sure that you have specified what board you want to upload to.  Under Tools, navigate to Board and select the type of Arduino you own.  I selected Arduino UNO.  You also need to choose a serial port under Tools –> Port.  You are now ready to upload via Sketch –> Upload

10.   Watch the magic!

Christmas Lights

Attachments

© 2019 Lara Nyman

Theme by Anders NorenUp ↑