9910

          單飛

             :: 首頁 :: 聯系 :: 聚合  :: 管理

          公告

          Coding is Everything

          常用鏈接

          留言簿(11)

          我參與的團隊

          搜索

          •  

          最新評論

          閱讀排行榜

              public static String convertRGB2HexString(RGB theRGB) {
                  
          int val = (theRGB.red << 16+ (theRGB.green << 8+ theRGB.blue;
                  String result 
          = Integer.toHexString(val);
                  String answer 
          = "000000";
                  answer 
          = answer.substring(06 - result.length()) + result;
                  
          return answer;
              }
          public static RGB convertHexString2RGB(String colorText) {
                  RGB color = null;
                  if (!isNullOrEmpty(colorText))
                      try {
                          int colorValue = Integer.parseInt(colorText, 16);
                          int r = colorValue >> 16 & 0xff;
                          int g = colorValue >> 8 & 0xff;
                          int b = colorValue & 0xff;
                          color = new RGB(r, g, b);
                      } catch (NumberFormatException _ex) {
                      }
                  return color;
              }
          public static String getWindowsSystem32Path() {
                  String path = null;
                  try {
                      String windir = System.getenv("WinDir");
                      if (windir != null && windir.length() > 0)
                          path = windir + "\\system32";
                  } catch (Throwable _ex) {
                  }
                  if (path == null) {
                      path = "C:\\windows\\system32";
                      File windowsFolder = new File(path);
                      if (!windowsFolder.exists())
                          path = "C:\\winnt\\system32";
                  }
                  return path;
              }
              public static void appendToConsole(ILaunch theDebugElement,
                      final String theMessage) {
                  if (theMessage == null || theMessage.length() == 0)
                      return;
                  IConsole console = getConsole(theDebugElement);
                  if (console != null && (console instanceof TextConsole)) {
                      final IDocument consoleDoc = ((TextConsole) console).getDocument();
                      if (consoleDoc != null) {
                          final int length = consoleDoc.getLength();
                          Runnable runnable = new Runnable() {

                              public void run() {
                                  try {
                                      consoleDoc.replace(length, 0, theMessage);
                                  } catch (BadLocationException e) {
                                     
                                  }
                              }

                          };
                          SWTUtil.postOnDisplayQueue(runnable);
                      }
                  }
              }

              public static void postOnDisplayQueue(Runnable runnable) {
                  IWorkbench workbench = PlatformUI.getWorkbench();
                  IWorkbenchWindow windows[] = workbench.getWorkbenchWindows();
                  if (windows != null && windows.length > 0) {
                      Display display = windows[0].getShell().getDisplay();
                      display.asyncExec(runnable);
                  } else {
                      runnable.run();
                  }
              }

          public static boolean isWindowsPlatform() {
                  return Platform.getOS().equals("win32");
              }

              public static IConsole getConsole(ILaunch theLaunch) {
                  return DebugUITools.getConsole(theLaunch.getProcesses()[0]);
              }
              public static void reselectProject(IWorkbenchPart part, IAction action,
                      ISelection selection) {
                  if ((action instanceof ObjectPluginAction)
                          && (part instanceof ISetSelectionTarget) && selection != null)
                      ((ISetSelectionTarget) part).selectReveal(selection);
              }

          public static boolean showPreferencePage(Shell shell, String id,
                      IPreferencePage page) {
                  final IPreferenceNode targetNode = new PreferenceNode(id, page);
                  PreferenceManager manager = new PreferenceManager();
                  manager.addToRoot(targetNode);
                  final PreferenceDialog dialog = new PreferenceDialog(shell, manager);
                  final boolean result[] = new boolean[1];
                  BusyIndicator.showWhile(shell.getDisplay(), new Runnable() {

                      public void run() {
                          dialog.create();
                          dialog.setMessage(targetNode.getLabelText());
                          result[0] = dialog.open() == 0;
                      }

                  });
                  return result[0];
              }


          public static ImageDescriptor getEditorImageDescriptor(String theFilename) {
                  if (isNullOrEmpty(theFilename)) {
                      return null;
                  } else {
                      ImageDescriptor imgDescriptor = getWorkbench().getEditorRegistry()
                              .getImageDescriptor(theFilename.trim());
                      return imgDescriptor;
                  }
              }
              public static void openView(final String theViewId) {
                  SWTUtil.invokeOnDisplayThread(new Runnable() {

                      public void run() {
                          try {
                              IWorkbenchPage activePage = Util.getActivePage();
                              activePage.showView(theViewId);
                          } catch (Exception ex) {
                              CorePlugin.log("Unable to open view " + theViewId, ex);
                          }
                      }

                  });
              }

              public static void invokeOnDisplayThread(Runnable runnable) {
                  IWorkbench workbench = PlatformUI.getWorkbench();
                  IWorkbenchWindow windows[] = workbench.getWorkbenchWindows();
                  if (windows != null && windows.length > 0) {
                      Display display = windows[0].getShell().getDisplay();
                      display.syncExec(runnable);
                  } else {
                      runnable.run();
                  }
              }

              public static boolean openLink(String url) {
                  if (isWindowsPlatform() || isMacPlatform())
                      return Program.launch(url);
                  try {
                      Runtime.getRuntime().exec("mozilla " + url);
                  } catch (IOException _ex) {
                      return false;
                  }
                  return true;
              }



              public static void openPropertiesView() {
                  openView("org.eclipse.ui.views.PropertySheet");
              }
              public static Color getColor(RGB rgb) {
                  if (rgb == null)
                      return null;
                  Color color = JFaceResources.getColorRegistry().get(rgb.toString());
                  if (color == null) {
                      JFaceResources.getColorRegistry().put(rgb.toString(), rgb);
                      color = JFaceResources.getColorRegistry().get(rgb.toString());
                  }
                  return color;
              }

              public static boolean hasBuilder(IProjectDescription theProjectDescription,
                      String theBuilderId) {
                  if (theProjectDescription == null || isNullOrEmpty(theBuilderId))
                      return false;
                  boolean result = false;
                  ICommand commands[] = theProjectDescription.getBuildSpec();
                  for (int i = 0; i < commands.length && !result; i++)
                      result = commands[i].getBuilderName().equals(theBuilderId);

                  return result;
              }

              public static boolean hasBuilder(IProject theProject, String theBuilderId) {
                  if (theProject == null || isNullOrEmpty(theBuilderId))
                      return false;
                  boolean result = false;
                  try {
                      result = hasBuilder(theProject.getDescription(), theBuilderId);
                  } catch (CoreException _ex) {
                  }
                  return result;
              }

              public static FilenameFilter getTldFilenameFilter() {
                  FilenameFilter filter = new FilenameFilter() {

                      public boolean accept(File theDir, String theName) {
                          return theName.toLowerCase().endsWith("tld");
                      }

                  };
                  return filter;
              }
              public static FilenameFilter getJarZipFilenameFilter() {
                  FilenameFilter filter = new FilenameFilter() {

                      public boolean accept(File theDir, String theName) {
                          return theName.toLowerCase().endsWith("jar")
                                  || theName.toLowerCase().endsWith("zip");
                      }

                  };
                  return filter;
              }
              public static int getButtonWidthHint(Button button) {
                  button.setFont(JFaceResources.getDialogFont());
                  PixelConverter converter = new PixelConverter(button);
                  int widthHint = converter.convertHorizontalDLUsToPixels(61);
                  return Math.max(widthHint, button.computeSize(-1, -1, true).x);
              }

              public static int getButtonHeightHint(Button button) {
                  button.setFont(JFaceResources.getDialogFont());
                  PixelConverter converter = new PixelConverter(button);
                  return converter.convertVerticalDLUsToPixels(14);
              }
          public static Button createPushButton(Composite parent, String label,
                      Image image) {
                  Button button = new Button(parent, 8);
                  button.setFont(parent.getFont());
                  if (image != null)
                      button.setImage(image);
                  if (label != null)
                      button.setText(label);
                  GridData gd = new GridData();
                  button.setLayoutData(gd);
                  return button;
              }

              public static void createVerticalSpacer(Composite comp, int colSpan) {
                  Label label = new Label(comp, 0);
                  GridData gd = new GridData();
                  gd.horizontalSpan = colSpan;
                  label.setLayoutData(gd);
              }
          package com.ceopen.tools.core.util;

          import java.util.ArrayList;

          import org.eclipse.core.resources.ICommand;
          import org.eclipse.core.resources.IProject;
          import org.eclipse.core.resources.IProjectDescription;
          import org.eclipse.core.resources.ResourcesPlugin;
          import org.eclipse.core.runtime.CoreException;
          import org.eclipse.core.runtime.IPath;
          import org.eclipse.core.runtime.IProgressMonitor;

          import com.ceopen.tools.core.CorePlugin;

          public class ProjectUtil {

              public ProjectUtil() {
              }

              public static void addNatures(String addNatureIds[], IProject project,
                      IProgressMonitor monitor) throws CoreException {
                  IProjectDescription description = project.getDescription();
                  String prevIds[] = description.getNatureIds();
                  String newNatures[] = new String[prevIds.length + addNatureIds.length];
                  boolean needToAdd[] = new boolean[addNatureIds.length];
                  for (int i = 0; i < addNatureIds.length; i++)
                      needToAdd[i] = !project.hasNature(addNatureIds[i]);

                  int newNatureIndex = 0;
                  for (int i = 0; i < addNatureIds.length; i++)
                      if (needToAdd[i])
                          newNatures[newNatureIndex++] = addNatureIds[i];

                  for (int i = 0; i < prevIds.length; i++)
                      newNatures[newNatureIndex++] = prevIds[i];

                  String sizedNewNatures[] = new String[newNatureIndex];
                  System.arraycopy(newNatures, 0, sizedNewNatures, 0, newNatureIndex);
                  description.setNatureIds(sizedNewNatures);
                  project.setDescription(description, monitor);
              }

              public static void removeNature(String theNatureId, IProject project,
                      IProgressMonitor monitor) {

                  try {
                      if (!project.hasNature(theNatureId))
                          return;

                      IProjectDescription description = project.getDescription();
                      String prevIds[] = description.getNatureIds();
                      String newNatures[] = new String[prevIds.length - 1];
                      boolean found = false;
                      int i = 0;
                      int j = 0;
                      for (; i < prevIds.length; i++)
                          if (!prevIds[i].equals(theNatureId))
                              newNatures[j++] = prevIds[i];
                          else
                              found = true;

                      if (found) {
                          description.setNatureIds(newNatures);
                          project.setDescription(description, monitor);
                      }
                  } catch (CoreException ex) {
                      CorePlugin.log("Error - ", ex);
                  }
                  return;
              }

              public static ICommand getBuilderCommand(String theBuilderId,
                      IProjectDescription theDescription) throws CoreException {
                  if (theBuilderId == null || theBuilderId.length() == 0
                          || theDescription == null)
                      return null;
                  ICommand commands[] = theDescription.getBuildSpec();
                  for (int i = 0; i < commands.length; i++)
                      if (commands[i].getBuilderName().equals(theBuilderId))
                          return commands[i];

                  return null;
              }

              public static void setBuilderCommand(IProjectDescription theDescription,
                      ICommand theNewCommand) throws CoreException {
                  if (theDescription == null || theNewCommand == null)
                      return;
                  ICommand oldCommands[] = theDescription.getBuildSpec();
                  ICommand oldCommand = getBuilderCommand(theNewCommand.getBuilderName(),
                          theDescription);
                  ICommand newCommands[] = (ICommand[]) null;
                  if (oldCommand == null) {
                      newCommands = new ICommand[oldCommands.length + 1];
                      System
                              .arraycopy(oldCommands, 0, newCommands, 0,
                                      oldCommands.length);
                      newCommands[oldCommands.length] = theNewCommand;
                  } else {
                      int i = 0;
                      for (int max = oldCommands.length; i < max; i++) {
                          if (oldCommands[i] != oldCommand)
                              continue;
                          oldCommands[i] = theNewCommand;
                          break;
                      }

                      newCommands = oldCommands;
                  }
                  theDescription.setBuildSpec(newCommands);
              }

              public static void removeBuilderCommand(IProjectDescription theDescription,
                      String theBuilderName) throws CoreException {
                  if (theDescription == null || theBuilderName == null)
                      return;
                  ICommand oldCommands[] = theDescription.getBuildSpec();
                  ICommand newCommands[] = new ICommand[oldCommands.length - 1];
                  boolean found = false;
                  int i = 0;
                  int j = 0;
                  for (; i < oldCommands.length; i++)
                      if (!oldCommands[i].getBuilderName().equals(theBuilderName))
                          newCommands[j++] = oldCommands[i];
                      else
                          found = true;

                  if (found)
                      theDescription.setBuildSpec(newCommands);
              }

              public static IProject[] getProjects() {
                  return ResourcesPlugin.getWorkspace().getRoot().getProjects();
              }

              public static IProject getProject(String theProjectName) {
                  if (Util.isNullOrEmpty(theProjectName)) {
                      return null;
                  } else {
                      IProject project = ResourcesPlugin.getWorkspace().getRoot()
                              .getProject(theProjectName);
                      return project.exists() ? project : null;
                  }
              }

              public static IProject getProject(IPath thePath) {
                  org.eclipse.core.resources.IResource res = ResourcesPlugin
                          .getWorkspace().getRoot().findMember(thePath);
                  return res == null || !(res instanceof IProject) ? null
                          : (IProject) res;
              }

              public static IProject[] getProjectsWithNature(String theNatureId) {
                  IProject projects[] = getProjects();
                  if (projects.length == 0)
                      return projects;
                  ArrayList list = new ArrayList(projects.length);
                  for (int i = 0; i < projects.length; i++)
                      try {
                          IProject prj = projects[i];
                          boolean hasNature = prj.isAccessible()
                                  && prj.hasNature(theNatureId);
                          if (hasNature)
                              list.add(projects[i]);
                      } catch (CoreException _ex) {
                      }

                  return list.isEmpty() ? new IProject[0] : (IProject[]) list
                          .toArray(new IProject[list.size()]);
              }

              public static IProject[] getProjectsWithoutNature(String theNatureId) {
                  IProject projects[] = getProjects();
                  if (projects.length == 0)
                      return projects;
                  ArrayList list = new ArrayList(projects.length);
                  for (int i = 0; i < projects.length; i++)
                      try {
                          boolean hasNature = projects[i].getProject().hasNature(
                                  theNatureId);
                          if (!hasNature)
                              list.add(projects[i]);
                      } catch (CoreException _ex) {
                      }

                  return list.isEmpty() ? new IProject[0] : (IProject[]) list
                          .toArray(new IProject[list.size()]);
              }

              public static boolean projectHasNature(IProject theProject, String theNature) {
                  boolean result = false;
                  try {
                      result = theProject != null && !Util.isNullOrEmpty(theNature)
                              && theProject.hasNature(theNature);
                  } catch (CoreException _ex) {
                  }
                  return result;
              }
          }
          public static void openView(final String theViewId) {
                  SWTUtil.invokeOnDisplayThread(new Runnable() {

                      public void run() {
                          try {
                              IWorkbenchPage activePage = Util.getActivePage();
                              activePage.showView(theViewId);
                          } catch (Exception ex) {
                              CorePlugin.log("Unable to open view " + theViewId, ex);
                          }
                      }

                  });
              }

              public static void openPropertiesView() {
                  openView("org.eclipse.ui.views.PropertySheet");
              }
          public static String getEditorId(IFile file) {
                  return getEditorId(file, true);
              }

              public static String getEditorId(IFile file,
                      boolean refDefaultTextIfNoEditor) {
                  return getEditorId(file.getName(), refDefaultTextIfNoEditor);
              }

              public static String getEditorId(String fileName) {
                  return getEditorId(fileName, true);
              }

              public static String getEditorId(String fileName,
                      boolean refDefaultTextIfNoEditor) {
                  String result = null;
                  IWorkbench workbench = PlatformUI.getWorkbench();
                  IEditorRegistry editorRegistry = workbench.getEditorRegistry();
                  IEditorDescriptor descriptor = editorRegistry
                          .getDefaultEditor(fileName);
                  if (descriptor != null)
                      result = descriptor.getId();
                  else if (refDefaultTextIfNoEditor)
                      result = "org.eclipse.ui.DefaultTextEditor";
                  return result;
              }

              public static IStatus openEditor(IFile theFile, String theEditorId) {
                  return openEditor(((IEditorInput) (new FileEditorInput(theFile))),
                          theEditorId);
              }

              public static IStatus openEditor(IEditorInput theInput, String theEditorId) {
                  IStatus result = Util.getOKStatus();
                  try {
                      Util.getActivePage().openEditor(theInput, theEditorId);
                  } catch (PartInitException e) {
                      result = new Status(4, "com.eao.studio.core", 0,
                              "Unable to open editor.", e);
                  }
                  return result;
              }

              public static IStatus openDefaultEditor(IFile theFile) {
                  return openEditor(theFile, getEditorId(theFile));
              }

              public static IStatus openDefaultEditor(IEditorInput theInput) {
                  return openEditor(theInput, getEditorId(theInput.getName()));
              }

              public static void saveAllResources() {
                  IWorkbenchWindow windows[] = PlatformUI.getWorkbench()
                          .getWorkbenchWindows();
                  for (int i = 0; i < windows.length; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IWorkbenchPage page = pages[j];
                          IEditorPart editors[] = page.getDirtyEditors();
                          for (int k = 0; k < editors.length; k++) {
                              IEditorPart editor = editors[k];
                              editor.doSave(new NullProgressMonitor());
                          }

                      }

                  }

              }

              public static boolean hasDirtyEditors() {
                  IWorkbenchWindow windows[] = PlatformUI.getWorkbench()
                          .getWorkbenchWindows();
                  for (int i = 0; i < windows.length; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IWorkbenchPage page = pages[j];
                          IEditorPart editors[] = page.getDirtyEditors();
                          if (editors != null && editors.length > 0)
                              return true;
                      }

                  }

                  return false;
              }

              public static IFile getWorkspaceFile(File file) {
                  return getWorkspaceFile(((IPath) (new Path(file.getAbsolutePath()))));
              }

              public static IFile getWorkspaceFile(IPath location) {
                  IWorkspace workspace = ResourcesPlugin.getWorkspace();
                  IFile files[] = workspace.getRoot().findFilesForLocation(location);
                  return files != null && files.length != 0 ? files[0] : null;
              }

              public static IEditorInput createEditorInput(File file) {
                  IFile workspaceFile = getWorkspaceFile(file);
                  if (workspaceFile != null)
                      return new FileEditorInput(workspaceFile);
                  else
                      return null;       
              }

              public static IEditorPart getEditorForInput(IEditorInput editorInput,
                      IWorkbenchPage returnEditorPage[]) {
                  IEditorPart result = null;
                  IWorkbenchWindow windows[] = Util.getWorkbench().getWorkbenchWindows();
                  for (int i = 0; i < windows.length && result == null; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IEditorPart part = pages[j].findEditor(editorInput);
                          if (part == null)
                              continue;
                          result = part;
                          returnEditorPage[0] = pages[j];
                          break;
                      }

                  }

                  return result;
              }

              private static IEditorReference[] getEditorReferencesForInput(
                      IEditorInput input, IWorkbenchPage page[]) {
                  IEditorReference result[] = new IEditorReference[0];
                  IWorkbenchWindow windows[] = Util.getWorkbench().getWorkbenchWindows();
                  for (int i = 0; i < windows.length && result.length == 0; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IEditorReference references[] = pages[j].findEditors(input,
                                  null, 1);
                          if (references.length <= 0)
                              continue;
                          result = references;
                          page[0] = pages[j];
                          break;
                      }

                  }

                  return result;
              }

              public static void closeEditorForInput(final IEditorInput theInput,
                      boolean shouldCloseAsync) {
                  Runnable runnable = new Runnable() {

                      public void run() {
                          IWorkbenchPage page[] = new IWorkbenchPage[1];
                          IEditorPart part = EditorUtil.getEditorForInput(theInput, page);
                          if (page[0] != null && part != null)
                              page[0].closeEditor(part, false);
                      }

                  };
                  if (shouldCloseAsync)
                      SWTUtil.postOnDisplayQueue(runnable);
                  else
                      runnable.run();
              }

              public static IFile pickFile(IContainer container, List selection,
                      ViewerFilter filter) {
                  ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
                          getActiveShell(), new WorkbenchLabelProvider(),
                          new WorkbenchContentProvider());
                  if (filter != null)
                      dialog.addFilter(filter);
                  dialog.setAllowMultiple(false);
                  dialog.setDoubleClickSelects(true);
                  dialog.setInput(container);
                  dialog.setValidator(new SingleFileSelectionValidator());
                  dialog.setInitialElementSelections(selection);
                  IFile file;
                  if (dialog.open() == 0) {
                      Object result[] = dialog.getResult();
                      file = (IFile) result[0];
                  } else {
                      file = null;
                  }
                  return file;
              }

              public static void disableAllControls(Control control) {
                  if (control == null)
                      return;
                  control.setEnabled(false);
                  if (control instanceof Composite) {
                      Composite composite = (Composite) control;
                      Control children[] = composite.getChildren();
                      for (int i = 0; i < children.length; i++) {
                          Control control2 = children[i];
                          disableAllControls(control2);
                      }

                  }
              }

              public static InputStream createStream(Object obj) {
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  try {
                      ObjectOutputStream oos = new ObjectOutputStream(baos);
                      oos.writeObject(obj);
                      oos.close();
                      ByteArrayInputStream bais = new ByteArrayInputStream(baos
                              .toByteArray());
                      return bais;
                  } catch (IOException e) {
                      Logger.error(CorePlugin.getDefault(), e.getMessage(), e);
                  }
                  return new ByteArrayInputStream(new byte[0]);
              }

              public static Properties readProperties(File file) {
                  Properties props = new Properties();
                  InputStream input = null;
                  try {
                      input = new BufferedInputStream(new FileInputStream(file));
                      props.load(input);
                  } catch (Exception _ex) {
                  } finally {
                      if (input != null)
                          try {
                              input.close();
                          } catch (IOException _ex) {
                          }
                  }
                  return props;
              }

              public static void saveProperties(Properties props, File file) {
                  FileOutputStream out = null;
                  try {
                      out = new FileOutputStream(file);
                      props.store(out, null);
                  } catch (Exception _ex) {

                  } finally {
                      if (out != null) {
                          try {
                              out.close();
                          } catch (IOException e) {

                          }
                      }
                  }

              }

              public static String convertClassname2FilePath(String theJavaClassname,
                      boolean keepExtension) {
                  new StringBuffer(theJavaClassname.length() + 10);
                  int extSeparatorIdx = theJavaClassname.lastIndexOf('.');
                  char chars[] = theJavaClassname.toCharArray();
                  for (int i = 0; i < extSeparatorIdx - 1; i++) {
                      char ch = chars[i];
                      if (ch == '.')
                          chars[i] = '/';
                  }

                  int endCharIdx = keepExtension ? theJavaClassname.length()
                          : extSeparatorIdx;
                  return String.valueOf(chars, 0, endCharIdx);
              }
          public static void transferStreams(InputStream source,
                      OutputStream destination) throws IOException {
                  if (source == null || destination == null)
                      return;
                  byte buffer[] = new byte[8192];
                  int bytesRead = source.read(buffer);
                  if (bytesRead == -1)
                      return;
                  destination.write(buffer, 0, bytesRead);

                  try {
                      source.close();
                  } catch (IOException _ex) {
                  }
                  try {
                      destination.close();
                  } catch (IOException _ex) {
                  }
                  return;
              }
              public static String toUpperFirst(String var) {
                  char[] arrays = var.toCharArray();
                  char first = arrays[0];
                  char c = String.valueOf(first).toUpperCase().toCharArray()[0];
                  arrays[0] = c;
                  return String.valueOf(arrays);
              }
              public static String toLowerFirst(String var) {
                  char[] arrays = var.toCharArray();
                  char first = arrays[0];
                  char c = String.valueOf(first).toLowerCase().toCharArray()[0];
                  arrays[0] = c;
                  return String.valueOf(arrays);
              }
              public static String[] split(String str) {
                  return str.split("\\.");
              }
          public static String replaceAllString(String src, String from, String to) {
                  return replaceAllString(src, from, to, -1);
              }

              public static String replaceAllString(String s, String s1, String s2, int i) {
                  if (s == null || s.length() <= 0)
                      return s;
                  StringBuffer stringbuffer = new StringBuffer();
                  if (i < 0) {
                      int j;
                      while ((j = s.indexOf(s1)) >= 0) {
                          stringbuffer.append(s.substring(0, j));
                          stringbuffer.append(s2);
                          s = s.substring(j + s1.length());
                      }
                      stringbuffer.append(s);
                  } else {
                      int k = 0;
                      int l;
                      while ((l = s.indexOf(s1)) >= 0) {
                          k++;
                          stringbuffer.append(s.substring(0, l));
                          if (k == i)
                              stringbuffer.append(s2);
                          else
                              stringbuffer.append(s1);
                          s = s.substring(l + s1.length());
                      }
                      stringbuffer.append(s);
                  }
                  return stringbuffer.toString();
              }
          protected void openResource(final IFile resource) {
                  final IWorkbenchPage activePage = JavaPlugin.getActivePage();
                  if (activePage != null) {
                      Display display = getShell().getDisplay();
                      if (display != null)
                          display.asyncExec(new Runnable() {

                              public void run() {
                                  try {
                                      IDE.openEditor(activePage, resource, true);
                                  } catch (PartInitException e) {
                                      JavaPlugin.log(e);
                                  }
                              }

                          });
                  }
              }
          public static void reparent(Control child, Composite newParent) {
                  child.setParent(newParent);
                  newParent.layout(true);
                  newParent.redraw();
              }
              public static void saveAllResources() {
                  IWorkbenchWindow windows[] = PlatformUI.getWorkbench().getWorkbenchWindows();
                  for (int i = 0; i < windows.length; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IWorkbenchPage page = pages[j];
                          IEditorPart editors[] = page.getDirtyEditors();
                          for (int k = 0; k < editors.length; k++) {
                              IEditorPart editor = editors[k];
                              editor.doSave(new NullProgressMonitor());
                          }

                      }

                  }

              }
              public static boolean hasDirtyEditors() {
                  IWorkbenchWindow windows[] = PlatformUI.getWorkbench().getWorkbenchWindows();
                  for (int i = 0; i < windows.length; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IWorkbenchPage page = pages[j];
                          IEditorPart editors[] = page.getDirtyEditors();
                          if (editors != null && editors.length > 0)
                              return true;
                      }

                  }

                  return false;
              }
          public static IEditorInput createEditorInput(File file) {
                  IFile workspaceFile = getWorkspaceFile(file);
                  if (workspaceFile != null)
                      return new FileEditorInput(workspaceFile);
                  else
                      return null;
              }
          public static IFile getWorkspaceFile(File file) {
                  return getWorkspaceFile(((IPath) (new Path(file.getAbsolutePath()))));
              }
              public static IFile getWorkspaceFile(IPath location) {
                  IWorkspace workspace = ResourcesPlugin.getWorkspace();
                  IFile files[] = workspace.getRoot().findFilesForLocation(location);
                  return files != null && files.length != 0 ? files[0] : null;
              }
              public static void closeEditorForInput(final IEditorInput theInput, boolean shouldCloseAsync) {
                  Runnable runnable = new Runnable() {

                      public void run() {
                          IWorkbenchPage page[] = new IWorkbenchPage[1];
                          IEditorPart part = EditorUtil.getEditorForInput(theInput, page);
                          if (page[0] != null && part != null)
                              page[0].closeEditor(part, false);
                      }

                  };
                  if (shouldCloseAsync)
                      UIUtil.postOnDisplayQueue(runnable);
                  else
                      runnable.run();
              }

              private static IEditorReference[] getEditorReferencesForInput(IEditorInput input, IWorkbenchPage page[]) {
                  IEditorReference result[] = new IEditorReference[0];
                  IWorkbenchWindow windows[] = Util.getWorkbench().getWorkbenchWindows();
                  for (int i = 0; i < windows.length && result.length == 0; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IEditorReference references[] = pages[j].findEditors(input, null, 1);
                          if (references.length <= 0)
                              continue;
                          result = references;
                          page[0] = pages[j];
                          break;
                      }

                  }

                  return result;
              }

              public static IEditorPart getEditorForInput(IEditorInput editorInput, IWorkbenchPage returnEditorPage[]) {
                  IEditorPart result = null;
                  IWorkbenchWindow windows[] = Util.getWorkbench().getWorkbenchWindows();
                  for (int i = 0; i < windows.length && result == null; i++) {
                      IWorkbenchPage pages[] = windows[i].getPages();
                      for (int j = 0; j < pages.length; j++) {
                          IEditorPart part = pages[j].findEditor(editorInput);
                          if (part == null)
                              continue;
                          result = part;
                          returnEditorPage[0] = pages[j];
                          break;
                      }

                  }

                  return result;
              }
          public static void copyFile(String source_name, String dest_name) throws Exception {
                  File source_file = new File(source_name);
                  File destination_file = new File(dest_name);
                  FileInputStream source = null;
                  FileOutputStream destination = null;
                  try {
                      if (!source_file.exists() || !source_file.isFile())
                          throw new Exception("copyFile: no such source file: " + source_name);
                      if (!source_file.canRead())
                          throw new Exception("copyFile: source file is unreadable: " + source_name);
                      source = new FileInputStream(source_file);
                      destination = new FileOutputStream(destination_file);
                      byte buffer[] = new byte[1024];
                      do {
                          int bytes_read = source.read(buffer);
                          if (bytes_read == -1)
                              break;
                          destination.write(buffer, 0, bytes_read);
                          destination.flush();
                      } while (true);
                  } catch (Exception e) {
                      throw new Exception(e);
                  } finally {
                      if (source != null)
                          try {
                              source.close();
                          } catch (Exception _ex) {
                          }
                      if (destination != null)
                          try {
                              destination.close();
                          } catch (Exception _ex) {
                          }
                  }
                  return;
              }
              public static boolean createFile(String strFullPath) throws Exception {
                  try {
                      if (strFullPath == null)
                          return false;
                      if (fileExisted(strFullPath))
                          return false;
                      File file = new File(strFullPath);
                      File parent = file.getParentFile();
                      if (!parent.exists())
                          parent.mkdirs();
                      return file.createNewFile();
                  } catch (Exception e) {
                      throw new Exception(e);
                  }
              }


          posted on 2009-03-18 15:02 單飛 閱讀(404) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 乌兰察布市| 长汀县| 丹棱县| 新晃| 股票| 寻乌县| 襄汾县| 无锡市| 长武县| 碌曲县| 砚山县| 巢湖市| 富锦市| 舒城县| 铜陵市| 阿鲁科尔沁旗| 遂平县| 文登市| 徐汇区| 高清| 东兰县| 镇安县| 伊宁县| 长宁县| 德安县| 凉山| 衡阳市| 通道| 乐陵市| 巴青县| 宁都县| 冀州市| 平阳县| 华蓥市| 昭苏县| 嘉荫县| 永德县| 合作市| 伊通| 本溪| 若羌县|