00001 package com.generalrobotix.ui.grxui;
00002
00003 import java.io.File;
00004 import java.io.IOException;
00005 import java.io.RandomAccessFile;
00006 import java.nio.channels.OverlappingFileLockException;
00007 import java.net.URL;
00008 import java.util.Date;
00009 import java.text.SimpleDateFormat;
00010
00011 import org.eclipse.core.runtime.FileLocator;
00012 import org.eclipse.jface.preference.IPreferenceStore;
00013 import org.eclipse.jface.preference.PreferenceConverter;
00014 import org.eclipse.jface.resource.ColorRegistry;
00015 import org.eclipse.jface.resource.FontRegistry;
00016 import org.eclipse.jface.resource.ImageDescriptor;
00017 import org.eclipse.jface.resource.ImageRegistry;
00018 import org.eclipse.jface.resource.StringConverter;
00019 import org.eclipse.jface.dialogs.MessageDialog;
00020 import org.eclipse.swt.SWT;
00021 import org.eclipse.swt.graphics.Color;
00022 import org.eclipse.swt.graphics.Font;
00023 import org.eclipse.swt.graphics.FontData;
00024 import org.eclipse.swt.graphics.Image;
00025 import org.eclipse.swt.graphics.RGB;
00026 import org.eclipse.swt.widgets.Display;
00027 import org.eclipse.ui.IPerspectiveDescriptor;
00028 import org.eclipse.ui.IPerspectiveListener;
00029 import org.eclipse.ui.IPerspectiveRegistry;
00030 import org.eclipse.ui.IWindowListener;
00031 import org.eclipse.ui.IWorkbench;
00032 import org.eclipse.ui.IWorkbenchListener;
00033 import org.eclipse.ui.IWorkbenchPage;
00034 import org.eclipse.ui.IWorkbenchWindow;
00035 import org.eclipse.ui.PerspectiveAdapter;
00036 import org.eclipse.ui.PlatformUI;
00037 import org.eclipse.ui.plugin.AbstractUIPlugin;
00038 import org.eclipse.ui.preferences.ScopedPreferenceStore;
00039
00040 import org.osgi.framework.BundleContext;
00041 import org.osgi.framework.BundleEvent;
00042 import org.osgi.framework.FrameworkEvent;
00043 import org.osgi.framework.FrameworkListener;
00044 import org.osgi.framework.SynchronousBundleListener;
00045
00046 import com.generalrobotix.ui.GrxPluginManager;
00047 import com.generalrobotix.ui.util.GrxCorbaUtil;
00048 import com.generalrobotix.ui.util.GrxDebugUtil;
00049 import com.generalrobotix.ui.util.GrxProcessManager;
00050 import com.generalrobotix.ui.util.GrxServerManager;
00051 import com.generalrobotix.ui.util.MessageBundle;
00052 import com.generalrobotix.ui.util.GrxProcessManager.ProcessInfo;
00053 import com.generalrobotix.ui.grxui.GrxUIPerspectiveFactory;
00054
00058 public class Activator extends AbstractUIPlugin implements IWorkbenchListener, IWindowListener, IPerspectiveListener{
00059 public static final String PLUGIN_ID = "com.generalrobotix.ui.grxui";
00060 private static Activator plugin;
00061 private static final String LINUX_HOME_DIR = System.getenv("HOME") + File.separator ;
00062 private static final String WIN_HOME_DIR = System.getenv("APPDATA") + File.separator;
00063 private static final String LINUX_TMP_DIR = LINUX_HOME_DIR + ".OpenHRP-3.1" + File.separator;
00064 private static final String WIN_TMP_DIR = WIN_HOME_DIR + "OpenHRP-3.1" + File.separator;
00065 private static final File HOME_DIR = initHomeDir();
00066 private static final File TMP_DIR = initTempDir();
00067 public GrxPluginManager manager_;
00068 private ImageRegistry ireg_ = null;
00069 private FontRegistry freg_ = null;
00070 private ColorRegistry creg_ = null;
00071 private boolean bStartedGrxUI_ = false;
00072 private boolean initProjectLoad_ = false;
00073 private final static String[] images_ = { "save_edit.png",
00074 "saveas_edit.png",
00075 "sim_start.png",
00076 "sim_stop.png",
00077 "sim_script_start.png",
00078 "sim_script_stop.png",
00079 "grxrobot1.png",
00080 "robot_servo_start.png",
00081 "robot_servo_stop.png",
00082 "icon_fastrwd.png",
00083 "icon_slowrwd.png",
00084 "icon_pause.png",
00085 "icon_playback.png",
00086 "icon_slowfwd.png",
00087 "icon_fastfwd.png",
00088 "icond_fastrwd.png",
00089 "icond_slowrwd.png",
00090 "icond_pause.png",
00091 "icond_playback.png",
00092 "icond_slowfwd.png",
00093 "icond_fastfwd.png",
00094 "icon_frame+.png",
00095 "icon_frame-.png"};
00096 private final static File lockFilePath_ = new File( TMP_DIR, "tryLockFileInActivator");
00097 private SimpleDateFormat dateFormat_ = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS z Z");
00098 private RandomAccessFile lockFile_ = null;
00099
00100 public void postShutdown(IWorkbench workbench) {
00101 }
00102
00103 public boolean preShutdown(IWorkbench workbench, boolean forced) {
00104 try {
00105 IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
00106 for(IWorkbenchWindow window : windows){
00107 IWorkbenchPage[] pages = window.getPages();
00108 for(IWorkbenchPage page : pages){
00109 IPerspectiveDescriptor[] perspectives = page.getOpenPerspectives();
00110 for(IPerspectiveDescriptor perspective : perspectives){
00111 if(perspective.getId().equals(GrxUIPerspectiveFactory.ID+".project"))
00112 page.closePerspective(perspective, false, false);
00113 }
00114 }
00115 }
00116 IPerspectiveRegistry perspectiveRegistry=workbench.getPerspectiveRegistry();
00117 IPerspectiveDescriptor tempPd=perspectiveRegistry.findPerspectiveWithId(GrxUIPerspectiveFactory.ID + ".project");
00118 if(tempPd!=null)
00119 perspectiveRegistry.deletePerspective(tempPd);
00120 } catch (Exception ex) {
00121 ex.printStackTrace();
00122 }
00123 return true;
00124 }
00125
00126
00127 public void windowActivated(IWorkbenchWindow window) {
00128 }
00129
00130 public void windowClosed(IWorkbenchWindow window) {
00131 window.removePerspectiveListener(this);
00132 }
00133
00134 public void windowDeactivated(IWorkbenchWindow window) {
00135 }
00136
00137 public void windowOpened(IWorkbenchWindow window) {
00138 window.addPerspectiveListener(this);
00139 checkServer();
00140 }
00141
00142 public void perspectiveActivated(IWorkbenchPage page,
00143 IPerspectiveDescriptor perspective) {
00144 if(!initProjectLoad_)
00145 if(perspective.getId().equals(GrxUIPerspectiveFactory.ID ))
00146 loadInitialProject();
00147 }
00148
00149 public void loadInitialProject(){
00150 if(!initProjectLoad_){
00151 initProjectLoad_ = true;
00152 manager_.loadInitialProject();
00153 }
00154 }
00155
00156 public void perspectiveChanged(IWorkbenchPage page,
00157 IPerspectiveDescriptor perspective, String changeId) {
00158 }
00159
00160 public Activator() {
00161 System.out.println("[ACTIVATOR] CONSTRUCT");
00162 }
00163
00167 public void start(BundleContext context)
00168 throws Exception {
00169 super.start(context);
00170 plugin = this;
00171
00172
00173 GrxDebugUtil.setDebugFlag(true);
00174
00175 File cur = new File(".");
00176 URL cur_url = cur.toURI().toURL();
00177 GrxDebugUtil.println("[ACTIVATOR] START in " + cur_url);
00178
00179
00180 if(PlatformUI.isWorkbenchRunning()){
00181 IWorkbench workbench = PlatformUI.getWorkbench();
00182 workbench.addWorkbenchListener(this);
00183 workbench.addWindowListener(this);
00184 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
00185 if(window!=null)
00186 window.addPerspectiveListener(this);
00187 registryImage();
00188 registryFont();
00189 registryColor();
00190
00191 if( lockFile_ == null ){
00192 try{
00193 tryLockFile();
00194 } catch (Exception ex) {
00195 ex.printStackTrace();
00196 return;
00197 }
00198 startGrxUI();
00199 }
00200 }
00201 }
00202
00203 public void registryImage() throws Exception{
00204 ireg_ = new ImageRegistry();
00205 for(int i=0; i<images_.length; i++){
00206 URL url = getClass().getResource("/resources/images/"+images_[i]);
00207 ireg_.put(images_[i], ImageDescriptor.createFromURL(url));
00208 }
00209
00210 }
00211
00212 public void registryFont(){
00213 freg_ = new FontRegistry();
00214 FontData[] monospaced = {new FontData("monospaced", 10, SWT.NORMAL)};
00215 freg_.put("monospaced", monospaced);
00216 FontData[] dialog10 = {new FontData("dialog", 10, SWT.NORMAL )};
00217 freg_.put("dialog10", dialog10);
00218 FontData[] dialog12 = {new FontData("dialog", 12, SWT.NORMAL )};
00219 freg_.put("dialog12", dialog12);
00220 updateTableFont();
00221 updateEditerFont();
00222 }
00223
00224 public void updateTableFont(){
00225 if(freg_ == null)
00226 return;
00227
00228 IPreferenceStore store = getPreferenceStore();
00229 FontData[] table = PreferenceConverter.getFontDataArray(store, PreferenceConstants.FONT_TABLE);
00230 freg_.put("preference_table", table);
00231
00232 FontData[] normal = table.clone();
00233 for (int i=0; i<table.length; i++) {
00234 normal[i] = new FontData(table[i].getName(), table[i].getHeight(), SWT.NORMAL);
00235 }
00236 freg_.put("robot_state_plain", normal);
00237
00238 FontData[] bold = table.clone();
00239 for (int i=0; i<bold.length; i++) {
00240 bold[i] = new FontData(table[i].getName(), table[i].getHeight(), SWT.NORMAL);
00241 }
00242 freg_.put("robot_state_bold", bold);
00243 }
00244
00245 public void updateEditerFont(){
00246 if(freg_ == null)
00247 return;
00248
00249 IPreferenceStore store = getPreferenceStore();
00250 FontData[] editer = PreferenceConverter.getFontDataArray(store, PreferenceConstants.FONT_EDITER);
00251 freg_.put("preference_editer", editer);
00252 }
00253
00254 public void registryColor(){
00255 creg_ = new ColorRegistry();
00256 RGB focusedColor = new RGB(0,0,100);
00257 setColor("focusedColor", focusedColor);
00258 RGB markerColor = new RGB(255,128,128);
00259 setColor("markerColor", markerColor);
00260 RGB green = Display.getDefault().getSystemColor(SWT.COLOR_GREEN).getRGB();
00261 setColor("green", green);
00262 RGB yellow = Display.getDefault().getSystemColor(SWT.COLOR_YELLOW).getRGB();
00263 setColor("yellow", yellow);
00264 RGB cyan = Display.getDefault().getSystemColor(SWT.COLOR_CYAN).getRGB();
00265 creg_.put("cyan", cyan);
00266 RGB magenta = Display.getDefault().getSystemColor(SWT.COLOR_MAGENTA).getRGB();
00267 setColor("magenta", magenta);
00268 RGB red = Display.getDefault().getSystemColor(SWT.COLOR_RED).getRGB();
00269 setColor("red", red);
00270 RGB blue = Display.getDefault().getSystemColor(SWT.COLOR_BLUE).getRGB();
00271 setColor("blue", blue);
00272 RGB black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK).getRGB();
00273 setColor("black", black);
00274 RGB gray = Display.getDefault().getSystemColor(SWT.COLOR_GRAY).getRGB();
00275 setColor("gray", gray);
00276 RGB darkGray = Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY).getRGB();
00277 setColor("darkGray", darkGray);
00278 RGB white = Display.getDefault().getSystemColor(SWT.COLOR_WHITE).getRGB();
00279 setColor("white", white);
00280 }
00281
00282
00288 public static String getPath() {
00289 URL entry = getDefault().getBundle().getEntry("/");
00290 String pluginDirectory = "";
00291 try {
00292 pluginDirectory = FileLocator.resolve(entry).getPath();
00293 } catch (IOException e) {
00294 e.printStackTrace();
00295 }
00296 return pluginDirectory;
00297 }
00298
00304 public File getHomeDir(){
00305 return HOME_DIR;
00306 }
00307
00314 public File getTempDir(){
00315 return TMP_DIR;
00316 }
00317
00318
00324 public static Activator getDefault() {
00325 return plugin;
00326 }
00327
00332 public static void refNSHostPort(StringBuffer nsHost, StringBuffer nsPort) {
00333 if (nsHost.length() == 0){
00334 nsHost.append(GrxCorbaUtil.nsHost());
00335 }
00336 if (nsPort.length() == 0){
00337 nsPort.append(Integer.toString(GrxCorbaUtil.nsPort()));
00338 }
00339 }
00340
00341
00342 public Image getImage(String iconName) {
00343 return ireg_.get(iconName);
00344 }
00345
00346
00347 public ImageDescriptor getDescriptor(String iconName) {
00348 return ireg_.getDescriptor(iconName);
00349 }
00350
00351 public Font getFont(String fontName){
00352 return freg_.get(fontName);
00353 }
00354
00355 public Color getColor(RGB rgb){
00356 String s=StringConverter.asString(rgb);
00357 Color color = creg_.get(s);
00358 if(color==null)
00359 creg_.put(s, rgb);
00360 return creg_.get(s);
00361 }
00362
00363 public Color getColor(String colorName){
00364 Color color = creg_.get(colorName);
00365 if(color==null)
00366 color = creg_.get("white");
00367 return color;
00368 }
00369
00370 public void setColor(String colorName, RGB rgb){
00371 creg_.put(colorName, rgb);
00372 }
00373
00374
00375
00376
00377
00378
00379
00380 public void stop(BundleContext context)
00381 throws Exception {
00382 super.stop(context);
00383 stopGrxUI();
00384 }
00385
00386 public void startGrxUI() {
00387 if( !bStartedGrxUI_ ) {
00388
00389
00390 GrxProcessManager.shutDown();
00391
00392 bStartedGrxUI_ = true;
00393 manager_ = new GrxPluginManager();
00394 manager_.start();
00395 }
00396 }
00397
00398 public void stopGrxUI() {
00399 if(!bStartedGrxUI_)
00400 return;
00401 manager_.shutdown();
00402 manager_ = null;
00403 releaseLockFile();
00404 ScopedPreferenceStore store = (ScopedPreferenceStore)plugin.getPreferenceStore();
00405 try {
00406 store.save();
00407 } catch (IOException e) {
00408 e.printStackTrace();
00409 }
00410 bStartedGrxUI_ = false;
00411 }
00412
00413 public ImageRegistry getImageRegistry(){
00414 return ireg_;
00415 }
00416
00417 public FontRegistry getFontRegistry(){
00418 return freg_;
00419 }
00420
00421 public ColorRegistry getColorRegistry(){
00422 return creg_;
00423 }
00424
00425 public void tryLockFile() throws Exception{
00426 try{
00427 String localStr = "Start GrxUI:" +
00428 dateFormat_.format(new Date()) + System.getProperty("line.separator");
00429 lockFile_ = new RandomAccessFile(lockFilePath_,"rwd");
00430
00431 if ( lockFile_.getChannel().tryLock() == null){
00432 throw new OverlappingFileLockException();
00433 }
00434 lockFile_.seek(lockFile_.length());
00435 lockFile_.write(localStr.getBytes());
00436 } catch(Exception eX){
00437 eX.printStackTrace();
00438 if(lockFile_ != null){
00439 lockFile_.close();
00440 lockFile_ = null;
00441 }
00442 throw eX;
00443 }
00444 }
00445
00446 public void releaseLockFile(){
00447 try{
00448 if(lockFile_ != null){
00449 String localStr = "End GrxUI:" +
00450 dateFormat_.format(new Date()) + System.getProperty("line.separator");
00451 lockFile_.write(localStr.getBytes());
00452 lockFile_.close();
00453 lockFile_ = null;
00454 }
00455 } catch(Exception ex){
00456 ex.printStackTrace();
00457 }
00458 }
00459
00460 public void breakStart(Exception eX, IPerspectiveDescriptor closeDesc ){
00461
00462 MessageDialog.openError(
00463 Display.getDefault().getActiveShell(),
00464 MessageBundle.get("Activator.dialog.title.doubleban"),
00465 MessageBundle.get("Activator.dialog.message.doubleban") );
00466
00467 IWorkbench work = PlatformUI.getWorkbench();
00468 IWorkbenchWindow wnd = work.getActiveWorkbenchWindow();
00469 if(wnd == null){
00470
00471 work.close();
00472 System.exit( PlatformUI.RETURN_UNSTARTABLE );
00473 } else {
00474 IWorkbenchPage page = wnd.getActivePage();
00475 if(closeDesc == null){
00476
00477 closeDesc = page.getPerspective();
00478 }
00479 page.closePerspective(closeDesc, false, false);
00480 for( IPerspectiveDescriptor local : page.getOpenPerspectives()){
00481 if( !local.getId().equals(GrxUIPerspectiveFactory.ID)){
00482 page.setPerspective(local);
00483 break;
00484 }
00485 }
00486 }
00487 }
00488
00489 static private File initTempDir() {
00490 File ret = null;
00491
00492 String dir = System.getenv("ROBOT_DIR");
00493 if (dir != null && new File(dir).isDirectory()) {
00494 ret = new File(dir+File.separator);
00495 } else {
00496 if ( System.getProperty("os.name").equals("Linux") ||
00497 System.getProperty("os.name").equals("Mac OS X")) {
00498 ret = new File(LINUX_TMP_DIR);
00499 } else {
00500 ret = new File(WIN_TMP_DIR);
00501 }
00502 }
00503 if( !ret.exists() ){
00504 ret.mkdirs();
00505 }
00506
00507 return ret;
00508 }
00509
00510 static private File initHomeDir() {
00511 File ret = null;
00512
00513 if ( System.getProperty("os.name").equals("Linux") ||
00514 System.getProperty("os.name").equals("Mac OS X")) {
00515 ret = new File(LINUX_HOME_DIR);
00516 } else {
00517 ret = new File(WIN_HOME_DIR);
00518 }
00519 if( !ret.exists() ){
00520 ret.mkdirs();
00521 }
00522
00523 return ret;
00524 }
00525
00526 public void checkServer() {
00527 GrxServerManager server_ = (GrxServerManager) manager_.getItem("serverManager");
00528 for(ProcessInfo pi : server_.getServerInfo()){
00529 try{
00530 GrxCorbaUtil.getReference(pi.id)._non_existent();
00531 }catch (Exception ex) {
00532 MessageDialog.openError(GrxUIPerspectiveFactory.getCurrentShell(),
00533 MessageBundle.get("GrxModelItem.dialog.title.error"),
00534 pi.id + " " + MessageBundle.get("GrxItem.dialog.message.NoServer") );
00535 }
00536 }
00537
00538 }
00539
00540 }