學無止境  
          日歷
          <2005年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678
          統計
          • 隨筆 - 9
          • 文章 - 0
          • 評論 - 2
          • 引用 - 0

          導航

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          2005年9月2日

          You have implemented your own graphical editor as a new eclipse plugin. You are looking for a way to add zoom functions into the graphical editor. It is very easy!
          1. Add the action into the toolbar:
          ...in Class: ActionBarContributor...
              ...in Method: contributeToToolBar...
                  toolBarManager.add(getAction(GEFActionConstants.ZOOM_IN));
                  toolBarManager.add(getAction(GEFActionConstants.ZOOM_OUT));
                  toolBarManager.add(new ZoomComboContributionItem(getPage()));

          2. Connect the Zoom Manager with your edit part:
          ...in Class: YourEditor...
              ...in Method: configureGraphicalViewer...
                  ScalableFreeformRootEditPart rootEditPart= new ScalableFreeformRootEditPart();
                  getGraphicalViewer().setRootEditPart(rootEditPart);

                  ZoomManager manager = rootEditPart.getZoomManager();
                  IAction action = new ZoomInAction(manager);
                  getActionRegistry().registerAction(action);
                  action = new ZoomOutAction(manager);
                  getActionRegistry().registerAction(action);

                  //define the zoom possibilities
                  double[] zoomLevels = new double[] {0.25,0.5,0.75,1.0,1.5,2.0,2.5,3.0,4.0};
                  manager.setZoomLevels(zoomLevels);
             ...in Method: getAdapter...
                  if (type == ZoomManager.class)
                        return ((ScalableFreeformRootEditPart) getGraphicalViewer().
                                  getRootEditPart()).getZoomManager();

          That's all! Now you have two buttons to zoom in and zoom out. And you can see the zoom level in percent.  With the example here you  can change  zoom level from 25% to 400%.

          posted @ 2005-10-25 23:38 lucia 閱讀(1378) | 評論 (0)編輯 收藏
           
          There is an example of creating linked resources in code.
          IProject project = workspace.getProject("Project");//assume this exists
          IFolder link = project.getFolder("Link");
          IPath location = new Path("C:\TEMP\folder");
          if (workspace.validateLinkLocation(location).isOK()) {
          link.createLink(location, IResource.NONE, null);
          } else {
          //invalid location, throw an exception or warn user
          }

          This example will create a linked folder called "Link" that is located at "c:\temp\folder".

          link.getFullPath() => "/Project/Link"
          link.getLocation() => "c:\temp\folder"
          link.getRawLocation() => "temp/folder"
          link.isLinked() => "true"
          IFile child = link.getFile("abc.txt");
          child.create(...);
          child.getFullPath() => "/Project/Link/abc.txt"
          child.getLocation() => "c:\temp\folder\abc.txt"
          child.getRawLocation() => "c:\temp\folder\abc.txt"
          child.isLinked() => "false"

          posted @ 2005-10-17 05:25 lucia 閱讀(843) | 評論 (0)編輯 收藏
           
          Recently I had a problem in converting java.io.File to IFile for external files. The external files sind the files which are not located in the eclipse workspace. So they are not eclipse "resources" and can not be converted into IFile instances. With the following code you can solve this problem:

                           IEditorInput input= createEditorInput(selectedFile);
                           String editorId= getEditorId(selectedFile);
                           IWorkbenchPage page= fWindow.getActivePage();
                           try {
                               page.openEditor(input, editorId);
                           } catch (PartInitException e) {
                               //EditorsPlugin.log(e.getStatus());
                           }

          This idea comes from org.eclipse.ui.internal.editors.text.OpenExternalFileAction. In this class you can also find the implementation for the methods "createEditorInput" and "getEditorId".
          posted @ 2005-09-21 17:50 lucia 閱讀(586) | 評論 (0)編輯 收藏
           
          In JDK 1.4 System.getenv() is deprecated. (In JDK 1.5 it is un-deprected again.) The getProperty method is now the correspoding method to get the variables. For example: System.getProperty("java.class.path",""). But it doesn't work for user defined environment variable. This problem can be solved by using the following code:
          import java.io.BufferedReader;
          import java.io.InputStreamReader;

          /**
          * Environment class simulates the System.getenv() method which is deprecated
          * on java 1.4.2
          *
          * @author v-josp
          *
          */
          public class TestRoot
          {
          //result of all enviornment variables
          private static BufferedReader commandResult;

          static
          {
          String cmd = null;
          String os = null;

          //getting the OS name
          os = System.getProperty("os.name").toLowerCase();

          // according to OS set the command to execute
          if(os.startsWith("windows"))
          {
          cmd = "cmd /c SET";
          }
          else
          {
          cmd="env";
          }

          try
          {
          //execute the command and get the result in the form of InputStream
          Process p = Runtime.getRuntime().exec(cmd);

          //parse the InputStream data
          InputStreamReader isr = new InputStreamReader(p.getInputStream());
          commandResult= new BufferedReader(isr);
          }
          catch (Exception e)
          {
          System.out.println("OSEnvironment.class error: " + cmd + ":" + e);
          }
          }

          /**
          * This method is used to get the path of the given enviornment variable. This
          * method tries to simulates the System.getenv() which is deprecated on java 1.4.2
          *
          * @param String - name of the environment variable
          * @param String - default value
          * @return
          */
          public static String getenv(String envName,String defaultValue)
          {
          String line = null;
          try
          {
          while ((line = commandResult.readLine()) != null)
          {
          if(line.indexOf(envName)>-1)
          {
          return line.substring(line.indexOf(envName)+envName.length()+1);
          }
          }
          return defaultValue;
          }
          catch (Exception e)
          {
          return defaultValue;
          }
          }

          public static void main(String args[])
          {
          System.out.println(TestRoot.getenv("CLASSPATH",""));
          }
          }

          Output
          _____
          F:\software\javaws-1_2-dev\jnlp.jar;
          posted @ 2005-09-14 02:36 lucia 閱讀(669) | 評論 (0)編輯 收藏
           
          try {
          File currDir = new File (".");
          System.out.println("Dir: " + currDir.getCanonicalPath());
          }
          catch (IOException ex){
          //
          }


          posted @ 2005-09-10 22:12 lucia 閱讀(325) | 評論 (0)編輯 收藏
           
          I will use one simple example to explain how to add context menu into editor dynamically. "Dynamically" means the menu items will change according to the selected object.
          There are three steps:

          1. Define the action. Please read the following example code:

          /**
           *  What I want to realize is to provide a "copy" menu item in popup menu
           *  whenever the user selects the object "Example".
           */

          public class CopyExampleAction extends org.eclipse.gef.ui.actions.SelectionAction
          {
              private static final String
                  COPY_REQUEST = "COPY";  //$NON-NLS-1$
              public static final String
                  COPY = "COPY";   //$NON-NLS-1$

              Request request;

              public CopyExampleAction(IWorkbenchPart part) {
                  super(part);
                  request = new Request(COPY_REQUEST);
                  setText("copy Example");
                  setId(COPY);
                  setImageDescriptor(
                      ImageDescriptor.createFromFile(ExamplePlugin.class,"icons/copy.gif")); //$NON-NLS-1$
                  setHoverImageDescriptor(getImageDescriptor());
              }

              protected boolean calculateEnabled() {
                  return canPerformAction();
              }

              private boolean canPerformAction() {
                  if (getSelectedObjects().isEmpty())
                      return false;
                  List parts = getSelectedObjects();
                  for (int i=0; i<parts.size(); i++){
                      Object o = parts.get(i);
                      if (!(o instanceof EditPart))
                          return false;
                      EditPart part = (EditPart)o;
                     // This menu item will only be activated if an object of Typ "Example" is selected.
                      if (!(part.getModel() instanceof Example))
                          return false;
                      }
                  }
                  return true;
              }

          // What happened for this action is defined by the edit part of this object.
              private Command getCommand() {
                  List editparts = getSelectedObjects();
                  CompoundCommand cc = new CompoundCommand();
                  cc.setDebugLabel("Copy Example");//$NON-NLS-1$
                  for (int i=0; i < editparts.size(); i++) {
                      EditPart part = (EditPart)editparts.get(i);
                      cc.add(part.getCommand(request));
                  }
                  return cc;
              }

              public void run() {
                  execute(getCommand());
              }
              }

          2. Implement the action. In this step you will decide what will exactly happen after the user clicked this menu item. As you have already seen from the code in the step 1, the command for this action is defined in edit part for the object "Example".
          So the next you will do is to complete the ExampleEditPolicy for ExampleEditPart.

          public class ExampleEditPolicy extends ComponentEditPolicy{
            
              private static final String
              COPY_REQUEST = "COPY",  //$NON-NLS-1$

          public Command getCommand(Request request) {
              if (COPY_REQUEST.equals(request.getType()))
                  return getCopyExampleCommand();
              return super.getCommand(request);
          }

          protected Command getCopyExampleCommand(){
           // The implementation of CopyExampleCommand is simple, will not be explained here.
              CopyExampleCommand command = new CopyExampleCommand();
              command.setCopyObject((Example)getHost().getModel());
              return command;
          }
          }

          3. Add the action into Context Menu.
          There is always a class to provider the context menu, the next step is to add this defined action into the context menu.

          public class ExampleContextMenuProvider extends ContextMenuProvider {
            
              private ActionRegistry actionRegistry;
             
              public ExampleContextMenuProvider(EditPartViewer viewer, ActionRegistry registry) {
                  super(viewer);
                  setActionRegistry(registry);
              }

              public void buildContextMenu(IMenuManager manager) {
                  GEFActionConstants.addStandardActionGroups(manager);
                  IAction action;
                  action = getActionRegistry().getAction(CopyExampleAction.COPY);
                  if (action.isEnabled())
                      manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
              }   

              private ActionRegistry getActionRegistry() {
                  return actionRegistry;
              }

              private void setActionRegistry(ActionRegistry registry) {
                  actionRegistry = registry;
              }
          }

          4. Add the action into editor. There is a method "createActions" in editor. Add the action in it. That's all.

          protected void createActions() {
            super.createActions();
            ActionRegistry registry = getActionRegistry();
            IAction action;

            action = new CopyExampleAction(this);
            registry.registerAction(action);
            getSelectionActions().add(action.getId());
          }

          posted @ 2005-09-07 17:07 lucia 閱讀(687) | 評論 (0)編輯 收藏
           
          Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)
          posted @ 2005-09-02 20:31 lucia 閱讀(228) | 評論 (0)編輯 收藏
           
          Copyright © lucia Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 三原县| 大化| 平顺县| 衡阳市| 安岳县| 鄄城县| 琼中| 安徽省| 楚雄市| 张掖市| 穆棱市| 鄂州市| 岳西县| 子洲县| 繁峙县| 安庆市| 金湖县| 高唐县| 新余市| 奉化市| 沧源| 哈巴河县| 宁夏| 克山县| 射阳县| 柏乡县| 宣威市| 葫芦岛市| 庆安县| 错那县| 松原市| 钦州市| 南皮县| 梁河县| 惠安县| 长寿区| 镇宁| 卓尼县| 乌兰浩特市| 玛多县| 长兴县|