Home » Newcomers » Newcomers » How do you "Clear" the console view programmatically?
How do you "Clear" the console view programmatically? [message #173040] |
Fri, 29 September 2006 20:44 |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: talbste.iit.edu
I am writing a simple test program. It is a simple reverse polish
notation (RPN) / postfix math expression calculator. I enter an
expression in the console view (i.e.: 34+52/*) and I output the result
(17.5) using "System.out.println( result );" to the console view.
What's my question then, you ask? Well, instead of leaving the output in
the console view, I wish to "clear" the console programmatically.
I know that I can right-click on the console view and select "clear",
which clears the console view. However, I prefer the programmatic method.
I have attempted the following to make this work (as yet to no avail):
1) my project hierarchy is:
- Project: Project1
- Package: Test
- File: PostFix.java
1)Import the "org.eclipse.ui.console" plug-in
- Apparently, the only way to access the console is through plug-in API.
- Right click "import", with "Project1" selected, in the package explorer
window.
- In the "Import" window, select "Plug-ins and Fragments" and click
next->next
- In the "Plug-ins and Fragments" window, select
"org.eclipse.ui.console(3.1.100.v20060605)", click the button "Add->"
- Click the "Finish" button
- The plug-in project "org.eclipse.ui.console" will show up in your
package explorer
2) Configure the Java Build Path for "Project1"
- Select "Project1" in the package explorer window.
- Right click, scroll to the bottom and select "Properties"
- OR, Click on File drop down menu, scroll to the bottom and select
"Properties"
- In the Java Build Path window, select the "Projects" tab
- Click the "add" button
- In the "Required Project Selection" window, "org.eclipse.ui.console"
should be available, so click its checkbox to select it
- Click the "Ok" button
3) Use "import" statement in my file PostFix.java
- Until I did steps 1 & 2 above, I was unable to use an "import"
statement to link to "org.eclipse" source code.
- I believe something like the following should work, but this doesnt
work yet:
line # | code
------------------------------------------------------------ ---------
141 "ConsolePlugin consolePlugin = ConsolePlugin.getDefault();
142 IConsoleManager consoleManager = consolePlugin.getConsoleManager();
143 IConsole[] consoles = consoleManager.getConsoles();"
- After this, I would do something like "consoles[].clearConsole()"
- But I would have to know which element in the array "consoles" to use ..
( console[0], console[1], etc )
- Also, when I attempted this, I got an exception:
"Exception in thread "main" java.lang.NullPointerException
at lab_5_Exercise_3.PostFix.main(PostFix.java:142)"
This is presumably because line 141 or line 142 returns null, which
suggests to me that I am using the wrong package ..
("ConsolePlugin.getDefault()" is supposed to return a singleton instance
of the ConsolePlugin, which I had figured would be instantiated by Eclipse
upon starting Eclipse .. not returning "null") ..
4) So what about the "org.eclipse.ui" package, eh?
- The class "PlatformUI" is the self-proclaimed "central class for access
to the Eclipse Platform User Interface" where "This class cannot be
instantiated; all functionality is provided by static methods"
- So you would think this would be the next best place to look for access
to the console, right?
- Try something like
"IWorkbench workBench = PlatformUI.getWorkbench();"
assuming that the "workbench" is supposed to be analogous to the "JFrame"
in Swing.
- But then what?
Your assistance is much appreciated. I have attempted to find information
pertaining to this topic in the help files and in the newsgroups, but I
was unable to locate anything at all. I sincerely hope that I have been
thorough enough in presenting my question.
NOTE: When/if you answer this post, I would appreciate it greatly if you
could also EMAIL me directly your response, in order to expedite the
delivery. Thanks alot! (also, see below for "PostFix.java")
Steve
talbste@iit.edu
freydakin@yahoo.com
PS: The following is my code for "PostFix.java"
package Test;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
import org.eclipse.ui.console.*;
public class PostFix {
public PostFix() {}
public static void main( String[] args ) {
LStack valueStack = new LStack(); // stack of operands and results
char digit = 'w'; // Input operand/operator
float operand1 = 0,
operand2 = 0, // operands in current operation
result = 0; // result of operation on operands
// for the current operation
System.out.println("POSTFIX CALCULATOR");
System.out.println("(aka, \"Reverse Polish Notation\")");
System.out.println();
System.out.println("Enter an expression consisting "
+ "of a sequence of single digits mixed with operands.");
System.out.println();
System.out.println("Only the following operands are allowed: \"+,
-, *, /\"");
System.out.println();
System.out.println("Expression should be entered in postfix
format.");
System.out.println();
System.out.println("i.e.: \"34+52/*\" is equivalent to
\"(3+4)*(5/2)\"");
System.out.println();
System.out.println("Also, no parantheses, brackets, carrots, etc
allowed.");
System.out.println();
System.out.println("To QUIT, type \"Q\" or \"q\".");
System.out.println();
do
{
System.out.print("Expression: "); // Read expression
try { // catch IOExceptions
digit = (char) System.in.read(); // read in the 1st char
while ( Character.isWhitespace( digit ) ||
( digit == '\n' ) || ( digit == '\r' ) ) { // ignore
whitespace
digit = (char) System.in.read();
}
while ( ( digit != '\n' ) && ( digit != '\r' ) && // ignore returns
( digit != 'Q' ) && ( digit != 'q' ) &&
( digit != 'C' ) && ( digit != 'c' ) ) { // flag quit
if ( Character.isDigit( digit ) ) { // push digit onto stack
String charString = new Character( digit ).toString();
alueStack.push( new Float( charString ) ); // store 1 digit
}
while ( Character.isWhitespace( digit ) ){ // ignore
whitespace
digit = (char) System.in.read();
}
switch ( digit ) {
case '+' : // add
// push/pop order is unimportant to operation
operand1 = ((Float) valueStack.pop()).floatValue();
operand2 = ((Float) valueStack.pop()).floatValue();
result = ( operand1 + operand2 );
valueStack.push( new Float( result ) );
break;
case '-' : // subtract
// ie: "ab-" is push(a), push(b)
// pop b (operand1), pop a (operand2)
// a - b (operand2 - operand1)
operand1 = ((Float) valueStack.pop()).floatValue();
operand2 = ((Float) valueStack.pop()).floatValue();
result = ( operand2 - operand1 );
valueStack.push( new Float( result ) );
break;
case '*' : // multiply
// push/pop order is unimportant to operation
operand1 = ((Float) valueStack.pop()).floatValue();
operand2 = ((Float) valueStack.pop()).floatValue();
result = ( operand1 * operand2 );
valueStack.push( new Float( result ) );
break;
case '/' : // divide
// ie: "ab/" is push(a), push(b)
// pop b (operand1), pop a (operand2)
// a / b (operand2 / operand1)
operand1 = ((Float) valueStack.pop()).floatValue();
operand2 = ((Float) valueStack.pop()).floatValue();
result = ( operand2 / operand1 );
valueStack.push( new Float( result ) );
break;
//default : // Invalid expression
//if ( !( Character.isWhitespace( digit ) ) )
//System.out.println("Invalid expression");
} // switch
digit = (char) System.in.read(); // read in the next
char
} // while
if ( (digit == 'Q') || (digit == 'q') ){
break; // quit if letter "Q" or "q" encountered
}
if ( (digit == 'C') || (digit == 'c') ){
ConsolePlugin consolePlugin = ConsolePlugin.getDefault();
IConsoleManager consoleManager = consolePlugin.getConsoleManager();
IConsole[] consoles = consoleManager.getConsoles();
for (int i = 0; i < consoles.length; i++) {
// see whats there
System.out.println("console: " + consoles[i].getName());
}
//IOConsole.clearConsole();
break; // quit if letter "Q" or "q" encountered
}
// Output & Format the RESULT:
DecimalFormat format = new DecimalFormat("0.##");
result = (Float) valueStack.pop();
System.out.println();
System.out.println(" RESULT: " + format.format( result ) );
System.out.println();
} catch (IOException ioe) {
}
} while ( digit != 'Q' && digit != 'q' );
} // end of main
}
|
|
|
Goto Forum:
Current Time: Sun Sep 01 08:15:11 GMT 2024
Powered by FUDForum. Page generated in 0.07981 seconds
|