Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / Java
Tip/Trick

Case Converter, Works as Background Service

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Jun 2012CPOL2 min read 7.9K   1   1
Case converter which runs as a background service and can convert cases fro uppercase to lowercase or vice-versa.

Sample Image - maximum width is 600 pixels

Introduction

Suppose you are writing in a text editor like Notepad. You have written a long series of text, but now you realize that you wanted some text to be in uppercase and by mistake you wrote them in lowercase, then in Notepad there is no feature to correct this. But you may use this software to accomplish this using these simple steps:

  1. Just cut/copy the text you want to convert into uppercase or lowercase (required one time only).
  2. Press F7 to convert text to uppercase letters or F8 converts to lowercase letters (the text is not visible yet).
  3. Now perform paste operation either by Ctrl + v or by mouse right click + paste.
  4. Close the main window to exit the app (required one time only).

Using the Code

I will give an overview of the code which has been used to do this:

Java
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;

Above are the imports which will be needed by the program.

  1. StringSelection is for storing the updated text to be placed in the clipboard.
  2. DataFlavor objects are constant and never change once instantiated. They represent the data format.
  3. Transferable can be used to provide data for a transfer operation.

Now we will define the program:

Java
public class TextConverter extends JFrame 
{
     public native int getKey ();
     static
     {
      System . loadLibrary ("TextConverter");
     }

Here:

  1. We have defined a native method also whose function is getKey(). The purpose of this is to log keypress and determine when user presses F7, F8, F10, F12.
  2. We load the DLL which logs the key.
Java
public static void main(String args[])
{
    //F7 converts text to Uppercase letters and F8 converts to Lowercase letters.
    JFrame textconverter=new JFrame("Text Converter");
    textconverter.setSize(700,700);
    JLabel startinfo0=new JLabel("The Software has Started Running...");
    JLabel startinfo=new JLabel("F10 --> Hide this screen");
    JLabel startinfo1=new JLabel("F12 --> Unhide the Screen");
    JLabel startinfo2=new JLabel("Steps for Usage:");
    JLabel startinfo3=new JLabel("1)Just cut/copy the text you want to convert into uppercase or lowercase");
    JLabel startinfo4=new JLabel("2)Press F7 to convert text to Uppercase letters " + 
           "or F8 converts to Lowercase letters(The text is not visible yet).");
    JLabel startinfo5=new JLabel("3)Now perform paste operation either by ctrl+v or by mouse right click+paste");
    JLabel startinfo6=new JLabel("4)Close this window to exit the app.");
    JLabel blank=new JLabel("");
    JLabel startinfo7=new JLabel("Software has been Developed by....");
    JLabel startinfo8=new JLabel("Anurag Jain");
    JLabel startinfo9=new JLabel("Software Engineer");
    JLabel startinfo10=new JLabel("(cs.anurag.jain@gmail.com)");
    JLabel startinfo11=new JLabel("Project Homepage: https://sourceforge.net/projects/caseconverter/");
    JLabel startinfo12=new JLabel("For any problems or feedback " + 
           "you may contact me directly at cs.anurag.jain@gmail.com");
    startinfo.setVisible(true);
    textconverter.add(startinfo);
    textconverter.add(startinfo1);
    textconverter.add(startinfo2);
    textconverter.add(startinfo3);
    textconverter.add(startinfo4);
    textconverter.add(startinfo5);
    textconverter.add(startinfo6);
    textconverter.add(blank);
    textconverter.add(blank);
    textconverter.add(blank);
    textconverter.add(startinfo7);
    textconverter.add(startinfo8);
    textconverter.add(startinfo9);
    textconverter.add(startinfo10);
    textconverter.add(startinfo11);
    textconverter.add(startinfo12);
    textconverter.setVisible(true);
     textconverter.getContentPane().setLayout(new GridLayout(16,2));
    textconverter.setDefaultCloseOperation(EXIT_ON_CLOSE);

Here:

  1. I have added a JFrame. This JFrame will be used to guide user on how to use this software.
Java
while(true)
{
    int value=new TextConverter().getKey();
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

    try {
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String)t.getTransferData(DataFlavor.stringFlavor);
            if(value==118)
            text=text.toUpperCase();
           if(value==119)
               text=text.toLowerCase();
           if(value==121)
               textconverter.setVisible(false);
           if(value==123)
               textconverter.setVisible(true);
            StringSelection ss = new StringSelection(text);
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
         
        }
    } catch (UnsupportedFlavorException e) {
    } catch (IOException e) {
    }
    }
}
}

Here:

  1. We run an infinite loop because we want the software to run as a background service.
  2. Now we obtain the key which is pressed by the user using the get method. The get method is defined in TextConverter.dll.
  3. We make use of Toolkit to obtain the contents from the clipboard and store them in the transferable object.
  4. We check whether our transferable object supports stringFlavor.
  5. Now we obtain the clipboard data from the Transferable object making use of getTransferData(DataFlavor.stringFlavor).
  6. Now we check which key has been pressed. If it is F7 (118) then we convert text into uppercase, otherwise if it's F8 (119), then lowercase. In case of F10 (121), we hide screen, and for F12 (123), we show the frame.
  7. After this we rewrite the clipboard with this new modified data.
  8. Now if the data you copied was xyz, then after this program runs, the data will become XYZ.

Full source code:

Java
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class TextConverter extends JFrame 
{
     public native int getKey ();
     static
     {
      System . loadLibrary ("TextConverter");
     } 

public static void main(String args[])
{
    //F7 converts text to Uppercase letters and F8 converts to Lowercase letters.
    JFrame textconverter=new JFrame("Text Converter");
    textconverter.setSize(700,700);
    JLabel startinfo0=new JLabel("The Software has Started Running...");
    JLabel startinfo=new JLabel("F10 --> Hide this screen");
    JLabel startinfo1=new JLabel("F12 --> Unhide the Screen");
    JLabel startinfo2=new JLabel("Steps for Usage:");
    JLabel startinfo3=new JLabel("1)Just cut/copy the text you want to convert into uppercase or lowercase");
    JLabel startinfo4=new JLabel("2)Press F7 to convert text to Uppercase letters " + 
           "or F8 converts to Lowercase letters(The text is not visible yet).");
    JLabel startinfo5=new JLabel("3)Now perform paste operation either by ctrl+v or by mouse right click+paste");
    JLabel startinfo6=new JLabel("4)Close this window to exit the app.");
    JLabel blank=new JLabel("");
    JLabel startinfo7=new JLabel("Software has been Developed by....");
    JLabel startinfo8=new JLabel("Anurag Jain");
    JLabel startinfo9=new JLabel("Software Engineer");
    JLabel startinfo10=new JLabel("(cs.anurag.jain@gmail.com)");
    JLabel startinfo11=new JLabel("Project Homepage: https://sourceforge.net/projects/caseconverter/");
    JLabel startinfo12=new JLabel("For any problems or feedback you " + 
           "may contact me directly at cs.anurag.jain@gmail.com");
    startinfo.setVisible(true);
    textconverter.add(startinfo);
    textconverter.add(startinfo1);
    textconverter.add(startinfo2);
    textconverter.add(startinfo3);
    textconverter.add(startinfo4);
    textconverter.add(startinfo5);
    textconverter.add(startinfo6);
    textconverter.add(blank);
    textconverter.add(blank);
    textconverter.add(blank);
    textconverter.add(startinfo7);
    textconverter.add(startinfo8);
    textconverter.add(startinfo9);
    textconverter.add(startinfo10);
    textconverter.add(startinfo11);
    textconverter.add(startinfo12);
    textconverter.setVisible(true);
     textconverter.getContentPane().setLayout(new GridLayout(16,2));
    textconverter.setDefaultCloseOperation(EXIT_ON_CLOSE);
    while(true)
    {
    int value=new TextConverter().getKey();
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

    try {
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String)t.getTransferData(DataFlavor.stringFlavor);
            if(value==118)
            text=text.toUpperCase();
           if(value==119)
               text=text.toLowerCase();
           if(value==121)
               textconverter.setVisible(false);
           if(value==123)
               textconverter.setVisible(true);
            StringSelection ss = new StringSelection(text);
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
         
        }
    } catch (UnsupportedFlavorException e) {
    } catch (IOException e) {
    }
    }
}
}

Points of Interest

This software runs in the background, so there is no GUI (one screen but you can hide it). Now it is too easy to convert cases with this software. The best thing is it is not dependent on any editor or screen, it will work anywhere.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I am a Java software developer. I like to make new software which can be helpful to people. You may get in touch with me at https://cooltrickshome.blogspot.in

Comments and Discussions

 
QuestionNice Job Pin
Member 1332314222-Jul-17 2:52
Member 1332314222-Jul-17 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.