GrxUIonEclipse-project-0.9.8/src/com/generalrobotix/ui/grxui/Activator.java
Go to the documentation of this file.
1 package com.generalrobotix.ui.grxui;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.RandomAccessFile;
6 import java.nio.channels.OverlappingFileLockException;
7 import java.net.URL;
8 import java.util.Date;
9 import java.text.SimpleDateFormat;
10 
11 import org.eclipse.core.runtime.FileLocator;
12 import org.eclipse.jface.preference.IPreferenceStore;
13 import org.eclipse.jface.preference.PreferenceConverter;
14 import org.eclipse.jface.resource.ColorRegistry;
15 import org.eclipse.jface.resource.FontRegistry;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.resource.ImageRegistry;
18 import org.eclipse.jface.resource.StringConverter;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.FontData;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.RGB;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.ui.IPerspectiveDescriptor;
28 import org.eclipse.ui.IPerspectiveListener;
29 import org.eclipse.ui.IPerspectiveRegistry;
30 import org.eclipse.ui.IWindowListener;
31 import org.eclipse.ui.IWorkbench;
32 import org.eclipse.ui.IWorkbenchListener;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PerspectiveAdapter;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.plugin.AbstractUIPlugin;
38 import org.eclipse.ui.preferences.ScopedPreferenceStore;
39 
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.BundleEvent;
42 import org.osgi.framework.FrameworkEvent;
43 import org.osgi.framework.FrameworkListener;
44 import org.osgi.framework.SynchronousBundleListener;
45 
54 
58 public class Activator extends AbstractUIPlugin implements IWorkbenchListener, IWindowListener, IPerspectiveListener{
59  public static final String PLUGIN_ID = "com.generalrobotix.ui.grxui";
60  private static Activator plugin;
61  private static final String LINUX_HOME_DIR = System.getenv("HOME") + File.separator ;
62  private static final String WIN_HOME_DIR = System.getenv("APPDATA") + File.separator;
63  private static final String LINUX_TMP_DIR = LINUX_HOME_DIR + ".OpenHRP-3.1" + File.separator;
64  private static final String WIN_TMP_DIR = WIN_HOME_DIR + "OpenHRP-3.1" + File.separator;
65  private static final File HOME_DIR = initHomeDir();
66  private static final File TMP_DIR = initTempDir();
68  private ImageRegistry ireg_ = null;
69  private FontRegistry freg_ = null;
70  private ColorRegistry creg_ = null;
71  private boolean bStartedGrxUI_ = false;
72  private boolean initProjectLoad_ = false;
73  private final static String[] images_ = { "save_edit.png",
74  "saveas_edit.png",
75  "sim_start.png",
76  "sim_stop.png",
77  "sim_script_start.png",
78  "sim_script_stop.png",
79  "grxrobot1.png",
80  "robot_servo_start.png",
81  "robot_servo_stop.png",
82  "icon_fastrwd.png",
83  "icon_slowrwd.png",
84  "icon_pause.png",
85  "icon_playback.png",
86  "icon_slowfwd.png",
87  "icon_fastfwd.png",
88  "icond_fastrwd.png",
89  "icond_slowrwd.png",
90  "icond_pause.png",
91  "icond_playback.png",
92  "icond_slowfwd.png",
93  "icond_fastfwd.png",
94  "icon_frame+.png",
95  "icon_frame-.png"};
96  private final static File lockFilePath_ = new File( TMP_DIR, "tryLockFileInActivator");
97  private SimpleDateFormat dateFormat_ = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS z Z");
98  private RandomAccessFile lockFile_ = null;
99 
100  public void postShutdown(IWorkbench workbench) {
101  }
102 
103  public boolean preShutdown(IWorkbench workbench, boolean forced) {
104  try {
105  IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
106  for(IWorkbenchWindow window : windows){
107  IWorkbenchPage[] pages = window.getPages();
108  for(IWorkbenchPage page : pages){
109  IPerspectiveDescriptor[] perspectives = page.getOpenPerspectives();
110  for(IPerspectiveDescriptor perspective : perspectives){
111  if(perspective.getId().equals(GrxUIPerspectiveFactory.ID+".project"))
112  page.closePerspective(perspective, false, false);
113  }
114  }
115  }
116  IPerspectiveRegistry perspectiveRegistry=workbench.getPerspectiveRegistry();
117  IPerspectiveDescriptor tempPd=perspectiveRegistry.findPerspectiveWithId(GrxUIPerspectiveFactory.ID + ".project");
118  if(tempPd!=null)
119  perspectiveRegistry.deletePerspective(tempPd);
120  } catch (Exception ex) {
121  ex.printStackTrace();
122  }
123  return true;
124  }
125 
126 
127  public void windowActivated(IWorkbenchWindow window) {
128  }
129 
130  public void windowClosed(IWorkbenchWindow window) {
131  window.removePerspectiveListener(this);
132  }
133 
134  public void windowDeactivated(IWorkbenchWindow window) {
135  }
136 
137  public void windowOpened(IWorkbenchWindow window) {
138  window.addPerspectiveListener(this);
139  checkServer();
140  }
141 
142  public void perspectiveActivated(IWorkbenchPage page,
143  IPerspectiveDescriptor perspective) {
144  if(!initProjectLoad_)
145  if(perspective.getId().equals(GrxUIPerspectiveFactory.ID ))
147  }
148 
149  public void loadInitialProject(){
150  if(!initProjectLoad_){
151  initProjectLoad_ = true;
152  manager_.loadInitialProject();
153  }
154  }
155 
156  public void perspectiveChanged(IWorkbenchPage page,
157  IPerspectiveDescriptor perspective, String changeId) {
158  }
159 
160  public Activator() {
161  System.out.println("[ACTIVATOR] CONSTRUCT");
162  }
163 
167  public void start(BundleContext context)
168  throws Exception {
169  super.start(context);
170  plugin = this;
171 
172  // デバッグ表示モード
174 
175  File cur = new File(".");
176  URL cur_url = cur.toURI().toURL();
177  GrxDebugUtil.println("[ACTIVATOR] START in " + cur_url);
178 
179  // plugin で実行の時 RCPの時はこの時にはまだ、Workbenchが作成されていない  //
180  if(PlatformUI.isWorkbenchRunning()){
181  IWorkbench workbench = PlatformUI.getWorkbench();
182  workbench.addWorkbenchListener(this);
183  workbench.addWindowListener(this);
184  IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
185  if(window!=null)
186  window.addPerspectiveListener(this);
187  registryImage();
188  registryFont();
189  registryColor();
190 
191  if( lockFile_ == null ){
192  try{
193  tryLockFile();
194  } catch (Exception ex) {
195  ex.printStackTrace();
196  return;
197  }
198  startGrxUI();
199  }
200  }
201  }
202 
203  public void registryImage() throws Exception{
204  ireg_ = new ImageRegistry();
205  for(int i=0; i<images_.length; i++){
206  URL url = getClass().getResource("/resources/images/"+images_[i]);
207  ireg_.put(images_[i], ImageDescriptor.createFromURL(url));
208  }
209 
210  }
211 
212  public void registryFont(){
213  freg_ = new FontRegistry();
214  FontData[] monospaced = {new FontData("monospaced", 10, SWT.NORMAL)};
215  freg_.put("monospaced", monospaced);
216  FontData[] dialog10 = {new FontData("dialog", 10, SWT.NORMAL )};
217  freg_.put("dialog10", dialog10);
218  FontData[] dialog12 = {new FontData("dialog", 12, SWT.NORMAL )};
219  freg_.put("dialog12", dialog12);
220  updateTableFont();
222  }
223 
224  public void updateTableFont(){
225  if(freg_ == null)
226  return;
227 
228  IPreferenceStore store = getPreferenceStore();
229  FontData[] table = PreferenceConverter.getFontDataArray(store, PreferenceConstants.FONT_TABLE);
230  freg_.put("preference_table", table);
231 
232  FontData[] normal = table.clone();
233  for (int i=0; i<table.length; i++) {
234  normal[i] = new FontData(table[i].getName(), table[i].getHeight(), SWT.NORMAL);
235  }
236  freg_.put("robot_state_plain", normal);
237 
238  FontData[] bold = table.clone();
239  for (int i=0; i<bold.length; i++) {
240  bold[i] = new FontData(table[i].getName(), table[i].getHeight(), SWT.NORMAL);
241  }
242  freg_.put("robot_state_bold", bold);
243  }
244 
245  public void updateEditerFont(){
246  if(freg_ == null)
247  return;
248 
249  IPreferenceStore store = getPreferenceStore();
250  FontData[] editer = PreferenceConverter.getFontDataArray(store, PreferenceConstants.FONT_EDITER);
251  freg_.put("preference_editer", editer);
252  }
253 
254  public void registryColor(){
255  creg_ = new ColorRegistry();
256  RGB focusedColor = new RGB(0,0,100);
257  setColor("focusedColor", focusedColor);
258  RGB markerColor = new RGB(255,128,128);
259  setColor("markerColor", markerColor);
260  RGB green = Display.getDefault().getSystemColor(SWT.COLOR_GREEN).getRGB();
261  setColor("green", green);
262  RGB yellow = Display.getDefault().getSystemColor(SWT.COLOR_YELLOW).getRGB();
263  setColor("yellow", yellow);
264  RGB cyan = Display.getDefault().getSystemColor(SWT.COLOR_CYAN).getRGB();
265  creg_.put("cyan", cyan);
266  RGB magenta = Display.getDefault().getSystemColor(SWT.COLOR_MAGENTA).getRGB();
267  setColor("magenta", magenta);
268  RGB red = Display.getDefault().getSystemColor(SWT.COLOR_RED).getRGB();
269  setColor("red", red);
270  RGB blue = Display.getDefault().getSystemColor(SWT.COLOR_BLUE).getRGB();
271  setColor("blue", blue);
272  RGB black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK).getRGB();
273  setColor("black", black);
274  RGB gray = Display.getDefault().getSystemColor(SWT.COLOR_GRAY).getRGB();
275  setColor("gray", gray);
276  RGB darkGray = Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY).getRGB();
277  setColor("darkGray", darkGray);
278  RGB white = Display.getDefault().getSystemColor(SWT.COLOR_WHITE).getRGB();
279  setColor("white", white);
280  }
281 
282 
288  public static String getPath() {
289  URL entry = getDefault().getBundle().getEntry("/");
290  String pluginDirectory = "";
291  try {
292  pluginDirectory = FileLocator.resolve(entry).getPath();
293  } catch (IOException e) {
294  e.printStackTrace();
295  }
296  return pluginDirectory;
297  }
298 
304  public File getHomeDir(){
305  return HOME_DIR;
306  }
307 
314  public File getTempDir(){
315  return TMP_DIR;
316  }
317 
318 
324  public static Activator getDefault() {
325  return plugin;
326  }
327 
332  public static void refNSHostPort(StringBuffer nsHost, StringBuffer nsPort) {
333  if (nsHost.length() == 0){
334  nsHost.append(GrxCorbaUtil.nsHost());
335  }
336  if (nsPort.length() == 0){
337  nsPort.append(Integer.toString(GrxCorbaUtil.nsPort()));
338  }
339  }
340 
341  // アイコンの取得
342  public Image getImage(String iconName) {
343  return ireg_.get(iconName);
344  }
345 
346  // アイコンデスクリプタの取得
347  public ImageDescriptor getDescriptor(String iconName) {
348  return ireg_.getDescriptor(iconName);
349  }
350 
351  public Font getFont(String fontName){
352  return freg_.get(fontName);
353  }
354 
355  public Color getColor(RGB rgb){
356  String s=StringConverter.asString(rgb);
357  Color color = creg_.get(s);
358  if(color==null)
359  creg_.put(s, rgb);
360  return creg_.get(s);
361  }
362 
363  public Color getColor(String colorName){
364  Color color = creg_.get(colorName);
365  if(color==null)
366  color = creg_.get("white");
367  return color;
368  }
369 
370  public void setColor(String colorName, RGB rgb){
371  creg_.put(colorName, rgb);
372  }
373  /*
374  * (non-Javadoc)
375  *
376  * @see
377  * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
378  * )
379  */
380  public void stop(BundleContext context)
381  throws Exception {
382  super.stop(context);
383  stopGrxUI();
384  }
385 
386  public void startGrxUI() {
387  if( !bStartedGrxUI_ ) {
388  //暫定処置 Grx3DViewがGUIのみの機能に分離できたらstopGrxUIか
389  //manager_.shutdownで処理できるように変更
391 
392  bStartedGrxUI_ = true;
393  manager_ = new GrxPluginManager();
394  manager_.start();
395  }
396  }
397 
398  public void stopGrxUI() {
399  if(!bStartedGrxUI_)
400  return;
401  manager_.shutdown();
402  manager_ = null;
403  releaseLockFile();
404  ScopedPreferenceStore store = (ScopedPreferenceStore)plugin.getPreferenceStore();
405  try {
406  store.save();
407  } catch (IOException e) {
408  e.printStackTrace();
409  }
410  bStartedGrxUI_ = false;
411  }
412 
413  public ImageRegistry getImageRegistry(){
414  return ireg_;
415  }
416 
417  public FontRegistry getFontRegistry(){
418  return freg_;
419  }
420 
421  public ColorRegistry getColorRegistry(){
422  return creg_;
423  }
424 
425  public void tryLockFile() throws Exception{
426  try{
427  String localStr = "Start GrxUI:" +
428  dateFormat_.format(new Date()) + System.getProperty("line.separator");
429  lockFile_ = new RandomAccessFile(lockFilePath_,"rwd");
430 
431  if ( lockFile_.getChannel().tryLock() == null){
432  throw new OverlappingFileLockException();
433  }
434  lockFile_.seek(lockFile_.length());
435  lockFile_.write(localStr.getBytes());
436  } catch(Exception eX){
437  eX.printStackTrace();
438  if(lockFile_ != null){
439  lockFile_.close();
440  lockFile_ = null;
441  }
442  throw eX;
443  }
444  }
445 
446  public void releaseLockFile(){
447  try{
448  if(lockFile_ != null){
449  String localStr = "End GrxUI:" +
450  dateFormat_.format(new Date()) + System.getProperty("line.separator");
451  lockFile_.write(localStr.getBytes());
452  lockFile_.close();
453  lockFile_ = null;
454  }
455  } catch(Exception ex){
456  ex.printStackTrace();
457  }
458  }
459 
460  public void breakStart(Exception eX, IPerspectiveDescriptor closeDesc ){
461  //二重起動阻止の処理
462  MessageDialog.openError(
463  Display.getDefault().getActiveShell(),
464  MessageBundle.get("Activator.dialog.title.doubleban"),
465  MessageBundle.get("Activator.dialog.message.doubleban") );
466 
467  IWorkbench work = PlatformUI.getWorkbench();
468  IWorkbenchWindow wnd = work.getActiveWorkbenchWindow();
469  if(wnd == null){
470  //RCP時の処理
471  work.close();
472  System.exit( PlatformUI.RETURN_UNSTARTABLE );
473  } else {
474  IWorkbenchPage page = wnd.getActivePage();
475  if(closeDesc == null){
476  //パースペクティブが既に開いているワークスペースを開始する時
477  closeDesc = page.getPerspective();
478  }
479  page.closePerspective(closeDesc, false, false);
480  for( IPerspectiveDescriptor local : page.getOpenPerspectives()){
481  if( !local.getId().equals(GrxUIPerspectiveFactory.ID)){
482  page.setPerspective(local);
483  break;
484  }
485  }
486  }
487  }
488 
489  static private File initTempDir() {
490  File ret = null;
491 
492  String dir = System.getenv("ROBOT_DIR");
493  if (dir != null && new File(dir).isDirectory()) {
494  ret = new File(dir+File.separator);
495  } else {
496  if ( System.getProperty("os.name").equals("Linux") ||
497  System.getProperty("os.name").equals("Mac OS X")) {
498  ret = new File(LINUX_TMP_DIR);
499  } else { //Windows と 仮定
500  ret = new File(WIN_TMP_DIR);
501  }
502  }
503  if( !ret.exists() ){
504  ret.mkdirs();
505  }
506 
507  return ret;
508  }
509 
510  static private File initHomeDir() {
511  File ret = null;
512 
513  if ( System.getProperty("os.name").equals("Linux") ||
514  System.getProperty("os.name").equals("Mac OS X")) {
515  ret = new File(LINUX_HOME_DIR);
516  } else { //Windows と 仮定
517  ret = new File(WIN_HOME_DIR);
518  }
519  if( !ret.exists() ){
520  ret.mkdirs();
521  }
522 
523  return ret;
524  }
525 
526  public void checkServer() {
527  GrxServerManager server_ = (GrxServerManager) manager_.getItem("serverManager");
528  for(ProcessInfo pi : server_.getServerInfo()){
529  try{
530  GrxCorbaUtil.getReference(pi.id)._non_existent();
531  }catch (Exception ex) {
532  MessageDialog.openError(GrxUIPerspectiveFactory.getCurrentShell(),
533  MessageBundle.get("GrxModelItem.dialog.title.error"), //$NON-NLS-1$
534  pi.id + " " + MessageBundle.get("GrxItem.dialog.message.NoServer") );
535  }
536  }
537 
538  }
539 
540 }
static final String get(String key)
void shutdown()
shutdown this manager
void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective)
#define null
our own NULL pointer
Definition: IceTypes.h:57
GrxBaseItem getItem(Class<?extends GrxBaseItem > cls, String name)
int double double green
Definition: png.h:1628
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
static String nsHost()
get hostname where naming server is running
png_uint_32 i
Definition: png.h:2735
static void setDebugFlag(boolean flag)
static org.omg.CORBA.Object getReference(String id)
get CORBA object which is associated with id
static int nsPort()
get port number where naming server is listening
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
Vector< ProcessInfo > getServerInfo()
vecServerInfoの取得
#define local
Definition: crc32.c:31
org
int double red
Definition: png.h:1628
void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:36