/tarlog-plugins/src/tarlog/eclipse/plugins/openwe/OpenCommandPrompt.java

http://tarlog-plugins.googlecode.com/ · Java · 74 lines · 47 code · 9 blank · 18 comment · 7 complexity · 94b6dce014e468974e9636350f842927 MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright 2008,2010 Michael Elman (aka tarlog - http://tarlogonjava.blogspot.com)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. package tarlog.eclipse.plugins.openwe;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.text.MessageFormat;
  20. import org.eclipse.jface.preference.IPreferenceStore;
  21. import org.eclipse.jface.util.IPropertyChangeListener;
  22. import org.eclipse.jface.util.PropertyChangeEvent;
  23. import tarlog.eclipse.plugins.Activator;
  24. import tarlog.eclipse.plugins.preferences.TarlogPluginsPreferencePage;
  25. /**
  26. * @author elman
  27. */
  28. public class OpenCommandPrompt extends TreeSelectionAction implements IPropertyChangeListener {
  29. private String command = null;
  30. public OpenCommandPrompt() {
  31. super();
  32. IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
  33. command = preferenceStore.getString(TarlogPluginsPreferencePage.RUN_SHELL);
  34. preferenceStore.addPropertyChangeListener(this);
  35. }
  36. @Override
  37. protected void doAction(String path) {
  38. try {
  39. File file = new File(path);
  40. if (file.isFile()) {
  41. File parentFile = file.getParentFile();
  42. if (parentFile != null) {
  43. path = parentFile.getAbsolutePath();
  44. }
  45. }
  46. if (command.indexOf("{0}") >= 0) {
  47. Runtime.getRuntime().exec(MessageFormat.format(command, path));
  48. } else {
  49. Runtime.getRuntime().exec(command, null, new File(path));
  50. }
  51. }
  52. catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. @Override
  57. public void propertyChange(PropertyChangeEvent event) {
  58. if (event.getProperty().equals(TarlogPluginsPreferencePage.RUN_SHELL)) {
  59. Object newValue = event.getNewValue();
  60. if (newValue instanceof String) {
  61. command = (String) newValue;
  62. }
  63. }
  64. }
  65. }