This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have to develop GUI based interface for a (already developed)command prompt application. Condition is "I cant touch its source code.I have to play on interface level."
The problem is:
I dont know when that command prompt appication will aks for input.So how can I provide input to command prompt based application ? when that application is waiting for input from keyborad;at that time my GUI based interface suppose to provide input to command prompt based application.
I dont really have the idea about how to go with this. Please help me to come to any solution.
This isn't impossible at all; I've done it a million times. Maybe you're thinking it's impossible because you think the GUI has to understand the prompts from the command-line program; that's not the case at all. Instead, it just has to properly react to them, and the way it does that is because you program in the correct response for every possible prompt. The first thing you need to do is create a flowchart on paper, or a state machine using a FSM notation, which represents the flow of prompts and inputs. Although you can't modify the source, hopefully you can look at it, as this will make it easy to verify that you've covered all the possibilities.
So you write a separate program to be the GUI. The GUI program uses Runtime.exec() to start the command-line program. It reads from the Process output/error streams, and write to the input stream using a set of independent threads.
Imagine you start the command-line program and it says
Your GUI program knows (because you know) that these are the options when the program starts up. So your GUI has a menu (let's say) with four commands: "Defrag", "Install Linux", "Nuke", "Quit". The handlers for these four menu items are real simple: the last one just prints "D" to the process's input stream, while the first three each print a character to the stream, then put up a modal dialog (let's say) which provides a GUI for the corresponding sub-tree of the flowchart, with all the possible input information. If the program prints results or information, then your GUI program knows when this will happen, and reads the information from the streams at the appropriate time, displaying it in the GUI.
A tool like ExpectJ is a big help in reading and reacting to the command-line program's output.
As a programming exercise can the GUI interface just be a passthru from/to the commmandline program? The GUI pgm starts the cmdline pgm and reads its prompt, displays that prompt in its GUI, gets the user's response and passes that back to the cmdline pgm, etc etc. This would be a general purpose program that wouldn't need to be tailored to the cmdline program. It's only parameter would be the path to the cmdline pgm. [ July 02, 2008: Message edited by: Norm Radder ]
Joined: Nov 13, 2006
Thanks for the reply (solution) Ernest Friedman-Hill.:-)
I understood what you really want to say."sowftware application is nothing but responses generated for the events occured ." So I just have to keep the track of events fired on to application and Process's input & output stream will solve my probelm.
I will again post on same thread once I finish with this assignment (successfully :-).
Hi Mr. Norm Radder ,
At least for general purpose application. Isn't window based Application is answer to your question that has converted DOS to Window based Operating System . I think so. :-)
But for the specific application.. same question arise once agin.. that how will you keep the track of prompts,a command based application is asking for. So you need a different different GUI applications for different different applications .
Well but I still have a doubt in my mind.. would the "general purpose program " will help .
Joined: Oct 13, 2005
I was mistaken. Sorry.
I think you will have to write a tiny application and use Runtime.exec() to start it from a GUI. Remember you have to "drain" both streams which accompany the Process created by Runtime.exec(). There have been several threads about that in the last two weeks; this is just one of many threads you can find with search.