Power to Build

Home » Misc

Category Archives: Misc

Sharing: A GUI for the Command Line Interface | JBoss Developer


We are getting into Redhat JBOSS for our Java project. I am currently exploring all the options available in JBOSS (also comparing it with Sybase EA Server we are using currently). I intend to share our experiences with the platform here.

One of the things I use a lot in EA Server will be the Jagtool – a command line interface to the app Server’s repository. In JBoss, I found Jboss-cli to do this for us. And I just found out from the below link, that you can actually start that in GUI mode as well. This link has a detailed information on the GUI mode. More on the tool later.

Source: A GUI for the Command Line Interface | JBoss Developer

Sharing: 5 Best C Programming Books A C Programmer Must Read


Sharing:

Learn C programming by reading some of the best books from expert authors

Source: 5 Best C Programming Books A C Programmer Must Read

27 Best Free Eclipse Plug-ins for Java Developer to be Productive


Sharing:

An exhaustive list of best hand picked free Eclipse plug-ins for a developer to be efficient

Source: 27 Best Free Eclipse Plug-ins for Java Developer to be Productive

Regex Part 2


I earlier posted about exciting world of Regex here.  Let’s try to learn some more in this followup post.

First if you want to play with Regex, here is a nice tool you can use:

http://regexr.com

2017-05-25 17_39_08-RegExr_ Learn, Build, & Test RegEx

Regex Tool online

By default it comes up with a sample pattern and the matches in the given sample text.  You can type in the pattern you are testing, the actual text in the middle box and if you chose Replace, you get to see what the output will look like.

Let’s use this tool to convert our RSVP list in free text to a spreadsheet list.

If you read my original post, we had 4 RSVPs. It looked somewhat like this:

RSVP:

Name:
Will you attend:
Will you be here for lunch:
How many:

Here is my session in Regexr testing this:

2017-05-25 17_44_49-regexr.com_3g1nb

Using Regexr to test/convert our RSVP text

The bottom portion shows the Replaced text – converted and cleaned, readied to be copied out to Excel. The spreadsheet will look like this:

2017-05-25 17_53_11-Microsoft Excel - Book3

Here is the actual regular expression used in this example:

RSVP[0-9]+:\n+Name:[ ]*(.+)\nWill you attend:(.*)\nWill you be here for lunch:(.*)\nHow many:.*([0-9]+).*[\n]*

Let’s go through the regular expression for arriving at this:

First line would have, RSVP:

In Regex any number can be represented by [0-9]+ **. That is a digit is represented by any character between 0 and 9. A whole number would then be one or more digits.

Thus, RSVP: can be written as,

RSVP[0-9]+:

Next text is actually on another line. This can represented by \n. You may know this is newline. Now we are on the second line.

Name:<name>

This can be matched by, Name: (.*)

That would be any character after Name: . If you notice, we have a space after Name:. What if the user didn’t add any space or added many spaces in between? We can rewrite the above as,

Name:[ ]*(.*)

That’s a space inside the square brackets. [ ]* represents 0 or more spaces. This way, we will match it, even if there is no space between the label “Name:” and the name that followis it. If used with a +, it will mean one or more spaces – at least one space must be there.

Next we have (.*). Dot (.) is any character and .* means zero or more characters. By enclosing a pattern within parantheses (), we make a “Capture Group”. This is what will be remembered (like in a variable) when this pattern is matched with a text. This can be returned and useful in Replace functionality. Each captured group will have a number associated with it – like $1*** for the first, $2 for the second etc. This is also called back reference. More on this later.

And we continue with the rest of the lines. The replace pattern will look like this:

$1\t$2\t$3\t\$4\n

That’s all captured variables ($1 – $4) separated by tab characters recognized by Excel as cell separators (we could also use Comma). End it with \n to push to the next line.

Voila! We have successfully converted a free-form text into a spreadsheet.

That was a rudimentary example. And if you noticed, I left out some complications – for e.g., what if they said 1 or 2 people will be coming and I want to take the maximum, meaning 2.

The fact is, Regex is never used alone in a tool like this. Often times, you will build this (and more regex) into a script/program to achieve the full desired results. Perl and Python are heavily invested in Regex. Other programming languages (including Java) do support Regex using some library API.

To be contd…


** As an aside, this is similar to the BNF form for writing language grammar (I’ve considered only positive whole numbers for this discussion):

2017-05-26 09_58_13-Document6 - Microsoft Word.png

*** Sometimes you will see \1 instead of $1.

Quicktip: Windows Remote Desktop – Screen size


Remote Desktop
Do you use Windows Remote Desktop to connect to other computers? If you work in an environment with a lot of servers/machines, chances are you do this constantly.
Typically, you will click on the Remote Desktop icon and you get a screen that looks the one shown below.

It asks you for a computer/server name to connect to. You type in the address and click connect, you get the login window for the remote machine. You type those in and click OK, you will be looking at the other machine’s desktop on your PC. Great tool to have, if you are dealing with multiple machines and you don’t want to get up and go to the other one (what if it that is in the other coast or across the oceans?) or you don’t need multiple monitors.

The display issue
I have one pet peeve about this software. I don’t know about you, but the most annoying thing for me, when using this application, is the display size. Often times, the windows gets scrunched in a corner or too big for the current (host machine’s) display. In this case, you will have to constantly scroll up and down to get to the Start button/task bar and the top of the file you are editing. With so many varied size of monitors available, it seems to be an endless fight!

Where is the start button and the task bar??
Here are some ways to fix that. Actually, Remote Desktop program does offer screen resolution adjustment. This may work for you.

Adjusting the Display size in Remote Desktop program (mstsc)
If you pay attention there is a “Options” button on the left. If you click on this button and go to Display option, you will the below screen.

Here you have an option to make it Full screen (just slide down to the right).

This typically, fixes the issue. After you do this, your remote desktop screen come in full screen, so you won’t the problem mentioned above.

Even if it looks like it is set to Large, try changing it and change back to Large, that may fix the issue.

What if that didn’t help?

But, in reality above tip may not always work. This is where I found the tips in below site very helpful.

5 tips for fixing Remote Desktop Screen settings

We have been clicking on the Remote Desktop icon blindly, that we forget that the program (mstsc*) takes parameters. Try forcing it to open in Fullscreen mode always, by adding it to the shortcut you click for Remote Desktop.

mstsc* /f

Or if you want a specfic size, you can pass in the preferred height and witdth.

mstsc* /H:980 /W:1924

(The actual height and width you use may be different for you. I have a wide monitor).

Alternatively, you can actually save these settings (Save button On the General tab) into a .RDP file and use that to connect to the remote machine. RDP file is actually a text file!! You can edit the settings. You edit the below settings to reolve the display size issue:

DesktopHeight:i:980
Desktopwidth:i:1924

Again these are for my screen. Change to suit your need.

After you do this, hopefully you never again have to do the endless scrolling up and down in Remote Desktop! Here is my rescued Start menu!

See here for more about the program and its parameters.

* In case you are wondering what is mstsc, it is the Remote desktop program on Windows. MSCTSC refers Microsoft Terminal Services Client, probably left over from earlier times. They keep changing the “name/description” of these programs, to keep you guessing!

Java 9 update…


Java 9 is (still) coming! Here is another post from the author that gave us "Ultimate Guide to Java 9" recently.

https://www.sitepoint.com/inside-java-9-part-i/

Here are some features that caught my attention in Java 9:

Multi-version jar – it actually contains class files for different versions in a single Jar. Interesting. If you ever tried to run newer classes with older JVM, you would run into problem. With this, you can create class files for different versions and keep them separate.

The more I look at it, Java is slowly absorbing the "features" it refused to include from C/C++, in the first place. Wouldn’t a preprocessor + JIT better to handle this?? In those days, the programs had to have small foot print both in memory and disk space. With both available cheap, we seem to be able to splurge a little. I cannot imagine running Java on the same machine I ran my small C programs!

Do you really have to call it java9?? The Java runtime executable seem to be called that. Sounds like we are moving more and more like PowerBuilder, where you need to have the exact version of PBVM to corresponding version code. Every version of PB had to have its own executable PB10, PB125 etc.

Here is another blog that lists 5 important features in Java 9.

Here is a complete list of features in Java 9 in the Open JDK release:
http://openjdk.java.net/projects/jdk9/

There are mentions about security, logging, graphics etc. There is a Jshell, to run/test Java commands interactively – I think it’s getting a facelift from an earlier version. I see these are new features, but nothing sounds earth shattering or change the way I would program in Java. Hope to see some more before the final release comes out in 2017?

According to this site, we have another 264 days before we see the final release. Lot could change in the time we wait for it. This blog post talks about why Java 9 is delayed again?! Did you know Java 9 project is called Project Jigsaw?

Gotcha: Java JScrollPane


So, you are working with Swing. You build a small window with a Text box (JTextArea or JTextPane) like listed here. Everything works fine. You show text nicely wrapped in the text box.

Suddenly one day, the text pasted on to the Text box is longer than what’s in the view (View port). Sure, you say, I have seen this in other platforms. Just scroll, right?. Wait, there is no Scroll bar. Oops! Can you scroll with a up/down arrow key like in Windows? Nope! No luck! If you are coming from other IDE environment like me (PowerBuilder, Visual Studio – VB6/VB.Net or even C#) this will be a shocker.

In PowerBuilder, I would slap on Multi-line Edit on to a Window. Check the checkboxes for turning on Horizontal and Vertical Scrollbars. Set the text into the box. Bingo!

In PB and other such platforms, you don’t normally see the scrollbars, but the
Scrollbars appear magically, when the text is longer than the viewport, if you opted for it. You can also set Autoscroll instead of scrollbars, where user can scroll endlessly using the arrow key,until the memory runs out.

For e.g.,

In PowerBuilder, the code will look like this:

type mle_1 from multilineedit within w_sheet
 ....
 ....
 boolean init_hscrollbar = true
 boolean init_vscrollbar = true

end type

In C#, the scrollbars are added by default. If you want only Vertical bar only, you can set it like this:

      this.richTextBox1.ScrollBars =
             System.Windows.Forms.RichTextBoxScrollBars.Vertical;

This wraps text in the text box horizontally.

As you can see, scrollbars are just properties of the Text box cotrol. But then all the languages I mentioned are tightly coupled with the OS and the OS API has a lot of System functions that can be called as seen in C# code above.

None of those here in Java. Java tries to be everything for everyone. It has to mimic a lot OS level functions within, so it can run anywhere. In Java (here Swing) everything is available, but you have to go get it.Often times, you get a lot of options available to do the same thing, you have to find and use the right solution for you. And the Java solution is always like, if I can do this for this object, can I also do it for that other object? Thus, they try to extract the Scrolling functionality on its own and make it available for any component like Text box, or a JTable. You can even embed a pane (JPanel) onto a Scroll Pane. This approach of “decorating” any Visual object with Scroll Pane follows the Decorator Pattern. Here is a nice course material explaining how this is done.

Let’s see how the scrollbars can be added to a text box in Java:

To create a window, you create a JFrame or some kind of container (Equivalent to a Window in Windows environment). You plop a JPanel on to it, so you can contain all the controls together. Then you add a JTextArea on to the JPanel for your text box.

Wait, did you want scrolling capability? That’s a different pane; just add a JScrollPane to the JPanel, then add the JTextArea inside the JScrollPane. It is very hierarchical and very Object oriented. You can easily replace JTextArea with a Jtable or Jpanel to get scrollbar capability elsewhere.

Now, for the Gotcha part. Did I say, add the JTextArea to the JScrollPane? Wrong thing to do. This is what you do, typically add a component to a container/component using add method, but not so with JScrollPane. Here is why:

JScrollPane has a component called “View Port” already added to it. We can show a single component inside this “View Port”. So, essentially you will have to put the JTextArea in the View. Here is a diagram that may help you:

JScrollPane-1.gif

To do this in a Java way, you simply pass your component to the Constructor of JScrollPane. If you didn’t, then you will have to set the component to the View Port using, you guessed it,

scrollPane.setViewPortView(component)

or you can do it round about using

scrollPane.getViewPort.SetView(component)

But then, you have surely seen an add method in the Javadocs for JScrollPane?! Sure, it does have one, as it inherited it from JContainer. Like I said, JScrollPane already has a component inside it – that’s the ViewPort component. So, if you use the add method, you are replacing the Viewport with your component. That’s why if you added your component directly (by mistake) to the JScrollPane, your view port is gone and it will be grayed out.

So, here is the finished Java sample for a Text box with (only) Vertical Scrollbar:

import java.awt.*;
import javax.swing.*;

public class TextWithScrollbars
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 600);

      JTextArea area = new JTextArea(40, 60); // 40 rows, 60 columns
      JScrollPane scrollPane = new JScrollPane(area);
      scrollPane.setHorizontalScrollBar(null);

      Container contentPane = frame.getContentPane();
      contentPane.add(scrollPane, BorderLayout.CENTER);

      frame.setVisible(true);
   }
}

The line that reads,

scrollPane.setHorizontalScrollBar(null);

turns off the HorizontalScrollBar by setting it to Null. Otherwise, you would see that as well. Without horizontal scroll bar, it will automatically wrap text within the View port.