下面列出了怎么用org.eclipse.ui.console.IOConsole的API类实例代码及写法,或者点击链接到github查看源代码。
private static void onNewConsole(IOConsole console) {
IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
// See if a console exists matching this one and remove it if it does,
// so that we don't have multiple of the same console (they would be identical anyway)
IConsole[] existingMCConsoles = consoleManager.getConsoles();
for (IConsole existingConsole : existingMCConsoles) {
if (existingConsole.getName().equals(console.getName())) {
consoleManager.removeConsoles(new IConsole[] { existingConsole } );
break;
}
}
Logger.log(String.format("Creating new application console: %s of type %s", //$NON-NLS-1$
console.getName(), console.getClass().getSimpleName()));
consoleManager.addConsoles(new IConsole[] { console });
}
/**
* Fins the console with a given name
* @param name, name of the console
* @return
*/
private static IOConsole findConsole(String name)
{
if (name == null)
{
throw new IllegalArgumentException("Console name must be not null");
}
IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
IConsole[] existing = consoleManager.getConsoles();
// try to find existing
for (int i = 0; i < existing.length; i++)
{
if (name.equals(existing[i].getName()))
{
return (IOConsole) existing[i];
}
}
// no console found, create a new one
IOConsole myConsole = new IOConsole(name, null);
consoleManager.addConsoles(new IConsole[] { myConsole });
return myConsole;
}
private void addRef(IOConsole console) {
synchronized (referencesLock) {
//We'll clear the current references and add the new one if it's not there already.
int size = weakrefs.size();
for (int i = 0; i < size; i++) {
WeakReference<IOConsole> ref = weakrefs.get(i);
Object object = ref.get();
if (object == console) {
return; //already there (nothing to add).
}
if (object == null) {
weakrefs.remove(i);
i--;
size--;
}
}
//Add the new reference.
weakrefs.add(new WeakReference<IOConsole>(console));
}
}
private ArrayList<IOConsole> getCurrentRefs() {
int size = weakrefs.size();
ArrayList<IOConsole> currentRefs = new ArrayList<IOConsole>(size);
for (int i = 0; i < size; i++) {
WeakReference<IOConsole> ref = weakrefs.get(i);
IOConsole object = ref.get();
if (object == null) {
weakrefs.remove(i);
i--;
size--;
} else {
currentRefs.add(object);
}
}
return currentRefs;
}
@SuppressWarnings("restriction")
private void processIOConsole(IOConsole ioConsole) {
IDocument document = ioConsole.getDocument();
try {
Position[] positions = document.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY);
Arrays.sort(positions, new Comparator<Position>() {
@Override
public int compare(Position o1, Position o2) {
return Integer.compare(o1.getOffset(), o2.getOffset());
}
});
if (positions.length > 0) {
Position p = positions[positions.length - 1];
if (p instanceof ConsoleHyperlinkPosition) {
ConsoleHyperlinkPosition consoleHyperlinkPosition = (ConsoleHyperlinkPosition) p;
IHyperlink hyperLink = consoleHyperlinkPosition.getHyperLink();
hyperLink.linkActivated();
}
}
} catch (BadPositionCategoryException e) {
Log.log(e);
}
}
private CloudSdkDebugTarget(ILaunch launch, LocalAppEngineServerBehaviour serverBehaviour,
IOConsole console) {
super(null);
this.launch = launch;
this.serverBehaviour = serverBehaviour;
server = serverBehaviour.getServer();
this.console = console;
}
private static BufferedReader getConsoleReader() {
final IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
final List<IConsole> tlcConsole = Arrays.asList(consoleManager.getConsoles()).stream()
.filter(c -> "TLC-Console".equals(c.getName())).collect(Collectors.toList());
if (!tlcConsole.isEmpty()) {
final IConsole iConsole = tlcConsole.get(0);
if (iConsole instanceof IOConsole) {
IOConsoleInputStream inputStream = ((IOConsole) iConsole).getInputStream();
return new BufferedReader(new InputStreamReader(inputStream));
}
}
return null;
}
protected void showConsole() {
IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
IConsole[] consoles = consoleManager.getConsoles();
for (IConsole iConsole : consoles) {
if (TYPE.equals(iConsole.getType())) {
if (iConsole instanceof IOConsole) {
((IOConsole) iConsole).activate();
consoleManager.showConsoleView(iConsole);
}
}
}
}
@Override
public void ownCreatePartControl(final Composite parent) {
msgConsole = new IOConsole("GAMA Console", null);
setCharacterLimit(GamaPreferences.Interface.CORE_CONSOLE_SIZE.getValue());
GamaPreferences.Interface.CORE_CONSOLE_SIZE.onChange(newValue -> setCharacterLimit(newValue));
viewer = new IOConsoleViewer(parent, msgConsole);
viewer.setWordWrap(GamaPreferences.Interface.CORE_CONSOLE_WRAP.getValue());
}
public void consume(InputStream outputStream)
{
IOConsole messageConsole = getMessageConsole(consoleName);
messageConsole.activate();
MessageConsoleWriter messageConsoleWriter = new MessageConsoleWriter(messageConsole, outputStream, isStdErr);
new Thread(messageConsoleWriter).start();
}
public static IOConsole findConsole() {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++) {
if (KARAF_CONSOLE.equals(existing[i].getName()))
return (IOConsole) existing[i];
}
// no console found, so create a new one
IOConsole myConsole = new IOConsole(KARAF_CONSOLE, null);
conMan.addConsoles(new IConsole[] { myConsole });
return myConsole;
}
public static void clearConsole() {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++) {
if (KARAF_CONSOLE.equals(existing[i].getName())) {
((IOConsole) existing[i]).destroy();
conMan.removeConsoles(new IConsole[] { existing[i] });
}
}
}
@Override
public void preferenceChange(PreferenceChangeEvent event) {
String key = event.getKey();
if ("org.eclipse.debug.ui.consoleBackground".equals(key) || "org.eclipse.debug.ui.outColor".equals(key)
|| "org.eclipse.debug.ui.errorColor".equals(key)) {
synchronized (referencesLock) {
ArrayList<IOConsole> currentRefs = getCurrentRefs();
for (IOConsole console : currentRefs) {
updateConsole(console);
}
}
}
}
@Override
public void run(IAction action) {
for (IConsoleView c : ScriptConsole.iterConsoles()) {
IConsole console = c.getConsole();
if (console instanceof IOConsole) {
IOConsole ioConsole = (IOConsole) console;
processIOConsole(ioConsole);
break;
}
}
}
protected void addProcessConsole(IOConsole c) {
// What we'd like to do is not put in the input stream the contents we received
// in the console UNLESS we're waiting for input (but unfortunately, it seems there's
// no API for that).
//
// This means that if the user writes something (to do an evaluation) and later
// does a raw_input('say something:\n'), the raw_input will get the contents that
// the user wrote for the evaluation and not the contents it'd write now.
// As we now have a separate input console, this shouldn't be so troublesome, as
// we control things better when the user writes to the PromptOverlay console, but
// if he writes to the other console, things may misbehave.
}
private IOConsole findOrCreateConsole(String name) {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++)
if (name.equals(existing[i].getName()))
return (IOConsole) existing[i];
IOConsole console = new IOConsole(name, null);
conMan.addConsoles(new IConsole[] { console });
return console;
}
public static IOConsole getTLCConsole()
{
IOConsole console = findConsole(TLC_ID);
return console;
}
public IOConsole getConsole() {
if (console == null)
createConsole();
return console;
}
public InputStream getInputStream()
{
IOConsole messageConsole = getMessageConsole(consoleName);
messageConsole.activate();
return messageConsole.getInputStream();
}
private MessageConsoleWriter(IOConsole messageConsole, InputStream from, boolean isStdErr)
{
this.messageConsole = messageConsole;
this.from = from;
this.isStdErr = isStdErr;
}
@Override
public void run() {
IOConsole console = findOrCreateConsole(M.FormulaInterpreter);
ConsoleJob job = new ConsoleJob(console);
job.schedule();
}
public ConsoleJob(IOConsole console) {
super(M.FormulaInterpreter);
this.console = console;
}
/**
* In this method we'll make sure a console will have its colors properly updated.
*
* @param console This is the console to keep updated.
*
* The console should have a getAttribute("themeConsoleStreamToColor") which returns a Map<IOConsoleOutputStream, String>
* where the values may be:
*
* "console.output" or "console.error"
*/
public void keepConsoleColorsSynched(IOConsole console) {
updateConsole(console);
addRef(console);
}