1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
/*
* $Id$
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package adtree;
import ojawa.WorkSpace;
import org.eclipse.core.resources.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.plugin.*;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.osgi.framework.BundleContext;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.event.EventListenerList;
import org.eclipse.ui.*;
import org.eclipse.core.runtime.*;
import adtree.controler.ADTREEControler;
import adtree.scripts.ScriptManager;
/**
* The main plugin class to be used in the desktop.
*/
public class ADTREEPlugin extends AbstractUIPlugin {
private ImageRegistry image_registry = new ImageRegistry();
// part identifier
public static final String CFG_EDITOR_ID = "adtree.editor.jung.CFGEditorPart";
// constants
public static final String PLUGIN_TAG = "ADTREE",
RECENTS_TAG = "recents",
RECENT_TAG = "recent",
WCET_TAG = "wcet";
// element identifiers
public static final String CLASS_COLOR_TAG = "class-color",
CLASS_NUM_TAG = "class-num",
FF_TAG = "ff",
FFC_TAG = "ffct",
FLOWFACT_TAG = "flowfact",
FLOWFACTS_TAG = "flowfacts",
LOOP_TAG = "loop",
NOTE_TAG = "note",
PROGRAM_TAG = "program",
TASK_TAG = "task";
// attribute identifiers
public static final String ADDR_ATT = "addr",
BLUE_ATT = "blue",
BRANCH_ATT = "branch",
CHECKSUM_ATT = "checksum",
CONF_SCRIPT_ATT = "configuration",
CONTROL_ATT = "control",
EXACT_ATT = "exact",
EXEC_ATT = "executed",
EXTERN_ATT = "extern",
FILE_ATT = "file",
GREEN_ATT = "green",
KIND_ATT = "kind",
LABEL_ATT = "label",
LIMIT_ATT = "limit",
LINE_ATT = "line",
LOOPID_ATT = "loopid",
MAX_ATT = "max",
MAXEXPR_ATT = "maxexpr",
MIN_ATT = "min",
NAME_ATTR = "name",
NUMCALL_ATT = "numcall",
OFFSET_ATT = "offset",
OPTION_ATT = "option",
PATH_SCRIPT_ATT = "path_script",
PROJECT_ATT = "project",
RED_ATT = "red",
SOURCE_ATT = "source",
TOTAL_ATT = "total",
TOTEXPR_ATT = "totalexpr",
TSKN_ATT = "taskname",
UNRESOLVED_ATT = "unresolved",
WCET_ATT = "wcet";
// flow fact kinds
public static final String FUNCTION_KIND = "function",
CALL_KIND = "call",
NO_CALL_KIND = "no_call";
public static final String OTAWA_FILE = "otawa.xml";
public static final Event PROP_REFRESH_EVENT = new PropRefreshEvent();
// the shared instance.
private static ADTREEPlugin plugin;
private static final int RECENT_CAPACITY = 5;
private final LinkedList<String> recent_files = new LinkedList<String>();
private static ScriptManager scripts = new ScriptManager();
// colorization
public static final ADTREEControler controler =new ADTREEControler();
//The identifiers for the preferences
public static final String BAD_WORDS_PREFERENCE = "badwords";
public static final String HIGHLIGHT_PREFERENCE = "highlight";
//The default values for the preferences
public static final String DEFAULT_BAD_WORDS = "bug;bogus;hack;";
public static final int DEFAULT_HIGHLIGHT = SWT.COLOR_BLUE;
// configuration
public boolean display_unnamed = false,
display_unlabeled = true;
// data
private EventListenerList listeners = new EventListenerList();
private LinkedList<Prog> progs = new LinkedList<Prog>();
boolean colorize = false;
Task colorized_task = null;
/**
* The constructor.
*/
public ADTREEPlugin() {
plugin = this;
}
/**
* Get the script manager.
* @return Script manager.
*/
public final ScriptManager getScripts() {
return scripts;
}
/**
* Get the image registry.
* @return Image registry.
*/
public final ImageRegistry getImageRegistry() {
return image_registry;
}
/**
* Declare the image from the given list of pair (image key, image path).
* @param images Images to declare.
*/
public final void declareImages(String[] images) {
String iconPath = "/icons/";
for(int i = 0; i < images.length - 1; i += 2)
if(image_registry.get(images[i]) == null)
image_registry.put(
images[i],
ImageDescriptor.createFromFile(adtree.views.property.ViewProperty.class, iconPath + images[i + 1])
);
}
/**
* Get an image from its key.
* @param key Image key.
* @return Image or null if none.
*/
public Image getImage(String key) {
return image_registry.get(key);
}
/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
// build OJAWA path
String dirpath = null;
String path = FileLocator.toFileURL(FileLocator.find(context.getBundle(), new Path("/"), null)).getPath();
String os = java.lang.System.getProperty("os.name");
if(os.equals("Linux"))
path = path + "/libotawa/lib/libojawa.so";
else if(os.startsWith("Windows")){
path = FileLocator.getBundleFile(context.getBundle()).getAbsolutePath();
dirpath = path + "\\libotawa\\lib\\";
path = path + "\\libotawa\\lib\\libojawa.dll";
//if(new File(path).exists())
// path = "found "+path;
}
else {
path = null;
ADTREEPlugin.logError("unsupported OS: OTAWA library shouldn't be accessible.", null);
}
// link with OJAWA
try {
if(os.startsWith("Windows")){
System.load(dirpath+"libxml2.dll");
System.load(dirpath+"iconv.dll");
System.load(dirpath+"zlib1.dll");
System.load(dirpath+"libexslt.dll");
System.load(dirpath+"libxslt.dll");
}
System.load(path);
logInfo("using relocated OJAWA from " + path);
}
catch(java.lang.UnsatisfiedLinkError e) {
logInfo("can not find relocated OJAWA at " + path + ": attempting from libraries of LD_LIBRARY_PATH.");
e.printStackTrace();
if(os.equals("Linux"))
System.loadLibrary("ojawa");
else if(os.startsWith("Windows"))
System.loadLibrary("libojawa");
}
String info = "OJAWA V" + ojawa.Otawa.version() + " (" + ojawa.Otawa.date() + ") found";
logInfo(info);
// configure Swing UI
/* !!WARNING!! With last version of JVM (1.6), seems to cause CDT freeze ?!?
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc2) {
System.err.println("Could not load LookAndFeel: ");
}*/
// load configuration
loadConf();
}
/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
saveConf();
super.stop(context);
plugin = null;
}
/**
* Returns the shared instance.
*/
public static ADTREEPlugin getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path.
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("ADTREE", path);
}
/**
* Get the URL of a resource in the plugin.
* @param path Path relative to the plugin.
* @return Matching URL.
*/
public static URL getResourceURL(String path) {
return FileLocator.find(ADTREEPlugin.getDefault().getBundle(), new Path(path), null);
}
/**
* Save the state of the plugin.
*/
private void saveConf() {
// save WCETs
saveProgConf();
// save configuration
XMLMemento memento = XMLMemento.createWriteRoot(PLUGIN_TAG);
saveConf(memento);
FileWriter writer = null;
try {
writer = new FileWriter(getConfFile());
memento.save(writer);
}
catch(IOException e) {
logError("cannot save the configuration", e);
}
finally {
if(writer != null)
try {
writer.close();
} catch (IOException e) {
logError("cannot close configuration", e);
}
}
}
/**
* Get the configuration file.
* @return Configuration file.
*/
private java.io.File getConfFile() {
return plugin.getStateLocation().append("conf.xml").toFile();
}
/**
* Save the configuration in the given memento.
* @param memento Memento to save in.
*/
private void saveConf(XMLMemento memento) {
IMemento child = memento.createChild(RECENTS_TAG);
for(String file : recent_files) {
IMemento mem = child.createChild(RECENT_TAG);
mem.putTextData(file);
}
}
/**
* Load the configuration from a memento.
* @param memento Memento to load.
*/
private void loadConf(XMLMemento memento) {
IMemento[] children = memento.getChildren(RECENTS_TAG);
recent_files.clear();
if(children.length != 0) {
IMemento[] files = children[0].getChildren(RECENT_TAG);
for(int i = 0; i < files.length; i++)
recent_files.addLast(files[i].getTextData());
}
}
/**
* Load the configuration.
*/
private void loadConf() {
FileReader reader = null;
try {
reader = new FileReader(getConfFile());
XMLMemento memento = XMLMemento.createReadRoot(reader);
loadConf(memento);
} catch (FileNotFoundException e) {
logInfo("configuration does not exists: default configuration");
} catch (WorkbenchException e) {
logError("bad configuration file", e);
}
finally {
if(reader != null)
try {
reader.close();
} catch (IOException e) {
logError("cannot close configuration", e);
}
}
}
/**
* Add a file to the list of recent files.
* @param file File to add.
*/
public void addRecentFile(String file) {
if(recent_files.contains(file))
recent_files.remove(file);
else if(recent_files.size() >= RECENT_CAPACITY)
recent_files.removeLast();
recent_files.addFirst(file);
}
/**
* Get the collection of recent files.
* @return Recent files collection.
*/
public Collection<String> getRecentFiles() {
return recent_files;
}
/**
* Log an error.
* @param message Message to display.
* @param exn Exception causing the error.
*/
public static void logError(String message, Throwable exn) {
plugin.getLog().log(
new Status(IStatus.ERROR, PLUGIN_TAG, IStatus.OK, message, exn));
}
/**
* Log an information.
* @param message Message to display.
*/
public static void logInfo(String message) {
plugin.getLog().log(
new Status(IStatus.INFO, PLUGIN_TAG, IStatus.OK, message, null));
}
/**
* Log an information.
* @param message Message to display.
*/
public static void logWarning(String message) {
plugin.getLog().log(
new Status(IStatus.WARNING, PLUGIN_TAG, IStatus.OK, message, null));
}
/**
* Set the status bar message.
* @param view Current view.
*/
public void setStatusBarMessage(IViewPart view, String message) {
IActionBars bars = view.getViewSite().getActionBars();
bars.getStatusLineManager().setMessage(message);
}
/**
* Display an error dialog.
* @param message Message to display.
*/
public void doErrorDialog(String message) {
Status status = new Status(IStatus.ERROR, "OTAWA Plugin", 0,
message, null);
ErrorDialog.openError(Display.getCurrent().getActiveShell(),
"JFace Error", "The program encountered a problem", status);
}
/**
* Root of event classes.
*/
public static class Event { }
/**
* Event class fire refreshing the property view.
*/
private static class PropRefreshEvent extends Event { }
//------------------------------------------------------------------------------
/**
* Get the list of programs.
* @return Iterable on programs.
*/
public Iterable<Prog> getPrograms() {
return progs;
}
/**
* Find a program by its name.
* @param path Path of the code.
* @return Program or null if not found.
*/
public Prog getProgram(IFile file) {
for(Prog p:progs)
if(p.file.equals(file))
return p;
return null;
}
/**
* Build a new program.
* @param path Path to project.
* @param project Project containing it (may be null).
* @return Created program.
*/
public Prog obtainProgram(IFile file, WorkSpace ws) {
Prog prog = getProgram(file);
if(prog == null) {
prog = new Prog(file, ws);
progs.add(prog);
}
return prog;
}
/**
* Save a program in the configuration.
*/
private void saveProgConf() {
for(Prog prog: progs)
prog.save();
}
/**
* Get the file matching the given absolute path.
* @param path Absolute path.
* @return Matching IFile.
*/
public IFile getFile(String path) {
IPath ipath = new Path(path);
IFile res = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(ipath);
if(res == null)
res = ResourcesPlugin.getWorkspace().getRoot().getFile(ipath);
return res;
}
/**
* Initializes a preference store with default preference values
* for this plug-in.
*/
protected void initializeDefaultPreferences(IPreferenceStore store) {
store.setDefault(BAD_WORDS_PREFERENCE, DEFAULT_BAD_WORDS);
Color color= Display.getDefault().getSystemColor(DEFAULT_HIGHLIGHT);
PreferenceConverter.setDefault(store, HIGHLIGHT_PREFERENCE, color.getRGB());
}
/**
* Add a listener on plug-in events.
* @param listener Listener to add.
*/
public void addListener(Listener listener) {
listeners.add(Listener.class, listener);
}
/**
* Remove a listener on plug-in events.
* @param listener Listener to remove.
*/
public void removeListener(Listener listener) {
listeners.remove(Listener.class, listener);
}
/**
* Add a program.
* @param prog Program to add.
*/
public void addProgram(Prog prog) {
progs.add(prog);
for(Listener listener: listeners.getListeners(Listener.class))
listener.onProgramOpen(prog);
}
/**
* Remove a program.
* @param prog Program to remove.
*/
public void removeProgram(Prog prog) {
prog.close();
progs.remove(prog);
}
/**
* @Author spiquel
* @Date 7 Apr 2009
* @param listener
* @Description Add a colorize listener.
*/
public void addColorizeListener(ColorizeListener listener) {
listeners.add(ColorizeListener.class, listener);
listener.setColorization(colorize, colorized_task);
}
/**
* @Author spiquel
* @Date 7 Apr 2009
* @param listener
* @Description Remove a colorize listener.
*/
public void removeColorizeListener(ColorizeListener listener) {
listeners.remove(ColorizeListener.class, listener);
}
/**
* @Author spiquel
* @Date 7 Apr 2009
* @return
* @Description Test if colorization is enabled.
*/
public boolean isColorizationEnabled() {
return colorize;
}
/**
* Fire an event of colorization.
* @param enabled True for enabling colorization, false else.
* @param part Part emitting the colorization.
*/
public void setColorization(boolean enabled) {
if(colorize == enabled)
return;
colorize = enabled;
for(ColorizeListener listener: listeners.getListeners(ColorizeListener.class))
listener.setColorization(enabled, colorized_task);
}
/**
* Refresh the colorization.
*/
public void refreshColorization() {
for(ColorizeListener listener: listeners.getListeners(ColorizeListener.class))
listener.refresh();
}
/**
* Change the colorized task.
* @param task Set task.
*/
public void setColorizedTask(Task task) {
if(task == colorized_task)
return;
colorized_task = task;
for(ColorizeListener listener: listeners.getListeners(ColorizeListener.class))
listener.changeTask(task);
}
/**
* Return the current colorized task.
*/
public Task getColorizedTask() {
return colorized_task;
}
/**
* Listener about events on the plug-in.
*/
public static interface Listener extends EventListener {
/**
* Called when a program is opened.
* @param prog Opened program.
*/
void onProgramOpen(Prog prog);
}
} |
Partager