PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/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 *******************************************************************************/
16package tarlog.eclipse.plugins.openwe;
17
18import java.io.File;
19import java.io.IOException;
20import java.text.MessageFormat;
21
22import org.eclipse.jface.preference.IPreferenceStore;
23import org.eclipse.jface.util.IPropertyChangeListener;
24import org.eclipse.jface.util.PropertyChangeEvent;
25
26import tarlog.eclipse.plugins.Activator;
27import tarlog.eclipse.plugins.preferences.TarlogPluginsPreferencePage;
28
29/**
30 * @author elman
31 */
32public class OpenCommandPrompt extends TreeSelectionAction implements IPropertyChangeListener {
33
34    private String command = null;
35
36    public OpenCommandPrompt() {
37        super();
38        IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
39        command = preferenceStore.getString(TarlogPluginsPreferencePage.RUN_SHELL);
40        preferenceStore.addPropertyChangeListener(this);
41    }
42
43    @Override
44    protected void doAction(String path) {
45        try {
46            File file = new File(path);
47            if (file.isFile()) {
48                File parentFile = file.getParentFile();
49                if (parentFile != null) {
50                    path = parentFile.getAbsolutePath();
51                }
52            }
53            if (command.indexOf("{0}") >= 0) {
54                Runtime.getRuntime().exec(MessageFormat.format(command, path));
55            } else {
56                Runtime.getRuntime().exec(command, null, new File(path));
57            }
58        }
59        catch (IOException e) {
60            e.printStackTrace();
61        }
62    }
63
64    @Override
65    public void propertyChange(PropertyChangeEvent event) {
66        if (event.getProperty().equals(TarlogPluginsPreferencePage.RUN_SHELL)) {
67            Object newValue = event.getNewValue();
68            if (newValue instanceof String) {
69                command = (String) newValue;
70            }
71        }
72    }
73
74}